ad277b5859dec12b1125a4a6b0ac40329a6d2a50
[pspp-builds.git] / src / language / syntax-file.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010 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/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>
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 = UP_CAST (s, struct syntax_file_source,
66                                                   parent);
67   return sfs->fn;
68 }
69
70 static int
71 line_number (const struct getl_interface *s)
72 {
73   const struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
74                                                   parent);
75   return sfs->ln;
76 }
77
78
79 /* Reads a line from syntax file source S into LINE.
80    Returns true if successful, false at end of file. */
81 static bool
82 read_syntax_file (struct getl_interface *s,
83                   struct string *line)
84 {
85   struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
86                                             parent);
87
88   /* Open file, if not yet opened. */
89   if (sfs->syntax_file == NULL)
90     {
91       sfs->syntax_file = fn_open (sfs->fn, "r");
92
93       if (sfs->syntax_file == NULL)
94         {
95           msg (ME, _("Opening `%s': %s."), sfs->fn, strerror (errno));
96           return false;
97         }
98     }
99
100   /* Read line from file and remove new-line.
101      Skip initial "#! /usr/bin/pspp" line. */
102   do
103     {
104       sfs->ln++;
105       ds_clear (line);
106       if (!ds_read_line (line, sfs->syntax_file, SIZE_MAX))
107         {
108           if (ferror (sfs->syntax_file))
109             msg (ME, _("Reading `%s': %s."), sfs->fn, strerror (errno));
110           return false;
111         }
112       ds_chomp (line, '\n');
113     }
114   while (sfs->ln == 1 && !memcmp (ds_cstr (line), "#!", 2));
115
116   /* Echo to listing file, if configured to do so. */
117   if (settings_get_echo ())
118     tab_output_text (TAB_LEFT | TAB_FIX, ds_cstr (line));
119
120   return true;
121 }
122
123 static void
124 syntax_close (struct getl_interface *s)
125 {
126   struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
127                                             parent);
128
129   if (sfs->syntax_file && EOF == fn_close (sfs->fn, sfs->syntax_file))
130     msg (MW, _("Closing `%s': %s."), sfs->fn, strerror (errno));
131   free (sfs->fn);
132   free (sfs);
133 }
134
135 static bool
136 always_false (const struct getl_interface *s UNUSED)
137 {
138   return false;
139 }
140
141
142 /* Creates a syntax file source with file name FN. */
143 struct getl_interface *
144 create_syntax_file_source (const char *fn)
145 {
146   struct syntax_file_source *ss = xzalloc (sizeof (*ss));
147
148   ss->fn = xstrdup (fn);
149
150   ss->parent.interactive = always_false;
151   ss->parent.read = read_syntax_file ;
152   ss->parent.filter = NULL;
153   ss->parent.close = syntax_close ;
154   ss->parent.name = name ;
155   ss->parent.location = line_number;
156
157   return &ss->parent;
158 }
159