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