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., 59 Temple Place - Suite 330, Boston, MA
36 /* FIXME? Should the output configuration format be changed to
37 drivername:classname:devicetype:options, where devicetype is zero
38 or more of screen, printer, listing? */
40 /* FIXME: Have the reentrancy problems been solved? */
42 /* Where the output driver name came from. */
45 OUTP_S_COMMAND_LINE, /* Specified by the user. */
46 OUTP_S_INIT_FILE /* `default' or the init file. */
49 /* Names the output drivers to be used. */
52 char *name; /* Name of the output driver. */
53 int source; /* OUTP_S_* */
54 struct outp_names *next, *prev;
57 /* Defines an init file macro. */
62 struct outp_defn *next, *prev;
65 static struct outp_defn *outp_macros;
66 static struct outp_names *outp_configure_vec;
68 struct outp_driver_class_list *outp_class_list;
69 struct outp_driver *outp_driver_list;
74 /* A set of OUTP_DEV_* bits indicating the devices that are
78 static void destroy_driver (struct outp_driver *);
79 static void configure_driver (char *);
82 /* This mechanism attempts to catch reentrant use of outp_driver_list. */
83 static int iterating_driver_list;
85 #define reentrancy() msg (FE, _("Attempt to iterate driver list reentrantly."))
88 /* Add a class to the class list. */
90 add_class (struct outp_class *class)
92 struct outp_driver_class_list *new_list = xmalloc (sizeof *new_list);
94 new_list->class = class;
95 new_list->ref_count = 0;
99 outp_class_list = new_list;
100 new_list->next = NULL;
104 new_list->next = outp_class_list;
105 outp_class_list = new_list;
109 /* Finds the outp_names in outp_configure_vec with name between BP and
111 static struct outp_names *
112 search_names (char *bp, char *ep)
114 struct outp_names *n;
116 for (n = outp_configure_vec; n; n = n->next)
117 if ((int) strlen (n->name) == ep - bp && !memcmp (n->name, bp, ep - bp))
122 /* Deletes outp_names NAME from outp_configure_vec. */
124 delete_name (struct outp_names * n)
128 n->prev->next = n->next;
130 n->next->prev = n->prev;
131 if (n == outp_configure_vec)
132 outp_configure_vec = n->next;
136 /* Adds the name between BP and EP exclusive to list
137 outp_configure_vec with source SOURCE. */
139 add_name (char *bp, char *ep, int source)
141 struct outp_names *n = xmalloc (sizeof *n);
142 n->name = xmalloc (ep - bp + 1);
143 memcpy (n->name, bp, ep - bp);
144 n->name[ep - bp] = 0;
146 n->next = outp_configure_vec;
148 if (outp_configure_vec)
149 outp_configure_vec->prev = n;
150 outp_configure_vec = n;
153 /* Checks that outp_configure_vec is empty, bitches & clears it if it
156 check_configure_vec (void)
158 struct outp_names *n;
160 for (n = outp_configure_vec; n; n = n->next)
161 if (n->source == OUTP_S_COMMAND_LINE)
162 msg (ME, _("Unknown output driver `%s'."), n->name);
164 msg (IE, _("Output driver `%s' referenced but never defined."), n->name);
165 outp_configure_clear ();
168 /* Searches outp_configure_vec for the name between BP and EP
169 exclusive. If found, it is deleted, then replaced by the names
170 given in EP+1, if any. */
172 expand_name (char *bp, char *ep)
174 struct outp_names *n = search_names (bp, ep);
182 while (isspace ((unsigned char) *bp))
185 while (*ep && !isspace ((unsigned char) *ep))
189 if (!search_names (bp, ep))
190 add_name (bp, ep, OUTP_S_INIT_FILE);
195 /* Looks for a macro with key KEY, and returns the corresponding value
196 if found, or NULL if not. */
198 find_defn_value (const char *key)
200 static char buf[INT_DIGITS + 1];
203 for (d = outp_macros; d; d = d->next)
204 if (!strcmp (key, d->key))
206 if (!strcmp (key, "viewwidth"))
208 sprintf (buf, "%d", set_viewwidth);
211 else if (!strcmp (key, "viewlength"))
213 sprintf (buf, "%d", set_viewlength);
220 /* Initializes global variables. */
224 extern struct outp_class ascii_class;
226 extern struct outp_class postscript_class;
227 extern struct outp_class epsf_class;
230 extern struct outp_class html_class;
233 char def[] = "default";
236 add_class (&html_class);
239 add_class (&epsf_class);
240 add_class (&postscript_class);
242 add_class (&ascii_class);
244 add_name (def, &def[strlen (def)], OUTP_S_INIT_FILE);
249 /* Deletes all the output macros. */
253 struct outp_defn *d, *next;
255 for (d = outp_macros; d; d = next)
264 /* Reads the initialization file; initializes outp_driver_list. */
266 outp_read_devices (void)
274 struct file_locator where;
277 if (iterating_driver_list)
281 init_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_INIT_FILE",
283 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
286 where.filename = init_fn;
287 where.line_number = 0;
288 err_push_file_locator (&where);
290 ds_init (NULL, &line, 128);
294 msg (IE, _("Cannot find output initialization file. Use `-vv' to view "
299 msg (VM (1), _("%s: Opening device description file..."), init_fn);
300 f = fopen (init_fn, "r");
303 msg (IE, _("Opening %s: %s."), init_fn, strerror (errno));
311 if (!ds_get_config_line (f, &line, &where))
314 msg (ME, _("Reading %s: %s."), init_fn, strerror (errno));
317 for (cp = ds_value (&line); isspace ((unsigned char) *cp); cp++);
318 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
319 outp_configure_macro (&cp[7]);
323 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
325 expand_name (cp, ep);
328 struct outp_names *n = search_names (cp, ep);
331 configure_driver (cp);
336 msg (IS, _("Syntax error."));
341 check_configure_vec ();
344 err_pop_file_locator (&where);
345 if (f && -1 == fclose (f))
346 msg (MW, _("Closing %s: %s."), init_fn, strerror (errno));
350 if (outp_driver_list == NULL)
351 msg (MW, _("No output drivers are active."));
354 msg (VM (2), _("Device definition file read successfully."));
356 msg (VM (1), _("Error reading device definition file."));
360 /* Clear the list of drivers to configure. */
362 outp_configure_clear (void)
364 struct outp_names *n, *next;
366 for (n = outp_configure_vec; n; n = next)
372 outp_configure_vec = NULL;
375 /* Adds the name BP to the list of drivers to configure into
378 outp_configure_add (char *bp)
380 char *ep = &bp[strlen (bp)];
381 if (!search_names (bp, ep))
382 add_name (bp, ep, OUTP_S_COMMAND_LINE);
385 /* Defines one configuration macro based on the text in BP, which
386 should be of the form `KEY=VALUE'. */
388 outp_configure_macro (char *bp)
393 while (isspace ((unsigned char) *bp))
396 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
399 d = xmalloc (sizeof *d);
400 d->key = xmalloc (ep - bp + 1);
401 memcpy (d->key, bp, ep - bp);
404 /* Earlier definitions for a particular KEY override later ones. */
405 if (find_defn_value (d->key))
414 while (isspace ((unsigned char) *ep))
416 d->value = fn_interp_vars (ep, find_defn_value);
417 d->next = outp_macros;
420 outp_macros->prev = d;
424 /* Destroys all the drivers in driver list *DL and sets *DL to
427 destroy_list (struct outp_driver ** dl)
429 struct outp_driver *d, *next;
431 for (d = *dl; d; d = next)
440 /* Closes all the output drivers. */
445 if (iterating_driver_list)
448 destroy_list (&outp_driver_list);
453 /* Display on stdout a list of all registered driver classes. */
455 outp_list_classes (void)
457 int width = set_viewwidth;
458 struct outp_driver_class_list *c;
460 printf (_("Driver classes:\n\t"));
462 for (c = outp_class_list; c; c = c->next)
464 if ((int) strlen (c->class->name) + 1 > width)
467 width = set_viewwidth - 8;
471 fputs (c->class->name, stdout);
476 static int op_token; /* `=', 'a', 0. */
477 static struct string op_tokstr;
480 /* Parses a token from prog into op_token, op_tokstr. Sets op_token
481 to '=' on an equals sign, to 'a' on a string or identifier token,
482 or to 0 at end of line. Returns the new op_token. */
488 msg (IS, _("Syntax error."));
492 while (isspace ((unsigned char) *prog))
504 ds_clear (&op_tokstr);
506 if (*prog == '\'' || *prog == '"')
510 while (*prog && *prog != quote)
513 ds_putchar (&op_tokstr, *prog++);
519 assert ((int) *prog); /* How could a line end in `\'? */
568 while (*prog >= '0' && *prog <= '7')
569 c = c * 8 + *prog++ - '0';
576 while (isxdigit ((unsigned char) *prog))
579 if (isdigit ((unsigned char) *prog))
582 c += (tolower ((unsigned char) (*prog))
589 msg (IS, _("Syntax error in string constant."));
591 ds_putchar (&op_tokstr, (unsigned char) c);
597 while (*prog && !isspace ((unsigned char) *prog) && *prog != '=')
598 ds_putchar (&op_tokstr, *prog++);
605 /* Applies the user-specified options in string S to output driver D
606 (at configuration time). */
608 parse_options (char *s, struct outp_driver * d)
613 ds_init (NULL, &op_tokstr, 64);
620 msg (IS, _("Syntax error in options."));
624 ds_truncate (&op_tokstr, 64);
625 strcpy (key, ds_value (&op_tokstr));
630 msg (IS, _("Syntax error in options (`=' expected)."));
637 msg (IS, _("Syntax error in options (value expected after `=')."));
640 d->class->option (d, key, &op_tokstr);
642 ds_destroy (&op_tokstr);
645 /* Find the driver in outp_driver_list with name NAME. */
646 static struct outp_driver *
647 find_driver (char *name)
649 struct outp_driver *d;
652 if (iterating_driver_list)
655 for (d = outp_driver_list; d; d = d->next)
656 if (!strcmp (d->name, name))
661 /* Tokenize string S into colon-separated fields, removing leading and
662 trailing whitespace on tokens. Returns a pointer to the
663 null-terminated token, which is formed by setting a NUL character
664 into the string. After the first call, subsequent calls should set
665 S to NULL. CP should be consistent across calls. Returns NULL
666 after all fields have been used up.
668 FIXME: Should ignore colons inside double quotes. */
670 colon_tokenize (char *s, char **cp)
680 token = s += strspn (s, " \t\v\r");
681 *cp = strchr (s, ':');
683 s = *cp = strchr (s, 0);
686 while (s > token && strchr (" \t\v\r", s[-1]))
692 /* String S is in format:
693 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
694 Adds a driver to outp_driver_list pursuant to the specification
697 configure_driver (char *s)
700 struct outp_driver *d = NULL, *iter;
701 struct outp_driver_class_list *c = NULL;
703 s = fn_interp_vars (s, find_defn_value);
706 token = colon_tokenize (s, &cp);
709 msg (IS, _("Driver name expected."));
713 d = xmalloc (sizeof *d);
716 d->name = xstrdup (token);
720 d->next = d->prev = NULL;
722 d->device = OUTP_DEV_NONE;
727 token = colon_tokenize (NULL, &cp);
730 msg (IS, _("Class name expected."));
734 for (c = outp_class_list; c; c = c->next)
735 if (!strcmp (c->class->name, token))
739 msg (IS, _("Unknown output driver class `%s'."), token);
744 if (!c->ref_count && !d->class->open_global (d->class))
746 msg (IS, _("Can't initialize output driver class `%s'."),
751 if (!d->class->preopen_driver (d))
753 msg (IS, _("Can't initialize output driver `%s' of class `%s'."),
754 d->name, d->class->name);
759 token = colon_tokenize (NULL, &cp);
764 for (type = strtok_r (token, " \t\r\v", &sp); type;
765 type = strtok_r (NULL, " \t\r\v", &sp))
767 if (!strcmp (type, "listing"))
768 d->device |= OUTP_DEV_LISTING;
769 else if (!strcmp (type, "screen"))
770 d->device |= OUTP_DEV_SCREEN;
771 else if (!strcmp (type, "printer"))
772 d->device |= OUTP_DEV_PRINTER;
775 msg (IS, _("Unknown device type `%s'."), type);
782 token = colon_tokenize (NULL, &cp);
784 parse_options (token, d);
785 if (!d->class->postopen_driver (d))
787 msg (IS, _("Can't complete initialization of output driver `%s' of "
788 "class `%s'."), d->name, d->class->name);
792 /* Find like-named driver and delete. */
793 iter = find_driver (d->name);
795 destroy_driver (iter);
798 d->next = outp_driver_list;
800 if (outp_driver_list)
801 outp_driver_list->prev = d;
802 outp_driver_list = d;
812 /* Destroys output driver D. */
814 destroy_driver (struct outp_driver *d)
817 d->class->close_page (d);
820 struct outp_driver_class_list *c;
823 d->class->close_driver (d);
825 for (c = outp_class_list; c; c = c->next)
826 if (c->class == d->class)
831 if (c->ref_count == 0)
833 if (!d->class->close_global (d->class))
834 msg (IS, _("Can't deinitialize output driver class `%s'."),
840 /* Remove this driver from the global driver list. */
842 d->prev->next = d->next;
844 d->next->prev = d->prev;
845 if (d == outp_driver_list)
846 outp_driver_list = d->next;
850 option_cmp (const void *a, const void *b)
852 const struct outp_option *o1 = a;
853 const struct outp_option *o2 = b;
854 return strcmp (o1->keyword, o2->keyword);
857 /* Tries to match S as one of the keywords in TAB, with corresponding
858 information structure INFO. Returns category code or 0 on failure;
859 if category code is negative then stores subcategory in *SUBCAT. */
861 outp_match_keyword (const char *s, struct outp_option *tab,
862 struct outp_option_info *info, int *subcat)
865 struct outp_option *oip;
867 /* Form hash table. */
868 if (NULL == info->initial)
873 struct outp_option *ptr[255], **oip;
875 for (count = 0; tab[count].keyword[0]; count++)
879 qsort (tab, count, sizeof *tab, option_cmp);
883 *cp = tab[0].keyword[0];
885 for (i = 0; i < count; i++)
886 if (tab[i].keyword[0] != *cp)
888 *++cp = tab[i].keyword[0];
893 info->initial = xstrdup (s);
894 info->options = xmalloc (sizeof *info->options * (cp - s));
895 memcpy (info->options, ptr, sizeof *info->options * (cp - s));
899 oip = *info->options;
903 cp = strchr (info->initial, s[0]);
907 printf (_("Trying to find keyword `%s'...\n"), s);
909 oip = info->options[cp - info->initial];
910 while (oip->keyword[0] == s[0])
913 printf ("- %s\n", oip->keyword);
915 if (!strcmp (s, oip->keyword))
918 *subcat = oip->subcat;
927 /* Encapsulate two characters in a single int. */
928 #define TWO_CHARS(A, B) \
931 /* Determines the size of a dimensional measurement and returns the
932 size in units of 1/72000". Units if not specified explicitly are
933 inches for values under 50, millimeters otherwise. Returns 0,
934 stores NULL to *TAIL on error; otherwise returns dimension, stores
937 outp_evaluate_dimension (char *dimen, char **tail)
943 value = strtod (s, &ptail);
950 b = strtod (s, &ptail);
951 if (b <= 0.0 || ptail == s)
956 c = strtod (s, &ptail);
957 if (c <= 0.0 || ptail == s)
960 if (approx_eq (c, 0.0))
967 else if (*ptail == '/')
971 b = strtod (s, &ptail);
972 if (approx_le (b, 0.0) || ptail == s)
979 if (*s == 0 || isspace ((unsigned char) *s))
984 value *= 72000 / 25.4;
990 /* Standard TeX units are supported. */
994 switch (TWO_CHARS (s[0], s[1]))
996 case TWO_CHARS ('p', 't'):
997 factor = 72000 / 72.27;
999 case TWO_CHARS ('p', 'c'):
1000 factor = 72000 / 72.27 * 12.0;
1002 case TWO_CHARS ('i', 'n'):
1005 case TWO_CHARS ('b', 'p'):
1006 factor = 72000 / 72.0;
1008 case TWO_CHARS ('c', 'm'):
1009 factor = 72000 / 2.54;
1011 case TWO_CHARS ('m', 'm'):
1012 factor = 72000 / 25.4;
1014 case TWO_CHARS ('d', 'd'):
1015 factor = 72000 / 72.27 * 1.0700086;
1017 case TWO_CHARS ('c', 'c'):
1018 factor = 72000 / 72.27 * 12.840104;
1020 case TWO_CHARS ('s', 'p'):
1021 factor = 72000 / 72.27 / 65536.0;
1024 msg (SE, _("Unit \"%s\" is unknown in dimension \"%s\"."), s, dimen);
1031 if (approx_lt (value, 0.0))
1039 msg (SE, _("Bad dimension \"%s\"."), dimen);
1043 /* Stores the dimensions in 1/72000" units of paper identified by
1044 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
1045 of HORZ and VERT are dimensions, into *H and *V. Return nonzero on
1048 internal_get_paper_size (char *size, int *h, int *v)
1052 while (isspace ((unsigned char) *size))
1054 *h = outp_evaluate_dimension (size, &tail);
1057 while (isspace ((unsigned char) *tail))
1061 else if (*tail == 'b' && tail[1] == 'y')
1065 msg (SE, _("`x' expected in paper size `%s'."), size);
1068 *v = outp_evaluate_dimension (tail, &tail);
1071 while (isspace ((unsigned char) *tail))
1075 msg (SE, _("Trailing garbage `%s' on paper size `%s'."), tail, size);
1082 /* Stores the dimensions, in 1/72000" units, of paper identified by
1083 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
1084 V', or it may be a case-insensitive paper identifier, which is
1085 looked up in the `papersize' configuration file. Returns nonzero
1086 on success. May modify SIZE. */
1087 /* Don't read further unless you've got a strong stomach. */
1089 outp_get_paper_size (char *size, int *h, int *v)
1098 static struct paper_size cache[4];
1105 struct file_locator where;
1109 int min_value, min_index;
1113 while (isspace ((unsigned char) *size))
1115 if (isdigit ((unsigned char) *size))
1116 return internal_get_paper_size (size, h, v);
1120 while (isspace ((unsigned char) *ep) && ep >= size)
1124 msg (SE, _("Paper size name must not be empty."));
1133 for (i = 0; i < 4; i++)
1134 if (cache[i].name != NULL && !strcasecmp (cache[i].name, size))
1142 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1144 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1148 where.filename = pprsz_fn;
1149 where.line_number = 0;
1150 err_push_file_locator (&where);
1152 if (pprsz_fn == NULL)
1154 msg (IE, _("Cannot find `papersize' configuration file."));
1158 msg (VM (1), _("%s: Opening paper size definition file..."), pprsz_fn);
1159 f = fopen (pprsz_fn, "r");
1162 msg (IE, _("Opening %s: %s."), pprsz_fn, strerror (errno));
1166 ds_init (NULL, &line, 128);
1171 if (!ds_get_config_line (f, &line, &where))
1174 msg (ME, _("Reading %s: %s."), pprsz_fn, strerror (errno));
1177 for (cp = ds_value (&line); isspace ((unsigned char) *cp); cp++);
1182 for (bp = ep = cp + 1; *ep && *ep != '"'; ep++);
1186 if (0 != strcasecmp (bp, size))
1189 for (cp = ep + 1; isspace ((unsigned char) *cp); cp++);
1192 size = xmalloc (ep - bp + 1);
1201 msg (IE, _("Syntax error in paper size definition."));
1204 /* We found the one we want! */
1205 result = internal_get_paper_size (size, h, v);
1208 min_value = cache[0].use;
1210 for (i = 1; i < 4; i++)
1211 if (cache[0].use < min_value)
1213 min_value = cache[i].use;
1216 free (cache[min_index].name);
1217 cache[min_index].name = xstrdup (size);
1218 cache[min_index].use = use;
1219 cache[min_index].h = *h;
1220 cache[min_index].v = *v;
1224 err_pop_file_locator (&where);
1230 msg (VM (2), _("Paper size definition file read successfully."));
1232 msg (VM (1), _("Error reading paper size definition file."));
1237 /* If D is NULL, returns the first enabled driver if any, NULL if
1238 none. Otherwise D must be the last driver returned by this
1239 function, in which case the next enabled driver is returned or NULL
1240 if that was the last. */
1241 struct outp_driver *
1242 outp_drivers (struct outp_driver *d)
1244 #if GLOBAL_DEBUGGING
1245 struct outp_driver *orig_d = d;
1251 d = outp_driver_list;
1258 || (d->device & disabled_devices) != d->device)))
1262 #if GLOBAL_DEBUGGING
1265 if (iterating_driver_list++)
1268 else if (orig_d && !d)
1270 assert (iterating_driver_list == 1);
1271 iterating_driver_list = 0;
1278 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1279 device(s) given in mask DEVICE. */
1281 outp_enable_device (int enable, int device)
1284 disabled_devices &= ~device;
1286 disabled_devices |= device;
1289 /* Ejects the paper on device D, if the page is not blank. */
1291 outp_eject_page (struct outp_driver *d)
1293 if (d->page_open == 0)
1298 d->cp_x = d->cp_y = 0;
1300 if (d->class->close_page (d) == 0)
1301 msg (ME, _("Error closing page on %s device of %s class."),
1302 d->name, d->class->name);
1303 if (d->class->open_page (d) == 0)
1305 msg (ME, _("Error opening page on %s device of %s class."),
1306 d->name, d->class->name);
1313 /* Returns the width of string S, in device units, when output on
1316 outp_string_width (struct outp_driver *d, const char *s)
1318 struct outp_text text;
1320 text.options = OUTP_T_JUST_LEFT;
1321 ls_init (&text.s, (char *) s, strlen (s));
1322 d->class->text_metrics (d, &text);