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