1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "language/command.h"
21 #include "data/dataset.h"
22 #include "data/session.h"
23 #include "language/lexer/lexer.h"
24 #include "libpspp/message.h"
25 #include "output/tab.h"
28 #define _(msgid) gettext (msgid)
31 parse_window (struct lexer *lexer, unsigned int allowed,
32 enum dataset_display def)
34 if (!lex_match_id (lexer, "WINDOW"))
36 lex_match (lexer, T_EQUALS);
38 if (allowed & (1 << DATASET_MINIMIZED) && lex_match_id (lexer, "MINIMIZED"))
39 return DATASET_MINIMIZED;
40 else if (allowed & (1 << DATASET_ASIS) && lex_match_id (lexer, "ASIS"))
42 else if (allowed & (1 << DATASET_FRONT) && lex_match_id (lexer, "FRONT"))
44 else if (allowed & (1 << DATASET_HIDDEN) && lex_match_id (lexer, "HIDDEN"))
45 return DATASET_HIDDEN;
47 lex_error (lexer, NULL);
51 static struct dataset *
52 parse_dataset_name (struct lexer *lexer, struct session *session)
56 if (!lex_force_id (lexer))
59 ds = session_lookup_dataset (session, lex_tokcstr (lexer));
63 msg (SE, _("There is no dataset named %s."), lex_tokcstr (lexer));
68 cmd_dataset_name (struct lexer *lexer, struct dataset *active)
72 if (!lex_force_id (lexer))
74 dataset_set_name (active, lex_tokcstr (lexer));
77 display = parse_window (lexer, (1 << DATASET_ASIS) | (1 << DATASET_FRONT),
81 else if (display != DATASET_ASIS)
82 dataset_set_display (active, display);
88 cmd_dataset_activate (struct lexer *lexer, struct dataset *active)
90 struct session *session = dataset_session (active);
94 ds = parse_dataset_name (lexer, session);
100 proc_execute (active);
101 session_set_active_dataset (session, ds);
102 if (dataset_name (active)[0] == '\0')
103 dataset_destroy (active);
107 display = parse_window (lexer, (1 << DATASET_ASIS) | (1 << DATASET_FRONT),
111 else if (display != DATASET_ASIS)
112 dataset_set_display (ds, display);
118 cmd_dataset_copy (struct lexer *lexer, struct dataset *old)
120 struct session *session = dataset_session (old);
125 /* Parse the entire command first. proc_execute() can attempt to parse
126 BEGIN DATA...END DATA and it will fail confusingly if we are in the
127 middle of the command at the point. */
128 if (!lex_force_id (lexer))
130 name = xstrdup (lex_tokcstr (lexer));
133 display = parse_window (lexer, ((1 << DATASET_MINIMIZED)
134 | (1 << DATASET_HIDDEN)
135 | (1 << DATASET_FRONT)),
143 if (session_lookup_dataset (session, name) == old)
146 dataset_set_name (old, "");
151 new = dataset_clone (old, name);
153 dataset_set_display (new, display);
160 cmd_dataset_declare (struct lexer *lexer, struct dataset *ds)
162 struct session *session = dataset_session (ds);
166 if (!lex_force_id (lexer))
169 new = session_lookup_dataset (session, lex_tokcstr (lexer));
171 new = dataset_create (session, lex_tokcstr (lexer));
174 display = parse_window (lexer, ((1 << DATASET_MINIMIZED)
175 | (1 << DATASET_HIDDEN)
176 | (1 << DATASET_FRONT)),
180 dataset_set_display (new, display);
186 dataset_close_cb (struct dataset *ds, void *session_)
188 struct session *session = session_;
190 if (ds != session_active_dataset (session))
191 dataset_destroy (ds);
195 cmd_dataset_close (struct lexer *lexer, struct dataset *ds)
197 struct session *session = dataset_session (ds);
199 if (lex_match (lexer, T_ALL))
201 session_for_each_dataset (session, dataset_close_cb, session);
202 dataset_set_name (session_active_dataset (session), "");
206 if (!lex_match (lexer, T_ASTERISK))
208 ds = parse_dataset_name (lexer, session);
213 if (ds == session_active_dataset (session))
214 dataset_set_name (ds, "");
216 dataset_destroy (ds);
223 dataset_display_cb (struct dataset *ds, void *p_)
225 struct dataset ***p = p_;
231 sort_datasets (const void *a_, const void *b_)
233 struct dataset *const *a = a_;
234 struct dataset *const *b = b_;
236 return strcmp (dataset_name (*a), dataset_name (*b));
240 cmd_dataset_display (struct lexer *lexer UNUSED, struct dataset *ds)
242 struct session *session = dataset_session (ds);
243 struct dataset **datasets, **p;
247 n = session_n_datasets (session);
248 datasets = xmalloc (n * sizeof *datasets);
250 session_for_each_dataset (session, dataset_display_cb, &p);
251 qsort (datasets, n, sizeof *datasets, sort_datasets);
253 t = tab_create (1, n + 1);
254 tab_headers (t, 0, 0, 1, 0);
255 tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, tab_nc (t) - 1, tab_nr (t) - 1);
256 tab_hline (t, TAL_2, 0, 0, 1);
257 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Dataset"));
258 for (i = 0; i < n; i++)
260 struct dataset *ds = datasets[i];
263 name = dataset_name (ds);
265 name = _("unnamed dataset");
267 if (ds == session_active_dataset (session))
268 tab_text_format (t, 0, i + 1, TAB_LEFT, "%s %s",
269 name, _("(active dataset)"));
271 tab_text (t, 0, i + 1, TAB_LEFT, name);
273 tab_title (t, "Open datasets.");