97f73fd6fca44f344a163be35038d7d77aa55754
[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/str.h>
35 #include <libpspp/version.h>
36 #include <output/tab.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 = UP_CAST (s, struct syntax_file_source,
65                                                   parent);
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 = UP_CAST (s, struct syntax_file_source,
73                                                   parent);
74   return sfs->ln;
75 }
76
77
78 /* Reads a line from syntax file source S into LINE.
79    Returns true if successful, false at end of file. */
80 static bool
81 read_syntax_file (struct getl_interface *s,
82                   struct string *line)
83 {
84   struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
85                                             parent);
86
87   if (sfs->syntax_file == NULL)
88     return false;
89
90   /* Read line from file and remove new-line.
91      Skip initial "#! /usr/bin/pspp" line. */
92   do
93     {
94       sfs->ln++;
95       ds_clear (line);
96       if (!ds_read_line (line, sfs->syntax_file, SIZE_MAX))
97         {
98           if (ferror (sfs->syntax_file))
99             msg (ME, _("Reading `%s': %s."), sfs->fn, strerror (errno));
100           return false;
101         }
102       ds_chomp (line, '\n');
103     }
104   while (sfs->ln == 1 && !memcmp (ds_cstr (line), "#!", 2));
105
106   return true;
107 }
108
109 static void
110 syntax_close (struct getl_interface *s)
111 {
112   struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
113                                             parent);
114
115   if (sfs->syntax_file && EOF == fn_close (sfs->fn, sfs->syntax_file))
116     msg (MW, _("Closing `%s': %s."), sfs->fn, strerror (errno));
117   free (sfs->fn);
118   free (sfs);
119 }
120
121 static bool
122 always_false (const struct getl_interface *s UNUSED)
123 {
124   return false;
125 }
126
127
128 /* Creates a syntax file source with file name FN. */
129 struct getl_interface *
130 create_syntax_file_source (const char *fn)
131 {
132   struct syntax_file_source *ss = xzalloc (sizeof (*ss));
133
134   ss->fn = xstrdup (fn);
135   ss->syntax_file = fn_open (ss->fn, "r");
136   if (ss->syntax_file == NULL)
137     msg (ME, _("Opening `%s': %s."), ss->fn, strerror (errno));
138
139   ss->parent.interactive = always_false;
140   ss->parent.read = read_syntax_file ;
141   ss->parent.filter = NULL;
142   ss->parent.close = syntax_close ;
143   ss->parent.name = name ;
144   ss->parent.location = line_number;
145
146   return &ss->parent;
147 }
148