1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include <libpspp/str.h>
25 #include <libpspp/ll.h>
26 #include <libpspp/version.h>
27 #include <libpspp/alloc.h>
31 struct getl_source *included_from; /* File that this is nested inside. */
32 struct getl_source *includes; /* File nested inside this file. */
34 struct ll ll; /* Element in the sources list */
36 struct getl_interface *interface;
41 struct ll_list sources ; /* List of source files. */
43 struct string the_include_path;
47 getl_include_path (const struct source_stream *ss)
49 return ds_cstr (&ss->the_include_path);
52 static struct getl_source *
53 current_source (const struct source_stream *ss)
55 const struct ll *ll = ll_head (&ss->sources);
56 return ll_data (ll, struct getl_source, ll );
59 /* Initialize getl. */
60 struct source_stream *
61 create_source_stream (const char *initial_include_path)
63 struct source_stream *ss = xzalloc (sizeof (*ss));
64 ll_init (&ss->sources);
66 ds_init_cstr (&ss->the_include_path,
67 fn_getenv_default ("STAT_INCLUDE_PATH", include_path));
69 ds_init_cstr (&ss->the_include_path, initial_include_path);
74 /* Delete everything from the include path. */
76 getl_clear_include_path (struct source_stream *ss)
78 ds_clear (&ss->the_include_path);
81 /* Add to the include path. */
83 getl_add_include_dir (struct source_stream *ss, const char *path)
85 if (ds_length (&ss->the_include_path))
86 ds_put_char (&ss->the_include_path, ':');
88 ds_put_cstr (&ss->the_include_path, path);
91 /* Appends source S to the list of source files. */
93 getl_append_source (struct source_stream *ss, struct getl_interface *i)
95 struct getl_source *s = xzalloc (sizeof ( struct getl_source ));
99 ll_push_head (&ss->sources, &s->ll);
102 /* Nests source S within the current source file. */
104 getl_include_source (struct source_stream *ss, struct getl_interface *i)
106 struct getl_source *current = current_source (ss);
107 struct getl_source *s = xzalloc (sizeof ( struct getl_source ));
111 s->included_from = current ;
113 current->includes = s;
115 ll_push_head (&ss->sources, &s->ll);
118 /* Closes the current source, and move the current source to the
119 next file in the chain. */
121 close_source (struct source_stream *ss)
123 struct getl_source *s = current_source (ss);
125 if ( s->interface->close )
126 s->interface->close (s->interface);
128 ll_pop_head (&ss->sources);
130 if (s->included_from != NULL)
131 current_source (ss)->includes = NULL;
136 /* Closes all sources until an interactive source is
139 getl_abort_noninteractive (struct source_stream *ss)
141 while ( ! ll_is_empty (&ss->sources))
143 const struct getl_source *s = current_source (ss);
145 if ( !s->interface->interactive (s->interface) )
150 /* Returns true if the current source is interactive,
153 getl_is_interactive (const struct source_stream *ss)
155 const struct getl_source *s = current_source (ss);
157 if (ll_is_empty (&ss->sources) )
160 return s->interface->interactive (s->interface);
163 /* Returns the name of the current source, or NULL if there is no
166 getl_source_name (const struct source_stream *ss)
168 const struct getl_source *s = current_source (ss);
170 if ( ll_is_empty (&ss->sources) )
173 if ( ! s->interface->name )
176 return s->interface->name (s->interface);
179 /* Returns the location within the current source, or -1 if there is
182 getl_source_location (const struct source_stream *ss)
184 const struct getl_source *s = current_source (ss);
186 if ( ll_is_empty (&ss->sources) )
189 if ( !s->interface->location )
192 return s->interface->location (s->interface);
198 destroy_source_stream (struct source_stream *ss)
200 while ( !ll_is_empty (&ss->sources))
202 ds_destroy (&ss->the_include_path);
208 /* Reads a single line into LINE.
209 Returns true when a line has been read, false at end of input.
210 On success, sets *SYNTAX to the style of the syntax read. */
212 getl_read_line (struct source_stream *ss, struct string *line,
213 enum getl_syntax *syntax)
215 while (!ll_is_empty (&ss->sources))
217 struct getl_source *s = current_source (ss);
220 if (s->interface->read (s->interface, line, syntax))
224 if (s->interface->filter)
225 s->interface->filter (s->interface, line, *syntax);
226 s = s->included_from;