summaryrefslogtreecommitdiff
path: root/src/preproc/html2/pre-html.cc
blob: 8824f3ac801e7f08681734c10601235f7e7ae42a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// -*- C++ -*-
/* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
     Written by Gaius Mulley (gaius@glam.ac.uk).

This file is part of groff.

groff is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

groff is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with groff; see the file COPYING.  If not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

#define PREHTMLC

#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include "lib.h"
#include "errarg.h"
#include "error.h"
#include "stringclass.h"
#include "posix.h"

#include <errno.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifndef errno
extern int errno;
#endif

#ifdef _POSIX_VERSION
#include <sys/wait.h>
#define PID_T pid_t
#else /* not _POSIX_VERSION */
#define PID_T int
#endif /* not _POSIX_VERSION */

extern char *strerror();

#include "pre-html.h"
#include "pushbackbuffer.h"

#define POSTSCRIPTRES          72000   // maybe there is a better way to find this? --fixme--
#define DEFAULT_IMAGE_RES         80
#define IMAGE_BOARDER_PIXELS      10

// #define TRANSPARENT  "-background \"#FFF\" -transparent \"#FFF\""
#define TRANSPARENT  ""

#define DEBUGGING

#if !defined(TRUE)
#   define TRUE (1==1)
#endif
#if !defined(FALSE)
#   define FALSE (1==0)
#endif

void stop() {}


static int   stdoutfd      =1;                  // output file descriptor - normally 1 but might move
                                                // -1 means closed
static char *psFileName    =0;                  // name of postscript file
static char *regionFileName=0;                  // name of file containing all image regions
static char *image_device  = "pnmraw";
static int   image_res     = DEFAULT_IMAGE_RES;


/*
 *  Images are generated via postscript, gs and the pnm utilities.
 */

#define IMAGEDEVICE    "-Tps"


static int do_file(const char *filename);

/*
 *  sys_fatal - writes a fatal error message. Taken from src/roff/groff/pipeline.c
 */

void sys_fatal (const char *s)
{
  fprintf(stderr, "%s: %s: %s", program_name, s, strerror(errno));
}

/*
 *  the class and methods for retaining ascii text
 */

struct char_block {
  enum { SIZE = 256 };
  char          buffer[SIZE];
  int           used;
  char_block   *next;

  char_block();
};

char_block::char_block()
: used(0), next(0)
{
}

class char_buffer {
public:
  char_buffer();
  ~char_buffer();
  int  read_file(FILE *fp);
  int  do_html(int argc, char *argv[]);
  int  do_image(int argc, char *argv[]);
  void write_file_html(void);
  void write_file_troff(void);
  void write_upto_newline (char_block **t, int *i);
  int  can_see(char_block **t, int *i, char *string);
private:
  char_block *head;
  char_block *tail;
};

char_buffer::char_buffer()
: head(0), tail(0)
{
}

char_buffer::~char_buffer()
{
  while (head != 0) {
    char_block *temp = head;
    head = head->next;
    delete temp;
  }
}

int char_buffer::read_file (FILE *fp)
{
  int i=0;
  unsigned int old_used;
  int n;

  while (! feof(fp)) {
    if (tail == 0) {
      tail = new char_block;
      head = tail;
    } else {
      if (tail->used == char_block::SIZE) {
	tail->next = new char_block;
	tail       = tail->next;
      }
    }
    // at this point we have a tail which is ready for the next SIZE bytes of the file

    n = fread(tail->buffer, sizeof(char), char_block::SIZE-tail->used, fp);
    if (n <= 0) {
      // error
      return( 0 );
    } else {
      tail->used += n*sizeof(char);
    }
  }
  return( 1 );
}

/*
 *  writeNbytes - writes n bytes to stdout.
 */

static void writeNbytes (char *s, int l)
{
  int n=0;
  int r;

  while (n<l) {
    r = write(stdoutfd, s, l-n);
    if (r<0) {
      sys_fatal("write");
    }
    n += r;
    s += r;
  }
}

/*
 *  writeString - writes a string to stdout.
 */

static void writeString (char *s)
{
  writeNbytes(s, strlen(s));
}

/*
 *  write_upto_newline - writes the contents of the buffer until a newline is seen.
 */

void char_buffer::write_upto_newline (char_block **t, int *i)
{
  int j=*i;

  if (*t) {
    while ((j < (*t)->used) && ((*t)->buffer[j] != '\n')) {
      j++;
    }
    if ((j < (*t)->used) && ((*t)->buffer[j] == '\n')) {
      j++;
    }
    writeNbytes((*t)->buffer+(*i), j-(*i));
    if (j == (*t)->used) {
      *i = 0;
      *t = (*t)->next;
      write_upto_newline(t, i);
    } else {
      // newline was seen
      *i = j;
    }
  }
}

/*
 *  can_see - returns TRUE if we can see string in t->buffer[i] onwards
 */

int char_buffer::can_see(char_block **t, int *i, char *string)
{
  int j         = 0;
  int l         = strlen(string);
  int k         = *i;
  char_block *s = *t;

  while (s) {
    while ((k<s->used) && (j<l) && (s->buffer[k] == string[j])) {
      j++;
      k++;
    }
    if (j == l) {
      *i = k;
      *t = s;
      return( TRUE );
    } else if ((k<s->used) && (s->buffer[k] != string[j])) {
      return( FALSE );
    }
    s = s->next;
    k = 0;
  }
  return( FALSE );
}

/*
 *  write_file_troff - writes the buffer to stdout (troff).
 *                     It prepends the number register set to 0.
 */

void char_buffer::write_file_troff (void)
{
  char_block *t=head;
  int r;

  writeString(".nr html2enable 1\n");
  writeString(".nr htmlflip 0\n");
  if (t != 0) {
    do {
      writeNbytes(t->buffer, t->used);
      t = t->next;
    } while ((t != head) && (t != 0));
  }
  if (close(stdoutfd) < 0)
    sys_fatal("close");

  // now we grab fd=1 so that the next pipe cannot use fd=1
  if (stdoutfd == 1) {
    if (dup(2) != stdoutfd) {
      sys_fatal("dup failed to use fd=1");
    }
  }
}

/*
 *  the image class remembers the position of all images in the postscript file
 *  and assigns names for each image.
 */

struct imageItem {
  imageItem  *next;
  int         X1;
  int         Y1;
  int         X2;
  int         Y2;
  char       *imageName;
  int         resolution;
  int         pageNo;

  imageItem (int x1, int y1, int x2, int y2, int page, int res, char *name);
  ~imageItem ();
};

/*
 *  imageItem - constructor
 */

imageItem::imageItem (int x1, int y1, int x2, int y2, int page, int res, char *name)
{
  X1         = x1;
  Y1         = y1;
  X2         = x2;
  Y2         = y2;
  pageNo     = page;
  resolution = res;
  imageName  = name;
  next       = 0;
}

/*
 *  imageItem - deconstructor
 */

imageItem::~imageItem ()
{
}

/*
 *  imageList - class containing a list of imageItems.
 */

class imageList {
private:
  imageItem *head;
  imageItem *tail;
  int        count;
public:
  imageList();
  ~imageList();
  void  add(int x1, int y1, int x2, int y2, int page, int res);
  char *get(int i);
};

/*
 *  imageList - constructor.
 */

imageList::imageList ()
  : head(0), tail(0), count(0)
{
}

/*
 *  imageList - deconstructor.
 */

imageList::~imageList ()
{
  while (head != 0) {
    imageItem *i = head;
    head = head->next;
    delete i;
  }
}

/*
 *  createImage - generates a png file from the information held in, i, and
 *                the postscript file.
 */

static void createImage (imageItem *i)
{
  if (i->X1 != -1) {
    char buffer[4096];

    sprintf(buffer,
	    "echo showpage | gs -q -dFirstPage=%d -dLastPage=%d -dSAFER -sDEVICE=%s -r%d -sOutputFile=- %s - 2> /dev/null | pnmcut %d %d %d %d | pnmtopng %s > %s.png \n",
	    i->pageNo, i->pageNo,
	    image_device,
	    image_res,
	    psFileName,
	    i->X1*image_res/POSTSCRIPTRES,
	    i->Y1*image_res/POSTSCRIPTRES-IMAGE_BOARDER_PIXELS,
	    (i->X2-i->X1)*image_res/POSTSCRIPTRES+IMAGE_BOARDER_PIXELS,
	    (i->Y2-i->Y1)*image_res/POSTSCRIPTRES+IMAGE_BOARDER_PIXELS,
	    TRANSPARENT,
	    i->imageName);
    fprintf(stderr, buffer);
    system(buffer);
  }
}

/*
 *  add - an image description to the imageList.
 */

void imageList::add (int x1, int y1, int x2, int y2, int page, int res)
{
  char *name = (char *)malloc(50);

  if (name == 0)
    sys_fatal("malloc");

  if (x1 == -1) {
    name[0] = (char)0;
  } else {
    count++;
    sprintf(name, "grohtml-%d", count);
  }
  imageItem *i = new imageItem(x1, y1, x2, y2, page, res, name);

  if (head == 0) {
    head = i;
    tail = i;
  } else {
    tail->next = i;
    tail = i;
  }
  createImage(i);
}

/*
 *  get - returns the name for image number, i.
 */

char *imageList::get(int i)
{
  imageItem *t=head;

  while (i>0) {
    if (i == 1) {
      if (t->X1 == -1) {
	return( NULL );
      } else {
	return( t->imageName );
      }
    }
    t = t->next;
    i--;
  }
}

static imageList listOfImages;  // list of images defined by the region file.

/*
 *  write_file_html - writes the buffer to stdout (troff).
 *                    It prepends the number register set to 1 and writes
 *                    out the file replacing template image names with
 *                    actual image names.
 */

void char_buffer::write_file_html (void)
{
  char_block *t      =head;
  int         imageNo=0;
  char       *name;
  int         i=0;

  writeString(".nr html2enable 1\n");
  writeString(".nr htmlflip 1\n");
  if (t != 0) {
    stop();
    do {
      if (can_see(&t, &i, ".if '\\*(.T'html2' .IMAGE <pre-html-image>\n")) {
	imageNo++;
	name = listOfImages.get(imageNo);
	if (name != 0) {
	  writeString(".if '\\*(.T'html2' .IMAGE \"");
	  writeString(name);
	  writeString(".png\"\n");
	}
      } else {
	write_upto_newline(&t, &i);
      }
    } while (t != 0);
  }
  if (close(stdoutfd) < 0)
    sys_fatal("close");

  // now we grab fd=1 so that the next pipe cannot use fd=1
  if (stdoutfd == 1) {
    if (dup(2) != stdoutfd) {
      sys_fatal("dup failed to use fd=1");
    }
  }
}

/*
 *  generateImages - parses the region file and generates images
 *                   from the postscript file. The region file
 *                   contains the x1,y1  x2,y2 extents of each
 *                   image.
 */

static void generateImages (char *regionFileName)
{
  pushBackBuffer *f=new pushBackBuffer(regionFileName);
  char ch;

  if (f->putPB('\n') == '\n') {
  }
  while (f->putPB(f->getPB()) != eof) {
    if (f->isString("\ngrohtml-info:page")) {
      int page= f->readInt();
      int x1  = f->readInt();
      int y1  = f->readInt();
      int x2  = f->readInt();
      int y2  = f->readInt();
      int res = POSTSCRIPTRES;  // --fixme--    prefer (f->readInt()) providing that troff can discover the value
      listOfImages.add(x1, y1, x2, y2, page, res);
    }
    ch = f->getPB();
  }
}

/*
 *  replaceFd - replace a file descriptor, was, with, willbe.
 */

static void replaceFd (int was, int willbe)
{
  int dupres;

  if (was != willbe) {
    if (close(was)<0) {
      sys_fatal("close");
    }
    dupres = dup(willbe);
    if (dupres != was) {
      sys_fatal("dup");
      fprintf(stderr, "trying to replace fd=%d with %d dup used %d\n", was, willbe, dupres);
      if (willbe == 1) {
	fprintf(stderr, "likely that stdout should be opened before %d\n", was);
      }
      exit(1);
    }
    if (close(willbe) < 0) {
      sys_fatal("close");
    }
  }
}

/*
 *  waitForChild - waits for child, pid, to exit.
 */

static void waitForChild (PID_T pid)
{
  PID_T waitpd;
  int   status;

  waitpd = wait(&status);
  if (waitpd != pid)
    sys_fatal("wait");
}

/*
 *  alterDeviceTo - if toImage is set then the arg list is altered to include
 *                     IMAGEDEVICE and we invoke groff rather than troff.
 *                  else 
 *                     set -Thtml2 and troff
 */

static void alterDeviceTo (int argc, char *argv[], int toImage)
{
  int i=0;

  if (toImage) {
    while (i < argc) {
      if (strcmp(argv[i], "-Thtml2") == 0) {
	argv[i] = IMAGEDEVICE;
      }
      i++;
    }
    argv[1] = "groff";  /* rather than troff */
  } else {
    while (i < argc) {
      if (strcmp(argv[i], IMAGEDEVICE) == 0) {
	argv[i] = "-Thtml2";
      }
      i++;
    }
    argv[1] = "troff";  /* use troff */
  }
}

/*
 *  do_html - sets the troff number htmlflip and
 *            writes out the buffer to troff -Thtml
 */

int char_buffer::do_html(int argc, char *argv[])
{
  int pdes[2];
  PID_T pid;

  if (pipe(pdes) < 0)
    sys_fatal("pipe");

  alterDeviceTo(argc, argv, 0);
  argv++;   // skip pre-grohtml argv[0]
  pid = fork();
  if (pid < 0)
    sys_fatal("fork");

  if (pid == 0) {
    // child
    replaceFd(0, pdes[0]);
    // close end we are not using
    if (close(pdes[1])<0)
      sys_fatal("close");

    execvp(argv[0], argv);
    error("couldn't exec %1: %2", argv[0], strerror(errno), (char *)0);
    fflush(stderr);		/* just in case error() doesn't */
    exit(1);
  } else {
    // parent

    replaceFd(1, pdes[1]);
    // close end we are not using
    if (close(pdes[0])<0)
      sys_fatal("close");

    write_file_html();
    waitForChild(pid);
  }
  return( 0 );
}

/*
 *  do_image - sets the troff number htmlflip and
 *             writes out the buffer to troff -Tps
 */

int char_buffer::do_image(int argc, char *argv[])
{
  PID_T pid;
  int pdes[2];

  if (pipe(pdes) < 0)
    sys_fatal("pipe");

  alterDeviceTo(argc, argv, 1);
  argv++;   // skip pre-grohtml argv[0]

  pid = fork();
  if (pid == 0) {
    // child

#if defined(DEBUGGING)
    int psFd     = creat(psFileName,     S_IWUSR|S_IRUSR);
    int regionFd = creat(regionFileName, S_IWUSR|S_IRUSR);
#else
    int psFd     = mkstemp(psFileName);
    int regionFd = mkstemp(regionFileName);
#endif

    replaceFd(1, psFd);
    replaceFd(0, pdes[0]);
    replaceFd(2, regionFd);

    // close end we are not using
    if (close(pdes[1])<0)
      sys_fatal("close");

    execvp(argv[0], argv);
    error("couldn't exec %1: %2", argv[0], strerror(errno), (char *)0);
    fflush(stderr);		/* just in case error() doesn't */
    exit(1);
  } else {
    // parent

    replaceFd(1, pdes[1]);
    write_file_troff();
    waitForChild(pid);
  }
  return( 0 );
}

static char_buffer inputFile;


/*
 *  usage - emit usage arguments and exit.
 */

void usage()
{
  fprintf(stderr, "usage: %s troffname [ troff flags ] [ files ]\n", program_name);
  exit(1);
}

/*
 *  makeTempFiles - name the temporary files
 */

static void makeTempFiles (void)
{
#if defined(DEBUGGING)
  psFileName     = "/tmp/prehtml-ps";
  regionFileName = "/tmp/prehtml-region";
#else
  psFileName     = xtmptemplate("-ps-");
  regionFileName = xtmptemplate("-regions-");
#endif
}

int main(int argc, char **argv)
{
  program_name = argv[0];
  int i; // skip over troff name
  int found=0;
  int ok=1;

  for (i = 2; i < argc; i++) {
    if (argv[i][0] == '-') {
      if (argv[i][1] == 'v') {
	extern const char *Version_string;
	fprintf(stderr, "GNU pre-grohtml version %s\n", Version_string);
	fflush(stderr);
      }
    } else {
      ok = do_file(argv[i]);
      if (! ok) {
	return( 0 );
      }
      found = 1;
    }
  }

  if (! found) {
    do_file("-");
  }
  makeTempFiles();
  ok = inputFile.do_image(argc, argv);
  if (ok == 0) {
    generateImages(regionFileName);
    ok = inputFile.do_html(argc, argv);
  }
  return ok;
}

static int do_file(const char *filename)
{
  FILE *fp;

  current_filename = filename;
  if (strcmp(filename, "-") == 0) {
    fp = stdin;
  } else {
    fp = fopen(filename, "r");
    if (fp == 0) {
      error("can't open `%1': %2", filename, strerror(errno));
      return 0;
    }
  }

  if (inputFile.read_file(fp)) {
  }

  if (fp != stdin)
    fclose(fp);
  current_filename = 0;
  return 1;
}