0a6afba98a5d7eeec20b1060217a1ea65e643296
[pspp] / src / output / driver.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "output/driver.h"
20 #include "output/driver-provider.h"
21
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include <string.h>
27
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/message-item.h"
38 #include "output/output-item.h"
39 #include "output/text-item.h"
40
41 #include "gl/xalloc.h"
42 #include "gl/xmemdup0.h"
43
44 #include "gettext.h"
45 #define _(msgid) gettext (msgid)
46
47 static const struct output_driver_factory *factories[];
48
49 /* Drivers currently registered with output_driver_register(). */
50 static struct llx_list drivers = LLX_INITIALIZER (drivers);
51
52 /* TEXT_ITEM_SYNTAX being accumulated until another kind of output arrives. */
53 static struct string deferred_syntax = DS_EMPTY_INITIALIZER;
54
55 void
56 output_close (void)
57 {
58   while (!llx_is_empty (&drivers))
59     {
60       struct output_driver *d = llx_pop_head (&drivers, &llx_malloc_mgr);
61       output_driver_destroy (d);
62     }
63 }
64
65 void
66 output_get_supported_formats (struct string_set *formats)
67 {
68   const struct output_driver_factory **fp;
69
70   for (fp = factories; *fp != NULL; fp++)
71     string_set_insert (formats, (*fp)->extension);
72 }
73
74 static void
75 output_submit__ (struct output_item *item)
76 {
77   struct llx *llx, *next;
78
79   for (llx = llx_head (&drivers); llx != llx_null (&drivers); llx = next)
80     {
81       struct output_driver *d = llx_data (llx);
82       enum settings_output_type type;
83
84       next = llx_next (llx);
85
86       if (is_message_item (item))
87         {
88           const struct msg *m = message_item_get_msg (to_message_item (item));
89           if (m->severity == MSG_S_NOTE)
90             type = SETTINGS_OUTPUT_NOTE;
91           else
92             type = SETTINGS_OUTPUT_ERROR;
93         }
94       else if (is_text_item (item)
95                && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
96         type = SETTINGS_OUTPUT_SYNTAX;
97       else
98         type = SETTINGS_OUTPUT_RESULT;
99
100       if (settings_get_output_routing (type) & d->device_type)
101         d->class->submit (d, item);
102     }
103
104   output_item_unref (item);
105 }
106
107 static void
108 flush_deferred_syntax (void)
109 {
110   if (!ds_is_empty (&deferred_syntax))
111     {
112       char *syntax = ds_steal_cstr (&deferred_syntax);
113       output_submit__ (text_item_super (
114                          text_item_create_nocopy (TEXT_ITEM_SYNTAX, syntax)));
115     }
116 }
117
118 static bool
119 is_syntax_item (const struct output_item *item)
120 {
121   return (is_text_item (item)
122           && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
123 }
124
125 /* Submits ITEM to the configured output drivers, and transfers ownership to
126    the output subsystem. */
127 void
128 output_submit (struct output_item *item)
129 {
130   if (is_syntax_item (item))
131     {
132       ds_put_cstr (&deferred_syntax, text_item_get_text (to_text_item (item)));
133       output_item_unref (item);
134       return;
135     }
136
137   flush_deferred_syntax ();
138   output_submit__ (item);
139 }
140
141 /* Flushes output to screen devices, so that the user can see
142    output that doesn't fill up an entire page. */
143 void
144 output_flush (void)
145 {
146   struct llx *llx;
147
148   flush_deferred_syntax ();
149   for (llx = llx_head (&drivers); llx != llx_null (&drivers);
150        llx = llx_next (llx))
151     {
152       struct output_driver *d = llx_data (llx);
153       if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
154         d->class->flush (d);
155     }
156 }
157 \f
158 void
159 output_driver_init (struct output_driver *driver,
160                     const struct output_driver_class *class,
161                     const char *name, enum settings_output_devices type)
162 {
163   driver->class = class;
164   driver->name = xstrdup (name);
165   driver->device_type = type;
166 }
167
168 void
169 output_driver_destroy (struct output_driver *driver)
170 {
171   if (driver != NULL)
172     {
173       char *name = driver->name;
174       if (output_driver_is_registered (driver))
175         output_driver_unregister (driver);
176       if (driver->class->destroy)
177         driver->class->destroy (driver);
178       free (name);
179     }
180 }
181
182 const char *
183 output_driver_get_name (const struct output_driver *driver)
184 {
185   return driver->name;
186 }
187 \f
188 void
189 output_driver_register (struct output_driver *driver)
190 {
191   assert (!output_driver_is_registered (driver));
192   llx_push_tail (&drivers, driver, &llx_malloc_mgr);
193 }
194
195 void
196 output_driver_unregister (struct output_driver *driver)
197 {
198   llx_remove (llx_find (llx_head (&drivers), llx_null (&drivers), driver),
199               &llx_malloc_mgr);
200 }
201
202 bool
203 output_driver_is_registered (const struct output_driver *driver)
204 {
205   return llx_find (llx_head (&drivers), llx_null (&drivers), driver) != NULL;
206 }
207 \f
208 /* Useful functions for output driver implementation. */
209
210 void
211 output_driver_track_current_command (const struct output_item *output_item,
212                                      char **command_namep)
213 {
214   if (is_text_item (output_item))
215     {
216       const struct text_item *item = to_text_item (output_item);
217       const char *text = text_item_get_text (item);
218       enum text_item_type type = text_item_get_type (item);
219
220       if (type == TEXT_ITEM_COMMAND_OPEN)
221         {
222           free (*command_namep);
223           *command_namep = xstrdup (text);
224         }
225       else if (type == TEXT_ITEM_COMMAND_CLOSE)
226         {
227           free (*command_namep);
228           *command_namep = NULL;
229         }
230     }
231 }
232 \f
233 extern const struct output_driver_factory txt_driver_factory;
234 extern const struct output_driver_factory list_driver_factory;
235 extern const struct output_driver_factory html_driver_factory;
236 extern const struct output_driver_factory csv_driver_factory;
237 #ifdef ODF_WRITE_SUPPORT
238 extern const struct output_driver_factory odt_driver_factory;
239 #endif
240 #ifdef HAVE_CAIRO
241 extern const struct output_driver_factory pdf_driver_factory;
242 extern const struct output_driver_factory ps_driver_factory;
243 extern const struct output_driver_factory svg_driver_factory;
244 #endif
245
246 static const struct output_driver_factory *factories[] =
247   {
248     &txt_driver_factory,
249     &list_driver_factory,
250     &html_driver_factory,
251     &csv_driver_factory,
252 #ifdef ODF_WRITE_SUPPORT
253     &odt_driver_factory,
254 #endif
255 #ifdef HAVE_CAIRO
256     &pdf_driver_factory,
257     &ps_driver_factory,
258     &svg_driver_factory,
259 #endif
260     NULL
261   };
262
263 static const struct output_driver_factory *
264 find_factory (const char *format)
265 {
266   const struct output_driver_factory **fp;
267
268   for (fp = factories; *fp != NULL; fp++)
269     {
270       const struct output_driver_factory *f = *fp;
271
272       if (!strcmp (f->extension, format))
273         return f;
274     }
275   return &txt_driver_factory;
276 }
277
278 static enum settings_output_devices
279 default_device_type (const char *file_name)
280 {
281   return (!strcmp (file_name, "-")
282           ? SETTINGS_DEVICE_TERMINAL
283           : SETTINGS_DEVICE_LISTING);
284 }
285
286 struct output_driver *
287 output_driver_create (struct string_map *options)
288 {
289   enum settings_output_devices device_type;
290   const struct output_driver_factory *f;
291   struct output_driver *driver;
292   char *device_string;
293   char *file_name;
294   char *format;
295
296   format = string_map_find_and_delete (options, "format");
297   file_name = string_map_find_and_delete (options, "output-file");
298
299   if (format == NULL)
300     {
301       if (file_name != NULL)
302         {
303           const char *extension = strrchr (file_name, '.');
304           format = xstrdup (extension != NULL ? extension + 1 : "");
305         }
306       else
307         format = xstrdup ("txt");
308     }
309   f = find_factory (format);
310
311   if (file_name == NULL)
312     file_name = xstrdup (f->default_file_name);
313
314   /* XXX should use parse_enum(). */
315   device_string = string_map_find_and_delete (options, "device");
316   if (device_string == NULL || device_string[0] == '\0')
317     device_type = default_device_type (file_name);
318   else if (!strcmp (device_string, "terminal"))
319     device_type = SETTINGS_DEVICE_TERMINAL;
320   else if (!strcmp (device_string, "listing"))
321     device_type = SETTINGS_DEVICE_LISTING;
322   else
323     {
324       msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
325                      device_string, "terminal", "listing");
326       device_type = default_device_type (file_name);
327     }
328
329   driver = f->create (file_name, device_type, options);
330   if (driver != NULL)
331     {
332       const struct string_map_node *node;
333       const char *key;
334
335       STRING_MAP_FOR_EACH_KEY (key, node, options)
336         msg (MW, _("%s: unknown option `%s'"), file_name, key);
337     }
338   string_map_clear (options);
339
340   free (file_name);
341   free (format);
342   free (device_string);
343
344   return driver;
345 }