driver: New function output_driver_parse_option().
[pspp] / src / output / driver.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012, 2014 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-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"
40
41 #include "gl/error.h"
42 #include "gl/xalloc.h"
43 #include "gl/xmemdup0.h"
44
45 #include "gettext.h"
46 #define _(msgid) gettext (msgid)
47
48 struct output_engine
49   {
50     struct llx_list drivers;       /* Contains "struct output_driver"s. */
51     struct string deferred_syntax; /* TEXT_ITEM_SYNTAX being accumulated. */
52     char *command_name;            /* Name of command being processed. */
53   };
54
55 static const struct output_driver_factory *factories[];
56
57 /* A stack of output engines.. */
58 static struct output_engine *engine_stack;
59 static size_t n_stack, allocated_stack;
60
61 static struct output_engine *
62 engine_stack_top (void)
63 {
64   assert (n_stack > 0);
65   return &engine_stack[n_stack - 1];
66 }
67
68 void
69 output_engine_push (void)
70 {
71   struct output_engine *e;
72
73   if (n_stack >= allocated_stack)
74     engine_stack = x2nrealloc (engine_stack, &allocated_stack,
75                                sizeof *engine_stack);
76
77   e = &engine_stack[n_stack++];
78   llx_init (&e->drivers);
79   ds_init_empty (&e->deferred_syntax);
80   e->command_name = NULL;
81 }
82
83 void
84 output_engine_pop (void)
85 {
86   struct output_engine *e;
87
88   assert (n_stack > 0);
89   e = &engine_stack[--n_stack];
90   while (!llx_is_empty (&e->drivers))
91     {
92       struct output_driver *d = llx_pop_head (&e->drivers, &llx_malloc_mgr);
93       output_driver_destroy (d);
94     }
95   ds_destroy (&e->deferred_syntax);
96   free (e->command_name);
97 }
98
99 void
100 output_get_supported_formats (struct string_set *formats)
101 {
102   const struct output_driver_factory **fp;
103
104   for (fp = factories; *fp != NULL; fp++)
105     string_set_insert (formats, (*fp)->extension);
106 }
107
108 static void
109 output_submit__ (struct output_engine *e, struct output_item *item)
110 {
111   struct llx *llx, *next;
112
113   for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers); llx = next)
114     {
115       struct output_driver *d = llx_data (llx);
116       enum settings_output_type type;
117
118       next = llx_next (llx);
119
120       if (is_message_item (item))
121         {
122           const struct msg *m = message_item_get_msg (to_message_item (item));
123           if (m->severity == MSG_S_NOTE)
124             type = SETTINGS_OUTPUT_NOTE;
125           else
126             type = SETTINGS_OUTPUT_ERROR;
127         }
128       else if (is_text_item (item)
129                && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
130         type = SETTINGS_OUTPUT_SYNTAX;
131       else
132         type = SETTINGS_OUTPUT_RESULT;
133
134       if (settings_get_output_routing (type) & d->device_type)
135         d->class->submit (d, item);
136     }
137
138   output_item_unref (item);
139 }
140
141 static void
142 flush_deferred_syntax (struct output_engine *e)
143 {
144   if (!ds_is_empty (&e->deferred_syntax))
145     {
146       ds_trim (&e->deferred_syntax, ss_cstr ("\n"));
147       if (!ds_is_empty (&e->deferred_syntax))
148         {
149           char *syntax = ds_steal_cstr (&e->deferred_syntax);
150           output_submit__ (e, text_item_super (text_item_create_nocopy (
151                                                  TEXT_ITEM_SYNTAX, syntax)));
152         }
153     }
154 }
155
156 static bool
157 is_syntax_item (const struct output_item *item)
158 {
159   return (is_text_item (item)
160           && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
161 }
162
163 /* Submits ITEM to the configured output drivers, and transfers ownership to
164    the output subsystem. */
165 void
166 output_submit (struct output_item *item)
167 {
168   struct output_engine *e = engine_stack_top ();
169
170   if (item == NULL)
171     return;
172
173   if (is_syntax_item (item))
174     {
175       ds_put_cstr (&e->deferred_syntax, text_item_get_text (to_text_item (item)));
176       output_item_unref (item);
177       return;
178     }
179
180   flush_deferred_syntax (e);
181
182   if (is_text_item (item))
183     {
184       const struct text_item *text_item = to_text_item (item);
185       const char *text = text_item_get_text (text_item);
186       enum text_item_type type = text_item_get_type (text_item);
187
188       if (type == TEXT_ITEM_COMMAND_OPEN)
189         {
190           free (e->command_name);
191           e->command_name = xstrdup (text);
192         }
193       else if (type == TEXT_ITEM_COMMAND_CLOSE)
194         {
195           free (e->command_name);
196           e->command_name = NULL;
197         }
198     }
199   else if (is_message_item (item))
200     {
201       struct message_item *message_item = to_message_item (item);
202       free (message_item->command_name);
203       message_item->command_name = (e->command_name
204                                     ? xstrdup (e->command_name)
205                                     : NULL);
206     }
207
208   output_submit__ (e, item);
209 }
210
211 /* Flushes output to screen devices, so that the user can see
212    output that doesn't fill up an entire page. */
213 void
214 output_flush (void)
215 {
216   struct output_engine *e = engine_stack_top ();
217   struct llx *llx;
218
219   flush_deferred_syntax (e);
220   for (llx = llx_head (&e->drivers); llx != llx_null (&e->drivers);
221        llx = llx_next (llx))
222     {
223       struct output_driver *d = llx_data (llx);
224       if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
225         d->class->flush (d);
226     }
227 }
228 \f
229 void
230 output_driver_init (struct output_driver *driver,
231                     const struct output_driver_class *class,
232                     const char *name, enum settings_output_devices type)
233 {
234   driver->class = class;
235   driver->name = xstrdup (name);
236   driver->device_type = type;
237 }
238
239 void
240 output_driver_destroy (struct output_driver *driver)
241 {
242   if (driver != NULL)
243     {
244       char *name = driver->name;
245       if (output_driver_is_registered (driver))
246         output_driver_unregister (driver);
247       if (driver->class->destroy)
248         driver->class->destroy (driver);
249       free (name);
250     }
251 }
252
253 const char *
254 output_driver_get_name (const struct output_driver *driver)
255 {
256   return driver->name;
257 }
258 \f
259 static struct output_engine *
260 output_driver_get_engine (const struct output_driver *driver)
261 {
262   struct output_engine *e;
263
264   for (e = engine_stack; e < &engine_stack[n_stack]; e++)
265     if (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver))
266       return e;
267
268   return NULL;
269 }
270
271 void
272 output_driver_register (struct output_driver *driver)
273 {
274   struct output_engine *e = engine_stack_top ();
275
276   assert (!output_driver_is_registered (driver));
277   llx_push_tail (&e->drivers, driver, &llx_malloc_mgr);
278 }
279
280 void
281 output_driver_unregister (struct output_driver *driver)
282 {
283   struct output_engine *e = output_driver_get_engine (driver);
284
285   assert (e != NULL);
286   llx_remove (llx_find (llx_head (&e->drivers), llx_null (&e->drivers), driver),
287               &llx_malloc_mgr);
288 }
289
290 bool
291 output_driver_is_registered (const struct output_driver *driver)
292 {
293   return output_driver_get_engine (driver) != NULL;
294 }
295 \f
296 extern const struct output_driver_factory txt_driver_factory;
297 extern const struct output_driver_factory list_driver_factory;
298 extern const struct output_driver_factory html_driver_factory;
299 extern const struct output_driver_factory csv_driver_factory;
300 extern const struct output_driver_factory odt_driver_factory;
301 #ifdef HAVE_CAIRO
302 extern const struct output_driver_factory pdf_driver_factory;
303 extern const struct output_driver_factory ps_driver_factory;
304 extern const struct output_driver_factory svg_driver_factory;
305 #endif
306
307 static const struct output_driver_factory *factories[] =
308   {
309     &txt_driver_factory,
310     &list_driver_factory,
311     &html_driver_factory,
312     &csv_driver_factory,
313     &odt_driver_factory,
314 #ifdef HAVE_CAIRO
315     &pdf_driver_factory,
316     &ps_driver_factory,
317     &svg_driver_factory,
318 #endif
319     NULL
320   };
321
322 static const struct output_driver_factory *
323 find_factory (const char *format)
324 {
325   const struct output_driver_factory **fp;
326
327   for (fp = factories; *fp != NULL; fp++)
328     {
329       const struct output_driver_factory *f = *fp;
330
331       if (!strcmp (f->extension, format))
332         return f;
333     }
334   return &txt_driver_factory;
335 }
336
337 static enum settings_output_devices
338 default_device_type (const char *file_name)
339 {
340   return (!strcmp (file_name, "-")
341           ? SETTINGS_DEVICE_TERMINAL
342           : SETTINGS_DEVICE_LISTING);
343 }
344
345 struct output_driver *
346 output_driver_create (struct string_map *options)
347 {
348   enum settings_output_devices device_type;
349   const struct output_driver_factory *f;
350   struct output_driver *driver;
351   char *device_string;
352   char *file_name;
353   char *format;
354
355   format = string_map_find_and_delete (options, "format");
356   file_name = string_map_find_and_delete (options, "output-file");
357
358   if (format == NULL)
359     {
360       if (file_name != NULL)
361         {
362           const char *extension = strrchr (file_name, '.');
363           format = xstrdup (extension != NULL ? extension + 1 : "");
364         }
365       else
366         format = xstrdup ("txt");
367     }
368   f = find_factory (format);
369
370   if (file_name == NULL)
371     file_name = xstrdup (f->default_file_name);
372
373   /* XXX should use parse_enum(). */
374   device_string = string_map_find_and_delete (options, "device");
375   if (device_string == NULL || device_string[0] == '\0')
376     device_type = default_device_type (file_name);
377   else if (!strcmp (device_string, "terminal"))
378     device_type = SETTINGS_DEVICE_TERMINAL;
379   else if (!strcmp (device_string, "listing"))
380     device_type = SETTINGS_DEVICE_LISTING;
381   else
382     {
383       msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
384                      device_string, "terminal", "listing");
385       device_type = default_device_type (file_name);
386     }
387
388   struct file_handle *fh = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
389
390   driver = f->create (fh, device_type, options);
391   if (driver != NULL)
392     {
393       const struct string_map_node *node;
394       const char *key;
395
396       STRING_MAP_FOR_EACH_KEY (key, node, options)
397         msg (MW, _("%s: unknown option `%s'"), file_name, key);
398     }
399   string_map_clear (options);
400
401   free (file_name);
402   free (format);
403   free (device_string);
404
405   return driver;
406 }
407
408 void
409 output_driver_parse_option (const char *option, struct string_map *options)
410 {
411   const char *equals = strchr (option, '=');
412   if (equals == NULL)
413     {
414       error (0, 0, _("%s: output option missing `='"), option);
415       return;
416     }
417
418   char *key = xmemdup0 (option, equals - option);
419   if (string_map_contains (options, key))
420     {
421       error (0, 0, _("%s: output option specified more than once"), key);
422       free (key);
423       return;
424     }
425
426   char *value = xmemdup0 (equals + 1, strlen (equals + 1));
427   string_map_insert_nocopy (options, key, value);
428 }