summaryrefslogtreecommitdiff
path: root/pop.c
diff options
context:
space:
mode:
Diffstat (limited to 'pop.c')
-rw-r--r--pop.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/pop.c b/pop.c
new file mode 100644
index 0000000..99ed3d1
--- /dev/null
+++ b/pop.c
@@ -0,0 +1,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;
+}