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. */
40 #include "getlogin_r.h"
45 #include "make-file.h"
48 #define _(msgid) gettext (msgid)
51 static int postopen (struct file_ext *);
52 static int preclose (struct file_ext *);
55 html_open_global (struct outp_class *this UNUSED)
61 html_close_global (struct outp_class *this UNUSED)
67 html_preopen_driver (struct outp_driver *this)
69 struct html_driver_ext *x;
71 assert (this->driver_open == 0);
72 msg (VM (1), _("HTML driver initializing as `%s'..."), this->name);
74 this->ext = x = xmalloc (sizeof *x);
76 this->horiz = this->vert = 0;
77 this->width = this->length = 0;
79 this->cp_x = this->cp_y = 0;
81 x->prologue_fn = NULL;
83 x->file.filename = NULL;
86 x->file.sequence_no = &x->sequence_no;
88 x->file.postopen = postopen;
89 x->file.preclose = preclose;
97 html_postopen_driver (struct outp_driver *this)
99 struct html_driver_ext *x = this->ext;
101 assert (this->driver_open == 0);
102 if (NULL == x->file.filename)
103 x->file.filename = xstrdup ("pspp.html");
105 if (x->prologue_fn == NULL)
106 x->prologue_fn = xstrdup ("html-prologue");
108 msg (VM (2), _("%s: Initialization complete."), this->name);
109 this->driver_open = 1;
115 html_close_driver (struct outp_driver *this)
117 struct html_driver_ext *x = this->ext;
119 assert (this->driver_open);
120 msg (VM (2), _("%s: Beginning closing..."), this->name);
121 fn_close_ext (&x->file);
122 free (x->prologue_fn);
123 free (x->file.filename);
125 msg (VM (3), _("%s: Finished closing."), this->name);
126 this->driver_open = 0;
132 /* Link the image contained in FILENAME to the
133 HTML stream in file F. */
135 link_image (struct file_ext *f, char *filename)
138 "<IMG SRC=\"%s\"/>", filename);
140 if (ferror (f->file))
147 /* Generic option types. */
155 /* All the options that the HTML driver supports. */
156 static struct outp_option option_tab[] =
159 {"output-file", 1, 0},
160 {"prologue-file", string_arg, 0},
164 static struct outp_option_info option_info;
167 html_option (struct outp_driver *this, const char *key, const struct string *val)
169 struct html_driver_ext *x = this->ext;
172 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
176 msg (SE, _("Unknown configuration parameter `%s' for HTML device "
180 free (x->file.filename);
181 x->file.filename = xstrdup (ds_c_str (val));
189 dest = &x->prologue_fn;
197 *dest = xstrdup (ds_c_str (val));
205 /* Variables for the prologue. */
212 static struct html_variable *html_var_tab;
214 /* Searches html_var_tab for a html_variable with key KEY, and returns
215 the associated value. */
217 html_get_var (const char *key)
219 struct html_variable *v;
221 for (v = html_var_tab; v->key; v++)
222 if (!strcmp (key, v->key))
227 /* Writes the HTML prologue to file F. */
229 postopen (struct file_ext *f)
231 static struct html_variable dict[] =
241 char login[128], host[128];
245 struct outp_driver *this = f->param;
246 struct html_driver_ext *x = this->ext;
248 char *prologue_fn = fn_search_path (x->prologue_fn, config_path, NULL);
254 if (prologue_fn == NULL)
256 msg (IE, _("Cannot find HTML prologue. The use of `-vv' "
257 "on the command line is suggested as a debugging aid."));
261 msg (VM (1), _("%s: %s: Opening HTML prologue..."), this->name, prologue_fn);
262 prologue_file = fopen (prologue_fn, "rb");
263 if (prologue_file == NULL)
265 fclose (prologue_file);
267 msg (IE, "%s: %s", prologue_fn, strerror (errno));
271 dict[0].value = version;
273 curtime = time (NULL);
274 loctime = localtime (&curtime);
275 dict[1].value = asctime (loctime);
277 char *cp = strchr (dict[1].value, '\n');
282 if (getenv ("LOGNAME") != NULL)
283 str_copy_rpad (login, sizeof login, getenv ("LOGNAME"));
284 else if (getlogin_r (login, sizeof login))
285 strcpy (login, _("nobody"));
286 dict[2].value = login;
289 if (gethostname (host, 128) == -1)
291 if (errno == ENAMETOOLONG)
294 strcpy (host, _("nowhere"));
297 strcpy (host, _("nowhere"));
299 dict[3].value = host;
301 dict[4].value = outp_title ? outp_title : "";
302 dict[5].value = outp_subtitle ? outp_subtitle : "";
305 while (-1 != getline (&buf, &buf_size, prologue_file))
310 if (strstr (buf, "!!!"))
314 char *cp = strstr (buf, "!title");
317 if (outp_title == NULL)
325 char *cp = strstr (buf, "!subtitle");
328 if (outp_subtitle == NULL)
335 /* PORTME: Line terminator. */
336 buf2 = fn_interp_vars (buf, html_get_var);
338 fwrite (buf2, len, 1, f->file);
339 if (buf2[len - 1] != '\n')
340 putc ('\n', f->file);
343 if (ferror (f->file))
344 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
345 fclose (prologue_file);
350 if (ferror (f->file))
353 msg (VM (2), _("%s: HTML prologue read successfully."), this->name);
357 msg (VM (1), _("%s: Error reading HTML prologue."), this->name);
361 /* Writes the HTML epilogue to file F. */
363 preclose (struct file_ext *f)
368 "<!-- end of file -->\n");
370 if (ferror (f->file))
376 html_open_page (struct outp_driver *this)
378 struct html_driver_ext *x = this->ext;
380 assert (this->driver_open && this->page_open == 0);
382 if (!fn_open_ext (&x->file))
385 msg (ME, _("HTML output driver: %s: %s"), x->file.filename,
390 if (!ferror (x->file.file))
392 return !ferror (x->file.file);
396 html_close_page (struct outp_driver *this)
398 struct html_driver_ext *x = this->ext;
400 assert (this->driver_open && this->page_open);
402 return !ferror (x->file.file);
405 static void output_tab_table (struct outp_driver *, struct tab_table *);
408 html_submit (struct outp_driver *this, struct som_entity *s)
410 extern struct som_table_class tab_table_class;
411 struct html_driver_ext *x = this->ext;
413 assert (this->driver_open && this->page_open);
414 if (x->sequence_no == 0 && !html_open_page (this))
416 msg (ME, _("Cannot open first page on HTML device %s."), this->name);
420 assert ( s->class == &tab_table_class ) ;
425 output_tab_table ( this, (struct tab_table *) s->ext);
428 link_image( &x->file, ((struct chart *)s->ext)->filename);
437 /* Write string S of length LEN to file F, escaping characters as
438 necessary for HTML. */
440 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 /* Write table T to THIS output driver. */
473 output_tab_table (struct outp_driver *this, struct tab_table *t)
475 struct html_driver_ext *x = this->ext;
477 if (t->nr == 1 && t->nc == 1)
479 fputs ("<P>", x->file.file);
480 if (!ls_empty_p (t->cc))
481 escape_string (x->file.file, ls_c_str (t->cc), ls_length (t->cc));
482 fputs ("</P>\n", x->file.file);
487 fputs ("<TABLE BORDER=1>\n", x->file.file);
489 if (!ls_empty_p (&t->title))
491 fprintf (x->file.file, " <TR>\n <TH COLSPAN=%d>", t->nc);
492 escape_string (x->file.file, ls_c_str (&t->title),
493 ls_length (&t->title));
494 fputs ("</TH>\n </TR>\n", x->file.file);
499 unsigned char *ct = t->ct;
501 for (r = 0; r < t->nr; r++)
505 fputs (" <TR>\n", x->file.file);
506 for (c = 0; c < t->nc; c++, ct++)
508 struct fixed_string *cc;
512 struct tab_joined_cell *j = NULL;
514 cc = t->cc + c + r * t->nc;
517 j = (struct tab_joined_cell *) ls_c_str (cc);
519 if (j->x1 != c || j->y1 != r)
523 if (r < t->t || r >= t->nr - t->b
524 || c < t->l || c >= t->nc - t->r)
528 cp = stpcpy (header, " <T");
531 switch (*ct & TAB_ALIGN_MASK)
534 cp = stpcpy (cp, " ALIGN=RIGHT");
539 cp = stpcpy (cp, " ALIGN=CENTER");
547 if (j->x2 - j->x1 > 1)
548 cp = spprintf (cp, " COLSPAN=%d", j->x2 - j->x1);
549 if (j->y2 - j->y1 > 1)
550 cp = spprintf (cp, " ROWSPAN=%d", j->y2 - j->y1);
556 fputs (header, x->file.file);
558 if ( ! (*ct & TAB_EMPTY) )
560 char *s = ls_c_str (cc);
561 size_t l = ls_length (cc);
563 while (l && isspace ((unsigned char) *s))
569 escape_string (x->file.file, s, l);
572 fprintf (x->file.file, "</T%c>\n", tag);
574 fputs (" </TR>\n", x->file.file);
578 fputs ("</TABLE>\n\n", x->file.file);
582 html_initialise_chart(struct outp_driver *d UNUSED, struct chart *ch)
587 make_unique_file_stream(&fp, &ch->filename);
592 ch->pl_params = pl_newplparams();
593 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
599 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
606 /* HTML driver class. */
607 struct outp_class html_class =
619 html_postopen_driver,
646 html_initialise_chart,
651 #endif /* !NO_HTML */