1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012, 2014 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 "output/driver.h"
20 #include "output/driver-provider.h"
28 #include "data/file-handle-def.h"
29 #include "data/settings.h"
30 #include "libpspp/array.h"
31 #include "libpspp/assertion.h"
32 #include "libpspp/message.h"
33 #include "libpspp/llx.h"
34 #include "libpspp/string-map.h"
35 #include "libpspp/string-set.h"
36 #include "libpspp/str.h"
37 #include "output/message-item.h"
38 #include "output/output-item.h"
39 #include "output/text-item.h"
42 #include "gl/xalloc.h"
43 #include "gl/xmemdup0.h"
46 #define _(msgid) gettext (msgid)
50 struct llx_list drivers; /* Contains "struct output_driver"s. */
51 struct string deferred_syntax; /* TEXT_ITEM_SYNTAX being accumulated. */
52 char *command_name; /* Name of command being processed. */
55 static const struct output_driver_factory *factories[];
57 /* A stack of output engines.. */
58 static struct output_engine *engine_stack;
59 static size_t n_stack, allocated_stack;
61 static struct output_engine *
62 engine_stack_top (void)
65 return &engine_stack[n_stack - 1];
69 output_engine_push (void)
71 struct output_engine *e;
73 if (n_stack >= allocated_stack)
74 engine_stack = x2nrealloc (engine_stack, &allocated_stack,
75 sizeof *engine_stack);
77 e = &engine_stack[n_stack++];
78 llx_init (&e->drivers);
79 ds_init_empty (&e->deferred_syntax);
80 e->command_name = NULL;
84 output_engine_pop (void)
86 struct output_engine *e;
89 e = &engine_stack[--n_stack];
90 while (!llx_is_empty (&e->drivers))
92 struct output_driver *d = llx_pop_head (&e->drivers, &llx_malloc_mgr);
93 output_driver_destroy (d);
95 ds_destroy (&e->deferred_syntax);
96 free (e->command_name);
100 output_get_supported_formats (struct string_set *formats)
102 const struct output_driver_factory **fp;
104 for (fp = factories; *fp != NULL; fp++)
105 string_set_insert (formats, (*fp)->extension);
109 output_submit__ (struct output_engine *e, struct output_item *item)
111 struct llx *llx, *next;
113 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers); llx = next)
115 struct output_driver *d = llx_data (llx);
116 enum settings_output_type type;
118 next = llx_next (llx);
120 if (is_message_item (item))
122 const struct msg *m = message_item_get_msg (to_message_item (item));
123 if (m->severity == MSG_S_NOTE)
124 type = SETTINGS_OUTPUT_NOTE;
126 type = SETTINGS_OUTPUT_ERROR;
128 else if (is_text_item (item)
129 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
130 type = SETTINGS_OUTPUT_SYNTAX;
132 type = SETTINGS_OUTPUT_RESULT;
134 if (settings_get_output_routing (type) & d->device_type)
135 d->class->submit (d, item);
138 output_item_unref (item);
142 flush_deferred_syntax (struct output_engine *e)
144 if (!ds_is_empty (&e->deferred_syntax))
146 ds_trim (&e->deferred_syntax, ss_cstr ("\n"));
147 if (!ds_is_empty (&e->deferred_syntax))
149 char *syntax = ds_steal_cstr (&e->deferred_syntax);
150 output_submit__ (e, text_item_super (text_item_create_nocopy (
151 TEXT_ITEM_SYNTAX, syntax)));
157 is_syntax_item (const struct output_item *item)
159 return (is_text_item (item)
160 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
163 /* Submits ITEM to the configured output drivers, and transfers ownership to
164 the output subsystem. */
166 output_submit (struct output_item *item)
168 struct output_engine *e = engine_stack_top ();
173 if (is_syntax_item (item))
175 ds_put_cstr (&e->deferred_syntax, text_item_get_text (to_text_item (item)));
176 output_item_unref (item);
180 flush_deferred_syntax (e);
182 if (is_text_item (item))
184 const struct text_item *text_item = to_text_item (item);
185 const char *text = text_item_get_text (text_item);
186 enum text_item_type type = text_item_get_type (text_item);
188 if (type == TEXT_ITEM_COMMAND_OPEN)
190 free (e->command_name);
191 e->command_name = xstrdup (text);
193 else if (type == TEXT_ITEM_COMMAND_CLOSE)
195 free (e->command_name);
196 e->command_name = NULL;
199 else if (is_message_item (item))
201 struct message_item *message_item = to_message_item (item);
202 free (message_item->command_name);
203 message_item->command_name = (e->command_name
204 ? xstrdup (e->command_name)
208 output_submit__ (e, item);
211 /* Flushes output to screen devices, so that the user can see
212 output that doesn't fill up an entire page. */
216 struct output_engine *e = engine_stack_top ();
219 flush_deferred_syntax (e);
220 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers);
221 llx = llx_next (llx))
223 struct output_driver *d = llx_data (llx);
224 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
230 output_driver_init (struct output_driver *driver,
231 const struct output_driver_class *class,
232 const char *name, enum settings_output_devices type)
234 driver->class = class;
235 driver->name = xstrdup (name);
236 driver->device_type = type;
240 output_driver_destroy (struct output_driver *driver)
244 char *name = driver->name;
245 if (output_driver_is_registered (driver))
246 output_driver_unregister (driver);
247 if (driver->class->destroy)
248 driver->class->destroy (driver);
254 output_driver_get_name (const struct output_driver *driver)
259 static struct output_engine *
260 output_driver_get_engine (const struct output_driver *driver)
262 struct output_engine *e;
264 for (e = engine_stack; e < &engine_stack[n_stack]; e++)
265 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
272 output_driver_register (struct output_driver *driver)
274 struct output_engine *e = engine_stack_top ();
276 assert (!output_driver_is_registered (driver));
277 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
281 output_driver_unregister (struct output_driver *driver)
283 struct output_engine *e = output_driver_get_engine (driver);
286 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
291 output_driver_is_registered (const struct output_driver *driver)
293 return output_driver_get_engine (driver) != NULL;
296 extern const struct output_driver_factory txt_driver_factory;
297 extern const struct output_driver_factory list_driver_factory;
298 extern const struct output_driver_factory html_driver_factory;
299 extern const struct output_driver_factory csv_driver_factory;
300 extern const struct output_driver_factory odt_driver_factory;
302 extern const struct output_driver_factory pdf_driver_factory;
303 extern const struct output_driver_factory ps_driver_factory;
304 extern const struct output_driver_factory svg_driver_factory;
307 static const struct output_driver_factory *factories[] =
310 &list_driver_factory,
311 &html_driver_factory,
322 static const struct output_driver_factory *
323 find_factory (const char *format)
325 const struct output_driver_factory **fp;
327 for (fp = factories; *fp != NULL; fp++)
329 const struct output_driver_factory *f = *fp;
331 if (!strcmp (f->extension, format))
334 return &txt_driver_factory;
337 static enum settings_output_devices
338 default_device_type (const char *file_name)
340 return (!strcmp (file_name, "-")
341 ? SETTINGS_DEVICE_TERMINAL
342 : SETTINGS_DEVICE_LISTING);
345 struct output_driver *
346 output_driver_create (struct string_map *options)
348 enum settings_output_devices device_type;
349 const struct output_driver_factory *f;
350 struct output_driver *driver;
355 format = string_map_find_and_delete (options, "format");
356 file_name = string_map_find_and_delete (options, "output-file");
360 if (file_name != NULL)
362 const char *extension = strrchr (file_name, '.');
363 format = xstrdup (extension != NULL ? extension + 1 : "");
366 format = xstrdup ("txt");
368 f = find_factory (format);
370 if (file_name == NULL)
371 file_name = xstrdup (f->default_file_name);
373 /* XXX should use parse_enum(). */
374 device_string = string_map_find_and_delete (options, "device");
375 if (device_string == NULL || device_string[0] == '\0')
376 device_type = default_device_type (file_name);
377 else if (!strcmp (device_string, "terminal"))
378 device_type = SETTINGS_DEVICE_TERMINAL;
379 else if (!strcmp (device_string, "listing"))
380 device_type = SETTINGS_DEVICE_LISTING;
383 msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
384 device_string, "terminal", "listing");
385 device_type = default_device_type (file_name);
388 struct file_handle *fh = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
390 driver = f->create (fh, device_type, options);
393 const struct string_map_node *node;
396 STRING_MAP_FOR_EACH_KEY (key, node, options)
397 msg (MW, _("%s: unknown option `%s'"), file_name, key);
399 string_map_clear (options);
403 free (device_string);
409 output_driver_parse_option (const char *option, struct string_map *options)
411 const char *equals = strchr (option, '=');
414 error (0, 0, _("%s: output option missing `='"), option);
418 char *key = xmemdup0 (option, equals - option);
419 if (string_map_contains (options, key))
421 error (0, 0, _("%s: output option specified more than once"), key);
426 char *value = xmemdup0 (equals + 1, strlen (equals + 1));
427 string_map_insert_nocopy (options, key, value);