str: Rename ss_chomp() to ss_chomp_byte(), ds_chomp() to ds_chomp_byte().
[pspp-builds.git] / src / language / syntax-file.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011 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 "language/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 "language/prompt.h"
32 #include "libpspp/assertion.h"
33 #include "libpspp/cast.h"
34 #include "libpspp/getl.h"
35 #include "libpspp/ll.h"
36 #include "libpspp/message.h"
37 #include "libpspp/str.h"
38 #include "libpspp/version.h"
39 #include "output/tab.h"
40
41 #include "gl/xalloc.h"
42
43 #include "gettext.h"
44 #define _(msgid) gettext (msgid)
45
46 struct syntax_file_source
47   {
48     struct getl_interface parent ;
49
50     FILE *syntax_file;
51
52     /* Current location. */
53     char *fn;                           /* File name. */
54     int ln;                             /* Line number. */
55   };
56
57 static const char *
58 name (const struct getl_interface *s)
59 {
60   const struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
61                                                   parent);
62   return sfs->fn;
63 }
64
65 static int
66 line_number (const struct getl_interface *s)
67 {
68   const struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
69                                                   parent);
70   return sfs->ln;
71 }
72
73
74 /* Reads a line from syntax file source S into LINE.
75    Returns true if successful, false at end of file. */
76 static bool
77 read_syntax_file (struct getl_interface *s,
78                   struct string *line)
79 {
80   struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
81                                             parent);
82
83   if (sfs->syntax_file == NULL)
84     return false;
85
86   /* Read line from file and remove new-line.
87      Skip initial "#! /usr/bin/pspp" line. */
88   do
89     {
90       sfs->ln++;
91       ds_clear (line);
92       if (!ds_read_line (line, sfs->syntax_file, SIZE_MAX))
93         {
94           if (ferror (sfs->syntax_file))
95             msg (ME, _("Reading `%s': %s."), sfs->fn, strerror (errno));
96           return false;
97         }
98       ds_chomp_byte (line, '\n');
99     }
100   while (sfs->ln == 1 && !memcmp (ds_cstr (line), "#!", 2));
101
102   return true;
103 }
104
105 static void
106 syntax_close (struct getl_interface *s)
107 {
108   struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
109                                             parent);
110
111   if (sfs->syntax_file && EOF == fn_close (sfs->fn, sfs->syntax_file))
112     msg (MW, _("Closing `%s': %s."), sfs->fn, strerror (errno));
113   free (sfs->fn);
114   free (sfs);
115 }
116
117 static bool
118 always_false (const struct getl_interface *s UNUSED)
119 {
120   return false;
121 }
122
123
124 /* Creates a syntax file source with file name FN. */
125 struct getl_interface *
126 create_syntax_file_source (const char *fn)
127 {
128   struct syntax_file_source *ss = xzalloc (sizeof (*ss));
129
130   ss->fn = xstrdup (fn);
131   ss->syntax_file = fn_open (ss->fn, "r");
132   if (ss->syntax_file == NULL)
133     msg (ME, _("Opening `%s': %s."), ss->fn, strerror (errno));
134
135   ss->parent.interactive = always_false;
136   ss->parent.read = read_syntax_file ;
137   ss->parent.filter = NULL;
138   ss->parent.close = syntax_close ;
139   ss->parent.name = name ;
140   ss->parent.location = line_number;
141
142   return &ss->parent;
143 }
144