home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / xdvik / squeeze.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  5KB  |  191 lines

  1. /*
  2.  * Copyright (c) 1994 Paul Vojta.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 1. Redistributions of source code must retain the above copyright
  8.  *    notice, this list of conditions and the following disclaimer.
  9.  * 2. Redistributions in binary form must reproduce the above copyright
  10.  *    notice, this list of conditions and the following disclaimer in the
  11.  *    documentation and/or other materials provided with the distribution.
  12.  *
  13.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23.  * SUCH DAMAGE.
  24.  *
  25.  * NOTE:
  26.  * This routine is adapted from the squeeze.c that comes with dvips;
  27.  * it bears the message:
  28.  *   This software is Copyright 1988 by Radical Eye Software.
  29.  * Used with permission.
  30.  */
  31. /*
  32.  *   This routine squeezes a PostScript file down to its
  33.  *   minimum.  We parse and then output it.
  34.  *   Adapted for xdvi 1/94.  Writes a C program that contains the PS file
  35.  *   as a constant string.
  36.  */
  37. #include <stdio.h>
  38. #define LINELENGTH (72)
  39. #define BUFLENGTH (1000)
  40. #undef putchar
  41. #define putchar(a) (void)putc(a, out) ;
  42. FILE *in, *out ;
  43. static int linepos = 0 ;
  44. static int lastspecial = 1 ;
  45. static int stringlen = 0;
  46. extern int strlen() ;
  47. /*
  48.  *   This next routine writes out a `special' character.  In this case,
  49.  *   we simply put it out, since any special character terminates the
  50.  *   preceding token.
  51.  */
  52. void specialout(c)
  53. char c ;
  54. {
  55.    if (linepos + 1 > LINELENGTH) {
  56.       (void)fputs("\\n\\\n", out);
  57.       stringlen += linepos + 1;
  58.       linepos = 0 ;
  59.    }
  60.    putchar(c) ;
  61.    linepos++ ;
  62.    lastspecial = 1 ;
  63. }
  64. void strout(s)
  65. char *s ;
  66. {
  67.    if (linepos + strlen(s) > LINELENGTH) {
  68.       (void)fputs("\\n\\\n", out);
  69.       stringlen += linepos + 1;
  70.       linepos = 0 ;
  71.    }
  72.    linepos += strlen(s) ;
  73.    while (*s != 0)
  74.       putchar(*s++) ;
  75.    lastspecial = 1 ;
  76. }
  77. void cmdout(s)
  78. char *s ;
  79. {
  80.    int l ;
  81.  
  82.    l = strlen(s) ;
  83.    if (linepos + l + 1 > LINELENGTH) {
  84.       (void)fputs("\\n\\\n", out);
  85.       stringlen += linepos + 1;
  86.       linepos = 0 ;
  87.       lastspecial = 1 ;
  88.    }
  89.    if (! lastspecial) {
  90.       putchar(' ') ;
  91.       linepos++ ;
  92.    }
  93.    while (*s != 0) {
  94.       putchar(*s++) ;
  95.    }
  96.    linepos += l ;
  97.    lastspecial = 0 ;
  98. }
  99. char buf[BUFLENGTH] ;
  100. #ifndef VMS
  101. void
  102. #endif
  103. main(argc, argv)
  104. int argc ;
  105. char *argv[] ;
  106. {
  107.    int c ;
  108.    char *b ;
  109.    char seeking ;
  110.    extern void exit() ;
  111.  
  112.    if (argc > 3 || (in=(argc < 2 ? stdin : fopen(argv[1], "r")))==NULL ||
  113.                     (out=(argc < 3 ? stdout : fopen(argv[2], "w")))==NULL) {
  114.       (void)fprintf(stderr, "Usage:  squeeze [infile [outfile]]\n") ;
  115.       exit(1) ;
  116.    }
  117.    (void)fputs("/*\n\
  118.  *   DO NOT EDIT THIS FILE!\n\
  119.  *   It was created by squeeze.c from another file (see the Makefile).\n\
  120.  */\n\n\
  121. #ifndef _Xconst\n\
  122. #if __STDC__\n\
  123. #define _Xconst const\n\
  124. #else\n\
  125. #define    _Xconst\n\
  126. #endif\n\
  127. #endif\n\n\
  128. _Xconst char psheader[] = \"\\\n", out);
  129.    while (1) {
  130.       c = getc(in) ;
  131.       if (c==EOF)
  132.          break ;
  133.       if (c=='%') {
  134.          while ((c=getc(in))!='\n') ;
  135.       }
  136.       if (c <= ' ')
  137.          continue ;
  138.       switch (c) {
  139. case '{' :
  140. case '}' :
  141. case '[' :
  142. case ']' :
  143.          specialout(c) ;
  144.          break ;
  145. case '<' :
  146. case '(' :
  147.          if (c=='(')
  148.             seeking = ')' ;
  149.          else
  150.             seeking = '>' ;
  151.          b = buf ;
  152.          *b++ = c ;
  153.          do {
  154.             c = getc(in) ;
  155.             if (b > buf + BUFLENGTH-2) {
  156.                (void)fprintf(stderr, "Overran buffer seeking %c", seeking) ;
  157.                exit(1) ;
  158.             }
  159.             *b++ = c ;
  160.             if (c=='\\')
  161.                *b++ = getc(in) ;
  162.          } while (c != seeking) ;
  163.          *b++ = 0 ;
  164.          strout(buf) ;
  165.          break ;
  166. default:
  167.          b = buf ;
  168.          while ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||
  169.                 (c>='0'&&c<='9')||(c=='/')||(c=='@')||
  170.                 (c=='!')||(c=='"')||(c=='&')||(c=='*')||(c==':')||
  171.                 (c==',')||(c==';')||(c=='?')||(c=='^')||(c=='~')||
  172.                 (c=='-')||(c=='.')||(c=='#')||(c=='|')||(c=='_')||
  173.                 (c=='=')||(c=='$')||(c=='+')) {
  174.             *b++ = c ;
  175.             c = getc(in) ;
  176.          }
  177.          if (b == buf) {
  178.             (void)fprintf(stderr, "Oops!  Missed a case: %c.\n", c) ;
  179.             exit(1) ;
  180.          }
  181.          *b++ = 0 ;
  182.          (void)ungetc(c, in) ;
  183.          cmdout(buf) ;
  184.       }
  185.    }
  186.    (void)fprintf(out, "\\n\";\n\n\
  187. int\tpsheaderlen\t= %d;\n", stringlen + linepos + 1);
  188.    exit(0) ;
  189.    /*NOTREACHED*/
  190. }
  191.