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 #define _(msgid) gettext (msgid)
39 /* FIXME? Should the output configuration format be changed to
40 drivername:classname:devicetype:options, where devicetype is zero
41 or more of screen, printer, listing? */
43 /* FIXME: Have the reentrancy problems been solved? */
45 /* Where the output driver name came from. */
48 OUTP_S_COMMAND_LINE, /* Specified by the user. */
49 OUTP_S_INIT_FILE /* `default' or the init file. */
52 /* Names the output drivers to be used. */
55 char *name; /* Name of the output driver. */
56 int source; /* OUTP_S_* */
57 struct outp_names *next, *prev;
60 /* Defines an init file macro. */
65 struct outp_defn *next, *prev;
68 static struct outp_defn *outp_macros;
69 static struct outp_names *outp_configure_vec;
71 struct outp_driver_class_list *outp_class_list;
72 struct outp_driver *outp_driver_list;
77 /* A set of OUTP_DEV_* bits indicating the devices that are
79 static int disabled_devices;
81 static void destroy_driver (struct outp_driver *);
82 static void configure_driver (char *);
85 /* This mechanism attempts to catch reentrant use of outp_driver_list. */
86 static int iterating_driver_list;
88 #define reentrancy() msg (FE, _("Attempt to iterate driver list reentrantly."))
91 /* Add a class to the class list. */
93 add_class (struct outp_class *class)
95 struct outp_driver_class_list *new_list = xmalloc (sizeof *new_list);
97 new_list->class = class;
98 new_list->ref_count = 0;
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 msg (ME, _("Unknown output driver `%s'."), n->name);
167 msg (IE, _("Output driver `%s' referenced but never defined."), n->name);
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_DIGITS + 1];
206 for (d = outp_macros; d; d = d->next)
207 if (!strcmp (key, d->key))
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;
229 extern struct outp_class postscript_class;
230 extern struct outp_class epsf_class;
232 extern struct outp_class html_class;
234 char def[] = "default";
237 add_class (&html_class);
240 add_class (&epsf_class);
241 add_class (&postscript_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. "
296 "Use `-vvvvv' to view search path."));
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;
458 outp_class_list = NULL;
463 free (outp_subtitle);
464 outp_subtitle = NULL;
469 /* Display on stdout a list of all registered driver classes. */
471 outp_list_classes (void)
473 int width = get_viewwidth();
474 struct outp_driver_class_list *c;
476 printf (_("Driver classes:\n\t"));
478 for (c = outp_class_list; c; c = c->next)
480 if ((int) strlen (c->class->name) + 1 > width)
483 width = get_viewwidth() - 8;
487 fputs (c->class->name, stdout);
492 static int op_token; /* `=', 'a', 0. */
493 static struct string op_tokstr;
496 /* Parses a token from prog into op_token, op_tokstr. Sets op_token
497 to '=' on an equals sign, to 'a' on a string or identifier token,
498 or to 0 at end of line. Returns the new op_token. */
504 msg (IS, _("Syntax error."));
508 while (isspace ((unsigned char) *prog))
520 ds_clear (&op_tokstr);
522 if (*prog == '\'' || *prog == '"')
526 while (*prog && *prog != quote)
529 ds_putc (&op_tokstr, *prog++);
535 assert ((int) *prog); /* How could a line end in `\'? */
584 while (*prog >= '0' && *prog <= '7')
585 c = c * 8 + *prog++ - '0';
592 while (isxdigit ((unsigned char) *prog))
595 if (isdigit ((unsigned char) *prog))
598 c += (tolower ((unsigned char) (*prog))
605 msg (IS, _("Syntax error in string constant."));
608 ds_putc (&op_tokstr, (unsigned char) c);
614 while (*prog && !isspace ((unsigned char) *prog) && *prog != '=')
615 ds_putc (&op_tokstr, *prog++);
622 /* Applies the user-specified options in string S to output driver D
623 (at configuration time). */
625 parse_options (char *s, struct outp_driver * d)
630 ds_init (&op_tokstr, 64);
637 msg (IS, _("Syntax error in options."));
641 ds_truncate (&op_tokstr, 64);
642 strcpy (key, ds_c_str (&op_tokstr));
647 msg (IS, _("Syntax error in options (`=' expected)."));
654 msg (IS, _("Syntax error in options (value expected after `=')."));
657 d->class->option (d, key, &op_tokstr);
659 ds_destroy (&op_tokstr);
662 /* Find the driver in outp_driver_list with name NAME. */
663 static struct outp_driver *
664 find_driver (char *name)
666 struct outp_driver *d;
669 if (iterating_driver_list)
672 for (d = outp_driver_list; d; d = d->next)
673 if (!strcmp (d->name, name))
678 /* Tokenize string S into colon-separated fields, removing leading and
679 trailing whitespace on tokens. Returns a pointer to the
680 null-terminated token, which is formed by setting a NUL character
681 into the string. After the first call, subsequent calls should set
682 S to NULL. CP should be consistent across calls. Returns NULL
683 after all fields have been used up.
685 FIXME: Should ignore colons inside double quotes. */
687 colon_tokenize (char *s, char **cp)
697 token = s += strspn (s, " \t\v\r");
698 *cp = strchr (s, ':');
700 s = *cp = strchr (s, 0);
703 while (s > token && strchr (" \t\v\r", s[-1]))
709 /* String S is in format:
710 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
711 Adds a driver to outp_driver_list pursuant to the specification
714 configure_driver (char *s)
717 struct outp_driver *d = NULL, *iter;
718 struct outp_driver_class_list *c = NULL;
720 s = fn_interp_vars (s, find_defn_value);
723 token = colon_tokenize (s, &cp);
726 msg (IS, _("Driver name expected."));
730 d = xmalloc (sizeof *d);
733 d->name = xstrdup (token);
737 d->next = d->prev = NULL;
739 d->device = OUTP_DEV_NONE;
744 token = colon_tokenize (NULL, &cp);
747 msg (IS, _("Class name expected."));
751 for (c = outp_class_list; c; c = c->next)
752 if (!strcmp (c->class->name, token))
756 msg (IS, _("Unknown output driver class `%s'."), token);
761 if (!c->ref_count && !d->class->open_global (d->class))
763 msg (IS, _("Can't initialize output driver class `%s'."),
768 if (!d->class->preopen_driver (d))
770 msg (IS, _("Can't initialize output driver `%s' of class `%s'."),
771 d->name, d->class->name);
776 token = colon_tokenize (NULL, &cp);
781 for (type = strtok_r (token, " \t\r\v", &sp); type;
782 type = strtok_r (NULL, " \t\r\v", &sp))
784 if (!strcmp (type, "listing"))
785 d->device |= OUTP_DEV_LISTING;
786 else if (!strcmp (type, "screen"))
787 d->device |= OUTP_DEV_SCREEN;
788 else if (!strcmp (type, "printer"))
789 d->device |= OUTP_DEV_PRINTER;
792 msg (IS, _("Unknown device type `%s'."), type);
799 token = colon_tokenize (NULL, &cp);
801 parse_options (token, d);
802 if (!d->class->postopen_driver (d))
804 msg (IS, _("Can't complete initialization of output driver `%s' of "
805 "class `%s'."), d->name, d->class->name);
809 /* Find like-named driver and delete. */
810 iter = find_driver (d->name);
812 destroy_driver (iter);
815 d->next = outp_driver_list;
817 if (outp_driver_list)
818 outp_driver_list->prev = d;
819 outp_driver_list = d;
829 /* Destroys output driver D. */
831 destroy_driver (struct outp_driver *d)
834 d->class->close_page (d);
837 struct outp_driver_class_list *c;
840 d->class->close_driver (d);
842 for (c = outp_class_list; c; c = c->next)
843 if (c->class == d->class)
848 if (c->ref_count == 0)
850 if (!d->class->close_global (d->class))
851 msg (IS, _("Can't deinitialize output driver class `%s'."),
857 /* Remove this driver from the global driver list. */
859 d->prev->next = d->next;
861 d->next->prev = d->prev;
862 if (d == outp_driver_list)
863 outp_driver_list = d->next;
867 option_cmp (const void *a, const void *b)
869 const struct outp_option *o1 = a;
870 const struct outp_option *o2 = b;
871 return strcmp (o1->keyword, o2->keyword);
874 /* Tries to match S as one of the keywords in TAB, with corresponding
875 information structure INFO. Returns category code or 0 on failure;
876 if category code is negative then stores subcategory in *SUBCAT. */
878 outp_match_keyword (const char *s, struct outp_option *tab,
879 struct outp_option_info *info, int *subcat)
882 struct outp_option *oip;
884 /* Form hash table. */
885 if (NULL == info->initial)
890 struct outp_option *ptr[255], **oip;
892 for (count = 0; tab[count].keyword[0]; count++)
896 qsort (tab, count, sizeof *tab, option_cmp);
900 *cp = tab[0].keyword[0];
902 for (i = 0; i < count; i++)
903 if (tab[i].keyword[0] != *cp)
905 *++cp = tab[i].keyword[0];
910 info->initial = xstrdup (s);
911 info->options = xnmalloc (cp - s, sizeof *info->options);
912 memcpy (info->options, ptr, sizeof *info->options * (cp - s));
916 oip = *info->options;
920 cp = strchr (info->initial, s[0]);
924 printf (_("Trying to find keyword `%s'...\n"), s);
926 oip = info->options[cp - info->initial];
927 while (oip->keyword[0] == s[0])
930 printf ("- %s\n", oip->keyword);
932 if (!strcmp (s, oip->keyword))
935 *subcat = oip->subcat;
944 /* Encapsulate two characters in a single int. */
945 #define TWO_CHARS(A, B) \
948 /* Determines the size of a dimensional measurement and returns the
949 size in units of 1/72000". Units if not specified explicitly are
950 inches for values under 50, millimeters otherwise. Returns 0,
951 stores NULL to *TAIL on error; otherwise returns dimension, stores
954 outp_evaluate_dimension (char *dimen, char **tail)
960 value = strtod (s, &ptail);
967 b = strtod (s, &ptail);
968 if (b <= 0.0 || ptail == s)
973 c = strtod (s, &ptail);
974 if (c <= 0.0 || ptail == s)
984 else if (*ptail == '/')
988 b = strtod (s, &ptail);
989 if (b <= 0.0 || ptail == s)
996 if (*s == 0 || isspace ((unsigned char) *s))
1001 value *= 72000 / 25.4;
1007 /* Standard TeX units are supported. */
1009 factor = 72000, s++;
1011 switch (TWO_CHARS (s[0], s[1]))
1013 case TWO_CHARS ('p', 't'):
1014 factor = 72000 / 72.27;
1016 case TWO_CHARS ('p', 'c'):
1017 factor = 72000 / 72.27 * 12.0;
1019 case TWO_CHARS ('i', 'n'):
1022 case TWO_CHARS ('b', 'p'):
1023 factor = 72000 / 72.0;
1025 case TWO_CHARS ('c', 'm'):
1026 factor = 72000 / 2.54;
1028 case TWO_CHARS ('m', 'm'):
1029 factor = 72000 / 25.4;
1031 case TWO_CHARS ('d', 'd'):
1032 factor = 72000 / 72.27 * 1.0700086;
1034 case TWO_CHARS ('c', 'c'):
1035 factor = 72000 / 72.27 * 12.840104;
1037 case TWO_CHARS ('s', 'p'):
1038 factor = 72000 / 72.27 / 65536.0;
1041 msg (SE, _("Unit \"%s\" is unknown in dimension \"%s\"."), s, dimen);
1056 msg (SE, _("Bad dimension \"%s\"."), dimen);
1060 /* Stores the dimensions in 1/72000" units of paper identified by
1061 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
1062 of HORZ and VERT are dimensions, into *H and *V. Return nonzero on
1065 internal_get_paper_size (char *size, int *h, int *v)
1069 while (isspace ((unsigned char) *size))
1071 *h = outp_evaluate_dimension (size, &tail);
1074 while (isspace ((unsigned char) *tail))
1078 else if (*tail == 'b' && tail[1] == 'y')
1082 msg (SE, _("`x' expected in paper size `%s'."), size);
1085 *v = outp_evaluate_dimension (tail, &tail);
1088 while (isspace ((unsigned char) *tail))
1092 msg (SE, _("Trailing garbage `%s' on paper size `%s'."), tail, size);
1099 /* Stores the dimensions, in 1/72000" units, of paper identified by
1100 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
1101 V', or it may be a case-insensitive paper identifier, which is
1102 looked up in the `papersize' configuration file. Returns nonzero
1103 on success. May modify SIZE. */
1104 /* Don't read further unless you've got a strong stomach. */
1106 outp_get_paper_size (char *size, int *h, int *v)
1115 static struct paper_size cache[4];
1122 struct file_locator where;
1126 int min_value, min_index;
1130 while (isspace ((unsigned char) *size))
1132 if (isdigit ((unsigned char) *size))
1133 return internal_get_paper_size (size, h, v);
1137 while (isspace ((unsigned char) *ep) && ep >= size)
1141 msg (SE, _("Paper size name must not be empty."));
1150 for (i = 0; i < 4; i++)
1151 if (cache[i].name != NULL && !strcasecmp (cache[i].name, size))
1159 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1161 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1165 where.filename = pprsz_fn;
1166 where.line_number = 0;
1167 err_push_file_locator (&where);
1168 ds_init (&line, 128);
1170 if (pprsz_fn == NULL)
1172 msg (IE, _("Cannot find `papersize' configuration file."));
1176 msg (VM (1), _("%s: Opening paper size definition file..."), pprsz_fn);
1177 f = fopen (pprsz_fn, "r");
1180 msg (IE, _("Opening %s: %s."), pprsz_fn, strerror (errno));
1188 if (!ds_get_config_line (f, &line, &where))
1191 msg (ME, _("Reading %s: %s."), pprsz_fn, strerror (errno));
1194 for (cp = ds_c_str (&line); isspace ((unsigned char) *cp); cp++);
1199 for (bp = ep = cp + 1; *ep && *ep != '"'; ep++);
1203 if (0 != strcasecmp (bp, size))
1206 for (cp = ep + 1; isspace ((unsigned char) *cp); cp++);
1209 size = xmalloc (ep - bp + 1);
1218 msg (IE, _("Syntax error in paper size definition."));
1221 /* We found the one we want! */
1222 result = internal_get_paper_size (size, h, v);
1225 min_value = cache[0].use;
1227 for (i = 1; i < 4; i++)
1228 if (cache[0].use < min_value)
1230 min_value = cache[i].use;
1233 free (cache[min_index].name);
1234 cache[min_index].name = xstrdup (size);
1235 cache[min_index].use = use;
1236 cache[min_index].h = *h;
1237 cache[min_index].v = *v;
1241 err_pop_file_locator (&where);
1247 msg (VM (2), _("Paper size definition file read successfully."));
1249 msg (VM (1), _("Error reading paper size definition file."));
1254 /* If D is NULL, returns the first enabled driver if any, NULL if
1255 none. Otherwise D must be the last driver returned by this
1256 function, in which case the next enabled driver is returned or NULL
1257 if that was the last. */
1258 struct outp_driver *
1259 outp_drivers (struct outp_driver *d)
1261 #if GLOBAL_DEBUGGING
1262 struct outp_driver *orig_d = d;
1268 d = outp_driver_list;
1275 || (d->device & disabled_devices) != d->device)))
1279 #if GLOBAL_DEBUGGING
1282 if (iterating_driver_list++)
1285 else if (orig_d && !d)
1287 assert (iterating_driver_list == 1);
1288 iterating_driver_list = 0;
1295 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1296 device(s) given in mask DEVICE. */
1298 outp_enable_device (int enable, int device)
1301 disabled_devices &= ~device;
1303 disabled_devices |= device;
1306 /* Ejects the paper on device D, if the page is not blank. */
1308 outp_eject_page (struct outp_driver *d)
1310 if (d->page_open == 0)
1315 d->cp_x = d->cp_y = 0;
1317 if (d->class->close_page (d) == 0)
1318 msg (ME, _("Error closing page on %s device of %s class."),
1319 d->name, d->class->name);
1320 if (d->class->open_page (d) == 0)
1322 msg (ME, _("Error opening page on %s device of %s class."),
1323 d->name, d->class->name);
1330 /* Returns the width of string S, in device units, when output on
1333 outp_string_width (struct outp_driver *d, const char *s)
1335 struct outp_text text;
1337 text.options = OUTP_T_JUST_LEFT;
1338 ls_init (&text.s, (char *) s, strlen (s));
1339 d->class->text_metrics (d, &text);