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/group-item.h"
38 #include "output/message-item.h"
39 #include "output/output-item.h"
40 #include "output/text-item.h"
43 #include "gl/xalloc.h"
44 #include "gl/xmemdup0.h"
47 #define _(msgid) gettext (msgid)
51 struct llx_list drivers; /* Contains "struct output_driver"s. */
52 struct string deferred_syntax; /* TEXT_ITEM_SYNTAX being accumulated. */
53 char *command_name; /* Name of command being processed. */
54 char *title, *subtitle; /* Components of page title. */
56 /* Output grouping stack.
58 TEXT_ITEM_GROUP_OPEN pushes a group on the stack and
59 TEXT_ITEM_GROUP_CLOSE pops one off. */
60 char **groups; /* Command names of nested sections. */
62 size_t allocated_groups;
65 static const struct output_driver_factory *factories[];
67 /* A stack of output engines.. */
68 static struct output_engine *engine_stack;
69 static size_t n_stack, allocated_stack;
71 static struct output_engine *
72 engine_stack_top (void)
75 return &engine_stack[n_stack - 1];
79 output_engine_push (void)
81 struct output_engine *e;
83 if (n_stack >= allocated_stack)
84 engine_stack = x2nrealloc (engine_stack, &allocated_stack,
85 sizeof *engine_stack);
87 e = &engine_stack[n_stack++];
88 memset (e, 0, sizeof *e);
89 llx_init (&e->drivers);
90 ds_init_empty (&e->deferred_syntax);
96 output_engine_pop (void)
98 struct output_engine *e;
100 assert (n_stack > 0);
101 e = &engine_stack[--n_stack];
102 while (!llx_is_empty (&e->drivers))
104 struct output_driver *d = llx_pop_head (&e->drivers, &llx_malloc_mgr);
105 output_driver_destroy (d);
107 ds_destroy (&e->deferred_syntax);
108 free (e->command_name);
111 for (size_t i = 0; i < e->n_groups; i++)
117 output_get_supported_formats (struct string_set *formats)
119 const struct output_driver_factory **fp;
121 for (fp = factories; *fp != NULL; fp++)
122 string_set_insert (formats, (*fp)->extension);
126 output_submit__ (struct output_engine *e, struct output_item *item)
128 struct llx *llx, *next;
130 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers); llx = next)
132 struct output_driver *d = llx_data (llx);
133 enum settings_output_type type;
135 next = llx_next (llx);
137 if (is_message_item (item))
139 const struct msg *m = message_item_get_msg (to_message_item (item));
140 if (m->severity == MSG_S_NOTE)
141 type = SETTINGS_OUTPUT_NOTE;
143 type = SETTINGS_OUTPUT_ERROR;
145 else if (is_text_item (item)
146 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
147 type = SETTINGS_OUTPUT_SYNTAX;
149 type = SETTINGS_OUTPUT_RESULT;
151 if (settings_get_output_routing (type) & d->device_type)
152 d->class->submit (d, item);
155 output_item_unref (item);
159 flush_deferred_syntax (struct output_engine *e)
161 if (!ds_is_empty (&e->deferred_syntax))
163 ds_trim (&e->deferred_syntax, ss_cstr ("\n"));
164 if (!ds_is_empty (&e->deferred_syntax))
166 char *syntax = ds_steal_cstr (&e->deferred_syntax);
167 output_submit__ (e, text_item_super (text_item_create_nocopy (
168 TEXT_ITEM_SYNTAX, syntax)));
174 is_syntax_item (const struct output_item *item)
176 return (is_text_item (item)
177 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
180 /* Submits ITEM to the configured output drivers, and transfers ownership to
181 the output subsystem. */
183 output_submit (struct output_item *item)
185 struct output_engine *e = engine_stack_top ();
190 if (is_syntax_item (item))
192 ds_put_cstr (&e->deferred_syntax, text_item_get_text (to_text_item (item)));
193 output_item_unref (item);
197 flush_deferred_syntax (e);
199 if (is_group_open_item (item))
201 const struct group_open_item *group_open_item
202 = to_group_open_item (item);
203 if (e->n_groups >= e->allocated_groups)
204 e->groups = x2nrealloc (e->groups, &e->allocated_groups,
206 e->groups[e->n_groups] = (group_open_item->command_name
207 ? xstrdup (group_open_item->command_name)
211 else if (is_group_close_item (item))
213 assert (e->n_groups > 0);
215 size_t idx = --e->n_groups;
216 free (e->groups[idx]);
218 output_submit__ (e, item);
222 output_get_command_name (void)
226 struct output_engine *e = engine_stack_top ();
227 for (size_t i = e->n_groups; i-- > 0; )
235 /* Flushes output to screen devices, so that the user can see
236 output that doesn't fill up an entire page. */
240 struct output_engine *e = engine_stack_top ();
243 flush_deferred_syntax (e);
244 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers);
245 llx = llx_next (llx))
247 struct output_driver *d = llx_data (llx);
248 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
254 output_set_title__ (struct output_engine *e, char **dst, const char *src)
257 *dst = src ? xstrdup (src) : NULL;
260 = (e->title && e->subtitle ? xasprintf ("%s\n%s", e->title, e->subtitle)
261 : e->title ? xstrdup (e->title)
262 : e->subtitle ? xstrdup (e->subtitle)
264 text_item_submit (text_item_create_nocopy (TEXT_ITEM_PAGE_TITLE,
269 output_set_title (const char *title)
271 struct output_engine *e = engine_stack_top ();
273 output_set_title__ (e, &e->title, title);
277 output_set_subtitle (const char *subtitle)
279 struct output_engine *e = engine_stack_top ();
281 output_set_title__ (e, &e->subtitle, subtitle);
285 output_get_group_level (void)
287 struct output_engine *e = engine_stack_top ();
293 output_driver_init (struct output_driver *driver,
294 const struct output_driver_class *class,
295 const char *name, enum settings_output_devices type)
297 driver->class = class;
298 driver->name = xstrdup (name);
299 driver->device_type = type;
303 output_driver_destroy (struct output_driver *driver)
307 char *name = driver->name;
308 if (output_driver_is_registered (driver))
309 output_driver_unregister (driver);
310 if (driver->class->destroy)
311 driver->class->destroy (driver);
317 output_driver_get_name (const struct output_driver *driver)
322 static struct output_engine *
323 output_driver_get_engine (const struct output_driver *driver)
325 struct output_engine *e;
327 for (e = engine_stack; e < &engine_stack[n_stack]; e++)
328 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
335 output_driver_register (struct output_driver *driver)
337 struct output_engine *e = engine_stack_top ();
339 assert (!output_driver_is_registered (driver));
340 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
344 output_driver_unregister (struct output_driver *driver)
346 struct output_engine *e = output_driver_get_engine (driver);
349 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
354 output_driver_is_registered (const struct output_driver *driver)
356 return output_driver_get_engine (driver) != NULL;
359 extern const struct output_driver_factory txt_driver_factory;
360 extern const struct output_driver_factory list_driver_factory;
361 extern const struct output_driver_factory html_driver_factory;
362 extern const struct output_driver_factory csv_driver_factory;
363 extern const struct output_driver_factory odt_driver_factory;
365 extern const struct output_driver_factory pdf_driver_factory;
366 extern const struct output_driver_factory ps_driver_factory;
367 extern const struct output_driver_factory svg_driver_factory;
370 static const struct output_driver_factory *factories[] =
373 &list_driver_factory,
374 &html_driver_factory,
385 static const struct output_driver_factory *
386 find_factory (const char *format)
388 const struct output_driver_factory **fp;
390 for (fp = factories; *fp != NULL; fp++)
392 const struct output_driver_factory *f = *fp;
394 if (!strcmp (f->extension, format))
397 return &txt_driver_factory;
400 static enum settings_output_devices
401 default_device_type (const char *file_name)
403 return (!strcmp (file_name, "-")
404 ? SETTINGS_DEVICE_TERMINAL
405 : SETTINGS_DEVICE_LISTING);
408 struct output_driver *
409 output_driver_create (struct string_map *options)
411 enum settings_output_devices device_type;
412 const struct output_driver_factory *f;
413 struct output_driver *driver;
418 format = string_map_find_and_delete (options, "format");
419 file_name = string_map_find_and_delete (options, "output-file");
423 if (file_name != NULL)
425 const char *extension = strrchr (file_name, '.');
426 format = xstrdup (extension != NULL ? extension + 1 : "");
429 format = xstrdup ("txt");
431 f = find_factory (format);
433 if (file_name == NULL)
434 file_name = xstrdup (f->default_file_name);
436 /* XXX should use parse_enum(). */
437 device_string = string_map_find_and_delete (options, "device");
438 if (device_string == NULL || device_string[0] == '\0')
439 device_type = default_device_type (file_name);
440 else if (!strcmp (device_string, "terminal"))
441 device_type = SETTINGS_DEVICE_TERMINAL;
442 else if (!strcmp (device_string, "listing"))
443 device_type = SETTINGS_DEVICE_LISTING;
446 msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
447 device_string, "terminal", "listing");
448 device_type = default_device_type (file_name);
451 struct file_handle *fh = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
453 driver = f->create (fh, device_type, options);
456 const struct string_map_node *node;
459 STRING_MAP_FOR_EACH_KEY (key, node, options)
460 msg (MW, _("%s: unknown option `%s'"), file_name, key);
462 string_map_clear (options);
466 free (device_string);
472 output_driver_parse_option (const char *option, struct string_map *options)
474 const char *equals = strchr (option, '=');
477 error (0, 0, _("%s: output option missing `='"), option);
481 char *key = xmemdup0 (option, equals - option);
482 if (string_map_contains (options, key))
484 error (0, 0, _("%s: output option specified more than once"), key);
489 char *value = xmemdup0 (equals + 1, strlen (equals + 1));
490 string_map_insert_nocopy (options, key, value);