MEANS: Prevent parser from entering infinite loop.
[pspp] / src / language / stats / means.c
index b14fe7849230a490bc2864a2566773812378dd03..8fa1ba81cd51f7326e228c6a2d2047a5804e74a1 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2011, 2012 Free Software Foundation, Inc.
+   Copyright (C) 2011, 2012, 2013 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
@@ -384,7 +384,7 @@ first_update (void *stat, double w UNUSED, double x)
   double *f = stat;
 
   if (*f == SYSMIS)
-     *f = x;
+    *f = x;
 }
 
 static double
@@ -395,7 +395,7 @@ first_get (const struct per_var_data *pvd UNUSED,  void *stat)
   return *f;
 }
 
-enum 
+enum
   {
     MEANS_MEAN = 0,
     MEANS_N,
@@ -502,43 +502,40 @@ parse_means_table_syntax (struct lexer *lexer, const struct means *cmd, struct m
   table->ii = 0;
   table->n_layers = 0;
   table->layers = NULL;
+  table->interactions = NULL;
 
   /* Dependent variable (s) */
-  if (!parse_variables_const (lexer, cmd->dict,
-                             &table->dep_vars, &table->n_dep_vars,
-                             PV_NO_DUPLICATE | PV_NUMERIC))
+  if (!parse_variables_const_pool (lexer, cmd->pool, cmd->dict,
+                                  &table->dep_vars, &table->n_dep_vars,
+                                  PV_NO_DUPLICATE | PV_NUMERIC))
     return false;
 
   /* Factor variable (s) */
-  while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
+  while (lex_match (lexer, T_BY))
     {
-      if (lex_match (lexer, T_BY))
-       {
-         table->n_layers++;
-          table->layers = 
-           pool_realloc (cmd->pool, table->layers, 
+      table->n_layers++;
+      table->layers =
+       pool_realloc (cmd->pool, table->layers,
                      sizeof (*table->layers) * table->n_layers);
 
-         if (!parse_variables_const 
-              (lexer, cmd->dict,
-               &table->layers[table->n_layers - 1].factor_vars,
-               &table->layers[table->n_layers - 1].n_factor_vars,
-               PV_NO_DUPLICATE))
-           return false;
-
-       }
+      if (!parse_variables_const_pool
+         (lexer, cmd->pool, cmd->dict,
+          &table->layers[table->n_layers - 1].factor_vars,
+          &table->layers[table->n_layers - 1].n_factor_vars,
+          PV_NO_DUPLICATE))
+       return false;
     }
 
   /* There is always at least one layer.
      However the final layer is the total, and not
-     normally considered by the user as a 
+     normally considered by the user as a
      layer.
   */
 
   table->n_layers++;
-  table->layers = 
-    pool_realloc (cmd->pool, table->layers, 
-              sizeof (*table->layers) * table->n_layers);
+  table->layers =
+    pool_realloc (cmd->pool, table->layers,
+                 sizeof (*table->layers) * table->n_layers);
   table->layers[table->n_layers - 1].factor_vars = NULL;
   table->layers[table->n_layers - 1].n_factor_vars = 0;
 
@@ -596,7 +593,8 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
   /*   Optional TABLES =   */
   if (lex_match_id (lexer, "TABLES"))
     {
-      lex_force_match (lexer, T_EQUALS);
+      if (! lex_force_match (lexer, T_EQUALS))
+       goto error;
     }
 
 
@@ -607,7 +605,7 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
       means.n_tables ++;
       means.table = pool_realloc (means.pool, means.table, means.n_tables * sizeof (*means.table));
 
-      if (! parse_means_table_syntax (lexer, &means, 
+      if (! parse_means_table_syntax (lexer, &means,
                                      &means.table[means.n_tables - 1]))
        {
          goto error;
@@ -620,7 +618,7 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
          if (lex_is_variable (lexer, means.dict, 1) )
            {
              more_tables = true;
-             lex_force_match (lexer, T_SLASH);
+             lex_match (lexer, T_SLASH);
            }
        }
     }
@@ -633,10 +631,10 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
       if (lex_match_id (lexer, "MISSING"))
        {
          /*
-            If no MISSING subcommand is specified, each combination of
-            a dependent variable and categorical variables is handled
-            separately.
-          */
+           If no MISSING subcommand is specified, each combination of
+           a dependent variable and categorical variables is handled
+           separately.
+         */
          lex_match (lexer, T_EQUALS);
          if (lex_match_id (lexer, "INCLUDE"))
            {
@@ -661,17 +659,17 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
            }
          else if (lex_match_id (lexer, "DEPENDENT"))
            /*
-            Use the command "/MISSING=DEPENDENT" to
-            include user-missing values for the categorical variables, 
-            while excluding them for the dependent variables.
+             Use the command "/MISSING=DEPENDENT" to
+             include user-missing values for the categorical variables,
+             while excluding them for the dependent variables.
 
-            Cases are dropped only when user-missing values
-            appear in dependent  variables.  User-missing
-            values for categorical variables are treated according to
-            their face value.
+             Cases are dropped only when user-missing values
+             appear in dependent  variables.  User-missing
+             values for categorical variables are treated according to
+             their face value.
 
-            Cases are ALWAYS dropped when System Missing values appear 
-            in the categorical variables.
+             Cases are ALWAYS dropped when System Missing values appear
+             in the categorical variables.
            */
            {
              means.dep_exclude = MV_ANY;
@@ -698,7 +696,7 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
                  int x;
                  means.cells =
                    pool_realloc (means.pool, means.cells,
-                             (means.n_cells += n_C) * sizeof (*means.cells));
+                                 (means.n_cells += n_C) * sizeof (*means.cells));
 
                  for (x = 0; x < n_C; ++x)
                    means.cells[means.n_cells - (n_C - 1 - x) - 1] = x;
@@ -711,8 +709,8 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
                {
                  means.cells =
                    pool_realloc (means.pool, means.cells,
-                             (means.n_cells += 3) * sizeof (*means.cells));
-                 
+                                 (means.n_cells += 3) * sizeof (*means.cells));
+
                  means.cells[means.n_cells - 2 - 1] = MEANS_MEAN;
                  means.cells[means.n_cells - 1 - 1] = MEANS_N;
                  means.cells[means.n_cells - 0 - 1] = MEANS_STDDEV;
@@ -725,8 +723,8 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
                        {
                          means.cells =
                            pool_realloc (means.pool, means.cells,
-                                     ++means.n_cells * sizeof (*means.cells));
-                       
+                                         ++means.n_cells * sizeof (*means.cells));
+
                          means.cells[means.n_cells - 1] = k;
                          break;
                        }
@@ -749,28 +747,28 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
 
 
   for (t = 0; t < means.n_tables; ++t)
-  {
-    struct mtable *table = &means.table[t];
+    {
+      struct mtable *table = &means.table[t];
 
-    table->interactions =
-      pool_calloc (means.pool, table->n_layers, sizeof (*table->interactions));
+      table->interactions =
+       pool_calloc (means.pool, table->n_layers, sizeof (*table->interactions));
 
-    table->summary =
-      pool_calloc (means.pool, table->n_dep_vars * table->n_layers, sizeof (*table->summary));
+      table->summary =
+       pool_calloc (means.pool, table->n_dep_vars * table->n_layers, sizeof (*table->summary));
 
-    for (l = 0; l < table->n_layers; ++l)
-      {
-        int v;
-        const struct layer *lyr = &table->layers[l];
-       const int n_vars = lyr->n_factor_vars;
-        table->interactions[l] = interaction_create (NULL);
-        for (v = 0 ; v < n_vars ; ++v)
-          {
-            interaction_add_variable (table->interactions[l],
-                                      lyr->factor_vars[v]);
-          }
-      }
-  }
+      for (l = 0; l < table->n_layers; ++l)
+       {
+         int v;
+         const struct layer *lyr = &table->layers[l];
+         const int n_vars = lyr->n_factor_vars;
+         table->interactions[l] = interaction_create (NULL);
+         for (v = 0; v < n_vars ; ++v)
+           {
+             interaction_add_variable (table->interactions[l],
+                                       lyr->factor_vars[v]);
+           }
+       }
+    }
 
   {
     struct casegrouper *grouper;
@@ -786,11 +784,32 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
     ok = proc_commit (ds) && ok;
   }
 
+  for (t = 0; t < means.n_tables; ++t)
+    {
+      int l;
+      struct mtable *table = &means.table[t];
+      if (table->interactions)
+       for (l = 0; l < table->n_layers; ++l)
+         {
+           interaction_destroy (table->interactions[l]);
+         }
+    }
 
   pool_destroy (means.pool);
   return CMD_SUCCESS;
 
-error:
+ error:
+
+  for (t = 0; t < means.n_tables; ++t)
+    {
+      int l;
+      struct mtable *table = &means.table[t];
+      if (table->interactions)
+       for (l = 0; l < table->n_layers; ++l)
+         {
+           interaction_destroy (table->interactions[l]);
+         }
+    }
 
   pool_destroy (means.pool);
   return CMD_FAILURE;
@@ -827,6 +846,22 @@ struct per_cat_data
   bool warn;
 };
 
+
+static void
+destroy_n (const void *aux1 UNUSED, void *aux2, void *user_data)
+{
+  struct mtable *table = aux2;
+  int v;
+  struct per_cat_data *per_cat_data = user_data;
+  struct per_var_data *pvd = per_cat_data->pvd;
+
+  for (v = 0; v < table->n_dep_vars; ++v)
+    {
+      struct per_var_data *pp = &pvd[v];
+      moments1_destroy (pp->mom);
+    }
+}
+
 static void *
 create_n (const void *aux1, void *aux2)
 {
@@ -843,7 +878,7 @@ create_n (const void *aux1, void *aux2)
       struct per_var_data *pp = &pvd[v];
 
       pp->cell_stats = pool_calloc (means->pool, means->n_cells, sizeof *pp->cell_stats);
-      
+
 
       for (i = 0; i < means->n_cells; ++i)
        {
@@ -938,19 +973,20 @@ run_means (struct means *cmd, struct casereader *input,
   struct payload payload;
   payload.create = create_n;
   payload.update = update_n;
-  payload.destroy = calculate_n;
-  
+  payload.calculate = calculate_n;
+  payload.destroy = destroy_n;
+
   for (t = 0; t < cmd->n_tables; ++t)
-  {
-    struct mtable *table = &cmd->table[t];
-    table->cats
-      = categoricals_create (table->interactions,
-                            table->n_layers, wv, cmd->dep_exclude, cmd->exclude);
+    {
+      struct mtable *table = &cmd->table[t];
+      table->cats
+       = categoricals_create (table->interactions,
+                              table->n_layers, wv, cmd->dep_exclude, cmd->exclude);
 
-    categoricals_set_payload (table->cats, &payload, cmd, table);
-  }
+      categoricals_set_payload (table->cats, &payload, cmd, table);
+    }
 
-  for (reader = casereader_clone (input);
+  for (reader = input;
        (c = casereader_read (reader)) != NULL; case_unref (c))
     {
       for (t = 0; t < cmd->n_tables; ++t)
@@ -1071,32 +1107,32 @@ output_case_processing_summary (const struct mtable *table)
                    TAB_LEFT | TAT_TITLE, ds_cstr (&str));
 
 
-         n_total = table->summary[row].missing + 
+         n_total = table->summary[row].missing +
            table->summary[row].non_missing;
 
          tab_double (t, 1, row + heading_rows,
-                     0, table->summary[row].non_missing, &F_8_0);
+                     0, table->summary[row].non_missing, NULL, RC_INTEGER);
 
          tab_text_format (t, 2, row + heading_rows,
-                          0, _("%g%%"), 
+                          0, _("%g%%"),
                           table->summary[row].non_missing / (double) n_total * 100.0);
 
 
          tab_double (t, 3, row + heading_rows,
-                     0, table->summary[row].missing, &F_8_0);
+                     0, table->summary[row].missing, NULL, RC_INTEGER);
 
 
          tab_text_format (t, 4, row + heading_rows,
-                          0, _("%g%%"), 
+                          0, _("%g%%"),
                           table->summary[row].missing / (double) n_total * 100.0);
 
 
          tab_double (t, 5, row + heading_rows,
-                     0, table->summary[row].missing + 
-                     table->summary[row].non_missing, &F_8_0);
+                     0, table->summary[row].missing +
+                     table->summary[row].non_missing, NULL, RC_INTEGER);
 
          tab_text_format (t, 6, row + heading_rows,
-                          0, _("%g%%"), 
+                          0, _("%g%%"),
                           n_total / (double) n_total * 100.0);
 
 
@@ -1202,7 +1238,7 @@ output_report (const struct means *cmd,  int iact_idx,
 
              tab_double (t, heading_columns + i,
                          heading_rows + grp + dv * n_cats,
-                         0, result, 0);
+                         0, result, NULL, RC_OTHER);
            }
        }
     }