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"
28 #include "data/file-handle-def.h"
29 #include "data/settings.h"
30 #include "libpspp/array.h"
31 #include "libpspp/assertion.h"
32 #include "libpspp/message.h"
33 #include "libpspp/llx.h"
34 #include "libpspp/string-map.h"
35 #include "libpspp/string-set.h"
36 #include "libpspp/str.h"
37 #include "output/message-item.h"
38 #include "output/output-item.h"
39 #include "output/text-item.h"
42 #include "gl/xalloc.h"
43 #include "gl/xmemdup0.h"
46 #define _(msgid) gettext (msgid)
50 struct llx_list drivers; /* Contains "struct output_driver"s. */
51 struct string deferred_syntax; /* TEXT_ITEM_SYNTAX being accumulated. */
52 char *command_name; /* Name of command being processed. */
53 char *title, *subtitle; /* Components of page title. */
56 static const struct output_driver_factory *factories[];
58 /* A stack of output engines.. */
59 static struct output_engine *engine_stack;
60 static size_t n_stack, allocated_stack;
62 static struct output_engine *
63 engine_stack_top (void)
66 return &engine_stack[n_stack - 1];
70 output_engine_push (void)
72 struct output_engine *e;
74 if (n_stack >= allocated_stack)
75 engine_stack = x2nrealloc (engine_stack, &allocated_stack,
76 sizeof *engine_stack);
78 e = &engine_stack[n_stack++];
79 llx_init (&e->drivers);
80 ds_init_empty (&e->deferred_syntax);
81 e->command_name = NULL;
87 output_engine_pop (void)
89 struct output_engine *e;
92 e = &engine_stack[--n_stack];
93 while (!llx_is_empty (&e->drivers))
95 struct output_driver *d = llx_pop_head (&e->drivers, &llx_malloc_mgr);
96 output_driver_destroy (d);
98 ds_destroy (&e->deferred_syntax);
99 free (e->command_name);
105 output_get_supported_formats (struct string_set *formats)
107 const struct output_driver_factory **fp;
109 for (fp = factories; *fp != NULL; fp++)
110 string_set_insert (formats, (*fp)->extension);
114 output_submit__ (struct output_engine *e, struct output_item *item)
116 struct llx *llx, *next;
118 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers); llx = next)
120 struct output_driver *d = llx_data (llx);
121 enum settings_output_type type;
123 next = llx_next (llx);
125 if (is_message_item (item))
127 const struct msg *m = message_item_get_msg (to_message_item (item));
128 if (m->severity == MSG_S_NOTE)
129 type = SETTINGS_OUTPUT_NOTE;
131 type = SETTINGS_OUTPUT_ERROR;
133 else if (is_text_item (item)
134 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
135 type = SETTINGS_OUTPUT_SYNTAX;
137 type = SETTINGS_OUTPUT_RESULT;
139 if (settings_get_output_routing (type) & d->device_type)
140 d->class->submit (d, item);
143 output_item_unref (item);
147 flush_deferred_syntax (struct output_engine *e)
149 if (!ds_is_empty (&e->deferred_syntax))
151 ds_trim (&e->deferred_syntax, ss_cstr ("\n"));
152 if (!ds_is_empty (&e->deferred_syntax))
154 char *syntax = ds_steal_cstr (&e->deferred_syntax);
155 output_submit__ (e, text_item_super (text_item_create_nocopy (
156 TEXT_ITEM_SYNTAX, syntax)));
162 is_syntax_item (const struct output_item *item)
164 return (is_text_item (item)
165 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
168 /* Submits ITEM to the configured output drivers, and transfers ownership to
169 the output subsystem. */
171 output_submit (struct output_item *item)
173 struct output_engine *e = engine_stack_top ();
178 if (is_syntax_item (item))
180 ds_put_cstr (&e->deferred_syntax, text_item_get_text (to_text_item (item)));
181 output_item_unref (item);
185 flush_deferred_syntax (e);
187 if (is_text_item (item))
189 const struct text_item *text_item = to_text_item (item);
190 const char *text = text_item_get_text (text_item);
191 enum text_item_type type = text_item_get_type (text_item);
193 if (type == TEXT_ITEM_COMMAND_OPEN)
195 free (e->command_name);
196 e->command_name = xstrdup (text);
198 else if (type == TEXT_ITEM_COMMAND_CLOSE)
200 free (e->command_name);
201 e->command_name = NULL;
204 else if (is_message_item (item))
206 struct message_item *message_item = to_message_item (item);
207 free (message_item->command_name);
208 message_item->command_name = (e->command_name
209 ? xstrdup (e->command_name)
213 output_submit__ (e, item);
216 /* Flushes output to screen devices, so that the user can see
217 output that doesn't fill up an entire page. */
221 struct output_engine *e = engine_stack_top ();
224 flush_deferred_syntax (e);
225 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers);
226 llx = llx_next (llx))
228 struct output_driver *d = llx_data (llx);
229 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
235 output_set_title__ (struct output_engine *e, char **dst, const char *src)
238 *dst = src ? xstrdup (src) : NULL;
241 = (e->title && e->subtitle ? xasprintf ("%s\n%s", e->title, e->subtitle)
242 : e->title ? xstrdup (e->title)
243 : e->subtitle ? xstrdup (e->subtitle)
245 text_item_submit (text_item_create_nocopy (TEXT_ITEM_PAGE_TITLE,
250 output_set_title (const char *title)
252 struct output_engine *e = engine_stack_top ();
254 output_set_title__ (e, &e->title, title);
258 output_set_subtitle (const char *subtitle)
260 struct output_engine *e = engine_stack_top ();
262 output_set_title__ (e, &e->subtitle, subtitle);
266 output_driver_init (struct output_driver *driver,
267 const struct output_driver_class *class,
268 const char *name, enum settings_output_devices type)
270 driver->class = class;
271 driver->name = xstrdup (name);
272 driver->device_type = type;
276 output_driver_destroy (struct output_driver *driver)
280 char *name = driver->name;
281 if (output_driver_is_registered (driver))
282 output_driver_unregister (driver);
283 if (driver->class->destroy)
284 driver->class->destroy (driver);
290 output_driver_get_name (const struct output_driver *driver)
295 static struct output_engine *
296 output_driver_get_engine (const struct output_driver *driver)
298 struct output_engine *e;
300 for (e = engine_stack; e < &engine_stack[n_stack]; e++)
301 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
308 output_driver_register (struct output_driver *driver)
310 struct output_engine *e = engine_stack_top ();
312 assert (!output_driver_is_registered (driver));
313 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
317 output_driver_unregister (struct output_driver *driver)
319 struct output_engine *e = output_driver_get_engine (driver);
322 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
327 output_driver_is_registered (const struct output_driver *driver)
329 return output_driver_get_engine (driver) != NULL;
332 extern const struct output_driver_factory txt_driver_factory;
333 extern const struct output_driver_factory list_driver_factory;
334 extern const struct output_driver_factory html_driver_factory;
335 extern const struct output_driver_factory csv_driver_factory;
336 extern const struct output_driver_factory odt_driver_factory;
338 extern const struct output_driver_factory pdf_driver_factory;
339 extern const struct output_driver_factory ps_driver_factory;
340 extern const struct output_driver_factory svg_driver_factory;
343 static const struct output_driver_factory *factories[] =
346 &list_driver_factory,
347 &html_driver_factory,
358 static const struct output_driver_factory *
359 find_factory (const char *format)
361 const struct output_driver_factory **fp;
363 for (fp = factories; *fp != NULL; fp++)
365 const struct output_driver_factory *f = *fp;
367 if (!strcmp (f->extension, format))
370 return &txt_driver_factory;
373 static enum settings_output_devices
374 default_device_type (const char *file_name)
376 return (!strcmp (file_name, "-")
377 ? SETTINGS_DEVICE_TERMINAL
378 : SETTINGS_DEVICE_LISTING);
381 struct output_driver *
382 output_driver_create (struct string_map *options)
384 enum settings_output_devices device_type;
385 const struct output_driver_factory *f;
386 struct output_driver *driver;
391 format = string_map_find_and_delete (options, "format");
392 file_name = string_map_find_and_delete (options, "output-file");
396 if (file_name != NULL)
398 const char *extension = strrchr (file_name, '.');
399 format = xstrdup (extension != NULL ? extension + 1 : "");
402 format = xstrdup ("txt");
404 f = find_factory (format);
406 if (file_name == NULL)
407 file_name = xstrdup (f->default_file_name);
409 /* XXX should use parse_enum(). */
410 device_string = string_map_find_and_delete (options, "device");
411 if (device_string == NULL || device_string[0] == '\0')
412 device_type = default_device_type (file_name);
413 else if (!strcmp (device_string, "terminal"))
414 device_type = SETTINGS_DEVICE_TERMINAL;
415 else if (!strcmp (device_string, "listing"))
416 device_type = SETTINGS_DEVICE_LISTING;
419 msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
420 device_string, "terminal", "listing");
421 device_type = default_device_type (file_name);
424 struct file_handle *fh = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
426 driver = f->create (fh, device_type, options);
429 const struct string_map_node *node;
432 STRING_MAP_FOR_EACH_KEY (key, node, options)
433 msg (MW, _("%s: unknown option `%s'"), file_name, key);
435 string_map_clear (options);
439 free (device_string);
445 output_driver_parse_option (const char *option, struct string_map *options)
447 const char *equals = strchr (option, '=');
450 error (0, 0, _("%s: output option missing `='"), option);
454 char *key = xmemdup0 (option, equals - option);
455 if (string_map_contains (options, key))
457 error (0, 0, _("%s: output option specified more than once"), key);
462 char *value = xmemdup0 (equals + 1, strlen (equals + 1));
463 string_map_insert_nocopy (options, key, value);