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",
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 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 destroy_list (&outp_driver_list);
447 struct outp_driver_class_list *next = n->next;
451 outp_class_list = NULL;
456 free (outp_subtitle);
457 outp_subtitle = NULL;
460 /* Display on stdout a list of all registered driver classes. */
462 outp_list_classes (void)
464 int width = get_viewwidth();
465 struct outp_driver_class_list *c;
467 printf (_("Driver classes:\n\t"));
469 for (c = outp_class_list; c; c = c->next)
471 if ((int) strlen (c->class->name) + 1 > width)
474 width = get_viewwidth() - 8;
478 fputs (c->class->name, stdout);
483 /* Obtains a token from S and advances its position. Errors are
484 reported against the given DRIVER_NAME.
485 The token is stored in TOKEN. Returns true if successful,
486 false on syntax error.
488 Caller is responsible for skipping leading spaces. */
490 get_option_token (struct substring *s, const char *driver_name,
491 struct string *token)
499 error (0, 0, _("syntax error parsing options for \"%s\" driver"),
503 else if (c == '\'' || c == '"')
515 _("reached end of options inside quoted string "
516 "parsing options for \"%s\" driver"),
521 ds_put_char (token, c);
568 while (ss_first (*s) >= '0' && ss_first (*s) <= '7')
569 out = c * 8 + (ss_get_char (s) - '0');
574 while (isxdigit (ss_first (*s)))
581 out += tolower (c) - 'a' + 10;
585 error (0, 0, _("syntax error in string constant "
586 "parsing options for \"%s\" driver"),
590 ds_put_char (token, out);
598 ds_put_char (token, c);
601 if (c == EOF || c == '=' || isspace (c))
611 outp_parse_options (struct substring options,
612 bool (*callback) (struct outp_driver *, const char *key,
613 const struct string *value),
614 struct outp_driver *driver)
616 struct string key = DS_EMPTY_INITIALIZER;
617 struct string value = DS_EMPTY_INITIALIZER;
618 struct substring left = options;
623 ss_ltrim (&left, ss_cstr (CC_SPACES));
624 if (ss_is_empty (left))
627 if (!get_option_token (&left, driver->name, &key))
630 ss_ltrim (&left, ss_cstr (CC_SPACES));
631 if (!ss_match_char (&left, '='))
633 error (0, 0, _("syntax error expecting `=' "
634 "parsing options for driver \"%s\""),
639 ss_ltrim (&left, ss_cstr (CC_SPACES));
640 if (!get_option_token (&left, driver->name, &value))
643 ok = callback (driver, ds_cstr (&key), &value);
653 /* Find the driver in outp_driver_list with name NAME. */
654 static struct outp_driver *
655 find_driver (char *name)
657 struct outp_driver *d;
659 for (d = outp_driver_list; d; d = d->next)
660 if (!strcmp (d->name, name))
665 /* Adds a driver to outp_driver_list pursuant to the
666 specification provided. */
668 configure_driver (struct substring driver_name, struct substring class_name,
669 struct substring device_type, struct substring options)
671 struct outp_driver *d, *iter;
672 struct outp_driver_class_list *c;
674 struct substring token;
679 for (c = outp_class_list; c; c = c->next)
680 if (!ss_compare (ss_cstr (c->class->name), class_name))
684 error (0, 0, _("unknown output driver class `%.*s'"),
685 (int) ss_length (class_name), ss_data (class_name));
689 /* Parse device type. */
691 while (ss_tokenize (device_type, ss_cstr (CC_SPACES), &save_idx, &token))
692 if (!ss_compare (token, ss_cstr ("listing")))
693 device |= OUTP_DEV_LISTING;
694 else if (!ss_compare (token, ss_cstr ("screen")))
695 device |= OUTP_DEV_SCREEN;
696 else if (!ss_compare (token, ss_cstr ("printer")))
697 device |= OUTP_DEV_PRINTER;
699 error (0, 0, _("unknown device type `%.*s'"),
700 (int) ss_length (token), ss_data (token));
702 /* Open the device. */
703 d = xmalloc (sizeof *d);
704 d->next = d->prev = NULL;
706 d->name = ss_xstrdup (driver_name);
707 d->page_open = false;
708 d->device = OUTP_DEV_NONE;
709 d->cp_x = d->cp_y = 0;
714 if (!d->class->open_driver (d, options))
716 error (0, 0, _("cannot initialize output driver `%s' of class `%s'"),
717 d->name, d->class->name);
723 /* Find like-named driver and delete. */
724 iter = find_driver (d->name);
726 destroy_driver (iter);
729 d->next = outp_driver_list;
731 if (outp_driver_list != NULL)
732 outp_driver_list->prev = d;
733 outp_driver_list = d;
736 /* String LINE is in format:
737 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
738 Adds a driver to outp_driver_list pursuant to the specification
741 configure_driver_line (struct substring line_)
743 struct string line = DS_EMPTY_INITIALIZER;
744 struct substring tokens[4];
748 fn_interp_vars (line_, find_defn_value, &line);
751 for (i = 0; i < 4; i++)
753 struct substring *token = &tokens[i];
754 ds_separate (&line, ss_cstr (i < 3 ? ":" : ""), &save_idx, token);
755 ss_trim (token, ss_cstr (CC_SPACES));
758 if (!ss_is_empty (tokens[0]) && !ss_is_empty (tokens[1]))
759 configure_driver (tokens[0], tokens[1], tokens[2], tokens[3]);
762 _("driver definition line missing driver name or class name"));
767 /* Destroys output driver D. */
769 destroy_driver (struct outp_driver *d)
774 struct outp_driver_class_list *c;
776 d->class->close_driver (d);
778 for (c = outp_class_list; c; c = c->next)
779 if (c->class == d->class)
785 /* Remove this driver from the global driver list. */
787 d->prev->next = d->next;
789 d->next->prev = d->prev;
790 if (d == outp_driver_list)
791 outp_driver_list = d->next;
794 /* Tries to match S as one of the keywords in TAB, with
795 corresponding information structure INFO. Returns category
796 code and stores subcategory in *SUBCAT on success. Returns -1
799 outp_match_keyword (const char *s, const struct outp_option *tab, int *subcat)
801 for (; tab->keyword != NULL; tab++)
802 if (!strcmp (s, tab->keyword))
804 *subcat = tab->subcat;
810 /* Encapsulate two characters in a single int. */
811 #define TWO_CHARS(A, B) \
814 /* Determines the size of a dimensional measurement and returns the
815 size in units of 1/72000". Units if not specified explicitly are
816 inches for values under 50, millimeters otherwise. Returns 0,
817 stores NULL to *TAIL on error; otherwise returns dimension, stores
820 outp_evaluate_dimension (char *dimen, char **tail)
826 value = strtod (s, &ptail);
833 b = strtod (s, &ptail);
834 if (b <= 0.0 || ptail == s)
839 c = strtod (s, &ptail);
840 if (c <= 0.0 || ptail == s)
850 else if (*ptail == '/')
854 b = strtod (s, &ptail);
855 if (b <= 0.0 || ptail == s)
862 if (*s == 0 || isspace ((unsigned char) *s))
867 value *= 72000 / 25.4;
873 /* Standard TeX units are supported. */
877 switch (TWO_CHARS (s[0], s[1]))
879 case TWO_CHARS ('p', 't'):
880 factor = 72000 / 72.27;
882 case TWO_CHARS ('p', 'c'):
883 factor = 72000 / 72.27 * 12.0;
885 case TWO_CHARS ('i', 'n'):
888 case TWO_CHARS ('b', 'p'):
889 factor = 72000 / 72.0;
891 case TWO_CHARS ('c', 'm'):
892 factor = 72000 / 2.54;
894 case TWO_CHARS ('m', 'm'):
895 factor = 72000 / 25.4;
897 case TWO_CHARS ('d', 'd'):
898 factor = 72000 / 72.27 * 1.0700086;
900 case TWO_CHARS ('c', 'c'):
901 factor = 72000 / 72.27 * 12.840104;
903 case TWO_CHARS ('s', 'p'):
904 factor = 72000 / 72.27 / 65536.0;
908 _("unit \"%s\" is unknown in dimension \"%s\""), s, dimen);
923 error (0, 0, _("bad dimension \"%s\""), dimen);
927 /* Stores the dimensions in 1/72000" units of paper identified by
928 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
929 of HORZ and VERT are dimensions, into *H and *V. Return true on
932 internal_get_paper_size (char *size, int *h, int *v)
936 while (isspace ((unsigned char) *size))
938 *h = outp_evaluate_dimension (size, &tail);
941 while (isspace ((unsigned char) *tail))
945 else if (*tail == 'b' && tail[1] == 'y')
949 error (0, 0, _("`x' expected in paper size `%s'"), size);
952 *v = outp_evaluate_dimension (tail, &tail);
955 while (isspace ((unsigned char) *tail))
959 error (0, 0, _("trailing garbage `%s' on paper size `%s'"), tail, size);
966 /* Stores the dimensions, in 1/72000" units, of paper identified by
967 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
968 V', or it may be a case-insensitive paper identifier, which is
969 looked up in the `papersize' configuration file. Returns true
970 on success. May modify SIZE. */
971 /* Don't read further unless you've got a strong stomach. */
973 outp_get_paper_size (char *size, int *h, int *v)
988 bool free_it = false;
992 while (isspace ((unsigned char) *size))
994 if (isdigit ((unsigned char) *size))
995 return internal_get_paper_size (size, h, v);
999 while (isspace ((unsigned char) *ep) && ep >= size)
1003 error (0, 0, _("paper size name cannot be empty"));
1011 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1013 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1017 ds_init_empty (&line);
1019 if (pprsz_fn == NULL)
1021 error (0, 0, _("cannot find `papersize' configuration file"));
1025 f = fopen (pprsz_fn, "r");
1028 error (0, errno, _("error opening \"%s\""), pprsz_fn);
1034 struct substring p, name;
1036 if (!ds_read_config_line (&line, &line_number, f))
1039 error (0, errno, _("error reading \"%s\""), pprsz_fn);
1044 ss_ltrim (&p, ss_cstr (CC_SPACES));
1045 if (!ss_match_char (&p, '"') || !ss_get_until (&p, '"', &name))
1047 if (ss_compare (name, ss_cstr (size)))
1050 ss_ltrim (&p, ss_cstr (CC_SPACES));
1051 if (ss_match_char (&p, '='))
1055 ss_trim (&p, ss_cstr (CC_SPACES));
1056 size = ss_xstrdup (p);
1064 error_at_line (0, 0, pprsz_fn, line_number,
1065 _("syntax error in paper size definition"));
1068 /* We found the one we want! */
1069 result = internal_get_paper_size (size, h, v);
1077 error (0, 0, _("error reading paper size definition file"));
1082 /* If D is NULL, returns the first enabled driver if any, NULL if
1083 none. Otherwise D must be the last driver returned by this
1084 function, in which case the next enabled driver is returned or NULL
1085 if that was the last. */
1086 struct outp_driver *
1087 outp_drivers (struct outp_driver *d)
1092 d = outp_driver_list;
1097 || (d->device == 0 || (d->device & disabled_devices) != d->device))
1104 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1105 device(s) given in mask DEVICE. */
1107 outp_enable_device (int enable, int device)
1110 disabled_devices &= ~device;
1112 disabled_devices |= device;
1115 /* Opens a page on driver D (if one is not open). */
1117 outp_open_page (struct outp_driver *d)
1121 d->cp_x = d->cp_y = 0;
1123 d->page_open = true;
1124 if (d->class->open_page != NULL)
1125 d->class->open_page (d);
1129 /* Closes the page on driver D (if one is open). */
1131 outp_close_page (struct outp_driver *d)
1135 if (d->class->close_page != NULL)
1136 d->class->close_page (d);
1137 d->page_open = false;
1141 /* Ejects the page on device D, if a page is open and non-blank,
1142 and opens a new page. */
1144 outp_eject_page (struct outp_driver *d)
1146 if (d->page_open && d->cp_y != 0)
1147 outp_close_page (d);
1151 /* Returns the width of string S, in device units, when output on
1154 outp_string_width (struct outp_driver *d, const char *s, enum outp_font font)
1156 struct outp_text text;
1160 text.justification = OUTP_LEFT;
1161 text.string = ss_cstr (s);
1162 text.h = text.v = INT_MAX;
1163 d->class->text_metrics (d, &text, &width, NULL);