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
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 struct outp_driver_class_list *outp_class_list;
70 struct outp_driver *outp_driver_list;
75 /* A set of OUTP_DEV_* bits indicating the devices that are
77 static int disabled_devices;
79 static void destroy_driver (struct outp_driver *);
80 static void configure_driver (char *);
83 /* This mechanism attempts to catch reentrant use of outp_driver_list. */
84 static int iterating_driver_list;
86 #define reentrancy() msg (FE, _("Attempt to iterate driver list reentrantly."))
89 /* Add a class to the class list. */
91 add_class (struct outp_class *class)
93 struct outp_driver_class_list *new_list = xmalloc (sizeof *new_list);
95 new_list->class = class;
96 new_list->ref_count = 0;
100 outp_class_list = new_list;
101 new_list->next = NULL;
105 new_list->next = outp_class_list;
106 outp_class_list = new_list;
110 /* Finds the outp_names in outp_configure_vec with name between BP and
112 static struct outp_names *
113 search_names (char *bp, char *ep)
115 struct outp_names *n;
117 for (n = outp_configure_vec; n; n = n->next)
118 if ((int) strlen (n->name) == ep - bp && !memcmp (n->name, bp, ep - bp))
123 /* Deletes outp_names NAME from outp_configure_vec. */
125 delete_name (struct outp_names * n)
129 n->prev->next = n->next;
131 n->next->prev = n->prev;
132 if (n == outp_configure_vec)
133 outp_configure_vec = n->next;
137 /* Adds the name between BP and EP exclusive to list
138 outp_configure_vec with source SOURCE. */
140 add_name (char *bp, char *ep, int source)
142 struct outp_names *n = xmalloc (sizeof *n);
143 n->name = xmalloc (ep - bp + 1);
144 memcpy (n->name, bp, ep - bp);
145 n->name[ep - bp] = 0;
147 n->next = outp_configure_vec;
149 if (outp_configure_vec)
150 outp_configure_vec->prev = n;
151 outp_configure_vec = n;
154 /* Checks that outp_configure_vec is empty, bitches & clears it if it
157 check_configure_vec (void)
159 struct outp_names *n;
161 for (n = outp_configure_vec; n; n = n->next)
162 if (n->source == OUTP_S_COMMAND_LINE)
163 msg (ME, _("Unknown output driver `%s'."), n->name);
165 msg (IE, _("Output driver `%s' referenced but never defined."), n->name);
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_DIGITS + 1];
204 for (d = outp_macros; d; d = d->next)
205 if (!strcmp (key, d->key))
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;
227 extern struct outp_class postscript_class;
228 extern struct outp_class epsf_class;
230 extern struct outp_class html_class;
231 extern struct outp_class devind_class;
233 char def[] = "default";
236 add_class (&html_class);
239 add_class (&epsf_class);
240 add_class (&postscript_class);
242 add_class (&devind_class);
243 add_class (&ascii_class);
245 add_name (def, &def[strlen (def)], OUTP_S_INIT_FILE);
250 /* Deletes all the output macros. */
254 struct outp_defn *d, *next;
256 for (d = outp_macros; d; d = next)
265 /* Reads the initialization file; initializes outp_driver_list. */
267 outp_read_devices (void)
275 struct file_locator where;
278 if (iterating_driver_list)
282 init_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_INIT_FILE",
284 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
287 where.filename = init_fn;
288 where.line_number = 0;
289 err_push_file_locator (&where);
291 ds_init (&line, 128);
295 msg (IE, _("Cannot find output initialization file. Use `-vv' to view "
300 msg (VM (1), _("%s: Opening device description file..."), init_fn);
301 f = fopen (init_fn, "r");
304 msg (IE, _("Opening %s: %s."), init_fn, strerror (errno));
312 if (!ds_get_config_line (f, &line, &where))
315 msg (ME, _("Reading %s: %s."), init_fn, strerror (errno));
318 for (cp = ds_c_str (&line); isspace ((unsigned char) *cp); cp++);
319 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
320 outp_configure_macro (&cp[7]);
324 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
326 expand_name (cp, ep);
329 struct outp_names *n = search_names (cp, ep);
332 configure_driver (cp);
337 msg (IS, _("Syntax error."));
342 check_configure_vec ();
345 err_pop_file_locator (&where);
346 if (f && -1 == fclose (f))
347 msg (MW, _("Closing %s: %s."), init_fn, strerror (errno));
351 if (outp_driver_list == NULL)
352 msg (MW, _("No output drivers are active."));
355 msg (VM (2), _("Device definition file read successfully."));
357 msg (VM (1), _("Error reading device definition file."));
361 /* Clear the list of drivers to configure. */
363 outp_configure_clear (void)
365 struct outp_names *n, *next;
367 for (n = outp_configure_vec; n; n = next)
373 outp_configure_vec = NULL;
376 /* Adds the name BP to the list of drivers to configure into
379 outp_configure_add (char *bp)
381 char *ep = &bp[strlen (bp)];
382 if (!search_names (bp, ep))
383 add_name (bp, ep, OUTP_S_COMMAND_LINE);
386 /* Defines one configuration macro based on the text in BP, which
387 should be of the form `KEY=VALUE'. */
389 outp_configure_macro (char *bp)
394 while (isspace ((unsigned char) *bp))
397 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
400 d = xmalloc (sizeof *d);
401 d->key = xmalloc (ep - bp + 1);
402 memcpy (d->key, bp, ep - bp);
405 /* Earlier definitions for a particular KEY override later ones. */
406 if (find_defn_value (d->key))
415 while (isspace ((unsigned char) *ep))
417 d->value = fn_interp_vars (ep, find_defn_value);
418 d->next = outp_macros;
421 outp_macros->prev = d;
425 /* Destroys all the drivers in driver list *DL and sets *DL to
428 destroy_list (struct outp_driver ** dl)
430 struct outp_driver *d, *next;
432 for (d = *dl; d; d = next)
441 /* Closes all the output drivers. */
445 struct outp_driver_class_list *n = outp_class_list ;
447 if (iterating_driver_list)
450 destroy_list (&outp_driver_list);
454 struct outp_driver_class_list *next = n->next;
459 free (outp_subtitle);
464 /* Display on stdout a list of all registered driver classes. */
466 outp_list_classes (void)
468 int width = get_viewwidth();
469 struct outp_driver_class_list *c;
471 printf (_("Driver classes:\n\t"));
473 for (c = outp_class_list; c; c = c->next)
475 if ((int) strlen (c->class->name) + 1 > width)
478 width = get_viewwidth() - 8;
482 fputs (c->class->name, stdout);
487 static int op_token; /* `=', 'a', 0. */
488 static struct string op_tokstr;
491 /* Parses a token from prog into op_token, op_tokstr. Sets op_token
492 to '=' on an equals sign, to 'a' on a string or identifier token,
493 or to 0 at end of line. Returns the new op_token. */
499 msg (IS, _("Syntax error."));
503 while (isspace ((unsigned char) *prog))
515 ds_clear (&op_tokstr);
517 if (*prog == '\'' || *prog == '"')
521 while (*prog && *prog != quote)
524 ds_putc (&op_tokstr, *prog++);
530 assert ((int) *prog); /* How could a line end in `\'? */
579 while (*prog >= '0' && *prog <= '7')
580 c = c * 8 + *prog++ - '0';
587 while (isxdigit ((unsigned char) *prog))
590 if (isdigit ((unsigned char) *prog))
593 c += (tolower ((unsigned char) (*prog))
600 msg (IS, _("Syntax error in string constant."));
603 ds_putc (&op_tokstr, (unsigned char) c);
609 while (*prog && !isspace ((unsigned char) *prog) && *prog != '=')
610 ds_putc (&op_tokstr, *prog++);
617 /* Applies the user-specified options in string S to output driver D
618 (at configuration time). */
620 parse_options (char *s, struct outp_driver * d)
625 ds_init (&op_tokstr, 64);
632 msg (IS, _("Syntax error in options."));
636 ds_truncate (&op_tokstr, 64);
637 strcpy (key, ds_c_str (&op_tokstr));
642 msg (IS, _("Syntax error in options (`=' expected)."));
649 msg (IS, _("Syntax error in options (value expected after `=')."));
652 d->class->option (d, key, &op_tokstr);
654 ds_destroy (&op_tokstr);
657 /* Find the driver in outp_driver_list with name NAME. */
658 static struct outp_driver *
659 find_driver (char *name)
661 struct outp_driver *d;
664 if (iterating_driver_list)
667 for (d = outp_driver_list; d; d = d->next)
668 if (!strcmp (d->name, name))
673 /* Tokenize string S into colon-separated fields, removing leading and
674 trailing whitespace on tokens. Returns a pointer to the
675 null-terminated token, which is formed by setting a NUL character
676 into the string. After the first call, subsequent calls should set
677 S to NULL. CP should be consistent across calls. Returns NULL
678 after all fields have been used up.
680 FIXME: Should ignore colons inside double quotes. */
682 colon_tokenize (char *s, char **cp)
692 token = s += strspn (s, " \t\v\r");
693 *cp = strchr (s, ':');
695 s = *cp = strchr (s, 0);
698 while (s > token && strchr (" \t\v\r", s[-1]))
704 /* String S is in format:
705 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
706 Adds a driver to outp_driver_list pursuant to the specification
709 configure_driver (char *s)
712 struct outp_driver *d = NULL, *iter;
713 struct outp_driver_class_list *c = NULL;
715 s = fn_interp_vars (s, find_defn_value);
718 token = colon_tokenize (s, &cp);
721 msg (IS, _("Driver name expected."));
725 d = xmalloc (sizeof *d);
728 d->name = xstrdup (token);
732 d->next = d->prev = NULL;
734 d->device = OUTP_DEV_NONE;
739 token = colon_tokenize (NULL, &cp);
742 msg (IS, _("Class name expected."));
746 for (c = outp_class_list; c; c = c->next)
747 if (!strcmp (c->class->name, token))
751 msg (IS, _("Unknown output driver class `%s'."), token);
756 if (!c->ref_count && !d->class->open_global (d->class))
758 msg (IS, _("Can't initialize output driver class `%s'."),
763 if (!d->class->preopen_driver (d))
765 msg (IS, _("Can't initialize output driver `%s' of class `%s'."),
766 d->name, d->class->name);
771 token = colon_tokenize (NULL, &cp);
776 for (type = strtok_r (token, " \t\r\v", &sp); type;
777 type = strtok_r (NULL, " \t\r\v", &sp))
779 if (!strcmp (type, "listing"))
780 d->device |= OUTP_DEV_LISTING;
781 else if (!strcmp (type, "screen"))
782 d->device |= OUTP_DEV_SCREEN;
783 else if (!strcmp (type, "printer"))
784 d->device |= OUTP_DEV_PRINTER;
787 msg (IS, _("Unknown device type `%s'."), type);
794 token = colon_tokenize (NULL, &cp);
796 parse_options (token, d);
797 if (!d->class->postopen_driver (d))
799 msg (IS, _("Can't complete initialization of output driver `%s' of "
800 "class `%s'."), d->name, d->class->name);
804 /* Find like-named driver and delete. */
805 iter = find_driver (d->name);
807 destroy_driver (iter);
810 d->next = outp_driver_list;
812 if (outp_driver_list)
813 outp_driver_list->prev = d;
814 outp_driver_list = d;
824 /* Destroys output driver D. */
826 destroy_driver (struct outp_driver *d)
829 d->class->close_page (d);
832 struct outp_driver_class_list *c;
835 d->class->close_driver (d);
837 for (c = outp_class_list; c; c = c->next)
838 if (c->class == d->class)
843 if (c->ref_count == 0)
845 if (!d->class->close_global (d->class))
846 msg (IS, _("Can't deinitialize output driver class `%s'."),
852 /* Remove this driver from the global driver list. */
854 d->prev->next = d->next;
856 d->next->prev = d->prev;
857 if (d == outp_driver_list)
858 outp_driver_list = d->next;
862 option_cmp (const void *a, const void *b)
864 const struct outp_option *o1 = a;
865 const struct outp_option *o2 = b;
866 return strcmp (o1->keyword, o2->keyword);
869 /* Tries to match S as one of the keywords in TAB, with corresponding
870 information structure INFO. Returns category code or 0 on failure;
871 if category code is negative then stores subcategory in *SUBCAT. */
873 outp_match_keyword (const char *s, struct outp_option *tab,
874 struct outp_option_info *info, int *subcat)
877 struct outp_option *oip;
879 /* Form hash table. */
880 if (NULL == info->initial)
885 struct outp_option *ptr[255], **oip;
887 for (count = 0; tab[count].keyword[0]; count++)
891 qsort (tab, count, sizeof *tab, option_cmp);
895 *cp = tab[0].keyword[0];
897 for (i = 0; i < count; i++)
898 if (tab[i].keyword[0] != *cp)
900 *++cp = tab[i].keyword[0];
905 info->initial = xstrdup (s);
906 info->options = xmalloc (sizeof *info->options * (cp - s));
907 memcpy (info->options, ptr, sizeof *info->options * (cp - s));
911 oip = *info->options;
915 cp = strchr (info->initial, s[0]);
919 printf (_("Trying to find keyword `%s'...\n"), s);
921 oip = info->options[cp - info->initial];
922 while (oip->keyword[0] == s[0])
925 printf ("- %s\n", oip->keyword);
927 if (!strcmp (s, oip->keyword))
930 *subcat = oip->subcat;
939 /* Encapsulate two characters in a single int. */
940 #define TWO_CHARS(A, B) \
943 /* Determines the size of a dimensional measurement and returns the
944 size in units of 1/72000". Units if not specified explicitly are
945 inches for values under 50, millimeters otherwise. Returns 0,
946 stores NULL to *TAIL on error; otherwise returns dimension, stores
949 outp_evaluate_dimension (char *dimen, char **tail)
955 value = strtod (s, &ptail);
962 b = strtod (s, &ptail);
963 if (b <= 0.0 || ptail == s)
968 c = strtod (s, &ptail);
969 if (c <= 0.0 || ptail == s)
979 else if (*ptail == '/')
983 b = strtod (s, &ptail);
984 if (b <= 0.0 || ptail == s)
991 if (*s == 0 || isspace ((unsigned char) *s))
996 value *= 72000 / 25.4;
1002 /* Standard TeX units are supported. */
1004 factor = 72000, s++;
1006 switch (TWO_CHARS (s[0], s[1]))
1008 case TWO_CHARS ('p', 't'):
1009 factor = 72000 / 72.27;
1011 case TWO_CHARS ('p', 'c'):
1012 factor = 72000 / 72.27 * 12.0;
1014 case TWO_CHARS ('i', 'n'):
1017 case TWO_CHARS ('b', 'p'):
1018 factor = 72000 / 72.0;
1020 case TWO_CHARS ('c', 'm'):
1021 factor = 72000 / 2.54;
1023 case TWO_CHARS ('m', 'm'):
1024 factor = 72000 / 25.4;
1026 case TWO_CHARS ('d', 'd'):
1027 factor = 72000 / 72.27 * 1.0700086;
1029 case TWO_CHARS ('c', 'c'):
1030 factor = 72000 / 72.27 * 12.840104;
1032 case TWO_CHARS ('s', 'p'):
1033 factor = 72000 / 72.27 / 65536.0;
1036 msg (SE, _("Unit \"%s\" is unknown in dimension \"%s\"."), s, dimen);
1051 msg (SE, _("Bad dimension \"%s\"."), dimen);
1055 /* Stores the dimensions in 1/72000" units of paper identified by
1056 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
1057 of HORZ and VERT are dimensions, into *H and *V. Return nonzero on
1060 internal_get_paper_size (char *size, int *h, int *v)
1064 while (isspace ((unsigned char) *size))
1066 *h = outp_evaluate_dimension (size, &tail);
1069 while (isspace ((unsigned char) *tail))
1073 else if (*tail == 'b' && tail[1] == 'y')
1077 msg (SE, _("`x' expected in paper size `%s'."), size);
1080 *v = outp_evaluate_dimension (tail, &tail);
1083 while (isspace ((unsigned char) *tail))
1087 msg (SE, _("Trailing garbage `%s' on paper size `%s'."), tail, size);
1094 /* Stores the dimensions, in 1/72000" units, of paper identified by
1095 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
1096 V', or it may be a case-insensitive paper identifier, which is
1097 looked up in the `papersize' configuration file. Returns nonzero
1098 on success. May modify SIZE. */
1099 /* Don't read further unless you've got a strong stomach. */
1101 outp_get_paper_size (char *size, int *h, int *v)
1110 static struct paper_size cache[4];
1117 struct file_locator where;
1121 int min_value, min_index;
1125 while (isspace ((unsigned char) *size))
1127 if (isdigit ((unsigned char) *size))
1128 return internal_get_paper_size (size, h, v);
1132 while (isspace ((unsigned char) *ep) && ep >= size)
1136 msg (SE, _("Paper size name must not be empty."));
1145 for (i = 0; i < 4; i++)
1146 if (cache[i].name != NULL && !strcasecmp (cache[i].name, size))
1154 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1156 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1160 where.filename = pprsz_fn;
1161 where.line_number = 0;
1162 err_push_file_locator (&where);
1163 ds_init (&line, 128);
1165 if (pprsz_fn == NULL)
1167 msg (IE, _("Cannot find `papersize' configuration file."));
1171 msg (VM (1), _("%s: Opening paper size definition file..."), pprsz_fn);
1172 f = fopen (pprsz_fn, "r");
1175 msg (IE, _("Opening %s: %s."), pprsz_fn, strerror (errno));
1183 if (!ds_get_config_line (f, &line, &where))
1186 msg (ME, _("Reading %s: %s."), pprsz_fn, strerror (errno));
1189 for (cp = ds_c_str (&line); isspace ((unsigned char) *cp); cp++);
1194 for (bp = ep = cp + 1; *ep && *ep != '"'; ep++);
1198 if (0 != strcasecmp (bp, size))
1201 for (cp = ep + 1; isspace ((unsigned char) *cp); cp++);
1204 size = xmalloc (ep - bp + 1);
1213 msg (IE, _("Syntax error in paper size definition."));
1216 /* We found the one we want! */
1217 result = internal_get_paper_size (size, h, v);
1220 min_value = cache[0].use;
1222 for (i = 1; i < 4; i++)
1223 if (cache[0].use < min_value)
1225 min_value = cache[i].use;
1228 free (cache[min_index].name);
1229 cache[min_index].name = xstrdup (size);
1230 cache[min_index].use = use;
1231 cache[min_index].h = *h;
1232 cache[min_index].v = *v;
1236 err_pop_file_locator (&where);
1242 msg (VM (2), _("Paper size definition file read successfully."));
1244 msg (VM (1), _("Error reading paper size definition file."));
1249 /* If D is NULL, returns the first enabled driver if any, NULL if
1250 none. Otherwise D must be the last driver returned by this
1251 function, in which case the next enabled driver is returned or NULL
1252 if that was the last. */
1253 struct outp_driver *
1254 outp_drivers (struct outp_driver *d)
1256 #if GLOBAL_DEBUGGING
1257 struct outp_driver *orig_d = d;
1263 d = outp_driver_list;
1270 || (d->device & disabled_devices) != d->device)))
1274 #if GLOBAL_DEBUGGING
1277 if (iterating_driver_list++)
1280 else if (orig_d && !d)
1282 assert (iterating_driver_list == 1);
1283 iterating_driver_list = 0;
1290 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1291 device(s) given in mask DEVICE. */
1293 outp_enable_device (int enable, int device)
1296 disabled_devices &= ~device;
1298 disabled_devices |= device;
1301 /* Ejects the paper on device D, if the page is not blank. */
1303 outp_eject_page (struct outp_driver *d)
1305 if (d->page_open == 0)
1310 d->cp_x = d->cp_y = 0;
1312 if (d->class->close_page (d) == 0)
1313 msg (ME, _("Error closing page on %s device of %s class."),
1314 d->name, d->class->name);
1315 if (d->class->open_page (d) == 0)
1317 msg (ME, _("Error opening page on %s device of %s class."),
1318 d->name, d->class->name);
1325 /* Returns the width of string S, in device units, when output on
1328 outp_string_width (struct outp_driver *d, const char *s)
1330 struct outp_text text;
1332 text.options = OUTP_T_JUST_LEFT;
1333 ls_init (&text.s, (char *) s, strlen (s));
1334 d->class->text_metrics (d, &text);