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_line (char *);
83 static void configure_driver (const char *, const char *,
84 const char *, const char *);
87 /* This mechanism attempts to catch reentrant use of outp_driver_list. */
88 static int iterating_driver_list;
90 #define reentrancy() msg (FE, _("Attempt to iterate driver list reentrantly."))
93 /* Add a class to the class list. */
95 add_class (struct outp_class *class)
97 struct outp_driver_class_list *new_list = xmalloc (sizeof *new_list);
99 new_list->class = class;
100 new_list->ref_count = 0;
102 if (!outp_class_list)
104 outp_class_list = new_list;
105 new_list->next = NULL;
109 new_list->next = outp_class_list;
110 outp_class_list = new_list;
114 /* Finds the outp_names in outp_configure_vec with name between BP and
116 static struct outp_names *
117 search_names (char *bp, char *ep)
119 struct outp_names *n;
121 for (n = outp_configure_vec; n; n = n->next)
122 if ((int) strlen (n->name) == ep - bp && !memcmp (n->name, bp, ep - bp))
127 /* Deletes outp_names NAME from outp_configure_vec. */
129 delete_name (struct outp_names * n)
133 n->prev->next = n->next;
135 n->next->prev = n->prev;
136 if (n == outp_configure_vec)
137 outp_configure_vec = n->next;
141 /* Adds the name between BP and EP exclusive to list
142 outp_configure_vec with source SOURCE. */
144 add_name (char *bp, char *ep, int source)
146 struct outp_names *n = xmalloc (sizeof *n);
147 n->name = xmalloc (ep - bp + 1);
148 memcpy (n->name, bp, ep - bp);
149 n->name[ep - bp] = 0;
151 n->next = outp_configure_vec;
153 if (outp_configure_vec)
154 outp_configure_vec->prev = n;
155 outp_configure_vec = n;
158 /* Checks that outp_configure_vec is empty, bitches & clears it if it
161 check_configure_vec (void)
163 struct outp_names *n;
165 for (n = outp_configure_vec; n; n = n->next)
166 if (n->source == OUTP_S_COMMAND_LINE)
167 msg (ME, _("Unknown output driver `%s'."), n->name);
169 msg (IE, _("Output driver `%s' referenced but never defined."), n->name);
170 outp_configure_clear ();
173 /* Searches outp_configure_vec for the name between BP and EP
174 exclusive. If found, it is deleted, then replaced by the names
175 given in EP+1, if any. */
177 expand_name (char *bp, char *ep)
179 struct outp_names *n = search_names (bp, ep);
187 while (isspace ((unsigned char) *bp))
190 while (*ep && !isspace ((unsigned char) *ep))
194 if (!search_names (bp, ep))
195 add_name (bp, ep, OUTP_S_INIT_FILE);
200 /* Looks for a macro with key KEY, and returns the corresponding value
201 if found, or NULL if not. */
203 find_defn_value (const char *key)
205 static char buf[INT_DIGITS + 1];
208 for (d = outp_macros; d; d = d->next)
209 if (!strcmp (key, d->key))
211 if (!strcmp (key, "viewwidth"))
213 sprintf (buf, "%d", get_viewwidth ());
216 else if (!strcmp (key, "viewlength"))
218 sprintf (buf, "%d", get_viewlength ());
225 /* Initializes global variables. */
229 extern struct outp_class ascii_class;
231 extern struct outp_class postscript_class;
232 extern struct outp_class epsf_class;
235 extern struct outp_class html_class;
238 char def[] = "default";
241 add_class (&html_class);
244 add_class (&epsf_class);
245 add_class (&postscript_class);
247 add_class (&ascii_class);
249 add_name (def, &def[strlen (def)], OUTP_S_INIT_FILE);
252 /* Deletes all the output macros. */
256 struct outp_defn *d, *next;
258 for (d = outp_macros; d; d = next)
268 init_default_drivers (void)
270 msg (MM, _("Using default output driver configuration."));
271 configure_driver ("list-ascii", "ascii", "listing",
272 "length=66 width=79 char-set=ascii "
273 "output-file=\"pspp.list\" "
274 "bold-on=\"\" italic-on=\"\" bold-italic-on=\"\"");
277 /* Reads the initialization file; initializes
280 outp_read_devices (void)
288 struct file_locator where;
291 if (iterating_driver_list)
295 init_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_INIT_FILE",
297 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
300 where.filename = init_fn;
301 where.line_number = 0;
302 err_push_file_locator (&where);
304 ds_init (&line, 128);
308 msg (IE, _("Cannot find output initialization file. "
309 "Use `-vvvvv' to view search path."));
313 msg (VM (1), _("%s: Opening device description file..."), init_fn);
314 f = fopen (init_fn, "r");
317 msg (IE, _("Opening %s: %s."), init_fn, strerror (errno));
325 if (!ds_get_config_line (f, &line, &where))
328 msg (ME, _("Reading %s: %s."), init_fn, strerror (errno));
331 for (cp = ds_c_str (&line); isspace ((unsigned char) *cp); cp++);
332 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
333 outp_configure_macro (&cp[7]);
337 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
339 expand_name (cp, ep);
342 struct outp_names *n = search_names (cp, ep);
345 configure_driver_line (cp);
350 msg (IS, _("Syntax error."));
355 check_configure_vec ();
358 err_pop_file_locator (&where);
359 if (f && -1 == fclose (f))
360 msg (MW, _("Closing %s: %s."), init_fn, strerror (errno));
367 msg (VM (2), _("Device definition file read successfully."));
368 if (outp_driver_list == NULL)
369 msg (MW, _("No output drivers are active."));
372 msg (VM (1), _("Error reading device definition file."));
374 if (!result || outp_driver_list == NULL)
375 init_default_drivers ();
378 /* Clear the list of drivers to configure. */
380 outp_configure_clear (void)
382 struct outp_names *n, *next;
384 for (n = outp_configure_vec; n; n = next)
390 outp_configure_vec = NULL;
393 /* Adds the name BP to the list of drivers to configure into
396 outp_configure_add (char *bp)
398 char *ep = &bp[strlen (bp)];
399 if (!search_names (bp, ep))
400 add_name (bp, ep, OUTP_S_COMMAND_LINE);
403 /* Defines one configuration macro based on the text in BP, which
404 should be of the form `KEY=VALUE'. */
406 outp_configure_macro (char *bp)
411 while (isspace ((unsigned char) *bp))
414 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
417 d = xmalloc (sizeof *d);
418 d->key = xmalloc (ep - bp + 1);
419 memcpy (d->key, bp, ep - bp);
422 /* Earlier definitions for a particular KEY override later ones. */
423 if (find_defn_value (d->key))
432 while (isspace ((unsigned char) *ep))
434 d->value = fn_interp_vars (ep, find_defn_value);
435 d->next = outp_macros;
438 outp_macros->prev = d;
442 /* Destroys all the drivers in driver list *DL and sets *DL to
445 destroy_list (struct outp_driver ** dl)
447 struct outp_driver *d, *next;
449 for (d = *dl; d; d = next)
458 /* Closes all the output drivers. */
462 struct outp_driver_class_list *n = outp_class_list ;
464 if (iterating_driver_list)
467 destroy_list (&outp_driver_list);
471 struct outp_driver_class_list *next = n->next;
475 outp_class_list = NULL;
480 free (outp_subtitle);
481 outp_subtitle = NULL;
484 /* Display on stdout a list of all registered driver classes. */
486 outp_list_classes (void)
488 int width = get_viewwidth();
489 struct outp_driver_class_list *c;
491 printf (_("Driver classes:\n\t"));
493 for (c = outp_class_list; c; c = c->next)
495 if ((int) strlen (c->class->name) + 1 > width)
498 width = get_viewwidth() - 8;
502 fputs (c->class->name, stdout);
507 static int op_token; /* `=', 'a', 0. */
508 static struct string op_tokstr;
509 static const char *prog;
511 /* Parses a token from prog into op_token, op_tokstr. Sets op_token
512 to '=' on an equals sign, to 'a' on a string or identifier token,
513 or to 0 at end of line. Returns the new op_token. */
519 msg (IS, _("Syntax error."));
523 while (isspace ((unsigned char) *prog))
535 ds_clear (&op_tokstr);
537 if (*prog == '\'' || *prog == '"')
541 while (*prog && *prog != quote)
544 ds_putc (&op_tokstr, *prog++);
550 assert ((int) *prog); /* How could a line end in `\'? */
599 while (*prog >= '0' && *prog <= '7')
600 c = c * 8 + *prog++ - '0';
607 while (isxdigit ((unsigned char) *prog))
610 if (isdigit ((unsigned char) *prog))
613 c += (tolower ((unsigned char) (*prog))
620 msg (IS, _("Syntax error in string constant."));
623 ds_putc (&op_tokstr, (unsigned char) c);
629 while (*prog && !isspace ((unsigned char) *prog) && *prog != '=')
630 ds_putc (&op_tokstr, *prog++);
637 /* Applies the user-specified options in string S to output driver D
638 (at configuration time). */
640 parse_options (const char *s, struct outp_driver * d)
645 ds_init (&op_tokstr, 64);
652 msg (IS, _("Syntax error in options."));
656 ds_truncate (&op_tokstr, 64);
657 strcpy (key, ds_c_str (&op_tokstr));
662 msg (IS, _("Syntax error in options (`=' expected)."));
669 msg (IS, _("Syntax error in options (value expected after `=')."));
672 d->class->option (d, key, &op_tokstr);
674 ds_destroy (&op_tokstr);
677 /* Find the driver in outp_driver_list with name NAME. */
678 static struct outp_driver *
679 find_driver (char *name)
681 struct outp_driver *d;
684 if (iterating_driver_list)
687 for (d = outp_driver_list; d; d = d->next)
688 if (!strcmp (d->name, name))
693 /* Tokenize string S into colon-separated fields, removing leading and
694 trailing whitespace on tokens. Returns a pointer to the
695 null-terminated token, which is formed by setting a NUL character
696 into the string. After the first call, subsequent calls should set
697 S to NULL. CP should be consistent across calls. Returns NULL
698 after all fields have been used up.
700 FIXME: Should ignore colons inside double quotes. */
702 colon_tokenize (char *s, char **cp)
712 token = s += strspn (s, " \t\v\r");
713 *cp = strchr (s, ':');
715 s = *cp = strchr (s, 0);
718 while (s > token && strchr (" \t\v\r", s[-1]))
724 /* String S is in format:
725 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
726 Adds a driver to outp_driver_list pursuant to the specification
729 configure_driver (const char *driver_name, const char *class_name,
730 const char *device_type, const char *options)
732 struct outp_driver *d = NULL, *iter;
733 struct outp_driver_class_list *c = NULL;
735 d = xmalloc (sizeof *d);
737 d->name = xstrdup (driver_name);
740 d->next = d->prev = NULL;
741 d->device = OUTP_DEV_NONE;
744 for (c = outp_class_list; c; c = c->next)
745 if (!strcmp (c->class->name, class_name))
749 msg (IS, _("Unknown output driver class `%s'."), class_name);
754 if (!c->ref_count && !d->class->open_global (d->class))
756 msg (IS, _("Can't initialize output driver class `%s'."),
761 if (!d->class->preopen_driver (d))
763 msg (IS, _("Can't initialize output driver `%s' of class `%s'."),
764 d->name, d->class->name);
769 if (device_type != NULL)
771 char *copy = xstrdup (device_type);
774 for (type = strtok_r (copy, " \t\r\v", &sp); type;
775 type = strtok_r (NULL, " \t\r\v", &sp))
777 if (!strcmp (type, "listing"))
778 d->device |= OUTP_DEV_LISTING;
779 else if (!strcmp (type, "screen"))
780 d->device |= OUTP_DEV_SCREEN;
781 else if (!strcmp (type, "printer"))
782 d->device |= OUTP_DEV_PRINTER;
785 msg (IS, _("Unknown device type `%s'."), type);
795 parse_options (options, d);
796 if (!d->class->postopen_driver (d))
798 msg (IS, _("Can't complete initialization of output driver `%s' of "
799 "class `%s'."), d->name, d->class->name);
803 /* Find like-named driver and delete. */
804 iter = find_driver (d->name);
806 destroy_driver (iter);
809 d->next = outp_driver_list;
811 if (outp_driver_list)
812 outp_driver_list->prev = d;
813 outp_driver_list = d;
822 /* String S is in format:
823 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
824 Adds a driver to outp_driver_list pursuant to the specification
827 configure_driver_line (char *s)
830 const char *driver_name, *class_name, *device_type, *options;
832 s = fn_interp_vars (s, find_defn_value);
835 driver_name = colon_tokenize (s, &cp);
836 class_name = colon_tokenize (NULL, &cp);
837 device_type = colon_tokenize (NULL, &cp);
838 options = colon_tokenize (NULL, &cp);
839 if (driver_name == NULL || class_name == NULL)
841 msg (IS, _("Driver definition line contains fewer fields "
846 configure_driver (driver_name, class_name, device_type, options);
849 /* Destroys output driver D. */
851 destroy_driver (struct outp_driver *d)
854 d->class->close_page (d);
857 struct outp_driver_class_list *c;
860 d->class->close_driver (d);
862 for (c = outp_class_list; c; c = c->next)
863 if (c->class == d->class)
868 if (c->ref_count == 0)
870 if (!d->class->close_global (d->class))
871 msg (IS, _("Can't deinitialize output driver class `%s'."),
877 /* Remove this driver from the global driver list. */
879 d->prev->next = d->next;
881 d->next->prev = d->prev;
882 if (d == outp_driver_list)
883 outp_driver_list = d->next;
887 option_cmp (const void *a, const void *b)
889 const struct outp_option *o1 = a;
890 const struct outp_option *o2 = b;
891 return strcmp (o1->keyword, o2->keyword);
894 /* Tries to match S as one of the keywords in TAB, with corresponding
895 information structure INFO. Returns category code or 0 on failure;
896 if category code is negative then stores subcategory in *SUBCAT. */
898 outp_match_keyword (const char *s, struct outp_option *tab,
899 struct outp_option_info *info, int *subcat)
902 struct outp_option *oip;
904 /* Form hash table. */
905 if (NULL == info->initial)
910 struct outp_option *ptr[255], **oip;
912 for (count = 0; tab[count].keyword[0]; count++)
916 qsort (tab, count, sizeof *tab, option_cmp);
920 *cp = tab[0].keyword[0];
922 for (i = 0; i < count; i++)
923 if (tab[i].keyword[0] != *cp)
925 *++cp = tab[i].keyword[0];
930 info->initial = xstrdup (s);
931 info->options = xnmalloc (cp - s, sizeof *info->options);
932 memcpy (info->options, ptr, sizeof *info->options * (cp - s));
936 oip = *info->options;
940 cp = strchr (info->initial, s[0]);
944 printf (_("Trying to find keyword `%s'...\n"), s);
946 oip = info->options[cp - info->initial];
947 while (oip->keyword[0] == s[0])
950 printf ("- %s\n", oip->keyword);
952 if (!strcmp (s, oip->keyword))
955 *subcat = oip->subcat;
964 /* Encapsulate two characters in a single int. */
965 #define TWO_CHARS(A, B) \
968 /* Determines the size of a dimensional measurement and returns the
969 size in units of 1/72000". Units if not specified explicitly are
970 inches for values under 50, millimeters otherwise. Returns 0,
971 stores NULL to *TAIL on error; otherwise returns dimension, stores
974 outp_evaluate_dimension (char *dimen, char **tail)
980 value = strtod (s, &ptail);
987 b = strtod (s, &ptail);
988 if (b <= 0.0 || ptail == s)
993 c = strtod (s, &ptail);
994 if (c <= 0.0 || ptail == s)
1004 else if (*ptail == '/')
1008 b = strtod (s, &ptail);
1009 if (b <= 0.0 || ptail == s)
1016 if (*s == 0 || isspace ((unsigned char) *s))
1021 value *= 72000 / 25.4;
1027 /* Standard TeX units are supported. */
1029 factor = 72000, s++;
1031 switch (TWO_CHARS (s[0], s[1]))
1033 case TWO_CHARS ('p', 't'):
1034 factor = 72000 / 72.27;
1036 case TWO_CHARS ('p', 'c'):
1037 factor = 72000 / 72.27 * 12.0;
1039 case TWO_CHARS ('i', 'n'):
1042 case TWO_CHARS ('b', 'p'):
1043 factor = 72000 / 72.0;
1045 case TWO_CHARS ('c', 'm'):
1046 factor = 72000 / 2.54;
1048 case TWO_CHARS ('m', 'm'):
1049 factor = 72000 / 25.4;
1051 case TWO_CHARS ('d', 'd'):
1052 factor = 72000 / 72.27 * 1.0700086;
1054 case TWO_CHARS ('c', 'c'):
1055 factor = 72000 / 72.27 * 12.840104;
1057 case TWO_CHARS ('s', 'p'):
1058 factor = 72000 / 72.27 / 65536.0;
1061 msg (SE, _("Unit \"%s\" is unknown in dimension \"%s\"."), s, dimen);
1076 msg (SE, _("Bad dimension \"%s\"."), dimen);
1080 /* Stores the dimensions in 1/72000" units of paper identified by
1081 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
1082 of HORZ and VERT are dimensions, into *H and *V. Return nonzero on
1085 internal_get_paper_size (char *size, int *h, int *v)
1089 while (isspace ((unsigned char) *size))
1091 *h = outp_evaluate_dimension (size, &tail);
1094 while (isspace ((unsigned char) *tail))
1098 else if (*tail == 'b' && tail[1] == 'y')
1102 msg (SE, _("`x' expected in paper size `%s'."), size);
1105 *v = outp_evaluate_dimension (tail, &tail);
1108 while (isspace ((unsigned char) *tail))
1112 msg (SE, _("Trailing garbage `%s' on paper size `%s'."), tail, size);
1119 /* Stores the dimensions, in 1/72000" units, of paper identified by
1120 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
1121 V', or it may be a case-insensitive paper identifier, which is
1122 looked up in the `papersize' configuration file. Returns nonzero
1123 on success. May modify SIZE. */
1124 /* Don't read further unless you've got a strong stomach. */
1126 outp_get_paper_size (char *size, int *h, int *v)
1135 static struct paper_size cache[4];
1142 struct file_locator where;
1146 int min_value, min_index;
1150 while (isspace ((unsigned char) *size))
1152 if (isdigit ((unsigned char) *size))
1153 return internal_get_paper_size (size, h, v);
1157 while (isspace ((unsigned char) *ep) && ep >= size)
1161 msg (SE, _("Paper size name must not be empty."));
1170 for (i = 0; i < 4; i++)
1171 if (cache[i].name != NULL && !strcasecmp (cache[i].name, size))
1179 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1181 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1185 where.filename = pprsz_fn;
1186 where.line_number = 0;
1187 err_push_file_locator (&where);
1188 ds_init (&line, 128);
1190 if (pprsz_fn == NULL)
1192 msg (IE, _("Cannot find `papersize' configuration file."));
1196 msg (VM (1), _("%s: Opening paper size definition file..."), pprsz_fn);
1197 f = fopen (pprsz_fn, "r");
1200 msg (IE, _("Opening %s: %s."), pprsz_fn, strerror (errno));
1208 if (!ds_get_config_line (f, &line, &where))
1211 msg (ME, _("Reading %s: %s."), pprsz_fn, strerror (errno));
1214 for (cp = ds_c_str (&line); isspace ((unsigned char) *cp); cp++);
1219 for (bp = ep = cp + 1; *ep && *ep != '"'; ep++);
1223 if (0 != strcasecmp (bp, size))
1226 for (cp = ep + 1; isspace ((unsigned char) *cp); cp++);
1229 size = xmalloc (ep - bp + 1);
1238 msg (IE, _("Syntax error in paper size definition."));
1241 /* We found the one we want! */
1242 result = internal_get_paper_size (size, h, v);
1245 min_value = cache[0].use;
1247 for (i = 1; i < 4; i++)
1248 if (cache[0].use < min_value)
1250 min_value = cache[i].use;
1253 free (cache[min_index].name);
1254 cache[min_index].name = xstrdup (size);
1255 cache[min_index].use = use;
1256 cache[min_index].h = *h;
1257 cache[min_index].v = *v;
1261 err_pop_file_locator (&where);
1267 msg (VM (2), _("Paper size definition file read successfully."));
1269 msg (VM (1), _("Error reading paper size definition file."));
1274 /* If D is NULL, returns the first enabled driver if any, NULL if
1275 none. Otherwise D must be the last driver returned by this
1276 function, in which case the next enabled driver is returned or NULL
1277 if that was the last. */
1278 struct outp_driver *
1279 outp_drivers (struct outp_driver *d)
1281 #if GLOBAL_DEBUGGING
1282 struct outp_driver *orig_d = d;
1288 d = outp_driver_list;
1295 || (d->device & disabled_devices) != d->device)))
1299 #if GLOBAL_DEBUGGING
1302 if (iterating_driver_list++)
1305 else if (orig_d && !d)
1307 assert (iterating_driver_list == 1);
1308 iterating_driver_list = 0;
1315 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1316 device(s) given in mask DEVICE. */
1318 outp_enable_device (int enable, int device)
1321 disabled_devices &= ~device;
1323 disabled_devices |= device;
1326 /* Ejects the paper on device D, if the page is not blank. */
1328 outp_eject_page (struct outp_driver *d)
1330 if (d->page_open == 0)
1335 d->cp_x = d->cp_y = 0;
1337 if (d->class->close_page (d) == 0)
1338 msg (ME, _("Error closing page on %s device of %s class."),
1339 d->name, d->class->name);
1340 if (d->class->open_page (d) == 0)
1342 msg (ME, _("Error opening page on %s device of %s class."),
1343 d->name, d->class->name);
1350 /* Returns the width of string S, in device units, when output on
1353 outp_string_width (struct outp_driver *d, const char *s)
1355 struct outp_text text;
1357 text.options = OUTP_T_JUST_LEFT;
1358 ls_init (&text.s, (char *) s, strlen (s));
1359 d->class->text_metrics (d, &text);