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;
45 struct ll_list sources ; /* List of source files. */
47 struct string the_include_path;
51 getl_include_path (const struct source_stream *ss)
53 return ds_cstr (&ss->the_include_path);
56 static struct getl_source *
57 current_source (const struct source_stream *ss)
59 const struct ll *ll = ll_head (&ss->sources);
60 return ll_data (ll, struct getl_source, ll );
63 /* Initialize getl. */
64 struct source_stream *
65 create_source_stream (void)
67 struct source_stream *ss = xzalloc (sizeof (*ss));
68 ll_init (&ss->sources);
69 ds_init_cstr (&ss->the_include_path,
70 fn_getenv_default ("STAT_INCLUDE_PATH", 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;