Annotation of researchv10no/cmd/postscript/postgif/Opostgif.c, revision 1.1

1.1     ! root        1: 
        !             2: #include <stdio.h>
        !             3: #include <string.h>
        !             4: #include <signal.h>
        !             5: #include <ctype.h>
        !             6: #include <fcntl.h>
        !             7: 
        !             8: #include "comments.h"
        !             9: #include "gen.h"
        !            10: #include "path.h"
        !            11: #include "ext.h"
        !            12: 
        !            13: #define dbprt  if (debug) fprintf
        !            14: 
        !            15: char   *optnames = "a:c:fglm:n:o:p:x:y:C:DG:IL:P:";
        !            16: char    *prologue = POSTGIF;           /* default PostScript prologue */
        !            17: char    *formfile = FORMFILE;           /* stuff for multiple pages per sheet */
        !            18: int     formsperpage = 1;               /* page images on each piece of paper */
        !            19: int    copies = 1;                     /* and this many copies of each sheet */
        !            20: int     page = 0;                       /* last page we worked on */
        !            21: int     printed = 0;                    /* and the number of pages printed */
        !            22: 
        !            23: extern char *malloc();
        !            24: extern void free();
        !            25: extern double atof(), pow();
        !            26: 
        !            27: unsigned char ibuf[BUFSIZ];
        !            28: unsigned char *cmap, *gcmap, *lcmap;
        !            29: unsigned char *gmap, *ggmap, *lgmap;
        !            30: unsigned char *pmap;
        !            31: double gamma;
        !            32: float cr = 0.3, cg = 0.59, cb = 0.11;
        !            33: int maplength, gmaplength, lmaplength;
        !            34: int scrwidth, scrheight;
        !            35: int gcolormap, lcolormap;
        !            36: int bitperpixel, background;
        !            37: int imageleft, imagetop;
        !            38: int imagewidth, imageheight;
        !            39: int interlaced, lbitperpixel;
        !            40: int gray = 0;
        !            41: int gammaflag = 0;
        !            42: int negative = 0;
        !            43: int terminate = 0;
        !            44: int codesize, clearcode, endcode, curstblsize, pmindex, byteinibuf, bitsleft;
        !            45: int prefix[4096], suffix[4096], cstbl[4096];
        !            46: int bburx = -32767, bbury = -32767;
        !            47: FILE *fp_in = NULL;
        !            48: FILE *fp_out = stdout;
        !            49: 
        !            50: char *
        !            51: allocate(size)
        !            52:     int size;
        !            53: {
        !            54:     char *p;
        !            55: 
        !            56:     if ((p = malloc(size)) == NULL) error(FATAL, "not enough memory");
        !            57:     return(p);
        !            58: }
        !            59: 
        !            60: void
        !            61: puthex(c, fp)
        !            62:     unsigned char c;
        !            63:     FILE *fp;
        !            64: {
        !            65:     static char hextbl[16] = {
        !            66:        '0', '1', '2', '3', '4', '5', '6', '7',
        !            67:        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
        !            68:     };
        !            69: 
        !            70:     putc(hextbl[(c >> 4) & 017], fp);
        !            71:     putc(hextbl[c & 017], fp);
        !            72: }
        !            73: 
        !            74: void
        !            75: setcolormap(bp)
        !            76:     int bp;
        !            77: {
        !            78:     int i, entries = 1, scale = 1;
        !            79:     unsigned char *p, *q;
        !            80: 
        !            81:     for (i = 0; i < bp; i++) entries *= 2;
        !            82:     for (i = 0; i < 8 - bp; i++) scale *= 2;
        !            83:     gcmap = (unsigned char *) allocate(entries*3);
        !            84:     ggmap = (unsigned char *) allocate(entries);
        !            85:     gmaplength = entries;
        !            86:     for (i = 0, p = gcmap, q = ggmap; i < 256; i += scale, p += 3, q++) {
        !            87:        if (negative) {
        !            88:            *p = 255 - i; p[1] = *p; p[2] = *p;
        !            89:            *q = *p;
        !            90:        }
        !            91:        else {
        !            92:            *p = i; p[1] = i; p[2] = i;
        !            93:            *q = i;
        !            94:        }
        !            95:     }
        !            96:     if (gammaflag)
        !            97:        for (i = 0, p = gcmap; i < 256; i += scale, p += 3) {
        !            98:            *p = (unsigned char) (pow((double) *p/256.0, gamma)*256);
        !            99:            p[1] = *p; p[2] = *p;
        !           100:        }
        !           101: dbprt(stderr,"default color map:\n");
        !           102: for (i = 0; i < entries*3; i += 3)
        !           103: dbprt(stderr, "%d, %d, %d\n", gcmap[i], gcmap[i+1], gcmap[i+2]);
        !           104: }
        !           105: 
        !           106: void
        !           107: readgcolormap(bp)
        !           108:     int bp;
        !           109: {
        !           110:     int i, entries = 1;
        !           111:     unsigned char *p, *q;
        !           112: 
        !           113:     for (i = 0; i < bp; i++) entries *= 2;
        !           114:     gcmap = (unsigned char *) allocate(entries*3);
        !           115:     ggmap = (unsigned char *) allocate(entries);
        !           116:     gmaplength = entries;
        !           117:     fread(gcmap, sizeof(*gcmap), entries*3, fp_in);
        !           118:     if (negative)
        !           119:        for (i = 0, p = gcmap; i < entries*3; i++, p++) *p = 255 - *p;
        !           120:     for (i = 0, p = gcmap, q = ggmap; i < entries; i++, p += 3, q++)
        !           121:        *q = cr*p[0] + cg*p[1] + cb*p[2] + 0.5;
        !           122:     if (gammaflag)
        !           123:        for (i = 0, p = gcmap; i < entries*3; i++, p++)
        !           124:            *p = (unsigned char) (pow((double) *p/256.0, gamma)*256);
        !           125: dbprt(stderr,"global color map:\n");
        !           126: for (i = 0; i < entries*3; i += 3)
        !           127: dbprt(stderr, "%d, %d, %d\n", gcmap[i], gcmap[i+1], gcmap[i+2]);
        !           128: }
        !           129: 
        !           130: void
        !           131: readlcolormap(bp)
        !           132:     int bp;
        !           133: {
        !           134:     int i, entries = 1;
        !           135:     unsigned char *p, *q;
        !           136: 
        !           137:     for (i = 0; i < bp; i++) entries *= 2;
        !           138:     lcmap = (unsigned char *) allocate(entries*3);
        !           139:     lgmap = (unsigned char *) allocate(entries);
        !           140:     lmaplength = entries;
        !           141:     fread(lcmap, sizeof(*lcmap), entries*3, fp_in);
        !           142:     if (negative)
        !           143:        for (i = 0, p = lcmap; i < entries*3; i++, p++) *p = 255 - *p;
        !           144:     for (i = 0, p = lcmap, q = lgmap; i < entries; i++, p += 3, q++)
        !           145:        *q = cr*p[0] + cg*p[1] + cb*p[2] + 0.5;
        !           146:     if (gammaflag)
        !           147:        for (i = 0, p = lcmap; i < entries*3; i++, p++)
        !           148:            *p = (unsigned char) (pow((double) *p/256.0, gamma)*256);
        !           149: dbprt(stderr,"local color map:\n");
        !           150: for (i = 0; i < entries*3; i += 3)
        !           151: dbprt(stderr, "%d, %d, %d\n", lcmap[i], lcmap[i+1], lcmap[i+2]);
        !           152: }
        !           153: 
        !           154: void
        !           155: initstbl()
        !           156: {
        !           157:     int i, entries = 1, *p, *s;
        !           158: 
        !           159:     for (i = 0; i < codesize; i++) entries *= 2;
        !           160:     clearcode = entries;
        !           161:     endcode = clearcode + 1;
        !           162:     for (i = 0, p = prefix, s = suffix; i <= endcode; i++, p++, s++) {
        !           163:        *p = endcode;
        !           164:        *s = i;
        !           165:     }
        !           166:     curstblsize = endcode + 1;
        !           167:     pmindex = 0;
        !           168:     byteinibuf = 0;
        !           169:     bitsleft = 0;
        !           170: }
        !           171: 
        !           172: int
        !           173: nextbyte()
        !           174: {
        !           175:     static ibufindex;
        !           176: 
        !           177:     if (byteinibuf) {
        !           178:        byteinibuf--;
        !           179:        ibufindex++;
        !           180:     }
        !           181:     else {
        !           182:        fread(ibuf, sizeof(*ibuf), 1, fp_in);
        !           183:        byteinibuf = ibuf[0];
        !           184: dbprt(stderr, "byte count: %d\n", byteinibuf);
        !           185:        if (byteinibuf) fread(ibuf, sizeof(*ibuf), byteinibuf, fp_in);
        !           186:        else error(FATAL, "encounter zero byte count block before end code");
        !           187:        ibufindex = 0;
        !           188:        byteinibuf--;
        !           189:        ibufindex++;
        !           190:     }
        !           191:     return(ibuf[ibufindex-1]);
        !           192: }
        !           193: 
        !           194: int masktbl[25] = {
        !           195:     0, 01, 03, 07, 017, 037, 077, 0177, 0377, 0777, 01777, 03777, 07777,
        !           196:     017777, 037777, 077777, 0177777, 0377777, 0777777, 01777777, 03777777,
        !           197:     07777777, 017777777, 037777777, 077777777
        !           198: };
        !           199: 
        !           200: int
        !           201: getcode()
        !           202: {
        !           203:     int cs, c;
        !           204:     static int oldc;
        !           205: 
        !           206:     if (curstblsize < 4096) cs = cstbl[curstblsize];
        !           207:     else cs = 12;
        !           208:     while (bitsleft < cs) {
        !           209:        oldc = (oldc & masktbl[bitsleft]) | ((nextbyte() & 0377) << bitsleft);
        !           210:        bitsleft += 8;
        !           211:     }
        !           212:     c = oldc & masktbl[cs];
        !           213:     oldc = oldc >> cs;
        !           214:     bitsleft -= cs;
        !           215: /* dbprt(stderr, "code: %d %d %d\n", curstblsize, cs, c); */
        !           216:     return(c);
        !           217: }
        !           218: 
        !           219: void
        !           220: putcode(c)
        !           221:     int c;
        !           222: {
        !           223:     if (prefix[c] != endcode) {
        !           224:        putcode(prefix[c]);
        !           225:        pmap[pmindex] = suffix[c];
        !           226:        pmindex++;
        !           227:     }
        !           228:     else {
        !           229:        pmap[pmindex] = suffix[c];
        !           230:        pmindex++;
        !           231:     }
        !           232: }
        !           233: 
        !           234: int
        !           235: firstof(c)
        !           236:     int c;
        !           237: {
        !           238:     while (prefix[c] != endcode) c = prefix[c];
        !           239:     return(suffix[c]);
        !           240: }
        !           241: 
        !           242: void
        !           243: writeimage()
        !           244: {
        !           245:     int i, j, k;
        !           246: 
        !           247: dbprt(stderr, "pmindex: %d\n", pmindex);
        !           248:     fputs("save\n", fp_out);
        !           249:     fprintf(fp_out, "/codestr %d string def\n", imagewidth);
        !           250:     if (!gray) {
        !           251:        fprintf(fp_out, "/colortbl currentfile %d string readhexstring\n",
        !           252:            maplength*3);
        !           253:         for (i = 0; i < maplength; i++) puthex(cmap[i], fp_out);
        !           254:         fputs("\n", fp_out);
        !           255:         for (i = maplength ; i < maplength*2; i++) puthex(cmap[i], fp_out);
        !           256:         fputs("\n", fp_out);
        !           257:         for (i = maplength*2 ; i < maplength*3; i++) puthex(cmap[i], fp_out);
        !           258:         fputs("\npop def\n", fp_out);
        !           259:        fprintf(fp_out, "/graytbl currentfile %d string readhexstring\n",
        !           260:            maplength);
        !           261:         for (i = 0; i < maplength; i++) puthex(gmap[i], fp_out);
        !           262:         fputs("\npop def\n", fp_out);
        !           263:     }
        !           264:     fprintf(fp_out, "%s %d %d %d %d gifimage\n",
        !           265:        gray ? "true" : "false", imagewidth, imageheight,
        !           266:        scrwidth - imageleft - imagewidth, scrheight - imagetop - imageheight);
        !           267:     if (gray) {
        !           268:        if (interlaced) {
        !           269:            int *iltbl;
        !           270: 
        !           271:            iltbl = (int *) allocate(imageheight*sizeof(int));
        !           272:            j = 0;
        !           273:            for (i = 0; i < imageheight; i += 8) {
        !           274:                iltbl[i] = j;
        !           275:                j += imagewidth;
        !           276:            }
        !           277: dbprt(stderr, "pass1: %d\n", j);
        !           278:            for (i = 4; i < imageheight; i += 8) {
        !           279:                iltbl[i] = j;
        !           280:                j += imagewidth;
        !           281:            }
        !           282: dbprt(stderr, "pass2: %d\n", j);
        !           283:            for (i = 2; i < imageheight; i += 4) {
        !           284:                iltbl[i] = j;
        !           285:                j += imagewidth;
        !           286:            }
        !           287: dbprt(stderr, "pass3: %d\n", j);
        !           288:            for (i = 1; i < imageheight; i += 2) {
        !           289:                iltbl[i] = j;
        !           290:                j += imagewidth;
        !           291:            }
        !           292: dbprt(stderr, "pass4: %d\n", j);
        !           293: 
        !           294:            for (i = 0; i < imageheight; i++) {
        !           295:                k = iltbl[i];
        !           296:                for (j = 0; j < imagewidth; j++, k++)
        !           297:                    puthex(gmap[pmap[k]], fp_out);
        !           298:                fputs("\n", fp_out);
        !           299:            }
        !           300:        }
        !           301:        else {
        !           302:            for (i = 0, k = 0; i < imageheight; i++) {
        !           303:                for (j = 0; j < imagewidth; j++, k++)
        !           304:                    puthex(gmap[pmap[k]], fp_out);
        !           305:                fputs("\n", fp_out);
        !           306:            }
        !           307:        }
        !           308:     }
        !           309:     else {
        !           310:        if (interlaced) {
        !           311:            int *iltbl;
        !           312: 
        !           313:            iltbl = (int *) allocate(imageheight*sizeof(int));
        !           314:            j = 0;
        !           315:            for (i = 0; i < imageheight; i += 8) {
        !           316:                iltbl[i] = j;
        !           317:                j += imagewidth;
        !           318:            }
        !           319: dbprt(stderr, "pass1: %d\n", j);
        !           320:            for (i = 4; i < imageheight; i += 8) {
        !           321:                iltbl[i] = j;
        !           322:                j += imagewidth;
        !           323:            }
        !           324: dbprt(stderr, "pass2: %d\n", j);
        !           325:            for (i = 2; i < imageheight; i += 4) {
        !           326:                iltbl[i] = j;
        !           327:                j += imagewidth;
        !           328:            }
        !           329: dbprt(stderr, "pass3: %d\n", j);
        !           330:            for (i = 1; i < imageheight; i += 2) {
        !           331:                iltbl[i] = j;
        !           332:                j += imagewidth;
        !           333:            }
        !           334: dbprt(stderr, "pass4: %d\n", j);
        !           335: 
        !           336:            for (i = 0; i < imageheight; i++) {
        !           337:                k = iltbl[i];
        !           338:                for (j = 0; j < imagewidth; j++, k++) puthex(pmap[k], fp_out);
        !           339:                fputs("\n", fp_out);
        !           340:            }
        !           341:        }
        !           342:        else {
        !           343:            for (i = 0, k = 0; i < imageheight; i++) {
        !           344:                for (j = 0; j < imagewidth; j++, k++) puthex(pmap[k], fp_out);
        !           345:                fputs("\n", fp_out);
        !           346:            }
        !           347:        }
        !           348:     }
        !           349:     fputs("restore\n", fp_out);
        !           350: }
        !           351: 
        !           352: void
        !           353: readimage()
        !           354: {
        !           355:     int bytecount, zerobytecount = 0;
        !           356:     int code, oldcode;
        !           357: 
        !           358:     fread(ibuf, sizeof(*ibuf), 9, fp_in);
        !           359:     imageleft = ibuf[0] + 256*ibuf[1];
        !           360:     imagetop = ibuf[2] + 256*ibuf[3];
        !           361:     imagewidth = ibuf[4] + 256*ibuf[5];
        !           362:     imageheight = ibuf[6] + 256*ibuf[7];
        !           363:     lcolormap = ibuf[8] & 0200;
        !           364:     interlaced = ibuf[8] & 0100;
        !           365:     lbitperpixel = (ibuf[8] & 07) + 1;
        !           366: dbprt(stderr, "imageleft: %d\n", imageleft);
        !           367: dbprt(stderr, "imagetop: %d\n", imagetop);
        !           368: dbprt(stderr, "imagewidth: %d\n", imagewidth);
        !           369: dbprt(stderr, "imgaeheight: %d\n", imageheight);
        !           370: dbprt(stderr, "lcolormap: %d\n", lcolormap ? 1 : 0);
        !           371: dbprt(stderr, "interlaced: %d\n", interlaced ? 1 : 0);
        !           372: dbprt(stderr, "lbitperpixel: %d\n", lbitperpixel);
        !           373:     if (lcolormap) {
        !           374:        readlcolormap(lbitperpixel);
        !           375:        cmap = lcmap;
        !           376:        gmap = lgmap;
        !           377:        maplength = lmaplength;
        !           378:     }
        !           379: 
        !           380: dbprt(stderr, "start reading raster data\n");
        !           381:     fread(ibuf, sizeof(*ibuf), 1, fp_in);
        !           382:     codesize = ibuf[0];
        !           383: dbprt(stderr, "codesize: %d\n", codesize);
        !           384:     pmap = (unsigned char *) allocate(imagewidth*imageheight);
        !           385:     initstbl();
        !           386:     while ((code = getcode()) != endcode) {
        !           387:        if (code == clearcode) {
        !           388:            curstblsize = endcode + 1;
        !           389:            code = getcode();
        !           390:            putcode(code);
        !           391:            oldcode = code;
        !           392:        }
        !           393:        else if (code < curstblsize) {
        !           394:            putcode(code);
        !           395:            prefix[curstblsize] = oldcode;
        !           396:            suffix[curstblsize] = firstof(code);
        !           397:            curstblsize++;
        !           398:            oldcode = code;
        !           399:        }
        !           400:        else {
        !           401:           if (code != curstblsize) error(FATAL, "code out of order");
        !           402:           prefix[curstblsize] = oldcode;
        !           403:           suffix[curstblsize] = firstof(oldcode);
        !           404:           curstblsize++;
        !           405:           putcode(curstblsize-1);
        !           406:           oldcode = code;
        !           407:        }
        !           408:     }
        !           409: dbprt(stderr, "finish reading raster data\n");
        !           410: 
        !           411:     /* read the rest of the raster data */
        !           412:     do {
        !           413:        fread(ibuf, sizeof(*ibuf), 1, fp_in);
        !           414:        bytecount = ibuf[0];
        !           415: dbprt(stderr, "byte count: %d\n", bytecount);
        !           416:        if (bytecount) fread(ibuf, sizeof(*ibuf), bytecount, fp_in);
        !           417:        else zerobytecount = 1;
        !           418:     } while (!zerobytecount);
        !           419: 
        !           420:     writeimage();
        !           421: 
        !           422:     if (lcolormap) {
        !           423:        cmap = gcmap;
        !           424:        gmap = ggmap;
        !           425:        maplength = gmaplength;
        !           426:        free(lcmap);
        !           427:        free(lgmap);
        !           428:     }
        !           429: }
        !           430: 
        !           431: void
        !           432: readextensionblock()
        !           433: {
        !           434:     int functioncode, bytecount, zerobytecount = 0;
        !           435: 
        !           436:     fread(ibuf, sizeof(*ibuf), 1, fp_in);
        !           437:     functioncode = ibuf[0];
        !           438: dbprt(stderr, "function code: %d\n", functioncode);
        !           439:     do {
        !           440:        fread(ibuf, sizeof(*ibuf), 1, fp_in);
        !           441:        bytecount = ibuf[0];
        !           442: dbprt(stderr, "byte count: %d\n", bytecount);
        !           443:        if (bytecount) fread(ibuf, sizeof(*ibuf), bytecount, fp_in);
        !           444:        else zerobytecount = 1;
        !           445:     } while (!zerobytecount);
        !           446: }
        !           447: 
        !           448: void
        !           449: writebgscr()
        !           450: {
        !           451:     fprintf(fp_out, "%s %d %d\n", PAGE, page, printed+1);
        !           452:     fputs("/saveobj save def\n", fp_out);
        !           453:     fprintf(fp_out, "%s: %d %d %d %d\n",
        !           454:        "%%PageBoundingBox", 0, 0, scrwidth, scrheight);
        !           455:     if (scrwidth > bburx) bburx = scrwidth;
        !           456:     if (scrheight > bbury) bbury = scrheight;
        !           457:     fprintf(fp_out, "%d %d gifscreen\n", scrwidth, scrheight);
        !           458: }
        !           459: 
        !           460: void
        !           461: writeendscr()
        !           462: {
        !           463:     if ( fp_out == stdout ) printed++;
        !           464:     fputs("showpage\n", fp_out);
        !           465:     fputs("saveobj restore\n", fp_out);
        !           466:     fprintf(fp_out, "%s %d %d\n", ENDPAGE, page, printed);
        !           467: }
        !           468: 
        !           469: void
        !           470: redirect(pg)
        !           471:     int                pg;                     /* next page we're printing */
        !           472: {
        !           473:     static FILE        *fp_null = NULL;        /* if output is turned off */
        !           474: 
        !           475:     if ( pg >= 0 && in_olist(pg) == ON )
        !           476:        fp_out = stdout;
        !           477:     else if ( (fp_out = fp_null) == NULL )
        !           478:        fp_out = fp_null = fopen("/dev/null", "w");
        !           479: 
        !           480: }
        !           481: 
        !           482: void
        !           483: readgif()
        !           484: {
        !           485:     int i, j, k;
        !           486: 
        !           487:     for (i = 0, j = 1, k = 0; i < 13; i++) {
        !           488:        for (; k < j; k++) cstbl[k] = i;
        !           489:        j *= 2;
        !           490:     }
        !           491: 
        !           492:     fread(ibuf, sizeof(*ibuf), 6, fp_in);
        !           493: dbprt(stderr, "%.6s\n", ibuf);
        !           494:     if (strncmp(ibuf, "GIF87a", 6) != 0) {
        !           495:        fread(ibuf, sizeof(*ibuf), 122, fp_in);
        !           496:        fread(ibuf, sizeof(*ibuf), 6, fp_in);
        !           497: dbprt(stderr, "%.6s\n", ibuf);
        !           498:        if (strncmp(ibuf, "GIF87a", 6) != 0)
        !           499:                 error(FATAL, "wrong GIF signature");
        !           500:     }
        !           501:     fread(ibuf, sizeof(*ibuf), 7, fp_in);
        !           502:     scrwidth = ibuf[0] + 256*ibuf[1];
        !           503:     scrheight = ibuf[2] + 256*ibuf[3];
        !           504:     gcolormap = ibuf[4] & 0200;
        !           505:     bitperpixel = (ibuf[4] & 07) + 1;
        !           506:     background = ibuf[5];
        !           507: dbprt(stderr, "scrwidth: %d\n", scrwidth);
        !           508: dbprt(stderr, "scrheight: %d\n", scrheight);
        !           509: dbprt(stderr, "gcolormap: %d\n", gcolormap ? 1 : 0);
        !           510: dbprt(stderr, "bitperpixel: %d\n", bitperpixel);
        !           511: dbprt(stderr, "background: %d\n", background);
        !           512:     if (ibuf[6] != 0) error(FATAL, "wrong screen descriptor");
        !           513:     if (gcolormap) readgcolormap(bitperpixel);
        !           514:     else setcolormap(bitperpixel);
        !           515: 
        !           516:     redirect(++page);
        !           517:     writebgscr();
        !           518: 
        !           519:     cmap = gcmap;
        !           520:     gmap = ggmap;
        !           521:     maplength = gmaplength;
        !           522: 
        !           523:     do {
        !           524:        fread(ibuf, sizeof(*ibuf), 1, fp_in);
        !           525:        if (ibuf[0] == ',') readimage();
        !           526:        else if (ibuf[0] == ';') terminate = 1;
        !           527:        else if (ibuf[0] == '!') readextensionblock();
        !           528:        else
        !           529:        error(FATAL, "wrong image separator character or wrong GIF terminator");
        !           530:     } while (!terminate);
        !           531: 
        !           532:     writeendscr();
        !           533: 
        !           534:     free(gcmap);
        !           535:     free(ggmap);
        !           536: }
        !           537: 
        !           538: void
        !           539: init_signals()
        !           540: {
        !           541:     int         interrupt();            /* signal handler */
        !           542: 
        !           543:     if ( signal(SIGINT, interrupt) == SIG_IGN )  {
        !           544:         signal(SIGINT, SIG_IGN);
        !           545:         signal(SIGQUIT, SIG_IGN);
        !           546:         signal(SIGHUP, SIG_IGN);
        !           547:     }
        !           548:     else {
        !           549:         signal(SIGHUP, interrupt);
        !           550:         signal(SIGQUIT, interrupt);
        !           551:     }
        !           552: 
        !           553:     signal(SIGTERM, interrupt);
        !           554: }
        !           555: 
        !           556: void
        !           557: header()
        !           558: {
        !           559:     int         ch;                     /* return value from getopt() */
        !           560:     int         old_optind = optind;    /* for restoring optind - should be 1 */
        !           561: 
        !           562:     while ( (ch = getopt(argc, argv, optnames)) != EOF )
        !           563:         if ( ch == 'L' )
        !           564:             prologue = optarg;
        !           565:         else if ( ch == '?' )
        !           566:             error(FATAL, "");
        !           567: 
        !           568:     optind = old_optind;                /* get ready for option scanning */
        !           569: 
        !           570:     fprintf(stdout, "%s", CONFORMING);
        !           571:     fprintf(stdout, "%s %s\n", VERSION, PROGRAMVERSION);
        !           572:     fprintf(stdout, "%s %s\n", BOUNDINGBOX, ATEND);
        !           573:     fprintf(stdout, "%s %s\n", PAGES, ATEND);
        !           574:     fprintf(stdout, "%s", ENDCOMMENTS);
        !           575: 
        !           576:     if ( cat(prologue) == FALSE )
        !           577:         error(FATAL, "can't read %s", prologue);
        !           578: 
        !           579:     fprintf(stdout, "%s", ENDPROLOG);
        !           580:     fprintf(stdout, "%s", BEGINSETUP);
        !           581:     fprintf(stdout, "mark\n");
        !           582: 
        !           583: }
        !           584: 
        !           585: void
        !           586: options()
        !           587: {
        !           588:     int                ch;                     /* return value from getopt() */
        !           589: 
        !           590:     while ( (ch = getopt(argc, argv, optnames)) != EOF )  {
        !           591:        switch ( ch )  {
        !           592: 
        !           593:            case 'a':                   /* aspect ratio */
        !           594:                    fprintf(stdout, "/aspectratio %s def\n", optarg);
        !           595:                    break;
        !           596: 
        !           597:            case 'c':                   /* copies */
        !           598:                    copies = atoi(optarg);
        !           599:                    fprintf(stdout, "/#copies %s store\n", optarg);
        !           600:                    break;
        !           601: 
        !           602:            case 'f':
        !           603:                    negative = TRUE;
        !           604:                    break;
        !           605: 
        !           606:            case 'g':
        !           607:                    gray = TRUE;
        !           608:                    break;
        !           609: 
        !           610:            case 'l':
        !           611:                    fprintf(stdout, "/alignment true def\n");
        !           612:                    break;
        !           613: 
        !           614:            case 'm':                   /* magnification */
        !           615:                    fprintf(stdout, "/magnification %s def\n", optarg);
        !           616:                    break;
        !           617: 
        !           618:            case 'n':                   /* forms per page */
        !           619:                    formsperpage = atoi(optarg);
        !           620:                    fprintf(stdout, "%s %s\n", FORMSPERPAGE, optarg);
        !           621:                    fprintf(stdout, "/formsperpage %s def\n", optarg);
        !           622:                    break;
        !           623: 
        !           624:            case 'o':                   /* output page list */
        !           625:                    out_list(optarg);
        !           626:                    break;
        !           627: 
        !           628:            case 'p':                   /* landscape or portrait mode */
        !           629:                    if ( *optarg == 'l' )
        !           630:                        fprintf(stdout, "/landscape true def\n");
        !           631:                    else fprintf(stdout, "/landscape false def\n");
        !           632:                    break;
        !           633: 
        !           634:            case 'x':                   /* shift things horizontally */
        !           635:                    fprintf(stdout, "/xoffset %s def\n", optarg);
        !           636:                    break;
        !           637: 
        !           638:            case 'y':                   /* and vertically on the page */
        !           639:                    fprintf(stdout, "/yoffset %s def\n", optarg);
        !           640:                    break;
        !           641: 
        !           642:            case 'C':                   /* copy file straight to output */
        !           643:                    if ( cat(optarg) == FALSE )
        !           644:                        error(FATAL, "can't read %s", optarg);
        !           645:                    break;
        !           646: 
        !           647:            case 'D':                   /* debug flag */
        !           648:                    debug = ON;
        !           649:                    break;
        !           650: 
        !           651:            case 'G':
        !           652:                    gammaflag = ON;
        !           653:                    gamma = atof(optarg);
        !           654:                    break;
        !           655: 
        !           656:            case 'I':                   /* ignore FATAL errors */
        !           657:                    ignore = ON;
        !           658:                    break;
        !           659: 
        !           660:            case 'L':                   /* PostScript prologue file */
        !           661:                    prologue = optarg;
        !           662:                    break;
        !           663: 
        !           664:            case 'P':                   /* PostScript pass through */
        !           665:                    fprintf(stdout, "%s\n", optarg);
        !           666:                    break;
        !           667: 
        !           668:            case '?':                   /* don't understand the option */
        !           669:                    error(FATAL, "");
        !           670:                    break;
        !           671: 
        !           672:            default:                    /* don't know what to do for ch */
        !           673:                    error(FATAL, "missing case for option %c\n", ch);
        !           674:                    break;
        !           675: 
        !           676:        }
        !           677:     }
        !           678: 
        !           679:     argc -= optind;                    /* get ready for non-option args */
        !           680:     argv += optind;
        !           681: }
        !           682: 
        !           683: void
        !           684: setup()
        !           685: {
        !           686:     fprintf(stdout, "setup\n");
        !           687: 
        !           688:     if ( formsperpage > 1 )  {          /* followed by stuff for multiple pages
        !           689: */
        !           690:         if ( cat(formfile) == FALSE )
        !           691:             error(FATAL, "can't read %s", formfile);
        !           692:         fprintf(stdout, "%d setupforms\n", formsperpage);
        !           693:     }   /* End if */
        !           694: 
        !           695:     fprintf(stdout, "%s", ENDSETUP);
        !           696: 
        !           697: }
        !           698: 
        !           699: void
        !           700: arguments()
        !           701: {
        !           702:     if ( argc < 1 ) {
        !           703:        fp_in = stdin;
        !           704:        readgif();
        !           705:     }
        !           706:     else  {                            /* at least one argument is left */
        !           707:        while ( argc > 0 )  {
        !           708:            if ( strcmp(*argv, "-") == 0 )
        !           709:                fp_in = stdin;
        !           710:            else if ( (fp_in = fopen(*argv, "r")) == NULL )
        !           711:                error(FATAL, "can't open %s", *argv);
        !           712:            readgif();
        !           713:            if ( fp_in != stdin )
        !           714:                fclose(fp_in);
        !           715:            argc--;
        !           716:            argv++;
        !           717:        }
        !           718:     }
        !           719: }
        !           720: 
        !           721: void
        !           722: done()
        !           723: {
        !           724:     fprintf(stdout, "%s", TRAILER);
        !           725:     fprintf(stdout, "done\n");
        !           726:     fprintf(stdout, "%s 0 0 %d %d\n", BOUNDINGBOX, bburx, bbury);
        !           727:     fprintf(stdout, "%s %d\n", PAGES, printed); 
        !           728: }
        !           729: 
        !           730: main(agc, agv)
        !           731:     int agc;
        !           732:     char *agv[];
        !           733: {
        !           734:     argc = agc;
        !           735:     argv = agv;
        !           736:     prog_name = argv[0];
        !           737: 
        !           738:     init_signals();
        !           739:     header();
        !           740:     options();
        !           741:     setup();
        !           742:     arguments();
        !           743:     done();
        !           744: 
        !           745:     exit(0);
        !           746: }
        !           747: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.