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
20 /* This #if encloses the rest of the file. */
41 #include "getlogin_r.h"
46 #include "make-file.h"
49 #define _(msgid) gettext (msgid)
52 static int postopen (struct file_ext *);
53 static int preclose (struct file_ext *);
56 html_open_global (struct outp_class *this UNUSED)
62 html_close_global (struct outp_class *this UNUSED)
68 html_preopen_driver (struct outp_driver *this)
70 struct html_driver_ext *x;
72 assert (this->driver_open == 0);
73 msg (VM (1), _("HTML driver initializing as `%s'..."), this->name);
75 this->ext = x = xmalloc (sizeof *x);
77 this->horiz = this->vert = 0;
78 this->width = this->length = 0;
80 this->cp_x = this->cp_y = 0;
82 x->prologue_fn = NULL;
84 x->file.filename = NULL;
87 x->file.sequence_no = &x->sequence_no;
89 x->file.postopen = postopen;
90 x->file.preclose = preclose;
98 html_postopen_driver (struct outp_driver *this)
100 struct html_driver_ext *x = this->ext;
102 assert (this->driver_open == 0);
103 if (NULL == x->file.filename)
104 x->file.filename = xstrdup ("pspp.html");
106 if (x->prologue_fn == NULL)
107 x->prologue_fn = xstrdup ("html-prologue");
109 msg (VM (2), _("%s: Initialization complete."), this->name);
110 this->driver_open = 1;
116 html_close_driver (struct outp_driver *this)
118 struct html_driver_ext *x = this->ext;
120 assert (this->driver_open);
121 msg (VM (2), _("%s: Beginning closing..."), this->name);
122 fn_close_ext (&x->file);
123 free (x->prologue_fn);
124 free (x->file.filename);
126 msg (VM (3), _("%s: Finished closing."), this->name);
127 this->driver_open = 0;
133 /* Link the image contained in FILENAME to the
134 HTML stream in file F. */
136 link_image (struct file_ext *f, char *filename)
139 "<IMG SRC=\"%s\"/>", filename);
141 if (ferror (f->file))
148 /* Generic option types. */
156 /* All the options that the HTML driver supports. */
157 static struct outp_option option_tab[] =
160 {"output-file", 1, 0},
161 {"prologue-file", string_arg, 0},
165 static struct outp_option_info option_info;
168 html_option (struct outp_driver *this, const char *key, const struct string *val)
170 struct html_driver_ext *x = this->ext;
173 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
177 msg (SE, _("Unknown configuration parameter `%s' for HTML device "
181 free (x->file.filename);
182 x->file.filename = xstrdup (ds_c_str (val));
190 dest = &x->prologue_fn;
198 *dest = xstrdup (ds_c_str (val));
206 /* Variables for the prologue. */
213 static struct html_variable *html_var_tab;
215 /* Searches html_var_tab for a html_variable with key KEY, and returns
216 the associated value. */
218 html_get_var (const char *key)
220 struct html_variable *v;
222 for (v = html_var_tab; v->key; v++)
223 if (!strcmp (key, v->key))
228 /* Writes the HTML prologue to file F. */
230 postopen (struct file_ext *f)
232 static struct html_variable dict[] =
242 char login[128], host[128];
246 struct outp_driver *this = f->param;
247 struct html_driver_ext *x = this->ext;
249 char *prologue_fn = fn_search_path (x->prologue_fn, config_path, NULL);
255 if (prologue_fn == NULL)
257 msg (IE, _("Cannot find HTML prologue. The use of `-vv' "
258 "on the command line is suggested as a debugging aid."));
262 msg (VM (1), _("%s: %s: Opening HTML prologue..."), this->name, prologue_fn);
263 prologue_file = fopen (prologue_fn, "rb");
264 if (prologue_file == NULL)
266 fclose (prologue_file);
268 msg (IE, "%s: %s", prologue_fn, strerror (errno));
272 dict[0].value = version;
274 curtime = time (NULL);
275 loctime = localtime (&curtime);
276 dict[1].value = asctime (loctime);
278 char *cp = strchr (dict[1].value, '\n');
283 if (getenv ("LOGNAME") != NULL)
284 str_copy_rpad (login, sizeof login, getenv ("LOGNAME"));
285 else if (getlogin_r (login, sizeof login))
286 strcpy (login, _("nobody"));
287 dict[2].value = login;
290 if (gethostname (host, 128) == -1)
292 if (errno == ENAMETOOLONG)
295 strcpy (host, _("nowhere"));
298 strcpy (host, _("nowhere"));
300 dict[3].value = host;
302 dict[4].value = outp_title ? outp_title : "";
303 dict[5].value = outp_subtitle ? outp_subtitle : "";
306 while (-1 != getline (&buf, &buf_size, prologue_file))
311 if (strstr (buf, "!!!"))
315 char *cp = strstr (buf, "!title");
318 if (outp_title == NULL)
326 char *cp = strstr (buf, "!subtitle");
329 if (outp_subtitle == NULL)
336 /* PORTME: Line terminator. */
337 buf2 = fn_interp_vars (buf, html_get_var);
339 fwrite (buf2, len, 1, f->file);
340 if (buf2[len - 1] != '\n')
341 putc ('\n', f->file);
344 if (ferror (f->file))
345 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
346 fclose (prologue_file);
351 if (ferror (f->file))
354 msg (VM (2), _("%s: HTML prologue read successfully."), this->name);
358 msg (VM (1), _("%s: Error reading HTML prologue."), this->name);
362 /* Writes the HTML epilogue to file F. */
364 preclose (struct file_ext *f)
369 "<!-- end of file -->\n");
371 if (ferror (f->file))
377 html_open_page (struct outp_driver *this)
379 struct html_driver_ext *x = this->ext;
381 assert (this->driver_open && this->page_open == 0);
383 if (!fn_open_ext (&x->file))
386 msg (ME, _("HTML output driver: %s: %s"), x->file.filename,
391 if (!ferror (x->file.file))
393 return !ferror (x->file.file);
397 html_close_page (struct outp_driver *this)
399 struct html_driver_ext *x = this->ext;
401 assert (this->driver_open && this->page_open);
403 return !ferror (x->file.file);
406 static void output_tab_table (struct outp_driver *, struct tab_table *);
409 html_submit (struct outp_driver *this, struct som_entity *s)
411 extern struct som_table_class tab_table_class;
412 struct html_driver_ext *x = this->ext;
414 assert (this->driver_open && this->page_open);
415 if (x->sequence_no == 0 && !html_open_page (this))
417 msg (ME, _("Cannot open first page on HTML device %s."), this->name);
421 assert ( s->class == &tab_table_class ) ;
426 output_tab_table ( this, (struct tab_table *) s->ext);
429 link_image( &x->file, ((struct chart *)s->ext)->filename);
438 /* Write string S of length LEN to file F, escaping characters as
439 necessary for HTML. */
441 escape_string (FILE *f, char *s, int len)
446 for (bp = cp = s; bp < ep; bp = cp)
448 while (cp < ep && *cp != '&' && *cp != '<' && *cp != '>' && *cp)
451 fwrite (bp, 1, cp - bp, f);
472 /* Write table T to THIS output driver. */
474 output_tab_table (struct outp_driver *this, struct tab_table *t)
476 struct html_driver_ext *x = this->ext;
478 if (t->nr == 1 && t->nc == 1)
480 fputs ("<P>", x->file.file);
481 if (!ls_empty_p (t->cc))
482 escape_string (x->file.file, ls_c_str (t->cc), ls_length (t->cc));
483 fputs ("</P>\n", x->file.file);
488 fputs ("<TABLE BORDER=1>\n", x->file.file);
490 if (!ls_empty_p (&t->title))
492 fprintf (x->file.file, " <TR>\n <TH COLSPAN=%d>", t->nc);
493 escape_string (x->file.file, ls_c_str (&t->title),
494 ls_length (&t->title));
495 fputs ("</TH>\n </TR>\n", x->file.file);
500 unsigned char *ct = t->ct;
502 for (r = 0; r < t->nr; r++)
506 fputs (" <TR>\n", x->file.file);
507 for (c = 0; c < t->nc; c++, ct++)
509 struct fixed_string *cc;
513 struct tab_joined_cell *j = NULL;
515 cc = t->cc + c + r * t->nc;
518 j = (struct tab_joined_cell *) ls_c_str (cc);
520 if (j->x1 != c || j->y1 != r)
524 if (r < t->t || r >= t->nr - t->b
525 || c < t->l || c >= t->nc - t->r)
529 cp = stpcpy (header, " <T");
532 switch (*ct & TAB_ALIGN_MASK)
535 cp = stpcpy (cp, " ALIGN=RIGHT");
540 cp = stpcpy (cp, " ALIGN=CENTER");
548 if (j->x2 - j->x1 > 1)
549 cp = spprintf (cp, " COLSPAN=%d", j->x2 - j->x1);
550 if (j->y2 - j->y1 > 1)
551 cp = spprintf (cp, " ROWSPAN=%d", j->y2 - j->y1);
557 fputs (header, x->file.file);
559 if ( ! (*ct & TAB_EMPTY) )
561 char *s = ls_c_str (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);
583 html_initialise_chart(struct outp_driver *d UNUSED, struct chart *ch)
588 make_unique_file_stream(&fp, &ch->filename);
593 ch->pl_params = pl_newplparams();
594 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
600 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
607 /* HTML driver class. */
608 struct outp_class html_class =
620 html_postopen_driver,
647 html_initialise_chart,
652 #endif /* !NO_HTML */