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