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
22 #include <libpspp/message.h>
27 #include <libpspp/alloc.h>
28 #include <libpspp/message.h>
29 #include <data/filename.h>
32 #include <libpspp/misc.h>
33 #include <data/settings.h>
34 #include <libpspp/str.h>
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 *);
86 /* Add a class to the class list. */
88 add_class (struct outp_class *class)
90 struct outp_driver_class_list *new_list = xmalloc (sizeof *new_list);
92 new_list->class = class;
93 new_list->ref_count = 0;
97 outp_class_list = new_list;
98 new_list->next = NULL;
102 new_list->next = outp_class_list;
103 outp_class_list = new_list;
107 /* Finds the outp_names in outp_configure_vec with name between BP and
109 static struct outp_names *
110 search_names (char *bp, char *ep)
112 struct outp_names *n;
114 for (n = outp_configure_vec; n; n = n->next)
115 if ((int) strlen (n->name) == ep - bp && !memcmp (n->name, bp, ep - bp))
120 /* Deletes outp_names NAME from outp_configure_vec. */
122 delete_name (struct outp_names * n)
126 n->prev->next = n->next;
128 n->next->prev = n->prev;
129 if (n == outp_configure_vec)
130 outp_configure_vec = n->next;
134 /* Adds the name between BP and EP exclusive to list
135 outp_configure_vec with source SOURCE. */
137 add_name (char *bp, char *ep, int source)
139 struct outp_names *n = xmalloc (sizeof *n);
140 n->name = xmalloc (ep - bp + 1);
141 memcpy (n->name, bp, ep - bp);
142 n->name[ep - bp] = 0;
144 n->next = outp_configure_vec;
146 if (outp_configure_vec)
147 outp_configure_vec->prev = n;
148 outp_configure_vec = n;
151 /* Checks that outp_configure_vec is empty, bitches & clears it if it
154 check_configure_vec (void)
156 struct outp_names *n;
158 for (n = outp_configure_vec; n; n = n->next)
159 if (n->source == OUTP_S_COMMAND_LINE)
160 msg (ME, _("Unknown output driver `%s'."), n->name);
162 msg (IE, _("Output driver `%s' referenced but never defined."), n->name);
163 outp_configure_clear ();
166 /* Searches outp_configure_vec for the name between BP and EP
167 exclusive. If found, it is deleted, then replaced by the names
168 given in EP+1, if any. */
170 expand_name (char *bp, char *ep)
172 struct outp_names *n = search_names (bp, ep);
180 while (isspace ((unsigned char) *bp))
183 while (*ep && !isspace ((unsigned char) *ep))
187 if (!search_names (bp, ep))
188 add_name (bp, ep, OUTP_S_INIT_FILE);
193 /* Looks for a macro with key KEY, and returns the corresponding value
194 if found, or NULL if not. */
196 find_defn_value (const char *key)
198 static char buf[INT_STRLEN_BOUND (int) + 1];
201 for (d = outp_macros; d; d = d->next)
202 if (!strcmp (key, d->key))
204 if (!strcmp (key, "viewwidth"))
206 sprintf (buf, "%d", get_viewwidth ());
209 else if (!strcmp (key, "viewlength"))
211 sprintf (buf, "%d", get_viewlength ());
218 /* Initializes global variables. */
222 extern struct outp_class ascii_class;
223 extern struct outp_class postscript_class;
224 extern struct outp_class epsf_class;
225 extern struct outp_class html_class;
227 char def[] = "default";
229 add_class (&html_class);
230 add_class (&epsf_class);
231 add_class (&postscript_class);
232 add_class (&ascii_class);
234 add_name (def, &def[strlen (def)], OUTP_S_INIT_FILE);
237 /* Deletes all the output macros. */
241 struct outp_defn *d, *next;
243 for (d = outp_macros; d; d = next)
253 init_default_drivers (void)
255 msg (MM, _("Using default output driver configuration."));
256 configure_driver ("list-ascii", "ascii", "listing",
257 "length=66 width=79 char-set=ascii "
258 "output-file=\"pspp.list\" "
259 "bold-on=\"\" italic-on=\"\" bold-italic-on=\"\"");
262 /* Reads the initialization file; initializes
265 outp_read_devices (void)
273 struct file_locator where;
275 init_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_INIT_FILE",
277 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
280 where.filename = init_fn;
281 where.line_number = 0;
282 err_push_file_locator (&where);
284 ds_init (&line, 128);
288 msg (IE, _("Cannot find output initialization file. "
289 "Use `-vvvvv' to view search path."));
293 msg (VM (1), _("%s: Opening device description file..."), init_fn);
294 f = fopen (init_fn, "r");
297 msg (IE, _("Opening %s: %s."), init_fn, strerror (errno));
305 if (!ds_get_config_line (f, &line, &where))
308 msg (ME, _("Reading %s: %s."), init_fn, strerror (errno));
311 for (cp = ds_c_str (&line); isspace ((unsigned char) *cp); cp++);
312 if (!strncmp ("define", cp, 6) && isspace ((unsigned char) cp[6]))
313 outp_configure_macro (&cp[7]);
317 for (ep = cp; *ep && *ep != ':' && *ep != '='; ep++);
319 expand_name (cp, ep);
322 struct outp_names *n = search_names (cp, ep);
325 configure_driver_line (cp);
330 msg (IS, _("Syntax error."));
335 check_configure_vec ();
338 err_pop_file_locator (&where);
339 if (f && -1 == fclose (f))
340 msg (MW, _("Closing %s: %s."), init_fn, strerror (errno));
347 msg (VM (2), _("Device definition file read successfully."));
348 if (outp_driver_list == NULL)
349 msg (MW, _("No output drivers are active."));
352 msg (VM (1), _("Error reading device definition file."));
354 if (!result || outp_driver_list == NULL)
355 init_default_drivers ();
358 /* Clear the list of drivers to configure. */
360 outp_configure_clear (void)
362 struct outp_names *n, *next;
364 for (n = outp_configure_vec; n; n = next)
370 outp_configure_vec = NULL;
373 /* Adds the name BP to the list of drivers to configure into
376 outp_configure_add (char *bp)
378 char *ep = &bp[strlen (bp)];
379 if (!search_names (bp, ep))
380 add_name (bp, ep, OUTP_S_COMMAND_LINE);
383 /* Defines one configuration macro based on the text in BP, which
384 should be of the form `KEY=VALUE'. */
386 outp_configure_macro (char *bp)
391 while (isspace ((unsigned char) *bp))
394 while (*ep && !isspace ((unsigned char) *ep) && *ep != '=')
397 d = xmalloc (sizeof *d);
398 d->key = xmalloc (ep - bp + 1);
399 memcpy (d->key, bp, ep - bp);
402 /* Earlier definitions for a particular KEY override later ones. */
403 if (find_defn_value (d->key))
412 while (isspace ((unsigned char) *ep))
414 d->value = fn_interp_vars (ep, find_defn_value);
415 d->next = outp_macros;
418 outp_macros->prev = d;
422 /* Destroys all the drivers in driver list *DL and sets *DL to
425 destroy_list (struct outp_driver ** dl)
427 struct outp_driver *d, *next;
429 for (d = *dl; d; d = next)
438 /* Closes all the output drivers. */
442 struct outp_driver_class_list *n = outp_class_list ;
443 destroy_list (&outp_driver_list);
447 struct outp_driver_class_list *next = n->next;
451 outp_class_list = NULL;
456 free (outp_subtitle);
457 outp_subtitle = NULL;
460 /* Display on stdout a list of all registered driver classes. */
462 outp_list_classes (void)
464 int width = get_viewwidth();
465 struct outp_driver_class_list *c;
467 printf (_("Driver classes:\n\t"));
469 for (c = outp_class_list; c; c = c->next)
471 if ((int) strlen (c->class->name) + 1 > width)
474 width = get_viewwidth() - 8;
478 fputs (c->class->name, stdout);
483 static int op_token; /* `=', 'a', 0. */
484 static struct string op_tokstr;
485 static const char *prog;
487 /* Parses a token from prog into op_token, op_tokstr. Sets op_token
488 to '=' on an equals sign, to 'a' on a string or identifier token,
489 or to 0 at end of line. Returns the new op_token. */
495 msg (IS, _("Syntax error."));
499 while (isspace ((unsigned char) *prog))
511 ds_clear (&op_tokstr);
513 if (*prog == '\'' || *prog == '"')
517 while (*prog && *prog != quote)
520 ds_putc (&op_tokstr, *prog++);
526 assert ((int) *prog); /* How could a line end in `\'? */
575 while (*prog >= '0' && *prog <= '7')
576 c = c * 8 + *prog++ - '0';
583 while (isxdigit ((unsigned char) *prog))
586 if (isdigit ((unsigned char) *prog))
589 c += (tolower ((unsigned char) (*prog))
596 msg (IS, _("Syntax error in string constant."));
599 ds_putc (&op_tokstr, (unsigned char) c);
605 while (*prog && !isspace ((unsigned char) *prog) && *prog != '=')
606 ds_putc (&op_tokstr, *prog++);
613 /* Applies the user-specified options in string S to output driver D
614 (at configuration time). */
616 parse_options (const char *s, struct outp_driver * d)
621 ds_init (&op_tokstr, 64);
628 msg (IS, _("Syntax error in options."));
632 ds_truncate (&op_tokstr, 64);
633 strcpy (key, ds_c_str (&op_tokstr));
638 msg (IS, _("Syntax error in options (`=' expected)."));
645 msg (IS, _("Syntax error in options (value expected after `=')."));
648 d->class->option (d, key, &op_tokstr);
650 ds_destroy (&op_tokstr);
653 /* Find the driver in outp_driver_list with name NAME. */
654 static struct outp_driver *
655 find_driver (char *name)
657 struct outp_driver *d;
659 for (d = outp_driver_list; d; d = d->next)
660 if (!strcmp (d->name, name))
665 /* Tokenize string S into colon-separated fields, removing leading and
666 trailing whitespace on tokens. Returns a pointer to the
667 null-terminated token, which is formed by setting a NUL character
668 into the string. After the first call, subsequent calls should set
669 S to NULL. CP should be consistent across calls. Returns NULL
670 after all fields have been used up.
672 FIXME: Should ignore colons inside double quotes. */
674 colon_tokenize (char *s, char **cp)
684 token = s += strspn (s, " \t\v\r");
685 *cp = strchr (s, ':');
687 s = *cp = strchr (s, 0);
690 while (s > token && strchr (" \t\v\r", s[-1]))
696 /* String S is in format:
697 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
698 Adds a driver to outp_driver_list pursuant to the specification
701 configure_driver (const char *driver_name, const char *class_name,
702 const char *device_type, const char *options)
704 struct outp_driver *d = NULL, *iter;
705 struct outp_driver_class_list *c = NULL;
707 d = xmalloc (sizeof *d);
709 d->name = xstrdup (driver_name);
712 d->next = d->prev = NULL;
713 d->device = OUTP_DEV_NONE;
716 for (c = outp_class_list; c; c = c->next)
717 if (!strcmp (c->class->name, class_name))
721 msg (IS, _("Unknown output driver class `%s'."), class_name);
726 if (!c->ref_count && !d->class->open_global (d->class))
728 msg (IS, _("Can't initialize output driver class `%s'."),
733 if (!d->class->preopen_driver (d))
735 msg (IS, _("Can't initialize output driver `%s' of class `%s'."),
736 d->name, d->class->name);
741 if (device_type != NULL)
743 char *copy = xstrdup (device_type);
746 for (type = strtok_r (copy, " \t\r\v", &sp); type;
747 type = strtok_r (NULL, " \t\r\v", &sp))
749 if (!strcmp (type, "listing"))
750 d->device |= OUTP_DEV_LISTING;
751 else if (!strcmp (type, "screen"))
752 d->device |= OUTP_DEV_SCREEN;
753 else if (!strcmp (type, "printer"))
754 d->device |= OUTP_DEV_PRINTER;
757 msg (IS, _("Unknown device type `%s'."), type);
767 parse_options (options, d);
768 if (!d->class->postopen_driver (d))
770 msg (IS, _("Can't complete initialization of output driver `%s' of "
771 "class `%s'."), d->name, d->class->name);
775 /* Find like-named driver and delete. */
776 iter = find_driver (d->name);
778 destroy_driver (iter);
781 d->next = outp_driver_list;
783 if (outp_driver_list)
784 outp_driver_list->prev = d;
785 outp_driver_list = d;
794 /* String S is in format:
795 DRIVERNAME:CLASSNAME:DEVICETYPE:OPTIONS
796 Adds a driver to outp_driver_list pursuant to the specification
799 configure_driver_line (char *s)
802 const char *driver_name, *class_name, *device_type, *options;
804 s = fn_interp_vars (s, find_defn_value);
807 driver_name = colon_tokenize (s, &cp);
808 class_name = colon_tokenize (NULL, &cp);
809 device_type = colon_tokenize (NULL, &cp);
810 options = colon_tokenize (NULL, &cp);
811 if (driver_name == NULL || class_name == NULL)
813 msg (IS, _("Driver definition line contains fewer fields "
818 configure_driver (driver_name, class_name, device_type, options);
821 /* Destroys output driver D. */
823 destroy_driver (struct outp_driver *d)
826 d->class->close_page (d);
829 struct outp_driver_class_list *c;
832 d->class->close_driver (d);
834 for (c = outp_class_list; c; c = c->next)
835 if (c->class == d->class)
840 if (c->ref_count == 0)
842 if (!d->class->close_global (d->class))
843 msg (IS, _("Can't deinitialize output driver class `%s'."),
849 /* Remove this driver from the global driver list. */
851 d->prev->next = d->next;
853 d->next->prev = d->prev;
854 if (d == outp_driver_list)
855 outp_driver_list = d->next;
859 option_cmp (const void *a, const void *b)
861 const struct outp_option *o1 = a;
862 const struct outp_option *o2 = b;
863 return strcmp (o1->keyword, o2->keyword);
866 /* Tries to match S as one of the keywords in TAB, with corresponding
867 information structure INFO. Returns category code or 0 on failure;
868 if category code is negative then stores subcategory in *SUBCAT. */
870 outp_match_keyword (const char *s, struct outp_option *tab,
871 struct outp_option_info *info, int *subcat)
874 struct outp_option *oip;
876 /* Form hash table. */
877 if (NULL == info->initial)
882 struct outp_option *ptr[255], **oip;
884 for (count = 0; tab[count].keyword[0]; count++)
888 qsort (tab, count, sizeof *tab, option_cmp);
892 *cp = tab[0].keyword[0];
894 for (i = 0; i < count; i++)
895 if (tab[i].keyword[0] != *cp)
897 *++cp = tab[i].keyword[0];
902 info->initial = xstrdup (s);
903 info->options = xnmalloc (cp - s, sizeof *info->options);
904 memcpy (info->options, ptr, sizeof *info->options * (cp - s));
908 oip = *info->options;
912 cp = strchr (info->initial, s[0]);
916 printf (_("Trying to find keyword `%s'...\n"), s);
918 oip = info->options[cp - info->initial];
919 while (oip->keyword[0] == s[0])
922 printf ("- %s\n", oip->keyword);
924 if (!strcmp (s, oip->keyword))
927 *subcat = oip->subcat;
936 /* Encapsulate two characters in a single int. */
937 #define TWO_CHARS(A, B) \
940 /* Determines the size of a dimensional measurement and returns the
941 size in units of 1/72000". Units if not specified explicitly are
942 inches for values under 50, millimeters otherwise. Returns 0,
943 stores NULL to *TAIL on error; otherwise returns dimension, stores
946 outp_evaluate_dimension (char *dimen, char **tail)
952 value = strtod (s, &ptail);
959 b = strtod (s, &ptail);
960 if (b <= 0.0 || ptail == s)
965 c = strtod (s, &ptail);
966 if (c <= 0.0 || ptail == s)
976 else if (*ptail == '/')
980 b = strtod (s, &ptail);
981 if (b <= 0.0 || ptail == s)
988 if (*s == 0 || isspace ((unsigned char) *s))
993 value *= 72000 / 25.4;
999 /* Standard TeX units are supported. */
1001 factor = 72000, s++;
1003 switch (TWO_CHARS (s[0], s[1]))
1005 case TWO_CHARS ('p', 't'):
1006 factor = 72000 / 72.27;
1008 case TWO_CHARS ('p', 'c'):
1009 factor = 72000 / 72.27 * 12.0;
1011 case TWO_CHARS ('i', 'n'):
1014 case TWO_CHARS ('b', 'p'):
1015 factor = 72000 / 72.0;
1017 case TWO_CHARS ('c', 'm'):
1018 factor = 72000 / 2.54;
1020 case TWO_CHARS ('m', 'm'):
1021 factor = 72000 / 25.4;
1023 case TWO_CHARS ('d', 'd'):
1024 factor = 72000 / 72.27 * 1.0700086;
1026 case TWO_CHARS ('c', 'c'):
1027 factor = 72000 / 72.27 * 12.840104;
1029 case TWO_CHARS ('s', 'p'):
1030 factor = 72000 / 72.27 / 65536.0;
1033 msg (SE, _("Unit \"%s\" is unknown in dimension \"%s\"."), s, dimen);
1048 msg (SE, _("Bad dimension \"%s\"."), dimen);
1052 /* Stores the dimensions in 1/72000" units of paper identified by
1053 SIZE, which is of form `HORZ x VERT' or `HORZ by VERT' where each
1054 of HORZ and VERT are dimensions, into *H and *V. Return nonzero on
1057 internal_get_paper_size (char *size, int *h, int *v)
1061 while (isspace ((unsigned char) *size))
1063 *h = outp_evaluate_dimension (size, &tail);
1066 while (isspace ((unsigned char) *tail))
1070 else if (*tail == 'b' && tail[1] == 'y')
1074 msg (SE, _("`x' expected in paper size `%s'."), size);
1077 *v = outp_evaluate_dimension (tail, &tail);
1080 while (isspace ((unsigned char) *tail))
1084 msg (SE, _("Trailing garbage `%s' on paper size `%s'."), tail, size);
1091 /* Stores the dimensions, in 1/72000" units, of paper identified by
1092 SIZE into *H and *V. SIZE may be a pair of dimensions of form `H x
1093 V', or it may be a case-insensitive paper identifier, which is
1094 looked up in the `papersize' configuration file. Returns nonzero
1095 on success. May modify SIZE. */
1096 /* Don't read further unless you've got a strong stomach. */
1098 outp_get_paper_size (char *size, int *h, int *v)
1107 static struct paper_size cache[4];
1114 struct file_locator where;
1118 int min_value, min_index;
1122 while (isspace ((unsigned char) *size))
1124 if (isdigit ((unsigned char) *size))
1125 return internal_get_paper_size (size, h, v);
1129 while (isspace ((unsigned char) *ep) && ep >= size)
1133 msg (SE, _("Paper size name must not be empty."));
1142 for (i = 0; i < 4; i++)
1143 if (cache[i].name != NULL && !strcasecmp (cache[i].name, size))
1151 pprsz_fn = fn_search_path (fn_getenv_default ("STAT_OUTPUT_PAPERSIZE_FILE",
1153 fn_getenv_default ("STAT_OUTPUT_INIT_PATH",
1157 where.filename = pprsz_fn;
1158 where.line_number = 0;
1159 err_push_file_locator (&where);
1160 ds_init (&line, 128);
1162 if (pprsz_fn == NULL)
1164 msg (IE, _("Cannot find `papersize' configuration file."));
1168 msg (VM (1), _("%s: Opening paper size definition file..."), pprsz_fn);
1169 f = fopen (pprsz_fn, "r");
1172 msg (IE, _("Opening %s: %s."), pprsz_fn, strerror (errno));
1180 if (!ds_get_config_line (f, &line, &where))
1183 msg (ME, _("Reading %s: %s."), pprsz_fn, strerror (errno));
1186 for (cp = ds_c_str (&line); isspace ((unsigned char) *cp); cp++);
1191 for (bp = ep = cp + 1; *ep && *ep != '"'; ep++);
1195 if (0 != strcasecmp (bp, size))
1198 for (cp = ep + 1; isspace ((unsigned char) *cp); cp++);
1201 size = xmalloc (ep - bp + 1);
1210 msg (IE, _("Syntax error in paper size definition."));
1213 /* We found the one we want! */
1214 result = internal_get_paper_size (size, h, v);
1217 min_value = cache[0].use;
1219 for (i = 1; i < 4; i++)
1220 if (cache[0].use < min_value)
1222 min_value = cache[i].use;
1225 free (cache[min_index].name);
1226 cache[min_index].name = xstrdup (size);
1227 cache[min_index].use = use;
1228 cache[min_index].h = *h;
1229 cache[min_index].v = *v;
1233 err_pop_file_locator (&where);
1239 msg (VM (2), _("Paper size definition file read successfully."));
1241 msg (VM (1), _("Error reading paper size definition file."));
1246 /* If D is NULL, returns the first enabled driver if any, NULL if
1247 none. Otherwise D must be the last driver returned by this
1248 function, in which case the next enabled driver is returned or NULL
1249 if that was the last. */
1250 struct outp_driver *
1251 outp_drivers (struct outp_driver *d)
1254 struct outp_driver *orig_d = d;
1260 d = outp_driver_list;
1267 || (d->device & disabled_devices) != d->device)))
1274 /* Enables (if ENABLE is nonzero) or disables (if ENABLE is zero) the
1275 device(s) given in mask DEVICE. */
1277 outp_enable_device (int enable, int device)
1280 disabled_devices &= ~device;
1282 disabled_devices |= device;
1285 /* Ejects the paper on device D, if the page is not blank. */
1287 outp_eject_page (struct outp_driver *d)
1289 if (d->page_open == 0)
1294 d->cp_x = d->cp_y = 0;
1296 if (d->class->close_page (d) == 0)
1297 msg (ME, _("Error closing page on %s device of %s class."),
1298 d->name, d->class->name);
1299 if (d->class->open_page (d) == 0)
1301 msg (ME, _("Error opening page on %s device of %s class."),
1302 d->name, d->class->name);
1309 /* Returns the width of string S, in device units, when output on
1312 outp_string_width (struct outp_driver *d, const char *s)
1314 struct outp_text text;
1316 text.options = OUTP_T_JUST_LEFT;
1317 ls_init (&text.s, (char *) s, strlen (s));
1318 d->class->text_metrics (d, &text);