From: John Darrington Date: Tue, 29 Mar 2016 11:17:25 +0000 (+0200) Subject: Cleanup the RENAME VARIABLES command. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=77d9752b0182d3a6ea4c03e0e241ede7414b9bbb;p=pspp Cleanup the RENAME VARIABLES command. Also fix some crashes which occured on invalid syntax. --- diff --git a/src/language/dictionary/rename-variables.c b/src/language/dictionary/rename-variables.c index 7c3f8bdf99..33d8f6887f 100644 --- a/src/language/dictionary/rename-variables.c +++ b/src/language/dictionary/rename-variables.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2010, 2011, 2015 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2010, 2011, 2015, 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 @@ -37,9 +37,12 @@ int cmd_rename_variables (struct lexer *lexer, struct dataset *ds) { - struct variable **rename_vars = NULL; - char **rename_new_names = NULL; - size_t rename_cnt = 0; + struct variable **vars_to_be_renamed = NULL; + size_t n_vars_to_be_renamed = 0; + + char **new_names = NULL; + size_t n_new_names = 0; + char *err_name; int status = CMD_CASCADING_FAILURE; @@ -50,40 +53,40 @@ cmd_rename_variables (struct lexer *lexer, struct dataset *ds) do { - size_t prev_nv_1 = rename_cnt; - size_t prev_nv_2 = rename_cnt; int opts = PV_APPEND | PV_NO_DUPLICATE; if (!lex_match (lexer, T_LPAREN)) opts |= PV_SINGLE; if (!parse_variables (lexer, dataset_dict (ds), - &rename_vars, &rename_cnt, opts)) - goto lossage; + &vars_to_be_renamed, &n_vars_to_be_renamed, opts)) + { + goto lossage; + } if (!lex_force_match (lexer, T_EQUALS)) - goto lossage; + { + goto lossage; + } if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds), - &rename_new_names, &prev_nv_1, opts)) - goto lossage; - if (prev_nv_1 != rename_cnt) + &new_names, &n_new_names, opts)) + { + goto lossage; + } + if (n_new_names != n_vars_to_be_renamed) { - size_t i; - msg (SE, _("Differing number of variables in old name list " "(%zu) and in new name list (%zu)."), - rename_cnt - prev_nv_2, prev_nv_1 - prev_nv_2); - for (i = 0; i < prev_nv_1; i++) - free (rename_new_names[i]); - free (rename_new_names); - rename_new_names = NULL; + n_vars_to_be_renamed, n_new_names); goto lossage; } if (!(opts & PV_SINGLE) && !lex_force_match (lexer, T_RPAREN)) - goto lossage; + { + goto lossage; + } } while (lex_token (lexer) != T_ENDCMD); if (!dict_rename_vars (dataset_dict (ds), - rename_vars, rename_new_names, rename_cnt, + vars_to_be_renamed, new_names, n_new_names, &err_name)) { msg (SE, _("Renaming would duplicate variable name %s."), err_name); @@ -93,13 +96,13 @@ cmd_rename_variables (struct lexer *lexer, struct dataset *ds) status = CMD_SUCCESS; lossage: - free (rename_vars); - if (rename_new_names != NULL) + free (vars_to_be_renamed); + if (new_names != NULL) { size_t i; - for (i = 0; i < rename_cnt; i++) - free (rename_new_names[i]); - free (rename_new_names); + for (i = 0; i < n_new_names; ++i) + free (new_names[i]); + free (new_names); } return status; } diff --git a/tests/language/dictionary/rename-variables.at b/tests/language/dictionary/rename-variables.at index f528dcb046..4a88741967 100644 --- a/tests/language/dictionary/rename-variables.at +++ b/tests/language/dictionary/rename-variables.at @@ -19,8 +19,7 @@ LIST. SAVE /OUTFILE='rename.sav'. ]) -AT_CHECK([pspp -o pspp.csv rename-variables.sps]) -AT_CHECK([cat pspp.csv], [0], [dnl +AT_CHECK([pspp -O format=csv rename-variables.sps], [0], [dnl Table: Reading free-form data from INLINE. Variable,Format brakeFluid,F2.0 @@ -45,11 +44,43 @@ applecarts,bananamobiles,xyzzy,foobar AT_CHECK([grep '[bB][rR][aA][kK][eE]' rename.sav], [1], [ignore-nolog]) AT_CLEANUP + +AT_SETUP([RENAME VARIABLES -- multiple sets]) +AT_DATA([rename-variables.sps], [dnl +data list list /a b c d e *. +begin data. +1 2 3 4 5 +end data. + +rename variables (a b=x y) (c d e=z zz zzz). + +list. +]) + +AT_CHECK([pspp -O format=csv rename-variables.sps], [0], [dnl +Table: Reading free-form data from INLINE. +Variable,Format +a,F8.0 +b,F8.0 +c,F8.0 +d,F8.0 +e,F8.0 + +Table: Data List +x,y,z,zz,zzz +1.00,2.00,3.00,4.00,5.00 +]) + +AT_CLEANUP + + AT_SETUP([RENAME VARIABLES -- invalid syntax 1]) + AT_DATA([rename-variables.sps], [dnl DATA LIST LIST /brakeFluid y auxiliary warp (F2.0). RENAME VARIABLES warp auxiliary=foobar xyzzy. ]) + AT_CHECK([pspp -o pspp.csv rename-variables.sps], [1], [dnl rename-variables.sps:2.23-2.31: error: RENAME VARIABLES: Syntax error at `auxiliary': expecting `='. ]) @@ -60,7 +91,28 @@ AT_DATA([rename-variables.sps], [dnl DATA LIST LIST /brakeFluid y auxiliary warp (F2.0). RENAME VARIABLES (brakeFluid=applecarts y=bananamobiles). ]) + AT_CHECK([pspp -o pspp.csv rename-variables.sps], [1], [dnl rename-variables.sps:2: error: RENAME VARIABLES: Differing number of variables in old name list (1) and in new name list (2). ]) AT_CLEANUP + + + + +AT_SETUP([RENAME VARIABLES -- invalid syntax 3]) +AT_DATA([rename-variables.sps], [dnl +DATA LIST NOTABLE LIST /z y p q (F2.0). +BEGIN DATA. +4 3 8 11 +END DATA. + +RENAME VARIABLES z=a y}bqnanamobiles. + +LIST. +]) + +AT_CHECK([pspp -O format=csv rename-variables.sps], [1], [ignore]) + + +AT_CLEANUP