4f667c1284795d84393cdc1d0b9c5355436ee0a6
[pspp-builds.git] / src / language / syntax-file.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include "syntax-file.h"
23
24 #include <stdio.h>
25 #include <errno.h>
26 #include <stdlib.h>
27
28 #include <data/file-name.h>
29 #include <data/settings.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/lexer/lexer.h>
33 #include <libpspp/alloc.h>
34 #include <libpspp/assertion.h>
35 #include <libpspp/message.h>
36 #include <libpspp/message.h>
37 #include <libpspp/str.h>
38 #include <libpspp/verbose-msg.h>
39 #include <libpspp/version.h>
40 #include <output/table.h>
41
42 #include <libpspp/ll.h>
43
44 #include "prompt.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 = (const struct syntax_file_source *) s;
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 = (const struct syntax_file_source *) s;
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 bool
81 read_syntax_file (struct getl_interface *s,
82                   struct string *line, enum getl_syntax *syntax)
83 {
84   struct syntax_file_source *sfs = (struct syntax_file_source *) s;
85
86   /* Open file, if not yet opened. */
87   if (sfs->syntax_file == NULL)
88     {
89       verbose_msg (1, _("opening \"%s\" as syntax file"), sfs->fn);
90       sfs->syntax_file = fn_open (sfs->fn, "r");
91
92       if (sfs->syntax_file == NULL)
93         {
94           msg (ME, _("Opening `%s': %s."), sfs->fn, strerror (errno));
95           return false;
96         }
97     }
98
99   /* Read line from file and remove new-line.
100      Skip initial "#! /usr/bin/pspp" line. */
101   do 
102     {
103       sfs->ln++;
104       if (!ds_read_line (line, sfs->syntax_file))
105         {
106           if (ferror (sfs->syntax_file))
107             msg (ME, _("Reading `%s': %s."), sfs->fn, strerror (errno));
108           return false;
109         }
110       ds_chomp (line, '\n');
111     }
112   while (sfs->ln == 1 && !memcmp (ds_cstr (line), "#!", 2));
113
114   /* Echo to listing file, if configured to do so. */
115   if (get_echo ())
116     tab_output_text (TAB_LEFT | TAB_FIX, ds_cstr (line));
117
118   *syntax = GETL_BATCH;
119   return true;
120 }
121
122 static void
123 syntax_close (struct getl_interface *s)
124 {
125   struct syntax_file_source *sfs = (struct syntax_file_source *) s;
126
127   if (sfs->syntax_file && EOF == fn_close (sfs->fn, sfs->syntax_file))
128     msg (MW, _("Closing `%s': %s."), sfs->fn, strerror (errno));
129   free (sfs->fn);
130   free (sfs);
131 }
132
133 static bool
134 always_false (const struct getl_interface *s UNUSED)
135 {
136   return false;
137 }
138
139
140 /* Creates a syntax file source with file name FN. */
141 struct getl_interface *
142 create_syntax_file_source (const char *fn) 
143 {
144   struct syntax_file_source *ss = xzalloc (sizeof (*ss));
145
146   ss->fn = xstrdup (fn);
147
148   ss->parent.interactive = always_false;
149   ss->parent.read = read_syntax_file ;
150   ss->parent.filter = NULL;
151   ss->parent.close = syntax_close ;
152   ss->parent.name = name ;
153   ss->parent.location = line_number;
154
155   return (struct getl_interface *) ss;
156 }
157