Put unique dictionary indexes into internal variables
[pspp-builds.git] / src / language / utilities / include.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007 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 #include <ctype.h>
19 #include <stdlib.h>
20 #include <language/command.h>
21 #include <libpspp/message.h>
22 #include <libpspp/getl.h>
23 #include <language/syntax-file.h>
24 #include <language/lexer/lexer.h>
25 #include <libpspp/str.h>
26 #include <data/file-name.h>
27
28 #include "dirname.h"
29 #include "xalloc.h"
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34 static int parse_insert (struct lexer *lexer, char **filename);
35
36
37 int
38 cmd_include (struct lexer *lexer, struct dataset *ds UNUSED)
39 {
40   char *filename = NULL;
41   int status = parse_insert (lexer, &filename);
42
43   if ( CMD_SUCCESS != status)
44     return status;
45
46   lex_get (lexer);
47
48   status = lex_end_of_command (lexer);
49
50   if ( status == CMD_SUCCESS)
51     {
52       struct source_stream *ss = lex_get_source_stream (lexer);
53
54       assert (filename);
55       getl_include_source (ss, create_syntax_file_source (filename),
56                            GETL_BATCH, ERRMODE_STOP);
57       free (filename);
58     }
59
60   return status;
61 }
62
63
64 int
65 cmd_insert (struct lexer *lexer, struct dataset *ds UNUSED)
66 {
67   enum syntax_mode syntax_mode = GETL_INTERACTIVE;
68   enum error_mode error_mode = ERRMODE_CONTINUE;
69   char *filename = NULL;
70   int status = parse_insert (lexer, &filename);
71   bool cd = false;
72
73   if ( CMD_SUCCESS != status)
74     return status;
75
76   lex_get (lexer);
77
78   while ( '.' != lex_token (lexer))
79     {
80       if (lex_match_id (lexer, "SYNTAX"))
81         {
82           lex_match (lexer, '=');
83           if ( lex_match_id (lexer, "INTERACTIVE") )
84             syntax_mode = GETL_INTERACTIVE;
85           else if ( lex_match_id (lexer, "BATCH"))
86             syntax_mode = GETL_BATCH;
87           else
88             {
89               lex_error(lexer,
90                         _("Expecting BATCH or INTERACTIVE after SYNTAX."));
91               return CMD_FAILURE;
92             }
93         }
94       else if (lex_match_id (lexer, "CD"))
95         {
96           lex_match (lexer, '=');
97           if ( lex_match_id (lexer, "YES") )
98             {
99               cd = true;
100             }
101           else if ( lex_match_id (lexer, "NO"))
102             {
103               cd = false;
104             }
105           else
106             {
107               lex_error (lexer, _("Expecting YES or NO after CD."));
108               return CMD_FAILURE;
109             }
110         }
111       else if (lex_match_id (lexer, "ERROR"))
112         {
113           lex_match (lexer, '=');
114           if ( lex_match_id (lexer, "CONTINUE") )
115             {
116               error_mode = ERRMODE_CONTINUE;
117             }
118           else if ( lex_match_id (lexer, "STOP"))
119             {
120               error_mode = ERRMODE_STOP;
121             }
122           else
123             {
124               lex_error (lexer, _("Expecting CONTINUE or STOP after ERROR."));
125               return CMD_FAILURE;
126             }
127         }
128
129       else
130         {
131           lex_error (lexer, _("Unexpected token: `%s'."),
132                      lex_token_representation (lexer));
133
134           return CMD_FAILURE;
135         }
136     }
137
138   status = lex_end_of_command (lexer);
139
140   if ( status == CMD_SUCCESS)
141     {
142       struct source_stream *ss = lex_get_source_stream (lexer);
143
144       assert (filename);
145       getl_include_source (ss, create_syntax_file_source (filename),
146                            syntax_mode,
147                            error_mode);
148
149       if ( cd )
150         {
151           char *directory = dir_name (filename);
152           chdir (directory);
153           free (directory);
154         }
155
156       free (filename);
157     }
158
159   return status;
160 }
161
162
163 static int
164 parse_insert (struct lexer *lexer, char **filename)
165 {
166   char *target_fn;
167   char *relative_filename;
168
169   /* Skip optional FILE=. */
170   if (lex_match_id (lexer, "FILE"))
171     lex_match (lexer, '=');
172
173   /* File name can be identifier or string. */
174   if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING)
175     {
176       lex_error (lexer, _("expecting file name"));
177       return CMD_FAILURE;
178     }
179
180   target_fn = ds_cstr (lex_tokstr (lexer));
181
182   relative_filename =
183     fn_search_path (target_fn,
184                     getl_include_path (lex_get_source_stream (lexer)));
185
186   if ( ! relative_filename)
187     {
188       msg (SE, _("Can't find `%s' in include file search path."),
189          target_fn);
190       return CMD_FAILURE;
191     }
192
193   *filename = relative_filename;
194
195   return CMD_SUCCESS;
196 }