1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 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, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include <libpspp/str.h>
27 #include <libpspp/ll.h>
28 #include <libpspp/version.h>
29 #include <libpspp/alloc.h>
31 #include <data/file-name.h>
35 struct getl_source *included_from; /* File that this is nested inside. */
36 struct getl_source *includes; /* File nested inside this file. */
38 struct ll ll; /* Element in the sources list */
40 struct getl_interface *interface;
43 /* List of source files. */
44 static struct ll_list sources ;
46 static struct string the_include_path;
49 getl_include_path (void)
51 return ds_cstr (&the_include_path);
54 static struct getl_source *
55 current_source (struct ll_list *list)
57 const struct ll *ll = ll_head (list);
58 return ll_data (ll, struct getl_source, ll );
61 /* Initialize getl. */
63 getl_initialize (void)
66 ds_init_cstr (&the_include_path,
67 fn_getenv_default ("STAT_INCLUDE_PATH", include_path));
70 /* Delete everything from the include path. */
72 getl_clear_include_path (void)
74 ds_clear (&the_include_path);
77 /* Add to the include path. */
79 getl_add_include_dir (const char *path)
81 if (ds_length (&the_include_path))
82 ds_put_char (&the_include_path, ':');
84 ds_put_cstr (&the_include_path, path);
87 /* Appends source S to the list of source files. */
89 getl_append_source (struct getl_interface *i)
91 struct getl_source *s = xzalloc (sizeof ( struct getl_source ));
95 ll_push_head (&sources, &s->ll);
98 /* Nests source S within the current source file. */
100 getl_include_source (struct getl_interface *i)
102 struct getl_source *current = current_source (&sources);
103 struct getl_source *s = xzalloc (sizeof ( struct getl_source ));
107 s->included_from = current ;
109 current->includes = s;
111 ll_push_head (&sources, &s->ll);
114 /* Closes the current source, and move the current source to the
115 next file in the chain. */
119 struct getl_source *s = current_source (&sources);
121 if ( s->interface->close )
122 s->interface->close (s->interface);
124 ll_pop_head (&sources);
126 if (s->included_from != NULL)
127 current_source (&sources)->includes = NULL;
132 /* Closes all sources until an interactive source is
135 getl_abort_noninteractive (void)
137 while ( ! ll_is_empty (&sources))
139 const struct getl_source *s = current_source (&sources);
141 if ( !s->interface->interactive (s->interface) )
146 /* Returns true if the current source is interactive,
149 getl_is_interactive (void)
151 const struct getl_source *s = current_source (&sources);
153 if (ll_is_empty (&sources) )
156 return s->interface->interactive (s->interface);
159 /* Returns the name of the current source, or NULL if there is no
162 getl_source_name (void)
164 const struct getl_source *s = current_source (&sources);
166 if ( ll_is_empty (&sources) )
169 if ( ! s->interface->name )
172 return s->interface->name (s->interface);
175 /* Returns the location within the current source, or -1 if there is
178 getl_source_location (void)
180 const struct getl_source *s = current_source (&sources);
182 if ( ll_is_empty (&sources) )
185 if ( !s->interface->location )
188 return s->interface->location (s->interface);
194 getl_uninitialize (void)
196 while ( !ll_is_empty (&sources))
198 ds_destroy (&the_include_path);
202 /* Reads a single line into LINE.
203 Returns true when a line has been read, false at end of input.
204 If INTERACTIVE is non-null, then when true is returned
205 *INTERACTIVE will be set to true if the line was obtained
206 interactively, false otherwise. */
208 do_read_line (struct string *line, bool *interactive)
210 while (!ll_is_empty (&sources))
212 struct getl_source *s = current_source (&sources);
215 if (s->interface->read (s->interface, line))
217 if (interactive != NULL)
218 *interactive = s->interface->interactive (s->interface);
222 if (s->interface->filter)
223 s->interface->filter (s->interface, line);
224 s = s->included_from;