ffe0c0452a9c77e372bace6f825483dd3aff4a20
[pspp-builds.git] / src / output / ascii.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include "message.h"
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include "alloc.h"
27 #include "message.h"
28 #include "chart.h"
29 #include "filename.h"
30 #include "misc.h"
31 #include "output.h"
32 #include "pool.h"
33 #include "start-date.h"
34 #include "version.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 /* ASCII driver options: (defaults listed first)
40
41    output-file="pspp.list"
42    char-set=ascii|latin1
43    form-feed-string="\f"        Written as a formfeed.
44    newline-string=default|"\r\n"|"\n"   
45                                 Written as a newline.
46    paginate=on|off              Formfeeds are desired?
47    tab-width=8                  Width of a tab; 0 to not use tabs.
48    init=""                      Written at beginning of output.
49    done=""                      Written at end of output.
50    
51    headers=on|off               Put headers at top of page?
52    length=66
53    width=130
54    lpi=6                        Only used to determine font size.
55    cpi=10                       
56    squeeze=off|on               Squeeze multiple newlines into exactly one.
57
58    left-margin=0
59    right-margin=0
60    top-margin=2
61    bottom-margin=2
62
63    box[x]="strng"               Sets box character X (X in base 4: 0-3333).
64    italic-on=overstrike|"strng" Turns on italic (underline).
65    italic-off=""|"strng"        Turns off italic; ignored for overstrike.
66    bold-on=overstrike|"strng"   Turns on bold.
67    bold-off=""|"strng"          Turns off bold; ignored for overstrike.
68    bold-italic-on=overstrike|"strng" Turns on bold-italic.
69    bold-italic-off=""|"strng"   Turns off bold-italic; ignored for overstrike.
70    overstrike-style=single|line Can we print a whole line then BS over it, or
71    must we go char by char, as on a terminal?
72    carriage-return-style=bs|cr  Must we return the carriage with a sequence of
73    BSes, or will a single CR do it?
74  */
75
76 /* Disable messages by failed range checks. */
77 /*#define SUPPRESS_WARNINGS 1 */
78
79 /* Character set. */
80 enum
81   {
82     CHS_ASCII,                  /* 7-bit ASCII */
83     CHS_LATIN1                  /* Latin 1; not really supported at the moment */
84   };
85
86 /* Overstrike style. */
87 enum
88   {
89     OVS_SINGLE,                 /* Overstrike each character: "a\b_b\b_c\b_" */
90     OVS_LINE                    /* Overstrike lines: "abc\b\b\b___" (or if
91                                    newline is "\r\n", then "abc\r___").  Easier
92                                    on the printer, doesn't work on a tty. */
93   };
94
95 /* Basic output strings. */
96 enum
97   {
98     OPS_INIT,                   /* Document initialization string. */
99     OPS_DONE,                   /* Document uninit string. */
100     OPS_FORMFEED,               /* Formfeed string. */
101     OPS_NEWLINE,                /* Newline string. */
102
103     OPS_COUNT                   /* Number of output strings. */
104   };
105
106 /* Line styles bit shifts. */
107 enum
108   {
109     LNS_TOP = 0,
110     LNS_LEFT = 2,
111     LNS_BOTTOM = 4,
112     LNS_RIGHT = 6,
113
114     LNS_COUNT = 256
115   };
116
117 /* Carriage return style. */
118 enum
119   {
120     CRS_BS,                     /* Multiple backspaces. */
121     CRS_CR                      /* Single carriage return. */
122   };
123
124 /* Assembles a byte from four taystes. */
125 #define TAYSTE2BYTE(T, L, B, R)                 \
126         (((T) << LNS_TOP)                       \
127          | ((L) << LNS_LEFT)                    \
128          | ((B) << LNS_BOTTOM)                  \
129          | ((R) << LNS_RIGHT))
130
131 /* Extract tayste with shift value S from byte B. */
132 #define BYTE2TAYSTE(B, S)                       \
133         (((B) >> (S)) & 3)
134
135 /* Font style; take one of the first group |'d with one of the second group. */
136 enum
137   {
138     FSTY_ON = 000,              /* Turn font on. */
139     FSTY_OFF = 001,             /* Turn font off. */
140
141     FSTY_ITALIC = 0,            /* Italic font. */
142     FSTY_BOLD = 2,              /* Bold font. */
143     FSTY_BOLD_ITALIC = 4,       /* Bold-italic font. */
144
145     FSTY_COUNT = 6              /* Number of font styles. */
146   };
147
148 /* A line of text. */
149 struct line 
150   {
151     unsigned short *chars;      /* Characters and attributes. */
152     int char_cnt;               /* Length. */
153     int char_cap;               /* Allocated bytes. */
154   };
155
156 /* ASCII output driver extension record. */
157 struct ascii_driver_ext
158   {
159     /* User parameters. */
160     int char_set;               /* CHS_ASCII/CHS_LATIN1; no-op right now. */
161     int headers;                /* 1=print headers at top of page. */
162     int page_length;            /* Page length in lines. */
163     int page_width;             /* Page width in characters. */
164     int lpi;                    /* Lines per inch. */
165     int cpi;                    /* Characters per inch. */
166     int left_margin;            /* Left margin in characters. */
167     int right_margin;           /* Right margin in characters. */
168     int top_margin;             /* Top margin in lines. */
169     int bottom_margin;          /* Bottom margin in lines. */
170     int paginate;               /* 1=insert formfeeds. */
171     int tab_width;              /* Width of a tab; 0 not to use tabs. */
172     struct fixed_string ops[OPS_COUNT]; /* Basic output strings. */
173     struct fixed_string box[LNS_COUNT]; /* Line & box drawing characters. */
174     struct fixed_string fonts[FSTY_COUNT]; /* Font styles; NULL=overstrike. */
175     int overstrike_style;       /* OVS_SINGLE or OVS_LINE. */
176     int carriage_return_style;  /* Carriage return style. */
177     int squeeze_blank_lines;    /* 1=squeeze multiple blank lines into one. */
178
179     /* Internal state. */
180     struct file_ext file;       /* Output file. */
181     int page_number;            /* Current page number. */
182     struct line *lines;         /* Page content. */
183     int lines_cap;              /* Number of lines allocated. */
184     int w, l;                   /* Actual width & length w/o margins, etc. */
185     int cur_font;               /* Current font by OUTP_F_*. */
186 #if GLOBAL_DEBUGGING
187     int debug;                  /* Set by som_text_draw(). */
188 #endif
189   };
190
191 static int postopen (struct file_ext *);
192 static int preclose (struct file_ext *);
193
194 static struct outp_option_info *option_info;
195
196 static int
197 ascii_open_global (struct outp_class *this UNUSED)
198 {
199   option_info = xmalloc (sizeof *option_info);
200   option_info->initial = 0;
201   option_info->options = 0;
202   return 1;
203 }
204
205
206 static char *s;
207 static int
208 ascii_close_global (struct outp_class *this UNUSED)
209 {
210   free(option_info->initial);
211   free(option_info->options);
212   free(option_info);
213   free(s);
214   return 1;
215 }
216
217 static int *
218 ascii_font_sizes (struct outp_class *this UNUSED, int *n_valid_sizes)
219 {
220   static int valid_sizes[] = {12, 12, 0, 0};
221
222   assert (n_valid_sizes);
223   *n_valid_sizes = 1;
224   return valid_sizes;
225 }
226
227 static int
228 ascii_preopen_driver (struct outp_driver *this)
229 {
230   struct ascii_driver_ext *x;
231   int i;
232   
233   assert (this->driver_open == 0);
234   msg (VM (1), _("ASCII driver initializing as `%s'..."), this->name);
235   this->ext = x = xmalloc (sizeof *x);
236   x->char_set = CHS_ASCII;
237   x->headers = 1;
238   x->page_length = 66;
239   x->page_width = 79;
240   x->lpi = 6;
241   x->cpi = 10;
242   x->left_margin = 0;
243   x->right_margin = 0;
244   x->top_margin = 2;
245   x->bottom_margin = 2;
246   x->paginate = 1;
247   x->tab_width = 8;
248   for (i = 0; i < OPS_COUNT; i++)
249     ls_null (&x->ops[i]);
250   for (i = 0; i < LNS_COUNT; i++)
251     ls_null (&x->box[i]);
252   for (i = 0; i < FSTY_COUNT; i++)
253     ls_null (&x->fonts[i]);
254   x->overstrike_style = OVS_SINGLE;
255   x->carriage_return_style = CRS_BS;
256   x->squeeze_blank_lines = 0;
257   x->file.filename = NULL;
258   x->file.mode = "wb";
259   x->file.file = NULL;
260   x->file.sequence_no = &x->page_number;
261   x->file.param = x;
262   x->file.postopen = postopen;
263   x->file.preclose = preclose;
264   x->page_number = 0;
265   x->lines = NULL;
266   x->lines_cap = 0;
267   x->cur_font = OUTP_F_R;
268 #if GLOBAL_DEBUGGING
269   x->debug = 0;
270 #endif
271   return 1;
272 }
273
274 static int
275 ascii_postopen_driver (struct outp_driver *this)
276 {
277   struct ascii_driver_ext *x = this->ext;
278   
279   assert (this->driver_open == 0);
280   
281   if (NULL == x->file.filename)
282     x->file.filename = xstrdup ("pspp.list");
283   
284   x->w = x->page_width - x->left_margin - x->right_margin;
285   x->l = (x->page_length - (x->headers ? 3 : 0) - x->top_margin
286           - x->bottom_margin - 1);
287   if (x->w < 59 || x->l < 15)
288     {
289       msg (SE, _("ascii driver: Area of page excluding margins and headers "
290                  "must be at least 59 characters wide by 15 lines long.  Page as "
291                  "configured is only %d characters by %d lines."), x->w, x->l);
292       return 0;
293     }
294   
295   this->res = x->lpi * x->cpi;
296   this->horiz = x->lpi;
297   this->vert = x->cpi;
298   this->width = x->w * this->horiz;
299   this->length = x->l * this->vert;
300   
301   if (ls_null_p (&x->ops[OPS_FORMFEED]))
302     ls_create (&x->ops[OPS_FORMFEED], "\f");
303   if (ls_null_p (&x->ops[OPS_NEWLINE])
304       || !strcmp (ls_c_str (&x->ops[OPS_NEWLINE]), "default"))
305     {
306       ls_create (&x->ops[OPS_NEWLINE], "\n");
307       x->file.mode = "wt";
308     }
309   
310   {
311     int i;
312     
313     for (i = 0; i < LNS_COUNT; i++)
314       {
315         char c[2];
316         c[1] = 0;
317         if (!ls_null_p (&x->box[i]))
318           continue;
319         switch (i)
320           {
321           case TAYSTE2BYTE (0, 0, 0, 0):
322             c[0] = ' ';
323             break;
324
325           case TAYSTE2BYTE (0, 1, 0, 0):
326           case TAYSTE2BYTE (0, 1, 0, 1):
327           case TAYSTE2BYTE (0, 0, 0, 1):
328             c[0] = '-';
329             break;
330
331           case TAYSTE2BYTE (1, 0, 0, 0):
332           case TAYSTE2BYTE (1, 0, 1, 0):
333           case TAYSTE2BYTE (0, 0, 1, 0):
334             c[0] = '|';
335             break;
336
337           case TAYSTE2BYTE (0, 3, 0, 0):
338           case TAYSTE2BYTE (0, 3, 0, 3):
339           case TAYSTE2BYTE (0, 0, 0, 3):
340           case TAYSTE2BYTE (0, 2, 0, 0):
341           case TAYSTE2BYTE (0, 2, 0, 2):
342           case TAYSTE2BYTE (0, 0, 0, 2):
343             c[0] = '=';
344             break;
345
346           case TAYSTE2BYTE (3, 0, 0, 0):
347           case TAYSTE2BYTE (3, 0, 3, 0):
348           case TAYSTE2BYTE (0, 0, 3, 0):
349           case TAYSTE2BYTE (2, 0, 0, 0):
350           case TAYSTE2BYTE (2, 0, 2, 0):
351           case TAYSTE2BYTE (0, 0, 2, 0):
352             c[0] = '#';
353             break;
354
355           default:
356             if (BYTE2TAYSTE (i, LNS_LEFT) > 1
357                 || BYTE2TAYSTE (i, LNS_TOP) > 1
358                 || BYTE2TAYSTE (i, LNS_RIGHT) > 1
359                 || BYTE2TAYSTE (i, LNS_BOTTOM) > 1)
360               c[0] = '#';
361             else
362               c[0] = '+';
363             break;
364           }
365         ls_create (&x->box[i], c);
366       }
367   }
368   
369   {
370     int i;
371     
372     this->cp_x = this->cp_y = 0;
373     this->font_height = this->vert;
374     this->prop_em_width = this->horiz;
375     this->fixed_width = this->horiz;
376
377     this->horiz_line_width[0] = 0;
378     this->vert_line_width[0] = 0;
379     
380     for (i = 1; i < OUTP_L_COUNT; i++)
381       {
382         this->horiz_line_width[i] = this->vert;
383         this->vert_line_width[i] = this->horiz;
384       }
385     
386     for (i = 0; i < (1 << OUTP_L_COUNT); i++)
387       {
388         this->horiz_line_spacing[i] = (i & ~1) ? this->vert : 0;
389         this->vert_line_spacing[i] = (i & ~1) ? this->horiz : 0;
390       }
391   }
392   
393   this->driver_open = 1;
394   msg (VM (2), _("%s: Initialization complete."), this->name);
395
396   return 1;
397 }
398
399 static int
400 ascii_close_driver (struct outp_driver *this)
401 {
402   struct ascii_driver_ext *x = this->ext;
403   int i;
404   
405   assert (this->driver_open == 1);
406   msg (VM (2), _("%s: Beginning closing..."), this->name);
407   
408   x = this->ext;
409   for (i = 0; i < OPS_COUNT; i++)
410     ls_destroy (&x->ops[i]);
411   for (i = 0; i < LNS_COUNT; i++)
412     ls_destroy (&x->box[i]);
413   for (i = 0; i < FSTY_COUNT; i++)
414     ls_destroy (&x->fonts[i]);
415   if (x->lines != NULL) 
416     {
417       int line;
418       
419       for (line = 0; line < x->lines_cap; line++) 
420         free (x->lines[line].chars);
421       free (x->lines); 
422     }
423   fn_close_ext (&x->file);
424   free (x->file.filename);
425   free (x);
426   
427   this->driver_open = 0;
428   msg (VM (3), _("%s: Finished closing."), this->name);
429   
430   return 1;
431 }
432
433 /* Generic option types. */
434 enum
435   {
436     pos_int_arg = -10,
437     nonneg_int_arg,
438     string_arg,
439     font_string_arg,
440     boolean_arg
441   };
442
443 static struct outp_option option_tab[] =
444   {
445     {"headers", boolean_arg, 0},
446     {"output-file", 1, 0},
447     {"char-set", 2, 0},
448     {"length", pos_int_arg, 0},
449     {"width", pos_int_arg, 1},
450     {"lpi", pos_int_arg, 2},
451     {"cpi", pos_int_arg, 3},
452     {"init", string_arg, 0},
453     {"done", string_arg, 1},
454     {"left-margin", nonneg_int_arg, 0},
455     {"right-margin", nonneg_int_arg, 1},
456     {"top-margin", nonneg_int_arg, 2},
457     {"bottom-margin", nonneg_int_arg, 3},
458     {"paginate", boolean_arg, 1},
459     {"form-feed-string", string_arg, 2},
460     {"newline-string", string_arg, 3},
461     {"italic-on", font_string_arg, 0},
462     {"italic-off", font_string_arg, 1},
463     {"bold-on", font_string_arg, 2},
464     {"bold-off", font_string_arg, 3},
465     {"bold-italic-on", font_string_arg, 4},
466     {"bold-italic-off", font_string_arg, 5},
467     {"overstrike-style", 3, 0},
468     {"tab-width", nonneg_int_arg, 4},
469     {"carriage-return-style", 4, 0},
470     {"squeeze", boolean_arg, 2},
471     {"", 0, 0},
472   };
473
474 static void
475 ascii_option (struct outp_driver *this, const char *key,
476               const struct string *val)
477 {
478   struct ascii_driver_ext *x = this->ext;
479   int cat, subcat;
480   const char *value;
481
482   value = ds_c_str (val);
483   if (!strncmp (key, "box[", 4))
484     {
485       char *tail;
486       int indx = strtol (&key[4], &tail, 4);
487       if (*tail != ']' || indx < 0 || indx > LNS_COUNT)
488         {
489           msg (SE, _("Bad index value for `box' key: syntax is box[INDEX], "
490                "0 <= INDEX < %d decimal, with INDEX expressed in base 4."),
491                LNS_COUNT);
492           return;
493         }
494       if (!ls_null_p (&x->box[indx]))
495         msg (SW, _("Duplicate value for key `%s'."), key);
496       ls_create (&x->box[indx], value);
497       return;
498     }
499
500   cat = outp_match_keyword (key, option_tab, option_info, &subcat);
501   switch (cat)
502     {
503     case 0:
504       msg (SE, _("Unknown configuration parameter `%s' for ascii device driver."),
505            key);
506       break;
507     case 1:
508       free (x->file.filename);
509       x->file.filename = xstrdup (value);
510       break;
511     case 2:
512       if (!strcmp (value, "ascii"))
513         x->char_set = CHS_ASCII;
514       else if (!strcmp (value, "latin1"))
515         x->char_set = CHS_LATIN1;
516       else
517         msg (SE, _("Unknown character set `%s'.  Valid character sets are "
518              "`ascii' and `latin1'."), value);
519       break;
520     case 3:
521       if (!strcmp (value, "single"))
522         x->overstrike_style = OVS_SINGLE;
523       else if (!strcmp (value, "line"))
524         x->overstrike_style = OVS_LINE;
525       else
526         msg (SE, _("Unknown overstrike style `%s'.  Valid overstrike styles "
527              "are `single' and `line'."), value);
528       break;
529     case 4:
530       if (!strcmp (value, "bs"))
531         x->carriage_return_style = CRS_BS;
532       else if (!strcmp (value, "cr"))
533         x->carriage_return_style = CRS_CR;
534       else
535         msg (SE, _("Unknown carriage return style `%s'.  Valid carriage "
536              "return styles are `cr' and `bs'."), value);
537       break;
538     case pos_int_arg:
539       {
540         char *tail;
541         int arg;
542
543         errno = 0;
544         arg = strtol (value, &tail, 0);
545         if (arg < 1 || errno == ERANGE || *tail)
546           {
547             msg (SE, _("Positive integer required as value for `%s'."), key);
548             break;
549           }
550         switch (subcat)
551           {
552           case 0:
553             x->page_length = arg;
554             break;
555           case 1:
556             x->page_width = arg;
557             break;
558           case 2:
559             x->lpi = arg;
560             break;
561           case 3:
562             x->cpi = arg;
563             break;
564           default:
565             assert (0);
566           }
567       }
568       break;
569     case nonneg_int_arg:
570       {
571         char *tail;
572         int arg;
573
574         errno = 0;
575         arg = strtol (value, &tail, 0);
576         if (arg < 0 || errno == ERANGE || *tail)
577           {
578             msg (SE, _("Zero or positive integer required as value for `%s'."),
579                  key);
580             break;
581           }
582         switch (subcat)
583           {
584           case 0:
585             x->left_margin = arg;
586             break;
587           case 1:
588             x->right_margin = arg;
589             break;
590           case 2:
591             x->top_margin = arg;
592             break;
593           case 3:
594             x->bottom_margin = arg;
595             break;
596           case 4:
597             x->tab_width = arg;
598             break;
599           default:
600             assert (0);
601           }
602       }
603       break;
604     case string_arg:
605       {
606         struct fixed_string *s;
607         switch (subcat)
608           {
609           case 0:
610             s = &x->ops[OPS_INIT];
611             break;
612           case 1:
613             s = &x->ops[OPS_DONE];
614             break;
615           case 2:
616             s = &x->ops[OPS_FORMFEED];
617             break;
618           case 3:
619             s = &x->ops[OPS_NEWLINE];
620             break;
621           default:
622             assert (0);
623             abort ();
624           }
625         ls_create (s, value);
626       }
627       break;
628     case font_string_arg:
629       {
630         if (!strcmp (value, "overstrike"))
631           {
632             ls_destroy (&x->fonts[subcat]);
633             return;
634           }
635         ls_create (&x->fonts[subcat], value);
636       }
637       break;
638     case boolean_arg:
639       {
640         int setting;
641         if (!strcmp (value, "on") || !strcmp (value, "true")
642             || !strcmp (value, "yes") || atoi (value))
643           setting = 1;
644         else if (!strcmp (value, "off") || !strcmp (value, "false")
645                  || !strcmp (value, "no") || !strcmp (value, "0"))
646           setting = 0;
647         else
648           {
649             msg (SE, _("Boolean value expected for %s."), key);
650             return;
651           }
652         switch (subcat)
653           {
654           case 0:
655             x->headers = setting;
656             break;
657           case 1:
658             x->paginate = setting;
659             break;
660           case 2:
661             x->squeeze_blank_lines = setting;
662             break;
663           default:
664             assert (0);
665           }
666       }
667       break;
668     default:
669       assert (0);
670     }
671 }
672
673 int
674 postopen (struct file_ext *f)
675 {
676   struct ascii_driver_ext *x = f->param;
677   struct fixed_string *s = &x->ops[OPS_INIT];
678
679   if (!ls_empty_p (s) && fwrite (ls_c_str (s), ls_length (s), 1, f->file) < 1)
680     {
681       msg (ME, _("ASCII output driver: %s: %s"),
682            f->filename, strerror (errno));
683       return 0;
684     }
685   return 1;
686 }
687
688 int
689 preclose (struct file_ext *f)
690 {
691   struct ascii_driver_ext *x = f->param;
692   struct fixed_string *d = &x->ops[OPS_DONE];
693
694   if (!ls_empty_p (d) && fwrite (ls_c_str (d), ls_length (d), 1, f->file) < 1)
695     {
696       msg (ME, _("ASCII output driver: %s: %s"),
697            f->filename, strerror (errno));
698       return 0;
699     }
700   return 1;
701 }
702
703 static int
704 ascii_open_page (struct outp_driver *this)
705 {
706   struct ascii_driver_ext *x = this->ext;
707   int i;
708
709   assert (this->driver_open && !this->page_open);
710   x->page_number++;
711   if (!fn_open_ext (&x->file))
712     {
713       msg (ME, _("ASCII output driver: %s: %s"), x->file.filename,
714            strerror (errno));
715       return 0;
716     }
717
718   if (x->l > x->lines_cap)
719     {
720       x->lines = xnrealloc (x->lines, x->l, sizeof *x->lines);
721       for (i = x->lines_cap; i < x->l; i++) 
722         {
723           struct line *line = &x->lines[i];
724           line->chars = NULL;
725           line->char_cap = 0;
726         }
727       x->lines_cap = x->l;
728     }
729
730   for (i = 0; i < x->l; i++)
731     x->lines[i].char_cnt = 0;
732
733   this->page_open = 1;
734   return 1;
735 }
736
737 /* Ensures that at least the first L characters of line I in the
738    driver identified by struct ascii_driver_ext *X have been cleared out. */
739 static inline void
740 expand_line (struct ascii_driver_ext *x, int i, int l)
741 {
742   struct line *line;
743   int j;
744
745   assert (i < x->lines_cap);
746   line = &x->lines[i];
747   if (l > line->char_cap) 
748     {
749       line->char_cap = l * 2;
750       line->chars = xnrealloc (line->chars,
751                                line->char_cap, sizeof *line->chars); 
752     }
753   for (j = line->char_cnt; j < l; j++)
754     line->chars[j] = ' ';
755   line->char_cnt = l;
756 }
757
758 /* Puts line L at (H,K) in the current output page.  Assumes
759    struct ascii_driver_ext named `ext'. */
760 #define draw_line(H, K, L)                              \
761         ext->lines[K].chars[H] = (L) | 0x800
762
763 /* Line styles for each position. */
764 #define T(STYLE) (STYLE<<LNS_TOP)
765 #define L(STYLE) (STYLE<<LNS_LEFT)
766 #define B(STYLE) (STYLE<<LNS_BOTTOM)
767 #define R(STYLE) (STYLE<<LNS_RIGHT)
768
769 static void
770 ascii_line_horz (struct outp_driver *this, const struct rect *r,
771                  const struct color *c UNUSED, int style)
772 {
773   struct ascii_driver_ext *ext = this->ext;
774   int x1 = r->x1 / this->horiz;
775   int x2 = r->x2 / this->horiz;
776   int y1 = r->y1 / this->vert;
777   int x;
778
779   assert (this->driver_open && this->page_open);
780   if (x1 == x2)
781     return;
782 #if GLOBAL_DEBUGGING
783   if (x1 > x2
784       || x1 < 0 || x1 >= ext->w
785       || x2 <= 0 || x2 > ext->w
786       || y1 < 0 || y1 >= ext->l)
787     {
788 #if !SUPPRESS_WARNINGS
789       printf (_("ascii_line_horz: bad hline (%d,%d),%d out of (%d,%d)\n"),
790               x1, x2, y1, ext->w, ext->l);
791 #endif
792       return;
793     }
794 #endif
795
796   if (ext->lines[y1].char_cnt < x2)
797     expand_line (ext, y1, x2);
798
799   for (x = x1; x < x2; x++)
800     draw_line (x, y1, (style << LNS_LEFT) | (style << LNS_RIGHT));
801 }
802
803 static void
804 ascii_line_vert (struct outp_driver *this, const struct rect *r,
805                  const struct color *c UNUSED, int style)
806 {
807   struct ascii_driver_ext *ext = this->ext;
808   int x1 = r->x1 / this->horiz;
809   int y1 = r->y1 / this->vert;
810   int y2 = r->y2 / this->vert;
811   int y;
812
813   assert (this->driver_open && this->page_open);
814   if (y1 == y2)
815     return;
816 #if GLOBAL_DEBUGGING
817   if (y1 > y2
818       || x1 < 0 || x1 >= ext->w
819       || y1 < 0 || y1 >= ext->l
820       || y2 < 0 || y2 > ext->l)
821     {
822 #if !SUPPRESS_WARNINGS
823       printf (_("ascii_line_vert: bad vline %d,(%d,%d) out of (%d,%d)\n"),
824               x1, y1, y2, ext->w, ext->l);
825 #endif
826       return;
827     }
828 #endif
829
830   for (y = y1; y < y2; y++)
831     if (ext->lines[y].char_cnt <= x1)
832       expand_line (ext, y, x1 + 1);
833
834   for (y = y1; y < y2; y++)
835     draw_line (x1, y, (style << LNS_TOP) | (style << LNS_BOTTOM));
836 }
837
838 static void
839 ascii_line_intersection (struct outp_driver *this, const struct rect *r,
840                          const struct color *c UNUSED,
841                          const struct outp_styles *style)
842 {
843   struct ascii_driver_ext *ext = this->ext;
844   int x = r->x1 / this->horiz;
845   int y = r->y1 / this->vert;
846   int l;
847
848   assert (this->driver_open && this->page_open);
849 #if GLOBAL_DEBUGGING
850   if (x < 0 || x >= ext->w || y < 0 || y >= ext->l)
851     {
852 #if !SUPPRESS_WARNINGS
853       printf (_("ascii_line_intersection: bad intsct (%d,%d) out of (%d,%d)\n"),
854               x, y, ext->w, ext->l);
855 #endif
856       return;
857     }
858 #endif
859
860   l = ((style->l << LNS_LEFT) | (style->r << LNS_RIGHT)
861        | (style->t << LNS_TOP) | (style->b << LNS_BOTTOM));
862
863   if (ext->lines[y].char_cnt <= x)
864     expand_line (ext, y, x + 1);
865   draw_line (x, y, l);
866 }
867
868 /* FIXME: Later we could set this up so that for certain devices it
869    performs shading? */
870 static void
871 ascii_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
872            const struct color *bord UNUSED, const struct color *fill UNUSED)
873 {
874   assert (this->driver_open && this->page_open);
875 }
876
877 /* Polylines not supported. */
878 static void
879 ascii_polyline_begin (struct outp_driver *this UNUSED, const struct color *c UNUSED)
880 {
881   assert (this->driver_open && this->page_open);
882 }
883 static void
884 ascii_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
885 {
886   assert (this->driver_open && this->page_open);
887 }
888 static void
889 ascii_polyline_end (struct outp_driver *this UNUSED)
890 {
891   assert (this->driver_open && this->page_open);
892 }
893
894 static void
895 ascii_text_set_font_by_name (struct outp_driver * this, const char *s)
896 {
897   struct ascii_driver_ext *x = this->ext;
898   int len = strlen (s);
899
900   assert (this->driver_open && this->page_open);
901   x->cur_font = OUTP_F_R;
902   if (len == 0)
903     return;
904   if (s[len - 1] == 'I')
905     {
906       if (len > 1 && s[len - 2] == 'B')
907         x->cur_font = OUTP_F_BI;
908       else
909         x->cur_font = OUTP_F_I;
910     }
911   else if (s[len - 1] == 'B')
912     x->cur_font = OUTP_F_B;
913 }
914
915 static void
916 ascii_text_set_font_by_position (struct outp_driver *this, int pos)
917 {
918   struct ascii_driver_ext *x = this->ext;
919   assert (this->driver_open && this->page_open);
920   x->cur_font = pos >= 0 && pos < 4 ? pos : 0;
921 }
922
923 static void
924 ascii_text_set_font_by_family (struct outp_driver *this UNUSED, const char *s UNUSED)
925 {
926   assert (this->driver_open && this->page_open);
927 }
928
929 static const char *
930 ascii_text_get_font_name (struct outp_driver *this)
931 {
932   struct ascii_driver_ext *x = this->ext;
933
934   assert (this->driver_open && this->page_open);
935   switch (x->cur_font)
936     {
937     case OUTP_F_R:
938       return "R";
939     case OUTP_F_I:
940       return "I";
941     case OUTP_F_B:
942       return "B";
943     case OUTP_F_BI:
944       return "BI";
945     default:
946       assert (0);
947     }
948   abort ();
949 }
950
951 static const char *
952 ascii_text_get_font_family (struct outp_driver *this UNUSED)
953 {
954   assert (this->driver_open && this->page_open);
955   return "";
956 }
957
958 static int
959 ascii_text_set_size (struct outp_driver *this, int size)
960 {
961   assert (this->driver_open && this->page_open);
962   return size == this->vert;
963 }
964
965 static int
966 ascii_text_get_size (struct outp_driver *this, int *em_width)
967 {
968   assert (this->driver_open && this->page_open);
969   if (em_width)
970     *em_width = this->horiz;
971   return this->vert;
972 }
973
974 static void text_draw (struct outp_driver *this, struct outp_text *t);
975
976 /* Divides the text T->S into lines of width T->H.  Sets T->V to the
977    number of lines necessary.  Actually draws the text if DRAW is
978    nonzero.
979
980    You probably don't want to look at this code. */
981 static void
982 delineate (struct outp_driver *this, struct outp_text *t, int draw)
983 {
984   /* Width we're fitting everything into. */
985   int width = t->h / this->horiz;
986
987   /* Maximum `y' position we can write to. */
988   int max_y;
989
990   /* Current position in string, character following end of string. */
991   const char *s = ls_c_str (&t->s);
992   const char *end = ls_end (&t->s);
993
994   /* Temporary struct outp_text to pass to low-level function. */
995   struct outp_text temp;
996
997 #if GLOBAL_DEBUGGING && 0
998   if (!ext->debug)
999     {
1000       ext->debug = 1;
1001       printf (_("%s: horiz=%d, vert=%d\n"), this->name, this->horiz, this->vert);
1002     }
1003 #endif
1004
1005   if (!width)
1006     {
1007       t->h = t->v = 0;
1008       return;
1009     }
1010
1011   if (draw)
1012     {
1013       temp.options = t->options;
1014       ls_shallow_copy (&temp.s, &t->s);
1015       temp.h = t->h / this->horiz;
1016       temp.x = t->x / this->horiz;
1017     }
1018   else
1019     t->y = 0;
1020   temp.y = t->y / this->vert;
1021
1022   if (t->options & OUTP_T_VERT)
1023     max_y = (t->v / this->vert) + temp.y - 1;
1024   else
1025     max_y = INT_MAX;
1026   
1027   while (end - s > width)
1028     {
1029       const char *beg = s;
1030       const char *space;
1031
1032       /* Find first space before &s[width]. */
1033       space = &s[width];
1034       for (;;)
1035         {
1036           if (space > s)
1037             {
1038               if (!isspace ((unsigned char) space[-1]))
1039                 {
1040                   space--;
1041                   continue;
1042                 }
1043               else
1044                 s = space;
1045             }
1046           else
1047             s = space = &s[width];
1048           break;
1049         }
1050
1051       /* Draw text. */
1052       if (draw)
1053         {
1054           ls_init (&temp.s, beg, space - beg);
1055           temp.w = space - beg;
1056           text_draw (this, &temp);
1057         }
1058       if (++temp.y > max_y)
1059         return;
1060
1061       /* Find first nonspace after space. */
1062       while (s < end && isspace ((unsigned char) *s))
1063         s++;
1064     }
1065   if (s < end)
1066     {
1067       if (draw)
1068         {
1069           ls_init (&temp.s, s, end - s);
1070           temp.w = end - s;
1071           text_draw (this, &temp);
1072         }
1073       temp.y++;
1074     }
1075
1076   t->v = (temp.y * this->vert) - t->y;
1077 }
1078
1079 static void
1080 ascii_text_metrics (struct outp_driver *this, struct outp_text *t)
1081 {
1082   assert (this->driver_open && this->page_open);
1083   if (!(t->options & OUTP_T_HORZ))
1084     {
1085       t->v = this->vert;
1086       t->h = ls_length (&t->s) * this->horiz;
1087     }
1088   else
1089     delineate (this, t, 0);
1090 }
1091
1092 static void
1093 ascii_text_draw (struct outp_driver *this, struct outp_text *t)
1094 {
1095   /* FIXME: orientations not supported. */
1096   assert (this->driver_open && this->page_open);
1097   if (!(t->options & OUTP_T_HORZ))
1098     {
1099       struct outp_text temp;
1100
1101       temp.options = t->options;
1102       temp.s = t->s;
1103       temp.h = temp.v = 0;
1104       temp.x = t->x / this->horiz;
1105       temp.y = t->y / this->vert;
1106       text_draw (this, &temp);
1107       ascii_text_metrics (this, t);
1108       
1109       return;
1110     }
1111   delineate (this, t, 1);
1112 }
1113
1114 static void
1115 text_draw (struct outp_driver *this, struct outp_text *t)
1116 {
1117   struct ascii_driver_ext *ext = this->ext;
1118   unsigned attr = ext->cur_font << 8;
1119
1120   int x = t->x;
1121   int y = t->y;
1122
1123   char *s = ls_c_str (&t->s);
1124
1125   /* Expand the line with the assumption that S takes up LEN character
1126      spaces (sometimes it takes up less). */
1127   int min_len;
1128
1129   assert (this->driver_open && this->page_open);
1130   switch (t->options & OUTP_T_JUST_MASK)
1131     {
1132     case OUTP_T_JUST_LEFT:
1133       break;
1134     case OUTP_T_JUST_CENTER:
1135       x -= (t->h - t->w) / 2;   /* fall through */
1136     case OUTP_T_JUST_RIGHT:
1137       x += (t->h - t->w);
1138       break;
1139     default:
1140       assert (0);
1141     }
1142
1143   if (!(t->y < ext->l && x < ext->w))
1144     return;
1145   min_len = min (x + ls_length (&t->s), ext->w);
1146   if (ext->lines[t->y].char_cnt < min_len)
1147     expand_line (ext, t->y, min_len);
1148
1149   {
1150     int len = ls_length (&t->s);
1151
1152     if (len + x > ext->w)
1153       len = ext->w - x;
1154     while (len--)
1155       ext->lines[y].chars[x++] = *s++ | attr;
1156   }
1157 }
1158 \f
1159 /* ascii_close_page () and support routines. */
1160
1161 #define LINE_BUF_SIZE 1024
1162 static char *line_buf;
1163 static char *line_p;
1164
1165 static inline int
1166 commit_line_buf (struct outp_driver *this)
1167 {
1168   struct ascii_driver_ext *x = this->ext;
1169   
1170   if ((int) fwrite (line_buf, 1, line_p - line_buf, x->file.file)
1171       < line_p - line_buf)
1172     {
1173       msg (ME, _("Writing `%s': %s"), x->file.filename, strerror (errno));
1174       return 0;
1175     }
1176
1177   line_p = line_buf;
1178   return 1;
1179 }
1180
1181 /* Writes everything from BP to EP exclusive into line_buf, or to
1182    THIS->output if line_buf overflows. */
1183 static inline void
1184 output_string (struct outp_driver *this, const char *bp, const char *ep)
1185 {
1186   if (LINE_BUF_SIZE - (line_p - line_buf) >= ep - bp)
1187     {
1188       memcpy (line_p, bp, ep - bp);
1189       line_p += ep - bp;
1190     }
1191   else
1192     while (bp < ep)
1193       {
1194         if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1195           return;
1196         *line_p++ = *bp++;
1197       }
1198 }
1199
1200 /* Writes everything from BP to EP exclusive into line_buf, or to
1201    THIS->output if line_buf overflows.  Returns 1 if additional passes
1202    over the line are required.  FIXME: probably could do a lot of
1203    optimization here. */
1204 static inline int
1205 output_shorts (struct outp_driver *this,
1206                const unsigned short *bp, const unsigned short *ep)
1207 {
1208   struct ascii_driver_ext *ext = this->ext;
1209   size_t remaining = LINE_BUF_SIZE - (line_p - line_buf);
1210   int result = 0;
1211
1212   for (; bp < ep; bp++)
1213     {
1214       if (*bp & 0x800)
1215         {
1216           struct fixed_string *box = &ext->box[*bp & 0xff];
1217           size_t len = ls_length (box);
1218
1219           if (remaining >= len)
1220             {
1221               memcpy (line_p, ls_c_str (box), len);
1222               line_p += len;
1223               remaining -= len;
1224             }
1225           else
1226             {
1227               if (!commit_line_buf (this))
1228                 return 0;
1229               output_string (this, ls_c_str (box), ls_end (box));
1230               remaining = LINE_BUF_SIZE - (line_p - line_buf);
1231             }
1232         }
1233       else if (*bp & 0x0300)
1234         {
1235           struct fixed_string *on;
1236           char buf[5];
1237           int len;
1238
1239           switch (*bp & 0x0300)
1240             {
1241             case OUTP_F_I << 8:
1242               on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1243               break;
1244             case OUTP_F_B << 8:
1245               on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1246               break;
1247             case OUTP_F_BI << 8:
1248               on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1249               break;
1250             default:
1251               assert (0);
1252               abort ();
1253             }
1254           if (!on)
1255             {
1256               if (ext->overstrike_style == OVS_SINGLE)
1257                 switch (*bp & 0x0300)
1258                   {
1259                   case OUTP_F_I << 8:
1260                     buf[0] = '_';
1261                     buf[1] = '\b';
1262                     buf[2] = *bp;
1263                     len = 3;
1264                     break;
1265                   case OUTP_F_B << 8:
1266                     buf[0] = *bp;
1267                     buf[1] = '\b';
1268                     buf[2] = *bp;
1269                     len = 3;
1270                     break;
1271                   case OUTP_F_BI << 8:
1272                     buf[0] = '_';
1273                     buf[1] = '\b';
1274                     buf[2] = *bp;
1275                     buf[3] = '\b';
1276                     buf[4] = *bp;
1277                     len = 5;
1278                     break;
1279                   default:
1280                     assert (0);
1281                     abort ();
1282                   }
1283               else
1284                 {
1285                   buf[0] = *bp;
1286                   result = len = 1;
1287                 }
1288             }
1289           else
1290             {
1291               buf[0] = *bp;
1292               len = 1;
1293             }
1294           output_string (this, buf, &buf[len]);
1295         }
1296       else if (remaining)
1297         {
1298           *line_p++ = *bp;
1299           remaining--;
1300         }
1301       else
1302         {
1303           if (!commit_line_buf (this))
1304             return 0;
1305           remaining = LINE_BUF_SIZE - (line_p - line_buf);
1306           *line_p++ = *bp;
1307         }
1308     }
1309
1310   return result;
1311 }
1312
1313 /* Writes CH into line_buf N times, or to THIS->output if line_buf
1314    overflows. */
1315 static inline void
1316 output_char (struct outp_driver *this, int n, char ch)
1317 {
1318   if (LINE_BUF_SIZE - (line_p - line_buf) >= n)
1319     {
1320       memset (line_p, ch, n);
1321       line_p += n;
1322     }
1323   else
1324     while (n--)
1325       {
1326         if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1327           return;
1328         *line_p++ = ch;
1329       }
1330 }
1331
1332 /* Advance the carriage from column 0 to the left margin. */
1333 static void
1334 advance_to_left_margin (struct outp_driver *this)
1335 {
1336   struct ascii_driver_ext *ext = this->ext;
1337   int margin;
1338
1339   margin = ext->left_margin;
1340   if (margin == 0)
1341     return;
1342   if (ext->tab_width && margin >= ext->tab_width)
1343     {
1344       output_char (this, margin / ext->tab_width, '\t');
1345       margin %= ext->tab_width;
1346     }
1347   if (margin)
1348     output_char (this, margin, ' ');
1349 }
1350
1351 /* Move the output file carriage N_CHARS left, to the left margin. */
1352 static void
1353 return_carriage (struct outp_driver *this, int n_chars)
1354 {
1355   struct ascii_driver_ext *ext = this->ext;
1356
1357   switch (ext->carriage_return_style)
1358     {
1359     case CRS_BS:
1360       output_char (this, n_chars, '\b');
1361       break;
1362     case CRS_CR:
1363       output_char (this, 1, '\r');
1364       advance_to_left_margin (this);
1365       break;
1366     default:
1367       assert (0);
1368       abort ();
1369     }
1370 }
1371
1372 /* Writes COUNT lines from the line buffer in THIS, starting at line
1373    number FIRST. */
1374 static void
1375 output_lines (struct outp_driver *this, int first, int count)
1376 {
1377   struct ascii_driver_ext *ext = this->ext;
1378   int line_num;
1379
1380   struct fixed_string *newline = &ext->ops[OPS_NEWLINE];
1381
1382   int n_chars;
1383   int n_passes;
1384
1385   if (NULL == ext->file.file)
1386     return;
1387
1388   /* Iterate over all the lines to be output. */
1389   for (line_num = first; line_num < first + count; line_num++)
1390     {
1391       struct line *line = &ext->lines[line_num];
1392       unsigned short *p = line->chars;
1393       unsigned short *end_p = p + line->char_cnt;
1394       unsigned short *bp, *ep;
1395       unsigned short attr = 0;
1396
1397       assert (end_p >= p);
1398
1399       /* Squeeze multiple blank lines into a single blank line if
1400          requested. */
1401       if (ext->squeeze_blank_lines
1402           && line_num > first
1403           && ext->lines[line_num].char_cnt == 0
1404           && ext->lines[line_num - 1].char_cnt == 0)
1405         continue;
1406
1407       /* Output every character in the line in the appropriate
1408          manner. */
1409       n_passes = 1;
1410       bp = ep = p;
1411       n_chars = 0;
1412       advance_to_left_margin (this);
1413       for (;;)                  
1414         {
1415           while (ep < end_p && attr == (*ep & 0x0300))
1416             ep++;
1417           if (output_shorts (this, bp, ep))
1418             n_passes = 2;
1419           n_chars += ep - bp;
1420           bp = ep;
1421
1422           if (bp >= end_p)
1423             break;
1424
1425           /* Turn off old font. */
1426           if (attr != (OUTP_F_R << 8))
1427             {
1428               struct fixed_string *off;
1429
1430               switch (attr)
1431                 {
1432                 case OUTP_F_I << 8:
1433                   off = &ext->fonts[FSTY_OFF | FSTY_ITALIC];
1434                   break;
1435                 case OUTP_F_B << 8:
1436                   off = &ext->fonts[FSTY_OFF | FSTY_BOLD];
1437                   break;
1438                 case OUTP_F_BI << 8:
1439                   off = &ext->fonts[FSTY_OFF | FSTY_BOLD_ITALIC];
1440                   break;
1441                 default:
1442                   assert (0);
1443                   abort ();
1444                 }
1445               if (off)
1446                 output_string (this, ls_c_str (off), ls_end (off));
1447             }
1448
1449           /* Turn on new font. */
1450           attr = (*bp & 0x0300);
1451           if (attr != (OUTP_F_R << 8))
1452             {
1453               struct fixed_string *on;
1454
1455               switch (attr)
1456                 {
1457                 case OUTP_F_I << 8:
1458                   on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1459                   break;
1460                 case OUTP_F_B << 8:
1461                   on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1462                   break;
1463                 case OUTP_F_BI << 8:
1464                   on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1465                   break;
1466                 default:
1467                   assert (0);
1468                   abort ();
1469                 }
1470               if (on)
1471                 output_string (this, ls_c_str (on), ls_end (on));
1472             }
1473
1474           ep = bp + 1;
1475         }
1476       if (n_passes > 1)
1477         {
1478           char ch;
1479
1480           return_carriage (this, n_chars);
1481           n_chars = 0;
1482           bp = ep = p;
1483           for (;;)
1484             {
1485               while (ep < end_p && (*ep & 0x0300) == (OUTP_F_R << 8))
1486                 ep++;
1487               if (ep >= end_p)
1488                 break;
1489               output_char (this, ep - bp, ' ');
1490
1491               switch (*ep & 0x0300)
1492                 {
1493                 case OUTP_F_I << 8:
1494                   ch = '_';
1495                   break;
1496                 case OUTP_F_B << 8:
1497                   ch = *ep;
1498                   break;
1499                 case OUTP_F_BI << 8:
1500                   ch = *ep;
1501                   n_passes = 3;
1502                   break;
1503                 default:
1504                   assert (0);
1505                   abort ();
1506                 }
1507               output_char (this, 1, ch);
1508               n_chars += ep - bp + 1;
1509               bp = ep + 1;
1510               ep = bp;
1511             }
1512         }
1513       if (n_passes > 2)
1514         {
1515           return_carriage (this, n_chars);
1516           bp = ep = p;
1517           for (;;)
1518             {
1519               while (ep < end_p && (*ep & 0x0300) != (OUTP_F_BI << 8))
1520                 ep++;
1521               if (ep >= end_p)
1522                 break;
1523               output_char (this, ep - bp, ' ');
1524               output_char (this, 1, '_');
1525               bp = ep + 1;
1526               ep = bp;
1527             }
1528         }
1529
1530       output_string (this, ls_c_str (newline), ls_end (newline));
1531     }
1532 }
1533
1534
1535 static int
1536 ascii_close_page (struct outp_driver *this)
1537 {
1538   static int s_len;
1539
1540   struct ascii_driver_ext *x = this->ext;
1541   int nl_len, ff_len, total_len;
1542   char *cp;
1543   int i;
1544
1545   assert (this->driver_open && this->page_open);
1546   
1547   if (!line_buf)
1548     line_buf = xmalloc (LINE_BUF_SIZE);
1549   line_p = line_buf;
1550
1551   nl_len = ls_length (&x->ops[OPS_NEWLINE]);
1552   if (x->top_margin)
1553     {
1554       total_len = x->top_margin * nl_len;
1555       if (s_len < total_len)
1556         {
1557           s_len = total_len;
1558           s = xrealloc (s, s_len);
1559         }
1560       for (cp = s, i = 0; i < x->top_margin; i++)
1561         {
1562           memcpy (cp, ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1563           cp += nl_len;
1564         }
1565       output_string (this, s, &s[total_len]);
1566     }
1567   if (x->headers)
1568     {
1569       int len;
1570
1571       total_len = nl_len + x->w;
1572       if (s_len < total_len + 1)
1573         {
1574           s_len = total_len + 1;
1575           s = xrealloc (s, s_len);
1576         }
1577       
1578       memset (s, ' ', x->w);
1579
1580       {
1581         char temp[40];
1582
1583         snprintf (temp, 80, _("%s - Page %d"), get_start_date (),
1584                   x->page_number);
1585         memcpy (&s[x->w - strlen (temp)], temp, strlen (temp));
1586       }
1587
1588       if (outp_title && outp_subtitle)
1589         {
1590           len = min ((int) strlen (outp_title), x->w);
1591           memcpy (s, outp_title, len);
1592         }
1593       memcpy (&s[x->w], ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1594       output_string (this, s, &s[total_len]);
1595
1596       memset (s, ' ', x->w);
1597       len = strlen (version) + 3 + strlen (host_system);
1598       if (len < x->w)
1599         sprintf (&s[x->w - len], "%s - %s" , version, host_system);
1600       if (outp_subtitle || outp_title)
1601         {
1602           char *string = outp_subtitle ? outp_subtitle : outp_title;
1603           len = min ((int) strlen (string), x->w);
1604           memcpy (s, string, len);
1605         }
1606       memcpy (&s[x->w], ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1607       output_string (this, s, &s[total_len]);
1608       output_string (this, &s[x->w], &s[total_len]);
1609     }
1610   if (line_p != line_buf && !commit_line_buf (this))
1611     return 0;
1612
1613   output_lines (this, 0, x->l);
1614
1615   ff_len = ls_length (&x->ops[OPS_FORMFEED]);
1616   total_len = x->bottom_margin * nl_len + ff_len;
1617   if (s_len < total_len)
1618     s = xrealloc (s, total_len);
1619   for (cp = s, i = 0; i < x->bottom_margin; i++)
1620     {
1621       memcpy (cp, ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1622       cp += nl_len;
1623     }
1624   memcpy (cp, ls_c_str (&x->ops[OPS_FORMFEED]), ff_len);
1625   if ( x->paginate ) 
1626           output_string (this, s, &s[total_len]);
1627
1628   if (line_p != line_buf && !commit_line_buf (this))
1629     return 0;
1630
1631   this->page_open = 0;
1632   return 1;
1633 }
1634
1635
1636
1637 static void
1638 ascii_chart_initialise(struct outp_driver *d UNUSED, struct chart *ch )
1639 {
1640   msg(MW, _("Charts are unsupported with ascii drivers."));
1641   ch->lp = 0;
1642 }
1643
1644 static void 
1645 ascii_chart_finalise(struct outp_driver *d UNUSED, struct chart *ch UNUSED)
1646 {
1647   
1648 }
1649
1650 struct outp_class ascii_class =
1651 {
1652   "ascii",
1653   0,
1654   0,
1655
1656   ascii_open_global,
1657   ascii_close_global,
1658   ascii_font_sizes,
1659
1660   ascii_preopen_driver,
1661   ascii_option,
1662   ascii_postopen_driver,
1663   ascii_close_driver,
1664
1665   ascii_open_page,
1666   ascii_close_page,
1667
1668   NULL,
1669
1670   ascii_line_horz,
1671   ascii_line_vert,
1672   ascii_line_intersection,
1673
1674   ascii_box,
1675   ascii_polyline_begin,
1676   ascii_polyline_point,
1677   ascii_polyline_end,
1678
1679   ascii_text_set_font_by_name,
1680   ascii_text_set_font_by_position,
1681   ascii_text_set_font_by_family,
1682   ascii_text_get_font_name,
1683   ascii_text_get_font_family,
1684   ascii_text_set_size,
1685   ascii_text_get_size,
1686   ascii_text_metrics,
1687   ascii_text_draw,
1688
1689   ascii_chart_initialise,
1690   ascii_chart_finalise
1691 };