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"
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[] =
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 : "";
305 getl_location (&dict[6].value, NULL);
306 if (dict[6].value == NULL)
307 dict[6].value = "<stdin>";
310 while (-1 != getline (&buf, &buf_size, prologue_file))
315 if (strstr (buf, "!!!"))
319 char *cp = strstr (buf, "!title");
322 if (outp_title == NULL)
330 char *cp = strstr (buf, "!subtitle");
333 if (outp_subtitle == NULL)
340 /* PORTME: Line terminator. */
341 buf2 = fn_interp_vars (buf, html_get_var);
343 fwrite (buf2, len, 1, f->file);
344 if (buf2[len - 1] != '\n')
345 putc ('\n', f->file);
348 if (ferror (f->file))
349 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
350 fclose (prologue_file);
355 if (ferror (f->file))
358 msg (VM (2), _("%s: HTML prologue read successfully."), this->name);
362 msg (VM (1), _("%s: Error reading HTML prologue."), this->name);
366 /* Writes the HTML epilogue to file F. */
368 preclose (struct file_ext *f)
373 "<!-- end of file -->\n");
375 if (ferror (f->file))
381 html_open_page (struct outp_driver *this)
383 struct html_driver_ext *x = this->ext;
385 assert (this->driver_open && this->page_open == 0);
387 if (!fn_open_ext (&x->file))
390 msg (ME, _("HTML output driver: %s: %s"), x->file.filename,
395 if (!ferror (x->file.file))
397 return !ferror (x->file.file);
401 html_close_page (struct outp_driver *this)
403 struct html_driver_ext *x = this->ext;
405 assert (this->driver_open && this->page_open);
407 return !ferror (x->file.file);
410 static void output_tab_table (struct outp_driver *, struct tab_table *);
413 html_submit (struct outp_driver *this, struct som_entity *s)
415 extern struct som_table_class tab_table_class;
416 struct html_driver_ext *x = this->ext;
418 assert (this->driver_open && this->page_open);
419 if (x->sequence_no == 0 && !html_open_page (this))
421 msg (ME, _("Cannot open first page on HTML device %s."), this->name);
425 assert ( s->class == &tab_table_class ) ;
430 output_tab_table ( this, (struct tab_table *) s->ext);
433 link_image( &x->file, ((struct chart *)s->ext)->filename);
442 /* Write string S of length LEN to file F, escaping characters as
443 necessary for HTML. */
445 escape_string (FILE *f, char *s, int len)
450 for (bp = cp = s; bp < ep; bp = cp)
452 while (cp < ep && *cp != '&' && *cp != '<' && *cp != '>' && *cp)
455 fwrite (bp, 1, cp - bp, f);
476 /* Write table T to THIS output driver. */
478 output_tab_table (struct outp_driver *this, struct tab_table *t)
480 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_c_str (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_c_str (&t->title),
498 ls_length (&t->title));
499 fputs ("</TH>\n </TR>\n", x->file.file);
504 unsigned char *ct = t->ct;
506 for (r = 0; r < t->nr; r++)
510 fputs (" <TR>\n", x->file.file);
511 for (c = 0; c < t->nc; c++, ct++)
513 struct fixed_string *cc;
517 struct tab_joined_cell *j = NULL;
519 cc = t->cc + c + r * t->nc;
522 j = (struct tab_joined_cell *) ls_c_str (cc);
524 if (j->x1 != c || j->y1 != r)
528 if (r < t->t || r >= t->nr - t->b
529 || c < t->l || c >= t->nc - t->r)
533 cp = stpcpy (header, " <T");
536 switch (*ct & TAB_ALIGN_MASK)
539 cp = stpcpy (cp, " ALIGN=RIGHT");
544 cp = stpcpy (cp, " ALIGN=CENTER");
552 if (j->x2 - j->x1 > 1)
553 cp = spprintf (cp, " COLSPAN=%d", j->x2 - j->x1);
554 if (j->y2 - j->y1 > 1)
555 cp = spprintf (cp, " ROWSPAN=%d", j->y2 - j->y1);
561 fputs (header, x->file.file);
563 if ( ! (*ct & TAB_EMPTY) )
565 char *s = ls_c_str (cc);
566 size_t l = ls_length (cc);
568 while (l && isspace ((unsigned char) *s))
574 escape_string (x->file.file, s, l);
577 fprintf (x->file.file, "</T%c>\n", tag);
579 fputs (" </TR>\n", x->file.file);
583 fputs ("</TABLE>\n\n", x->file.file);
587 html_initialise_chart(struct outp_driver *d UNUSED, struct chart *ch)
592 make_unique_file_stream(&fp, &ch->filename);
597 ch->pl_params = pl_newplparams();
598 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
604 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
611 /* HTML driver class. */
612 struct outp_class html_class =
624 html_postopen_driver,
651 html_initialise_chart,
656 #endif /* !NO_HTML */