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>
30 #include <libpspp/alloc.h>
31 #include <libpspp/compiler.h>
32 #include <libpspp/message.h>
33 #include <data/filename.h>
35 #include "getlogin_r.h"
39 #include <libpspp/version.h>
40 #include <data/make-file.h>
43 #define _(msgid) gettext (msgid)
46 static int postopen (struct file_ext *);
47 static int preclose (struct file_ext *);
50 html_open_global (struct outp_class *this UNUSED)
56 html_close_global (struct outp_class *this UNUSED)
62 html_preopen_driver (struct outp_driver *this)
64 struct html_driver_ext *x;
66 assert (this->driver_open == 0);
67 msg (VM (1), _("HTML driver initializing as `%s'..."), this->name);
69 this->ext = x = xmalloc (sizeof *x);
71 this->horiz = this->vert = 0;
72 this->width = this->length = 0;
74 this->cp_x = this->cp_y = 0;
76 x->prologue_fn = NULL;
78 x->file.filename = NULL;
81 x->file.sequence_no = &x->sequence_no;
83 x->file.postopen = postopen;
84 x->file.preclose = preclose;
92 html_postopen_driver (struct outp_driver *this)
94 struct html_driver_ext *x = this->ext;
96 assert (this->driver_open == 0);
97 if (NULL == x->file.filename)
98 x->file.filename = xstrdup ("pspp.html");
100 if (x->prologue_fn == NULL)
101 x->prologue_fn = xstrdup ("html-prologue");
103 msg (VM (2), _("%s: Initialization complete."), this->name);
104 this->driver_open = 1;
110 html_close_driver (struct outp_driver *this)
112 struct html_driver_ext *x = this->ext;
114 assert (this->driver_open);
115 msg (VM (2), _("%s: Beginning closing..."), this->name);
116 fn_close_ext (&x->file);
117 free (x->prologue_fn);
118 free (x->file.filename);
120 msg (VM (3), _("%s: Finished closing."), this->name);
121 this->driver_open = 0;
127 /* Link the image contained in FILENAME to the
128 HTML stream in file F. */
130 link_image (struct file_ext *f, char *filename)
133 "<IMG SRC=\"%s\"/>", filename);
135 if (ferror (f->file))
142 /* Generic option types. */
150 /* All the options that the HTML driver supports. */
151 static struct outp_option option_tab[] =
154 {"output-file", 1, 0},
155 {"prologue-file", string_arg, 0},
159 static struct outp_option_info option_info;
162 html_option (struct outp_driver *this, const char *key, const struct string *val)
164 struct html_driver_ext *x = this->ext;
167 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
171 msg (SE, _("Unknown configuration parameter `%s' for HTML device "
175 free (x->file.filename);
176 x->file.filename = xstrdup (ds_c_str (val));
184 dest = &x->prologue_fn;
192 *dest = xstrdup (ds_c_str (val));
200 /* Variables for the prologue. */
207 static struct html_variable *html_var_tab;
209 /* Searches html_var_tab for a html_variable with key KEY, and returns
210 the associated value. */
212 html_get_var (const char *key)
214 struct html_variable *v;
216 for (v = html_var_tab; v->key; v++)
217 if (!strcmp (key, v->key))
222 /* Writes the HTML prologue to file F. */
224 postopen (struct file_ext *f)
226 static struct html_variable dict[] =
236 char login[128], host[128];
240 struct outp_driver *this = f->param;
241 struct html_driver_ext *x = this->ext;
243 char *prologue_fn = fn_search_path (x->prologue_fn, config_path, NULL);
249 if (prologue_fn == NULL)
251 msg (IE, _("Cannot find HTML prologue. The use of `-vv' "
252 "on the command line is suggested as a debugging aid."));
256 msg (VM (1), _("%s: %s: Opening HTML prologue..."), this->name, prologue_fn);
257 prologue_file = fopen (prologue_fn, "rb");
258 if (prologue_file == NULL)
260 fclose (prologue_file);
262 msg (IE, "%s: %s", prologue_fn, strerror (errno));
266 dict[0].value = version;
268 curtime = time (NULL);
269 loctime = localtime (&curtime);
270 dict[1].value = asctime (loctime);
272 char *cp = strchr (dict[1].value, '\n');
277 if (getenv ("LOGNAME") != NULL)
278 str_copy_rpad (login, sizeof login, getenv ("LOGNAME"));
279 else if (getlogin_r (login, sizeof login))
280 strcpy (login, _("nobody"));
281 dict[2].value = login;
284 if (gethostname (host, 128) == -1)
286 if (errno == ENAMETOOLONG)
289 strcpy (host, _("nowhere"));
292 strcpy (host, _("nowhere"));
294 dict[3].value = host;
296 dict[4].value = outp_title ? outp_title : "";
297 dict[5].value = outp_subtitle ? outp_subtitle : "";
300 while (-1 != getline (&buf, &buf_size, prologue_file))
305 if (strstr (buf, "!!!"))
309 char *cp = strstr (buf, "!title");
312 if (outp_title == NULL)
320 char *cp = strstr (buf, "!subtitle");
323 if (outp_subtitle == NULL)
330 /* PORTME: Line terminator. */
332 ds_create(&line, buf);
333 fn_interp_vars(&line, html_get_var);
334 len = ds_length(&line);
335 fwrite (ds_c_str(&line), len, 1, f->file);
336 if (ds_c_str(&line)[len - 1] != '\n')
337 putc ('\n', f->file);
340 if (ferror (f->file))
341 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
342 fclose (prologue_file);
347 if (ferror (f->file))
350 msg (VM (2), _("%s: HTML prologue read successfully."), this->name);
354 msg (VM (1), _("%s: Error reading HTML prologue."), this->name);
358 /* Writes the HTML epilogue to file F. */
360 preclose (struct file_ext *f)
365 "<!-- end of file -->\n");
367 if (ferror (f->file))
373 html_open_page (struct outp_driver *this)
375 struct html_driver_ext *x = this->ext;
377 assert (this->driver_open && this->page_open == 0);
379 if (!fn_open_ext (&x->file))
382 msg (ME, _("HTML output driver: %s: %s"), x->file.filename,
387 if (!ferror (x->file.file))
389 return !ferror (x->file.file);
393 html_close_page (struct outp_driver *this)
395 struct html_driver_ext *x = this->ext;
397 assert (this->driver_open && this->page_open);
399 return !ferror (x->file.file);
402 static void output_tab_table (struct outp_driver *, struct tab_table *);
405 html_submit (struct outp_driver *this, struct som_entity *s)
407 extern struct som_table_class tab_table_class;
408 struct html_driver_ext *x = this->ext;
410 assert (this->driver_open && this->page_open);
411 if (x->sequence_no == 0 && !html_open_page (this))
413 msg (ME, _("Cannot open first page on HTML device %s."), this->name);
417 assert ( s->class == &tab_table_class ) ;
422 output_tab_table ( this, (struct tab_table *) s->ext);
425 link_image( &x->file, ((struct chart *)s->ext)->filename);
434 /* Write string S of length LEN to file F, escaping characters as
435 necessary for HTML. */
437 escape_string (FILE *f, char *s, int len)
442 for (bp = cp = s; bp < ep; bp = cp)
444 while (cp < ep && *cp != '&' && *cp != '<' && *cp != '>' && *cp)
447 fwrite (bp, 1, cp - bp, f);
468 /* Write table T to THIS output driver. */
470 output_tab_table (struct outp_driver *this, struct tab_table *t)
472 struct html_driver_ext *x = this->ext;
474 if (t->nr == 1 && t->nc == 1)
476 fputs ("<P>", x->file.file);
477 if (!ls_empty_p (t->cc))
478 escape_string (x->file.file, ls_c_str (t->cc), ls_length (t->cc));
479 fputs ("</P>\n", x->file.file);
484 fputs ("<TABLE BORDER=1>\n", x->file.file);
486 if (!ls_empty_p (&t->title))
488 fprintf (x->file.file, " <TR>\n <TH COLSPAN=%d>", t->nc);
489 escape_string (x->file.file, ls_c_str (&t->title),
490 ls_length (&t->title));
491 fputs ("</TH>\n </TR>\n", x->file.file);
496 unsigned char *ct = t->ct;
498 for (r = 0; r < t->nr; r++)
502 fputs (" <TR>\n", x->file.file);
503 for (c = 0; c < t->nc; c++, ct++)
505 struct fixed_string *cc;
509 struct tab_joined_cell *j = NULL;
511 cc = t->cc + c + r * t->nc;
514 j = (struct tab_joined_cell *) ls_c_str (cc);
516 if (j->x1 != c || j->y1 != r)
520 if (r < t->t || r >= t->nr - t->b
521 || c < t->l || c >= t->nc - t->r)
525 cp = stpcpy (header, " <T");
528 switch (*ct & TAB_ALIGN_MASK)
531 cp = stpcpy (cp, " ALIGN=RIGHT");
536 cp = stpcpy (cp, " ALIGN=CENTER");
544 if (j->x2 - j->x1 > 1)
545 cp = spprintf (cp, " COLSPAN=%d", j->x2 - j->x1);
546 if (j->y2 - j->y1 > 1)
547 cp = spprintf (cp, " ROWSPAN=%d", j->y2 - j->y1);
553 fputs (header, x->file.file);
555 if ( ! (*ct & TAB_EMPTY) )
557 char *s = ls_c_str (cc);
558 size_t l = ls_length (cc);
560 while (l && isspace ((unsigned char) *s))
566 escape_string (x->file.file, s, l);
569 fprintf (x->file.file, "</T%c>\n", tag);
571 fputs (" </TR>\n", x->file.file);
575 fputs ("</TABLE>\n\n", x->file.file);
579 html_initialise_chart(struct outp_driver *d UNUSED, struct chart *ch)
584 make_unique_file_stream(&fp, &ch->filename);
589 ch->pl_params = pl_newplparams();
590 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
596 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
603 /* HTML driver class. */
604 struct outp_class html_class =
616 html_postopen_driver,
643 html_initialise_chart,