summaryrefslogtreecommitdiff
path: root/pop.c
blob: 99ed3d1f04b373f28cddd68090666327b25fd4f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "prv.h"
#include <stdlib.h>

int pop(const struct lex *l, struct prv *p)
{
    size_t n;
    struct pos *npos = NULL;

    if (!p->n)
    {
        fprintf(stderr, "%s: underflow\n", __func__);
        return -1;
    }
    else if (!(n = p->n - 1))
    {
        free(p->pos);
        p->pos = NULL;
    }
    else if (!(npos = realloc(p->pos, n * sizeof *npos)))
    {
        perror("realloc(3)");
        return -1;
    }

    p->pos = npos;
    p->n = n;

    if (p->n)
    {
        const struct pos *old = &p->pos[--p->i];

        if (old->seq->end && old->seq->end(l, p))
            return -1;
    }

    return 0;
}