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