1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
28 #include <data/file-name.h>
29 #include <data/settings.h>
30 #include <libpspp/misc.h>
31 #include <libpspp/str.h>
32 #include <output/htmlP.h>
33 #include <output/output.h>
40 #define _(msgid) gettext (msgid)
42 /* FIXME? Should the output configuration format be changed to
43 drivername:classname:devicetype:options, where devicetype is zero
44 or more of screen, printer, listing? */
46 /* FIXME: Have the reentrancy problems been solved? */
48 /* Where the output driver name came from. */
51 OUTP_S_COMMAND_LINE, /* Specified by the user. */
52 OUTP_S_INIT_FILE /* `default' or the init file. */
55 /* Names the output drivers to be used. */
58 char *name; /* Name of the output driver. */
59 int source; /* OUTP_S_* */
60 struct outp_names *next, *prev;
63 /* Defines an init file macro. */
68 struct outp_defn *next, *prev;
71 static struct outp_defn *outp_macros;
72 static struct outp_names *outp_configure_vec;
74 /* A list of driver classes. */
75 struct outp_driver_class_list
77 const struct outp_class *class;
78 struct outp_driver_class_list *next;
81 static struct outp_driver_class_list *outp_class_list;
82 static struct outp_driver *outp_driver_list;
87 /* A set of OUTP_DEV_* bits indicating the devices that are
89 static int disabled_devices;
91 static void destroy_driver (struct outp_driver *);
92 static void configure_driver (const struct substring, const struct substring,
93 const struct substring, const struct substring);
95 /* Add a class to the class list. */
97 add_class (const struct outp_class *class)
99 struct outp_driver_class_list *new_list = xmalloc (sizeof *new_list);
101 new_list->class = class;
103 if (!outp_class_list)
105 outp_class_list = new_list;
106 new_list->next = NULL;
110 new_list->next = outp_class_list;
111 outp_class_list = new_list;
115 /* Finds the outp_names in outp_configure_vec with name between BP and
117 static struct outp_names *
118 search_names (char *bp, char *ep)
120 struct outp_names *n;
122 for (n = outp_configure_vec; n; n = n->next)
123 if ((int) strlen (n->name) == ep - bp && !memcmp (n->name, bp, ep - bp))
128 /* Deletes outp_names NAME from outp_configure_vec. */
130 delete_name (struct outp_names * n)
134 n->prev->next = n->next;
136 n->next->prev = n->prev;
137 if (n == outp_configure_vec)
138 outp_configure_vec = n->next;
142 /* Adds the name between BP and EP exclusive to list
143 outp_configure_vec with source SOURCE. */
145 add_name (char *bp, char *ep, int source)
147 struct outp_names *n = xmalloc (sizeof *n);
148 n->name = xmalloc (ep - bp + 1);
149 memcpy (n->name, bp, ep - bp);
150 n->name[ep - bp] = 0;
152 n->next = outp_configure_vec;
154 if (outp_configure_vec)
155 outp_configure_vec->prev = n;
156 outp_configure_vec = n;
159 /* Checks that outp_configure_vec is empty, complains and clears
162 check_configure_vec (void)
164 struct outp_names *n;
166 for (n = outp_configure_vec; n; n = n->next)
167 if (n->source == OUTP_S_COMMAND_LINE)
168 error (0, 0, _("unknown output driver `%s'"), n->name);
170 error (0, 0, _("output driver `%s' referenced but never defined"),
172 outp_configure_clear ();
175 /* Searches outp_configure_vec for the name between BP and EP
176 exclusive. If found, it is deleted, then replaced by the names
177 given in EP+1, if any. */
179 expand_name (char *bp, char *ep)
181 struct outp_names *n = search_names (bp, ep);
189 while (isspace ((unsigned char) *bp))
192 while (*ep && !isspace ((unsigned char) *ep))
196 if (!search_names (bp, ep))
197 add_name (bp, ep, OUTP_S_INIT_FILE);
202 /* Looks for a macro with key KEY, and returns the corresponding value
203 if found, or NULL if not. */
205 find_defn_value (const char *key)
207 static char buf[INT_STRLEN_BOUND (int) + 1];
210 for (d = outp_macros; d; d = d->next)
211 if (!strcmp (key, d->key))
212 return ds_cstr (&d->value);
213 if (!strcmp (key, "viewwidth"))
215 sprintf (buf, "%d", settings_get_viewwidth ());
218 else if (!strcmp (key, "viewlength"))
220 sprintf (buf, "%d", settings_get_viewlength ());
227 /* Initializes global variables. */
231 extern struct outp_class ascii_class;
232 extern struct outp_class postscript_class;
234 extern struct outp_class cairo_class;
237 char def[] = "default";
239 add_class (&html_class);
240 add_class (&postscript_class);
241 add_class (&ascii_class);
243 add_class (&cairo_class);
246 add_name (def, &def[strlen (def)], OUTP_S_INIT_FILE);
249 /* Deletes all the output macros. */
253 struct outp_defn *d, *next;
255 for (d = outp_macros; d; d = next)
259 ds_destroy (&d->value);
265 init_default_drivers (void)
267 error (0, 0, _("using default output driver configuration"));
268 configure_driver (ss_cstr ("list"),
271 ss_cstr ("length=66 width=79 output-file=\"pspp.list\""));
274 /* Reads the initialization file; initializes
277 outp_read_devices (void)
287 init_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_INIT_FILE",
289 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
292 ds_init_empty (&line);
296 error (0, 0, _("cannot find output initialization file "
297 "(use `-vv' to view search path)"));
301 f = fopen (init_fn, "r");
304 error (0, errno, _("cannot open \"%s\""), init_fn);
313 if (!ds_read_config_line (&line, &line_number, f))
316 error (0, errno, _("reading \"%s\""), init_fn);
319 for (cp = ds_cstr (&line); isspace ((unsigned char) *cp); cp++);
320 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
321 outp_configure_macro (&cp[7]);
325 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
327 expand_name (cp, ep);
330 struct outp_names *n = search_names (cp, ep);
333 outp_configure_driver_line (ds_ss (&line));
338 error_at_line (0, 0, init_fn, line_number, _("syntax error"));
343 check_configure_vec ();
346 if (f && -1 == fclose (f))
347 error (0, errno, _("error closing \"%s\""), init_fn);
354 if (outp_driver_list == NULL)
355 error (0, 0, _("no active output drivers"));
358 error (0, 0, _("error reading device definition file"));
360 if (!result || outp_driver_list == NULL)
361 init_default_drivers ();
364 /* Clear the list of drivers to configure. */
366 outp_configure_clear (void)
368 struct outp_names *n, *next;
370 for (n = outp_configure_vec; n; n = next)
376 outp_configure_vec = NULL;
379 /* Adds the name BP to the list of drivers to configure into
382 outp_configure_add (char *bp)
384 char *ep = &bp[strlen (bp)];
385 if (!search_names (bp, ep))
386 add_name (bp, ep, OUTP_S_COMMAND_LINE);
389 /* Defines one configuration macro based on the text in BP, which
390 should be of the form `KEY=VALUE'. */
392 outp_configure_macro (char *bp)
397 while (isspace ((unsigned char) *bp))
400 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
403 d = xmalloc (sizeof *d);
404 d->key = xmalloc (ep - bp + 1);
405 memcpy (d->key, bp, ep - bp);
408 /* Earlier definitions for a particular KEY override later ones. */
409 if (find_defn_value (d->key))
418 while (isspace ((unsigned char) *ep))
421 ds_init_cstr (&d->value, ep);
422 fn_interp_vars (ds_ss (&d->value), find_defn_value, &d->value);
423 d->next = outp_macros;
426 outp_macros->prev = d;
430 /* Destroys all the drivers in driver list *DL and sets *DL to
433 destroy_list (struct outp_driver ** dl)
435 struct outp_driver *d, *next;
437 for (d = *dl; d; d = next)
446 /* Closes all the output drivers. */
450 struct outp_driver_class_list *n = outp_class_list ;
451 outp_configure_clear ();
452 destroy_list (&outp_driver_list);
456 struct outp_driver_class_list *next = n->next;
460 outp_class_list = NULL;
465 free (outp_subtitle);
466 outp_subtitle = NULL;
469 /* Display on stdout a list of all registered driver classes. */
471 outp_list_classes (void)
473 int width = settings_get_viewwidth ();
474 struct outp_driver_class_list *c;
476 printf (_("Driver classes:\n\t"));
478 for (c = outp_class_list; c; c = c->next)
480 if ((int) strlen (c->class->name) + 1 > width)
483 width = settings_get_viewwidth () - 8;
487 fputs (c->class->name, stdout);
492 /* Obtains a token from S and advances its position. Errors are
493 reported against the given DRIVER_NAME.
494 The token is stored in TOKEN. Returns true if successful,
495 false on syntax error.
497 Caller is responsible for skipping leading spaces. */
499 get_option_token (struct substring *s, const char *driver_name,
500 struct string *token)
508 error (0, 0, _("syntax error parsing options for \"%s\" driver"),
512 else if (c == '\'' || c == '"')
524 _("reached end of options inside quoted string "
525 "parsing options for \"%s\" driver"),
530 ds_put_char (token, c);
577 while (ss_first (*s) >= '0' && ss_first (*s) <= '7')
578 out = out * 8 + (ss_get_char (s) - '0');
583 while (isxdigit (ss_first (*s)))
590 out += tolower (c) - 'a' + 10;
594 error (0, 0, _("syntax error in string constant "
595 "parsing options for \"%s\" driver"),
599 ds_put_char (token, out);
607 ds_put_char (token, c);
610 if (c == EOF || c == '=' || isspace (c))
620 outp_parse_options (struct substring options,
621 bool (*callback) (struct outp_driver *, const char *key,
622 const struct string *value),
623 struct outp_driver *driver)
625 struct string key = DS_EMPTY_INITIALIZER;
626 struct string value = DS_EMPTY_INITIALIZER;
627 struct substring left = options;
632 ss_ltrim (&left, ss_cstr (CC_SPACES));
633 if (ss_is_empty (left))
636 if (!get_option_token (&left, driver->name, &key))
639 ss_ltrim (&left, ss_cstr (CC_SPACES));
640 if (!ss_match_char (&left, '='))
642 error (0, 0, _("syntax error expecting `=' "
643 "parsing options for driver \"%s\""),
648 ss_ltrim (&left, ss_cstr (CC_SPACES));
649 if (!get_option_token (&left, driver->name, &value))
652 ok = callback (driver, ds_cstr (&key), &value);
662 /* Find the driver in outp_driver_list with name NAME. */
663 static struct outp_driver *
664 find_driver (char *name)
666 struct outp_driver *d;
668 for (d = outp_driver_list; d; d = d->next)
669 if (!strcmp (d->name, name))
674 /* Adds a driver to outp_driver_list pursuant to the
675 specification provided. */
677 configure_driver (struct substring driver_name, struct substring class_name,
678 struct substring device_type, struct substring options)
680 struct outp_driver *d, *iter;
681 struct outp_driver_class_list *c;
683 struct substring token;
688 for (c = outp_class_list; c; c = c->next)
689 if (!ss_compare (ss_cstr (c->class->name), class_name))
693 error (0, 0, _("unknown output driver class `%.*s'"),
694 (int) ss_length (class_name), ss_data (class_name));
698 /* Parse device type. */
700 while (ss_tokenize (device_type, ss_cstr (CC_SPACES), &save_idx, &token))
701 if (!ss_compare (token, ss_cstr ("listing")))
702 device |= OUTP_DEV_LISTING;
703 else if (!ss_compare (token, ss_cstr ("screen")))
704 device |= OUTP_DEV_SCREEN;
705 else if (!ss_compare (token, ss_cstr ("printer")))
706 device |= OUTP_DEV_PRINTER;
708 error (0, 0, _("unknown device type `%.*s'"),
709 (int) ss_length (token), ss_data (token));
711 /* Open the device. */
712 d = xmalloc (sizeof *d);
713 d->next = d->prev = NULL;
715 d->name = ss_xstrdup (driver_name);
716 d->page_open = false;
718 d->cp_x = d->cp_y = 0;
723 if (!d->class->open_driver (d, options))
725 error (0, 0, _("cannot initialize output driver `%s' of class `%s'"),
726 d->name, d->class->name);
732 /* Find like-named driver and delete. */
733 iter = find_driver (d->name);
735 destroy_driver (iter);
738 d->next = outp_driver_list;
740 if (outp_driver_list != NULL)
741 outp_driver_list->prev = d;
742 outp_driver_list = d;
745 /* String LINE is in format:
746 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
747 Adds a driver to outp_driver_list pursuant to the specification
750 outp_configure_driver_line (struct substring line_)
752 struct string line = DS_EMPTY_INITIALIZER;
753 struct substring tokens[4];
757 fn_interp_vars (line_, find_defn_value, &line);
760 for (i = 0; i < 4; i++)
762 struct substring *token = &tokens[i];
763 ds_separate (&line, ss_cstr (i < 3 ? ":" : ""), &save_idx, token);
764 ss_trim (token, ss_cstr (CC_SPACES));
767 if (!ss_is_empty (tokens[0]) && !ss_is_empty (tokens[1]))
768 configure_driver (tokens[0], tokens[1], tokens[2], tokens[3]);
771 _("driver definition line missing driver name or class name"));
776 /* Destroys output driver D. */
778 destroy_driver (struct outp_driver *d)
783 struct outp_driver_class_list *c;
785 d->class->close_driver (d);
787 for (c = outp_class_list; c; c = c->next)
788 if (c->class == d->class)
794 /* Remove this driver from the global driver list. */
796 d->prev->next = d->next;
798 d->next->prev = d->prev;
799 if (d == outp_driver_list)
800 outp_driver_list = d->next;
803 /* Tries to match S as one of the keywords in TAB, with
804 corresponding information structure INFO. Returns category
805 code and stores subcategory in *SUBCAT on success. Returns -1
808 outp_match_keyword (const char *s, const struct outp_option *tab, int *subcat)
810 for (; tab->keyword != NULL; tab++)
811 if (!strcmp (s, tab->keyword))
813 *subcat = tab->subcat;
819 /* Parses UNIT as a dimensional unit. Returns the multiplicative
820 factor needed to change a quantity measured in that unit into
821 1/72000" units. If UNIT is empty, it is treated as
822 millimeters. If the unit is unrecognized, returns 0. */
824 parse_unit (const char *unit)
832 static const struct unit units[] =
835 {"pc", 72000 / 72 * 12.0},
837 {"cm", 72000 / 2.54},
838 {"mm", 72000 / 25.4},
842 const struct unit *p;
844 unit += strspn (unit, CC_SPACES);
845 for (p = units; p < units + sizeof units / sizeof *units; p++)
846 if (!strcasecmp (unit, p->name))
851 /* Determines the size of a dimensional measurement and returns
852 the size in units of 1/72000". Units are assumed to be
853 millimeters unless otherwise specified. Returns 0 on
856 outp_evaluate_dimension (const char *dimen)
862 raw = strtod (dimen, &tail);
867 factor = parse_unit (tail);
874 error (0, 0, _("`%s' is not a valid length."), dimen);
878 /* Stores the dimensions in 1/72000" units of paper identified by
879 SIZE, which is of form `HORZ x VERT [UNIT]' where HORZ and
880 VERT are numbers and UNIT is an optional unit of measurement,
881 into *H and *V. Return true on success. */
883 parse_paper_size (const char *size, int *h, int *v)
885 double raw_h, raw_v, factor;
889 raw_h = strtod (size, &tail);
894 tail += strspn (tail, CC_SPACES "x,");
897 raw_v = strtod (tail, &tail);
902 factor = parse_unit (tail);
906 *h = raw_h * factor + .5;
907 *v = raw_v * factor + .5;
912 get_standard_paper_size (struct substring name, int *h, int *v)
914 static const char *sizes[][2] =
916 {"a0", "841 x 1189 mm"},
917 {"a1", "594 x 841 mm"},
918 {"a2", "420 x 594 mm"},
919 {"a3", "297 x 420 mm"},
920 {"a4", "210 x 297 mm"},
921 {"a5", "148 x 210 mm"},
922 {"b5", "176 x 250 mm"},
923 {"a6", "105 x 148 mm"},
924 {"a7", "74 x 105 mm"},
925 {"a8", "52 x 74 mm"},
926 {"a9", "37 x 52 mm"},
927 {"a10", "26 x 37 mm"},
928 {"b0", "1000 x 1414 mm"},
929 {"b1", "707 x 1000 mm"},
930 {"b2", "500 x 707 mm"},
931 {"b3", "353 x 500 mm"},
932 {"b4", "250 x 353 mm"},
933 {"letter", "612 x 792 pt"},
934 {"legal", "612 x 1008 pt"},
935 {"executive", "522 x 756 pt"},
936 {"note", "612 x 792 pt"},
937 {"11x17", "792 x 1224 pt"},
938 {"tabloid", "792 x 1224 pt"},
939 {"statement", "396 x 612 pt"},
940 {"halfletter", "396 x 612 pt"},
941 {"halfexecutive", "378 x 522 pt"},
942 {"folio", "612 x 936 pt"},
943 {"quarto", "610 x 780 pt"},
944 {"ledger", "1224 x 792 pt"},
945 {"archA", "648 x 864 pt"},
946 {"archB", "864 x 1296 pt"},
947 {"archC", "1296 x 1728 pt"},
948 {"archD", "1728 x 2592 pt"},
949 {"archE", "2592 x 3456 pt"},
950 {"flsa", "612 x 936 pt"},
951 {"flse", "612 x 936 pt"},
952 {"csheet", "1224 x 1584 pt"},
953 {"dsheet", "1584 x 2448 pt"},
954 {"esheet", "2448 x 3168 pt"},
959 for (i = 0; i < sizeof sizes / sizeof *sizes; i++)
960 if (ss_equals_case (ss_cstr (sizes[i][0]), name))
962 bool ok = parse_paper_size (sizes[i][1], h, v);
966 error (0, 0, _("unknown paper type `%.*s'"),
967 (int) ss_length (name), ss_data (name));
971 /* Reads file FILE_NAME to find a paper size. Stores the
972 dimensions, in 1/72000" units, into *H and *V. Returns true
973 on success, false on failure. */
975 read_paper_conf (const char *file_name, int *h, int *v)
977 struct string line = DS_EMPTY_INITIALIZER;
981 file = fopen (file_name, "r");
984 error (0, errno, _("error opening \"%s\""), file_name);
990 struct substring name;
992 if (!ds_read_config_line (&line, &line_number, file))
995 error (0, errno, _("error reading \"%s\""), file_name);
999 name = ds_ss (&line);
1000 ss_trim (&name, ss_cstr (CC_SPACES));
1001 if (!ss_is_empty (name))
1003 bool ok = get_standard_paper_size (name, h, v);
1012 error (0, 0, _("paper size file \"%s\" does not state a paper size"),
1017 /* The user didn't specify a paper size, so let's choose a
1018 default based on his environment. Stores the
1019 dimensions, in 1/72000" units, into *H and *V. Returns true
1020 on success, false on failure. */
1022 get_default_paper_size (int *h, int *v)
1024 /* libpaper in Debian (and other distributions?) allows the
1025 paper size to be specified in $PAPERSIZE or in a file
1026 specified in $PAPERCONF. */
1027 if (getenv ("PAPERSIZE") != NULL)
1028 return get_standard_paper_size (ss_cstr (getenv ("PAPERSIZE")), h, v);
1029 if (getenv ("PAPERCONF") != NULL)
1030 return read_paper_conf (getenv ("PAPERCONF"), h, v);
1033 /* LC_PAPER is a non-standard glibc extension. */
1034 *h = (int) nl_langinfo(_NL_PAPER_WIDTH) * (72000 / 25.4);
1035 *v = (int) nl_langinfo(_NL_PAPER_HEIGHT) * (72000 / 25.4);
1036 if (*h > 0 && *v > 0)
1040 /* libpaper defaults to /etc/papersize. */
1041 if (fn_exists ("/etc/papersize"))
1042 return read_paper_conf ("/etc/papersize", h, v);
1044 /* Can't find a default. */
1048 /* Stores the dimensions, in 1/72000" units, of paper identified
1049 by SIZE into *H and *V. SIZE can be the name of a kind of
1050 paper ("a4", "letter", ...) or a pair of dimensions
1051 ("210x297", "8.5x11in", ...). Returns true on success, false
1052 on failure. On failure, *H and *V are set for A4 paper. */
1054 outp_get_paper_size (const char *size, int *h, int *v)
1060 ss_trim (&s, ss_cstr (CC_SPACES));
1062 if (ss_is_empty (s))
1064 /* Treat empty string as default paper size. */
1065 ok = get_default_paper_size (h, v);
1067 else if (isdigit (ss_first (s)))
1069 /* Treat string that starts with digit as explicit size. */
1070 ok = parse_paper_size (size, h, v);
1072 error (0, 0, _("syntax error in paper size `%s'"), size);
1076 /* Check against standard paper sizes. */
1077 ok = get_standard_paper_size (s, h, v);
1080 /* Default to A4 on error. */
1083 *h = 210 * (72000 / 25.4);
1084 *v = 297 * (72000 / 25.4);
1089 /* If D is NULL, returns the first enabled driver if any, NULL if
1090 none. Otherwise D must be the last driver returned by this
1091 function, in which case the next enabled driver is returned or NULL
1092 if that was the last. */
1093 struct outp_driver *
1094 outp_drivers (struct outp_driver *d)
1099 d = outp_driver_list;
1104 || (d->device == 0 || (d->device & disabled_devices) != d->device))
1111 /* Enables (if ENABLE is true) or disables (if ENABLE is false) the
1112 device(s) given in mask DEVICE. */
1114 outp_enable_device (bool enable, int device)
1117 disabled_devices &= ~device;
1119 disabled_devices |= device;
1122 /* Opens a page on driver D (if one is not open). */
1124 outp_open_page (struct outp_driver *d)
1128 d->cp_x = d->cp_y = 0;
1130 d->page_open = true;
1131 if (d->class->open_page != NULL)
1132 d->class->open_page (d);
1136 /* Closes the page on driver D (if one is open). */
1138 outp_close_page (struct outp_driver *d)
1142 if (d->class->close_page != NULL)
1143 d->class->close_page (d);
1144 d->page_open = false;
1148 /* Ejects the page on device D, if a page is open and non-blank,
1149 and opens a new page. */
1151 outp_eject_page (struct outp_driver *d)
1153 if (d->page_open && d->cp_y != 0)
1154 outp_close_page (d);
1158 /* Flushes output to screen devices, so that the user can see
1159 output that doesn't fill up an entire page. */
1161 outp_flush (struct outp_driver *d)
1163 if (d->device & OUTP_DEV_SCREEN && d->class->flush != NULL)
1165 outp_close_page (d);
1166 d->class->flush (d);
1170 /* Returns the width of string S, in device units, when output on
1173 outp_string_width (struct outp_driver *d, const char *s, enum outp_font font)
1175 struct outp_text text;
1179 text.justification = OUTP_LEFT;
1180 text.string = ss_cstr (s);
1181 text.h = text.v = INT_MAX;
1182 d->class->text_metrics (d, &text, &width, NULL);