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
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 struct outp_driver_class_list *outp_class_list;
71 struct outp_driver *outp_driver_list;
76 /* A set of OUTP_DEV_* bits indicating the devices that are
78 static int disabled_devices;
80 static void destroy_driver (struct outp_driver *);
81 static void configure_driver_line (char *);
82 static void configure_driver (const char *, const char *,
83 const char *, const char *);
85 /* Add a class to the class list. */
87 add_class (struct outp_class *class)
89 struct outp_driver_class_list *new_list = xmalloc (sizeof *new_list);
91 new_list->class = class;
92 new_list->ref_count = 0;
96 outp_class_list = new_list;
97 new_list->next = NULL;
101 new_list->next = outp_class_list;
102 outp_class_list = new_list;
106 /* Finds the outp_names in outp_configure_vec with name between BP and
108 static struct outp_names *
109 search_names (char *bp, char *ep)
111 struct outp_names *n;
113 for (n = outp_configure_vec; n; n = n->next)
114 if ((int) strlen (n->name) == ep - bp && !memcmp (n->name, bp, ep - bp))
119 /* Deletes outp_names NAME from outp_configure_vec. */
121 delete_name (struct outp_names * n)
125 n->prev->next = n->next;
127 n->next->prev = n->prev;
128 if (n == outp_configure_vec)
129 outp_configure_vec = n->next;
133 /* Adds the name between BP and EP exclusive to list
134 outp_configure_vec with source SOURCE. */
136 add_name (char *bp, char *ep, int source)
138 struct outp_names *n = xmalloc (sizeof *n);
139 n->name = xmalloc (ep - bp + 1);
140 memcpy (n->name, bp, ep - bp);
141 n->name[ep - bp] = 0;
143 n->next = outp_configure_vec;
145 if (outp_configure_vec)
146 outp_configure_vec->prev = n;
147 outp_configure_vec = n;
150 /* Checks that outp_configure_vec is empty, bitches & clears it if it
153 check_configure_vec (void)
155 struct outp_names *n;
157 for (n = outp_configure_vec; n; n = n->next)
158 if (n->source == OUTP_S_COMMAND_LINE)
159 msg (ME, _("Unknown output driver `%s'."), n->name);
161 msg (IE, _("Output driver `%s' referenced but never defined."), n->name);
162 outp_configure_clear ();
165 /* Searches outp_configure_vec for the name between BP and EP
166 exclusive. If found, it is deleted, then replaced by the names
167 given in EP+1, if any. */
169 expand_name (char *bp, char *ep)
171 struct outp_names *n = search_names (bp, ep);
179 while (isspace ((unsigned char) *bp))
182 while (*ep && !isspace ((unsigned char) *ep))
186 if (!search_names (bp, ep))
187 add_name (bp, ep, OUTP_S_INIT_FILE);
192 /* Looks for a macro with key KEY, and returns the corresponding value
193 if found, or NULL if not. */
195 find_defn_value (const char *key)
197 static char buf[INT_DIGITS + 1];
200 for (d = outp_macros; d; d = d->next)
201 if (!strcmp (key, d->key))
203 if (!strcmp (key, "viewwidth"))
205 sprintf (buf, "%d", get_viewwidth ());
208 else if (!strcmp (key, "viewlength"))
210 sprintf (buf, "%d", get_viewlength ());
217 /* Initializes global variables. */
221 extern struct outp_class ascii_class;
223 extern struct outp_class postscript_class;
224 extern struct outp_class epsf_class;
227 extern struct outp_class html_class;
230 char def[] = "default";
233 add_class (&html_class);
236 add_class (&epsf_class);
237 add_class (&postscript_class);
239 add_class (&ascii_class);
241 add_name (def, &def[strlen (def)], OUTP_S_INIT_FILE);
244 /* Deletes all the output macros. */
248 struct outp_defn *d, *next;
250 for (d = outp_macros; d; d = next)
260 init_default_drivers (void)
262 msg (MM, _("Using default output driver configuration."));
263 configure_driver ("list-ascii", "ascii", "listing",
264 "length=66 width=79 char-set=ascii "
265 "output-file=\"pspp.list\" "
266 "bold-on=\"\" italic-on=\"\" bold-italic-on=\"\"");
269 /* Reads the initialization file; initializes
272 outp_read_devices (void)
280 struct file_locator where;
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_line (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));
354 msg (VM (2), _("Device definition file read successfully."));
355 if (outp_driver_list == NULL)
356 msg (MW, _("No output drivers are active."));
359 msg (VM (1), _("Error reading device definition file."));
361 if (!result || outp_driver_list == NULL)
362 init_default_drivers ();
365 /* Clear the list of drivers to configure. */
367 outp_configure_clear (void)
369 struct outp_names *n, *next;
371 for (n = outp_configure_vec; n; n = next)
377 outp_configure_vec = NULL;
380 /* Adds the name BP to the list of drivers to configure into
383 outp_configure_add (char *bp)
385 char *ep = &bp[strlen (bp)];
386 if (!search_names (bp, ep))
387 add_name (bp, ep, OUTP_S_COMMAND_LINE);
390 /* Defines one configuration macro based on the text in BP, which
391 should be of the form `KEY=VALUE'. */
393 outp_configure_macro (char *bp)
398 while (isspace ((unsigned char) *bp))
401 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
404 d = xmalloc (sizeof *d);
405 d->key = xmalloc (ep - bp + 1);
406 memcpy (d->key, bp, ep - bp);
409 /* Earlier definitions for a particular KEY override later ones. */
410 if (find_defn_value (d->key))
419 while (isspace ((unsigned char) *ep))
421 d->value = fn_interp_vars (ep, find_defn_value);
422 d->next = outp_macros;
425 outp_macros->prev = d;
429 /* Destroys all the drivers in driver list *DL and sets *DL to
432 destroy_list (struct outp_driver ** dl)
434 struct outp_driver *d, *next;
436 for (d = *dl; d; d = next)
445 /* Closes all the output drivers. */
449 struct outp_driver_class_list *n = outp_class_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;
467 /* Display on stdout a list of all registered driver classes. */
469 outp_list_classes (void)
471 int width = get_viewwidth();
472 struct outp_driver_class_list *c;
474 printf (_("Driver classes:\n\t"));
476 for (c = outp_class_list; c; c = c->next)
478 if ((int) strlen (c->class->name) + 1 > width)
481 width = get_viewwidth() - 8;
485 fputs (c->class->name, stdout);
490 static int op_token; /* `=', 'a', 0. */
491 static struct string op_tokstr;
492 static const char *prog;
494 /* Parses a token from prog into op_token, op_tokstr. Sets op_token
495 to '=' on an equals sign, to 'a' on a string or identifier token,
496 or to 0 at end of line. Returns the new op_token. */
502 msg (IS, _("Syntax error."));
506 while (isspace ((unsigned char) *prog))
518 ds_clear (&op_tokstr);
520 if (*prog == '\'' || *prog == '"')
524 while (*prog && *prog != quote)
527 ds_putc (&op_tokstr, *prog++);
533 assert ((int) *prog); /* How could a line end in `\'? */
582 while (*prog >= '0' && *prog <= '7')
583 c = c * 8 + *prog++ - '0';
590 while (isxdigit ((unsigned char) *prog))
593 if (isdigit ((unsigned char) *prog))
596 c += (tolower ((unsigned char) (*prog))
603 msg (IS, _("Syntax error in string constant."));
606 ds_putc (&op_tokstr, (unsigned char) c);
612 while (*prog && !isspace ((unsigned char) *prog) && *prog != '=')
613 ds_putc (&op_tokstr, *prog++);
620 /* Applies the user-specified options in string S to output driver D
621 (at configuration time). */
623 parse_options (const char *s, struct outp_driver * d)
628 ds_init (&op_tokstr, 64);
635 msg (IS, _("Syntax error in options."));
639 ds_truncate (&op_tokstr, 64);
640 strcpy (key, ds_c_str (&op_tokstr));
645 msg (IS, _("Syntax error in options (`=' expected)."));
652 msg (IS, _("Syntax error in options (value expected after `=')."));
655 d->class->option (d, key, &op_tokstr);
657 ds_destroy (&op_tokstr);
660 /* Find the driver in outp_driver_list with name NAME. */
661 static struct outp_driver *
662 find_driver (char *name)
664 struct outp_driver *d;
666 for (d = outp_driver_list; d; d = d->next)
667 if (!strcmp (d->name, name))
672 /* Tokenize string S into colon-separated fields, removing leading and
673 trailing whitespace on tokens. Returns a pointer to the
674 null-terminated token, which is formed by setting a NUL character
675 into the string. After the first call, subsequent calls should set
676 S to NULL. CP should be consistent across calls. Returns NULL
677 after all fields have been used up.
679 FIXME: Should ignore colons inside double quotes. */
681 colon_tokenize (char *s, char **cp)
691 token = s += strspn (s, " \t\v\r");
692 *cp = strchr (s, ':');
694 s = *cp = strchr (s, 0);
697 while (s > token && strchr (" \t\v\r", s[-1]))
703 /* String S is in format:
704 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
705 Adds a driver to outp_driver_list pursuant to the specification
708 configure_driver (const char *driver_name, const char *class_name,
709 const char *device_type, const char *options)
711 struct outp_driver *d = NULL, *iter;
712 struct outp_driver_class_list *c = NULL;
714 d = xmalloc (sizeof *d);
716 d->name = xstrdup (driver_name);
719 d->next = d->prev = NULL;
720 d->device = OUTP_DEV_NONE;
723 for (c = outp_class_list; c; c = c->next)
724 if (!strcmp (c->class->name, class_name))
728 msg (IS, _("Unknown output driver class `%s'."), class_name);
733 if (!c->ref_count && !d->class->open_global (d->class))
735 msg (IS, _("Can't initialize output driver class `%s'."),
740 if (!d->class->preopen_driver (d))
742 msg (IS, _("Can't initialize output driver `%s' of class `%s'."),
743 d->name, d->class->name);
748 if (device_type != NULL)
750 char *copy = xstrdup (device_type);
753 for (type = strtok_r (copy, " \t\r\v", &sp); type;
754 type = strtok_r (NULL, " \t\r\v", &sp))
756 if (!strcmp (type, "listing"))
757 d->device |= OUTP_DEV_LISTING;
758 else if (!strcmp (type, "screen"))
759 d->device |= OUTP_DEV_SCREEN;
760 else if (!strcmp (type, "printer"))
761 d->device |= OUTP_DEV_PRINTER;
764 msg (IS, _("Unknown device type `%s'."), type);
774 parse_options (options, d);
775 if (!d->class->postopen_driver (d))
777 msg (IS, _("Can't complete initialization of output driver `%s' of "
778 "class `%s'."), d->name, d->class->name);
782 /* Find like-named driver and delete. */
783 iter = find_driver (d->name);
785 destroy_driver (iter);
788 d->next = outp_driver_list;
790 if (outp_driver_list)
791 outp_driver_list->prev = d;
792 outp_driver_list = d;
801 /* String S is in format:
802 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
803 Adds a driver to outp_driver_list pursuant to the specification
806 configure_driver_line (char *s)
809 const char *driver_name, *class_name, *device_type, *options;
811 s = fn_interp_vars (s, find_defn_value);
814 driver_name = colon_tokenize (s, &cp);
815 class_name = colon_tokenize (NULL, &cp);
816 device_type = colon_tokenize (NULL, &cp);
817 options = colon_tokenize (NULL, &cp);
818 if (driver_name == NULL || class_name == NULL)
820 msg (IS, _("Driver definition line contains fewer fields "
825 configure_driver (driver_name, class_name, device_type, options);
828 /* Destroys output driver D. */
830 destroy_driver (struct outp_driver *d)
833 d->class->close_page (d);
836 struct outp_driver_class_list *c;
839 d->class->close_driver (d);
841 for (c = outp_class_list; c; c = c->next)
842 if (c->class == d->class)
847 if (c->ref_count == 0)
849 if (!d->class->close_global (d->class))
850 msg (IS, _("Can't deinitialize output driver class `%s'."),
856 /* Remove this driver from the global driver list. */
858 d->prev->next = d->next;
860 d->next->prev = d->prev;
861 if (d == outp_driver_list)
862 outp_driver_list = d->next;
866 option_cmp (const void *a, const void *b)
868 const struct outp_option *o1 = a;
869 const struct outp_option *o2 = b;
870 return strcmp (o1->keyword, o2->keyword);
873 /* Tries to match S as one of the keywords in TAB, with corresponding
874 information structure INFO. Returns category code or 0 on failure;
875 if category code is negative then stores subcategory in *SUBCAT. */
877 outp_match_keyword (const char *s, struct outp_option *tab,
878 struct outp_option_info *info, int *subcat)
881 struct outp_option *oip;
883 /* Form hash table. */
884 if (NULL == info->initial)
889 struct outp_option *ptr[255], **oip;
891 for (count = 0; tab[count].keyword[0]; count++)
895 qsort (tab, count, sizeof *tab, option_cmp);
899 *cp = tab[0].keyword[0];
901 for (i = 0; i < count; i++)
902 if (tab[i].keyword[0] != *cp)
904 *++cp = tab[i].keyword[0];
909 info->initial = xstrdup (s);
910 info->options = xnmalloc (cp - s, sizeof *info->options);
911 memcpy (info->options, ptr, sizeof *info->options * (cp - s));
915 oip = *info->options;
919 cp = strchr (info->initial, s[0]);
923 printf (_("Trying to find keyword `%s'...\n"), s);
925 oip = info->options[cp - info->initial];
926 while (oip->keyword[0] == s[0])
929 printf ("- %s\n", oip->keyword);
931 if (!strcmp (s, oip->keyword))
934 *subcat = oip->subcat;
943 /* Encapsulate two characters in a single int. */
944 #define TWO_CHARS(A, B) \
947 /* Determines the size of a dimensional measurement and returns the
948 size in units of 1/72000". Units if not specified explicitly are
949 inches for values under 50, millimeters otherwise. Returns 0,
950 stores NULL to *TAIL on error; otherwise returns dimension, stores
953 outp_evaluate_dimension (char *dimen, char **tail)
959 value = strtod (s, &ptail);
966 b = strtod (s, &ptail);
967 if (b <= 0.0 || ptail == s)
972 c = strtod (s, &ptail);
973 if (c <= 0.0 || ptail == s)
983 else if (*ptail == '/')
987 b = strtod (s, &ptail);
988 if (b <= 0.0 || ptail == s)
995 if (*s == 0 || isspace ((unsigned char) *s))
1000 value *= 72000 / 25.4;
1006 /* Standard TeX units are supported. */
1008 factor = 72000, s++;
1010 switch (TWO_CHARS (s[0], s[1]))
1012 case TWO_CHARS ('p', 't'):
1013 factor = 72000 / 72.27;
1015 case TWO_CHARS ('p', 'c'):
1016 factor = 72000 / 72.27 * 12.0;
1018 case TWO_CHARS ('i', 'n'):
1021 case TWO_CHARS ('b', 'p'):
1022 factor = 72000 / 72.0;
1024 case TWO_CHARS ('c', 'm'):
1025 factor = 72000 / 2.54;
1027 case TWO_CHARS ('m', 'm'):
1028 factor = 72000 / 25.4;
1030 case TWO_CHARS ('d', 'd'):
1031 factor = 72000 / 72.27 * 1.0700086;
1033 case TWO_CHARS ('c', 'c'):
1034 factor = 72000 / 72.27 * 12.840104;
1036 case TWO_CHARS ('s', 'p'):
1037 factor = 72000 / 72.27 / 65536.0;
1040 msg (SE, _("Unit \"%s\" is unknown in dimension \"%s\"."), s, dimen);
1055 msg (SE, _("Bad dimension \"%s\"."), dimen);
1059 /* Stores the dimensions in 1/72000" units of paper identified by
1060 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
1061 of HORZ and VERT are dimensions, into *H and *V. Return nonzero on
1064 internal_get_paper_size (char *size, int *h, int *v)
1068 while (isspace ((unsigned char) *size))
1070 *h = outp_evaluate_dimension (size, &tail);
1073 while (isspace ((unsigned char) *tail))
1077 else if (*tail == 'b' && tail[1] == 'y')
1081 msg (SE, _("`x' expected in paper size `%s'."), size);
1084 *v = outp_evaluate_dimension (tail, &tail);
1087 while (isspace ((unsigned char) *tail))
1091 msg (SE, _("Trailing garbage `%s' on paper size `%s'."), tail, size);
1098 /* Stores the dimensions, in 1/72000" units, of paper identified by
1099 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
1100 V', or it may be a case-insensitive paper identifier, which is
1101 looked up in the `papersize' configuration file. Returns nonzero
1102 on success. May modify SIZE. */
1103 /* Don't read further unless you've got a strong stomach. */
1105 outp_get_paper_size (char *size, int *h, int *v)
1114 static struct paper_size cache[4];
1121 struct file_locator where;
1125 int min_value, min_index;
1129 while (isspace ((unsigned char) *size))
1131 if (isdigit ((unsigned char) *size))
1132 return internal_get_paper_size (size, h, v);
1136 while (isspace ((unsigned char) *ep) && ep >= size)
1140 msg (SE, _("Paper size name must not be empty."));
1149 for (i = 0; i < 4; i++)
1150 if (cache[i].name != NULL && !strcasecmp (cache[i].name, size))
1158 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1160 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1164 where.filename = pprsz_fn;
1165 where.line_number = 0;
1166 err_push_file_locator (&where);
1167 ds_init (&line, 128);
1169 if (pprsz_fn == NULL)
1171 msg (IE, _("Cannot find `papersize' configuration file."));
1175 msg (VM (1), _("%s: Opening paper size definition file..."), pprsz_fn);
1176 f = fopen (pprsz_fn, "r");
1179 msg (IE, _("Opening %s: %s."), pprsz_fn, strerror (errno));
1187 if (!ds_get_config_line (f, &line, &where))
1190 msg (ME, _("Reading %s: %s."), pprsz_fn, strerror (errno));
1193 for (cp = ds_c_str (&line); isspace ((unsigned char) *cp); cp++);
1198 for (bp = ep = cp + 1; *ep && *ep != '"'; ep++);
1202 if (0 != strcasecmp (bp, size))
1205 for (cp = ep + 1; isspace ((unsigned char) *cp); cp++);
1208 size = xmalloc (ep - bp + 1);
1217 msg (IE, _("Syntax error in paper size definition."));
1220 /* We found the one we want! */
1221 result = internal_get_paper_size (size, h, v);
1224 min_value = cache[0].use;
1226 for (i = 1; i < 4; i++)
1227 if (cache[0].use < min_value)
1229 min_value = cache[i].use;
1232 free (cache[min_index].name);
1233 cache[min_index].name = xstrdup (size);
1234 cache[min_index].use = use;
1235 cache[min_index].h = *h;
1236 cache[min_index].v = *v;
1240 err_pop_file_locator (&where);
1246 msg (VM (2), _("Paper size definition file read successfully."));
1248 msg (VM (1), _("Error reading paper size definition file."));
1253 /* If D is NULL, returns the first enabled driver if any, NULL if
1254 none. Otherwise D must be the last driver returned by this
1255 function, in which case the next enabled driver is returned or NULL
1256 if that was the last. */
1257 struct outp_driver *
1258 outp_drivers (struct outp_driver *d)
1260 #if GLOBAL_DEBUGGING
1261 struct outp_driver *orig_d = d;
1267 d = outp_driver_list;
1274 || (d->device & disabled_devices) != d->device)))
1281 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1282 device(s) given in mask DEVICE. */
1284 outp_enable_device (int enable, int device)
1287 disabled_devices &= ~device;
1289 disabled_devices |= device;
1292 /* Ejects the paper on device D, if the page is not blank. */
1294 outp_eject_page (struct outp_driver *d)
1296 if (d->page_open == 0)
1301 d->cp_x = d->cp_y = 0;
1303 if (d->class->close_page (d) == 0)
1304 msg (ME, _("Error closing page on %s device of %s class."),
1305 d->name, d->class->name);
1306 if (d->class->open_page (d) == 0)
1308 msg (ME, _("Error opening page on %s device of %s class."),
1309 d->name, d->class->name);
1316 /* Returns the width of string S, in device units, when output on
1319 outp_string_width (struct outp_driver *d, const char *s)
1321 struct outp_text text;
1323 text.options = OUTP_T_JUST_LEFT;
1324 ls_init (&text.s, (char *) s, strlen (s));
1325 d->class->text_metrics (d, &text);