X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fdictionary%2Fvector.c;h=fe1ab99e145227b2e808d23920ff7ae9c50ed7f9;hb=f4e3578b2c8ce537de5516af55a62b84ebf2b744;hp=28e97b15d8aa9643475dfdbbcd1d964d164ebc3c;hpb=6849adbe175e0d548b430ef3d4e94b8ec0f18528;p=pspp diff --git a/src/language/dictionary/vector.c b/src/language/dictionary/vector.c index 28e97b15d8..fe1ab99e14 100644 --- a/src/language/dictionary/vector.c +++ b/src/language/dictionary/vector.c @@ -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,28 +117,18 @@ 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) { @@ -146,28 +144,34 @@ 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)) { - msg (SE, _("%s is an existing variable name."), name); + lex_ofs_error (lexer, start_ofs, end_ofs, + _("%s is an existing variable name."), + name); free (name); goto fail; } @@ -176,19 +180,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); + 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