1: /*
   2:  *  Interpress utility - count the number of pages in a interpress file
   3:  *
   4:  *  Written for Xerox Corporation by Lee Moore & William LeFebvre
   5:  *
   6:  * Copyright (c) 1984, 1985 Xerox Corp.
   7:  *
   8:  * History:
   9:  *	 2-sep-85 lee moore	created out of iptotext.c
  10:  */
  11: 
  12: #ifdef vax11c
  13: # include stdio
  14: # include setjmp
  15: # include ctype
  16: # include "iptokens.h"
  17: # include "ipnames.h"
  18: #else
  19: # include <stdio.h>
  20: # include <setjmp.h>
  21: # include <ctype.h>
  22: # include "iptokens.h"
  23: # include "ipnames.h"
  24: #endif
  25: 
  26: jmp_buf next_file;
  27: 
  28: extern int errno;
  29: 
  30: main(argc, argv)
  31: 
  32: int  argc;
  33: char *argv[];
  34: 
  35: {
  36:     FILE *acctFile;
  37:     int c,
  38:     pageCount;
  39:     char *login,
  40:      *host;
  41:     extern int optind;
  42:     extern char *optarg;
  43: 
  44:     login = NULL;
  45:     host = NULL;
  46: 
  47:     while ((c = getopt(argc, argv, "cw:l:i:n:h:")) != EOF)
  48:     switch (c) {
  49:         case 'c':
  50:         case 'w':
  51:         case 'l':
  52:         case 'i':
  53:             break;
  54: 
  55:         case 'n':
  56:             login = optarg;
  57:             break;
  58: 
  59:         case 'h':
  60:             host = optarg;
  61:             break;
  62: 
  63:         default:
  64:             printf("option '%c' not allowed\n");
  65:     }
  66: 
  67:     if (argc - optind == 1)
  68:     {
  69:     pageCount = do_file(stdin);
  70: 
  71:     if( pageCount < 0 )
  72:         exit(2);
  73: 
  74:     if( (acctFile = fopen(argv[optind], "a")) == NULL ) {
  75:         fprintf(stderr, "ipf: can't open acct file: %s\n", argv[optind]);
  76:         exit(2);
  77:     }
  78: 
  79:     if( login )
  80:         if( host )
  81:         fprintf(acctFile, "%d\t%s:%s\n", pageCount, host, login);
  82:         else
  83:         fprintf(acctFile, "%d\t%s\n", pageCount, login);
  84: 
  85:     exit(0);
  86:     }
  87: 
  88: }
  89: 
  90: 
  91: /*
  92:  * process one file
  93:  */
  94: 
  95: do_file(file)
  96: FILE *file;
  97: {
  98: # define  Buffsize  256
  99:     char buff[Buffsize];
 100:     char *ptr;
 101:     int len;
 102:     int bodyDepth;  /* how many bodies down we are */
 103:     register int bodyCount, /* how many bodies we have seen so far */
 104:              val,
 105:              byte;      /* has to be "int" for stdio EOF detection */
 106:                 /* stdio is a pile! */
 107:     int hlen;
 108: 
 109:     hlen = strlen(IP_Header);
 110: 
 111:     /* for error recovery */
 112:     if (setjmp(next_file) != 0)
 113:     {
 114:     return -1;
 115:     }
 116: 
 117:     /* get the header */
 118:     for (hlen = 0, ptr = buff; hlen < Buffsize; hlen++)
 119:     {
 120:     if ((*ptr++ = getnoeofc(file)) == ' ')
 121:         break;
 122:     }
 123:     *ptr = '\0';
 124: 
 125:     /* check the validity of the header */
 126:     if (strcmp(buff, IP_Header) != 0)
 127:     {
 128:     fprintf(stderr, " (INVALID HEADER!)");
 129:     }
 130: 
 131:     bodyDepth = 0;
 132:     bodyCount = 0;
 133: 
 134:     /* main loop */
 135:     while ((byte = getc(file)) != EOF)
 136:     {
 137:     if ((byte & 0200) == 0)
 138:     {
 139:         /* a short number */
 140:         val = (byte << 8) + getnoeofc(file) - INTEGER_ZERO;
 141:     }
 142:     else
 143:     {
 144:         /* something else */
 145:         switch(byte >> 5)
 146:         {
 147:         case (SHORT_OP >> 5):
 148:             break;
 149: 
 150:         case (LONG_OP >> 5):
 151:             val = ((byte & 037) << 8) + getnoeofc(file);
 152:             if( val == OP_beginBody )
 153:             {
 154:             bodyDepth++;
 155:             }
 156:             else if( val == OP_endBody )
 157:             {
 158:             bodyDepth--;
 159: 
 160:             /* is this a top level body? */
 161:             if( bodyDepth == 0 )
 162:                 bodyCount++;
 163:             }
 164:             break;
 165: 
 166:         case (SHORT_SEQUENCE >> 5):
 167:             len = getnoeofc(file);
 168:             eatBytes(file, len);
 169:             break;
 170: 
 171:         case (LONG_SEQUENCE >> 5):
 172:             len  =  getnoeofc(file) << 16;
 173:             len += (getnoeofc(file) << 8);
 174:             len += getnoeofc(file);
 175:             eatBytes(file, len);
 176:             break;
 177:         }
 178:     }
 179:     }
 180: 
 181:     return bodyCount - 1;   /* the preamble is an extra body */
 182: }
 183: 
 184: 
 185: /*
 186:  * get a character
 187:  */
 188: 
 189: getnoeofc(file)
 190: 
 191: FILE *file;
 192: 
 193: {
 194:     register int val;
 195: 
 196: #ifdef vax11c
 197:     val= getc(file);
 198:     if ( feof(file) )
 199: #else
 200:     if ((val = getc(file)) == EOF)
 201: #endif
 202:     {
 203:     fprintf(stderr, "Unexpected EOF!");
 204:     longjmp(next_file, 1);
 205:     }
 206:     return(val);
 207: }
 208: 
 209: 
 210: /*
 211:  * read some bytes from the input stream
 212:  */
 213: 
 214: eatBytes(file, length)
 215: FILE *file;
 216: int length;
 217: 
 218: {
 219:     register int count;
 220: 
 221:     count = length;
 222: 
 223:     while(count-- > 0)
 224:     {
 225:     (void) getnoeofc(file);
 226:     }
 227: }

Defined functions

do_file defined in line 95; used 1 times
  • in line 69
eatBytes defined in line 214; used 2 times
getnoeofc defined in line 189; used 8 times
main defined in line 30; never used

Defined variables

next_file defined in line 26; used 2 times

Defined macros

Buffsize defined in line 98; used 2 times
Last modified: 1985-09-10
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1060
Valid CSS Valid XHTML 1.0 Strict