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/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/group-item.h"
41 #include "output/message-item.h"
42 #include "output/output-item.h"
43 #include "output/text-item.h"
46 #include "gl/xalloc.h"
47 #include "gl/xmemdup0.h"
50 #define _(msgid) gettext (msgid)
54 struct ll ll; /* Node for this engine. */
55 struct llx_list drivers; /* Contains "struct output_driver"s. */
56 struct string deferred_syntax; /* TEXT_ITEM_SYNTAX being accumulated. */
57 char *command_name; /* Name of command being processed. */
58 char *title, *subtitle; /* Components of page title. */
60 /* Output grouping stack.
62 TEXT_ITEM_GROUP_OPEN pushes a group on the stack and
63 TEXT_ITEM_GROUP_CLOSE pops one off. */
64 char **groups; /* Command names of nested sections. */
66 size_t allocated_groups;
68 struct string_map heading_vars;
71 static struct ll_list engine_stack = LL_INITIALIZER (engine_stack);
73 static const struct output_driver_factory *factories[];
75 static struct output_engine *
76 engine_stack_top (void)
78 struct ll *head = ll_head (&engine_stack);
79 if (ll_is_empty (&engine_stack))
81 return ll_data (head, struct output_engine, ll);
85 put_strftime (const char *key, const char *format,
86 const struct tm *tm, struct string_map *vars)
88 if (!string_map_find (vars, key))
91 strftime (value, sizeof value, format, tm);
92 string_map_insert (vars, key, value);
97 output_engine_push (void)
99 struct output_engine *e = xzalloc (sizeof (*e));
101 llx_init (&e->drivers);
102 ds_init_empty (&e->deferred_syntax);
104 string_map_init (&e->heading_vars);
106 time_t t = time (NULL);
107 const struct tm *tm = localtime (&t);
108 put_strftime ("Date", "%x", tm, &e->heading_vars);
109 put_strftime ("Time", "%X", tm, &e->heading_vars);
111 ll_push_head (&engine_stack, &e->ll);
115 output_engine_pop (void)
117 struct ll *head = ll_pop_head (&engine_stack);
118 struct output_engine *e =ll_data (head, struct output_engine, ll);
120 while (!llx_is_empty (&e->drivers))
122 struct output_driver *d = llx_pop_head (&e->drivers, &llx_malloc_mgr);
123 output_driver_destroy (d);
125 ds_destroy (&e->deferred_syntax);
126 free (e->command_name);
129 for (size_t i = 0; i < e->n_groups; i++)
132 string_map_destroy (&e->heading_vars);
137 output_get_supported_formats (struct string_set *formats)
139 const struct output_driver_factory **fp;
141 for (fp = factories; *fp != NULL; fp++)
142 string_set_insert (formats, (*fp)->extension);
146 output_submit__ (struct output_engine *e, struct output_item *item)
148 struct llx *llx, *next;
150 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers); llx = next)
152 struct output_driver *d = llx_data (llx);
153 enum settings_output_type type;
155 next = llx_next (llx);
157 if (is_message_item (item))
159 const struct msg *m = message_item_get_msg (to_message_item (item));
160 if (m->severity == MSG_S_NOTE)
161 type = SETTINGS_OUTPUT_NOTE;
163 type = SETTINGS_OUTPUT_ERROR;
165 else if (is_text_item (item)
166 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
167 type = SETTINGS_OUTPUT_SYNTAX;
169 type = SETTINGS_OUTPUT_RESULT;
171 if (settings_get_output_routing (type) & d->device_type)
172 d->class->submit (d, item);
175 output_item_unref (item);
179 flush_deferred_syntax (struct output_engine *e)
181 if (!ds_is_empty (&e->deferred_syntax))
183 ds_trim (&e->deferred_syntax, ss_cstr ("\n"));
184 if (!ds_is_empty (&e->deferred_syntax))
186 char *syntax = ds_steal_cstr (&e->deferred_syntax);
187 output_submit__ (e, text_item_super (text_item_create_nocopy (
188 TEXT_ITEM_SYNTAX, syntax)));
194 is_syntax_item (const struct output_item *item)
196 return (is_text_item (item)
197 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
200 /* Submits ITEM to the configured output drivers, and transfers ownership to
201 the output subsystem. */
203 output_submit (struct output_item *item)
205 struct output_engine *e = engine_stack_top ();
213 if (is_syntax_item (item))
215 ds_put_cstr (&e->deferred_syntax, text_item_get_text (to_text_item (item)));
216 output_item_unref (item);
220 flush_deferred_syntax (e);
222 if (is_group_open_item (item))
224 const struct group_open_item *group_open_item
225 = to_group_open_item (item);
226 if (e->n_groups >= e->allocated_groups)
227 e->groups = x2nrealloc (e->groups, &e->allocated_groups,
229 e->groups[e->n_groups] = (group_open_item->command_name
230 ? xstrdup (group_open_item->command_name)
234 else if (is_group_close_item (item))
236 assert (e->n_groups > 0);
238 size_t idx = --e->n_groups;
239 free (e->groups[idx]);
240 if (idx >= 1 && idx <= 4)
242 char *key = xasprintf ("Head%zu", idx);
243 string_map_find_and_delete (&e->heading_vars, key);
247 else if (is_text_item (item))
249 const struct text_item *text_item = to_text_item (item);
250 enum text_item_type type = text_item_get_type (text_item);
251 const char *text = text_item_get_text (text_item);
252 if (type == TEXT_ITEM_TITLE
253 && e->n_groups >= 1 && e->n_groups <= 4)
255 char *key = xasprintf ("Head%zu", e->n_groups);
256 string_map_replace (&e->heading_vars, key, text);
259 else if (type == TEXT_ITEM_PAGE_TITLE)
260 string_map_replace (&e->heading_vars, "PageTitle", text);
263 output_submit__ (e, item);
267 output_get_command_name (void)
269 struct output_engine *e = engine_stack_top ();
273 for (size_t i = e->n_groups; i-- > 0; )
280 /* Flushes output to screen devices, so that the user can see
281 output that doesn't fill up an entire page. */
285 struct output_engine *e = engine_stack_top ();
288 flush_deferred_syntax (e);
289 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers);
290 llx = llx_next (llx))
292 struct output_driver *d = llx_data (llx);
293 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
299 output_set_title__ (struct output_engine *e, char **dst, const char *src)
302 *dst = src ? xstrdup (src) : NULL;
305 = (e->title && e->subtitle ? xasprintf ("%s\n%s", e->title, e->subtitle)
306 : e->title ? xstrdup (e->title)
307 : e->subtitle ? xstrdup (e->subtitle)
309 text_item_submit (text_item_create_nocopy (TEXT_ITEM_PAGE_TITLE,
314 output_set_title (const char *title)
316 struct output_engine *e = engine_stack_top ();
318 output_set_title__ (e, &e->title, title);
322 output_set_subtitle (const char *subtitle)
324 struct output_engine *e = engine_stack_top ();
326 output_set_title__ (e, &e->subtitle, subtitle);
330 output_set_filename (const char *filename)
332 struct output_engine *e = engine_stack_top ();
334 string_map_replace (&e->heading_vars, "Filename", filename);
338 output_get_group_level (void)
340 struct output_engine *e = engine_stack_top ();
346 output_driver_init (struct output_driver *driver,
347 const struct output_driver_class *class,
348 const char *name, enum settings_output_devices type)
350 driver->class = class;
351 driver->name = xstrdup (name);
352 driver->device_type = type;
356 output_driver_destroy (struct output_driver *driver)
360 char *name = driver->name;
361 if (output_driver_is_registered (driver))
362 output_driver_unregister (driver);
363 if (driver->class->destroy)
364 driver->class->destroy (driver);
370 output_driver_get_name (const struct output_driver *driver)
375 static struct output_engine *
376 output_driver_get_engine (const struct output_driver *driver)
378 struct output_engine *e;
380 ll_for_each (e, struct output_engine, ll, &engine_stack)
382 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
390 output_driver_register (struct output_driver *driver)
392 struct output_engine *e = engine_stack_top ();
394 assert (!output_driver_is_registered (driver));
395 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
399 output_driver_unregister (struct output_driver *driver)
401 struct output_engine *e = output_driver_get_engine (driver);
404 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
409 output_driver_is_registered (const struct output_driver *driver)
411 return output_driver_get_engine (driver) != NULL;
414 extern const struct output_driver_factory txt_driver_factory;
415 extern const struct output_driver_factory list_driver_factory;
416 extern const struct output_driver_factory html_driver_factory;
417 extern const struct output_driver_factory csv_driver_factory;
418 extern const struct output_driver_factory odt_driver_factory;
420 extern const struct output_driver_factory pdf_driver_factory;
421 extern const struct output_driver_factory ps_driver_factory;
422 extern const struct output_driver_factory svg_driver_factory;
425 static const struct output_driver_factory *factories[] =
428 &list_driver_factory,
429 &html_driver_factory,
440 static const struct output_driver_factory *
441 find_factory (const char *format)
443 const struct output_driver_factory **fp;
445 for (fp = factories; *fp != NULL; fp++)
447 const struct output_driver_factory *f = *fp;
449 if (!strcmp (f->extension, format))
452 return &txt_driver_factory;
455 static enum settings_output_devices
456 default_device_type (const char *file_name)
458 return (!strcmp (file_name, "-")
459 ? SETTINGS_DEVICE_TERMINAL
460 : SETTINGS_DEVICE_LISTING);
463 struct output_driver *
464 output_driver_create (struct string_map *options)
466 enum settings_output_devices device_type;
467 const struct output_driver_factory *f;
468 struct output_driver *driver;
473 format = string_map_find_and_delete (options, "format");
474 file_name = string_map_find_and_delete (options, "output-file");
478 if (file_name != NULL)
480 const char *extension = strrchr (file_name, '.');
481 format = xstrdup (extension != NULL ? extension + 1 : "");
484 format = xstrdup ("txt");
486 f = find_factory (format);
488 if (file_name == NULL)
489 file_name = xstrdup (f->default_file_name);
491 /* XXX should use parse_enum(). */
492 device_string = string_map_find_and_delete (options, "device");
493 if (device_string == NULL || device_string[0] == '\0')
494 device_type = default_device_type (file_name);
495 else if (!strcmp (device_string, "terminal"))
496 device_type = SETTINGS_DEVICE_TERMINAL;
497 else if (!strcmp (device_string, "listing"))
498 device_type = SETTINGS_DEVICE_LISTING;
501 msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
502 device_string, "terminal", "listing");
503 device_type = default_device_type (file_name);
506 struct file_handle *fh = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
508 driver = f->create (fh, device_type, options);
511 const struct string_map_node *node;
514 STRING_MAP_FOR_EACH_KEY (key, node, options)
515 msg (MW, _("%s: unknown option `%s'"), file_name, key);
517 string_map_clear (options);
521 free (device_string);
527 output_driver_parse_option (const char *option, struct string_map *options)
529 const char *equals = strchr (option, '=');
532 error (0, 0, _("%s: output option missing `='"), option);
536 char *key = xmemdup0 (option, equals - option);
537 if (string_map_contains (options, key))
539 error (0, 0, _("%s: output option specified more than once"), key);
544 char *value = xmemdup0 (equals + 1, strlen (equals + 1));
545 string_map_insert_nocopy (options, key, value);
548 /* Extracts the actual text content from the given Pango MARKUP and returns it
549 as as a malloc()'d string. */
551 output_get_text_from_markup (const char *markup)
553 xmlParserCtxt *parser = xmlCreatePushParserCtxt (NULL, NULL, NULL, 0, NULL);
555 return xstrdup (markup);
557 xmlParseChunk (parser, "<xml>", strlen ("<xml>"), false);
558 xmlParseChunk (parser, markup, strlen (markup), false);
559 xmlParseChunk (parser, "</xml>", strlen ("</xml>"), true);
562 = (parser->wellFormed
564 xmlNodeGetContent (xmlDocGetRootElement (parser->myDoc)))
566 xmlFreeDoc (parser->myDoc);
567 xmlFreeParserCtxt (parser);
573 output_driver_substitute_heading_vars (const char *src, int page_number)
575 struct output_engine *e = engine_stack_top ();
576 struct string dst = DS_EMPTY_INITIALIZER;
577 ds_extend (&dst, strlen (src));
578 for (const char *p = src; *p; )
580 if (!strncmp (p, "&[", 6))
582 if (page_number != INT_MIN)
584 const char *start = p + 6;
585 const char *end = strchr (start, ']');
588 const char *value = string_map_find__ (&e->heading_vars,
591 ds_put_cstr (&dst, value);
592 else if (ss_equals (ss_buffer (start, end - start),
594 ds_put_format (&dst, "%d", page_number);
599 ds_put_cstr (&dst, "&");
603 ds_put_byte (&dst, *p++);
605 return ds_steal_cstr (&dst);