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