584b13762744afe820572dcdd7f0044c4725b5a4
[pspp-builds.git] / src / ui / gui / executor.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2009, 2010, 2011  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "ui/gui/executor.h"
20
21 #include "data/dataset.h"
22 #include "data/lazy-casereader.h"
23 #include "language/command.h"
24 #include "language/lexer/lexer.h"
25 #include "libpspp/cast.h"
26 #include "output/driver.h"
27 #include "ui/gui/psppire-data-store.h"
28 #include "ui/gui/psppire-output-window.h"
29
30 extern struct dataset *the_dataset;
31 extern PsppireDataStore *the_data_store;
32
33 /* Lazy casereader callback function used by execute_syntax. */
34 static struct casereader *
35 create_casereader_from_data_store (void *data_store_)
36 {
37   PsppireDataStore *data_store = data_store_;
38   return psppire_data_store_get_reader (data_store);
39 }
40
41 gboolean
42 execute_syntax (PsppireDataWindow *window, struct lex_reader *lex_reader)
43 {
44   struct lexer *lexer;
45   gboolean retval = TRUE;
46
47   struct casereader *reader;
48   const struct caseproto *proto;
49   casenumber case_cnt;
50   unsigned long int lazy_serial;
51
52   /* When the user executes a number of snippets of syntax in a
53      row, none of which read from the active dataset, the GUI becomes
54      progressively less responsive.  The reason is that each syntax
55      execution encapsulates the active dataset data in another
56      datasheet layer.  The cumulative effect of having a number of
57      layers of datasheets wastes time and space.
58
59      To solve the problem, we use a "lazy casereader", a wrapper
60      around the casereader obtained from the data store, that
61      only actually instantiates that casereader when it is
62      needed.  If the data store casereader is never needed, then
63      it is reused the next time syntax is run, without wrapping
64      it in another layer. */
65   proto = psppire_data_store_get_proto (the_data_store);
66   case_cnt = psppire_data_store_get_case_count (the_data_store);
67   reader = lazy_casereader_create (proto, case_cnt,
68                                    create_casereader_from_data_store,
69                                    the_data_store, &lazy_serial);
70   dataset_set_source (the_dataset, reader);
71
72   g_return_val_if_fail (dataset_has_source (the_dataset), FALSE);
73
74   lexer = lex_create ();
75   psppire_set_lexer (lexer);
76   lex_append (lexer, lex_reader);
77
78   for (;;)
79     {
80       enum cmd_result result = cmd_parse (lexer, the_dataset);
81
82       if ( cmd_result_is_failure (result))
83         {
84           retval = FALSE;
85           if ( lex_get_error_mode (lexer) == LEX_ERROR_STOP )
86             break;
87         }
88
89       if ( result == CMD_EOF || result == CMD_FINISH)
90         break;
91     }
92
93   proc_execute (the_dataset);
94
95   psppire_dict_replace_dictionary (the_data_store->dict,
96                                    dataset_dict (the_dataset));
97
98   reader = dataset_steal_source (the_dataset);
99   if (!lazy_casereader_destroy (reader, lazy_serial))
100     psppire_data_store_set_reader (the_data_store, reader);
101
102   /* Destroy the lexer only after obtaining the dataset, because the dataset
103      might depend on the lexer, if the casereader specifies inline data.  (In
104      such a case then we'll always get an error message--the inline data is
105      missing, otherwise it would have been parsed in the loop above.) */
106   lex_destroy (lexer);
107   psppire_set_lexer (NULL);
108
109   output_flush ();
110
111   return retval;
112 }
113
114 /* Executes null-terminated string SYNTAX as syntax.
115    Returns SYNTAX. */
116 gchar *
117 execute_syntax_string (PsppireDataWindow *window, gchar *syntax)
118 {
119   execute_const_syntax_string (window, syntax);
120   return syntax;
121 }
122
123 /* Executes null-terminated string SYNTAX as syntax. */
124 void
125 execute_const_syntax_string (PsppireDataWindow *window, const gchar *syntax)
126 {
127   execute_syntax (window, lex_reader_for_string (syntax));
128 }