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"
24 #include <libxml/parser.h>
25 #include <libxml/tree.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/group-item.h"
42 #include "output/message-item.h"
43 #include "output/output-item.h"
44 #include "output/text-item.h"
47 #include "gl/xalloc.h"
48 #include "gl/xmemdup0.h"
51 #define _(msgid) gettext (msgid)
55 struct ll ll; /* Node for this engine. */
56 struct llx_list drivers; /* Contains "struct output_driver"s. */
57 struct string deferred_text; /* Output text being accumulated. */
58 enum text_item_type deferred_type; /* Type of text being accumulated. */
59 char *command_name; /* Name of command being processed. */
60 char *title, *subtitle; /* Components of page title. */
62 /* Output grouping stack.
64 TEXT_ITEM_GROUP_OPEN pushes a group on the stack and
65 TEXT_ITEM_GROUP_CLOSE pops one off. */
66 char **groups; /* Command names of nested sections. */
68 size_t allocated_groups;
70 struct string_map heading_vars;
73 static struct ll_list engine_stack = LL_INITIALIZER (engine_stack);
75 static const struct output_driver_factory *factories[];
77 static struct output_engine *
78 engine_stack_top (void)
80 struct ll *head = ll_head (&engine_stack);
81 if (ll_is_empty (&engine_stack))
83 return ll_data (head, struct output_engine, ll);
87 put_strftime (const char *key, const char *format,
88 const struct tm *tm, struct string_map *vars)
90 if (!string_map_find (vars, key))
93 strftime (value, sizeof value, format, tm);
94 string_map_insert (vars, key, value);
99 output_engine_push (void)
101 struct output_engine *e = xzalloc (sizeof (*e));
103 llx_init (&e->drivers);
104 ds_init_empty (&e->deferred_text);
106 string_map_init (&e->heading_vars);
108 time_t t = time (NULL);
109 const struct tm *tm = localtime (&t);
110 put_strftime ("Date", "%x", tm, &e->heading_vars);
111 put_strftime ("Time", "%X", tm, &e->heading_vars);
113 ll_push_head (&engine_stack, &e->ll);
117 output_engine_pop (void)
119 struct ll *head = ll_pop_head (&engine_stack);
120 struct output_engine *e =ll_data (head, struct output_engine, ll);
122 while (!llx_is_empty (&e->drivers))
124 struct output_driver *d = llx_pop_head (&e->drivers, &llx_malloc_mgr);
125 output_driver_destroy (d);
127 ds_destroy (&e->deferred_text);
128 free (e->command_name);
131 for (size_t i = 0; i < e->n_groups; i++)
134 string_map_destroy (&e->heading_vars);
139 output_get_supported_formats (struct string_set *formats)
141 const struct output_driver_factory **fp;
143 for (fp = factories; *fp != NULL; fp++)
144 string_set_insert (formats, (*fp)->extension);
148 output_submit__ (struct output_engine *e, struct output_item *item)
150 struct llx *llx, *next;
152 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers); llx = next)
154 struct output_driver *d = llx_data (llx);
155 enum settings_output_type type;
157 next = llx_next (llx);
159 if (is_message_item (item))
161 const struct msg *m = message_item_get_msg (to_message_item (item));
162 if (m->severity == MSG_S_NOTE)
163 type = SETTINGS_OUTPUT_NOTE;
165 type = SETTINGS_OUTPUT_ERROR;
167 else if (is_text_item (item)
168 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
169 type = SETTINGS_OUTPUT_SYNTAX;
171 type = SETTINGS_OUTPUT_RESULT;
173 if (settings_get_output_routing (type) & d->device_type)
174 d->class->submit (d, item);
177 output_item_unref (item);
181 flush_deferred_text (struct output_engine *e)
183 if (!ds_is_empty (&e->deferred_text))
185 char *text = ds_steal_cstr (&e->deferred_text);
186 output_submit__ (e, text_item_super (text_item_create_nocopy (
187 e->deferred_type, text)));
192 defer_text (struct output_engine *e, struct output_item *item)
194 if (!is_text_item (item))
197 struct text_item *text_item = to_text_item (item);
198 if (text_item->markup) /* XXX */
201 enum text_item_type type = text_item_get_type (text_item);
202 if (type != TEXT_ITEM_SYNTAX && type != TEXT_ITEM_LOG)
205 if (!ds_is_empty (&e->deferred_text) && e->deferred_type != type)
206 flush_deferred_text (e);
208 e->deferred_type = type;
210 if (!ds_is_empty (&e->deferred_text))
211 ds_put_byte (&e->deferred_text, '\n');
213 const char *text = text_item_get_text (text_item);
214 ds_put_cstr (&e->deferred_text, text);
215 output_item_unref (item);
220 /* Submits ITEM to the configured output drivers, and transfers ownership to
221 the output subsystem. */
223 output_submit (struct output_item *item)
225 struct output_engine *e = engine_stack_top ();
233 if (defer_text (e, item))
235 flush_deferred_text (e);
237 if (is_group_open_item (item))
239 const struct group_open_item *group_open_item
240 = to_group_open_item (item);
241 if (e->n_groups >= e->allocated_groups)
242 e->groups = x2nrealloc (e->groups, &e->allocated_groups,
244 e->groups[e->n_groups] = (group_open_item->command_name
245 ? xstrdup (group_open_item->command_name)
249 else if (is_group_close_item (item))
251 assert (e->n_groups > 0);
253 size_t idx = --e->n_groups;
254 free (e->groups[idx]);
255 if (idx >= 1 && idx <= 4)
257 char *key = xasprintf ("Head%zu", idx);
258 free (string_map_find_and_delete (&e->heading_vars, key));
262 else if (is_text_item (item))
264 const struct text_item *text_item = to_text_item (item);
265 enum text_item_type type = text_item_get_type (text_item);
266 const char *text = text_item_get_text (text_item);
267 if (type == TEXT_ITEM_TITLE
268 && e->n_groups >= 1 && e->n_groups <= 4)
270 char *key = xasprintf ("Head%zu", e->n_groups);
271 string_map_replace (&e->heading_vars, key, text);
274 else if (type == TEXT_ITEM_PAGE_TITLE)
275 string_map_replace (&e->heading_vars, "PageTitle", text);
278 output_submit__ (e, item);
281 /* Returns the name of the command currently being parsed, in all uppercase.
282 The caller must free the returned value.
284 Returns NULL if no command is being parsed. */
286 output_get_command_name (void)
288 struct output_engine *e = engine_stack_top ();
292 for (size_t i = e->n_groups; i-- > 0;)
294 return utf8_to_upper (e->groups[i]);
299 /* Flushes output to screen devices, so that the user can see
300 output that doesn't fill up an entire page. */
304 struct output_engine *e = engine_stack_top ();
307 flush_deferred_text (e);
308 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers);
309 llx = llx_next (llx))
311 struct output_driver *d = llx_data (llx);
312 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
318 output_set_title__ (struct output_engine *e, char **dst, const char *src)
321 *dst = src ? xstrdup (src) : NULL;
324 = (e->title && e->subtitle ? xasprintf ("%s\n%s", e->title, e->subtitle)
325 : e->title ? xstrdup (e->title)
326 : e->subtitle ? xstrdup (e->subtitle)
328 text_item_submit (text_item_create_nocopy (TEXT_ITEM_PAGE_TITLE,
333 output_set_title (const char *title)
335 struct output_engine *e = engine_stack_top ();
337 output_set_title__ (e, &e->title, title);
341 output_set_subtitle (const char *subtitle)
343 struct output_engine *e = engine_stack_top ();
345 output_set_title__ (e, &e->subtitle, subtitle);
349 output_set_filename (const char *filename)
351 struct output_engine *e = engine_stack_top ();
353 string_map_replace (&e->heading_vars, "Filename", filename);
357 output_get_group_level (void)
359 struct output_engine *e = engine_stack_top ();
365 output_driver_init (struct output_driver *driver,
366 const struct output_driver_class *class,
367 const char *name, enum settings_output_devices type)
369 driver->class = class;
370 driver->name = xstrdup (name);
371 driver->device_type = type;
375 output_driver_destroy (struct output_driver *driver)
379 char *name = driver->name;
380 if (output_driver_is_registered (driver))
381 output_driver_unregister (driver);
382 if (driver->class->destroy)
383 driver->class->destroy (driver);
389 output_driver_get_name (const struct output_driver *driver)
394 static struct output_engine *
395 output_driver_get_engine (const struct output_driver *driver)
397 struct output_engine *e;
399 ll_for_each (e, struct output_engine, ll, &engine_stack)
401 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
409 output_driver_register (struct output_driver *driver)
411 struct output_engine *e = engine_stack_top ();
413 assert (!output_driver_is_registered (driver));
414 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
418 output_driver_unregister (struct output_driver *driver)
420 struct output_engine *e = output_driver_get_engine (driver);
423 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
428 output_driver_is_registered (const struct output_driver *driver)
430 return output_driver_get_engine (driver) != NULL;
433 extern const struct output_driver_factory txt_driver_factory;
434 extern const struct output_driver_factory list_driver_factory;
435 extern const struct output_driver_factory html_driver_factory;
436 extern const struct output_driver_factory csv_driver_factory;
437 extern const struct output_driver_factory odt_driver_factory;
438 extern const struct output_driver_factory spv_driver_factory;
440 extern const struct output_driver_factory pdf_driver_factory;
441 extern const struct output_driver_factory ps_driver_factory;
442 extern const struct output_driver_factory svg_driver_factory;
444 extern const struct output_driver_factory tex_driver_factory;
446 static const struct output_driver_factory *factories[] =
449 &list_driver_factory,
450 &html_driver_factory,
463 static const struct output_driver_factory *
464 find_factory (const char *format)
466 const struct output_driver_factory **fp;
468 for (fp = factories; *fp != NULL; fp++)
470 const struct output_driver_factory *f = *fp;
472 if (!strcmp (f->extension, format))
475 return &txt_driver_factory;
478 static enum settings_output_devices
479 default_device_type (const char *file_name)
481 return (!strcmp (file_name, "-")
482 ? SETTINGS_DEVICE_TERMINAL
483 : SETTINGS_DEVICE_LISTING);
486 struct output_driver *
487 output_driver_create (struct string_map *options)
489 enum settings_output_devices device_type;
490 const struct output_driver_factory *f;
491 struct output_driver *driver;
496 format = string_map_find_and_delete (options, "format");
497 file_name = string_map_find_and_delete (options, "output-file");
501 if (file_name != NULL)
503 const char *extension = strrchr (file_name, '.');
504 format = xstrdup (extension != NULL ? extension + 1 : "");
507 format = xstrdup ("txt");
509 f = find_factory (format);
511 if (file_name == NULL)
512 file_name = xstrdup (f->default_file_name);
514 /* XXX should use parse_enum(). */
515 device_string = string_map_find_and_delete (options, "device");
516 if (device_string == NULL || device_string[0] == '\0')
517 device_type = default_device_type (file_name);
518 else if (!strcmp (device_string, "terminal"))
519 device_type = SETTINGS_DEVICE_TERMINAL;
520 else if (!strcmp (device_string, "listing"))
521 device_type = SETTINGS_DEVICE_LISTING;
524 msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
525 device_string, "terminal", "listing");
526 device_type = default_device_type (file_name);
529 struct file_handle *fh = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
531 driver = f->create (fh, device_type, options);
534 const struct string_map_node *node;
537 STRING_MAP_FOR_EACH_KEY (key, node, options)
538 msg (MW, _("%s: unknown option `%s'"), file_name, key);
540 string_map_clear (options);
544 free (device_string);
550 output_driver_parse_option (const char *option, struct string_map *options)
552 const char *equals = strchr (option, '=');
555 error (0, 0, _("%s: output option missing `='"), option);
559 char *key = xmemdup0 (option, equals - option);
560 if (string_map_contains (options, key))
562 error (0, 0, _("%s: output option specified more than once"), key);
567 char *value = xmemdup0 (equals + 1, strlen (equals + 1));
568 string_map_insert_nocopy (options, key, value);
571 /* Extracts the actual text content from the given Pango MARKUP and returns it
572 as as a malloc()'d string. */
574 output_get_text_from_markup (const char *markup)
576 xmlParserCtxt *parser = xmlCreatePushParserCtxt (NULL, NULL, NULL, 0, NULL);
578 return xstrdup (markup);
580 xmlParseChunk (parser, "<xml>", strlen ("<xml>"), false);
581 xmlParseChunk (parser, markup, strlen (markup), false);
582 xmlParseChunk (parser, "</xml>", strlen ("</xml>"), true);
585 = (parser->wellFormed
587 xmlNodeGetContent (xmlDocGetRootElement (parser->myDoc)))
589 xmlFreeDoc (parser->myDoc);
590 xmlFreeParserCtxt (parser);
596 output_driver_substitute_heading_vars (const char *src, int page_number)
598 struct output_engine *e = engine_stack_top ();
599 struct string dst = DS_EMPTY_INITIALIZER;
600 ds_extend (&dst, strlen (src));
601 for (const char *p = src; *p;)
603 if (!strncmp (p, "&[", 6))
605 if (page_number != INT_MIN)
607 const char *start = p + 6;
608 const char *end = strchr (start, ']');
611 const char *value = string_map_find__ (&e->heading_vars,
614 ds_put_cstr (&dst, value);
615 else if (ss_equals (ss_buffer (start, end - start),
617 ds_put_format (&dst, "%d", page_number);
622 ds_put_cstr (&dst, "&");
626 ds_put_byte (&dst, *p++);
628 return ds_steal_cstr (&dst);