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