+static int
+parse_get_txt (struct lexer *lexer, struct dataset *ds)
+{
+ struct data_parser *parser = NULL;
+ struct dictionary *dict = dict_create (get_default_encoding ());
+ struct file_handle *fh = NULL;
+ struct dfm_reader *reader = NULL;
+ char *encoding = NULL;
+ char *name = NULL;
+
+ int record;
+ enum data_parser_type type;
+ bool has_type;
+
+ lex_force_match (lexer, T_SLASH);
+
+ if (!lex_force_match_id (lexer, "FILE"))
+ goto error;
+ lex_force_match (lexer, T_EQUALS);
+ fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
+ if (fh == NULL)
+ goto error;
+
+ parser = data_parser_create (dict);
+ has_type = false;
+ data_parser_set_type (parser, DP_DELIMITED);
+ data_parser_set_span (parser, false);
+ data_parser_set_quotes (parser, ss_empty ());
+ data_parser_set_quote_escape (parser, true);
+ data_parser_set_empty_line_has_field (parser, true);
+
+ for (;;)
+ {
+ if (!lex_force_match (lexer, T_SLASH))
+ goto error;
+
+ if (lex_match_id (lexer, "ENCODING"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_string (lexer))
+ goto error;
+
+ free (encoding);
+ encoding = ss_xstrdup (lex_tokss (lexer));
+
+ lex_get (lexer);
+ }
+ else if (lex_match_id (lexer, "ARRANGEMENT"))
+ {
+ bool ok;
+
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "FIXED"))
+ ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
+ else if (lex_match_id (lexer, "DELIMITED"))
+ ok = set_type (parser, "ARRANGEMENT=DELIMITED",
+ DP_DELIMITED, &has_type);
+ else
+ {
+ lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
+ goto error;
+ }
+ if (!ok)
+ goto error;
+ }
+ else if (lex_match_id (lexer, "FIRSTCASE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_int (lexer))
+ goto error;
+ if (lex_integer (lexer) < 1)
+ {
+ msg (SE, _("Value of %s must be 1 or greater."), "FIRSTCASE");
+ goto error;
+ }
+ data_parser_set_skip (parser, lex_integer (lexer) - 1);
+ lex_get (lexer);
+ }
+ else if (lex_match_id_n (lexer, "DELCASE", 4))
+ {
+ if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "LINE"))
+ data_parser_set_span (parser, false);
+ else if (lex_match_id (lexer, "VARIABLES"))
+ {
+ data_parser_set_span (parser, true);
+
+ /* VARIABLES takes an integer argument, but for no
+ good reason. We just ignore it. */
+ if (!lex_force_int (lexer))
+ goto error;
+ lex_get (lexer);
+ }
+ else
+ {
+ lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "FIXCASE"))
+ {
+ if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_int (lexer))
+ goto error;
+ if (lex_integer (lexer) < 1)
+ {
+ msg (SE, _("Value of %s must be 1 or greater."), "FIXCASE");
+ goto error;
+ }
+ data_parser_set_records (parser, lex_integer (lexer));
+ lex_get (lexer);
+ }
+ else if (lex_match_id (lexer, "IMPORTCASES"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match (lexer, T_ALL))
+ {
+ /* Nothing to do. */
+ }
+ else if (lex_match_id (lexer, "FIRST"))
+ {
+ if (!lex_force_int (lexer))
+ goto error;
+ lex_get (lexer);
+ }
+ else if (lex_match_id (lexer, "PERCENT"))
+ {
+ if (!lex_force_int (lexer))
+ goto error;
+ lex_get (lexer);
+ }
+ msg (SW, _("Ignoring obsolete IMPORTCASES subcommand. (N OF CASES "
+ "or SAMPLE may be used to substitute.)"));
+ }
+ else if (lex_match_id_n (lexer, "DELIMITERS", 4))
+ {
+ struct string hard_seps = DS_EMPTY_INITIALIZER;
+ const char *soft_seps = "";
+ struct substring s;
+ int c;
+
+ if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+
+ if (!lex_force_string (lexer))
+ goto error;
+
+ /* XXX should support multibyte UTF-8 characters */
+ s = lex_tokss (lexer);
+ if (ss_match_string (&s, ss_cstr ("\\t")))
+ ds_put_cstr (&hard_seps, "\t");
+ if (ss_match_string (&s, ss_cstr ("\\\\")))
+ ds_put_cstr (&hard_seps, "\\");
+ while ((c = ss_get_byte (&s)) != EOF)
+ if (c == ' ')
+ soft_seps = " ";
+ else
+ ds_put_byte (&hard_seps, c);
+ data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
+ data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
+ ds_destroy (&hard_seps);
+
+ lex_get (lexer);
+ }
+ else if (lex_match_id (lexer, "QUALIFIERS"))
+ {
+ if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+
+ if (!lex_force_string (lexer))
+ goto error;
+
+ /* XXX should support multibyte UTF-8 characters */
+ if (settings_get_syntax () == COMPATIBLE
+ && ss_length (lex_tokss (lexer)) != 1)
+ {
+ msg (SE, _("In compatible syntax mode, the QUALIFIER string "
+ "must contain exactly one character."));
+ goto error;
+ }
+
+ data_parser_set_quotes (parser, lex_tokss (lexer));
+ lex_get (lexer);
+ }
+ else if (lex_match_id (lexer, "VARIABLES"))
+ break;
+ else
+ {
+ lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
+ goto error;
+ }
+ }
+ lex_match (lexer, T_EQUALS);
+
+ record = 1;
+ type = data_parser_get_type (parser);
+ do
+ {
+ struct fmt_spec input, output;
+ struct variable *v;
+ int fc, lc;
+
+ while (type == DP_FIXED && lex_match (lexer, T_SLASH))
+ {
+ if (!lex_force_int (lexer))
+ goto error;
+ if (lex_integer (lexer) < record)
+ {
+ msg (SE, _("The record number specified, %ld, is at or "
+ "before the previous record, %d. Data "
+ "fields must be listed in order of "
+ "increasing record number."),
+ lex_integer (lexer), record);
+ goto error;
+ }
+ if (lex_integer (lexer) > data_parser_get_records (parser))
+ {
+ msg (SE, _("The record number specified, %ld, exceeds "
+ "the number of records per case specified "
+ "on FIXCASE, %d."),
+ lex_integer (lexer), data_parser_get_records (parser));
+ goto error;
+ }
+ record = lex_integer (lexer);
+ lex_get (lexer);
+ }
+
+ if (!lex_force_id (lexer)
+ || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
+ goto error;
+ name = xstrdup (lex_tokcstr (lexer));
+ lex_get (lexer);
+
+ if (type == DP_DELIMITED)
+ {
+ if (!parse_format_specifier (lexer, &input)
+ || !fmt_check_input (&input))
+ goto error;
+
+ output = fmt_for_output_from_input (&input);
+ }
+ else
+ {
+ char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
+ enum fmt_type fmt_type;
+ int w, d;
+
+ if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
+ goto error;
+
+ /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
+ if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
+ goto error;
+ if (!fmt_from_name (fmt_type_name, &fmt_type))
+ {
+ msg (SE, _("Unknown format type `%s'."), fmt_type_name);
+ goto error;
+ }
+
+ /* Compose input format. */
+ input.type = fmt_type;
+ input.w = lc - fc + 1;
+ input.d = 0;
+ if (!fmt_check_input (&input))
+ goto error;
+
+ /* Compose output format. */
+ if (w != 0)
+ {
+ output.type = fmt_type;
+ output.w = w;
+ output.d = d;
+ if (!fmt_check_output (&output))
+ goto error;
+ }
+ else
+ output = fmt_for_output_from_input (&input);
+ }
+
+ v = dict_create_var (dict, name, fmt_var_width (&input));
+ if (v == NULL)
+ {
+ msg (SE, _("%s is a duplicate variable name."), name);
+ goto error;
+ }
+ var_set_both_formats (v, &output);
+
+ if (type == DP_DELIMITED)
+ data_parser_add_delimited_field (parser, &input,
+ var_get_case_index (v),
+ name);
+ else
+ data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
+ name, record, fc);
+ free (name);
+ name = NULL;
+ }
+ while (lex_token (lexer) != T_ENDCMD);
+
+ reader = dfm_open_reader (fh, lexer, encoding);
+ if (reader == NULL)
+ goto error;
+
+ data_parser_make_active_file (parser, ds, reader, dict);
+ fh_unref (fh);
+ free (encoding);