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"
41 #include "gl/xalloc.h"
42 #include "gl/xmemdup0.h"
45 #define _(msgid) gettext (msgid)
49 struct llx_list drivers; /* Contains "struct output_driver"s. */
50 struct string deferred_syntax; /* TEXT_ITEM_SYNTAX being accumulated. */
53 static const struct output_driver_factory *factories[];
55 /* A stack of output engines.. */
56 static struct output_engine *engine_stack;
57 static size_t n_stack, allocated_stack;
59 static struct output_engine *
60 engine_stack_top (void)
63 return &engine_stack[n_stack - 1];
67 output_engine_push (void)
69 struct output_engine *e;
71 if (n_stack >= allocated_stack)
72 engine_stack = x2nrealloc (engine_stack, &allocated_stack,
73 sizeof *engine_stack);
75 e = &engine_stack[n_stack++];
76 llx_init (&e->drivers);
77 ds_init_empty (&e->deferred_syntax);
81 output_engine_pop (void)
83 struct output_engine *e;
86 e = &engine_stack[--n_stack];
87 while (!llx_is_empty (&e->drivers))
89 struct output_driver *d = llx_pop_head (&e->drivers, &llx_malloc_mgr);
90 output_driver_destroy (d);
92 ds_destroy (&e->deferred_syntax);
96 output_get_supported_formats (struct string_set *formats)
98 const struct output_driver_factory **fp;
100 for (fp = factories; *fp != NULL; fp++)
101 string_set_insert (formats, (*fp)->extension);
105 output_submit__ (struct output_engine *e, struct output_item *item)
107 struct llx *llx, *next;
109 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers); llx = next)
111 struct output_driver *d = llx_data (llx);
112 enum settings_output_type type;
114 next = llx_next (llx);
116 if (is_message_item (item))
118 const struct msg *m = message_item_get_msg (to_message_item (item));
119 if (m->severity == MSG_S_NOTE)
120 type = SETTINGS_OUTPUT_NOTE;
122 type = SETTINGS_OUTPUT_ERROR;
124 else if (is_text_item (item)
125 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
126 type = SETTINGS_OUTPUT_SYNTAX;
128 type = SETTINGS_OUTPUT_RESULT;
130 if (settings_get_output_routing (type) & d->device_type)
131 d->class->submit (d, item);
134 output_item_unref (item);
138 flush_deferred_syntax (struct output_engine *e)
140 if (!ds_is_empty (&e->deferred_syntax))
142 char *syntax = ds_steal_cstr (&e->deferred_syntax);
143 output_submit__ (e, text_item_super (
144 text_item_create_nocopy (TEXT_ITEM_SYNTAX, syntax)));
149 is_syntax_item (const struct output_item *item)
151 return (is_text_item (item)
152 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
155 /* Submits ITEM to the configured output drivers, and transfers ownership to
156 the output subsystem. */
158 output_submit (struct output_item *item)
160 struct output_engine *e = engine_stack_top ();
162 if (is_syntax_item (item))
164 ds_put_cstr (&e->deferred_syntax, text_item_get_text (to_text_item (item)));
165 output_item_unref (item);
169 flush_deferred_syntax (e);
170 output_submit__ (e, item);
173 /* Flushes output to screen devices, so that the user can see
174 output that doesn't fill up an entire page. */
178 struct output_engine *e = engine_stack_top ();
181 flush_deferred_syntax (e);
182 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers);
183 llx = llx_next (llx))
185 struct output_driver *d = llx_data (llx);
186 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
192 output_driver_init (struct output_driver *driver,
193 const struct output_driver_class *class,
194 const char *name, enum settings_output_devices type)
196 driver->class = class;
197 driver->name = xstrdup (name);
198 driver->device_type = type;
202 output_driver_destroy (struct output_driver *driver)
206 char *name = driver->name;
207 if (output_driver_is_registered (driver))
208 output_driver_unregister (driver);
209 if (driver->class->destroy)
210 driver->class->destroy (driver);
216 output_driver_get_name (const struct output_driver *driver)
221 static struct output_engine *
222 output_driver_get_engine (const struct output_driver *driver)
224 struct output_engine *e;
226 for (e = engine_stack; e < &engine_stack[n_stack]; e++)
227 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
234 output_driver_register (struct output_driver *driver)
236 struct output_engine *e = engine_stack_top ();
238 assert (!output_driver_is_registered (driver));
239 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
243 output_driver_unregister (struct output_driver *driver)
245 struct output_engine *e = output_driver_get_engine (driver);
248 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
253 output_driver_is_registered (const struct output_driver *driver)
255 return output_driver_get_engine (driver) != NULL;
258 /* Useful functions for output driver implementation. */
261 output_driver_track_current_command (const struct output_item *output_item,
262 char **command_namep)
264 if (is_text_item (output_item))
266 const struct text_item *item = to_text_item (output_item);
267 const char *text = text_item_get_text (item);
268 enum text_item_type type = text_item_get_type (item);
270 if (type == TEXT_ITEM_COMMAND_OPEN)
272 free (*command_namep);
273 *command_namep = xstrdup (text);
275 else if (type == TEXT_ITEM_COMMAND_CLOSE)
277 free (*command_namep);
278 *command_namep = NULL;
283 extern const struct output_driver_factory txt_driver_factory;
284 extern const struct output_driver_factory list_driver_factory;
285 extern const struct output_driver_factory html_driver_factory;
286 extern const struct output_driver_factory csv_driver_factory;
287 #ifdef ODF_WRITE_SUPPORT
288 extern const struct output_driver_factory odt_driver_factory;
291 extern const struct output_driver_factory pdf_driver_factory;
292 extern const struct output_driver_factory ps_driver_factory;
293 extern const struct output_driver_factory svg_driver_factory;
296 static const struct output_driver_factory *factories[] =
299 &list_driver_factory,
300 &html_driver_factory,
302 #ifdef ODF_WRITE_SUPPORT
313 static const struct output_driver_factory *
314 find_factory (const char *format)
316 const struct output_driver_factory **fp;
318 for (fp = factories; *fp != NULL; fp++)
320 const struct output_driver_factory *f = *fp;
322 if (!strcmp (f->extension, format))
325 return &txt_driver_factory;
328 static enum settings_output_devices
329 default_device_type (const char *file_name)
331 return (!strcmp (file_name, "-")
332 ? SETTINGS_DEVICE_TERMINAL
333 : SETTINGS_DEVICE_LISTING);
336 struct output_driver *
337 output_driver_create (struct string_map *options)
339 enum settings_output_devices device_type;
340 const struct output_driver_factory *f;
341 struct output_driver *driver;
346 format = string_map_find_and_delete (options, "format");
347 file_name = string_map_find_and_delete (options, "output-file");
351 if (file_name != NULL)
353 const char *extension = strrchr (file_name, '.');
354 format = xstrdup (extension != NULL ? extension + 1 : "");
357 format = xstrdup ("txt");
359 f = find_factory (format);
361 if (file_name == NULL)
362 file_name = xstrdup (f->default_file_name);
364 /* XXX should use parse_enum(). */
365 device_string = string_map_find_and_delete (options, "device");
366 if (device_string == NULL || device_string[0] == '\0')
367 device_type = default_device_type (file_name);
368 else if (!strcmp (device_string, "terminal"))
369 device_type = SETTINGS_DEVICE_TERMINAL;
370 else if (!strcmp (device_string, "listing"))
371 device_type = SETTINGS_DEVICE_LISTING;
374 msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
375 device_string, "terminal", "listing");
376 device_type = default_device_type (file_name);
379 struct file_handle *fh = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
381 driver = f->create (fh, device_type, options);
384 const struct string_map_node *node;
387 STRING_MAP_FOR_EACH_KEY (key, node, options)
388 msg (MW, _("%s: unknown option `%s'"), file_name, key);
390 string_map_clear (options);
394 free (device_string);