X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Futilities%2Finclude.c;h=f02291e5e72bed4f13b4465941e3f32ca07f6d6c;hb=cbf273bfb07d7db5353eb691b903a321b7e5bd59;hp=fe98aeb87f3347b82b61853adc70360cee87f132;hpb=deb4fd96c0c171fc8eb64f7f1e7f5c2af4931416;p=pspp diff --git a/src/language/utilities/include.c b/src/language/utilities/include.c index fe98aeb87f..f02291e5e7 100644 --- a/src/language/utilities/include.c +++ b/src/language/utilities/include.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2007, 2010, 2011, 2020 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,191 +15,169 @@ along with this program. If not, see . */ #include + #include #include #include #include -#include -#include -#include -#include -#include -#include -#include +#include + +#include "data/dataset.h" +#include "data/session.h" +#include "language/command.h" +#include "language/lexer/include-path.h" +#include "language/lexer/lexer.h" +#include "libpspp/i18n.h" +#include "libpspp/message.h" +#include "libpspp/str.h" -#include "dirname.h" -#include "xalloc.h" +#include "gl/dirname.h" +#include "gl/xalloc.h" #include "gettext.h" #define _(msgid) gettext (msgid) -static int parse_insert (struct lexer *lexer, char **filename); - +enum variant + { + INSERT, + INCLUDE + }; -int -cmd_include (struct lexer *lexer, struct dataset *ds UNUSED) +static int +do_insert (struct lexer *lexer, struct dataset *ds, enum variant variant) { - char *filename = NULL; - int status = parse_insert (lexer, &filename); + /* Skip optional FILE=. */ + if (lex_match_id (lexer, "FILE")) + lex_match (lexer, T_EQUALS); - if ( CMD_SUCCESS != status) - return status; + if (!lex_force_string_or_id (lexer)) + return CMD_FAILURE; - lex_get (lexer); + char *relative_name = utf8_to_filename (lex_tokcstr (lexer)); + char *filename = include_path_search (relative_name); + free (relative_name); - status = lex_end_of_command (lexer); - - if ( status == CMD_SUCCESS) + if (!filename) { - struct source_stream *ss = lex_get_source_stream (lexer); - - assert (filename); - getl_include_source (ss, create_syntax_file_source (filename), - GETL_BATCH, ERRMODE_STOP); - free (filename); + msg (SE, _("Can't find `%s' in include file search path."), + lex_tokcstr (lexer)); + return CMD_FAILURE; } - - return status; -} - - -int -cmd_insert (struct lexer *lexer, struct dataset *ds UNUSED) -{ - enum syntax_mode syntax_mode = GETL_INTERACTIVE; - enum error_mode error_mode = ERRMODE_CONTINUE; - char *filename = NULL; - int status = parse_insert (lexer, &filename); - bool cd = false; - - if ( CMD_SUCCESS != status) - return status; - lex_get (lexer); - while ( '.' != lex_token (lexer)) + enum segmenter_mode syntax_mode = SEG_MODE_INTERACTIVE; + enum lex_error_mode error_mode = LEX_ERROR_CONTINUE; + bool cd = false; + int status = CMD_FAILURE; + char *encoding = xstrdup (session_get_default_syntax_encoding ( + dataset_session (ds))); + while (T_ENDCMD != lex_token (lexer)) { - if (lex_match_id (lexer, "SYNTAX")) + if (lex_match_id (lexer, "ENCODING")) + { + lex_match (lexer, T_EQUALS); + if (!lex_force_string (lexer)) + goto exit; + + free (encoding); + encoding = xstrdup (lex_tokcstr (lexer)); + lex_get (lexer); + } + else if (variant == INSERT && lex_match_id (lexer, "SYNTAX")) { - lex_match (lexer, '='); - if ( lex_match_id (lexer, "INTERACTIVE") ) - syntax_mode = GETL_INTERACTIVE; - else if ( lex_match_id (lexer, "BATCH")) - syntax_mode = GETL_BATCH; + lex_match (lexer, T_EQUALS); + if (lex_match_id (lexer, "INTERACTIVE")) + syntax_mode = SEG_MODE_INTERACTIVE; + else if (lex_match_id (lexer, "BATCH")) + syntax_mode = SEG_MODE_BATCH; + else if (lex_match_id (lexer, "AUTO")) + syntax_mode = SEG_MODE_AUTO; else { - lex_error(lexer, - _("Expecting BATCH or INTERACTIVE after SYNTAX.")); - return CMD_FAILURE; + lex_error_expecting (lexer, "BATCH", "INTERACTIVE", "AUTO"); + goto exit; } } - else if (lex_match_id (lexer, "CD")) + else if (variant == INSERT && lex_match_id (lexer, "CD")) { - lex_match (lexer, '='); - if ( lex_match_id (lexer, "YES") ) - { - cd = true; - } - else if ( lex_match_id (lexer, "NO")) - { - cd = false; - } + lex_match (lexer, T_EQUALS); + if (lex_match_id (lexer, "YES")) + cd = true; + else if (lex_match_id (lexer, "NO")) + cd = false; else { - lex_error (lexer, _("Expecting YES or NO after CD.")); - return CMD_FAILURE; + lex_error_expecting (lexer, "YES", "NO"); + goto exit; } } - else if (lex_match_id (lexer, "ERROR")) + else if (variant == INSERT && lex_match_id (lexer, "ERROR")) { - lex_match (lexer, '='); - if ( lex_match_id (lexer, "CONTINUE") ) - { - error_mode = ERRMODE_CONTINUE; - } - else if ( lex_match_id (lexer, "STOP")) - { - error_mode = ERRMODE_STOP; - } + lex_match (lexer, T_EQUALS); + if (lex_match_id (lexer, "CONTINUE")) + error_mode = LEX_ERROR_CONTINUE; + else if (lex_match_id (lexer, "STOP")) + error_mode = LEX_ERROR_STOP; + else if (settings_get_testing_mode () + && lex_match_id (lexer, "IGNORE")) + error_mode = LEX_ERROR_IGNORE; else { - lex_error (lexer, _("Expecting CONTINUE or STOP after ERROR.")); - return CMD_FAILURE; + lex_error_expecting (lexer, "CONTINUE", "STOP"); + goto exit; } } - else { - lex_error (lexer, _("Unexpected token: `%s'."), - lex_token_representation (lexer)); - - return CMD_FAILURE; + if (variant == INSERT) + lex_error_expecting (lexer, "ENCODING", "SYNTAX", "CD", "ERROR"); + else + lex_error_expecting (lexer, "ENCODING"); + goto exit; } } - status = lex_end_of_command (lexer); - if ( status == CMD_SUCCESS) + if (status == CMD_SUCCESS) { - struct source_stream *ss = lex_get_source_stream (lexer); - - assert (filename); - getl_include_source (ss, create_syntax_file_source (filename), - syntax_mode, - error_mode); - - if ( cd ) - { - char *directory = dir_name (filename); - chdir (directory); - free (directory); - } - - free (filename); + struct lex_reader *reader = lex_reader_for_file (filename, encoding, + syntax_mode, error_mode); + if (reader != NULL) + { + lex_discard_rest_of_command (lexer); + lex_include (lexer, reader); + + if (cd) + { + char *directory = dir_name (filename); + if (chdir (directory)) + { + int err = errno; + msg (SE, _("Cannot change directory to %s: %s"), directory, + strerror (err)); + status = CMD_FAILURE; + } + + free (directory); + } + } } +exit: + free (encoding); + free (filename); return status; } - -static int -parse_insert (struct lexer *lexer, char **filename) +int +cmd_include (struct lexer *lexer, struct dataset *ds) { - char *target_fn; - char *relative_filename; - - /* Skip optional FILE=. */ - if (lex_match_id (lexer, "FILE")) - lex_match (lexer, '='); - - /* File name can be identifier or string. */ - if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING) - { - lex_error (lexer, _("expecting file name")); - return CMD_FAILURE; - } - - target_fn = ds_cstr (lex_tokstr (lexer)); - - relative_filename = - fn_search_path (target_fn, - getl_include_path (lex_get_source_stream (lexer))); - - if ( ! relative_filename) - { - msg (SE, _("Can't find `%s' in include file search path."), - target_fn); - return CMD_FAILURE; - } - - *filename = relative_filename; - if (*filename == NULL) - { - msg (SE, _("Unable to open `%s': %s."), - relative_filename, strerror (errno)); - free (relative_filename); - return CMD_FAILURE; - } + return do_insert (lexer, ds, INCLUDE); +} - return CMD_SUCCESS; +int +cmd_insert (struct lexer *lexer, struct dataset *ds) +{ + return do_insert (lexer, ds, INSERT); } +