1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012, 2014 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>
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/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 llx_list drivers; /* Contains "struct output_driver"s. */
54 struct string deferred_syntax; /* TEXT_ITEM_SYNTAX being accumulated. */
55 char *command_name; /* Name of command being processed. */
56 char *title, *subtitle; /* Components of page title. */
58 /* Output grouping stack.
60 TEXT_ITEM_GROUP_OPEN pushes a group on the stack and
61 TEXT_ITEM_GROUP_CLOSE pops one off. */
62 char **groups; /* Command names of nested sections. */
64 size_t allocated_groups;
67 static const struct output_driver_factory *factories[];
69 /* A stack of output engines.. */
70 static struct output_engine *engine_stack;
71 static size_t n_stack, allocated_stack;
73 static struct output_engine *
74 engine_stack_top (void)
77 return &engine_stack[n_stack - 1];
81 output_engine_push (void)
83 struct output_engine *e;
85 if (n_stack >= allocated_stack)
86 engine_stack = x2nrealloc (engine_stack, &allocated_stack,
87 sizeof *engine_stack);
89 e = &engine_stack[n_stack++];
90 memset (e, 0, sizeof *e);
91 llx_init (&e->drivers);
92 ds_init_empty (&e->deferred_syntax);
98 output_engine_pop (void)
100 struct output_engine *e;
102 assert (n_stack > 0);
103 e = &engine_stack[--n_stack];
104 while (!llx_is_empty (&e->drivers))
106 struct output_driver *d = llx_pop_head (&e->drivers, &llx_malloc_mgr);
107 output_driver_destroy (d);
109 ds_destroy (&e->deferred_syntax);
110 free (e->command_name);
113 for (size_t i = 0; i < e->n_groups; i++)
119 output_get_supported_formats (struct string_set *formats)
121 const struct output_driver_factory **fp;
123 for (fp = factories; *fp != NULL; fp++)
124 string_set_insert (formats, (*fp)->extension);
128 output_submit__ (struct output_engine *e, struct output_item *item)
130 struct llx *llx, *next;
132 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers); llx = next)
134 struct output_driver *d = llx_data (llx);
135 enum settings_output_type type;
137 next = llx_next (llx);
139 if (is_message_item (item))
141 const struct msg *m = message_item_get_msg (to_message_item (item));
142 if (m->severity == MSG_S_NOTE)
143 type = SETTINGS_OUTPUT_NOTE;
145 type = SETTINGS_OUTPUT_ERROR;
147 else if (is_text_item (item)
148 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
149 type = SETTINGS_OUTPUT_SYNTAX;
151 type = SETTINGS_OUTPUT_RESULT;
153 if (settings_get_output_routing (type) & d->device_type)
154 d->class->submit (d, item);
157 output_item_unref (item);
161 flush_deferred_syntax (struct output_engine *e)
163 if (!ds_is_empty (&e->deferred_syntax))
165 ds_trim (&e->deferred_syntax, ss_cstr ("\n"));
166 if (!ds_is_empty (&e->deferred_syntax))
168 char *syntax = ds_steal_cstr (&e->deferred_syntax);
169 output_submit__ (e, text_item_super (text_item_create_nocopy (
170 TEXT_ITEM_SYNTAX, syntax)));
176 is_syntax_item (const struct output_item *item)
178 return (is_text_item (item)
179 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
182 /* Submits ITEM to the configured output drivers, and transfers ownership to
183 the output subsystem. */
185 output_submit (struct output_item *item)
187 struct output_engine *e = engine_stack_top ();
192 if (is_syntax_item (item))
194 ds_put_cstr (&e->deferred_syntax, text_item_get_text (to_text_item (item)));
195 output_item_unref (item);
199 flush_deferred_syntax (e);
201 if (is_group_open_item (item))
203 const struct group_open_item *group_open_item
204 = to_group_open_item (item);
205 if (e->n_groups >= e->allocated_groups)
206 e->groups = x2nrealloc (e->groups, &e->allocated_groups,
208 e->groups[e->n_groups] = (group_open_item->command_name
209 ? xstrdup (group_open_item->command_name)
213 else if (is_group_close_item (item))
215 assert (e->n_groups > 0);
217 size_t idx = --e->n_groups;
218 free (e->groups[idx]);
220 output_submit__ (e, item);
224 output_get_command_name (void)
228 struct output_engine *e = engine_stack_top ();
229 for (size_t i = e->n_groups; i-- > 0; )
237 /* Flushes output to screen devices, so that the user can see
238 output that doesn't fill up an entire page. */
242 struct output_engine *e = engine_stack_top ();
245 flush_deferred_syntax (e);
246 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers);
247 llx = llx_next (llx))
249 struct output_driver *d = llx_data (llx);
250 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
256 output_set_title__ (struct output_engine *e, char **dst, const char *src)
259 *dst = src ? xstrdup (src) : NULL;
262 = (e->title && e->subtitle ? xasprintf ("%s\n%s", e->title, e->subtitle)
263 : e->title ? xstrdup (e->title)
264 : e->subtitle ? xstrdup (e->subtitle)
266 text_item_submit (text_item_create_nocopy (TEXT_ITEM_PAGE_TITLE,
271 output_set_title (const char *title)
273 struct output_engine *e = engine_stack_top ();
275 output_set_title__ (e, &e->title, title);
279 output_set_subtitle (const char *subtitle)
281 struct output_engine *e = engine_stack_top ();
283 output_set_title__ (e, &e->subtitle, subtitle);
287 output_get_group_level (void)
289 struct output_engine *e = engine_stack_top ();
295 output_driver_init (struct output_driver *driver,
296 const struct output_driver_class *class,
297 const char *name, enum settings_output_devices type)
299 driver->class = class;
300 driver->name = xstrdup (name);
301 driver->device_type = type;
305 output_driver_destroy (struct output_driver *driver)
309 char *name = driver->name;
310 if (output_driver_is_registered (driver))
311 output_driver_unregister (driver);
312 if (driver->class->destroy)
313 driver->class->destroy (driver);
319 output_driver_get_name (const struct output_driver *driver)
324 static struct output_engine *
325 output_driver_get_engine (const struct output_driver *driver)
327 struct output_engine *e;
329 for (e = engine_stack; e < &engine_stack[n_stack]; e++)
330 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
337 output_driver_register (struct output_driver *driver)
339 struct output_engine *e = engine_stack_top ();
341 assert (!output_driver_is_registered (driver));
342 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
346 output_driver_unregister (struct output_driver *driver)
348 struct output_engine *e = output_driver_get_engine (driver);
351 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
356 output_driver_is_registered (const struct output_driver *driver)
358 return output_driver_get_engine (driver) != NULL;
361 extern const struct output_driver_factory txt_driver_factory;
362 extern const struct output_driver_factory list_driver_factory;
363 extern const struct output_driver_factory html_driver_factory;
364 extern const struct output_driver_factory csv_driver_factory;
365 extern const struct output_driver_factory odt_driver_factory;
367 extern const struct output_driver_factory pdf_driver_factory;
368 extern const struct output_driver_factory ps_driver_factory;
369 extern const struct output_driver_factory svg_driver_factory;
372 static const struct output_driver_factory *factories[] =
375 &list_driver_factory,
376 &html_driver_factory,
387 static const struct output_driver_factory *
388 find_factory (const char *format)
390 const struct output_driver_factory **fp;
392 for (fp = factories; *fp != NULL; fp++)
394 const struct output_driver_factory *f = *fp;
396 if (!strcmp (f->extension, format))
399 return &txt_driver_factory;
402 static enum settings_output_devices
403 default_device_type (const char *file_name)
405 return (!strcmp (file_name, "-")
406 ? SETTINGS_DEVICE_TERMINAL
407 : SETTINGS_DEVICE_LISTING);
410 struct output_driver *
411 output_driver_create (struct string_map *options)
413 enum settings_output_devices device_type;
414 const struct output_driver_factory *f;
415 struct output_driver *driver;
420 format = string_map_find_and_delete (options, "format");
421 file_name = string_map_find_and_delete (options, "output-file");
425 if (file_name != NULL)
427 const char *extension = strrchr (file_name, '.');
428 format = xstrdup (extension != NULL ? extension + 1 : "");
431 format = xstrdup ("txt");
433 f = find_factory (format);
435 if (file_name == NULL)
436 file_name = xstrdup (f->default_file_name);
438 /* XXX should use parse_enum(). */
439 device_string = string_map_find_and_delete (options, "device");
440 if (device_string == NULL || device_string[0] == '\0')
441 device_type = default_device_type (file_name);
442 else if (!strcmp (device_string, "terminal"))
443 device_type = SETTINGS_DEVICE_TERMINAL;
444 else if (!strcmp (device_string, "listing"))
445 device_type = SETTINGS_DEVICE_LISTING;
448 msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
449 device_string, "terminal", "listing");
450 device_type = default_device_type (file_name);
453 struct file_handle *fh = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
455 driver = f->create (fh, device_type, options);
458 const struct string_map_node *node;
461 STRING_MAP_FOR_EACH_KEY (key, node, options)
462 msg (MW, _("%s: unknown option `%s'"), file_name, key);
464 string_map_clear (options);
468 free (device_string);
474 output_driver_parse_option (const char *option, struct string_map *options)
476 const char *equals = strchr (option, '=');
479 error (0, 0, _("%s: output option missing `='"), option);
483 char *key = xmemdup0 (option, equals - option);
484 if (string_map_contains (options, key))
486 error (0, 0, _("%s: output option specified more than once"), key);
491 char *value = xmemdup0 (equals + 1, strlen (equals + 1));
492 string_map_insert_nocopy (options, key, value);
495 /* Extracts the actual text content from the given Pango MARKUP and returns it
496 as as a malloc()'d string. */
498 output_get_text_from_markup (const char *markup)
500 xmlDoc *doc = xmlReadMemory (markup, strlen (markup), "noname.xml", "UTF-8",
501 (XML_PARSE_NOERROR | XML_PARSE_NOWARNING
502 | XML_PARSE_NONET | XML_PARSE_NOCDATA));
504 return xstrdup (markup);
506 char *content = CHAR_CAST (char *,
507 xmlNodeGetContent (xmlDocGetRootElement (doc)));
509 content = xstrdup (markup);