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