1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010 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-name.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/output-item.h"
38 #include "output/text-item.h"
41 #include "gl/xalloc.h"
42 #include "gl/xmemdup0.h"
45 #define _(msgid) gettext (msgid)
47 static const struct output_driver_factory *factories[];
49 /* Drivers currently registered with output_driver_register(). */
50 static struct llx_list drivers = LLX_INITIALIZER (drivers);
52 static struct output_item *deferred_syntax;
53 static bool in_command;
58 while (!llx_is_empty (&drivers))
60 struct output_driver *d = llx_pop_head (&drivers, &llx_malloc_mgr);
61 output_driver_destroy (d);
66 output_get_supported_formats (struct string_set *formats)
68 const struct output_driver_factory **fp;
70 for (fp = factories; *fp != NULL; fp++)
71 string_set_insert (formats, (*fp)->extension);
75 output_submit__ (struct output_item *item)
77 struct llx *llx, *next;
79 for (llx = llx_head (&drivers); llx != llx_null (&drivers); llx = next)
81 struct output_driver *d = llx_data (llx);
82 enum settings_output_type type;
84 next = llx_next (llx);
86 if (is_text_item (item)
87 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
88 type = SETTINGS_OUTPUT_SYNTAX;
90 type = SETTINGS_OUTPUT_RESULT;
92 if (settings_get_output_routing (type) & d->device_type)
93 d->class->submit (d, item);
96 output_item_unref (item);
100 flush_deferred_syntax (void)
102 if (deferred_syntax != NULL)
104 output_submit__ (deferred_syntax);
105 deferred_syntax = NULL;
109 /* Submits ITEM to the configured output drivers, and transfers ownership to
110 the output subsystem. */
112 output_submit (struct output_item *item)
114 if (is_text_item (item))
116 struct text_item *text = to_text_item (item);
117 switch (text_item_get_type (text))
119 case TEXT_ITEM_SYNTAX:
122 flush_deferred_syntax ();
123 deferred_syntax = item;
128 case TEXT_ITEM_COMMAND_OPEN:
129 output_submit__ (item);
130 flush_deferred_syntax ();
134 case TEXT_ITEM_COMMAND_CLOSE:
143 output_submit__ (item);
146 /* Flushes output to screen devices, so that the user can see
147 output that doesn't fill up an entire page. */
153 for (llx = llx_head (&drivers); llx != llx_null (&drivers);
154 llx = llx_next (llx))
156 struct output_driver *d = llx_data (llx);
157 if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
163 output_driver_init (struct output_driver *driver,
164 const struct output_driver_class *class,
165 const char *name, enum settings_output_devices type)
167 driver->class = class;
168 driver->name = xstrdup (name);
169 driver->device_type = type;
173 output_driver_destroy (struct output_driver *driver)
177 char *name = driver->name;
178 if (output_driver_is_registered (driver))
179 output_driver_unregister (driver);
180 if (driver->class->destroy)
181 driver->class->destroy (driver);
187 output_driver_get_name (const struct output_driver *driver)
193 output_driver_register (struct output_driver *driver)
195 assert (!output_driver_is_registered (driver));
196 llx_push_tail (&drivers, driver, &llx_malloc_mgr);
200 output_driver_unregister (struct output_driver *driver)
202 llx_remove (llx_find (llx_head (&drivers), llx_null (&drivers), driver),
207 output_driver_is_registered (const struct output_driver *driver)
209 return llx_find (llx_head (&drivers), llx_null (&drivers), driver) != NULL;
212 /* Useful functions for output driver implementation. */
215 output_driver_track_current_command (const struct output_item *output_item,
216 char **command_namep)
218 if (is_text_item (output_item))
220 const struct text_item *item = to_text_item (output_item);
221 const char *text = text_item_get_text (item);
222 enum text_item_type type = text_item_get_type (item);
224 if (type == TEXT_ITEM_COMMAND_OPEN)
226 free (*command_namep);
227 *command_namep = xstrdup (text);
229 else if (type == TEXT_ITEM_COMMAND_CLOSE)
231 free (*command_namep);
232 *command_namep = NULL;
237 extern const struct output_driver_factory txt_driver_factory;
238 extern const struct output_driver_factory list_driver_factory;
239 extern const struct output_driver_factory html_driver_factory;
240 extern const struct output_driver_factory odt_driver_factory;
241 extern const struct output_driver_factory csv_driver_factory;
243 extern const struct output_driver_factory pdf_driver_factory;
244 extern const struct output_driver_factory ps_driver_factory;
245 extern const struct output_driver_factory svg_driver_factory;
248 static const struct output_driver_factory *factories[] =
251 &list_driver_factory,
252 &html_driver_factory,
263 static const struct output_driver_factory *
264 find_factory (const char *format)
266 const struct output_driver_factory **fp;
268 for (fp = factories; *fp != NULL; fp++)
270 const struct output_driver_factory *f = *fp;
272 if (!strcmp (f->extension, format))
275 return &txt_driver_factory;
278 static enum settings_output_devices
279 default_device_type (const char *file_name)
281 return (!strcmp (file_name, "-")
282 ? SETTINGS_DEVICE_TERMINAL
283 : SETTINGS_DEVICE_LISTING);
286 struct output_driver *
287 output_driver_create (struct string_map *options)
289 enum settings_output_devices device_type;
290 const struct output_driver_factory *f;
291 struct output_driver *driver;
296 file_name = string_map_find_and_delete (options, "output-file");
297 if (file_name == NULL)
298 file_name = xstrdup ("-");
300 format = string_map_find_and_delete (options, "format");
303 const char *extension = strrchr (file_name, '.');
304 format = xstrdup (extension != NULL ? extension + 1 : "");
307 /* XXX should use parse_enum(). */
308 device_string = string_map_find_and_delete (options, "device");
309 if (device_string == NULL || device_string[0] == '\0')
310 device_type = default_device_type (file_name);
311 else if (!strcmp (device_string, "terminal"))
312 device_type = SETTINGS_DEVICE_TERMINAL;
313 else if (!strcmp (device_string, "listing"))
314 device_type = SETTINGS_DEVICE_LISTING;
317 error (0, 0, _("%s is not a valid device type (the choices are "
318 "\"terminal\" and \"listing\")"), device_string);
319 device_type = default_device_type (file_name);
322 f = find_factory (format);
323 driver = f->create (file_name, device_type, options);
326 const struct string_map_node *node;
329 STRING_MAP_FOR_EACH_KEY (key, node, options)
330 error (0, 0, _("%s: unknown option \"%s\""), file_name, key);
332 string_map_clear (options);
336 free (device_string);