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/>. */
26 #include <data/file-name.h>
27 #include <data/settings.h>
28 #include <libpspp/misc.h>
29 #include <libpspp/str.h>
30 #include <output/htmlP.h>
31 #include <output/output.h>
38 #define _(msgid) gettext (msgid)
40 /* FIXME? Should the output configuration format be changed to
41 drivername:classname:devicetype:options, where devicetype is zero
42 or more of screen, printer, listing? */
44 /* FIXME: Have the reentrancy problems been solved? */
46 /* Where the output driver name came from. */
49 OUTP_S_COMMAND_LINE, /* Specified by the user. */
50 OUTP_S_INIT_FILE /* `default' or the init file. */
53 /* Names the output drivers to be used. */
56 char *name; /* Name of the output driver. */
57 int source; /* OUTP_S_* */
58 struct outp_names *next, *prev;
61 /* Defines an init file macro. */
66 struct outp_defn *next, *prev;
69 static struct outp_defn *outp_macros;
70 static struct outp_names *outp_configure_vec;
72 /* A list of driver classes. */
73 struct outp_driver_class_list
75 const struct outp_class *class;
76 struct outp_driver_class_list *next;
79 static struct outp_driver_class_list *outp_class_list;
80 static struct outp_driver *outp_driver_list;
85 /* A set of OUTP_DEV_* bits indicating the devices that are
87 static int disabled_devices;
89 static void destroy_driver (struct outp_driver *);
90 static void configure_driver (const struct substring, const struct substring,
91 const struct substring, const struct substring);
93 /* Add a class to the class list. */
95 add_class (const struct outp_class *class)
97 struct outp_driver_class_list *new_list = xmalloc (sizeof *new_list);
99 new_list->class = class;
101 if (!outp_class_list)
103 outp_class_list = new_list;
104 new_list->next = NULL;
108 new_list->next = outp_class_list;
109 outp_class_list = new_list;
113 /* Finds the outp_names in outp_configure_vec with name between BP and
115 static struct outp_names *
116 search_names (char *bp, char *ep)
118 struct outp_names *n;
120 for (n = outp_configure_vec; n; n = n->next)
121 if ((int) strlen (n->name) == ep - bp && !memcmp (n->name, bp, ep - bp))
126 /* Deletes outp_names NAME from outp_configure_vec. */
128 delete_name (struct outp_names * n)
132 n->prev->next = n->next;
134 n->next->prev = n->prev;
135 if (n == outp_configure_vec)
136 outp_configure_vec = n->next;
140 /* Adds the name between BP and EP exclusive to list
141 outp_configure_vec with source SOURCE. */
143 add_name (char *bp, char *ep, int source)
145 struct outp_names *n = xmalloc (sizeof *n);
146 n->name = xmalloc (ep - bp + 1);
147 memcpy (n->name, bp, ep - bp);
148 n->name[ep - bp] = 0;
150 n->next = outp_configure_vec;
152 if (outp_configure_vec)
153 outp_configure_vec->prev = n;
154 outp_configure_vec = n;
157 /* Checks that outp_configure_vec is empty, complains and clears
160 check_configure_vec (void)
162 struct outp_names *n;
164 for (n = outp_configure_vec; n; n = n->next)
165 if (n->source == OUTP_S_COMMAND_LINE)
166 error (0, 0, _("unknown output driver `%s'"), n->name);
168 error (0, 0, _("output driver `%s' referenced but never defined"),
170 outp_configure_clear ();
173 /* Searches outp_configure_vec for the name between BP and EP
174 exclusive. If found, it is deleted, then replaced by the names
175 given in EP+1, if any. */
177 expand_name (char *bp, char *ep)
179 struct outp_names *n = search_names (bp, ep);
187 while (isspace ((unsigned char) *bp))
190 while (*ep && !isspace ((unsigned char) *ep))
194 if (!search_names (bp, ep))
195 add_name (bp, ep, OUTP_S_INIT_FILE);
200 /* Looks for a macro with key KEY, and returns the corresponding value
201 if found, or NULL if not. */
203 find_defn_value (const char *key)
205 static char buf[INT_STRLEN_BOUND (int) + 1];
208 for (d = outp_macros; d; d = d->next)
209 if (!strcmp (key, d->key))
210 return ds_cstr (&d->value);
211 if (!strcmp (key, "viewwidth"))
213 sprintf (buf, "%d", get_viewwidth ());
216 else if (!strcmp (key, "viewlength"))
218 sprintf (buf, "%d", get_viewlength ());
225 /* Initializes global variables. */
229 extern struct outp_class ascii_class;
230 extern struct outp_class postscript_class;
232 char def[] = "default";
234 add_class (&html_class);
235 add_class (&postscript_class);
236 add_class (&ascii_class);
238 add_name (def, &def[strlen (def)], OUTP_S_INIT_FILE);
241 /* Deletes all the output macros. */
245 struct outp_defn *d, *next;
247 for (d = outp_macros; d; d = next)
251 ds_destroy (&d->value);
257 init_default_drivers (void)
259 error (0, 0, _("using default output driver configuration"));
260 configure_driver (ss_cstr ("list"),
263 ss_cstr ("length=66 width=79 output-file=\"pspp.list\""));
266 /* Reads the initialization file; initializes
269 outp_read_devices (void)
279 init_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_INIT_FILE",
281 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
284 ds_init_empty (&line);
288 error (0, 0, _("cannot find output initialization file "
289 "(use `-vv' to view search path)"));
293 f = fopen (init_fn, "r");
296 error (0, errno, _("cannot open \"%s\""), init_fn);
305 if (!ds_read_config_line (&line, &line_number, f))
308 error (0, errno, _("reading \"%s\""), init_fn);
311 for (cp = ds_cstr (&line); isspace ((unsigned char) *cp); cp++);
312 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
313 outp_configure_macro (&cp[7]);
317 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
319 expand_name (cp, ep);
322 struct outp_names *n = search_names (cp, ep);
325 outp_configure_driver_line (ds_ss (&line));
330 error_at_line (0, 0, init_fn, line_number, _("syntax error"));
335 check_configure_vec ();
338 if (f && -1 == fclose (f))
339 error (0, errno, _("error closing \"%s\""), init_fn);
346 if (outp_driver_list == NULL)
347 error (0, 0, _("no active output drivers"));
350 error (0, 0, _("error reading device definition file"));
352 if (!result || outp_driver_list == NULL)
353 init_default_drivers ();
356 /* Clear the list of drivers to configure. */
358 outp_configure_clear (void)
360 struct outp_names *n, *next;
362 for (n = outp_configure_vec; n; n = next)
368 outp_configure_vec = NULL;
371 /* Adds the name BP to the list of drivers to configure into
374 outp_configure_add (char *bp)
376 char *ep = &bp[strlen (bp)];
377 if (!search_names (bp, ep))
378 add_name (bp, ep, OUTP_S_COMMAND_LINE);
381 /* Defines one configuration macro based on the text in BP, which
382 should be of the form `KEY=VALUE'. */
384 outp_configure_macro (char *bp)
389 while (isspace ((unsigned char) *bp))
392 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
395 d = xmalloc (sizeof *d);
396 d->key = xmalloc (ep - bp + 1);
397 memcpy (d->key, bp, ep - bp);
400 /* Earlier definitions for a particular KEY override later ones. */
401 if (find_defn_value (d->key))
410 while (isspace ((unsigned char) *ep))
413 ds_init_cstr (&d->value, ep);
414 fn_interp_vars (ds_ss (&d->value), find_defn_value, &d->value);
415 d->next = outp_macros;
418 outp_macros->prev = d;
422 /* Destroys all the drivers in driver list *DL and sets *DL to
425 destroy_list (struct outp_driver ** dl)
427 struct outp_driver *d, *next;
429 for (d = *dl; d; d = next)
438 /* Closes all the output drivers. */
442 struct outp_driver_class_list *n = outp_class_list ;
443 outp_configure_clear ();
444 destroy_list (&outp_driver_list);
448 struct outp_driver_class_list *next = n->next;
452 outp_class_list = NULL;
457 free (outp_subtitle);
458 outp_subtitle = NULL;
461 /* Display on stdout a list of all registered driver classes. */
463 outp_list_classes (void)
465 int width = get_viewwidth();
466 struct outp_driver_class_list *c;
468 printf (_("Driver classes:\n\t"));
470 for (c = outp_class_list; c; c = c->next)
472 if ((int) strlen (c->class->name) + 1 > width)
475 width = get_viewwidth() - 8;
479 fputs (c->class->name, stdout);
484 /* Obtains a token from S and advances its position. Errors are
485 reported against the given DRIVER_NAME.
486 The token is stored in TOKEN. Returns true if successful,
487 false on syntax error.
489 Caller is responsible for skipping leading spaces. */
491 get_option_token (struct substring *s, const char *driver_name,
492 struct string *token)
500 error (0, 0, _("syntax error parsing options for \"%s\" driver"),
504 else if (c == '\'' || c == '"')
516 _("reached end of options inside quoted string "
517 "parsing options for \"%s\" driver"),
522 ds_put_char (token, c);
569 while (ss_first (*s) >= '0' && ss_first (*s) <= '7')
570 out = out * 8 + (ss_get_char (s) - '0');
575 while (isxdigit (ss_first (*s)))
582 out += tolower (c) - 'a' + 10;
586 error (0, 0, _("syntax error in string constant "
587 "parsing options for \"%s\" driver"),
591 ds_put_char (token, out);
599 ds_put_char (token, c);
602 if (c == EOF || c == '=' || isspace (c))
612 outp_parse_options (struct substring options,
613 bool (*callback) (struct outp_driver *, const char *key,
614 const struct string *value),
615 struct outp_driver *driver)
617 struct string key = DS_EMPTY_INITIALIZER;
618 struct string value = DS_EMPTY_INITIALIZER;
619 struct substring left = options;
624 ss_ltrim (&left, ss_cstr (CC_SPACES));
625 if (ss_is_empty (left))
628 if (!get_option_token (&left, driver->name, &key))
631 ss_ltrim (&left, ss_cstr (CC_SPACES));
632 if (!ss_match_char (&left, '='))
634 error (0, 0, _("syntax error expecting `=' "
635 "parsing options for driver \"%s\""),
640 ss_ltrim (&left, ss_cstr (CC_SPACES));
641 if (!get_option_token (&left, driver->name, &value))
644 ok = callback (driver, ds_cstr (&key), &value);
654 /* Find the driver in outp_driver_list with name NAME. */
655 static struct outp_driver *
656 find_driver (char *name)
658 struct outp_driver *d;
660 for (d = outp_driver_list; d; d = d->next)
661 if (!strcmp (d->name, name))
666 /* Adds a driver to outp_driver_list pursuant to the
667 specification provided. */
669 configure_driver (struct substring driver_name, struct substring class_name,
670 struct substring device_type, struct substring options)
672 struct outp_driver *d, *iter;
673 struct outp_driver_class_list *c;
675 struct substring token;
680 for (c = outp_class_list; c; c = c->next)
681 if (!ss_compare (ss_cstr (c->class->name), class_name))
685 error (0, 0, _("unknown output driver class `%.*s'"),
686 (int) ss_length (class_name), ss_data (class_name));
690 /* Parse device type. */
692 while (ss_tokenize (device_type, ss_cstr (CC_SPACES), &save_idx, &token))
693 if (!ss_compare (token, ss_cstr ("listing")))
694 device |= OUTP_DEV_LISTING;
695 else if (!ss_compare (token, ss_cstr ("screen")))
696 device |= OUTP_DEV_SCREEN;
697 else if (!ss_compare (token, ss_cstr ("printer")))
698 device |= OUTP_DEV_PRINTER;
700 error (0, 0, _("unknown device type `%.*s'"),
701 (int) ss_length (token), ss_data (token));
703 /* Open the device. */
704 d = xmalloc (sizeof *d);
705 d->next = d->prev = NULL;
707 d->name = ss_xstrdup (driver_name);
708 d->page_open = false;
710 d->cp_x = d->cp_y = 0;
715 if (!d->class->open_driver (d, options))
717 error (0, 0, _("cannot initialize output driver `%s' of class `%s'"),
718 d->name, d->class->name);
724 /* Find like-named driver and delete. */
725 iter = find_driver (d->name);
727 destroy_driver (iter);
730 d->next = outp_driver_list;
732 if (outp_driver_list != NULL)
733 outp_driver_list->prev = d;
734 outp_driver_list = d;
737 /* String LINE is in format:
738 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
739 Adds a driver to outp_driver_list pursuant to the specification
742 outp_configure_driver_line (struct substring line_)
744 struct string line = DS_EMPTY_INITIALIZER;
745 struct substring tokens[4];
749 fn_interp_vars (line_, find_defn_value, &line);
752 for (i = 0; i < 4; i++)
754 struct substring *token = &tokens[i];
755 ds_separate (&line, ss_cstr (i < 3 ? ":" : ""), &save_idx, token);
756 ss_trim (token, ss_cstr (CC_SPACES));
759 if (!ss_is_empty (tokens[0]) && !ss_is_empty (tokens[1]))
760 configure_driver (tokens[0], tokens[1], tokens[2], tokens[3]);
763 _("driver definition line missing driver name or class name"));
768 /* Destroys output driver D. */
770 destroy_driver (struct outp_driver *d)
775 struct outp_driver_class_list *c;
777 d->class->close_driver (d);
779 for (c = outp_class_list; c; c = c->next)
780 if (c->class == d->class)
786 /* Remove this driver from the global driver list. */
788 d->prev->next = d->next;
790 d->next->prev = d->prev;
791 if (d == outp_driver_list)
792 outp_driver_list = d->next;
795 /* Tries to match S as one of the keywords in TAB, with
796 corresponding information structure INFO. Returns category
797 code and stores subcategory in *SUBCAT on success. Returns -1
800 outp_match_keyword (const char *s, const struct outp_option *tab, int *subcat)
802 for (; tab->keyword != NULL; tab++)
803 if (!strcmp (s, tab->keyword))
805 *subcat = tab->subcat;
811 /* Parses UNIT as a dimensional unit. Returns the multiplicative
812 factor needed to change a quantity measured in that unit into
813 1/72000" units. If UNIT is empty, it is treated as
814 millimeters. If the unit is unrecognized, returns 0. */
816 parse_unit (const char *unit)
824 static const struct unit units[] =
827 {"pc", 72000 / 72 * 12.0},
829 {"cm", 72000 / 2.54},
830 {"mm", 72000 / 25.4},
834 const struct unit *p;
836 unit += strspn (unit, CC_SPACES);
837 for (p = units; p < units + sizeof units / sizeof *units; p++)
838 if (!strcasecmp (unit, p->name))
843 /* Determines the size of a dimensional measurement and returns
844 the size in units of 1/72000". Units are assumed to be
845 millimeters unless otherwise specified. Returns 0 on
848 outp_evaluate_dimension (const char *dimen)
854 raw = strtod (dimen, &tail);
859 factor = parse_unit (tail);
866 error (0, 0, _("`%s' is not a valid length."), dimen);
870 /* Stores the dimensions in 1/72000" units of paper identified by
871 SIZE, which is of form `HORZ x VERT [UNIT]' where HORZ and
872 VERT are numbers and UNIT is an optional unit of measurement,
873 into *H and *V. Return true on success. */
875 parse_paper_size (const char *size, int *h, int *v)
877 double raw_h, raw_v, factor;
881 raw_h = strtod (size, &tail);
886 tail += strspn (tail, CC_SPACES "x,");
889 raw_v = strtod (tail, &tail);
894 factor = parse_unit (tail);
898 *h = raw_h * factor + .5;
899 *v = raw_v * factor + .5;
904 get_standard_paper_size (struct substring name, int *h, int *v)
906 static const char *sizes[][2] =
908 {"a0", "841 x 1189 mm"},
909 {"a1", "594 x 841 mm"},
910 {"a2", "420 x 594 mm"},
911 {"a3", "297 x 420 mm"},
912 {"a4", "210 x 297 mm"},
913 {"a5", "148 x 210 mm"},
914 {"b5", "176 x 250 mm"},
915 {"a6", "105 x 148 mm"},
916 {"a7", "74 x 105 mm"},
917 {"a8", "52 x 74 mm"},
918 {"a9", "37 x 52 mm"},
919 {"a10", "26 x 37 mm"},
920 {"b0", "1000 x 1414 mm"},
921 {"b1", "707 x 1000 mm"},
922 {"b2", "500 x 707 mm"},
923 {"b3", "353 x 500 mm"},
924 {"b4", "250 x 353 mm"},
925 {"letter", "612 x 792 pt"},
926 {"legal", "612 x 1008 pt"},
927 {"executive", "522 x 756 pt"},
928 {"note", "612 x 792 pt"},
929 {"11x17", "792 x 1224 pt"},
930 {"tabloid", "792 x 1224 pt"},
931 {"statement", "396 x 612 pt"},
932 {"halfletter", "396 x 612 pt"},
933 {"halfexecutive", "378 x 522 pt"},
934 {"folio", "612 x 936 pt"},
935 {"quarto", "610 x 780 pt"},
936 {"ledger", "1224 x 792 pt"},
937 {"archA", "648 x 864 pt"},
938 {"archB", "864 x 1296 pt"},
939 {"archC", "1296 x 1728 pt"},
940 {"archD", "1728 x 2592 pt"},
941 {"archE", "2592 x 3456 pt"},
942 {"flsa", "612 x 936 pt"},
943 {"flse", "612 x 936 pt"},
944 {"csheet", "1224 x 1584 pt"},
945 {"dsheet", "1584 x 2448 pt"},
946 {"esheet", "2448 x 3168 pt"},
951 for (i = 0; i < sizeof sizes / sizeof *sizes; i++)
952 if (ss_equals_case (ss_cstr (sizes[i][0]), name))
954 bool ok = parse_paper_size (sizes[i][1], h, v);
958 error (0, 0, _("unknown paper type `%.*s'"),
959 (int) ss_length (name), ss_data (name));
963 /* Reads file FILE_NAME to find a paper size. Stores the
964 dimensions, in 1/72000" units, into *H and *V. Returns true
965 on success, false on failure. */
967 read_paper_conf (const char *file_name, int *h, int *v)
969 struct string line = DS_EMPTY_INITIALIZER;
973 file = fopen (file_name, "r");
976 error (0, errno, _("error opening \"%s\""), file_name);
982 struct substring name;
984 if (!ds_read_config_line (&line, &line_number, file))
987 error (0, errno, _("error reading \"%s\""), file_name);
991 name = ds_ss (&line);
992 ss_trim (&name, ss_cstr (CC_SPACES));
993 if (!ss_is_empty (name))
995 bool ok = get_standard_paper_size (name, h, v);
1004 error (0, 0, _("paper size file \"%s\" does not state a paper size"),
1009 /* The user didn't specify a paper size, so let's choose a
1010 default based on his environment. Stores the
1011 dimensions, in 1/72000" units, into *H and *V. Returns true
1012 on success, false on failure. */
1014 get_default_paper_size (int *h, int *v)
1016 /* libpaper in Debian (and other distributions?) allows the
1017 paper size to be specified in $PAPERSIZE or in a file
1018 specified in $PAPERCONF. */
1019 if (getenv ("PAPERSIZE") != NULL)
1020 return get_standard_paper_size (ss_cstr (getenv ("PAPERSIZE")), h, v);
1021 if (getenv ("PAPERCONF") != NULL)
1022 return read_paper_conf (getenv ("PAPERCONF"), h, v);
1025 /* LC_PAPER is a non-standard glibc extension. */
1026 *h = (int) nl_langinfo(_NL_PAPER_WIDTH) * (72000 / 25.4);
1027 *v = (int) nl_langinfo(_NL_PAPER_HEIGHT) * (72000 / 25.4);
1028 if (*h > 0 && *v > 0)
1032 /* libpaper defaults to /etc/papersize. */
1033 if (fn_exists ("/etc/papersize"))
1034 return read_paper_conf ("/etc/papersize", h, v);
1036 /* Can't find a default. */
1040 /* Stores the dimensions, in 1/72000" units, of paper identified
1041 by SIZE into *H and *V. SIZE can be the name of a kind of
1042 paper ("a4", "letter", ...) or a pair of dimensions
1043 ("210x297", "8.5x11in", ...). Returns true on success, false
1044 on failure. On failure, *H and *V are set for A4 paper. */
1046 outp_get_paper_size (const char *size, int *h, int *v)
1052 ss_trim (&s, ss_cstr (CC_SPACES));
1054 if (ss_is_empty (s))
1056 /* Treat empty string as default paper size. */
1057 ok = get_default_paper_size (h, v);
1059 else if (isdigit (ss_first (s)))
1061 /* Treat string that starts with digit as explicit size. */
1062 ok = parse_paper_size (size, h, v);
1064 error (0, 0, _("syntax error in paper size `%s'"), size);
1068 /* Check against standard paper sizes. */
1069 ok = get_standard_paper_size (s, h, v);
1072 /* Default to A4 on error. */
1075 *h = 210 * (72000 / 25.4);
1076 *v = 297 * (72000 / 25.4);
1081 /* If D is NULL, returns the first enabled driver if any, NULL if
1082 none. Otherwise D must be the last driver returned by this
1083 function, in which case the next enabled driver is returned or NULL
1084 if that was the last. */
1085 struct outp_driver *
1086 outp_drivers (struct outp_driver *d)
1091 d = outp_driver_list;
1096 || (d->device == 0 || (d->device & disabled_devices) != d->device))
1103 /* Enables (if ENABLE is true) or disables (if ENABLE is false) the
1104 device(s) given in mask DEVICE. */
1106 outp_enable_device (bool enable, int device)
1109 disabled_devices &= ~device;
1111 disabled_devices |= device;
1114 /* Opens a page on driver D (if one is not open). */
1116 outp_open_page (struct outp_driver *d)
1120 d->cp_x = d->cp_y = 0;
1122 d->page_open = true;
1123 if (d->class->open_page != NULL)
1124 d->class->open_page (d);
1128 /* Closes the page on driver D (if one is open). */
1130 outp_close_page (struct outp_driver *d)
1134 if (d->class->close_page != NULL)
1135 d->class->close_page (d);
1136 d->page_open = false;
1140 /* Ejects the page on device D, if a page is open and non-blank,
1141 and opens a new page. */
1143 outp_eject_page (struct outp_driver *d)
1145 if (d->page_open && d->cp_y != 0)
1146 outp_close_page (d);
1150 /* Flushes output to screen devices, so that the user can see
1151 output that doesn't fill up an entire page. */
1153 outp_flush (struct outp_driver *d)
1155 if (d->device & OUTP_DEV_SCREEN && d->class->flush != NULL)
1157 outp_close_page (d);
1158 d->class->flush (d);
1162 /* Returns the width of string S, in device units, when output on
1165 outp_string_width (struct outp_driver *d, const char *s, enum outp_font font)
1167 struct outp_text text;
1171 text.justification = OUTP_LEFT;
1172 text.string = ss_cstr (s);
1173 text.h = text.v = INT_MAX;
1174 d->class->text_metrics (d, &text, &width, NULL);