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);
292 msg (IE, _("Cannot find output initialization file. Use `-vv' to view "
297 msg (VM (1), _("%s: Opening device description file..."), init_fn);
298 f = fopen (init_fn, "r");
301 msg (IE, _("Opening %s: %s."), init_fn, strerror (errno));
305 ds_init (NULL, &line, 128);
310 if (!ds_get_config_line (f, &line, &where))
313 msg (ME, _("Reading %s: %s."), init_fn, strerror (errno));
316 for (cp = ds_value (&line); isspace ((unsigned char) *cp); cp++);
317 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
318 outp_configure_macro (&cp[7]);
322 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
324 expand_name (cp, ep);
327 struct outp_names *n = search_names (cp, ep);
330 configure_driver (cp);
335 msg (IS, _("Syntax error."));
340 check_configure_vec ();
343 err_pop_file_locator (&where);
344 if (f && -1 == fclose (f))
345 msg (MW, _("Closing %s: %s."), init_fn, strerror (errno));
349 if (outp_driver_list == NULL)
350 msg (MW, _("No output drivers are active."));
353 msg (VM (2), _("Device definition file read successfully."));
355 msg (VM (1), _("Error reading device definition file."));
359 /* Clear the list of drivers to configure. */
361 outp_configure_clear (void)
363 struct outp_names *n, *next;
365 for (n = outp_configure_vec; n; n = next)
371 outp_configure_vec = NULL;
374 /* Adds the name BP to the list of drivers to configure into
377 outp_configure_add (char *bp)
379 char *ep = &bp[strlen (bp)];
380 if (!search_names (bp, ep))
381 add_name (bp, ep, OUTP_S_COMMAND_LINE);
384 /* Defines one configuration macro based on the text in BP, which
385 should be of the form `KEY=VALUE'. */
387 outp_configure_macro (char *bp)
392 while (isspace ((unsigned char) *bp))
395 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
398 d = xmalloc (sizeof *d);
399 d->key = xmalloc (ep - bp + 1);
400 memcpy (d->key, bp, ep - bp);
403 /* Earlier definitions for a particular KEY override later ones. */
404 if (find_defn_value (d->key))
413 while (isspace ((unsigned char) *ep))
415 d->value = fn_interp_vars (ep, find_defn_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. */
444 if (iterating_driver_list)
447 destroy_list (&outp_driver_list);
452 /* Display on stdout a list of all registered driver classes. */
454 outp_list_classes (void)
456 int width = set_viewwidth;
457 struct outp_driver_class_list *c;
459 printf (_("Driver classes:\n\t"));
461 for (c = outp_class_list; c; c = c->next)
463 if ((int) strlen (c->class->name) + 1 > width)
466 width = set_viewwidth - 8;
470 fputs (c->class->name, stdout);
475 static int op_token; /* `=', 'a', 0. */
476 static struct string op_tokstr;
479 /* Parses a token from prog into op_token, op_tokstr. Sets op_token
480 to '=' on an equals sign, to 'a' on a string or identifier token,
481 or to 0 at end of line. Returns the new op_token. */
487 msg (IS, _("Syntax error."));
491 while (isspace ((unsigned char) *prog))
503 ds_clear (&op_tokstr);
505 if (*prog == '\'' || *prog == '"')
509 while (*prog && *prog != quote)
512 ds_putchar (&op_tokstr, *prog++);
518 assert ((int) *prog); /* How could a line end in `\'? */
567 while (*prog >= '0' && *prog <= '7')
568 c = c * 8 + *prog++ - '0';
575 while (isxdigit ((unsigned char) *prog))
578 if (isdigit ((unsigned char) *prog))
581 c += (tolower ((unsigned char) (*prog))
588 msg (IS, _("Syntax error in string constant."));
590 ds_putchar (&op_tokstr, (unsigned char) c);
596 while (*prog && !isspace ((unsigned char) *prog) && *prog != '=')
597 ds_putchar (&op_tokstr, *prog++);
604 /* Applies the user-specified options in string S to output driver D
605 (at configuration time). */
607 parse_options (char *s, struct outp_driver * d)
612 ds_init (NULL, &op_tokstr, 64);
619 msg (IS, _("Syntax error in options."));
623 ds_truncate (&op_tokstr, 64);
624 strcpy (key, ds_value (&op_tokstr));
629 msg (IS, _("Syntax error in options (`=' expected)."));
636 msg (IS, _("Syntax error in options (value expected after `=')."));
639 d->class->option (d, key, &op_tokstr);
641 ds_destroy (&op_tokstr);
644 /* Find the driver in outp_driver_list with name NAME. */
645 static struct outp_driver *
646 find_driver (char *name)
648 struct outp_driver *d;
651 if (iterating_driver_list)
654 for (d = outp_driver_list; d; d = d->next)
655 if (!strcmp (d->name, name))
660 /* Tokenize string S into colon-separated fields, removing leading and
661 trailing whitespace on tokens. Returns a pointer to the
662 null-terminated token, which is formed by setting a NUL character
663 into the string. After the first call, subsequent calls should set
664 S to NULL. CP should be consistent across calls. Returns NULL
665 after all fields have been used up.
667 FIXME: Should ignore colons inside double quotes. */
669 colon_tokenize (char *s, char **cp)
679 token = s += strspn (s, " \t\v\r");
680 *cp = strchr (s, ':');
682 s = *cp = strchr (s, 0);
685 while (s > token && strchr (" \t\v\r", s[-1]))
691 /* String S is in format:
692 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
693 Adds a driver to outp_driver_list pursuant to the specification
696 configure_driver (char *s)
699 struct outp_driver *d = NULL, *iter;
700 struct outp_driver_class_list *c = NULL;
702 s = fn_interp_vars (s, find_defn_value);
705 token = colon_tokenize (s, &cp);
708 msg (IS, _("Driver name expected."));
712 d = xmalloc (sizeof *d);
715 d->name = xstrdup (token);
719 d->next = d->prev = NULL;
721 d->device = OUTP_DEV_NONE;
726 token = colon_tokenize (NULL, &cp);
729 msg (IS, _("Class name expected."));
733 for (c = outp_class_list; c; c = c->next)
734 if (!strcmp (c->class->name, token))
738 msg (IS, _("Unknown output driver class `%s'."), token);
743 if (!c->ref_count && !d->class->open_global (d->class))
745 msg (IS, _("Can't initialize output driver class `%s'."),
750 if (!d->class->preopen_driver (d))
752 msg (IS, _("Can't initialize output driver `%s' of class `%s'."),
753 d->name, d->class->name);
758 token = colon_tokenize (NULL, &cp);
763 for (type = strtok_r (token, " \t\r\v", &sp); type;
764 type = strtok_r (NULL, " \t\r\v", &sp))
766 if (!strcmp (type, "listing"))
767 d->device |= OUTP_DEV_LISTING;
768 else if (!strcmp (type, "screen"))
769 d->device |= OUTP_DEV_SCREEN;
770 else if (!strcmp (type, "printer"))
771 d->device |= OUTP_DEV_PRINTER;
774 msg (IS, _("Unknown device type `%s'."), type);
781 token = colon_tokenize (NULL, &cp);
783 parse_options (token, d);
784 if (!d->class->postopen_driver (d))
786 msg (IS, _("Can't complete initialization of output driver `%s' of "
787 "class `%s'."), d->name, d->class->name);
791 /* Find like-named driver and delete. */
792 iter = find_driver (d->name);
794 destroy_driver (iter);
797 d->next = outp_driver_list;
799 if (outp_driver_list)
800 outp_driver_list->prev = d;
801 outp_driver_list = d;
811 /* Destroys output driver D. */
813 destroy_driver (struct outp_driver *d)
816 d->class->close_page (d);
819 struct outp_driver_class_list *c;
822 d->class->close_driver (d);
824 for (c = outp_class_list; c; c = c->next)
825 if (c->class == d->class)
830 if (c->ref_count == 0)
832 if (!d->class->close_global (d->class))
833 msg (IS, _("Can't deinitialize output driver class `%s'."),
839 /* Remove this driver from the global driver list. */
841 d->prev->next = d->next;
843 d->next->prev = d->prev;
844 if (d == outp_driver_list)
845 outp_driver_list = d->next;
849 option_cmp (const void *a, const void *b)
851 const struct outp_option *o1 = a;
852 const struct outp_option *o2 = b;
853 return strcmp (o1->keyword, o2->keyword);
856 /* Tries to match S as one of the keywords in TAB, with corresponding
857 information structure INFO. Returns category code or 0 on failure;
858 if category code is negative then stores subcategory in *SUBCAT. */
860 outp_match_keyword (const char *s, struct outp_option *tab,
861 struct outp_option_info *info, int *subcat)
864 struct outp_option *oip;
866 /* Form hash table. */
867 if (NULL == info->initial)
872 struct outp_option *ptr[255], **oip;
874 for (count = 0; tab[count].keyword[0]; count++)
878 qsort (tab, count, sizeof *tab, option_cmp);
882 *cp = tab[0].keyword[0];
884 for (i = 0; i < count; i++)
885 if (tab[i].keyword[0] != *cp)
887 *++cp = tab[i].keyword[0];
892 info->initial = xstrdup (s);
893 info->options = xmalloc (sizeof *info->options * (cp - s));
894 memcpy (info->options, ptr, sizeof *info->options * (cp - s));
898 oip = *info->options;
902 cp = strchr (info->initial, s[0]);
906 printf (_("Trying to find keyword `%s'...\n"), s);
908 oip = info->options[cp - info->initial];
909 while (oip->keyword[0] == s[0])
912 printf ("- %s\n", oip->keyword);
914 if (!strcmp (s, oip->keyword))
917 *subcat = oip->subcat;
926 /* Encapsulate two characters in a single int. */
927 #define TWO_CHARS(A, B) \
930 /* Determines the size of a dimensional measurement and returns the
931 size in units of 1/72000". Units if not specified explicitly are
932 inches for values under 50, millimeters otherwise. Returns 0,
933 stores NULL to *TAIL on error; otherwise returns dimension, stores
936 outp_evaluate_dimension (char *dimen, char **tail)
942 value = strtod (s, &ptail);
949 b = strtod (s, &ptail);
950 if (b <= 0.0 || ptail == s)
955 c = strtod (s, &ptail);
956 if (c <= 0.0 || ptail == s)
959 if (approx_eq (c, 0.0))
966 else if (*ptail == '/')
970 b = strtod (s, &ptail);
971 if (approx_le (b, 0.0) || ptail == s)
978 if (*s == 0 || isspace ((unsigned char) *s))
983 value *= 72000 / 25.4;
989 /* Standard TeX units are supported. */
993 switch (TWO_CHARS (s[0], s[1]))
995 case TWO_CHARS ('p', 't'):
996 factor = 72000 / 72.27;
998 case TWO_CHARS ('p', 'c'):
999 factor = 72000 / 72.27 * 12.0;
1001 case TWO_CHARS ('i', 'n'):
1004 case TWO_CHARS ('b', 'p'):
1005 factor = 72000 / 72.0;
1007 case TWO_CHARS ('c', 'm'):
1008 factor = 72000 / 2.54;
1010 case TWO_CHARS ('m', 'm'):
1011 factor = 72000 / 25.4;
1013 case TWO_CHARS ('d', 'd'):
1014 factor = 72000 / 72.27 * 1.0700086;
1016 case TWO_CHARS ('c', 'c'):
1017 factor = 72000 / 72.27 * 12.840104;
1019 case TWO_CHARS ('s', 'p'):
1020 factor = 72000 / 72.27 / 65536.0;
1023 msg (SE, _("Unit \"%s\" is unknown in dimension \"%s\"."), s, dimen);
1030 if (approx_lt (value, 0.0))
1038 msg (SE, _("Bad dimension \"%s\"."), dimen);
1042 /* Stores the dimensions in 1/72000" units of paper identified by
1043 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
1044 of HORZ and VERT are dimensions, into *H and *V. Return nonzero on
1047 internal_get_paper_size (char *size, int *h, int *v)
1051 while (isspace ((unsigned char) *size))
1053 *h = outp_evaluate_dimension (size, &tail);
1056 while (isspace ((unsigned char) *tail))
1060 else if (*tail == 'b' && tail[1] == 'y')
1064 msg (SE, _("`x' expected in paper size `%s'."), size);
1067 *v = outp_evaluate_dimension (tail, &tail);
1070 while (isspace ((unsigned char) *tail))
1074 msg (SE, _("Trailing garbage `%s' on paper size `%s'."), tail, size);
1081 /* Stores the dimensions, in 1/72000" units, of paper identified by
1082 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
1083 V', or it may be a case-insensitive paper identifier, which is
1084 looked up in the `papersize' configuration file. Returns nonzero
1085 on success. May modify SIZE. */
1086 /* Don't read further unless you've got a strong stomach. */
1088 outp_get_paper_size (char *size, int *h, int *v)
1097 static struct paper_size cache[4];
1104 struct file_locator where;
1108 int min_value, min_index;
1112 while (isspace ((unsigned char) *size))
1114 if (isdigit ((unsigned char) *size))
1115 return internal_get_paper_size (size, h, v);
1119 while (isspace ((unsigned char) *ep) && ep >= size)
1123 msg (SE, _("Paper size name must not be empty."));
1132 for (i = 0; i < 4; i++)
1133 if (cache[i].name != NULL && !strcasecmp (cache[i].name, size))
1141 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1143 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1147 where.filename = pprsz_fn;
1148 where.line_number = 0;
1149 err_push_file_locator (&where);
1151 if (pprsz_fn == NULL)
1153 msg (IE, _("Cannot find `papersize' configuration file."));
1157 msg (VM (1), _("%s: Opening paper size definition file..."), pprsz_fn);
1158 f = fopen (pprsz_fn, "r");
1161 msg (IE, _("Opening %s: %s."), pprsz_fn, strerror (errno));
1165 ds_init (NULL, &line, 128);
1170 if (!ds_get_config_line (f, &line, &where))
1173 msg (ME, _("Reading %s: %s."), pprsz_fn, strerror (errno));
1176 for (cp = ds_value (&line); isspace ((unsigned char) *cp); cp++);
1181 for (bp = ep = cp + 1; *ep && *ep != '"'; ep++);
1185 if (0 != strcasecmp (bp, size))
1188 for (cp = ep + 1; isspace ((unsigned char) *cp); cp++);
1191 size = xmalloc (ep - bp + 1);
1200 msg (IE, _("Syntax error in paper size definition."));
1203 /* We found the one we want! */
1204 result = internal_get_paper_size (size, h, v);
1207 min_value = cache[0].use;
1209 for (i = 1; i < 4; i++)
1210 if (cache[0].use < min_value)
1212 min_value = cache[i].use;
1215 free (cache[min_index].name);
1216 cache[min_index].name = xstrdup (size);
1217 cache[min_index].use = use;
1218 cache[min_index].h = *h;
1219 cache[min_index].v = *v;
1223 err_pop_file_locator (&where);
1229 msg (VM (2), _("Paper size definition file read successfully."));
1231 msg (VM (1), _("Error reading paper size definition file."));
1236 /* If D is NULL, returns the first enabled driver if any, NULL if
1237 none. Otherwise D must be the last driver returned by this
1238 function, in which case the next enabled driver is returned or NULL
1239 if that was the last. */
1240 struct outp_driver *
1241 outp_drivers (struct outp_driver *d)
1243 #if GLOBAL_DEBUGGING
1244 struct outp_driver *orig_d = d;
1250 d = outp_driver_list;
1257 || (d->device & disabled_devices) != d->device)))
1261 #if GLOBAL_DEBUGGING
1264 if (iterating_driver_list++)
1267 else if (orig_d && !d)
1269 assert (iterating_driver_list == 1);
1270 iterating_driver_list = 0;
1277 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1278 device(s) given in mask DEVICE. */
1280 outp_enable_device (int enable, int device)
1283 disabled_devices &= ~device;
1285 disabled_devices |= device;
1288 /* Ejects the paper on device D, if the page is not blank. */
1290 outp_eject_page (struct outp_driver *d)
1292 if (d->page_open == 0)
1297 d->cp_x = d->cp_y = 0;
1299 if (d->class->close_page (d) == 0)
1300 msg (ME, _("Error closing page on %s device of %s class."),
1301 d->name, d->class->name);
1302 if (d->class->open_page (d) == 0)
1304 msg (ME, _("Error opening page on %s device of %s class."),
1305 d->name, d->class->name);
1312 /* Returns the width of string S, in device units, when output on
1315 outp_string_width (struct outp_driver *d, const char *s)
1317 struct outp_text text;
1319 text.options = OUTP_T_JUST_LEFT;
1320 ls_init (&text.s, (char *) s, strlen (s));
1321 d->class->text_metrics (d, &text);