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/settings.h"
29 #include "libpspp/array.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/message.h"
32 #include "libpspp/llx.h"
33 #include "libpspp/string-map.h"
34 #include "libpspp/string-set.h"
35 #include "libpspp/str.h"
36 #include "output/message-item.h"
37 #include "output/output-item.h"
38 #include "output/text-item.h"
40 #include "gl/xalloc.h"
41 #include "gl/xmemdup0.h"
44 #define _(msgid) gettext (msgid)
48 struct llx_list drivers; /* Contains "struct output_driver"s. */
49 struct string deferred_syntax; /* TEXT_ITEM_SYNTAX being accumulated. */
52 static const struct output_driver_factory *factories[];
54 /* A stack of output engines.. */
55 static struct output_engine *engine_stack;
56 static size_t n_stack, allocated_stack;
58 static struct output_engine *
59 engine_stack_top (void)
62 return &engine_stack[n_stack - 1];
66 output_engine_push (void)
68 struct output_engine *e;
70 if (n_stack >= allocated_stack)
71 engine_stack = x2nrealloc (engine_stack, &allocated_stack,
72 sizeof *engine_stack);
74 e = &engine_stack[n_stack++];
75 llx_init (&e->drivers);
76 ds_init_empty (&e->deferred_syntax);
80 output_engine_pop (void)
82 struct output_engine *e;
85 e = &engine_stack[--n_stack];
86 while (!llx_is_empty (&e->drivers))
88 struct output_driver *d = llx_pop_head (&e->drivers, &llx_malloc_mgr);
89 output_driver_destroy (d);
91 ds_destroy (&e->deferred_syntax);
95 output_get_supported_formats (struct string_set *formats)
97 const struct output_driver_factory **fp;
99 for (fp = factories; *fp != NULL; fp++)
100 string_set_insert (formats, (*fp)->extension);
104 output_submit__ (struct output_engine *e, struct output_item *item)
106 struct llx *llx, *next;
108 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers); llx = next)
110 struct output_driver *d = llx_data (llx);
111 enum settings_output_type type;
113 next = llx_next (llx);
115 if (is_message_item (item))
117 const struct msg *m = message_item_get_msg (to_message_item (item));
118 if (m->severity == MSG_S_NOTE)
119 type = SETTINGS_OUTPUT_NOTE;
121 type = SETTINGS_OUTPUT_ERROR;
123 else if (is_text_item (item)
124 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
125 type = SETTINGS_OUTPUT_SYNTAX;
127 type = SETTINGS_OUTPUT_RESULT;
129 if (settings_get_output_routing (type) & d->device_type)
130 d->class->submit (d, item);
133 output_item_unref (item);
137 flush_deferred_syntax (struct output_engine *e)
139 if (!ds_is_empty (&e->deferred_syntax))
141 char *syntax = ds_steal_cstr (&e->deferred_syntax);
142 output_submit__ (e, text_item_super (
143 text_item_create_nocopy (TEXT_ITEM_SYNTAX, syntax)));
148 is_syntax_item (const struct output_item *item)
150 return (is_text_item (item)
151 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
154 /* Submits ITEM to the configured output drivers, and transfers ownership to
155 the output subsystem. */
157 output_submit (struct output_item *item)
159 struct output_engine *e = engine_stack_top ();
161 if (is_syntax_item (item))
163 ds_put_cstr (&e->deferred_syntax, text_item_get_text (to_text_item (item)));
164 output_item_unref (item);
168 flush_deferred_syntax (e);
169 output_submit__ (e, item);
172 /* Flushes output to screen devices, so that the user can see
173 output that doesn't fill up an entire page. */
177 struct output_engine *e = engine_stack_top ();
180 flush_deferred_syntax (e);
181 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers);
182 llx = llx_next (llx))
184 struct output_driver *d = llx_data (llx);
185 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
191 output_driver_init (struct output_driver *driver,
192 const struct output_driver_class *class,
193 const char *name, enum settings_output_devices type)
195 driver->class = class;
196 driver->name = xstrdup (name);
197 driver->device_type = type;
201 output_driver_destroy (struct output_driver *driver)
205 char *name = driver->name;
206 if (output_driver_is_registered (driver))
207 output_driver_unregister (driver);
208 if (driver->class->destroy)
209 driver->class->destroy (driver);
215 output_driver_get_name (const struct output_driver *driver)
220 static struct output_engine *
221 output_driver_get_engine (const struct output_driver *driver)
223 struct output_engine *e;
225 for (e = engine_stack; e < &engine_stack[n_stack]; e++)
226 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
233 output_driver_register (struct output_driver *driver)
235 struct output_engine *e = engine_stack_top ();
237 assert (!output_driver_is_registered (driver));
238 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
242 output_driver_unregister (struct output_driver *driver)
244 struct output_engine *e = output_driver_get_engine (driver);
247 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
252 output_driver_is_registered (const struct output_driver *driver)
254 return output_driver_get_engine (driver) != NULL;
257 /* Useful functions for output driver implementation. */
260 output_driver_track_current_command (const struct output_item *output_item,
261 char **command_namep)
263 if (is_text_item (output_item))
265 const struct text_item *item = to_text_item (output_item);
266 const char *text = text_item_get_text (item);
267 enum text_item_type type = text_item_get_type (item);
269 if (type == TEXT_ITEM_COMMAND_OPEN)
271 free (*command_namep);
272 *command_namep = xstrdup (text);
274 else if (type == TEXT_ITEM_COMMAND_CLOSE)
276 free (*command_namep);
277 *command_namep = NULL;
282 extern const struct output_driver_factory txt_driver_factory;
283 extern const struct output_driver_factory list_driver_factory;
284 extern const struct output_driver_factory html_driver_factory;
285 extern const struct output_driver_factory csv_driver_factory;
286 #ifdef ODF_WRITE_SUPPORT
287 extern const struct output_driver_factory odt_driver_factory;
290 extern const struct output_driver_factory pdf_driver_factory;
291 extern const struct output_driver_factory ps_driver_factory;
292 extern const struct output_driver_factory svg_driver_factory;
295 static const struct output_driver_factory *factories[] =
298 &list_driver_factory,
299 &html_driver_factory,
301 #ifdef ODF_WRITE_SUPPORT
312 static const struct output_driver_factory *
313 find_factory (const char *format)
315 const struct output_driver_factory **fp;
317 for (fp = factories; *fp != NULL; fp++)
319 const struct output_driver_factory *f = *fp;
321 if (!strcmp (f->extension, format))
324 return &txt_driver_factory;
327 static enum settings_output_devices
328 default_device_type (const char *file_name)
330 return (!strcmp (file_name, "-")
331 ? SETTINGS_DEVICE_TERMINAL
332 : SETTINGS_DEVICE_LISTING);
335 struct output_driver *
336 output_driver_create (struct string_map *options)
338 enum settings_output_devices device_type;
339 const struct output_driver_factory *f;
340 struct output_driver *driver;
345 format = string_map_find_and_delete (options, "format");
346 file_name = string_map_find_and_delete (options, "output-file");
350 if (file_name != NULL)
352 const char *extension = strrchr (file_name, '.');
353 format = xstrdup (extension != NULL ? extension + 1 : "");
356 format = xstrdup ("txt");
358 f = find_factory (format);
360 if (file_name == NULL)
361 file_name = xstrdup (f->default_file_name);
363 /* XXX should use parse_enum(). */
364 device_string = string_map_find_and_delete (options, "device");
365 if (device_string == NULL || device_string[0] == '\0')
366 device_type = default_device_type (file_name);
367 else if (!strcmp (device_string, "terminal"))
368 device_type = SETTINGS_DEVICE_TERMINAL;
369 else if (!strcmp (device_string, "listing"))
370 device_type = SETTINGS_DEVICE_LISTING;
373 msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
374 device_string, "terminal", "listing");
375 device_type = default_device_type (file_name);
378 driver = f->create (file_name, device_type, options);
381 const struct string_map_node *node;
384 STRING_MAP_FOR_EACH_KEY (key, node, options)
385 msg (MW, _("%s: unknown option `%s'"), file_name, key);
387 string_map_clear (options);
391 free (device_string);