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