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