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. */
51 char *command_name; /* Name of command being processed. */
54 static const struct output_driver_factory *factories[];
56 /* A stack of output engines.. */
57 static struct output_engine *engine_stack;
58 static size_t n_stack, allocated_stack;
60 static struct output_engine *
61 engine_stack_top (void)
64 return &engine_stack[n_stack - 1];
68 output_engine_push (void)
70 struct output_engine *e;
72 if (n_stack >= allocated_stack)
73 engine_stack = x2nrealloc (engine_stack, &allocated_stack,
74 sizeof *engine_stack);
76 e = &engine_stack[n_stack++];
77 llx_init (&e->drivers);
78 ds_init_empty (&e->deferred_syntax);
79 e->command_name = NULL;
83 output_engine_pop (void)
85 struct output_engine *e;
88 e = &engine_stack[--n_stack];
89 while (!llx_is_empty (&e->drivers))
91 struct output_driver *d = llx_pop_head (&e->drivers, &llx_malloc_mgr);
92 output_driver_destroy (d);
94 ds_destroy (&e->deferred_syntax);
95 free (e->command_name);
99 output_get_supported_formats (struct string_set *formats)
101 const struct output_driver_factory **fp;
103 for (fp = factories; *fp != NULL; fp++)
104 string_set_insert (formats, (*fp)->extension);
108 output_submit__ (struct output_engine *e, struct output_item *item)
110 struct llx *llx, *next;
112 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers); llx = next)
114 struct output_driver *d = llx_data (llx);
115 enum settings_output_type type;
117 next = llx_next (llx);
119 if (is_message_item (item))
121 const struct msg *m = message_item_get_msg (to_message_item (item));
122 if (m->severity == MSG_S_NOTE)
123 type = SETTINGS_OUTPUT_NOTE;
125 type = SETTINGS_OUTPUT_ERROR;
127 else if (is_text_item (item)
128 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
129 type = SETTINGS_OUTPUT_SYNTAX;
131 type = SETTINGS_OUTPUT_RESULT;
133 if (settings_get_output_routing (type) & d->device_type)
134 d->class->submit (d, item);
137 output_item_unref (item);
141 flush_deferred_syntax (struct output_engine *e)
143 if (!ds_is_empty (&e->deferred_syntax))
145 ds_trim (&e->deferred_syntax, ss_cstr ("\n"));
146 if (!ds_is_empty (&e->deferred_syntax))
148 char *syntax = ds_steal_cstr (&e->deferred_syntax);
149 output_submit__ (e, text_item_super (text_item_create_nocopy (
150 TEXT_ITEM_SYNTAX, syntax)));
156 is_syntax_item (const struct output_item *item)
158 return (is_text_item (item)
159 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
162 /* Submits ITEM to the configured output drivers, and transfers ownership to
163 the output subsystem. */
165 output_submit (struct output_item *item)
167 struct output_engine *e = engine_stack_top ();
172 if (is_syntax_item (item))
174 ds_put_cstr (&e->deferred_syntax, text_item_get_text (to_text_item (item)));
175 output_item_unref (item);
179 flush_deferred_syntax (e);
181 if (is_text_item (item))
183 const struct text_item *text_item = to_text_item (item);
184 const char *text = text_item_get_text (text_item);
185 enum text_item_type type = text_item_get_type (text_item);
187 if (type == TEXT_ITEM_COMMAND_OPEN)
189 free (e->command_name);
190 e->command_name = xstrdup (text);
192 else if (type == TEXT_ITEM_COMMAND_CLOSE)
194 free (e->command_name);
195 e->command_name = NULL;
198 else if (is_message_item (item))
200 struct message_item *message_item = to_message_item (item);
201 free (message_item->command_name);
202 message_item->command_name = (e->command_name
203 ? xstrdup (e->command_name)
207 output_submit__ (e, item);
210 /* Flushes output to screen devices, so that the user can see
211 output that doesn't fill up an entire page. */
215 struct output_engine *e = engine_stack_top ();
218 flush_deferred_syntax (e);
219 for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers);
220 llx = llx_next (llx))
222 struct output_driver *d = llx_data (llx);
223 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
229 output_driver_init (struct output_driver *driver,
230 const struct output_driver_class *class,
231 const char *name, enum settings_output_devices type)
233 driver->class = class;
234 driver->name = xstrdup (name);
235 driver->device_type = type;
239 output_driver_destroy (struct output_driver *driver)
243 char *name = driver->name;
244 if (output_driver_is_registered (driver))
245 output_driver_unregister (driver);
246 if (driver->class->destroy)
247 driver->class->destroy (driver);
253 output_driver_get_name (const struct output_driver *driver)
258 static struct output_engine *
259 output_driver_get_engine (const struct output_driver *driver)
261 struct output_engine *e;
263 for (e = engine_stack; e < &engine_stack[n_stack]; e++)
264 if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
271 output_driver_register (struct output_driver *driver)
273 struct output_engine *e = engine_stack_top ();
275 assert (!output_driver_is_registered (driver));
276 llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
280 output_driver_unregister (struct output_driver *driver)
282 struct output_engine *e = output_driver_get_engine (driver);
285 llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
290 output_driver_is_registered (const struct output_driver *driver)
292 return output_driver_get_engine (driver) != NULL;
295 extern const struct output_driver_factory txt_driver_factory;
296 extern const struct output_driver_factory list_driver_factory;
297 extern const struct output_driver_factory html_driver_factory;
298 extern const struct output_driver_factory csv_driver_factory;
299 extern const struct output_driver_factory odt_driver_factory;
301 extern const struct output_driver_factory pdf_driver_factory;
302 extern const struct output_driver_factory ps_driver_factory;
303 extern const struct output_driver_factory svg_driver_factory;
306 static const struct output_driver_factory *factories[] =
309 &list_driver_factory,
310 &html_driver_factory,
321 static const struct output_driver_factory *
322 find_factory (const char *format)
324 const struct output_driver_factory **fp;
326 for (fp = factories; *fp != NULL; fp++)
328 const struct output_driver_factory *f = *fp;
330 if (!strcmp (f->extension, format))
333 return &txt_driver_factory;
336 static enum settings_output_devices
337 default_device_type (const char *file_name)
339 return (!strcmp (file_name, "-")
340 ? SETTINGS_DEVICE_TERMINAL
341 : SETTINGS_DEVICE_LISTING);
344 struct output_driver *
345 output_driver_create (struct string_map *options)
347 enum settings_output_devices device_type;
348 const struct output_driver_factory *f;
349 struct output_driver *driver;
354 format = string_map_find_and_delete (options, "format");
355 file_name = string_map_find_and_delete (options, "output-file");
359 if (file_name != NULL)
361 const char *extension = strrchr (file_name, '.');
362 format = xstrdup (extension != NULL ? extension + 1 : "");
365 format = xstrdup ("txt");
367 f = find_factory (format);
369 if (file_name == NULL)
370 file_name = xstrdup (f->default_file_name);
372 /* XXX should use parse_enum(). */
373 device_string = string_map_find_and_delete (options, "device");
374 if (device_string == NULL || device_string[0] == '\0')
375 device_type = default_device_type (file_name);
376 else if (!strcmp (device_string, "terminal"))
377 device_type = SETTINGS_DEVICE_TERMINAL;
378 else if (!strcmp (device_string, "listing"))
379 device_type = SETTINGS_DEVICE_LISTING;
382 msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
383 device_string, "terminal", "listing");
384 device_type = default_device_type (file_name);
387 struct file_handle *fh = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
389 driver = f->create (fh, device_type, options);
392 const struct string_map_node *node;
395 STRING_MAP_FOR_EACH_KEY (key, node, options)
396 msg (MW, _("%s: unknown option `%s'"), file_name, key);
398 string_map_clear (options);
402 free (device_string);