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"
21 #include "output/options.h"
31 #include "data/file-handle-def.h"
32 #include "data/settings.h"
33 #include "libpspp/array.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/i18n.h"
36 #include "libpspp/message.h"
37 #include "libpspp/llx.h"
38 #include "libpspp/string-map.h"
39 #include "libpspp/string-set.h"
40 #include "libpspp/str.h"
41 #include "output/output-item.h"
44 #include "gl/xalloc.h"
45 #include "gl/xmemdup0.h"
46 #include "gl/xvasprintf.h"
49 #define _(msgid) gettext (msgid)
53 struct ll ll; /* Node for this engine. */
54 struct llx_list drivers; /* Contains "struct output_driver"s. */
55 struct output_item *deferred_text; /* Output text being accumulated. */
56 char *command_name; /* Name of command being processed. */
57 char *title, *subtitle; /* Components of page title. */
59 /* Output grouping stack. */
60 struct output_item **groups;
62 size_t allocated_groups;
64 struct string_map heading_vars;
67 static struct ll_list engine_stack = LL_INITIALIZER (engine_stack);
69 static const struct output_driver_factory *factories[];
71 static struct output_engine *
72 engine_stack_top (void)
74 struct ll *head = ll_head (&engine_stack);
75 if (ll_is_empty (&engine_stack))
77 return ll_data (head, struct output_engine, ll);
81 put_strftime (const char *key, const char *format,
82 const struct tm *tm, struct string_map *vars)
84 if (!string_map_find (vars, key))
87 strftime (value, sizeof value, format, tm);
88 string_map_insert (vars, key, value);
93 output_engine_push (void)
95 struct output_engine *e = XZALLOC (struct output_engine);
97 llx_init (&e->drivers);
99 string_map_init (&e->heading_vars);
101 time_t t = time (NULL);
102 const struct tm *tm = localtime (&t);
103 put_strftime ("Date", "%x", tm, &e->heading_vars);
104 put_strftime ("Time", "%X", tm, &e->heading_vars);
106 ll_push_head (&engine_stack, &e->ll);
110 output_engine_pop (void)
112 struct ll *head = ll_pop_head (&engine_stack);
113 struct output_engine *e = ll_data (head, struct output_engine, ll);
115 struct output_driver *d;
116 llx_for_each_preremove (d, &e->drivers, &llx_malloc_mgr)
117 output_driver_destroy (d);
118 output_item_unref (e->deferred_text);
119 free (e->command_name);
123 output_item_unref (e->groups[0]);
125 string_map_destroy (&e->heading_vars);
130 output_get_supported_formats (struct string_set *formats)
132 const struct output_driver_factory **fp;
134 for (fp = factories; *fp != NULL; fp++)
135 string_set_insert (formats, (*fp)->extension);
139 output_driver_should_show (const struct output_driver *d,
140 const struct output_item *item)
142 enum settings_output_type type = SETTINGS_OUTPUT_RESULT;
145 case OUTPUT_ITEM_MESSAGE:
146 type = (item->message->severity == MSG_S_NOTE
147 ? SETTINGS_OUTPUT_NOTE
148 : SETTINGS_OUTPUT_ERROR);
151 case OUTPUT_ITEM_TEXT:
152 if (item->text.subtype == TEXT_ITEM_SYNTAX)
153 type = SETTINGS_OUTPUT_SYNTAX;
156 case OUTPUT_ITEM_CHART:
157 case OUTPUT_ITEM_GROUP:
158 case OUTPUT_ITEM_IMAGE:
159 case OUTPUT_ITEM_PAGE_BREAK:
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_log_nocopy (s);
374 output_log_nocopy (char *s)
376 output_submit (text_item_create_nocopy (TEXT_ITEM_LOG, s, NULL));
380 output_get_title (void)
382 return engine_stack_top ()->title;
386 output_set_title (const char *title)
388 struct output_engine *e = engine_stack_top ();
390 output_set_title__ (e, &e->title, title);
394 output_get_subtitle (void)
396 return engine_stack_top ()->subtitle;
400 output_set_subtitle (const char *subtitle)
402 struct output_engine *e = engine_stack_top ();
404 output_set_title__ (e, &e->subtitle, subtitle);
408 output_set_filename (const char *filename)
410 struct output_engine *e = engine_stack_top ();
412 string_map_replace (&e->heading_vars, "Filename", filename);
416 output_driver_destroy (struct output_driver *driver)
420 char *name = driver->name;
421 if (output_driver_is_registered (driver))
422 output_driver_unregister (driver);
423 if (driver->class->destroy)
424 driver->class->destroy (driver);
430 output_driver_get_name (const struct output_driver *driver)
435 static struct output_engine *
436 output_driver_get_engine (const struct output_driver *driver)
438 struct output_engine *e;
440 ll_for_each (e, struct output_engine, ll, &engine_stack)
442 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
450 output_driver_register (struct output_driver *driver)
452 struct output_engine *e = engine_stack_top ();
454 assert (!output_driver_is_registered (driver));
455 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
459 output_driver_unregister (struct output_driver *driver)
461 struct output_engine *e = output_driver_get_engine (driver);
464 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
469 output_driver_is_registered (const struct output_driver *driver)
471 return output_driver_get_engine (driver) != NULL;
475 output_set_page_setup (const struct page_setup *ps)
477 struct output_engine *e = engine_stack_top ();
480 llx_for_each (llx, &e->drivers)
482 struct output_driver *d = llx_data (llx);
484 d->class->setup (d, ps);
488 extern const struct output_driver_factory csv_driver_factory;
489 extern const struct output_driver_factory html_driver_factory;
490 extern const struct output_driver_factory list_driver_factory;
491 extern const struct output_driver_factory odt_driver_factory;
492 extern const struct output_driver_factory pdf_driver_factory;
493 extern const struct output_driver_factory png_driver_factory;
494 extern const struct output_driver_factory ps_driver_factory;
495 extern const struct output_driver_factory spv_driver_factory;
496 extern const struct output_driver_factory svg_driver_factory;
497 extern const struct output_driver_factory tex_driver_factory;
498 extern const struct output_driver_factory txt_driver_factory;
500 static const struct output_driver_factory *factories[] =
503 &list_driver_factory,
504 &html_driver_factory,
516 static const struct output_driver_factory *
517 find_factory (const char *format)
519 const struct output_driver_factory **fp;
521 for (fp = factories; *fp != NULL; fp++)
523 const struct output_driver_factory *f = *fp;
525 if (!strcmp (f->extension, format))
528 return &txt_driver_factory;
531 static enum settings_output_devices
532 default_device_type (const char *file_name)
534 return (!strcmp (file_name, "-")
535 ? SETTINGS_DEVICE_TERMINAL
536 : SETTINGS_DEVICE_LISTING);
539 struct output_driver *
540 output_driver_create (struct string_map *options)
542 char *format = string_map_find_and_delete (options, "format");
543 char *file_name = string_map_find_and_delete (options, "output-file");
548 const char *extension = strrchr (file_name, '.');
549 format = xstrdup (extension != NULL ? extension + 1 : "");
552 format = xstrdup ("txt");
554 const struct output_driver_factory *f = find_factory (format);
556 struct driver_options o = {
557 .driver_name = f->extension,
558 .map = STRING_MAP_INITIALIZER (o.map),
559 .garbage = STRING_ARRAY_INITIALIZER,
561 string_map_swap (&o.map, options);
563 if (file_name == NULL)
564 file_name = xstrdup (f->default_file_name);
566 enum settings_output_devices default_type = default_device_type (file_name);
567 const char *default_type_string = (default_type == SETTINGS_DEVICE_TERMINAL
568 ? "terminal" : "listing");
569 enum settings_output_devices device_type = parse_enum (
570 driver_option_get (&o, "device", default_type_string),
571 "terminal", SETTINGS_DEVICE_TERMINAL,
572 "listing", SETTINGS_DEVICE_LISTING,
575 struct file_handle *fh = fh_create_file (NULL, file_name, NULL,
576 fh_default_properties ());
577 struct output_driver *driver = f->create (fh, device_type, &o);
580 const struct string_map_node *node;
583 STRING_MAP_FOR_EACH_KEY (key, node, &o.map)
584 msg (MW, _("%s: unknown option `%s'"), file_name, key);
587 string_map_destroy (&o.map);
588 string_array_destroy (&o.garbage);
597 output_driver_parse_option (const char *option, struct string_map *options)
599 const char *equals = strchr (option, '=');
602 error (0, 0, _("%s: output option missing `='"), option);
606 char *key = xmemdup0 (option, equals - option);
607 if (string_map_contains (options, key))
609 error (0, 0, _("%s: output option specified more than once"), key);
614 char *value = xmemdup0 (equals + 1, strlen (equals + 1));
615 string_map_insert_nocopy (options, key, value);
619 output_driver_substitute_heading_vars (const char *src, int page_number)
621 struct output_engine *e = engine_stack_top ();
622 struct string dst = DS_EMPTY_INITIALIZER;
623 ds_extend (&dst, strlen (src));
624 for (const char *p = src; *p;)
626 if (!strncmp (p, "&[", 6))
628 if (page_number != INT_MIN)
630 const char *start = p + 6;
631 const char *end = strchr (start, ']');
634 const char *value = string_map_find__ (&e->heading_vars,
637 ds_put_cstr (&dst, value);
638 else if (ss_equals (ss_buffer (start, end - start),
640 ds_put_format (&dst, "%d", page_number);
645 ds_put_cstr (&dst, "&");
649 ds_put_byte (&dst, *p++);
651 return ds_steal_cstr (&dst);