1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007 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 char def[] = "default";
236 add_class (&html_class);
237 add_class (&postscript_class);
238 add_class (&ascii_class);
240 add_name (def, &def[strlen (def)], OUTP_S_INIT_FILE);
243 /* Deletes all the output macros. */
247 struct outp_defn *d, *next;
249 for (d = outp_macros; d; d = next)
253 ds_destroy (&d->value);
259 init_default_drivers (void)
261 error (0, 0, _("using default output driver configuration"));
262 configure_driver (ss_cstr ("list"),
265 ss_cstr ("length=66 width=79 output-file=\"pspp.list\""));
268 /* Reads the initialization file; initializes
271 outp_read_devices (void)
281 init_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_INIT_FILE",
283 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
286 ds_init_empty (&line);
290 error (0, 0, _("cannot find output initialization file "
291 "(use `-vv' to view search path)"));
295 f = fopen (init_fn, "r");
298 error (0, errno, _("cannot open \"%s\""), init_fn);
307 if (!ds_read_config_line (&line, &line_number, f))
310 error (0, errno, _("reading \"%s\""), init_fn);
313 for (cp = ds_cstr (&line); isspace ((unsigned char) *cp); cp++);
314 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
315 outp_configure_macro (&cp[7]);
319 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
321 expand_name (cp, ep);
324 struct outp_names *n = search_names (cp, ep);
327 outp_configure_driver_line (ds_ss (&line));
332 error_at_line (0, 0, init_fn, line_number, _("syntax error"));
337 check_configure_vec ();
340 if (f && -1 == fclose (f))
341 error (0, errno, _("error closing \"%s\""), init_fn);
348 if (outp_driver_list == NULL)
349 error (0, 0, _("no active output drivers"));
352 error (0, 0, _("error reading device definition file"));
354 if (!result || outp_driver_list == NULL)
355 init_default_drivers ();
358 /* Clear the list of drivers to configure. */
360 outp_configure_clear (void)
362 struct outp_names *n, *next;
364 for (n = outp_configure_vec; n; n = next)
370 outp_configure_vec = NULL;
373 /* Adds the name BP to the list of drivers to configure into
376 outp_configure_add (char *bp)
378 char *ep = &bp[strlen (bp)];
379 if (!search_names (bp, ep))
380 add_name (bp, ep, OUTP_S_COMMAND_LINE);
383 /* Defines one configuration macro based on the text in BP, which
384 should be of the form `KEY=VALUE'. */
386 outp_configure_macro (char *bp)
391 while (isspace ((unsigned char) *bp))
394 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
397 d = xmalloc (sizeof *d);
398 d->key = xmalloc (ep - bp + 1);
399 memcpy (d->key, bp, ep - bp);
402 /* Earlier definitions for a particular KEY override later ones. */
403 if (find_defn_value (d->key))
412 while (isspace ((unsigned char) *ep))
415 ds_init_cstr (&d->value, ep);
416 fn_interp_vars (ds_ss (&d->value), find_defn_value, &d->value);
417 d->next = outp_macros;
420 outp_macros->prev = d;
424 /* Destroys all the drivers in driver list *DL and sets *DL to
427 destroy_list (struct outp_driver ** dl)
429 struct outp_driver *d, *next;
431 for (d = *dl; d; d = next)
440 /* Closes all the output drivers. */
444 struct outp_driver_class_list *n = outp_class_list ;
445 outp_configure_clear ();
446 destroy_list (&outp_driver_list);
450 struct outp_driver_class_list *next = n->next;
454 outp_class_list = NULL;
459 free (outp_subtitle);
460 outp_subtitle = NULL;
463 /* Display on stdout a list of all registered driver classes. */
465 outp_list_classes (void)
467 int width = settings_get_viewwidth ();
468 struct outp_driver_class_list *c;
470 printf (_("Driver classes:\n\t"));
472 for (c = outp_class_list; c; c = c->next)
474 if ((int) strlen (c->class->name) + 1 > width)
477 width = settings_get_viewwidth () - 8;
481 fputs (c->class->name, stdout);
486 /* Obtains a token from S and advances its position. Errors are
487 reported against the given DRIVER_NAME.
488 The token is stored in TOKEN. Returns true if successful,
489 false on syntax error.
491 Caller is responsible for skipping leading spaces. */
493 get_option_token (struct substring *s, const char *driver_name,
494 struct string *token)
502 error (0, 0, _("syntax error parsing options for \"%s\" driver"),
506 else if (c == '\'' || c == '"')
518 _("reached end of options inside quoted string "
519 "parsing options for \"%s\" driver"),
524 ds_put_char (token, c);
571 while (ss_first (*s) >= '0' && ss_first (*s) <= '7')
572 out = out * 8 + (ss_get_char (s) - '0');
577 while (isxdigit (ss_first (*s)))
584 out += tolower (c) - 'a' + 10;
588 error (0, 0, _("syntax error in string constant "
589 "parsing options for \"%s\" driver"),
593 ds_put_char (token, out);
601 ds_put_char (token, c);
604 if (c == EOF || c == '=' || isspace (c))
614 outp_parse_options (struct substring options,
615 bool (*callback) (struct outp_driver *, const char *key,
616 const struct string *value),
617 struct outp_driver *driver)
619 struct string key = DS_EMPTY_INITIALIZER;
620 struct string value = DS_EMPTY_INITIALIZER;
621 struct substring left = options;
626 ss_ltrim (&left, ss_cstr (CC_SPACES));
627 if (ss_is_empty (left))
630 if (!get_option_token (&left, driver->name, &key))
633 ss_ltrim (&left, ss_cstr (CC_SPACES));
634 if (!ss_match_char (&left, '='))
636 error (0, 0, _("syntax error expecting `=' "
637 "parsing options for driver \"%s\""),
642 ss_ltrim (&left, ss_cstr (CC_SPACES));
643 if (!get_option_token (&left, driver->name, &value))
646 ok = callback (driver, ds_cstr (&key), &value);
656 /* Find the driver in outp_driver_list with name NAME. */
657 static struct outp_driver *
658 find_driver (char *name)
660 struct outp_driver *d;
662 for (d = outp_driver_list; d; d = d->next)
663 if (!strcmp (d->name, name))
668 /* Adds a driver to outp_driver_list pursuant to the
669 specification provided. */
671 configure_driver (struct substring driver_name, struct substring class_name,
672 struct substring device_type, struct substring options)
674 struct outp_driver *d, *iter;
675 struct outp_driver_class_list *c;
677 struct substring token;
682 for (c = outp_class_list; c; c = c->next)
683 if (!ss_compare (ss_cstr (c->class->name), class_name))
687 error (0, 0, _("unknown output driver class `%.*s'"),
688 (int) ss_length (class_name), ss_data (class_name));
692 /* Parse device type. */
694 while (ss_tokenize (device_type, ss_cstr (CC_SPACES), &save_idx, &token))
695 if (!ss_compare (token, ss_cstr ("listing")))
696 device |= OUTP_DEV_LISTING;
697 else if (!ss_compare (token, ss_cstr ("screen")))
698 device |= OUTP_DEV_SCREEN;
699 else if (!ss_compare (token, ss_cstr ("printer")))
700 device |= OUTP_DEV_PRINTER;
702 error (0, 0, _("unknown device type `%.*s'"),
703 (int) ss_length (token), ss_data (token));
705 /* Open the device. */
706 d = xmalloc (sizeof *d);
707 d->next = d->prev = NULL;
709 d->name = ss_xstrdup (driver_name);
710 d->page_open = false;
712 d->cp_x = d->cp_y = 0;
717 if (!d->class->open_driver (d, options))
719 error (0, 0, _("cannot initialize output driver `%s' of class `%s'"),
720 d->name, d->class->name);
726 /* Find like-named driver and delete. */
727 iter = find_driver (d->name);
729 destroy_driver (iter);
732 d->next = outp_driver_list;
734 if (outp_driver_list != NULL)
735 outp_driver_list->prev = d;
736 outp_driver_list = d;
739 /* String LINE is in format:
740 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
741 Adds a driver to outp_driver_list pursuant to the specification
744 outp_configure_driver_line (struct substring line_)
746 struct string line = DS_EMPTY_INITIALIZER;
747 struct substring tokens[4];
751 fn_interp_vars (line_, find_defn_value, &line);
754 for (i = 0; i < 4; i++)
756 struct substring *token = &tokens[i];
757 ds_separate (&line, ss_cstr (i < 3 ? ":" : ""), &save_idx, token);
758 ss_trim (token, ss_cstr (CC_SPACES));
761 if (!ss_is_empty (tokens[0]) && !ss_is_empty (tokens[1]))
762 configure_driver (tokens[0], tokens[1], tokens[2], tokens[3]);
765 _("driver definition line missing driver name or class name"));
770 /* Destroys output driver D. */
772 destroy_driver (struct outp_driver *d)
777 struct outp_driver_class_list *c;
779 d->class->close_driver (d);
781 for (c = outp_class_list; c; c = c->next)
782 if (c->class == d->class)
788 /* Remove this driver from the global driver list. */
790 d->prev->next = d->next;
792 d->next->prev = d->prev;
793 if (d == outp_driver_list)
794 outp_driver_list = d->next;
797 /* Tries to match S as one of the keywords in TAB, with
798 corresponding information structure INFO. Returns category
799 code and stores subcategory in *SUBCAT on success. Returns -1
802 outp_match_keyword (const char *s, const struct outp_option *tab, int *subcat)
804 for (; tab->keyword != NULL; tab++)
805 if (!strcmp (s, tab->keyword))
807 *subcat = tab->subcat;
813 /* Parses UNIT as a dimensional unit. Returns the multiplicative
814 factor needed to change a quantity measured in that unit into
815 1/72000" units. If UNIT is empty, it is treated as
816 millimeters. If the unit is unrecognized, returns 0. */
818 parse_unit (const char *unit)
826 static const struct unit units[] =
829 {"pc", 72000 / 72 * 12.0},
831 {"cm", 72000 / 2.54},
832 {"mm", 72000 / 25.4},
836 const struct unit *p;
838 unit += strspn (unit, CC_SPACES);
839 for (p = units; p < units + sizeof units / sizeof *units; p++)
840 if (!strcasecmp (unit, p->name))
845 /* Determines the size of a dimensional measurement and returns
846 the size in units of 1/72000". Units are assumed to be
847 millimeters unless otherwise specified. Returns 0 on
850 outp_evaluate_dimension (const char *dimen)
856 raw = strtod (dimen, &tail);
861 factor = parse_unit (tail);
868 error (0, 0, _("`%s' is not a valid length."), dimen);
872 /* Stores the dimensions in 1/72000" units of paper identified by
873 SIZE, which is of form `HORZ x VERT [UNIT]' where HORZ and
874 VERT are numbers and UNIT is an optional unit of measurement,
875 into *H and *V. Return true on success. */
877 parse_paper_size (const char *size, int *h, int *v)
879 double raw_h, raw_v, factor;
883 raw_h = strtod (size, &tail);
888 tail += strspn (tail, CC_SPACES "x,");
891 raw_v = strtod (tail, &tail);
896 factor = parse_unit (tail);
900 *h = raw_h * factor + .5;
901 *v = raw_v * factor + .5;
906 get_standard_paper_size (struct substring name, int *h, int *v)
908 static const char *sizes[][2] =
910 {"a0", "841 x 1189 mm"},
911 {"a1", "594 x 841 mm"},
912 {"a2", "420 x 594 mm"},
913 {"a3", "297 x 420 mm"},
914 {"a4", "210 x 297 mm"},
915 {"a5", "148 x 210 mm"},
916 {"b5", "176 x 250 mm"},
917 {"a6", "105 x 148 mm"},
918 {"a7", "74 x 105 mm"},
919 {"a8", "52 x 74 mm"},
920 {"a9", "37 x 52 mm"},
921 {"a10", "26 x 37 mm"},
922 {"b0", "1000 x 1414 mm"},
923 {"b1", "707 x 1000 mm"},
924 {"b2", "500 x 707 mm"},
925 {"b3", "353 x 500 mm"},
926 {"b4", "250 x 353 mm"},
927 {"letter", "612 x 792 pt"},
928 {"legal", "612 x 1008 pt"},
929 {"executive", "522 x 756 pt"},
930 {"note", "612 x 792 pt"},
931 {"11x17", "792 x 1224 pt"},
932 {"tabloid", "792 x 1224 pt"},
933 {"statement", "396 x 612 pt"},
934 {"halfletter", "396 x 612 pt"},
935 {"halfexecutive", "378 x 522 pt"},
936 {"folio", "612 x 936 pt"},
937 {"quarto", "610 x 780 pt"},
938 {"ledger", "1224 x 792 pt"},
939 {"archA", "648 x 864 pt"},
940 {"archB", "864 x 1296 pt"},
941 {"archC", "1296 x 1728 pt"},
942 {"archD", "1728 x 2592 pt"},
943 {"archE", "2592 x 3456 pt"},
944 {"flsa", "612 x 936 pt"},
945 {"flse", "612 x 936 pt"},
946 {"csheet", "1224 x 1584 pt"},
947 {"dsheet", "1584 x 2448 pt"},
948 {"esheet", "2448 x 3168 pt"},
953 for (i = 0; i < sizeof sizes / sizeof *sizes; i++)
954 if (ss_equals_case (ss_cstr (sizes[i][0]), name))
956 bool ok = parse_paper_size (sizes[i][1], h, v);
960 error (0, 0, _("unknown paper type `%.*s'"),
961 (int) ss_length (name), ss_data (name));
965 /* Reads file FILE_NAME to find a paper size. Stores the
966 dimensions, in 1/72000" units, into *H and *V. Returns true
967 on success, false on failure. */
969 read_paper_conf (const char *file_name, int *h, int *v)
971 struct string line = DS_EMPTY_INITIALIZER;
975 file = fopen (file_name, "r");
978 error (0, errno, _("error opening \"%s\""), file_name);
984 struct substring name;
986 if (!ds_read_config_line (&line, &line_number, file))
989 error (0, errno, _("error reading \"%s\""), file_name);
993 name = ds_ss (&line);
994 ss_trim (&name, ss_cstr (CC_SPACES));
995 if (!ss_is_empty (name))
997 bool ok = get_standard_paper_size (name, h, v);
1006 error (0, 0, _("paper size file \"%s\" does not state a paper size"),
1011 /* The user didn't specify a paper size, so let's choose a
1012 default based on his environment. Stores the
1013 dimensions, in 1/72000" units, into *H and *V. Returns true
1014 on success, false on failure. */
1016 get_default_paper_size (int *h, int *v)
1018 /* libpaper in Debian (and other distributions?) allows the
1019 paper size to be specified in $PAPERSIZE or in a file
1020 specified in $PAPERCONF. */
1021 if (getenv ("PAPERSIZE") != NULL)
1022 return get_standard_paper_size (ss_cstr (getenv ("PAPERSIZE")), h, v);
1023 if (getenv ("PAPERCONF") != NULL)
1024 return read_paper_conf (getenv ("PAPERCONF"), h, v);
1027 /* LC_PAPER is a non-standard glibc extension. */
1028 *h = (int) nl_langinfo(_NL_PAPER_WIDTH) * (72000 / 25.4);
1029 *v = (int) nl_langinfo(_NL_PAPER_HEIGHT) * (72000 / 25.4);
1030 if (*h > 0 && *v > 0)
1034 /* libpaper defaults to /etc/papersize. */
1035 if (fn_exists ("/etc/papersize"))
1036 return read_paper_conf ("/etc/papersize", h, v);
1038 /* Can't find a default. */
1042 /* Stores the dimensions, in 1/72000" units, of paper identified
1043 by SIZE into *H and *V. SIZE can be the name of a kind of
1044 paper ("a4", "letter", ...) or a pair of dimensions
1045 ("210x297", "8.5x11in", ...). Returns true on success, false
1046 on failure. On failure, *H and *V are set for A4 paper. */
1048 outp_get_paper_size (const char *size, int *h, int *v)
1054 ss_trim (&s, ss_cstr (CC_SPACES));
1056 if (ss_is_empty (s))
1058 /* Treat empty string as default paper size. */
1059 ok = get_default_paper_size (h, v);
1061 else if (isdigit (ss_first (s)))
1063 /* Treat string that starts with digit as explicit size. */
1064 ok = parse_paper_size (size, h, v);
1066 error (0, 0, _("syntax error in paper size `%s'"), size);
1070 /* Check against standard paper sizes. */
1071 ok = get_standard_paper_size (s, h, v);
1074 /* Default to A4 on error. */
1077 *h = 210 * (72000 / 25.4);
1078 *v = 297 * (72000 / 25.4);
1083 /* If D is NULL, returns the first enabled driver if any, NULL if
1084 none. Otherwise D must be the last driver returned by this
1085 function, in which case the next enabled driver is returned or NULL
1086 if that was the last. */
1087 struct outp_driver *
1088 outp_drivers (struct outp_driver *d)
1093 d = outp_driver_list;
1098 || (d->device == 0 || (d->device & disabled_devices) != d->device))
1105 /* Enables (if ENABLE is true) or disables (if ENABLE is false) the
1106 device(s) given in mask DEVICE. */
1108 outp_enable_device (bool enable, int device)
1111 disabled_devices &= ~device;
1113 disabled_devices |= device;
1116 /* Opens a page on driver D (if one is not open). */
1118 outp_open_page (struct outp_driver *d)
1122 d->cp_x = d->cp_y = 0;
1124 d->page_open = true;
1125 if (d->class->open_page != NULL)
1126 d->class->open_page (d);
1130 /* Closes the page on driver D (if one is open). */
1132 outp_close_page (struct outp_driver *d)
1136 if (d->class->close_page != NULL)
1137 d->class->close_page (d);
1138 d->page_open = false;
1142 /* Ejects the page on device D, if a page is open and non-blank,
1143 and opens a new page. */
1145 outp_eject_page (struct outp_driver *d)
1147 if (d->page_open && d->cp_y != 0)
1148 outp_close_page (d);
1152 /* Flushes output to screen devices, so that the user can see
1153 output that doesn't fill up an entire page. */
1155 outp_flush (struct outp_driver *d)
1157 if (d->device & OUTP_DEV_SCREEN && d->class->flush != NULL)
1159 outp_close_page (d);
1160 d->class->flush (d);
1164 /* Returns the width of string S, in device units, when output on
1167 outp_string_width (struct outp_driver *d, const char *s, enum outp_font font)
1169 struct outp_text text;
1173 text.justification = OUTP_LEFT;
1174 text.string = ss_cstr (s);
1175 text.h = text.v = INT_MAX;
1176 d->class->text_metrics (d, &text, &width, NULL);