1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 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
21 #include "syntax-file.h"
27 #include <data/file-name.h>
28 #include <data/settings.h>
29 #include <data/variable.h>
30 #include <language/command.h>
31 #include <language/lexer/lexer.h>
32 #include <libpspp/alloc.h>
33 #include <libpspp/assertion.h>
34 #include <libpspp/message.h>
35 #include <libpspp/message.h>
36 #include <libpspp/str.h>
37 #include <libpspp/verbose-msg.h>
38 #include <libpspp/version.h>
39 #include <output/table.h>
41 #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 = (const struct syntax_file_source *) s;
70 line_number (const struct getl_interface *s)
72 const struct syntax_file_source *sfs = (const struct syntax_file_source *) s;
77 /* Reads a line from syntax file source S into LINE.
78 Returns true if successful, false at end of file. */
80 read_syntax_file (struct getl_interface *s,
81 struct string *line, enum getl_syntax *syntax)
83 struct syntax_file_source *sfs = (struct syntax_file_source *) s;
85 /* Open file, if not yet opened. */
86 if (sfs->syntax_file == NULL)
88 verbose_msg (1, _("opening \"%s\" as syntax file"), sfs->fn);
89 sfs->syntax_file = fn_open (sfs->fn, "r");
91 if (sfs->syntax_file == NULL)
93 msg (ME, _("Opening `%s': %s."), sfs->fn, strerror (errno));
98 /* Read line from file and remove new-line.
99 Skip initial "#! /usr/bin/pspp" line. */
103 if (!ds_read_line (line, sfs->syntax_file))
105 if (ferror (sfs->syntax_file))
106 msg (ME, _("Reading `%s': %s."), sfs->fn, strerror (errno));
109 ds_chomp (line, '\n');
111 while (sfs->ln == 1 && !memcmp (ds_cstr (line), "#!", 2));
113 /* Echo to listing file, if configured to do so. */
115 tab_output_text (TAB_LEFT | TAB_FIX, ds_cstr (line));
117 *syntax = GETL_BATCH;
122 syntax_close (struct getl_interface *s)
124 struct syntax_file_source *sfs = (struct syntax_file_source *) s;
126 if (sfs->syntax_file && EOF == fn_close (sfs->fn, sfs->syntax_file))
127 msg (MW, _("Closing `%s': %s."), sfs->fn, strerror (errno));
133 always_false (const struct getl_interface *s UNUSED)
139 /* Creates a syntax file source with file name FN. */
140 struct getl_interface *
141 create_syntax_file_source (const char *fn)
143 struct syntax_file_source *ss = xzalloc (sizeof (*ss));
145 ss->fn = xstrdup (fn);
147 ss->parent.interactive = always_false;
148 ss->parent.read = read_syntax_file ;
149 ss->parent.filter = NULL;
150 ss->parent.close = syntax_close ;
151 ss->parent.name = name ;
152 ss->parent.location = line_number;
154 return (struct getl_interface *) ss;