Annotation of 43BSDTahoe/new/dipress/src/bin/iptotext/iptotext.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  *  Interpress utility
                      3:  *
                      4:  *  Written for Xerox Corporation by William LeFebvre
                      5:  *
                      6:  *  7-June-1984
                      7:  *
                      8:  * Copyright (c) 1984, 1985, 1986 Xerox Corp.
                      9:  *
                     10:  * HISTORY
                     11:  * 20-Mar-86  Lee Moore (lee) at Xerox Webster Research Center
                     12:  *     Changed the output comment for sequencePackedPixel to print out the
                     13:  *     number of bits per sample and the number of samples per scan line.
                     14:  *
                     15:  * 13-Jan-86  lee at Xerox, WRC
                     16:  *     Changed a call to strmpn to strncmp.
                     17:  *
                     18:  * 01-Dec-85  lee at Xerox, WRC
                     19:  *     Linted
                     20:  *
                     21:  * 11-oct-85 ed flint
                     22:  *     don't worry about version # in Interpress header
                     23:  *     look only for 'Interpress/Xerox/'
                     24:  *
                     25:  * 23-may-85 ed flint
                     26:  *     fixed bug in print_string_from_file & print_string
                     27:  *     that would strip off high byte of ch in isprint
                     28:  *
                     29:  * 8-apr-85  ed flint
                     30:  *     add -d option to dump pixel arrays
                     31:  *     conditional compilation for vax-11c (vms)
                     32:  */
                     33: 
                     34: /*
                     35:  *  iptotext - convert an encoded interpress file into readable text
                     36:  */
                     37: 
                     38: #ifdef vax11c
                     39: # include stdio
                     40: # include setjmp
                     41: # include ctype
                     42: # include "iptokens.h"
                     43: # include "ipnames.h"
                     44: #else
                     45: # include <stdio.h>
                     46: # include <setjmp.h>
                     47: # include <ctype.h>
                     48: # include "iptokens.h"
                     49: # include "ipnames.h"
                     50: #endif
                     51: 
                     52: int hlen;
                     53: jmp_buf next_file;
                     54: FILE *file;
                     55: FILE *output;
                     56: 
                     57: int errno;             /* imported value */
                     58: 
                     59: char *op_name();
                     60: int dump_pixel= 0;
                     61: 
                     62: main(argc, argv)
                     63: 
                     64: int  argc;
                     65: char *argv[];
                     66: 
                     67: {
                     68:     int arg;
                     69:     int temp;
                     70:     char *outputname;
                     71: 
                     72:     hlen = strlen(IP_Header);
                     73: 
                     74:     outputname= 0;
                     75: 
                     76:     /* look for options */
                     77:     for ( arg= 1; arg < argc; arg++ )
                     78:     {
                     79:        if ( argv[arg][0] == '-' )
                     80:        {
                     81:            switch(argv[arg][1])
                     82:            {
                     83:                case('o'):
                     84:                case('O'):
                     85:                    if ( strlen(argv[arg]) > 2 )
                     86:                       outputname= &(argv[arg][2]);
                     87:                    else
                     88:                       outputname= argv[++arg];
                     89:                    break;
                     90: 
                     91:                case('d'):
                     92:                case('D'):
                     93:                    dump_pixel= 1;
                     94:                    break;
                     95: 
                     96:                default:
                     97:                    fprintf(stderr,"invalid option %s\n",argv[arg]);
                     98:                    break;
                     99:            }
                    100:        }
                    101:        else
                    102:        {
                    103:            break;
                    104:        }
                    105:     }
                    106: 
                    107:        /* open the output file */
                    108:     if ( outputname != 0 )
                    109:     {
                    110:        if ((output = fopen(outputname, "w")) == NULL)
                    111:        {
                    112:            /* save errno value */
                    113:            temp = errno;
                    114:            fputs("iptotext: ", stderr);
                    115:            errno = temp;
                    116:            perror(outputname);
                    117:            exit(1);
                    118:        }
                    119:     }
                    120:     else
                    121:     {
                    122:        /* default output -- use stdout */
                    123:        output = stdout;
                    124:     }
                    125: 
                    126:     if (argc == 1)
                    127:     {
                    128:        file = stdin;
                    129:        do_file();
                    130:        (void) putc('\n', output);
                    131:     }
                    132: 
                    133:     /* make the begin- and end-block names be upper case */
                    134:     op_names[OP_beginBlock] = "BEGIN";
                    135:     op_names[OP_endBlock]   = "END";
                    136: 
                    137:     for ( ; arg < argc; arg++)         /* assume arg is index of first file name */
                    138:     {
                    139:        if ((file = fopen(argv[arg], "r")) == NULL)
                    140:        {
                    141:            perror(argv[arg]);
                    142:            continue;           /* on to the next file */
                    143:        }
                    144: 
                    145:        /* print pretty banner */
                    146:        fprintf(output, "(File: \"%s\")\n", argv[arg]);
                    147: 
                    148:        do_file();
                    149:        (void) fclose(file);
                    150:        (void) putc('\n', output);
                    151:     }
                    152: }
                    153: 
                    154: do_file()
                    155: 
                    156: {
                    157: # define  Buffsize     256
                    158:     unsigned char buff[Buffsize];
                    159:     unsigned char *ptr;
                    160:     int val;
                    161:     int len;
                    162:     int byte;          /* has to be "int" for stdio EOF detection */
                    163:                        /* stdio is a pile! */
                    164: 
                    165:     /* for error recovery */
                    166:     if (setjmp(next_file) != 0)
                    167:     {
                    168:        return;
                    169:     }
                    170: 
                    171:     /* get the header */
                    172:     for (hlen = 0, ptr = buff; hlen < Buffsize; hlen++)
                    173:     {
                    174:        if ((*ptr++ = getnoeofc(file)) == ' ')
                    175:            break;
                    176:     }
                    177:     *ptr = '\0';
                    178: 
                    179:     /* display the header */
                    180:     fputs("Header: ", output);
                    181:     print_string(buff);
                    182: 
                    183:     /* check the validity of the header */
                    184:     if (strncmp((char *)buff, IP_Header, 17) != 0)
                    185:     {
                    186:        fprintf(output, " (INVALID HEADER!)");
                    187:     }
                    188:     (void) putc('\n', output);
                    189: 
                    190:     /* main loop */
                    191:     while ((byte = getc(file)) != EOF)
                    192:     {
                    193:        if ((byte & 0200) == 0)
                    194:        {
                    195:            /* a short number */
                    196:            val = (byte << 8) + getnoeofc(file) - INTEGER_ZERO;
                    197:            fprintf(output, "%d\n", val);
                    198:        }
                    199:        else
                    200:        {
                    201:            /* something else */
                    202:            switch(byte >> 5)
                    203:            {
                    204:                case (SHORT_OP >> 5):
                    205:                    fprintf(output, "%s\n", op_name(byte & 037));
                    206:                    break;
                    207: 
                    208:                case (LONG_OP >> 5):
                    209:                    val = ((byte & 037) << 8) + getnoeofc(file);
                    210:                    fprintf(output, "%s%s\n", op_name(val),
                    211:                        val == OP_beginBlock || val == OP_endBlock ?
                    212:                            " (block)" : "");
                    213:                    break;
                    214: 
                    215:                case (SHORT_SEQUENCE >> 5):
                    216:                    len = getnoeofc(file);
                    217:                    fputs("> ", output);
                    218:                    do_sequence(byte & 037, len);
                    219:                    break;
                    220: 
                    221:                case (LONG_SEQUENCE >> 5):
                    222:                    len  =  getnoeofc(file) << 16;
                    223:                    len += (getnoeofc(file) << 8);
                    224:                    len += getnoeofc(file);
                    225:                    fputs(">>", output);
                    226:                    do_sequence(byte & 037, len);
                    227:                    break;
                    228:            }
                    229:        }
                    230:     }
                    231: }
                    232: 
                    233: do_sequence(type, length)
                    234: 
                    235: int type;
                    236: int length;
                    237: 
                    238: {
                    239:     int val;
                    240:     int val2;
                    241: 
                    242:     switch(type)
                    243:     {
                    244:        case sequenceAdaptivePixelVector:
                    245:            fprintf(output, "Adaptive Pixel Vector: (%d words) [\n", length/2);
                    246:            print_words_from_file(file, length);
                    247:            fputs("]\n", file);
                    248:            break;
                    249: 
                    250:        case sequenceComment:
                    251:            fputs("Comment: ", output);
                    252:            print_string_from_file(file, length);
                    253:            (void) putc('\n', output);
                    254:            break;
                    255: 
                    256:        case sequenceCompressedPixelVector:
                    257:            fprintf(output, "Compressed Pixel Vector: (%d words) [\n", length/2);
                    258:            print_words_from_file(file, length);
                    259:            fputs("]\n", file);
                    260:            break;
                    261: 
                    262:        case sequenceContinued:
                    263:            fprintf(output, "Continuing last sequence: ");
                    264:            break;
                    265: 
                    266:        case sequenceIdentifier:
                    267:            fprintf(output, "Identifier: ");
                    268:            iocopy(length);
                    269:            fputs("\n", output);
                    270:            break;
                    271: 
                    272:        case sequenceInsertFile:
                    273:            fputs("Insert file: ", output);
                    274:            print_string_from_file(file, length);
                    275:            (void) putc('\n', output);
                    276:            break;
                    277: 
                    278:        case sequenceInteger:
                    279:            val = getint(length);
                    280:            fprintf(output, "Integer: %d\n", val);
                    281:            (void) putc('\n', output);
                    282:            break;
                    283: 
                    284:        case sequenceLargeVector:
                    285: #ifdef notdef
                    286:            val = getnoeofc(file);
                    287:            fprintf(output, "Large Pixel Vector: (%d words of %d bytes) [\n"
                    288:                (length - 1) / val, val);
                    289: #endif
                    290:            break;
                    291: 
                    292:        case sequencePackedPixelVector:
                    293:            val = getint(2);
                    294:            val2 = getint(2);
                    295:            fprintf(output, "Packed Pixel Vector:  (%d + 2 = %d words, %d bit(s) per sample, %d sample(s) per scanline) [\n",
                    296:                        length/2 -2, length/2, val, val2);
                    297:            print_words_from_file(file, length - 4);
                    298:            fputs("]\n", output);
                    299:            break;
                    300: 
                    301:        case sequenceRational:
                    302:            length >>= 1;
                    303:            val = getint(length);
                    304:            val2 = getint(length);
                    305:            fprintf(output, "Rational: %d/%d ", val, val2);
                    306:            if (val2 != 0)
                    307:            {
                    308:                fprintf(output, "(%f)\n", (float)val / (float)val2);
                    309:            }
                    310:            else
                    311:            {
                    312:                fputs("(???)\n", output);
                    313:            }
                    314:            break;
                    315: 
                    316:        case sequenceString:
                    317:            fputs("String: ", output);
                    318:            print_string_from_file(file, length);
                    319:            (void) putc('\n', output);
                    320:            break;
                    321:     }
                    322: }
                    323: 
                    324: iocopy(length)
                    325: 
                    326: int length;
                    327: 
                    328: {
                    329:     int byte;
                    330: 
                    331:     while(length-- > 0)
                    332:     {
                    333:        byte = getnoeofc(file);
                    334:        (void) putc(byte, output);
                    335:     }
                    336: }
                    337: 
                    338: getint(length)
                    339: 
                    340: int length;
                    341: 
                    342: {
                    343:     int val;
                    344: 
                    345:     val = getnoeofc(file);
                    346: 
                    347:     if ((val & 0x80) != 0) {
                    348:        /* this is a negative number -- extend the sign */
                    349:        val |= (-1 & ~(0xFF));
                    350:     }
                    351: 
                    352:     while (--length > 0) {
                    353:        val <<= 8;
                    354:        val |= getnoeofc(file);
                    355:     }
                    356: 
                    357:     return(val);
                    358: }
                    359: 
                    360: char *op_name(op_code)
                    361: 
                    362: int op_code;
                    363: 
                    364: {
                    365:     static char nbuff[10];
                    366: 
                    367:     if (op_names[op_code] == NULL)
                    368:     {
                    369:        (void) sprintf(nbuff, "--Unknown op: %d--", op_code);
                    370:        return(nbuff);
                    371:     }
                    372:     else
                    373:     {
                    374:        return(op_names[op_code]);
                    375:     }
                    376: }
                    377: 
                    378: getnoeofc(file)
                    379: 
                    380: FILE *file;
                    381: 
                    382: {
                    383:     int val;
                    384: 
                    385: #ifdef vax11c
                    386:     val= getc(file);
                    387:     if ( feof(file) )
                    388: #else
                    389:     if ((val = getc(file)) == EOF)
                    390: #endif
                    391:     {
                    392:        fprintf(output, "Unexpected EOF!");
                    393:        longjmp(next_file, 1);
                    394:     }
                    395:     return(val);
                    396: }
                    397: 
                    398: print_string_from_file(file, length)
                    399: 
                    400: FILE *file;
                    401: int length;
                    402: 
                    403: {
                    404:     register int ch;
                    405:     register int val;
                    406: 
                    407:     (void) putc('"', output);
                    408:     for (val = 0; val < length; val++)
                    409:     {
                    410:        ch = getnoeofc(file);
                    411:        if ( ((ch & 0x80) == 0) && (isprint(ch)) )
                    412:        {
                    413:            if (ch == '"' || ch == '\\')
                    414:            {
                    415:                (void) putc('\\', output);
                    416:            }
                    417:            (void) putc(ch, output);
                    418:        }
                    419:        else
                    420:        {
                    421:            fprintf(output, "\\%03o", ch);
                    422:        }
                    423:     }
                    424:     (void) putc('"', output);
                    425: }
                    426: 
                    427: print_string(string)
                    428: 
                    429: unsigned char *string;
                    430: 
                    431: {
                    432:     register unsigned char *ptr;
                    433:     register unsigned char ch;
                    434: 
                    435:     (void) putc('"', output);
                    436:     ptr = string;
                    437:     while ((ch = *ptr++) != '\0')
                    438:     {
                    439:        if ( ((ch & 0x80) == 0) && (isprint(ch)) )
                    440:        {
                    441:            if (ch == '"')
                    442:            {
                    443:                (void) putc('\\', output);
                    444:            }
                    445:            (void) putc(ch, output);
                    446:        }
                    447:        else
                    448:        {
                    449:            fprintf(output, "\\%03o", ch);
                    450:        }
                    451:     }
                    452:     (void) putc('"', output);
                    453: }
                    454: 
                    455: print_words_from_file(file, length)
                    456: 
                    457: FILE *file;
                    458: int length;
                    459: 
                    460: {
                    461:     int val;
                    462:     int cnt = 0;
                    463: 
                    464:     if ( dump_pixel == 1 )
                    465:     {
                    466:         while (length > 0)
                    467:        {
                    468:            val = getnoeofc(file) << 8;
                    469:            val += getnoeofc(file);
                    470:     
                    471:            fprintf(output, "%04x  ", val);
                    472:            if (++cnt > 12)
                    473:            {
                    474:                (void) fputc('\n',output);
                    475:                cnt = 0;
                    476:            }
                    477: 
                    478:            length -= 2;
                    479:        }
                    480:     }
                    481:     else
                    482:     {
                    483:        while ( length > 0 )
                    484:        {
                    485:            val= getnoeofc(file);
                    486:            length--;
                    487:        }       
                    488:     }
                    489: }

unix.superglobalmegacorp.com

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