Fix handling of #! at beginning of PSPP syntax file; add regression test.
[pspp-builds.git] / src / language / syntax-file.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "syntax-file.h"
20
21 #include <stdio.h>
22 #include <errno.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25
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/message.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
35 #include <libpspp/verbose-msg.h>
36 #include <libpspp/version.h>
37 #include <output/table.h>
38
39 #include <libpspp/ll.h>
40
41 #include "prompt.h"
42
43 #include "xalloc.h"
44
45 #include "gettext.h"
46 #define _(msgid) gettext (msgid)
47
48 #include <libpspp/getl.h>
49
50
51 struct syntax_file_source
52   {
53     struct getl_interface parent ;
54
55     FILE *syntax_file;
56
57     /* Current location. */
58     char *fn;                           /* File name. */
59     int ln;                             /* Line number. */
60   };
61
62 static const char *
63 name (const struct getl_interface *s)
64 {
65   const struct syntax_file_source *sfs = (const struct syntax_file_source *) s;
66   return sfs->fn;
67 }
68
69 static int
70 line_number (const struct getl_interface *s)
71 {
72   const struct syntax_file_source *sfs = (const struct syntax_file_source *) s;
73   return sfs->ln;
74 }
75
76
77 /* Reads a line from syntax file source S into LINE.
78    Returns true if successful, false at end of file. */
79 static bool
80 read_syntax_file (struct getl_interface *s,
81                   struct string *line)
82 {
83   struct syntax_file_source *sfs = (struct syntax_file_source *) s;
84
85   /* Open file, if not yet opened. */
86   if (sfs->syntax_file == NULL)
87     {
88       verbose_msg (1, _("opening \"%s\" as syntax file"), sfs->fn);
89       sfs->syntax_file = fn_open (sfs->fn, "r");
90
91       if (sfs->syntax_file == NULL)
92         {
93           msg (ME, _("Opening `%s': %s."), sfs->fn, strerror (errno));
94           return false;
95         }
96     }
97
98   /* Read line from file and remove new-line.
99      Skip initial "#! /usr/bin/pspp" line. */
100   do
101     {
102       sfs->ln++;
103       ds_clear (line);
104       if (!ds_read_line (line, sfs->syntax_file, SIZE_MAX))
105         {
106           if (ferror (sfs->syntax_file))
107             msg (ME, _("Reading `%s': %s."), sfs->fn, strerror (errno));
108           return false;
109         }
110       ds_chomp (line, '\n');
111     }
112   while (sfs->ln == 1 && !memcmp (ds_cstr (line), "#!", 2));
113
114   /* Echo to listing file, if configured to do so. */
115   if (settings_get_echo ())
116     tab_output_text (TAB_LEFT | TAB_FIX, ds_cstr (line));
117
118   return true;
119 }
120
121 static void
122 syntax_close (struct getl_interface *s)
123 {
124   struct syntax_file_source *sfs = (struct syntax_file_source *) s;
125
126   if (sfs->syntax_file && EOF == fn_close (sfs->fn, sfs->syntax_file))
127     msg (MW, _("Closing `%s': %s."), sfs->fn, strerror (errno));
128   free (sfs->fn);
129   free (sfs);
130 }
131
132 static bool
133 always_false (const struct getl_interface *s UNUSED)
134 {
135   return false;
136 }
137
138
139 /* Creates a syntax file source with file name FN. */
140 struct getl_interface *
141 create_syntax_file_source (const char *fn)
142 {
143   struct syntax_file_source *ss = xzalloc (sizeof (*ss));
144
145   ss->fn = xstrdup (fn);
146
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;
153
154   return (struct getl_interface *) ss;
155 }
156