1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012, 2014, 2019 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"
30 #include "data/file-handle-def.h"
31 #include "data/settings.h"
32 #include "libpspp/array.h"
33 #include "libpspp/assertion.h"
34 #include "libpspp/i18n.h"
35 #include "libpspp/message.h"
36 #include "libpspp/llx.h"
37 #include "libpspp/string-map.h"
38 #include "libpspp/string-set.h"
39 #include "libpspp/str.h"
40 #include "output/output-item.h"
43 #include "gl/xalloc.h"
44 #include "gl/xmemdup0.h"
45 #include "gl/xvasprintf.h"
48 #define _(msgid) gettext (msgid)
52 struct ll ll; /* Node for this engine. */
53 struct llx_list drivers; /* Contains "struct output_driver"s. */
54 struct output_item *deferred_text; /* Output text being accumulated. */
55 char *command_name; /* Name of command being processed. */
56 char *title, *subtitle; /* Components of page title. */
58 /* Output grouping stack. */
59 struct output_item **groups;
61 size_t allocated_groups;
63 struct string_map heading_vars;
66 static struct ll_list engine_stack = LL_INITIALIZER (engine_stack);
68 static const struct output_driver_factory *factories[];
70 static struct output_engine *
71 engine_stack_top (void)
73 struct ll *head = ll_head (&engine_stack);
74 if (ll_is_empty (&engine_stack))
76 return ll_data (head, struct output_engine, ll);
80 put_strftime (const char *key, const char *format,
81 const struct tm *tm, struct string_map *vars)
83 if (!string_map_find (vars, key))
86 strftime (value, sizeof value, format, tm);
87 string_map_insert (vars, key, value);
92 output_engine_push (void)
94 struct output_engine *e = xzalloc (sizeof (*e));
96 llx_init (&e->drivers);
98 string_map_init (&e->heading_vars);
100 time_t t = time (NULL);
101 const struct tm *tm = localtime (&t);
102 put_strftime ("Date", "%x", tm, &e->heading_vars);
103 put_strftime ("Time", "%X", tm, &e->heading_vars);
105 ll_push_head (&engine_stack, &e->ll);
109 output_engine_pop (void)
111 struct ll *head = ll_pop_head (&engine_stack);
112 struct output_engine *e = ll_data (head, struct output_engine, ll);
114 struct output_driver *d;
115 llx_for_each_preremove (d, &e->drivers, &llx_malloc_mgr)
116 output_driver_destroy (d);
117 output_item_unref (e->deferred_text);
118 free (e->command_name);
122 output_item_unref (e->groups[0]);
124 string_map_destroy (&e->heading_vars);
129 output_get_supported_formats (struct string_set *formats)
131 const struct output_driver_factory **fp;
133 for (fp = factories; *fp != NULL; fp++)
134 string_set_insert (formats, (*fp)->extension);
138 output_driver_should_show (const struct output_driver *d,
139 const struct output_item *item)
141 enum settings_output_type type = SETTINGS_OUTPUT_RESULT;
144 case OUTPUT_ITEM_MESSAGE:
145 type = (item->message->severity == MSG_S_NOTE
146 ? SETTINGS_OUTPUT_NOTE
147 : SETTINGS_OUTPUT_ERROR);
150 case OUTPUT_ITEM_TEXT:
151 if (item->text.subtype == TEXT_ITEM_SYNTAX)
152 type = SETTINGS_OUTPUT_SYNTAX;
155 case OUTPUT_ITEM_CHART:
156 case OUTPUT_ITEM_GROUP:
157 case OUTPUT_ITEM_IMAGE:
158 case OUTPUT_ITEM_PAGE_BREAK:
159 case OUTPUT_ITEM_PAGE_SETUP:
160 case OUTPUT_ITEM_TABLE:
164 return (settings_get_output_routing (type) & d->device_type) != 0;
167 /* Adds to OUT the subset of IN that driver D should show, considering routing
168 and visibility of each item, and flattening groups for drivers that don't
169 handle them internally. */
171 make_driver_output_subset (const struct output_item *in,
172 const struct output_driver *d,
173 struct output_item *out)
175 if (in->type == OUTPUT_ITEM_GROUP)
177 /* If we should include the group itself, then clone IN inside OUT, and
178 add any children to the clone instead to OUT directly. */
179 if (output_driver_should_show (d, in) && d->class->handles_groups)
181 struct output_item *group = group_item_clone_empty (in);
182 group_item_add_child (out, group);
186 for (size_t i = 0; i < in->group.n_children; i++)
187 make_driver_output_subset (in->group.children[i], d, out);
191 if (output_driver_should_show (d, in)
192 && (in->show || d->class->handles_show))
193 group_item_add_child (out, output_item_ref (in));
198 output_submit__ (struct output_engine *e, struct output_item *item)
202 group_item_add_child (e->groups[e->n_groups - 1], item);
206 struct llx *llx, *next;
207 llx_for_each_safe (llx, next, &e->drivers)
209 struct output_driver *d = llx_data (llx);
211 struct output_item *root = root_item_create ();
212 make_driver_output_subset (item, d, root);
213 for (size_t i = 0; i < root->group.n_children; i++)
214 d->class->submit (d, root->group.children[i]);
215 output_item_unref (root);
218 output_item_unref (item);
222 flush_deferred_text (struct output_engine *e)
224 struct output_item *deferred_text = e->deferred_text;
227 e->deferred_text = NULL;
228 output_submit__ (e, deferred_text);
233 defer_text (struct output_engine *e, struct output_item *item)
235 if (item->type != OUTPUT_ITEM_TEXT)
238 if (!e->deferred_text)
239 e->deferred_text = output_item_unshare (item);
240 else if (text_item_append (e->deferred_text, item))
241 output_item_unref (item);
244 flush_deferred_text (e);
245 e->deferred_text = output_item_unshare (item);
250 /* Submits ITEM to the configured output drivers, and transfers ownership to
251 the output subsystem. */
253 output_submit (struct output_item *item)
255 struct output_engine *e = engine_stack_top ();
263 if (defer_text (e, item))
265 flush_deferred_text (e);
267 /* XXX heading_vars */
269 output_submit__ (e, item);
272 /* Returns the name of the command currently being parsed, or NULL if no
273 command is being parsed. */
275 output_get_command_name (void)
277 struct output_engine *e = engine_stack_top ();
281 for (size_t i = e->n_groups; i-- > 0;)
282 if (e->groups[i]->command_name)
283 return e->groups[i]->command_name;
289 output_get_uppercase_command_name (void)
291 const char *command_name = output_get_command_name ();
292 return command_name ? utf8_to_upper (command_name) : NULL;
296 output_open_group (struct output_item *item)
298 struct output_engine *e = engine_stack_top ();
302 if (e->n_groups >= e->allocated_groups)
303 e->groups = x2nrealloc (e->groups, &e->allocated_groups,
305 e->groups[e->n_groups++] = item;
307 group_item_add_child (e->groups[e->n_groups - 2], item);
309 return e->n_groups - 1;
313 output_close_groups (size_t nesting_level)
315 struct output_engine *e = engine_stack_top ();
319 while (e->n_groups > nesting_level)
321 flush_deferred_text (e);
323 struct output_item *group = e->groups[--e->n_groups];
324 if (e->n_groups == 0)
325 output_submit__ (e, group);
329 /* Flushes output to screen devices, so that the user can see
330 output that doesn't fill up an entire page. */
334 struct output_engine *e = engine_stack_top ();
336 flush_deferred_text (e);
339 llx_for_each (llx, &e->drivers)
341 struct output_driver *d = llx_data (llx);
342 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
348 output_set_title__ (struct output_engine *e, char **dst, const char *src)
351 *dst = xstrdup_if_nonnull (src);
354 = (e->title && e->subtitle ? xasprintf ("%s\n%s", e->title, e->subtitle)
355 : e->title ? xstrdup (e->title)
356 : e->subtitle ? xstrdup (e->subtitle)
358 output_item_submit (text_item_create_nocopy (TEXT_ITEM_PAGE_TITLE,
362 void PRINTF_FORMAT (1, 2)
363 output_log (const char *format, ...)
366 va_start (args, format);
367 char *s = xvasprintf (format, args);
370 output_submit (text_item_create_nocopy (TEXT_ITEM_LOG, s, NULL));
374 output_set_title (const char *title)
376 struct output_engine *e = engine_stack_top ();
378 output_set_title__ (e, &e->title, title);
382 output_set_subtitle (const char *subtitle)
384 struct output_engine *e = engine_stack_top ();
386 output_set_title__ (e, &e->subtitle, subtitle);
390 output_set_filename (const char *filename)
392 struct output_engine *e = engine_stack_top ();
394 string_map_replace (&e->heading_vars, "Filename", filename);
398 output_driver_init (struct output_driver *driver,
399 const struct output_driver_class *class,
400 const char *name, enum settings_output_devices type)
402 driver->class = class;
403 driver->name = xstrdup (name);
404 driver->device_type = type;
408 output_driver_destroy (struct output_driver *driver)
412 char *name = driver->name;
413 if (output_driver_is_registered (driver))
414 output_driver_unregister (driver);
415 if (driver->class->destroy)
416 driver->class->destroy (driver);
422 output_driver_get_name (const struct output_driver *driver)
427 static struct output_engine *
428 output_driver_get_engine (const struct output_driver *driver)
430 struct output_engine *e;
432 ll_for_each (e, struct output_engine, ll, &engine_stack)
434 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
442 output_driver_register (struct output_driver *driver)
444 struct output_engine *e = engine_stack_top ();
446 assert (!output_driver_is_registered (driver));
447 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
451 output_driver_unregister (struct output_driver *driver)
453 struct output_engine *e = output_driver_get_engine (driver);
456 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
461 output_driver_is_registered (const struct output_driver *driver)
463 return output_driver_get_engine (driver) != NULL;
466 extern const struct output_driver_factory csv_driver_factory;
467 extern const struct output_driver_factory html_driver_factory;
468 extern const struct output_driver_factory list_driver_factory;
469 extern const struct output_driver_factory odt_driver_factory;
470 extern const struct output_driver_factory pdf_driver_factory;
471 extern const struct output_driver_factory png_driver_factory;
472 extern const struct output_driver_factory ps_driver_factory;
473 extern const struct output_driver_factory spv_driver_factory;
474 extern const struct output_driver_factory svg_driver_factory;
475 extern const struct output_driver_factory tex_driver_factory;
476 extern const struct output_driver_factory txt_driver_factory;
478 static const struct output_driver_factory *factories[] =
481 &list_driver_factory,
482 &html_driver_factory,
494 static const struct output_driver_factory *
495 find_factory (const char *format)
497 const struct output_driver_factory **fp;
499 for (fp = factories; *fp != NULL; fp++)
501 const struct output_driver_factory *f = *fp;
503 if (!strcmp (f->extension, format))
506 return &txt_driver_factory;
509 static enum settings_output_devices
510 default_device_type (const char *file_name)
512 return (!strcmp (file_name, "-")
513 ? SETTINGS_DEVICE_TERMINAL
514 : SETTINGS_DEVICE_LISTING);
517 struct output_driver *
518 output_driver_create (struct string_map *options)
520 enum settings_output_devices device_type;
521 const struct output_driver_factory *f;
522 struct output_driver *driver;
527 format = string_map_find_and_delete (options, "format");
528 file_name = string_map_find_and_delete (options, "output-file");
532 if (file_name != NULL)
534 const char *extension = strrchr (file_name, '.');
535 format = xstrdup (extension != NULL ? extension + 1 : "");
538 format = xstrdup ("txt");
540 f = find_factory (format);
542 if (file_name == NULL)
543 file_name = xstrdup (f->default_file_name);
545 /* XXX should use parse_enum(). */
546 device_string = string_map_find_and_delete (options, "device");
547 if (device_string == NULL || device_string[0] == '\0')
548 device_type = default_device_type (file_name);
549 else if (!strcmp (device_string, "terminal"))
550 device_type = SETTINGS_DEVICE_TERMINAL;
551 else if (!strcmp (device_string, "listing"))
552 device_type = SETTINGS_DEVICE_LISTING;
555 msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
556 device_string, "terminal", "listing");
557 device_type = default_device_type (file_name);
560 struct file_handle *fh = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
562 driver = f->create (fh, device_type, options);
565 const struct string_map_node *node;
568 STRING_MAP_FOR_EACH_KEY (key, node, options)
569 msg (MW, _("%s: unknown option `%s'"), file_name, key);
571 string_map_clear (options);
575 free (device_string);
581 output_driver_parse_option (const char *option, struct string_map *options)
583 const char *equals = strchr (option, '=');
586 error (0, 0, _("%s: output option missing `='"), option);
590 char *key = xmemdup0 (option, equals - option);
591 if (string_map_contains (options, key))
593 error (0, 0, _("%s: output option specified more than once"), key);
598 char *value = xmemdup0 (equals + 1, strlen (equals + 1));
599 string_map_insert_nocopy (options, key, value);
603 output_driver_substitute_heading_vars (const char *src, int page_number)
605 struct output_engine *e = engine_stack_top ();
606 struct string dst = DS_EMPTY_INITIALIZER;
607 ds_extend (&dst, strlen (src));
608 for (const char *p = src; *p;)
610 if (!strncmp (p, "&[", 6))
612 if (page_number != INT_MIN)
614 const char *start = p + 6;
615 const char *end = strchr (start, ']');
618 const char *value = string_map_find__ (&e->heading_vars,
621 ds_put_cstr (&dst, value);
622 else if (ss_equals (ss_buffer (start, end - start),
624 ds_put_format (&dst, "%d", page_number);
629 ds_put_cstr (&dst, "&");
633 ds_put_byte (&dst, *p++);
635 return ds_steal_cstr (&dst);