1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 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
22 #include "syntax-file.h"
28 #include <data/file-name.h>
29 #include <data/settings.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/lexer/lexer.h>
33 #include <libpspp/alloc.h>
34 #include <libpspp/assertion.h>
35 #include <libpspp/message.h>
36 #include <libpspp/message.h>
37 #include <libpspp/str.h>
38 #include <libpspp/verbose-msg.h>
39 #include <libpspp/version.h>
40 #include <output/table.h>
42 #include <libpspp/ll.h>
47 #define _(msgid) gettext (msgid)
49 #include <libpspp/getl.h>
52 struct syntax_file_source
54 struct getl_interface parent ;
58 /* Current location. */
59 char *fn; /* File name. */
60 int ln; /* Line number. */
64 name (const struct getl_interface *s)
66 const struct syntax_file_source *sfs = (const struct syntax_file_source *) s;
71 line_number (const struct getl_interface *s)
73 const struct syntax_file_source *sfs = (const struct syntax_file_source *) s;
78 /* Reads a line from syntax file source S into LINE.
79 Returns true if successful, false at end of file. */
81 read_syntax_file (struct getl_interface *s,
82 struct string *line, enum getl_syntax *syntax)
84 struct syntax_file_source *sfs = (struct syntax_file_source *) s;
86 /* Open file, if not yet opened. */
87 if (sfs->syntax_file == NULL)
89 verbose_msg (1, _("opening \"%s\" as syntax file"), sfs->fn);
90 sfs->syntax_file = fn_open (sfs->fn, "r");
92 if (sfs->syntax_file == NULL)
94 msg (ME, _("Opening `%s': %s."), sfs->fn, strerror (errno));
99 /* Read line from file and remove new-line.
100 Skip initial "#! /usr/bin/pspp" line. */
104 if (!ds_read_line (line, sfs->syntax_file))
106 if (ferror (sfs->syntax_file))
107 msg (ME, _("Reading `%s': %s."), sfs->fn, strerror (errno));
110 ds_chomp (line, '\n');
112 while (sfs->ln == 1 && !memcmp (ds_cstr (line), "#!", 2));
114 /* Echo to listing file, if configured to do so. */
116 tab_output_text (TAB_LEFT | TAB_FIX, ds_cstr (line));
118 *syntax = GETL_BATCH;
123 syntax_close (struct getl_interface *s)
125 struct syntax_file_source *sfs = (struct syntax_file_source *) s;
127 if (sfs->syntax_file && EOF == fn_close (sfs->fn, sfs->syntax_file))
128 msg (MW, _("Closing `%s': %s."), sfs->fn, strerror (errno));
134 always_false (const struct getl_interface *s UNUSED)
140 /* Creates a syntax file source with file name FN. */
141 struct getl_interface *
142 create_syntax_file_source (const char *fn)
144 struct syntax_file_source *ss = xzalloc (sizeof (*ss));
146 ss->fn = xstrdup (fn);
148 ss->parent.interactive = always_false;
149 ss->parent.read = read_syntax_file ;
150 ss->parent.filter = NULL;
151 ss->parent.close = syntax_close ;
152 ss->parent.name = name ;
153 ss->parent.location = line_number;
155 return (struct getl_interface *) ss;