1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include <libpspp/alloc.h>
27 #include <data/file-name.h>
30 #include <libpspp/misc.h>
31 #include <data/settings.h>
32 #include <libpspp/str.h>
36 #define _(msgid) gettext (msgid)
38 /* FIXME? Should the output configuration format be changed to
39 drivername:classname:devicetype:options, where devicetype is zero
40 or more of screen, printer, listing? */
42 /* FIXME: Have the reentrancy problems been solved? */
44 /* Where the output driver name came from. */
47 OUTP_S_COMMAND_LINE, /* Specified by the user. */
48 OUTP_S_INIT_FILE /* `default' or the init file. */
51 /* Names the output drivers to be used. */
54 char *name; /* Name of the output driver. */
55 int source; /* OUTP_S_* */
56 struct outp_names *next, *prev;
59 /* Defines an init file macro. */
64 struct outp_defn *next, *prev;
67 static struct outp_defn *outp_macros;
68 static struct outp_names *outp_configure_vec;
70 /* A list of driver classes. */
71 struct outp_driver_class_list
73 const struct outp_class *class;
74 struct outp_driver_class_list *next;
77 static struct outp_driver_class_list *outp_class_list;
78 static struct outp_driver *outp_driver_list;
83 /* A set of OUTP_DEV_* bits indicating the devices that are
85 static int disabled_devices;
87 static void destroy_driver (struct outp_driver *);
88 static void configure_driver_line (struct substring);
89 static void configure_driver (const struct substring, const struct substring,
90 const struct substring, const struct substring);
92 /* Add a class to the class list. */
94 add_class (const struct outp_class *class)
96 struct outp_driver_class_list *new_list = xmalloc (sizeof *new_list);
98 new_list->class = class;
100 if (!outp_class_list)
102 outp_class_list = new_list;
103 new_list->next = NULL;
107 new_list->next = outp_class_list;
108 outp_class_list = new_list;
112 /* Finds the outp_names in outp_configure_vec with name between BP and
114 static struct outp_names *
115 search_names (char *bp, char *ep)
117 struct outp_names *n;
119 for (n = outp_configure_vec; n; n = n->next)
120 if ((int) strlen (n->name) == ep - bp && !memcmp (n->name, bp, ep - bp))
125 /* Deletes outp_names NAME from outp_configure_vec. */
127 delete_name (struct outp_names * n)
131 n->prev->next = n->next;
133 n->next->prev = n->prev;
134 if (n == outp_configure_vec)
135 outp_configure_vec = n->next;
139 /* Adds the name between BP and EP exclusive to list
140 outp_configure_vec with source SOURCE. */
142 add_name (char *bp, char *ep, int source)
144 struct outp_names *n = xmalloc (sizeof *n);
145 n->name = xmalloc (ep - bp + 1);
146 memcpy (n->name, bp, ep - bp);
147 n->name[ep - bp] = 0;
149 n->next = outp_configure_vec;
151 if (outp_configure_vec)
152 outp_configure_vec->prev = n;
153 outp_configure_vec = n;
156 /* Checks that outp_configure_vec is empty, bitches & clears it if it
159 check_configure_vec (void)
161 struct outp_names *n;
163 for (n = outp_configure_vec; n; n = n->next)
164 if (n->source == OUTP_S_COMMAND_LINE)
165 error (0, 0, _("unknown output driver `%s'"), n->name);
167 error (0, 0, _("output driver `%s' referenced but never defined"),
169 outp_configure_clear ();
172 /* Searches outp_configure_vec for the name between BP and EP
173 exclusive. If found, it is deleted, then replaced by the names
174 given in EP+1, if any. */
176 expand_name (char *bp, char *ep)
178 struct outp_names *n = search_names (bp, ep);
186 while (isspace ((unsigned char) *bp))
189 while (*ep && !isspace ((unsigned char) *ep))
193 if (!search_names (bp, ep))
194 add_name (bp, ep, OUTP_S_INIT_FILE);
199 /* Looks for a macro with key KEY, and returns the corresponding value
200 if found, or NULL if not. */
202 find_defn_value (const char *key)
204 static char buf[INT_STRLEN_BOUND (int) + 1];
207 for (d = outp_macros; d; d = d->next)
208 if (!strcmp (key, d->key))
209 return ds_cstr (&d->value);
210 if (!strcmp (key, "viewwidth"))
212 sprintf (buf, "%d", get_viewwidth ());
215 else if (!strcmp (key, "viewlength"))
217 sprintf (buf, "%d", get_viewlength ());
224 /* Initializes global variables. */
228 extern struct outp_class ascii_class;
229 extern struct outp_class postscript_class;
231 char def[] = "default";
233 add_class (&html_class);
234 add_class (&postscript_class);
235 add_class (&ascii_class);
237 add_name (def, &def[strlen (def)], OUTP_S_INIT_FILE);
240 /* Deletes all the output macros. */
244 struct outp_defn *d, *next;
246 for (d = outp_macros; d; d = next)
250 ds_destroy (&d->value);
256 init_default_drivers (void)
258 error (0, 0, _("using default output driver configuration"));
259 configure_driver (ss_cstr ("list"),
262 ss_cstr ("length=66 width=79 output-file=\"pspp.list\""));
265 /* Reads the initialization file; initializes
268 outp_read_devices (void)
278 init_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_INIT_FILE",
280 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
283 ds_init_empty (&line);
287 error (0, 0, _("cannot find output initialization file "
288 "(use `-vv' to view search path)"));
292 f = fopen (init_fn, "r");
295 error (0, errno, _("cannot open \"%s\""), init_fn);
304 if (!ds_read_config_line (&line, &line_number, f))
307 error (0, errno, _("reading \"%s\""), init_fn);
310 for (cp = ds_cstr (&line); isspace ((unsigned char) *cp); cp++);
311 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
312 outp_configure_macro (&cp[7]);
316 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
318 expand_name (cp, ep);
321 struct outp_names *n = search_names (cp, ep);
324 configure_driver_line (ds_ss (&line));
329 error_at_line (0, 0, init_fn, line_number, _("syntax error"));
334 check_configure_vec ();
337 if (f && -1 == fclose (f))
338 error (0, errno, _("error closing \"%s\""), init_fn);
345 if (outp_driver_list == NULL)
346 error (0, 0, _("no active output drivers"));
349 error (0, 0, _("error reading device definition file"));
351 if (!result || outp_driver_list == NULL)
352 init_default_drivers ();
355 /* Clear the list of drivers to configure. */
357 outp_configure_clear (void)
359 struct outp_names *n, *next;
361 for (n = outp_configure_vec; n; n = next)
367 outp_configure_vec = NULL;
370 /* Adds the name BP to the list of drivers to configure into
373 outp_configure_add (char *bp)
375 char *ep = &bp[strlen (bp)];
376 if (!search_names (bp, ep))
377 add_name (bp, ep, OUTP_S_COMMAND_LINE);
380 /* Defines one configuration macro based on the text in BP, which
381 should be of the form `KEY=VALUE'. */
383 outp_configure_macro (char *bp)
388 while (isspace ((unsigned char) *bp))
391 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
394 d = xmalloc (sizeof *d);
395 d->key = xmalloc (ep - bp + 1);
396 memcpy (d->key, bp, ep - bp);
399 /* Earlier definitions for a particular KEY override later ones. */
400 if (find_defn_value (d->key))
409 while (isspace ((unsigned char) *ep))
412 ds_init_cstr (&d->value, ep);
413 fn_interp_vars (ds_ss (&d->value), find_defn_value, &d->value);
414 d->next = outp_macros;
417 outp_macros->prev = d;
421 /* Destroys all the drivers in driver list *DL and sets *DL to
424 destroy_list (struct outp_driver ** dl)
426 struct outp_driver *d, *next;
428 for (d = *dl; d; d = next)
437 /* Closes all the output drivers. */
441 struct outp_driver_class_list *n = outp_class_list ;
442 destroy_list (&outp_driver_list);
446 struct outp_driver_class_list *next = n->next;
450 outp_class_list = NULL;
455 free (outp_subtitle);
456 outp_subtitle = NULL;
459 /* Display on stdout a list of all registered driver classes. */
461 outp_list_classes (void)
463 int width = get_viewwidth();
464 struct outp_driver_class_list *c;
466 printf (_("Driver classes:\n\t"));
468 for (c = outp_class_list; c; c = c->next)
470 if ((int) strlen (c->class->name) + 1 > width)
473 width = get_viewwidth() - 8;
477 fputs (c->class->name, stdout);
482 /* Obtains a token from S and advances its position. Errors are
483 reported against the given DRIVER_NAME.
484 The token is stored in TOKEN. Returns true if successful,
485 false on syntax error.
487 Caller is responsible for skipping leading spaces. */
489 get_option_token (struct substring *s, const char *driver_name,
490 struct string *token)
498 error (0, 0, _("syntax error parsing options for \"%s\" driver"),
502 else if (c == '\'' || c == '"')
514 _("reached end of options inside quoted string "
515 "parsing options for \"%s\" driver"),
520 ds_put_char (token, c);
567 while (ss_first (*s) >= '0' && ss_first (*s) <= '7')
568 out = c * 8 + (ss_get_char (s) - '0');
573 while (isxdigit (ss_first (*s)))
580 out += tolower (c) - 'a' + 10;
584 error (0, 0, _("syntax error in string constant "
585 "parsing options for \"%s\" driver"),
589 ds_put_char (token, out);
597 ds_put_char (token, c);
600 if (c == EOF || c == '=' || isspace (c))
610 outp_parse_options (struct substring options,
611 bool (*callback) (struct outp_driver *, const char *key,
612 const struct string *value),
613 struct outp_driver *driver)
615 struct string key = DS_EMPTY_INITIALIZER;
616 struct string value = DS_EMPTY_INITIALIZER;
617 struct substring left = options;
622 ss_ltrim (&left, ss_cstr (CC_SPACES));
623 if (ss_is_empty (left))
626 if (!get_option_token (&left, driver->name, &key))
629 ss_ltrim (&left, ss_cstr (CC_SPACES));
630 if (!ss_match_char (&left, '='))
632 error (0, 0, _("syntax error expecting `=' "
633 "parsing options for driver \"%s\""),
638 ss_ltrim (&left, ss_cstr (CC_SPACES));
639 if (!get_option_token (&left, driver->name, &value))
642 ok = callback (driver, ds_cstr (&key), &value);
652 /* Find the driver in outp_driver_list with name NAME. */
653 static struct outp_driver *
654 find_driver (char *name)
656 struct outp_driver *d;
658 for (d = outp_driver_list; d; d = d->next)
659 if (!strcmp (d->name, name))
664 /* Adds a driver to outp_driver_list pursuant to the
665 specification provided. */
667 configure_driver (struct substring driver_name, struct substring class_name,
668 struct substring device_type, struct substring options)
670 struct outp_driver *d, *iter;
671 struct outp_driver_class_list *c;
673 struct substring token;
678 for (c = outp_class_list; c; c = c->next)
679 if (!ss_compare (ss_cstr (c->class->name), class_name))
683 error (0, 0, _("unknown output driver class `%.*s'"),
684 (int) ss_length (class_name), ss_data (class_name));
688 /* Parse device type. */
690 while (ss_tokenize (device_type, ss_cstr (CC_SPACES), &save_idx, &token))
691 if (!ss_compare (token, ss_cstr ("listing")))
692 device |= OUTP_DEV_LISTING;
693 else if (!ss_compare (token, ss_cstr ("screen")))
694 device |= OUTP_DEV_SCREEN;
695 else if (!ss_compare (token, ss_cstr ("printer")))
696 device |= OUTP_DEV_PRINTER;
698 error (0, 0, _("unknown device type `%.*s'"),
699 (int) ss_length (token), ss_data (token));
701 /* Open the device. */
702 d = xmalloc (sizeof *d);
703 d->next = d->prev = NULL;
705 d->name = ss_xstrdup (driver_name);
706 d->page_open = false;
707 d->device = OUTP_DEV_NONE;
708 d->cp_x = d->cp_y = 0;
713 if (!d->class->open_driver (d, options))
715 error (0, 0, _("cannot initialize output driver `%s' of class `%s'"),
716 d->name, d->class->name);
722 /* Find like-named driver and delete. */
723 iter = find_driver (d->name);
725 destroy_driver (iter);
728 d->next = outp_driver_list;
730 if (outp_driver_list != NULL)
731 outp_driver_list->prev = d;
732 outp_driver_list = d;
735 /* String LINE is in format:
736 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
737 Adds a driver to outp_driver_list pursuant to the specification
740 configure_driver_line (struct substring line_)
742 struct string line = DS_EMPTY_INITIALIZER;
743 struct substring tokens[4];
747 fn_interp_vars (line_, find_defn_value, &line);
750 for (i = 0; i < 4; i++)
752 struct substring *token = &tokens[i];
753 ds_separate (&line, ss_cstr (i < 3 ? ":" : ""), &save_idx, token);
754 ss_trim (token, ss_cstr (CC_SPACES));
757 if (!ss_is_empty (tokens[0]) && !ss_is_empty (tokens[1]))
758 configure_driver (tokens[0], tokens[1], tokens[2], tokens[3]);
761 _("driver definition line missing driver name or class name"));
766 /* Destroys output driver D. */
768 destroy_driver (struct outp_driver *d)
773 struct outp_driver_class_list *c;
775 d->class->close_driver (d);
777 for (c = outp_class_list; c; c = c->next)
778 if (c->class == d->class)
784 /* Remove this driver from the global driver list. */
786 d->prev->next = d->next;
788 d->next->prev = d->prev;
789 if (d == outp_driver_list)
790 outp_driver_list = d->next;
793 /* Tries to match S as one of the keywords in TAB, with
794 corresponding information structure INFO. Returns category
795 code and stores subcategory in *SUBCAT on success. Returns -1
798 outp_match_keyword (const char *s, const struct outp_option *tab, int *subcat)
800 for (; tab->keyword != NULL; tab++)
801 if (!strcmp (s, tab->keyword))
803 *subcat = tab->subcat;
809 /* Encapsulate two characters in a single int. */
810 #define TWO_CHARS(A, B) \
813 /* Determines the size of a dimensional measurement and returns the
814 size in units of 1/72000". Units if not specified explicitly are
815 inches for values under 50, millimeters otherwise. Returns 0,
816 stores NULL to *TAIL on error; otherwise returns dimension, stores
819 outp_evaluate_dimension (char *dimen, char **tail)
825 value = strtod (s, &ptail);
832 b = strtod (s, &ptail);
833 if (b <= 0.0 || ptail == s)
838 c = strtod (s, &ptail);
839 if (c <= 0.0 || ptail == s)
849 else if (*ptail == '/')
853 b = strtod (s, &ptail);
854 if (b <= 0.0 || ptail == s)
861 if (*s == 0 || isspace ((unsigned char) *s))
866 value *= 72000 / 25.4;
872 /* Standard TeX units are supported. */
876 switch (TWO_CHARS (s[0], s[1]))
878 case TWO_CHARS ('p', 't'):
879 factor = 72000 / 72.27;
881 case TWO_CHARS ('p', 'c'):
882 factor = 72000 / 72.27 * 12.0;
884 case TWO_CHARS ('i', 'n'):
887 case TWO_CHARS ('b', 'p'):
888 factor = 72000 / 72.0;
890 case TWO_CHARS ('c', 'm'):
891 factor = 72000 / 2.54;
893 case TWO_CHARS ('m', 'm'):
894 factor = 72000 / 25.4;
896 case TWO_CHARS ('d', 'd'):
897 factor = 72000 / 72.27 * 1.0700086;
899 case TWO_CHARS ('c', 'c'):
900 factor = 72000 / 72.27 * 12.840104;
902 case TWO_CHARS ('s', 'p'):
903 factor = 72000 / 72.27 / 65536.0;
907 _("unit \"%s\" is unknown in dimension \"%s\""), s, dimen);
922 error (0, 0, _("bad dimension \"%s\""), dimen);
926 /* Stores the dimensions in 1/72000" units of paper identified by
927 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
928 of HORZ and VERT are dimensions, into *H and *V. Return true on
931 internal_get_paper_size (char *size, int *h, int *v)
935 while (isspace ((unsigned char) *size))
937 *h = outp_evaluate_dimension (size, &tail);
940 while (isspace ((unsigned char) *tail))
944 else if (*tail == 'b' && tail[1] == 'y')
948 error (0, 0, _("`x' expected in paper size `%s'"), size);
951 *v = outp_evaluate_dimension (tail, &tail);
954 while (isspace ((unsigned char) *tail))
958 error (0, 0, _("trailing garbage `%s' on paper size `%s'"), tail, size);
965 /* Stores the dimensions, in 1/72000" units, of paper identified by
966 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
967 V', or it may be a case-insensitive paper identifier, which is
968 looked up in the `papersize' configuration file. Returns true
969 on success. May modify SIZE. */
970 /* Don't read further unless you've got a strong stomach. */
972 outp_get_paper_size (char *size, int *h, int *v)
987 bool free_it = false;
991 while (isspace ((unsigned char) *size))
993 if (isdigit ((unsigned char) *size))
994 return internal_get_paper_size (size, h, v);
998 while (isspace ((unsigned char) *ep) && ep >= size)
1002 error (0, 0, _("paper size name cannot be empty"));
1010 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1012 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1015 ds_init_empty (&line);
1017 if (pprsz_fn == NULL)
1019 error (0, 0, _("cannot find `papersize' configuration file"));
1023 f = fopen (pprsz_fn, "r");
1026 error (0, errno, _("error opening \"%s\""), pprsz_fn);
1032 struct substring p, name;
1034 if (!ds_read_config_line (&line, &line_number, f))
1037 error (0, errno, _("error reading \"%s\""), pprsz_fn);
1042 ss_ltrim (&p, ss_cstr (CC_SPACES));
1043 if (!ss_match_char (&p, '"') || !ss_get_until (&p, '"', &name))
1045 if (ss_compare (name, ss_cstr (size)))
1048 ss_ltrim (&p, ss_cstr (CC_SPACES));
1049 if (ss_match_char (&p, '='))
1053 ss_trim (&p, ss_cstr (CC_SPACES));
1054 size = ss_xstrdup (p);
1062 error_at_line (0, 0, pprsz_fn, line_number,
1063 _("syntax error in paper size definition"));
1066 /* We found the one we want! */
1067 result = internal_get_paper_size (size, h, v);
1075 error (0, 0, _("error reading paper size definition file"));
1080 /* If D is NULL, returns the first enabled driver if any, NULL if
1081 none. Otherwise D must be the last driver returned by this
1082 function, in which case the next enabled driver is returned or NULL
1083 if that was the last. */
1084 struct outp_driver *
1085 outp_drivers (struct outp_driver *d)
1090 d = outp_driver_list;
1095 || (d->device == 0 || (d->device & disabled_devices) != d->device))
1102 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1103 device(s) given in mask DEVICE. */
1105 outp_enable_device (int enable, int device)
1108 disabled_devices &= ~device;
1110 disabled_devices |= device;
1113 /* Opens a page on driver D (if one is not open). */
1115 outp_open_page (struct outp_driver *d)
1119 d->cp_x = d->cp_y = 0;
1121 d->page_open = true;
1122 if (d->class->open_page != NULL)
1123 d->class->open_page (d);
1127 /* Closes the page on driver D (if one is open). */
1129 outp_close_page (struct outp_driver *d)
1133 if (d->class->close_page != NULL)
1134 d->class->close_page (d);
1135 d->page_open = false;
1139 /* Ejects the page on device D, if a page is open and non-blank,
1140 and opens a new page. */
1142 outp_eject_page (struct outp_driver *d)
1144 if (d->page_open && d->cp_y != 0)
1145 outp_close_page (d);
1149 /* Returns the width of string S, in device units, when output on
1152 outp_string_width (struct outp_driver *d, const char *s, enum outp_font font)
1154 struct outp_text text;
1158 text.justification = OUTP_LEFT;
1159 text.string = ss_cstr (s);
1160 text.h = text.v = INT_MAX;
1161 d->class->text_metrics (d, &text, &width, NULL);