X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fdictionary%2Fvector.c;h=35e47380555ffb6693b5251dc97474d19a501435;hb=7be27d2d9fb7f5d62523c744599bc4cc2f3fc1ea;hp=512ec3c2bfeaf99cde80aae28644b08f55225b1a;hpb=6f3865480503c571963d8a2d1af858a4d72d4e88;p=pspp diff --git a/src/language/dictionary/vector.c b/src/language/dictionary/vector.c index 512ec3c2bf..35e4738055 100644 --- a/src/language/dictionary/vector.c +++ b/src/language/dictionary/vector.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2010, 2011, 2012 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2010, 2011, 2012, 2016 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 @@ -48,38 +48,46 @@ cmd_vector (struct lexer *lexer, struct dataset *ds) do { char **vectors; - size_t vector_cnt, vector_cap; + size_t n_vectors, allocated_vectors; /* Get the name(s) of the new vector(s). */ - if (!lex_force_id (lexer) - || !dict_id_is_valid (dict, lex_tokcstr (lexer), true)) + if (!lex_force_id (lexer)) return CMD_CASCADING_FAILURE; + char *error = dict_id_is_valid__ (dict, lex_tokcstr (lexer)); + if (error) + { + lex_error (lexer, "%s", error); + free (error); + return CMD_CASCADING_FAILURE; + } vectors = NULL; - vector_cnt = vector_cap = 0; + n_vectors = allocated_vectors = 0; while (lex_token (lexer) == T_ID) { size_t i; if (dict_lookup_vector (dict, lex_tokcstr (lexer))) { - msg (SE, _("A vector named %s already exists."), - lex_tokcstr (lexer)); + lex_next_error (lexer, 0, 0, + _("A vector named %s already exists."), + lex_tokcstr (lexer)); goto fail; } - for (i = 0; i < vector_cnt; i++) + for (i = 0; i < n_vectors; i++) if (!utf8_strcasecmp (vectors[i], lex_tokcstr (lexer))) { - msg (SE, _("Vector name %s is given twice."), - lex_tokcstr (lexer)); + lex_next_error (lexer, 0, 0, + _("Vector name %s is given twice."), + lex_tokcstr (lexer)); goto fail; } - if (vector_cnt == vector_cap) - vectors = pool_2nrealloc (pool, - vectors, &vector_cap, sizeof *vectors); - vectors[vector_cnt++] = pool_strdup (pool, lex_tokcstr (lexer)); + if (n_vectors == allocated_vectors) + vectors = pool_2nrealloc (pool, vectors, &allocated_vectors, + sizeof *vectors); + vectors[n_vectors++] = pool_strdup (pool, lex_tokcstr (lexer)); lex_get (lexer); lex_match (lexer, T_COMMA); @@ -93,10 +101,10 @@ cmd_vector (struct lexer *lexer, struct dataset *ds) struct variable **v; size_t nv; - if (vector_cnt > 1) + if (n_vectors > 1) { - msg (SE, _("A slash must separate each vector " - "specification in VECTOR's long form.")); + lex_error (lexer, _("A slash must separate each vector " + "specification in VECTOR's long form.")); goto fail; } @@ -109,36 +117,31 @@ cmd_vector (struct lexer *lexer, struct dataset *ds) else if (lex_match (lexer, T_LPAREN)) { /* Short form. */ - struct fmt_spec format; + struct fmt_spec format = fmt_for_output (FMT_F, 8, 2); bool seen_format = false; - - struct variable **vars; - int var_cnt; - - size_t i; - - var_cnt = 0; - format = fmt_for_output (FMT_F, 8, 2); - seen_format = false; + size_t n_vars = 0; + int start_ofs = lex_ofs (lexer) - 2; while (!lex_match (lexer, T_RPAREN)) { - if (lex_is_integer (lexer) && var_cnt == 0) + if (lex_is_integer (lexer) && n_vars == 0) { - var_cnt = lex_integer (lexer); + if (!lex_force_int_range (lexer, NULL, 1, INT_MAX)) + goto fail; + n_vars = lex_integer (lexer); lex_get (lexer); - if (var_cnt <= 0) - { - msg (SE, _("Vectors must have at least one element.")); - goto fail; - } } else if (lex_token (lexer) == T_ID && !seen_format) { seen_format = true; - if (!parse_format_specifier (lexer, &format) - || !fmt_check_output (&format) - || !fmt_check_type_compat (&format, VAL_NUMERIC)) + if (!parse_format_specifier (lexer, &format)) goto fail; + char *error = fmt_check_output__ (&format); + if (error) + { + lex_next_error (lexer, -1, -1, "%s", error); + free (error); + goto fail; + } } else { @@ -147,29 +150,35 @@ cmd_vector (struct lexer *lexer, struct dataset *ds) } lex_match (lexer, T_COMMA); } - if (var_cnt == 0) + int end_ofs = lex_ofs (lexer) - 1; + if (n_vars == 0) { - lex_error (lexer, _("expecting vector length")); + lex_error (lexer, _("Syntax error expecting vector length.")); goto fail; } /* Check that none of the variables exist and that their names are not excessively long. */ - for (i = 0; i < vector_cnt; i++) + for (size_t i = 0; i < n_vectors; i++) { int j; - for (j = 0; j < var_cnt; j++) + for (j = 0; j < n_vars; j++) { char *name = xasprintf ("%s%d", vectors[i], j + 1); - if (!dict_id_is_valid (dict, name, true)) + char *error = dict_id_is_valid__ (dict, name); + if (error) { + lex_ofs_error (lexer, start_ofs, end_ofs, "%s", error); + free (error); free (name); goto fail; } if (dict_lookup_var (dict, name)) { + lex_ofs_error (lexer, start_ofs, end_ofs, + _("%s is an existing variable name."), + name); free (name); - msg (SE, _("%s is an existing variable name."), name); goto fail; } free (name); @@ -177,18 +186,18 @@ cmd_vector (struct lexer *lexer, struct dataset *ds) } /* Finally create the variables and vectors. */ - vars = pool_nmalloc (pool, var_cnt, sizeof *vars); - for (i = 0; i < vector_cnt; i++) + struct variable **vars = pool_nmalloc (pool, n_vars, sizeof *vars); + for (size_t i = 0; i < n_vectors; i++) { - int j; - for (j = 0; j < var_cnt; j++) + for (size_t j = 0; j < n_vars; j++) { - char *name = xasprintf ("%s%d", vectors[i], j + 1); - vars[j] = dict_create_var_assert (dict, name, 0); + char *name = xasprintf ("%s%zu", vectors[i], j + 1); + vars[j] = dict_create_var_assert (dict, name, + fmt_var_width (&format)); var_set_both_formats (vars[j], &format); free (name); } - dict_create_vector_assert (dict, vectors[i], vars, var_cnt); + dict_create_vector_assert (dict, vectors[i], vars, n_vars); } } else