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
23 #include <libpspp/message.h>
33 #include <libpspp/alloc.h>
34 #include <libpspp/compiler.h>
35 #include <libpspp/message.h>
36 #include <data/filename.h>
38 #include "getlogin_r.h"
42 #include <libpspp/version.h>
43 #include <data/make-file.h>
46 #define _(msgid) gettext (msgid)
49 static int postopen (struct file_ext *);
50 static int preclose (struct file_ext *);
53 html_open_global (struct outp_class *this UNUSED)
59 html_close_global (struct outp_class *this UNUSED)
65 html_preopen_driver (struct outp_driver *this)
67 struct html_driver_ext *x;
69 assert (this->driver_open == 0);
70 msg (VM (1), _("HTML driver initializing as `%s'..."), this->name);
72 this->ext = x = xmalloc (sizeof *x);
74 this->horiz = this->vert = 0;
75 this->width = this->length = 0;
77 this->cp_x = this->cp_y = 0;
79 x->prologue_fn = NULL;
81 x->file.filename = NULL;
84 x->file.sequence_no = &x->sequence_no;
86 x->file.postopen = postopen;
87 x->file.preclose = preclose;
95 html_postopen_driver (struct outp_driver *this)
97 struct html_driver_ext *x = this->ext;
99 assert (this->driver_open == 0);
100 if (NULL == x->file.filename)
101 x->file.filename = xstrdup ("pspp.html");
103 if (x->prologue_fn == NULL)
104 x->prologue_fn = xstrdup ("html-prologue");
106 msg (VM (2), _("%s: Initialization complete."), this->name);
107 this->driver_open = 1;
113 html_close_driver (struct outp_driver *this)
115 struct html_driver_ext *x = this->ext;
117 assert (this->driver_open);
118 msg (VM (2), _("%s: Beginning closing..."), this->name);
119 fn_close_ext (&x->file);
120 free (x->prologue_fn);
121 free (x->file.filename);
123 msg (VM (3), _("%s: Finished closing."), this->name);
124 this->driver_open = 0;
130 /* Link the image contained in FILENAME to the
131 HTML stream in file F. */
133 link_image (struct file_ext *f, char *filename)
136 "<IMG SRC=\"%s\"/>", filename);
138 if (ferror (f->file))
145 /* Generic option types. */
153 /* All the options that the HTML driver supports. */
154 static struct outp_option option_tab[] =
157 {"output-file", 1, 0},
158 {"prologue-file", string_arg, 0},
162 static struct outp_option_info option_info;
165 html_option (struct outp_driver *this, const char *key, const struct string *val)
167 struct html_driver_ext *x = this->ext;
170 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
174 msg (SE, _("Unknown configuration parameter `%s' for HTML device "
178 free (x->file.filename);
179 x->file.filename = xstrdup (ds_c_str (val));
187 dest = &x->prologue_fn;
195 *dest = xstrdup (ds_c_str (val));
203 /* Variables for the prologue. */
210 static struct html_variable *html_var_tab;
212 /* Searches html_var_tab for a html_variable with key KEY, and returns
213 the associated value. */
215 html_get_var (const char *key)
217 struct html_variable *v;
219 for (v = html_var_tab; v->key; v++)
220 if (!strcmp (key, v->key))
225 /* Writes the HTML prologue to file F. */
227 postopen (struct file_ext *f)
229 static struct html_variable dict[] =
239 char login[128], host[128];
243 struct outp_driver *this = f->param;
244 struct html_driver_ext *x = this->ext;
246 char *prologue_fn = fn_search_path (x->prologue_fn, config_path, NULL);
252 if (prologue_fn == NULL)
254 msg (IE, _("Cannot find HTML prologue. The use of `-vv' "
255 "on the command line is suggested as a debugging aid."));
259 msg (VM (1), _("%s: %s: Opening HTML prologue..."), this->name, prologue_fn);
260 prologue_file = fopen (prologue_fn, "rb");
261 if (prologue_file == NULL)
263 fclose (prologue_file);
265 msg (IE, "%s: %s", prologue_fn, strerror (errno));
269 dict[0].value = version;
271 curtime = time (NULL);
272 loctime = localtime (&curtime);
273 dict[1].value = asctime (loctime);
275 char *cp = strchr (dict[1].value, '\n');
280 if (getenv ("LOGNAME") != NULL)
281 str_copy_rpad (login, sizeof login, getenv ("LOGNAME"));
282 else if (getlogin_r (login, sizeof login))
283 strcpy (login, _("nobody"));
284 dict[2].value = login;
287 if (gethostname (host, 128) == -1)
289 if (errno == ENAMETOOLONG)
292 strcpy (host, _("nowhere"));
295 strcpy (host, _("nowhere"));
297 dict[3].value = host;
299 dict[4].value = outp_title ? outp_title : "";
300 dict[5].value = outp_subtitle ? outp_subtitle : "";
303 while (-1 != getline (&buf, &buf_size, prologue_file))
308 if (strstr (buf, "!!!"))
312 char *cp = strstr (buf, "!title");
315 if (outp_title == NULL)
323 char *cp = strstr (buf, "!subtitle");
326 if (outp_subtitle == NULL)
333 /* PORTME: Line terminator. */
334 buf2 = fn_interp_vars (buf, html_get_var);
336 fwrite (buf2, len, 1, f->file);
337 if (buf2[len - 1] != '\n')
338 putc ('\n', f->file);
341 if (ferror (f->file))
342 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
343 fclose (prologue_file);
348 if (ferror (f->file))
351 msg (VM (2), _("%s: HTML prologue read successfully."), this->name);
355 msg (VM (1), _("%s: Error reading HTML prologue."), this->name);
359 /* Writes the HTML epilogue to file F. */
361 preclose (struct file_ext *f)
366 "<!-- end of file -->\n");
368 if (ferror (f->file))
374 html_open_page (struct outp_driver *this)
376 struct html_driver_ext *x = this->ext;
378 assert (this->driver_open && this->page_open == 0);
380 if (!fn_open_ext (&x->file))
383 msg (ME, _("HTML output driver: %s: %s"), x->file.filename,
388 if (!ferror (x->file.file))
390 return !ferror (x->file.file);
394 html_close_page (struct outp_driver *this)
396 struct html_driver_ext *x = this->ext;
398 assert (this->driver_open && this->page_open);
400 return !ferror (x->file.file);
403 static void output_tab_table (struct outp_driver *, struct tab_table *);
406 html_submit (struct outp_driver *this, struct som_entity *s)
408 extern struct som_table_class tab_table_class;
409 struct html_driver_ext *x = this->ext;
411 assert (this->driver_open && this->page_open);
412 if (x->sequence_no == 0 && !html_open_page (this))
414 msg (ME, _("Cannot open first page on HTML device %s."), this->name);
418 assert ( s->class == &tab_table_class ) ;
423 output_tab_table ( this, (struct tab_table *) s->ext);
426 link_image( &x->file, ((struct chart *)s->ext)->filename);
435 /* Write string S of length LEN to file F, escaping characters as
436 necessary for HTML. */
438 escape_string (FILE *f, char *s, int len)
443 for (bp = cp = s; bp < ep; bp = cp)
445 while (cp < ep && *cp != '&' && *cp != '<' && *cp != '>' && *cp)
448 fwrite (bp, 1, cp - bp, f);
469 /* Write table T to THIS output driver. */
471 output_tab_table (struct outp_driver *this, struct tab_table *t)
473 struct html_driver_ext *x = this->ext;
475 if (t->nr == 1 && t->nc == 1)
477 fputs ("<P>", x->file.file);
478 if (!ls_empty_p (t->cc))
479 escape_string (x->file.file, ls_c_str (t->cc), ls_length (t->cc));
480 fputs ("</P>\n", x->file.file);
485 fputs ("<TABLE BORDER=1>\n", x->file.file);
487 if (!ls_empty_p (&t->title))
489 fprintf (x->file.file, " <TR>\n <TH COLSPAN=%d>", t->nc);
490 escape_string (x->file.file, ls_c_str (&t->title),
491 ls_length (&t->title));
492 fputs ("</TH>\n </TR>\n", x->file.file);
497 unsigned char *ct = t->ct;
499 for (r = 0; r < t->nr; r++)
503 fputs (" <TR>\n", x->file.file);
504 for (c = 0; c < t->nc; c++, ct++)
506 struct fixed_string *cc;
510 struct tab_joined_cell *j = NULL;
512 cc = t->cc + c + r * t->nc;
515 j = (struct tab_joined_cell *) ls_c_str (cc);
517 if (j->x1 != c || j->y1 != r)
521 if (r < t->t || r >= t->nr - t->b
522 || c < t->l || c >= t->nc - t->r)
526 cp = stpcpy (header, " <T");
529 switch (*ct & TAB_ALIGN_MASK)
532 cp = stpcpy (cp, " ALIGN=RIGHT");
537 cp = stpcpy (cp, " ALIGN=CENTER");
545 if (j->x2 - j->x1 > 1)
546 cp = spprintf (cp, " COLSPAN=%d", j->x2 - j->x1);
547 if (j->y2 - j->y1 > 1)
548 cp = spprintf (cp, " ROWSPAN=%d", j->y2 - j->y1);
554 fputs (header, x->file.file);
556 if ( ! (*ct & TAB_EMPTY) )
558 char *s = ls_c_str (cc);
559 size_t l = ls_length (cc);
561 while (l && isspace ((unsigned char) *s))
567 escape_string (x->file.file, s, l);
570 fprintf (x->file.file, "</T%c>\n", tag);
572 fputs (" </TR>\n", x->file.file);
576 fputs ("</TABLE>\n\n", x->file.file);
580 html_initialise_chart(struct outp_driver *d UNUSED, struct chart *ch)
585 make_unique_file_stream(&fp, &ch->filename);
590 ch->pl_params = pl_newplparams();
591 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
597 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
604 /* HTML driver class. */
605 struct outp_class html_class =
617 html_postopen_driver,
644 html_initialise_chart,