Rewrite PSPP output engine.
[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/tab.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       ds_clear (line);
108       if (!ds_read_line (line, sfs->syntax_file, SIZE_MAX))
109         {
110           if (ferror (sfs->syntax_file))
111             msg (ME, _("Reading `%s': %s."), sfs->fn, strerror (errno));
112           return false;
113         }
114       ds_chomp (line, '\n');
115     }
116   while (sfs->ln == 1 && !memcmp (ds_cstr (line), "#!", 2));
117
118   /* Echo to listing file, if configured to do so. */
119   if (settings_get_echo ())
120     tab_output_text (TAB_LEFT | TAB_FIX, ds_cstr (line));
121
122   return true;
123 }
124
125 static void
126 syntax_close (struct getl_interface *s)
127 {
128   struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
129                                             parent);
130
131   if (sfs->syntax_file && EOF == fn_close (sfs->fn, sfs->syntax_file))
132     msg (MW, _("Closing `%s': %s."), sfs->fn, strerror (errno));
133   free (sfs->fn);
134   free (sfs);
135 }
136
137 static bool
138 always_false (const struct getl_interface *s UNUSED)
139 {
140   return false;
141 }
142
143
144 /* Creates a syntax file source with file name FN. */
145 struct getl_interface *
146 create_syntax_file_source (const char *fn)
147 {
148   struct syntax_file_source *ss = xzalloc (sizeof (*ss));
149
150   ss->fn = xstrdup (fn);
151
152   ss->parent.interactive = always_false;
153   ss->parent.read = read_syntax_file ;
154   ss->parent.filter = NULL;
155   ss->parent.close = syntax_close ;
156   ss->parent.name = name ;
157   ss->parent.location = line_number;
158
159   return &ss->parent;
160 }
161