1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
22 #include <libpspp/str.h>
23 #include <libpspp/ll.h>
24 #include <libpspp/version.h>
25 #include <libpspp/alloc.h>
29 struct getl_source *included_from; /* File that this is nested inside. */
30 struct getl_source *includes; /* File nested inside this file. */
32 struct ll ll; /* Element in the sources list */
34 struct getl_interface *interface;
39 struct ll_list sources ; /* List of source files. */
41 struct string the_include_path;
45 getl_include_path (const struct source_stream *ss)
47 return ds_cstr (&ss->the_include_path);
50 static struct getl_source *
51 current_source (const struct source_stream *ss)
53 const struct ll *ll = ll_head (&ss->sources);
54 return ll_data (ll, struct getl_source, ll );
57 /* Initialize getl. */
58 struct source_stream *
59 create_source_stream (const char *initial_include_path)
61 struct source_stream *ss = xzalloc (sizeof (*ss));
62 ll_init (&ss->sources);
64 ds_init_cstr (&ss->the_include_path,
65 fn_getenv_default ("STAT_INCLUDE_PATH", include_path));
67 ds_init_cstr (&ss->the_include_path, initial_include_path);
72 /* Delete everything from the include path. */
74 getl_clear_include_path (struct source_stream *ss)
76 ds_clear (&ss->the_include_path);
79 /* Add to the include path. */
81 getl_add_include_dir (struct source_stream *ss, const char *path)
83 if (ds_length (&ss->the_include_path))
84 ds_put_char (&ss->the_include_path, ':');
86 ds_put_cstr (&ss->the_include_path, path);
89 /* Appends source S to the list of source files. */
91 getl_append_source (struct source_stream *ss, struct getl_interface *i)
93 struct getl_source *s = xzalloc (sizeof ( struct getl_source ));
97 ll_push_head (&ss->sources, &s->ll);
100 /* Nests source S within the current source file. */
102 getl_include_source (struct source_stream *ss, struct getl_interface *i)
104 struct getl_source *current = current_source (ss);
105 struct getl_source *s = xzalloc (sizeof ( struct getl_source ));
109 s->included_from = current ;
111 current->includes = s;
113 ll_push_head (&ss->sources, &s->ll);
116 /* Closes the current source, and move the current source to the
117 next file in the chain. */
119 close_source (struct source_stream *ss)
121 struct getl_source *s = current_source (ss);
123 if ( s->interface->close )
124 s->interface->close (s->interface);
126 ll_pop_head (&ss->sources);
128 if (s->included_from != NULL)
129 current_source (ss)->includes = NULL;
134 /* Closes all sources until an interactive source is
137 getl_abort_noninteractive (struct source_stream *ss)
139 while ( ! ll_is_empty (&ss->sources))
141 const struct getl_source *s = current_source (ss);
143 if ( !s->interface->interactive (s->interface) )
148 /* Returns true if the current source is interactive,
151 getl_is_interactive (const struct source_stream *ss)
153 const struct getl_source *s = current_source (ss);
155 if (ll_is_empty (&ss->sources) )
158 return s->interface->interactive (s->interface);
161 /* Returns the name of the current source, or NULL if there is no
164 getl_source_name (const struct source_stream *ss)
166 const struct getl_source *s = current_source (ss);
168 if ( ll_is_empty (&ss->sources) )
171 if ( ! s->interface->name )
174 return s->interface->name (s->interface);
177 /* Returns the location within the current source, or -1 if there is
180 getl_source_location (const struct source_stream *ss)
182 const struct getl_source *s = current_source (ss);
184 if ( ll_is_empty (&ss->sources) )
187 if ( !s->interface->location )
190 return s->interface->location (s->interface);
196 destroy_source_stream (struct source_stream *ss)
198 while ( !ll_is_empty (&ss->sources))
200 ds_destroy (&ss->the_include_path);
206 /* Reads a single line into LINE.
207 Returns true when a line has been read, false at end of input.
208 On success, sets *SYNTAX to the style of the syntax read. */
210 getl_read_line (struct source_stream *ss, struct string *line,
211 enum getl_syntax *syntax)
214 while (!ll_is_empty (&ss->sources))
216 struct getl_source *s = current_source (ss);
219 if (s->interface->read (s->interface, line, syntax))
223 if (s->interface->filter)
224 s->interface->filter (s->interface, line, *syntax);
225 s = s->included_from;