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