ascii: Print syntax in output single-spaced.
[pspp-builds.git] / src / output / driver.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011 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-name.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 static const struct output_driver_factory *factories[];
49
50 /* Drivers currently registered with output_driver_register(). */
51 static struct llx_list drivers = LLX_INITIALIZER (drivers);
52
53 /* TEXT_ITEM_SYNTAX being accumulated until another kind of output arrives. */
54 static struct string deferred_syntax = DS_EMPTY_INITIALIZER;
55
56 void
57 output_close (void)
58 {
59   while (!llx_is_empty (&drivers))
60     {
61       struct output_driver *d = llx_pop_head (&drivers, &llx_malloc_mgr);
62       output_driver_destroy (d);
63     }
64 }
65
66 void
67 output_get_supported_formats (struct string_set *formats)
68 {
69   const struct output_driver_factory **fp;
70
71   for (fp = factories; *fp != NULL; fp++)
72     string_set_insert (formats, (*fp)->extension);
73 }
74
75 static void
76 output_submit__ (struct output_item *item)
77 {
78   struct llx *llx, *next;
79
80   for (llx = llx_head (&drivers); llx != llx_null (&drivers); llx = next)
81     {
82       struct output_driver *d = llx_data (llx);
83       enum settings_output_type type;
84
85       next = llx_next (llx);
86
87       if (is_message_item (item))
88         {
89           const struct msg *m = message_item_get_msg (to_message_item (item));
90           if (m->severity == MSG_S_NOTE)
91             type = SETTINGS_OUTPUT_NOTE;
92           else
93             type = SETTINGS_OUTPUT_ERROR;
94         }
95       else if (is_text_item (item)
96                && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX)
97         type = SETTINGS_OUTPUT_SYNTAX;
98       else
99         type = SETTINGS_OUTPUT_RESULT;
100
101       if (settings_get_output_routing (type) & d->device_type)
102         d->class->submit (d, item);
103     }
104
105   output_item_unref (item);
106 }
107
108 static void
109 flush_deferred_syntax (void)
110 {
111   if (!ds_is_empty (&deferred_syntax))
112     {
113       char *syntax = ds_steal_cstr (&deferred_syntax);
114       output_submit__ (text_item_super (
115                          text_item_create_nocopy (TEXT_ITEM_SYNTAX, syntax)));
116     }
117 }
118
119 static bool
120 is_syntax_item (const struct output_item *item)
121 {
122   return (is_text_item (item)
123           && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
124 }
125
126 /* Submits ITEM to the configured output drivers, and transfers ownership to
127    the output subsystem. */
128 void
129 output_submit (struct output_item *item)
130 {
131   if (is_syntax_item (item))
132     {
133       ds_put_cstr (&deferred_syntax, text_item_get_text (to_text_item (item)));
134       output_item_unref (item);
135       return;
136     }
137
138   flush_deferred_syntax ();
139   output_submit__ (item);
140 }
141
142 /* Flushes output to screen devices, so that the user can see
143    output that doesn't fill up an entire page. */
144 void
145 output_flush (void)
146 {
147   struct llx *llx;
148
149   flush_deferred_syntax ();
150   for (llx = llx_head (&drivers); llx != llx_null (&drivers);
151        llx = llx_next (llx))
152     {
153       struct output_driver *d = llx_data (llx);
154       if (d->device_type & SETTINGS_DEVICE_TERMINAL && d->class->flush != NULL)
155         d->class->flush (d);
156     }
157 }
158 \f
159 void
160 output_driver_init (struct output_driver *driver,
161                     const struct output_driver_class *class,
162                     const char *name, enum settings_output_devices type)
163 {
164   driver->class = class;
165   driver->name = xstrdup (name);
166   driver->device_type = type;
167 }
168
169 void
170 output_driver_destroy (struct output_driver *driver)
171 {
172   if (driver != NULL)
173     {
174       char *name = driver->name;
175       if (output_driver_is_registered (driver))
176         output_driver_unregister (driver);
177       if (driver->class->destroy)
178         driver->class->destroy (driver);
179       free (name);
180     }
181 }
182
183 const char *
184 output_driver_get_name (const struct output_driver *driver)
185 {
186   return driver->name;
187 }
188 \f
189 void
190 output_driver_register (struct output_driver *driver)
191 {
192   assert (!output_driver_is_registered (driver));
193   llx_push_tail (&drivers, driver, &llx_malloc_mgr);
194 }
195
196 void
197 output_driver_unregister (struct output_driver *driver)
198 {
199   llx_remove (llx_find (llx_head (&drivers), llx_null (&drivers), driver),
200               &llx_malloc_mgr);
201 }
202
203 bool
204 output_driver_is_registered (const struct output_driver *driver)
205 {
206   return llx_find (llx_head (&drivers), llx_null (&drivers), driver) != NULL;
207 }
208 \f
209 /* Useful functions for output driver implementation. */
210
211 void
212 output_driver_track_current_command (const struct output_item *output_item,
213                                      char **command_namep)
214 {
215   if (is_text_item (output_item))
216     {
217       const struct text_item *item = to_text_item (output_item);
218       const char *text = text_item_get_text (item);
219       enum text_item_type type = text_item_get_type (item);
220
221       if (type == TEXT_ITEM_COMMAND_OPEN)
222         {
223           free (*command_namep);
224           *command_namep = xstrdup (text);
225         }
226       else if (type == TEXT_ITEM_COMMAND_CLOSE)
227         {
228           free (*command_namep);
229           *command_namep = NULL;
230         }
231     }
232 }
233 \f
234 extern const struct output_driver_factory txt_driver_factory;
235 extern const struct output_driver_factory list_driver_factory;
236 extern const struct output_driver_factory html_driver_factory;
237 extern const struct output_driver_factory csv_driver_factory;
238 #ifdef ODT_SUPPORT
239 extern const struct output_driver_factory odt_driver_factory;
240 #endif
241 #ifdef HAVE_CAIRO
242 extern const struct output_driver_factory pdf_driver_factory;
243 extern const struct output_driver_factory ps_driver_factory;
244 extern const struct output_driver_factory svg_driver_factory;
245 #endif
246
247 static const struct output_driver_factory *factories[] =
248   {
249     &txt_driver_factory,
250     &list_driver_factory,
251     &html_driver_factory,
252     &csv_driver_factory,
253 #ifdef ODT_SUPPORT
254     &odt_driver_factory,
255 #endif
256 #ifdef HAVE_CAIRO
257     &pdf_driver_factory,
258     &ps_driver_factory,
259     &svg_driver_factory,
260 #endif
261     NULL
262   };
263
264 static const struct output_driver_factory *
265 find_factory (const char *format)
266 {
267   const struct output_driver_factory **fp;
268
269   for (fp = factories; *fp != NULL; fp++)
270     {
271       const struct output_driver_factory *f = *fp;
272
273       if (!strcmp (f->extension, format))
274         return f;
275     }
276   return &txt_driver_factory;
277 }
278
279 static enum settings_output_devices
280 default_device_type (const char *file_name)
281 {
282   return (!strcmp (file_name, "-")
283           ? SETTINGS_DEVICE_TERMINAL
284           : SETTINGS_DEVICE_LISTING);
285 }
286
287 struct output_driver *
288 output_driver_create (struct string_map *options)
289 {
290   enum settings_output_devices device_type;
291   const struct output_driver_factory *f;
292   struct output_driver *driver;
293   char *device_string;
294   char *file_name;
295   char *format;
296
297   file_name = string_map_find_and_delete (options, "output-file");
298   if (file_name == NULL)
299     file_name = xstrdup ("-");
300
301   format = string_map_find_and_delete (options, "format");
302   if (format == NULL)
303     {
304       const char *extension = strrchr (file_name, '.');
305       format = xstrdup (extension != NULL ? extension + 1 : "");
306     }
307
308   /* XXX should use parse_enum(). */
309   device_string = string_map_find_and_delete (options, "device");
310   if (device_string == NULL || device_string[0] == '\0')
311     device_type = default_device_type (file_name);
312   else if (!strcmp (device_string, "terminal"))
313     device_type = SETTINGS_DEVICE_TERMINAL;
314   else if (!strcmp (device_string, "listing"))
315     device_type = SETTINGS_DEVICE_LISTING;
316   else
317     {
318       /* TRANSLATORS: Don't translate the words `terminal' or `listing'. */
319       error (0, 0, _("%s is not a valid device type (the choices are "
320                      "`terminal' and `listing')"), device_string);
321       device_type = default_device_type (file_name);
322     }
323
324   f = find_factory (format);
325   driver = f->create (file_name, device_type, options);
326   if (driver != NULL)
327     {
328       const struct string_map_node *node;
329       const char *key;
330
331       STRING_MAP_FOR_EACH_KEY (key, node, options)
332         error (0, 0, _("%s: unknown option `%s'"), file_name, key);
333     }
334   string_map_clear (options);
335
336   free (file_name);
337   free (format);
338   free (device_string);
339
340   return driver;
341 }