1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010 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/>. */
19 #include "syntax-file.h"
26 #include <data/file-name.h>
27 #include <data/settings.h>
28 #include <data/variable.h>
29 #include <language/command.h>
30 #include <language/lexer/lexer.h>
31 #include <libpspp/assertion.h>
32 #include <libpspp/cast.h>
33 #include <libpspp/message.h>
34 #include <libpspp/message.h>
35 #include <libpspp/str.h>
36 #include <libpspp/version.h>
37 #include <output/tab.h>
39 #include <libpspp/ll.h>
46 #define _(msgid) gettext (msgid)
48 #include <libpspp/getl.h>
51 struct syntax_file_source
53 struct getl_interface parent ;
57 /* Current location. */
58 char *fn; /* File name. */
59 int ln; /* Line number. */
63 name (const struct getl_interface *s)
65 const struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
71 line_number (const struct getl_interface *s)
73 const struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
79 /* Reads a line from syntax file source S into LINE.
80 Returns true if successful, false at end of file. */
82 read_syntax_file (struct getl_interface *s,
85 struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
88 if (sfs->syntax_file == NULL)
91 /* Read line from file and remove new-line.
92 Skip initial "#! /usr/bin/pspp" line. */
97 if (!ds_read_line (line, sfs->syntax_file, SIZE_MAX))
99 if (ferror (sfs->syntax_file))
100 msg (ME, _("Reading `%s': %s."), sfs->fn, strerror (errno));
103 ds_chomp (line, '\n');
105 while (sfs->ln == 1 && !memcmp (ds_cstr (line), "#!", 2));
111 syntax_close (struct getl_interface *s)
113 struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
116 if (sfs->syntax_file && EOF == fn_close (sfs->fn, sfs->syntax_file))
117 msg (MW, _("Closing `%s': %s."), sfs->fn, strerror (errno));
123 always_false (const struct getl_interface *s UNUSED)
129 /* Creates a syntax file source with file name FN. */
130 struct getl_interface *
131 create_syntax_file_source (const char *fn)
133 struct syntax_file_source *ss = xzalloc (sizeof (*ss));
135 ss->fn = xstrdup (fn);
136 ss->syntax_file = fn_open (ss->fn, "r");
137 if (ss->syntax_file == NULL)
138 msg (ME, _("Opening `%s': %s."), ss->fn, strerror (errno));
140 ss->parent.interactive = always_false;
141 ss->parent.read = read_syntax_file ;
142 ss->parent.filter = NULL;
143 ss->parent.close = syntax_close ;
144 ss->parent.name = name ;
145 ss->parent.location = line_number;