1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 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/>. */
23 #include <libpspp/alloc.h>
24 #include <data/file-name.h>
27 #include <libpspp/misc.h>
28 #include <data/settings.h>
29 #include <libpspp/str.h>
33 #define _(msgid) gettext (msgid)
35 /* FIXME? Should the output configuration format be changed to
36 drivername:classname:devicetype:options, where devicetype is zero
37 or more of screen, printer, listing? */
39 /* FIXME: Have the reentrancy problems been solved? */
41 /* Where the output driver name came from. */
44 OUTP_S_COMMAND_LINE, /* Specified by the user. */
45 OUTP_S_INIT_FILE /* `default' or the init file. */
48 /* Names the output drivers to be used. */
51 char *name; /* Name of the output driver. */
52 int source; /* OUTP_S_* */
53 struct outp_names *next, *prev;
56 /* Defines an init file macro. */
61 struct outp_defn *next, *prev;
64 static struct outp_defn *outp_macros;
65 static struct outp_names *outp_configure_vec;
67 /* A list of driver classes. */
68 struct outp_driver_class_list
70 const struct outp_class *class;
71 struct outp_driver_class_list *next;
74 static struct outp_driver_class_list *outp_class_list;
75 static struct outp_driver *outp_driver_list;
80 /* A set of OUTP_DEV_* bits indicating the devices that are
82 static int disabled_devices;
84 static void destroy_driver (struct outp_driver *);
85 static void configure_driver_line (struct substring);
86 static void configure_driver (const struct substring, const struct substring,
87 const struct substring, const struct substring);
89 /* Add a class to the class list. */
91 add_class (const struct outp_class *class)
93 struct outp_driver_class_list *new_list = xmalloc (sizeof *new_list);
95 new_list->class = class;
99 outp_class_list = new_list;
100 new_list->next = NULL;
104 new_list->next = outp_class_list;
105 outp_class_list = new_list;
109 /* Finds the outp_names in outp_configure_vec with name between BP and
111 static struct outp_names *
112 search_names (char *bp, char *ep)
114 struct outp_names *n;
116 for (n = outp_configure_vec; n; n = n->next)
117 if ((int) strlen (n->name) == ep - bp && !memcmp (n->name, bp, ep - bp))
122 /* Deletes outp_names NAME from outp_configure_vec. */
124 delete_name (struct outp_names * n)
128 n->prev->next = n->next;
130 n->next->prev = n->prev;
131 if (n == outp_configure_vec)
132 outp_configure_vec = n->next;
136 /* Adds the name between BP and EP exclusive to list
137 outp_configure_vec with source SOURCE. */
139 add_name (char *bp, char *ep, int source)
141 struct outp_names *n = xmalloc (sizeof *n);
142 n->name = xmalloc (ep - bp + 1);
143 memcpy (n->name, bp, ep - bp);
144 n->name[ep - bp] = 0;
146 n->next = outp_configure_vec;
148 if (outp_configure_vec)
149 outp_configure_vec->prev = n;
150 outp_configure_vec = n;
153 /* Checks that outp_configure_vec is empty, bitches & clears it if it
156 check_configure_vec (void)
158 struct outp_names *n;
160 for (n = outp_configure_vec; n; n = n->next)
161 if (n->source == OUTP_S_COMMAND_LINE)
162 error (0, 0, _("unknown output driver `%s'"), n->name);
164 error (0, 0, _("output driver `%s' referenced but never defined"),
166 outp_configure_clear ();
169 /* Searches outp_configure_vec for the name between BP and EP
170 exclusive. If found, it is deleted, then replaced by the names
171 given in EP+1, if any. */
173 expand_name (char *bp, char *ep)
175 struct outp_names *n = search_names (bp, ep);
183 while (isspace ((unsigned char) *bp))
186 while (*ep && !isspace ((unsigned char) *ep))
190 if (!search_names (bp, ep))
191 add_name (bp, ep, OUTP_S_INIT_FILE);
196 /* Looks for a macro with key KEY, and returns the corresponding value
197 if found, or NULL if not. */
199 find_defn_value (const char *key)
201 static char buf[INT_STRLEN_BOUND (int) + 1];
204 for (d = outp_macros; d; d = d->next)
205 if (!strcmp (key, d->key))
206 return ds_cstr (&d->value);
207 if (!strcmp (key, "viewwidth"))
209 sprintf (buf, "%d", get_viewwidth ());
212 else if (!strcmp (key, "viewlength"))
214 sprintf (buf, "%d", get_viewlength ());
221 /* Initializes global variables. */
225 extern struct outp_class ascii_class;
226 extern struct outp_class postscript_class;
228 char def[] = "default";
230 add_class (&html_class);
231 add_class (&postscript_class);
232 add_class (&ascii_class);
234 add_name (def, &def[strlen (def)], OUTP_S_INIT_FILE);
237 /* Deletes all the output macros. */
241 struct outp_defn *d, *next;
243 for (d = outp_macros; d; d = next)
247 ds_destroy (&d->value);
253 init_default_drivers (void)
255 error (0, 0, _("using default output driver configuration"));
256 configure_driver (ss_cstr ("list"),
259 ss_cstr ("length=66 width=79 output-file=\"pspp.list\""));
262 /* Reads the initialization file; initializes
265 outp_read_devices (void)
275 init_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_INIT_FILE",
277 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
280 ds_init_empty (&line);
284 error (0, 0, _("cannot find output initialization file "
285 "(use `-vv' to view search path)"));
289 f = fopen (init_fn, "r");
292 error (0, errno, _("cannot open \"%s\""), init_fn);
301 if (!ds_read_config_line (&line, &line_number, f))
304 error (0, errno, _("reading \"%s\""), init_fn);
307 for (cp = ds_cstr (&line); isspace ((unsigned char) *cp); cp++);
308 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
309 outp_configure_macro (&cp[7]);
313 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
315 expand_name (cp, ep);
318 struct outp_names *n = search_names (cp, ep);
321 configure_driver_line (ds_ss (&line));
326 error_at_line (0, 0, init_fn, line_number, _("syntax error"));
331 check_configure_vec ();
334 if (f && -1 == fclose (f))
335 error (0, errno, _("error closing \"%s\""), init_fn);
342 if (outp_driver_list == NULL)
343 error (0, 0, _("no active output drivers"));
346 error (0, 0, _("error reading device definition file"));
348 if (!result || outp_driver_list == NULL)
349 init_default_drivers ();
352 /* Clear the list of drivers to configure. */
354 outp_configure_clear (void)
356 struct outp_names *n, *next;
358 for (n = outp_configure_vec; n; n = next)
364 outp_configure_vec = NULL;
367 /* Adds the name BP to the list of drivers to configure into
370 outp_configure_add (char *bp)
372 char *ep = &bp[strlen (bp)];
373 if (!search_names (bp, ep))
374 add_name (bp, ep, OUTP_S_COMMAND_LINE);
377 /* Defines one configuration macro based on the text in BP, which
378 should be of the form `KEY=VALUE'. */
380 outp_configure_macro (char *bp)
385 while (isspace ((unsigned char) *bp))
388 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
391 d = xmalloc (sizeof *d);
392 d->key = xmalloc (ep - bp + 1);
393 memcpy (d->key, bp, ep - bp);
396 /* Earlier definitions for a particular KEY override later ones. */
397 if (find_defn_value (d->key))
406 while (isspace ((unsigned char) *ep))
409 ds_init_cstr (&d->value, ep);
410 fn_interp_vars (ds_ss (&d->value), find_defn_value, &d->value);
411 d->next = outp_macros;
414 outp_macros->prev = d;
418 /* Destroys all the drivers in driver list *DL and sets *DL to
421 destroy_list (struct outp_driver ** dl)
423 struct outp_driver *d, *next;
425 for (d = *dl; d; d = next)
434 /* Closes all the output drivers. */
438 struct outp_driver_class_list *n = outp_class_list ;
439 destroy_list (&outp_driver_list);
443 struct outp_driver_class_list *next = n->next;
447 outp_class_list = NULL;
452 free (outp_subtitle);
453 outp_subtitle = NULL;
456 /* Display on stdout a list of all registered driver classes. */
458 outp_list_classes (void)
460 int width = get_viewwidth();
461 struct outp_driver_class_list *c;
463 printf (_("Driver classes:\n\t"));
465 for (c = outp_class_list; c; c = c->next)
467 if ((int) strlen (c->class->name) + 1 > width)
470 width = get_viewwidth() - 8;
474 fputs (c->class->name, stdout);
479 /* Obtains a token from S and advances its position. Errors are
480 reported against the given DRIVER_NAME.
481 The token is stored in TOKEN. Returns true if successful,
482 false on syntax error.
484 Caller is responsible for skipping leading spaces. */
486 get_option_token (struct substring *s, const char *driver_name,
487 struct string *token)
495 error (0, 0, _("syntax error parsing options for \"%s\" driver"),
499 else if (c == '\'' || c == '"')
511 _("reached end of options inside quoted string "
512 "parsing options for \"%s\" driver"),
517 ds_put_char (token, c);
564 while (ss_first (*s) >= '0' && ss_first (*s) <= '7')
565 out = c * 8 + (ss_get_char (s) - '0');
570 while (isxdigit (ss_first (*s)))
577 out += tolower (c) - 'a' + 10;
581 error (0, 0, _("syntax error in string constant "
582 "parsing options for \"%s\" driver"),
586 ds_put_char (token, out);
594 ds_put_char (token, c);
597 if (c == EOF || c == '=' || isspace (c))
607 outp_parse_options (struct substring options,
608 bool (*callback) (struct outp_driver *, const char *key,
609 const struct string *value),
610 struct outp_driver *driver)
612 struct string key = DS_EMPTY_INITIALIZER;
613 struct string value = DS_EMPTY_INITIALIZER;
614 struct substring left = options;
619 ss_ltrim (&left, ss_cstr (CC_SPACES));
620 if (ss_is_empty (left))
623 if (!get_option_token (&left, driver->name, &key))
626 ss_ltrim (&left, ss_cstr (CC_SPACES));
627 if (!ss_match_char (&left, '='))
629 error (0, 0, _("syntax error expecting `=' "
630 "parsing options for driver \"%s\""),
635 ss_ltrim (&left, ss_cstr (CC_SPACES));
636 if (!get_option_token (&left, driver->name, &value))
639 ok = callback (driver, ds_cstr (&key), &value);
649 /* Find the driver in outp_driver_list with name NAME. */
650 static struct outp_driver *
651 find_driver (char *name)
653 struct outp_driver *d;
655 for (d = outp_driver_list; d; d = d->next)
656 if (!strcmp (d->name, name))
661 /* Adds a driver to outp_driver_list pursuant to the
662 specification provided. */
664 configure_driver (struct substring driver_name, struct substring class_name,
665 struct substring device_type, struct substring options)
667 struct outp_driver *d, *iter;
668 struct outp_driver_class_list *c;
670 struct substring token;
675 for (c = outp_class_list; c; c = c->next)
676 if (!ss_compare (ss_cstr (c->class->name), class_name))
680 error (0, 0, _("unknown output driver class `%.*s'"),
681 (int) ss_length (class_name), ss_data (class_name));
685 /* Parse device type. */
687 while (ss_tokenize (device_type, ss_cstr (CC_SPACES), &save_idx, &token))
688 if (!ss_compare (token, ss_cstr ("listing")))
689 device |= OUTP_DEV_LISTING;
690 else if (!ss_compare (token, ss_cstr ("screen")))
691 device |= OUTP_DEV_SCREEN;
692 else if (!ss_compare (token, ss_cstr ("printer")))
693 device |= OUTP_DEV_PRINTER;
695 error (0, 0, _("unknown device type `%.*s'"),
696 (int) ss_length (token), ss_data (token));
698 /* Open the device. */
699 d = xmalloc (sizeof *d);
700 d->next = d->prev = NULL;
702 d->name = ss_xstrdup (driver_name);
703 d->page_open = false;
704 d->device = OUTP_DEV_NONE;
705 d->cp_x = d->cp_y = 0;
710 if (!d->class->open_driver (d, options))
712 error (0, 0, _("cannot initialize output driver `%s' of class `%s'"),
713 d->name, d->class->name);
719 /* Find like-named driver and delete. */
720 iter = find_driver (d->name);
722 destroy_driver (iter);
725 d->next = outp_driver_list;
727 if (outp_driver_list != NULL)
728 outp_driver_list->prev = d;
729 outp_driver_list = d;
732 /* String LINE is in format:
733 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
734 Adds a driver to outp_driver_list pursuant to the specification
737 configure_driver_line (struct substring line_)
739 struct string line = DS_EMPTY_INITIALIZER;
740 struct substring tokens[4];
744 fn_interp_vars (line_, find_defn_value, &line);
747 for (i = 0; i < 4; i++)
749 struct substring *token = &tokens[i];
750 ds_separate (&line, ss_cstr (i < 3 ? ":" : ""), &save_idx, token);
751 ss_trim (token, ss_cstr (CC_SPACES));
754 if (!ss_is_empty (tokens[0]) && !ss_is_empty (tokens[1]))
755 configure_driver (tokens[0], tokens[1], tokens[2], tokens[3]);
758 _("driver definition line missing driver name or class name"));
763 /* Destroys output driver D. */
765 destroy_driver (struct outp_driver *d)
770 struct outp_driver_class_list *c;
772 d->class->close_driver (d);
774 for (c = outp_class_list; c; c = c->next)
775 if (c->class == d->class)
781 /* Remove this driver from the global driver list. */
783 d->prev->next = d->next;
785 d->next->prev = d->prev;
786 if (d == outp_driver_list)
787 outp_driver_list = d->next;
790 /* Tries to match S as one of the keywords in TAB, with
791 corresponding information structure INFO. Returns category
792 code and stores subcategory in *SUBCAT on success. Returns -1
795 outp_match_keyword (const char *s, const struct outp_option *tab, int *subcat)
797 for (; tab->keyword != NULL; tab++)
798 if (!strcmp (s, tab->keyword))
800 *subcat = tab->subcat;
806 /* Encapsulate two characters in a single int. */
807 #define TWO_CHARS(A, B) \
810 /* Determines the size of a dimensional measurement and returns the
811 size in units of 1/72000". Units if not specified explicitly are
812 inches for values under 50, millimeters otherwise. Returns 0,
813 stores NULL to *TAIL on error; otherwise returns dimension, stores
816 outp_evaluate_dimension (char *dimen, char **tail)
822 value = strtod (s, &ptail);
829 b = strtod (s, &ptail);
830 if (b <= 0.0 || ptail == s)
835 c = strtod (s, &ptail);
836 if (c <= 0.0 || ptail == s)
846 else if (*ptail == '/')
850 b = strtod (s, &ptail);
851 if (b <= 0.0 || ptail == s)
858 if (*s == 0 || isspace ((unsigned char) *s))
863 value *= 72000 / 25.4;
869 /* Standard TeX units are supported. */
873 switch (TWO_CHARS (s[0], s[1]))
875 case TWO_CHARS ('p', 't'):
876 factor = 72000 / 72.27;
878 case TWO_CHARS ('p', 'c'):
879 factor = 72000 / 72.27 * 12.0;
881 case TWO_CHARS ('i', 'n'):
884 case TWO_CHARS ('b', 'p'):
885 factor = 72000 / 72.0;
887 case TWO_CHARS ('c', 'm'):
888 factor = 72000 / 2.54;
890 case TWO_CHARS ('m', 'm'):
891 factor = 72000 / 25.4;
893 case TWO_CHARS ('d', 'd'):
894 factor = 72000 / 72.27 * 1.0700086;
896 case TWO_CHARS ('c', 'c'):
897 factor = 72000 / 72.27 * 12.840104;
899 case TWO_CHARS ('s', 'p'):
900 factor = 72000 / 72.27 / 65536.0;
904 _("unit \"%s\" is unknown in dimension \"%s\""), s, dimen);
919 error (0, 0, _("bad dimension \"%s\""), dimen);
923 /* Stores the dimensions in 1/72000" units of paper identified by
924 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
925 of HORZ and VERT are dimensions, into *H and *V. Return true on
928 internal_get_paper_size (char *size, int *h, int *v)
932 while (isspace ((unsigned char) *size))
934 *h = outp_evaluate_dimension (size, &tail);
937 while (isspace ((unsigned char) *tail))
941 else if (*tail == 'b' && tail[1] == 'y')
945 error (0, 0, _("`x' expected in paper size `%s'"), size);
948 *v = outp_evaluate_dimension (tail, &tail);
951 while (isspace ((unsigned char) *tail))
955 error (0, 0, _("trailing garbage `%s' on paper size `%s'"), tail, size);
962 /* Stores the dimensions, in 1/72000" units, of paper identified by
963 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
964 V', or it may be a case-insensitive paper identifier, which is
965 looked up in the `papersize' configuration file. Returns true
966 on success. May modify SIZE. */
967 /* Don't read further unless you've got a strong stomach. */
969 outp_get_paper_size (char *size, int *h, int *v)
984 bool free_it = false;
988 while (isspace ((unsigned char) *size))
990 if (isdigit ((unsigned char) *size))
991 return internal_get_paper_size (size, h, v);
995 while (isspace ((unsigned char) *ep) && ep >= size)
999 error (0, 0, _("paper size name cannot be empty"));
1007 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1009 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1012 ds_init_empty (&line);
1014 if (pprsz_fn == NULL)
1016 error (0, 0, _("cannot find `papersize' configuration file"));
1020 f = fopen (pprsz_fn, "r");
1023 error (0, errno, _("error opening \"%s\""), pprsz_fn);
1029 struct substring p, name;
1031 if (!ds_read_config_line (&line, &line_number, f))
1034 error (0, errno, _("error reading \"%s\""), pprsz_fn);
1039 ss_ltrim (&p, ss_cstr (CC_SPACES));
1040 if (!ss_match_char (&p, '"') || !ss_get_until (&p, '"', &name))
1042 if (ss_compare (name, ss_cstr (size)))
1045 ss_ltrim (&p, ss_cstr (CC_SPACES));
1046 if (ss_match_char (&p, '='))
1050 ss_trim (&p, ss_cstr (CC_SPACES));
1051 size = ss_xstrdup (p);
1059 error_at_line (0, 0, pprsz_fn, line_number,
1060 _("syntax error in paper size definition"));
1063 /* We found the one we want! */
1064 result = internal_get_paper_size (size, h, v);
1072 error (0, 0, _("error reading paper size definition file"));
1077 /* If D is NULL, returns the first enabled driver if any, NULL if
1078 none. Otherwise D must be the last driver returned by this
1079 function, in which case the next enabled driver is returned or NULL
1080 if that was the last. */
1081 struct outp_driver *
1082 outp_drivers (struct outp_driver *d)
1087 d = outp_driver_list;
1092 || (d->device == 0 || (d->device & disabled_devices) != d->device))
1099 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1100 device(s) given in mask DEVICE. */
1102 outp_enable_device (int enable, int device)
1105 disabled_devices &= ~device;
1107 disabled_devices |= device;
1110 /* Opens a page on driver D (if one is not open). */
1112 outp_open_page (struct outp_driver *d)
1116 d->cp_x = d->cp_y = 0;
1118 d->page_open = true;
1119 if (d->class->open_page != NULL)
1120 d->class->open_page (d);
1124 /* Closes the page on driver D (if one is open). */
1126 outp_close_page (struct outp_driver *d)
1130 if (d->class->close_page != NULL)
1131 d->class->close_page (d);
1132 d->page_open = false;
1136 /* Ejects the page on device D, if a page is open and non-blank,
1137 and opens a new page. */
1139 outp_eject_page (struct outp_driver *d)
1141 if (d->page_open && d->cp_y != 0)
1142 outp_close_page (d);
1146 /* Returns the width of string S, in device units, when output on
1149 outp_string_width (struct outp_driver *d, const char *s, enum outp_font font)
1151 struct outp_text text;
1155 text.justification = OUTP_LEFT;
1156 text.string = ss_cstr (s);
1157 text.h = text.v = INT_MAX;
1158 d->class->text_metrics (d, &text, &width, NULL);