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"
29 #include "data/file-handle-def.h"
30 #include "data/settings.h"
31 #include "libpspp/array.h"
32 #include "libpspp/assertion.h"
33 #include "libpspp/i18n.h"
34 #include "libpspp/message.h"
35 #include "libpspp/llx.h"
36 #include "libpspp/string-map.h"
37 #include "libpspp/string-set.h"
38 #include "libpspp/str.h"
39 #include "output/group-item.h"
40 #include "output/message-item.h"
41 #include "output/output-item.h"
42 #include "output/text-item.h"
45 #include "gl/xalloc.h"
46 #include "gl/xmemdup0.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 text_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.
61 TEXT_ITEM_GROUP_OPEN pushes a group on the stack and
62 TEXT_ITEM_GROUP_CLOSE pops one off. */
63 char **groups; /* Command names of nested sections. */
65 size_t allocated_groups;
67 struct string_map heading_vars;
70 static struct ll_list engine_stack = LL_INITIALIZER (engine_stack);
72 static const struct output_driver_factory *factories[];
74 static struct output_engine *
75 engine_stack_top (void)
77 struct ll *head = ll_head (&engine_stack);
78 if (ll_is_empty (&engine_stack))
80 return ll_data (head, struct output_engine, ll);
84 put_strftime (const char *key, const char *format,
85 const struct tm *tm, struct string_map *vars)
87 if (!string_map_find (vars, key))
90 strftime (value, sizeof value, format, tm);
91 string_map_insert (vars, key, value);
96 output_engine_push (void)
98 struct output_engine *e = xzalloc (sizeof (*e));
100 llx_init (&e->drivers);
102 string_map_init (&e->heading_vars);
104 time_t t = time (NULL);
105 const struct tm *tm = localtime (&t);
106 put_strftime ("Date", "%x", tm, &e->heading_vars);
107 put_strftime ("Time", "%X", tm, &e->heading_vars);
109 ll_push_head (&engine_stack, &e->ll);
113 output_engine_pop (void)
115 struct ll *head = ll_pop_head (&engine_stack);
116 struct output_engine *e =ll_data (head, struct output_engine, ll);
118 while (!llx_is_empty (&e->drivers))
120 struct output_driver *d = llx_pop_head (&e->drivers, &llx_malloc_mgr);
121 output_driver_destroy (d);
123 text_item_unref (e->deferred_text);
124 free (e->command_name);
127 for (size_t i = 0; i < e->n_groups; i++)
130 string_map_destroy (&e->heading_vars);
135 output_get_supported_formats (struct string_set *formats)
137 const struct output_driver_factory **fp;
139 for (fp = factories; *fp != NULL; fp++)
140 string_set_insert (formats, (*fp)->extension);
144 output_submit__ (struct output_engine *e, struct output_item *item)
146 struct llx *llx, *next;
148 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers); llx = next)
150 struct output_driver *d = llx_data (llx);
151 enum settings_output_type type;
153 next = llx_next (llx);
155 if (is_message_item (item))
157 const struct msg *m = message_item_get_msg (to_message_item (item));
158 if (m->severity == MSG_S_NOTE)
159 type = SETTINGS_OUTPUT_NOTE;
161 type = SETTINGS_OUTPUT_ERROR;
163 else if (is_text_item (item)
164 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
165 type = SETTINGS_OUTPUT_SYNTAX;
167 type = SETTINGS_OUTPUT_RESULT;
169 if (settings_get_output_routing (type) & d->device_type)
170 d->class->submit (d, item);
173 output_item_unref (item);
177 flush_deferred_text (struct output_engine *e)
179 struct text_item *deferred_text = e->deferred_text;
182 e->deferred_text = NULL;
183 output_submit__ (e, text_item_super (deferred_text));
188 defer_text (struct output_engine *e, struct output_item *output_item)
190 if (!is_text_item (output_item))
193 struct text_item *text = to_text_item (output_item);
194 if (!e->deferred_text)
195 e->deferred_text = text_item_unshare (text);
196 else if (text_item_append (e->deferred_text, text))
197 text_item_unref (text);
200 flush_deferred_text (e);
201 e->deferred_text = text_item_unshare (text);
206 /* Submits ITEM to the configured output drivers, and transfers ownership to
207 the output subsystem. */
209 output_submit (struct output_item *item)
211 struct output_engine *e = engine_stack_top ();
219 if (defer_text (e, item))
221 flush_deferred_text (e);
223 if (is_group_open_item (item))
225 const struct group_open_item *group_open_item
226 = to_group_open_item (item);
227 if (e->n_groups >= e->allocated_groups)
228 e->groups = x2nrealloc (e->groups, &e->allocated_groups,
230 e->groups[e->n_groups] = (group_open_item->command_name
231 ? xstrdup (group_open_item->command_name)
235 else if (is_group_close_item (item))
237 assert (e->n_groups > 0);
239 size_t idx = --e->n_groups;
240 free (e->groups[idx]);
242 char *key = xasprintf ("Head%zu", idx);
243 free (string_map_find_and_delete (&e->heading_vars, key));
246 else if (is_text_item (item))
248 const struct text_item *text_item = to_text_item (item);
249 enum text_item_type type = text_item_get_type (text_item);
250 char *key = (type == TEXT_ITEM_TITLE ? xasprintf ("Head%zu", e->n_groups)
251 : type == TEXT_ITEM_PAGE_TITLE ? xstrdup ("PageTitle")
254 string_map_replace_nocopy (&e->heading_vars, key,
255 text_item_get_plain_text (text_item));
258 output_submit__ (e, item);
261 /* Returns the name of the command currently being parsed, in all uppercase.
262 The caller must free the returned value.
264 Returns NULL if no command is being parsed. */
266 output_get_command_name (void)
268 struct output_engine *e = engine_stack_top ();
272 for (size_t i = e->n_groups; i-- > 0;)
274 return utf8_to_upper (e->groups[i]);
279 /* Flushes output to screen devices, so that the user can see
280 output that doesn't fill up an entire page. */
284 struct output_engine *e = engine_stack_top ();
287 flush_deferred_text (e);
288 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers);
289 llx = llx_next (llx))
291 struct output_driver *d = llx_data (llx);
292 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
298 output_set_title__ (struct output_engine *e, char **dst, const char *src)
301 *dst = src ? xstrdup (src) : NULL;
304 = (e->title && e->subtitle ? xasprintf ("%s\n%s", e->title, e->subtitle)
305 : e->title ? xstrdup (e->title)
306 : e->subtitle ? xstrdup (e->subtitle)
308 text_item_submit (text_item_create_nocopy (TEXT_ITEM_PAGE_TITLE,
313 output_set_title (const char *title)
315 struct output_engine *e = engine_stack_top ();
317 output_set_title__ (e, &e->title, title);
321 output_set_subtitle (const char *subtitle)
323 struct output_engine *e = engine_stack_top ();
325 output_set_title__ (e, &e->subtitle, subtitle);
329 output_set_filename (const char *filename)
331 struct output_engine *e = engine_stack_top ();
333 string_map_replace (&e->heading_vars, "Filename", filename);
337 output_get_group_level (void)
339 struct output_engine *e = engine_stack_top ();
345 output_driver_init (struct output_driver *driver,
346 const struct output_driver_class *class,
347 const char *name, enum settings_output_devices type)
349 driver->class = class;
350 driver->name = xstrdup (name);
351 driver->device_type = type;
355 output_driver_destroy (struct output_driver *driver)
359 char *name = driver->name;
360 if (output_driver_is_registered (driver))
361 output_driver_unregister (driver);
362 if (driver->class->destroy)
363 driver->class->destroy (driver);
369 output_driver_get_name (const struct output_driver *driver)
374 static struct output_engine *
375 output_driver_get_engine (const struct output_driver *driver)
377 struct output_engine *e;
379 ll_for_each (e, struct output_engine, ll, &engine_stack)
381 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
389 output_driver_register (struct output_driver *driver)
391 struct output_engine *e = engine_stack_top ();
393 assert (!output_driver_is_registered (driver));
394 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
398 output_driver_unregister (struct output_driver *driver)
400 struct output_engine *e = output_driver_get_engine (driver);
403 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
408 output_driver_is_registered (const struct output_driver *driver)
410 return output_driver_get_engine (driver) != NULL;
413 extern const struct output_driver_factory csv_driver_factory;
414 extern const struct output_driver_factory html_driver_factory;
415 extern const struct output_driver_factory list_driver_factory;
416 extern const struct output_driver_factory odt_driver_factory;
417 extern const struct output_driver_factory pdf_driver_factory;
418 extern const struct output_driver_factory png_driver_factory;
419 extern const struct output_driver_factory ps_driver_factory;
420 extern const struct output_driver_factory spv_driver_factory;
421 extern const struct output_driver_factory svg_driver_factory;
422 extern const struct output_driver_factory tex_driver_factory;
423 extern const struct output_driver_factory txt_driver_factory;
425 static const struct output_driver_factory *factories[] =
428 &list_driver_factory,
429 &html_driver_factory,
441 static const struct output_driver_factory *
442 find_factory (const char *format)
444 const struct output_driver_factory **fp;
446 for (fp = factories; *fp != NULL; fp++)
448 const struct output_driver_factory *f = *fp;
450 if (!strcmp (f->extension, format))
453 return &txt_driver_factory;
456 static enum settings_output_devices
457 default_device_type (const char *file_name)
459 return (!strcmp (file_name, "-")
460 ? SETTINGS_DEVICE_TERMINAL
461 : SETTINGS_DEVICE_LISTING);
464 struct output_driver *
465 output_driver_create (struct string_map *options)
467 enum settings_output_devices device_type;
468 const struct output_driver_factory *f;
469 struct output_driver *driver;
474 format = string_map_find_and_delete (options, "format");
475 file_name = string_map_find_and_delete (options, "output-file");
479 if (file_name != NULL)
481 const char *extension = strrchr (file_name, '.');
482 format = xstrdup (extension != NULL ? extension + 1 : "");
485 format = xstrdup ("txt");
487 f = find_factory (format);
489 if (file_name == NULL)
490 file_name = xstrdup (f->default_file_name);
492 /* XXX should use parse_enum(). */
493 device_string = string_map_find_and_delete (options, "device");
494 if (device_string == NULL || device_string[0] == '\0')
495 device_type = default_device_type (file_name);
496 else if (!strcmp (device_string, "terminal"))
497 device_type = SETTINGS_DEVICE_TERMINAL;
498 else if (!strcmp (device_string, "listing"))
499 device_type = SETTINGS_DEVICE_LISTING;
502 msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
503 device_string, "terminal", "listing");
504 device_type = default_device_type (file_name);
507 struct file_handle *fh = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
509 driver = f->create (fh, device_type, options);
512 const struct string_map_node *node;
515 STRING_MAP_FOR_EACH_KEY (key, node, options)
516 msg (MW, _("%s: unknown option `%s'"), file_name, key);
518 string_map_clear (options);
522 free (device_string);
528 output_driver_parse_option (const char *option, struct string_map *options)
530 const char *equals = strchr (option, '=');
533 error (0, 0, _("%s: output option missing `='"), option);
537 char *key = xmemdup0 (option, equals - option);
538 if (string_map_contains (options, key))
540 error (0, 0, _("%s: output option specified more than once"), key);
545 char *value = xmemdup0 (equals + 1, strlen (equals + 1));
546 string_map_insert_nocopy (options, key, value);
550 output_driver_substitute_heading_vars (const char *src, int page_number)
552 struct output_engine *e = engine_stack_top ();
553 struct string dst = DS_EMPTY_INITIALIZER;
554 ds_extend (&dst, strlen (src));
555 for (const char *p = src; *p;)
557 if (!strncmp (p, "&[", 6))
559 if (page_number != INT_MIN)
561 const char *start = p + 6;
562 const char *end = strchr (start, ']');
565 const char *value = string_map_find__ (&e->heading_vars,
568 ds_put_cstr (&dst, value);
569 else if (ss_equals (ss_buffer (start, end - start),
571 ds_put_format (&dst, "%d", page_number);
576 ds_put_cstr (&dst, "&");
580 ds_put_byte (&dst, *p++);
582 return ds_steal_cstr (&dst);