Fix numerous memory leaks.
[pspp] / tests / output / render-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2011, 2012, 2013, 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 <errno.h>
20 #include <getopt.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "data/file-handle-def.h"
26 #include "libpspp/assertion.h"
27 #include "libpspp/compiler.h"
28 #include "libpspp/string-map.h"
29 #include "output/ascii.h"
30 #include "output/driver.h"
31 #include "output/tab.h"
32 #include "output/table-item.h"
33
34 #include "gl/error.h"
35 #include "gl/progname.h"
36 #include "gl/xalloc.h"
37 #include "gl/xvasprintf.h"
38
39 /* --transpose: Transpose the table before outputting? */
40 static int transpose;
41
42 /* --emphasis: ASCII driver emphasis option. */
43 static bool bold;
44 static bool underline;
45
46 /* --box: ASCII driver box option. */
47 static char *box;
48
49 /* --draw-mode: special ASCII driver test mode. */
50 static int draw_mode;
51
52 /* --no-txt: Whether to render to <base>.txt. */
53 static int render_txt = true;
54
55 /* --no-stdout: Whether to render to stdout. */
56 static int render_stdout = true;
57
58 /* --pdf: Also render PDF output. */
59 static int render_pdf;
60
61 /* --csv: Also render CSV output. */
62 static int render_csv;
63
64 /* ASCII driver, for ASCII driver test mode. */
65 static struct output_driver *ascii_driver;
66
67 /* -o, --output: Base name for output files. */
68 static const char *output_base = "render";
69
70 static const char *parse_options (int argc, char **argv);
71 static void usage (void) NO_RETURN;
72 static struct table *read_table (FILE *);
73 static void draw (FILE *);
74
75 int
76 main (int argc, char **argv)
77 {
78   const char *input_file_name;
79   FILE *input;
80
81   set_program_name (argv[0]);
82   output_engine_push ();
83   input_file_name = parse_options (argc, argv);
84
85   if (!strcmp (input_file_name, "-"))
86     input = stdin;
87   else
88     {
89       input = fopen (input_file_name, "r");
90       if (input == NULL)
91         error (1, errno, "%s: open failed", input_file_name);
92     }
93
94   if (!draw_mode)
95     {
96       struct table **tables = NULL;
97       size_t allocated_tables = 0;
98       size_t n_tables = 0;
99       struct table *table;
100
101       for (;;)
102         {
103           int ch;
104
105           if (n_tables >= allocated_tables)
106             tables = x2nrealloc (tables, &allocated_tables, sizeof *tables);
107
108           tables[n_tables] = read_table (input);
109           n_tables++;
110
111           ch = getc (input);
112           if (ch == EOF)
113             break;
114           ungetc (ch, input);
115         }
116
117       table = tables[n_tables - 1];
118       if (transpose)
119         table = table_transpose (table);
120       table_item_submit (table_item_create (table, NULL, NULL));
121       free (tables);
122     }
123   else
124     draw (input);
125
126   if (input != stdin)
127     fclose (input);
128
129   output_engine_pop ();
130   fh_done ();
131
132   return 0;
133 }
134
135 static void
136 configure_drivers (int width, int length, int min_break)
137 {
138   struct string_map options, tmp;
139   struct output_driver *driver;
140
141   string_map_init (&options);
142   string_map_insert (&options, "format", "txt");
143   string_map_insert (&options, "output-file", "-");
144   string_map_insert_nocopy (&options, xstrdup ("width"),
145                             xasprintf ("%d", width));
146   if (min_break >= 0)
147     string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
148                               xasprintf ("%d", min_break));
149   if (bold || underline)
150     string_map_insert (&options, "emphasis", "true");
151   if (box != NULL)
152     string_map_insert (&options, "box", box);
153
154   /* Render to stdout. */
155   if (render_stdout)
156     {
157       string_map_clone (&tmp, &options);
158       ascii_driver = driver = output_driver_create (&tmp);
159       if (driver == NULL)
160         exit (EXIT_FAILURE);
161       output_driver_register (driver);
162       string_map_destroy (&tmp);
163     }
164
165   if (draw_mode)
166    {
167      string_map_destroy (&options);
168      return;
169    }
170
171   /* Render to <base>.txt. */
172   if (render_txt)
173     {
174       string_map_clear (&options);
175       string_map_insert_nocopy (&options, xstrdup ("output-file"),
176                                 xasprintf ("%s.txt", output_base));
177       driver = output_driver_create (&options);
178       if (driver == NULL)
179         exit (EXIT_FAILURE);
180       output_driver_register (driver);
181     }
182
183 #ifdef HAVE_CAIRO
184   /* Render to <base>.pdf. */
185   if (render_pdf)
186     {
187       string_map_clear (&options);
188       string_map_insert_nocopy (&options, xstrdup ("output-file"),
189                                  xasprintf ("%s.pdf", output_base));
190       string_map_insert (&options, "top-margin", "0");
191       string_map_insert (&options, "bottom-margin", "0");
192       string_map_insert (&options, "left-margin", "0");
193       string_map_insert (&options, "right-margin", "0");
194       string_map_insert_nocopy (&options, xstrdup ("paper-size"),
195                                 xasprintf ("%dx%dpt", width * 5, length * 8));
196       if (min_break >= 0)
197         {
198           string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
199                                     xasprintf ("%d", min_break * 5));
200           string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
201                                     xasprintf ("%d", min_break * 8));
202         }
203       driver = output_driver_create (&options);
204       if (driver == NULL)
205         exit (EXIT_FAILURE);
206       output_driver_register (driver);
207     }
208 #endif
209
210   /* Render to <base>.csv. */
211   if (render_csv)
212     {
213       string_map_clear (&options);
214       string_map_insert_nocopy (&options, xstrdup ("output-file"),
215                                  xasprintf ("%s.csv", output_base));
216       driver = output_driver_create (&options);
217       if (driver == NULL)
218         exit (EXIT_FAILURE);
219       output_driver_register (driver);
220     }
221
222   /* Render to <base>.odt. */
223   string_map_replace_nocopy (&options, xstrdup ("output-file"),
224                              xasprintf ("%s.odt", output_base));
225   driver = output_driver_create (&options);
226   if (driver == NULL)
227     exit (EXIT_FAILURE);
228   output_driver_register (driver);
229
230   string_map_destroy (&options);
231 }
232
233 static const char *
234 parse_options (int argc, char **argv)
235 {
236   int width = 79;
237   int length = 66;
238   int min_break = -1;
239
240   for (;;)
241     {
242       enum {
243         OPT_WIDTH = UCHAR_MAX + 1,
244         OPT_LENGTH,
245         OPT_MIN_BREAK,
246         OPT_EMPHASIS,
247         OPT_BOX,
248         OPT_HELP
249       };
250       static const struct option options[] =
251         {
252           {"width", required_argument, NULL, OPT_WIDTH},
253           {"length", required_argument, NULL, OPT_LENGTH},
254           {"min-break", required_argument, NULL, OPT_MIN_BREAK},
255           {"transpose", no_argument, &transpose, 1},
256           {"emphasis", required_argument, NULL, OPT_EMPHASIS},
257           {"box", required_argument, NULL, OPT_BOX},
258           {"draw-mode", no_argument, &draw_mode, 1},
259           {"no-txt", no_argument, &render_txt, 0},
260           {"no-stdout", no_argument, &render_stdout, 0},
261           {"pdf", no_argument, &render_pdf, 1},
262           {"csv", no_argument, &render_csv, 1},
263           {"output", required_argument, NULL, 'o'},
264           {"help", no_argument, NULL, OPT_HELP},
265           {NULL, 0, NULL, 0},
266         };
267
268       int c = getopt_long (argc, argv, "o:", options, NULL);
269       if (c == -1)
270         break;
271
272       switch (c)
273         {
274         case OPT_WIDTH:
275           width = atoi (optarg);
276           break;
277
278         case OPT_LENGTH:
279           length = atoi (optarg);
280           break;
281
282         case OPT_MIN_BREAK:
283           min_break = atoi (optarg);
284           break;
285
286         case OPT_EMPHASIS:
287           if (!strcmp (optarg, "bold"))
288             {
289               bold = true;
290               underline = false;
291             }
292           else if (!strcmp (optarg, "underline"))
293             {
294               bold = false;
295               underline = true;
296             }
297           else if (!strcmp (optarg, "none"))
298             {
299               bold = underline = false;
300             }
301           else
302             error (1, 0, "argument to --emphasis must be \"bold\" or "
303                    "\"underline\" or \"none\"");
304           break;
305
306         case OPT_BOX:
307           box = optarg;
308           break;
309
310         case 'o':
311           output_base = optarg;
312           break;
313
314         case OPT_HELP:
315           usage ();
316
317         case 0:
318           break;
319
320         case '?':
321           exit(EXIT_FAILURE);
322           break;
323
324         default:
325           NOT_REACHED ();
326         }
327
328     }
329
330   configure_drivers (width, length, min_break);
331
332   if (optind + 1 != argc)
333     error (1, 0, "exactly one non-option argument required; "
334            "use --help for help");
335   return argv[optind];
336 }
337
338 static void
339 usage (void)
340 {
341   printf ("%s, to test rendering of PSPP tables\n"
342           "usage: %s [OPTIONS] INPUT\n"
343           "\nOptions:\n"
344           "  --width=WIDTH   set page width in characters\n"
345           "  --length=LINE   set page length in lines\n",
346           program_name, program_name);
347   exit (EXIT_SUCCESS);
348 }
349
350 static void
351 replace_newlines (char *p)
352 {
353   char *q;
354
355   for (q = p; *p != '\0'; )
356     if (*p == '\\' && p[1] == 'n')
357       {
358         *q++ = '\n';
359         p += 2;
360       }
361     else
362       *q++ = *p++;
363   *q = '\0';
364 }
365
366 static struct table *
367 read_table (FILE *stream)
368 {
369   struct tab_table *tab;
370   char buffer[1024];
371   int input[6];
372   int n_input = 0;
373   int nr, nc, hl, hr, ht, hb;
374   int r, c;
375   size_t n_footnotes = 0;
376
377   if (fgets (buffer, sizeof buffer, stream) == NULL
378       || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
379                             &input[0], &input[1], &input[2],
380                             &input[3], &input[4], &input[5])) < 2)
381     error (1, 0, "syntax error reading row and column count");
382
383   nr = input[0];
384   nc = input[1];
385   hl = n_input >= 3 ? input[2] : 0;
386   hr = n_input >= 4 ? input[3] : 0;
387   ht = n_input >= 5 ? input[4] : 0;
388   hb = n_input >= 6 ? input[5] : 0;
389
390   tab = tab_create (nc, nr);
391   tab_headers (tab, hl, hr, ht, hb);
392   for (r = 0; r < nr; r++)
393     for (c = 0; c < nc; c++)
394       if (tab_cell_is_empty (tab, c, r))
395         {
396           unsigned int opt;
397           char *new_line;
398           char *text;
399           int rs, cs;
400
401           if (fgets (buffer, sizeof buffer, stream) == NULL)
402             error (1, 0, "unexpected end of input reading row %d, column %d",
403                    r, c);
404           new_line = strchr (buffer, '\n');
405           if (new_line != NULL)
406             *new_line = '\0';
407
408           text = buffer;
409           if (sscanf (text, "%d*%d", &rs, &cs) == 2)
410             {
411               while (*text != ' ' && *text != '\0')
412                 text++;
413               if (*text == ' ')
414                 text++;
415             }
416           else
417             {
418               rs = 1;
419               cs = 1;
420             }
421
422           opt = 0;
423           while (*text && strchr ("<>^,@()|", *text))
424             switch (*text++)
425               {
426               case '<':
427                 tab_vline (tab, TAL_1, c, r, r + rs - 1);
428                 break;
429
430               case '>':
431                 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
432                 break;
433
434               case '^':
435                 tab_hline (tab, TAL_1, c, c + cs - 1, r);
436                 break;
437
438               case ',':
439                 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
440                 break;
441
442               case '@':
443                 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
444                          c + cs - 1, r + rs - 1);
445                 break;
446
447               case '(':
448                 opt &= ~TAB_HALIGN;
449                 opt |= TAB_LEFT;
450                 break;
451
452               case ')':
453                 opt &= ~TAB_HALIGN;
454                 opt |= TAB_RIGHT;
455                 break;
456
457               case '|':
458                 opt &= ~TAB_HALIGN;
459                 opt |= TAB_CENTER;
460                 break;
461
462               default:
463                 NOT_REACHED ();
464               }
465
466           replace_newlines (text);
467
468           char *pos = text;
469           char *content;
470           int i;
471
472           for (i = 0; (content = strsep (&pos, "#")) != NULL; i++)
473             if (!i)
474               tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, opt,
475                               content);
476             else
477               {
478                 char marker[2] = { 'a' + n_footnotes, '\0' };
479                 struct footnote *f = tab_create_footnote (
480                   tab, n_footnotes, content, marker, NULL);
481                 tab_add_footnote (tab, c, r, f);
482                 n_footnotes++;
483               }
484         }
485
486   return &tab->table;
487 }
488
489 static void
490 draw (FILE *stream)
491 {
492   char buffer[1024];
493   int line = 0;
494
495   while (fgets (buffer, sizeof buffer, stream))
496     {
497       char text[sizeof buffer];
498       int length;
499       int emph;
500       int x, y;
501
502       line++;
503       if (strchr ("#\r\n", buffer[0]))
504         continue;
505
506       if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
507         ascii_test_write (ascii_driver, text, x, y, emph ? bold : false,
508                           emph ? underline : false);
509       else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
510         ascii_test_set_length (ascii_driver, y, length);
511       else
512         error (1, 0, "line %d has invalid format", line);
513     }
514   ascii_test_flush (ascii_driver);
515 }