1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <libpspp/alloc.h>
26 #include <data/file-name.h>
29 #include <libpspp/misc.h>
30 #include <data/settings.h>
31 #include <libpspp/str.h>
35 #define _(msgid) gettext (msgid)
37 /* FIXME? Should the output configuration format be changed to
38 drivername:classname:devicetype:options, where devicetype is zero
39 or more of screen, printer, listing? */
41 /* FIXME: Have the reentrancy problems been solved? */
43 /* Where the output driver name came from. */
46 OUTP_S_COMMAND_LINE, /* Specified by the user. */
47 OUTP_S_INIT_FILE /* `default' or the init file. */
50 /* Names the output drivers to be used. */
53 char *name; /* Name of the output driver. */
54 int source; /* OUTP_S_* */
55 struct outp_names *next, *prev;
58 /* Defines an init file macro. */
63 struct outp_defn *next, *prev;
66 static struct outp_defn *outp_macros;
67 static struct outp_names *outp_configure_vec;
69 /* A list of driver classes. */
70 struct outp_driver_class_list
72 const struct outp_class *class;
73 struct outp_driver_class_list *next;
76 static struct outp_driver_class_list *outp_class_list;
77 static struct outp_driver *outp_driver_list;
82 /* A set of OUTP_DEV_* bits indicating the devices that are
84 static int disabled_devices;
86 static void destroy_driver (struct outp_driver *);
87 static void configure_driver_line (struct substring);
88 static void configure_driver (const struct substring, const struct substring,
89 const struct substring, const struct substring);
91 /* Add a class to the class list. */
93 add_class (const struct outp_class *class)
95 struct outp_driver_class_list *new_list = xmalloc (sizeof *new_list);
97 new_list->class = class;
101 outp_class_list = new_list;
102 new_list->next = NULL;
106 new_list->next = outp_class_list;
107 outp_class_list = new_list;
111 /* Finds the outp_names in outp_configure_vec with name between BP and
113 static struct outp_names *
114 search_names (char *bp, char *ep)
116 struct outp_names *n;
118 for (n = outp_configure_vec; n; n = n->next)
119 if ((int) strlen (n->name) == ep - bp && !memcmp (n->name, bp, ep - bp))
124 /* Deletes outp_names NAME from outp_configure_vec. */
126 delete_name (struct outp_names * n)
130 n->prev->next = n->next;
132 n->next->prev = n->prev;
133 if (n == outp_configure_vec)
134 outp_configure_vec = n->next;
138 /* Adds the name between BP and EP exclusive to list
139 outp_configure_vec with source SOURCE. */
141 add_name (char *bp, char *ep, int source)
143 struct outp_names *n = xmalloc (sizeof *n);
144 n->name = xmalloc (ep - bp + 1);
145 memcpy (n->name, bp, ep - bp);
146 n->name[ep - bp] = 0;
148 n->next = outp_configure_vec;
150 if (outp_configure_vec)
151 outp_configure_vec->prev = n;
152 outp_configure_vec = n;
155 /* Checks that outp_configure_vec is empty, bitches & clears it if it
158 check_configure_vec (void)
160 struct outp_names *n;
162 for (n = outp_configure_vec; n; n = n->next)
163 if (n->source == OUTP_S_COMMAND_LINE)
164 error (0, 0, _("unknown output driver `%s'"), n->name);
166 error (0, 0, _("output driver `%s' referenced but never defined"),
168 outp_configure_clear ();
171 /* Searches outp_configure_vec for the name between BP and EP
172 exclusive. If found, it is deleted, then replaced by the names
173 given in EP+1, if any. */
175 expand_name (char *bp, char *ep)
177 struct outp_names *n = search_names (bp, ep);
185 while (isspace ((unsigned char) *bp))
188 while (*ep && !isspace ((unsigned char) *ep))
192 if (!search_names (bp, ep))
193 add_name (bp, ep, OUTP_S_INIT_FILE);
198 /* Looks for a macro with key KEY, and returns the corresponding value
199 if found, or NULL if not. */
201 find_defn_value (const char *key)
203 static char buf[INT_STRLEN_BOUND (int) + 1];
206 for (d = outp_macros; d; d = d->next)
207 if (!strcmp (key, d->key))
208 return ds_cstr (&d->value);
209 if (!strcmp (key, "viewwidth"))
211 sprintf (buf, "%d", get_viewwidth ());
214 else if (!strcmp (key, "viewlength"))
216 sprintf (buf, "%d", get_viewlength ());
223 /* Initializes global variables. */
227 extern struct outp_class ascii_class;
228 extern struct outp_class postscript_class;
230 char def[] = "default";
232 add_class (&html_class);
233 add_class (&postscript_class);
234 add_class (&ascii_class);
236 add_name (def, &def[strlen (def)], OUTP_S_INIT_FILE);
239 /* Deletes all the output macros. */
243 struct outp_defn *d, *next;
245 for (d = outp_macros; d; d = next)
249 ds_destroy (&d->value);
255 init_default_drivers (void)
257 error (0, 0, _("using default output driver configuration"));
258 configure_driver (ss_cstr ("list"),
261 ss_cstr ("length=66 width=79 output-file=\"pspp.list\""));
264 /* Reads the initialization file; initializes
267 outp_read_devices (void)
277 init_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_INIT_FILE",
279 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
282 ds_init_empty (&line);
286 error (0, 0, _("cannot find output initialization file "
287 "(use `-vv' to view search path)"));
291 f = fopen (init_fn, "r");
294 error (0, errno, _("cannot open \"%s\""), init_fn);
303 if (!ds_read_config_line (&line, &line_number, f))
306 error (0, errno, _("reading \"%s\""), init_fn);
309 for (cp = ds_cstr (&line); isspace ((unsigned char) *cp); cp++);
310 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
311 outp_configure_macro (&cp[7]);
315 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
317 expand_name (cp, ep);
320 struct outp_names *n = search_names (cp, ep);
323 configure_driver_line (ds_ss (&line));
328 error_at_line (0, 0, init_fn, line_number, _("syntax error"));
333 check_configure_vec ();
336 if (f && -1 == fclose (f))
337 error (0, errno, _("error closing \"%s\""), init_fn);
344 if (outp_driver_list == NULL)
345 error (0, 0, _("no active output drivers"));
348 error (0, 0, _("error reading device definition file"));
350 if (!result || outp_driver_list == NULL)
351 init_default_drivers ();
354 /* Clear the list of drivers to configure. */
356 outp_configure_clear (void)
358 struct outp_names *n, *next;
360 for (n = outp_configure_vec; n; n = next)
366 outp_configure_vec = NULL;
369 /* Adds the name BP to the list of drivers to configure into
372 outp_configure_add (char *bp)
374 char *ep = &bp[strlen (bp)];
375 if (!search_names (bp, ep))
376 add_name (bp, ep, OUTP_S_COMMAND_LINE);
379 /* Defines one configuration macro based on the text in BP, which
380 should be of the form `KEY=VALUE'. */
382 outp_configure_macro (char *bp)
387 while (isspace ((unsigned char) *bp))
390 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
393 d = xmalloc (sizeof *d);
394 d->key = xmalloc (ep - bp + 1);
395 memcpy (d->key, bp, ep - bp);
398 /* Earlier definitions for a particular KEY override later ones. */
399 if (find_defn_value (d->key))
408 while (isspace ((unsigned char) *ep))
411 ds_init_cstr (&d->value, ep);
412 fn_interp_vars (ds_ss (&d->value), find_defn_value, &d->value);
413 d->next = outp_macros;
416 outp_macros->prev = d;
420 /* Destroys all the drivers in driver list *DL and sets *DL to
423 destroy_list (struct outp_driver ** dl)
425 struct outp_driver *d, *next;
427 for (d = *dl; d; d = next)
436 /* Closes all the output drivers. */
440 struct outp_driver_class_list *n = outp_class_list ;
441 destroy_list (&outp_driver_list);
445 struct outp_driver_class_list *next = n->next;
449 outp_class_list = NULL;
454 free (outp_subtitle);
455 outp_subtitle = NULL;
458 /* Display on stdout a list of all registered driver classes. */
460 outp_list_classes (void)
462 int width = get_viewwidth();
463 struct outp_driver_class_list *c;
465 printf (_("Driver classes:\n\t"));
467 for (c = outp_class_list; c; c = c->next)
469 if ((int) strlen (c->class->name) + 1 > width)
472 width = get_viewwidth() - 8;
476 fputs (c->class->name, stdout);
481 /* Obtains a token from S and advances its position. Errors are
482 reported against the given DRIVER_NAME.
483 The token is stored in TOKEN. Returns true if successful,
484 false on syntax error.
486 Caller is responsible for skipping leading spaces. */
488 get_option_token (struct substring *s, const char *driver_name,
489 struct string *token)
497 error (0, 0, _("syntax error parsing options for \"%s\" driver"),
501 else if (c == '\'' || c == '"')
513 _("reached end of options inside quoted string "
514 "parsing options for \"%s\" driver"),
519 ds_put_char (token, c);
566 while (ss_first (*s) >= '0' && ss_first (*s) <= '7')
567 out = c * 8 + (ss_get_char (s) - '0');
572 while (isxdigit (ss_first (*s)))
579 out += tolower (c) - 'a' + 10;
583 error (0, 0, _("syntax error in string constant "
584 "parsing options for \"%s\" driver"),
588 ds_put_char (token, out);
596 ds_put_char (token, c);
599 if (c == EOF || c == '=' || isspace (c))
609 outp_parse_options (struct substring options,
610 bool (*callback) (struct outp_driver *, const char *key,
611 const struct string *value),
612 struct outp_driver *driver)
614 struct string key = DS_EMPTY_INITIALIZER;
615 struct string value = DS_EMPTY_INITIALIZER;
616 struct substring left = options;
621 ss_ltrim (&left, ss_cstr (CC_SPACES));
622 if (ss_is_empty (left))
625 if (!get_option_token (&left, driver->name, &key))
628 ss_ltrim (&left, ss_cstr (CC_SPACES));
629 if (!ss_match_char (&left, '='))
631 error (0, 0, _("syntax error expecting `=' "
632 "parsing options for driver \"%s\""),
637 ss_ltrim (&left, ss_cstr (CC_SPACES));
638 if (!get_option_token (&left, driver->name, &value))
641 ok = callback (driver, ds_cstr (&key), &value);
651 /* Find the driver in outp_driver_list with name NAME. */
652 static struct outp_driver *
653 find_driver (char *name)
655 struct outp_driver *d;
657 for (d = outp_driver_list; d; d = d->next)
658 if (!strcmp (d->name, name))
663 /* Adds a driver to outp_driver_list pursuant to the
664 specification provided. */
666 configure_driver (struct substring driver_name, struct substring class_name,
667 struct substring device_type, struct substring options)
669 struct outp_driver *d, *iter;
670 struct outp_driver_class_list *c;
672 struct substring token;
677 for (c = outp_class_list; c; c = c->next)
678 if (!ss_compare (ss_cstr (c->class->name), class_name))
682 error (0, 0, _("unknown output driver class `%.*s'"),
683 (int) ss_length (class_name), ss_data (class_name));
687 /* Parse device type. */
689 while (ss_tokenize (device_type, ss_cstr (CC_SPACES), &save_idx, &token))
690 if (!ss_compare (token, ss_cstr ("listing")))
691 device |= OUTP_DEV_LISTING;
692 else if (!ss_compare (token, ss_cstr ("screen")))
693 device |= OUTP_DEV_SCREEN;
694 else if (!ss_compare (token, ss_cstr ("printer")))
695 device |= OUTP_DEV_PRINTER;
697 error (0, 0, _("unknown device type `%.*s'"),
698 (int) ss_length (token), ss_data (token));
700 /* Open the device. */
701 d = xmalloc (sizeof *d);
702 d->next = d->prev = NULL;
704 d->name = ss_xstrdup (driver_name);
705 d->page_open = false;
706 d->device = OUTP_DEV_NONE;
707 d->cp_x = d->cp_y = 0;
712 if (!d->class->open_driver (d, options))
714 error (0, 0, _("cannot initialize output driver `%s' of class `%s'"),
715 d->name, d->class->name);
721 /* Find like-named driver and delete. */
722 iter = find_driver (d->name);
724 destroy_driver (iter);
727 d->next = outp_driver_list;
729 if (outp_driver_list != NULL)
730 outp_driver_list->prev = d;
731 outp_driver_list = d;
734 /* String LINE is in format:
735 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
736 Adds a driver to outp_driver_list pursuant to the specification
739 configure_driver_line (struct substring line_)
741 struct string line = DS_EMPTY_INITIALIZER;
742 struct substring tokens[4];
746 fn_interp_vars (line_, find_defn_value, &line);
749 for (i = 0; i < 4; i++)
751 struct substring *token = &tokens[i];
752 ds_separate (&line, ss_cstr (i < 3 ? ":" : ""), &save_idx, token);
753 ss_trim (token, ss_cstr (CC_SPACES));
756 if (!ss_is_empty (tokens[0]) && !ss_is_empty (tokens[1]))
757 configure_driver (tokens[0], tokens[1], tokens[2], tokens[3]);
760 _("driver definition line missing driver name or class name"));
765 /* Destroys output driver D. */
767 destroy_driver (struct outp_driver *d)
772 struct outp_driver_class_list *c;
774 d->class->close_driver (d);
776 for (c = outp_class_list; c; c = c->next)
777 if (c->class == d->class)
783 /* Remove this driver from the global driver list. */
785 d->prev->next = d->next;
787 d->next->prev = d->prev;
788 if (d == outp_driver_list)
789 outp_driver_list = d->next;
792 /* Tries to match S as one of the keywords in TAB, with
793 corresponding information structure INFO. Returns category
794 code and stores subcategory in *SUBCAT on success. Returns -1
797 outp_match_keyword (const char *s, const struct outp_option *tab, int *subcat)
799 for (; tab->keyword != NULL; tab++)
800 if (!strcmp (s, tab->keyword))
802 *subcat = tab->subcat;
808 /* Encapsulate two characters in a single int. */
809 #define TWO_CHARS(A, B) \
812 /* Determines the size of a dimensional measurement and returns the
813 size in units of 1/72000". Units if not specified explicitly are
814 inches for values under 50, millimeters otherwise. Returns 0,
815 stores NULL to *TAIL on error; otherwise returns dimension, stores
818 outp_evaluate_dimension (char *dimen, char **tail)
824 value = strtod (s, &ptail);
831 b = strtod (s, &ptail);
832 if (b <= 0.0 || ptail == s)
837 c = strtod (s, &ptail);
838 if (c <= 0.0 || ptail == s)
848 else if (*ptail == '/')
852 b = strtod (s, &ptail);
853 if (b <= 0.0 || ptail == s)
860 if (*s == 0 || isspace ((unsigned char) *s))
865 value *= 72000 / 25.4;
871 /* Standard TeX units are supported. */
875 switch (TWO_CHARS (s[0], s[1]))
877 case TWO_CHARS ('p', 't'):
878 factor = 72000 / 72.27;
880 case TWO_CHARS ('p', 'c'):
881 factor = 72000 / 72.27 * 12.0;
883 case TWO_CHARS ('i', 'n'):
886 case TWO_CHARS ('b', 'p'):
887 factor = 72000 / 72.0;
889 case TWO_CHARS ('c', 'm'):
890 factor = 72000 / 2.54;
892 case TWO_CHARS ('m', 'm'):
893 factor = 72000 / 25.4;
895 case TWO_CHARS ('d', 'd'):
896 factor = 72000 / 72.27 * 1.0700086;
898 case TWO_CHARS ('c', 'c'):
899 factor = 72000 / 72.27 * 12.840104;
901 case TWO_CHARS ('s', 'p'):
902 factor = 72000 / 72.27 / 65536.0;
906 _("unit \"%s\" is unknown in dimension \"%s\""), s, dimen);
921 error (0, 0, _("bad dimension \"%s\""), dimen);
925 /* Stores the dimensions in 1/72000" units of paper identified by
926 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
927 of HORZ and VERT are dimensions, into *H and *V. Return true on
930 internal_get_paper_size (char *size, int *h, int *v)
934 while (isspace ((unsigned char) *size))
936 *h = outp_evaluate_dimension (size, &tail);
939 while (isspace ((unsigned char) *tail))
943 else if (*tail == 'b' && tail[1] == 'y')
947 error (0, 0, _("`x' expected in paper size `%s'"), size);
950 *v = outp_evaluate_dimension (tail, &tail);
953 while (isspace ((unsigned char) *tail))
957 error (0, 0, _("trailing garbage `%s' on paper size `%s'"), tail, size);
964 /* Stores the dimensions, in 1/72000" units, of paper identified by
965 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
966 V', or it may be a case-insensitive paper identifier, which is
967 looked up in the `papersize' configuration file. Returns true
968 on success. May modify SIZE. */
969 /* Don't read further unless you've got a strong stomach. */
971 outp_get_paper_size (char *size, int *h, int *v)
986 bool free_it = false;
990 while (isspace ((unsigned char) *size))
992 if (isdigit ((unsigned char) *size))
993 return internal_get_paper_size (size, h, v);
997 while (isspace ((unsigned char) *ep) && ep >= size)
1001 error (0, 0, _("paper size name cannot be empty"));
1009 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1011 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1014 ds_init_empty (&line);
1016 if (pprsz_fn == NULL)
1018 error (0, 0, _("cannot find `papersize' configuration file"));
1022 f = fopen (pprsz_fn, "r");
1025 error (0, errno, _("error opening \"%s\""), pprsz_fn);
1031 struct substring p, name;
1033 if (!ds_read_config_line (&line, &line_number, f))
1036 error (0, errno, _("error reading \"%s\""), pprsz_fn);
1041 ss_ltrim (&p, ss_cstr (CC_SPACES));
1042 if (!ss_match_char (&p, '"') || !ss_get_until (&p, '"', &name))
1044 if (ss_compare (name, ss_cstr (size)))
1047 ss_ltrim (&p, ss_cstr (CC_SPACES));
1048 if (ss_match_char (&p, '='))
1052 ss_trim (&p, ss_cstr (CC_SPACES));
1053 size = ss_xstrdup (p);
1061 error_at_line (0, 0, pprsz_fn, line_number,
1062 _("syntax error in paper size definition"));
1065 /* We found the one we want! */
1066 result = internal_get_paper_size (size, h, v);
1074 error (0, 0, _("error reading paper size definition file"));
1079 /* If D is NULL, returns the first enabled driver if any, NULL if
1080 none. Otherwise D must be the last driver returned by this
1081 function, in which case the next enabled driver is returned or NULL
1082 if that was the last. */
1083 struct outp_driver *
1084 outp_drivers (struct outp_driver *d)
1089 d = outp_driver_list;
1094 || (d->device == 0 || (d->device & disabled_devices) != d->device))
1101 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1102 device(s) given in mask DEVICE. */
1104 outp_enable_device (int enable, int device)
1107 disabled_devices &= ~device;
1109 disabled_devices |= device;
1112 /* Opens a page on driver D (if one is not open). */
1114 outp_open_page (struct outp_driver *d)
1118 d->cp_x = d->cp_y = 0;
1120 d->page_open = true;
1121 if (d->class->open_page != NULL)
1122 d->class->open_page (d);
1126 /* Closes the page on driver D (if one is open). */
1128 outp_close_page (struct outp_driver *d)
1132 if (d->class->close_page != NULL)
1133 d->class->close_page (d);
1134 d->page_open = false;
1138 /* Ejects the page on device D, if a page is open and non-blank,
1139 and opens a new page. */
1141 outp_eject_page (struct outp_driver *d)
1143 if (d->page_open && d->cp_y != 0)
1144 outp_close_page (d);
1148 /* Returns the width of string S, in device units, when output on
1151 outp_string_width (struct outp_driver *d, const char *s, enum outp_font font)
1153 struct outp_text text;
1157 text.justification = OUTP_LEFT;
1158 text.string = ss_cstr (s);
1159 text.h = text.v = INT_MAX;
1160 d->class->text_metrics (d, &text, &width, NULL);