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