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
20 /* This #if encloses the rest of the file. */
45 static int postopen (struct file_ext *);
46 static int preclose (struct file_ext *);
49 html_open_global (struct outp_class *this unused)
55 html_close_global (struct outp_class *this unused)
61 html_preopen_driver (struct outp_driver *this)
63 struct html_driver_ext *x;
65 assert (this->driver_open == 0);
66 msg (VM (1), _("HTML driver initializing as `%s'..."), this->name);
68 this->ext = x = xmalloc (sizeof *x);
70 this->horiz = this->vert = 0;
71 this->width = this->length = 0;
73 this->cp_x = this->cp_y = 0;
75 x->prologue_fn = NULL;
77 x->file.filename = NULL;
80 x->file.sequence_no = &x->sequence_no;
82 x->file.postopen = postopen;
83 x->file.preclose = preclose;
91 html_postopen_driver (struct outp_driver *this)
93 struct html_driver_ext *x = this->ext;
95 assert (this->driver_open == 0);
96 if (NULL == x->file.filename)
97 x->file.filename = xstrdup ("pspp.html");
99 if (x->prologue_fn == NULL)
100 x->prologue_fn = xstrdup ("html-prologue");
102 msg (VM (2), _("%s: Initialization complete."), this->name);
103 this->driver_open = 1;
109 html_close_driver (struct outp_driver *this)
111 struct html_driver_ext *x = this->ext;
113 assert (this->driver_open);
114 msg (VM (2), _("%s: Beginning closing..."), this->name);
115 fn_close_ext (&x->file);
116 free (x->prologue_fn);
117 free (x->file.filename);
119 msg (VM (3), _("%s: Finished closing."), this->name);
120 this->driver_open = 0;
125 /* Generic option types. */
133 /* All the options that the HTML driver supports. */
134 static struct outp_option option_tab[] =
137 {"output-file", 1, 0},
138 {"prologue-file", string_arg, 0},
142 static struct outp_option_info option_info;
145 html_option (struct outp_driver *this, const char *key, const struct string *val)
147 struct html_driver_ext *x = this->ext;
150 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
154 msg (SE, _("Unknown configuration parameter `%s' for HTML device "
158 free (x->file.filename);
159 x->file.filename = xstrdup (ds_value (val));
167 dest = &x->prologue_fn;
174 *dest = xstrdup (ds_value (val));
186 /* Variables for the prologue. */
193 static struct html_variable *html_var_tab;
195 /* Searches html_var_tab for a html_variable with key KEY, and returns
196 the associated value. */
198 html_get_var (const char *key)
200 struct html_variable *v;
202 for (v = html_var_tab; v->key; v++)
203 if (!strcmp (key, v->key))
208 /* Writes the HTML prologue to file F. */
210 postopen (struct file_ext *f)
212 static struct html_variable dict[] =
229 struct outp_driver *this = f->param;
230 struct html_driver_ext *x = this->ext;
232 char *prologue_fn = fn_search_path (x->prologue_fn, config_path, NULL);
238 if (prologue_fn == NULL)
240 msg (IE, _("Cannot find HTML prologue. The use of `-vv' "
241 "on the command line is suggested as a debugging aid."));
245 msg (VM (1), _("%s: %s: Opening HTML prologue..."), this->name, prologue_fn);
246 prologue_file = fopen (prologue_fn, "rb");
247 if (prologue_file == NULL)
249 fclose (prologue_file);
251 msg (IE, "%s: %s", prologue_fn, strerror (errno));
255 dict[0].value = version;
257 curtime = time (NULL);
258 loctime = localtime (&curtime);
259 dict[1].value = asctime (loctime);
261 char *cp = strchr (dict[1].value, '\n');
266 /* PORTME: Determine username, net address. */
268 dict[2].value = getenv ("LOGNAME");
270 dict[2].value = getlogin ();
272 dict[2].value = _("nobody");
274 if (gethostname (host, 128) == -1)
276 if (errno == ENAMETOOLONG)
279 strcpy (host, _("nowhere"));
281 dict[3].value = host;
282 #else /* !HAVE_UNISTD_H */
283 dict[2].value = _("nobody");
284 dict[3].value = _("nowhere");
285 #endif /* !HAVE_UNISTD_H */
287 dict[4].value = outp_title ? outp_title : "";
288 dict[5].value = outp_subtitle ? outp_subtitle : "";
290 getl_location (&dict[6].value, NULL);
291 if (dict[6].value == NULL)
292 dict[6].value = "<stdin>";
295 while (-1 != getline (&buf, &buf_size, prologue_file))
300 if (strstr (buf, "!!!"))
304 char *cp = strstr (buf, "!title");
307 if (outp_title == NULL)
315 char *cp = strstr (buf, "!subtitle");
318 if (outp_subtitle == NULL)
325 /* PORTME: Line terminator. */
326 buf2 = fn_interp_vars (buf, html_get_var);
328 fwrite (buf2, len, 1, f->file);
329 if (buf2[len - 1] != '\n')
330 putc ('\n', f->file);
333 if (ferror (f->file))
334 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
335 fclose (prologue_file);
340 if (ferror (f->file))
343 msg (VM (2), _("%s: HTML prologue read successfully."), this->name);
347 msg (VM (1), _("%s: Error reading HTML prologue."), this->name);
351 /* Writes the HTML epilogue to file F. */
353 preclose (struct file_ext *f)
358 "<!-- end of file -->\n");
360 if (ferror (f->file))
366 html_open_page (struct outp_driver *this)
368 struct html_driver_ext *x = this->ext;
370 assert (this->driver_open && this->page_open == 0);
372 if (!fn_open_ext (&x->file))
375 msg (ME, _("HTML output driver: %s: %s"), x->file.filename,
380 if (!ferror (x->file.file))
382 return !ferror (x->file.file);
386 html_close_page (struct outp_driver *this)
388 struct html_driver_ext *x = this->ext;
390 assert (this->driver_open && this->page_open);
392 return !ferror (x->file.file);
395 static void output_tab_table (struct outp_driver *, struct tab_table *);
398 html_submit (struct outp_driver *this, struct som_table *s)
400 extern struct som_table_class tab_table_class;
401 struct html_driver_ext *x = this->ext;
403 assert (this->driver_open && this->page_open);
404 if (x->sequence_no == 0 && !html_open_page (this))
406 msg (ME, _("Cannot open first page on HTML device %s."), this->name);
410 if (s->class == &tab_table_class)
411 output_tab_table (this, (struct tab_table *) s->ext);
416 /* Emit HTML to FILE to change from *OLD_ATTR attributes to NEW_ATTR.
417 Sets *OLD_ATTR to NEW_ATTR when done. */
419 change_attributes (FILE *f, int *old_attr, int new_attr)
421 if (*old_attr == new_attr)
424 if (*old_attr & OUTP_F_B)
426 if (*old_attr & OUTP_F_I)
428 if (new_attr & OUTP_F_I)
430 if (new_attr & OUTP_F_B)
433 *old_attr = new_attr;
436 /* Write string S of length LEN to file F, escaping characters as
437 necessary for HTML. */
439 escape_string (FILE *f, char *s, int len)
445 for (bp = cp = s; bp < ep; bp = cp)
447 while (cp < ep && *cp != '&' && *cp != '<' && *cp != '>' && *cp)
450 fwrite (bp, 1, cp - bp, f);
471 change_attributes (f, &attr, 0);
474 /* Write table T to THIS output driver. */
476 output_tab_table (struct outp_driver *this, struct tab_table *t)
478 struct html_driver_ext *x = this->ext;
482 if (t->nr == 1 && t->nc == 1)
484 fputs ("<P>", x->file.file);
485 if (!ls_empty_p (t->cc))
486 escape_string (x->file.file, ls_value (t->cc), ls_length (t->cc));
487 fputs ("</P>\n", x->file.file);
492 fputs ("<TABLE BORDER=1>\n", x->file.file);
494 if (!ls_empty_p (&t->title))
496 fprintf (x->file.file, " <TR>\n <TH COLSPAN=%d>", t->nc);
497 escape_string (x->file.file, ls_value (&t->title),
498 ls_length (&t->title));
499 fputs ("</TH>\n </TR>\n", x->file.file);
504 struct len_string *cc = t->cc;
505 unsigned char *ct = t->ct;
507 for (r = 0; r < t->nr; r++)
511 fputs (" <TR>\n", x->file.file);
512 for (c = 0; c < t->nc; c++, cc++, ct++)
519 && ((struct tab_joined_cell *) ls_value (cc))->hit == tab_hit)
522 if (r < t->t || r >= t->nr - t->b
523 || c < t->l || c >= t->nc - t->r)
527 cp = stpcpy (header, " <T");
530 switch (*ct & TAB_ALIGN_MASK)
533 cp = stpcpy (cp, " ALIGN=RIGHT");
538 cp = stpcpy (cp, " ALIGN=CENTER");
546 struct tab_joined_cell *j =
547 (struct tab_joined_cell *) ls_value (cc);
550 if (j->x2 - j->x1 > 1)
551 cp = spprintf (cp, " COLSPAN=%d", j->x2 - j->x1);
552 if (j->y2 - j->y1 > 1)
553 cp = spprintf (cp, " ROWSPAN=%d", j->y2 - j->y1);
557 fputs (header, x->file.file);
559 if ( ! (*ct & TAB_EMPTY) )
561 char *s = ls_value (cc);
562 size_t l = ls_length (cc);
564 while (l && isspace ((unsigned char) *s))
570 escape_string (x->file.file, s, l);
573 fprintf (x->file.file, "</T%c>\n", tag);
575 fputs (" </TR>\n", x->file.file);
579 fputs ("</TABLE>\n\n", x->file.file);
582 /* HTML driver class. */
583 struct outp_class html_class =
595 html_postopen_driver,
623 #endif /* !NO_HTML */