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 struct outp_class *class;
74 struct outp_driver_class_list *next;
77 struct outp_driver_class_list *outp_class_list;
78 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 (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;
230 extern struct outp_class html_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",
285 ds_init_empty (&line);
289 error (0, 0, _("cannot find output initialization file "
290 "(use `-vv' to view search path)"));
294 f = fopen (init_fn, "r");
297 error (0, errno, _("cannot open \"%s\""), init_fn);
306 if (!ds_read_config_line (&line, &line_number, f))
309 error (0, errno, _("reading \"%s\""), init_fn);
312 for (cp = ds_cstr (&line); isspace ((unsigned char) *cp); cp++);
313 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
314 outp_configure_macro (&cp[7]);
318 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
320 expand_name (cp, ep);
323 struct outp_names *n = search_names (cp, ep);
326 configure_driver_line (ds_ss (&line));
331 error_at_line (0, 0, init_fn, line_number, _("syntax error"));
336 check_configure_vec ();
339 if (f && -1 == fclose (f))
340 error (0, errno, _("error closing \"%s\""), init_fn);
347 if (outp_driver_list == NULL)
348 error (0, 0, _("no active output drivers"));
351 error (0, 0, _("error reading device definition file"));
353 if (!result || outp_driver_list == NULL)
354 init_default_drivers ();
357 /* Clear the list of drivers to configure. */
359 outp_configure_clear (void)
361 struct outp_names *n, *next;
363 for (n = outp_configure_vec; n; n = next)
369 outp_configure_vec = NULL;
372 /* Adds the name BP to the list of drivers to configure into
375 outp_configure_add (char *bp)
377 char *ep = &bp[strlen (bp)];
378 if (!search_names (bp, ep))
379 add_name (bp, ep, OUTP_S_COMMAND_LINE);
382 /* Defines one configuration macro based on the text in BP, which
383 should be of the form `KEY=VALUE'. */
385 outp_configure_macro (char *bp)
390 while (isspace ((unsigned char) *bp))
393 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
396 d = xmalloc (sizeof *d);
397 d->key = xmalloc (ep - bp + 1);
398 memcpy (d->key, bp, ep - bp);
401 /* Earlier definitions for a particular KEY override later ones. */
402 if (find_defn_value (d->key))
411 while (isspace ((unsigned char) *ep))
414 ds_init_cstr (&d->value, ep);
415 fn_interp_vars (ds_ss (&d->value), find_defn_value, &d->value);
416 d->next = outp_macros;
419 outp_macros->prev = d;
423 /* Destroys all the drivers in driver list *DL and sets *DL to
426 destroy_list (struct outp_driver ** dl)
428 struct outp_driver *d, *next;
430 for (d = *dl; d; d = next)
439 /* Closes all the output drivers. */
443 struct outp_driver_class_list *n = outp_class_list ;
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 = c * 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;
709 d->device = OUTP_DEV_NONE;
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 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, struct outp_option *tab, int *subcat)
802 for (; tab->keyword != NULL; tab++)
803 if (!strcmp (s, tab->keyword))
805 *subcat = tab->subcat;
811 /* Encapsulate two characters in a single int. */
812 #define TWO_CHARS(A, B) \
815 /* Determines the size of a dimensional measurement and returns the
816 size in units of 1/72000". Units if not specified explicitly are
817 inches for values under 50, millimeters otherwise. Returns 0,
818 stores NULL to *TAIL on error; otherwise returns dimension, stores
821 outp_evaluate_dimension (char *dimen, char **tail)
827 value = strtod (s, &ptail);
834 b = strtod (s, &ptail);
835 if (b <= 0.0 || ptail == s)
840 c = strtod (s, &ptail);
841 if (c <= 0.0 || ptail == s)
851 else if (*ptail == '/')
855 b = strtod (s, &ptail);
856 if (b <= 0.0 || ptail == s)
863 if (*s == 0 || isspace ((unsigned char) *s))
868 value *= 72000 / 25.4;
874 /* Standard TeX units are supported. */
878 switch (TWO_CHARS (s[0], s[1]))
880 case TWO_CHARS ('p', 't'):
881 factor = 72000 / 72.27;
883 case TWO_CHARS ('p', 'c'):
884 factor = 72000 / 72.27 * 12.0;
886 case TWO_CHARS ('i', 'n'):
889 case TWO_CHARS ('b', 'p'):
890 factor = 72000 / 72.0;
892 case TWO_CHARS ('c', 'm'):
893 factor = 72000 / 2.54;
895 case TWO_CHARS ('m', 'm'):
896 factor = 72000 / 25.4;
898 case TWO_CHARS ('d', 'd'):
899 factor = 72000 / 72.27 * 1.0700086;
901 case TWO_CHARS ('c', 'c'):
902 factor = 72000 / 72.27 * 12.840104;
904 case TWO_CHARS ('s', 'p'):
905 factor = 72000 / 72.27 / 65536.0;
909 _("unit \"%s\" is unknown in dimension \"%s\""), s, dimen);
924 error (0, 0, _("bad dimension \"%s\""), dimen);
928 /* Stores the dimensions in 1/72000" units of paper identified by
929 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
930 of HORZ and VERT are dimensions, into *H and *V. Return nonzero on
933 internal_get_paper_size (char *size, int *h, int *v)
937 while (isspace ((unsigned char) *size))
939 *h = outp_evaluate_dimension (size, &tail);
942 while (isspace ((unsigned char) *tail))
946 else if (*tail == 'b' && tail[1] == 'y')
950 error (0, 0, _("`x' expected in paper size `%s'"), size);
953 *v = outp_evaluate_dimension (tail, &tail);
956 while (isspace ((unsigned char) *tail))
960 error (0, 0, _("trailing garbage `%s' on paper size `%s'"), tail, size);
967 /* Stores the dimensions, in 1/72000" units, of paper identified by
968 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
969 V', or it may be a case-insensitive paper identifier, which is
970 looked up in the `papersize' configuration file. Returns nonzero
971 on success. May modify SIZE. */
972 /* Don't read further unless you've got a strong stomach. */
974 outp_get_paper_size (char *size, int *h, int *v)
989 bool free_it = false;
993 while (isspace ((unsigned char) *size))
995 if (isdigit ((unsigned char) *size))
996 return internal_get_paper_size (size, h, v);
1000 while (isspace ((unsigned char) *ep) && ep >= size)
1004 error (0, 0, _("paper size name cannot be empty"));
1012 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1014 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1018 ds_init_empty (&line);
1020 if (pprsz_fn == NULL)
1022 error (0, 0, _("cannot find `papersize' configuration file"));
1026 f = fopen (pprsz_fn, "r");
1029 error (0, errno, _("error opening \"%s\""), pprsz_fn);
1035 struct substring p, name;
1037 if (!ds_read_config_line (&line, &line_number, f))
1040 error (0, errno, _("error reading \"%s\""), pprsz_fn);
1045 ss_ltrim (&p, ss_cstr (CC_SPACES));
1046 if (!ss_match_char (&p, '"') || !ss_get_until (&p, '"', &name))
1048 if (ss_compare (name, ss_cstr (size)))
1051 ss_ltrim (&p, ss_cstr (CC_SPACES));
1052 if (ss_match_char (&p, '='))
1056 ss_trim (&p, ss_cstr (CC_SPACES));
1057 size = ss_xstrdup (p);
1065 error_at_line (0, 0, pprsz_fn, line_number,
1066 _("syntax error in paper size definition"));
1069 /* We found the one we want! */
1070 result = internal_get_paper_size (size, h, v);
1078 error (0, 0, _("error reading paper size definition file"));
1083 /* If D is NULL, returns the first enabled driver if any, NULL if
1084 none. Otherwise D must be the last driver returned by this
1085 function, in which case the next enabled driver is returned or NULL
1086 if that was the last. */
1087 struct outp_driver *
1088 outp_drivers (struct outp_driver *d)
1093 d = outp_driver_list;
1098 || (d->device == 0 || (d->device & disabled_devices) != d->device))
1105 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1106 device(s) given in mask DEVICE. */
1108 outp_enable_device (int enable, int device)
1111 disabled_devices &= ~device;
1113 disabled_devices |= device;
1116 /* Opens a page on driver D (if one is not open). */
1118 outp_open_page (struct outp_driver *d)
1122 d->cp_x = d->cp_y = 0;
1124 d->page_open = true;
1125 if (d->class->open_page != NULL)
1126 d->class->open_page (d);
1130 /* Closes the page on driver D (if one is open). */
1132 outp_close_page (struct outp_driver *d)
1136 if (d->class->close_page != NULL)
1137 d->class->close_page (d);
1138 d->page_open = false;
1142 /* Ejects the page on device D, if a page is open and non-blank,
1143 and opens a new page. */
1145 outp_eject_page (struct outp_driver *d)
1147 if (d->page_open && d->cp_y != 0)
1148 outp_close_page (d);
1152 /* Returns the width of string S, in device units, when output on
1155 outp_string_width (struct outp_driver *d, const char *s, enum outp_font font)
1157 struct outp_text text;
1161 text.justification = OUTP_LEFT;
1162 text.string = ss_cstr (s);
1163 text.h = text.v = INT_MAX;
1164 d->class->text_metrics (d, &text, &width, NULL);