2 PSPP - a program for statistical analysis.
3 Copyright (C) 2017 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "language/control/control-stack.h"
26 #include "libpspp/compiler.h"
27 #include "libpspp/message.h"
29 #include "gl/xalloc.h"
32 #define _(msgid) gettext (msgid)
36 const struct ctl_class *class; /* Class of control structure. */
37 struct ctl_struct *down; /* Points toward the bottom of ctl_stack. */
38 void *private; /* Private data. */
41 static struct ctl_struct *ctl_stack;
44 ctl_stack_clear (void)
46 while (ctl_stack != NULL)
48 struct ctl_struct *top = ctl_stack;
49 msg (SE, _("%s without %s."),
50 top->class->start_name, top->class->end_name);
51 ctl_stack_pop (top->private);
56 ctl_stack_push (const struct ctl_class *class, void *private)
58 struct ctl_struct *ctl;
60 assert (private != NULL);
61 ctl = xmalloc (sizeof *ctl);
63 ctl->down = ctl_stack;
64 ctl->private = private;
69 ctl_stack_top (const struct ctl_class *class)
71 struct ctl_struct *top = ctl_stack;
72 if (top != NULL && top->class == class)
76 if (ctl_stack_search (class) != NULL)
77 msg (SE, _("This command must appear inside %s...%s, "
78 "without intermediate %s...%s."),
79 class->start_name, class->end_name,
80 top->class->start_name, top->class->end_name);
86 ctl_stack_search (const struct ctl_class *class)
88 struct ctl_struct *ctl;
90 for (ctl = ctl_stack; ctl != NULL; ctl = ctl->down)
91 if (ctl->class == class)
94 msg (SE, _("This command cannot appear outside %s...%s."),
95 class->start_name, class->end_name);
100 ctl_stack_pop (void *private)
102 struct ctl_struct *top = ctl_stack;
104 assert (top != NULL);
105 assert (top->private == private);
107 top->class->close (top->private);
108 ctl_stack = top->down;
113 ctl_stack_is_empty (void)
115 return ctl_stack == NULL;