Annotation of 41BSD/cmd/cifplot/input.c, revision 1.1

1.1     ! root        1: /*******************************************************************
        !             2: *                                                                  *
        !             3: *    File: CIFPLOT/input.c                                         *
        !             4: *    Written by Dan Fitzpatrick                                    *
        !             5: *    copyright 1980 -- Regents of the University of California     *
        !             6: *                                                                  *
        !             7: ********************************************************************/
        !             8: 
        !             9: 
        !            10: #include <stdio.h>
        !            11: #include "defs.h"
        !            12: #include "globals.h"
        !            13: #include "scanner.h"
        !            14: 
        !            15: IMPORT string Concat();
        !            16: IMPORT alloc();
        !            17: IMPORT PrintError();
        !            18: IMPORT expand();
        !            19: 
        !            20: extern struct ErrorType *ErrorList;    /* Need to print line if there
        !            21:                                         * is an error          */
        !            22: int bad,LineLevel;
        !            23: 
        !            24: InitInput(b,n,v)
        !            25: int b,n;
        !            26: char **v;
        !            27: {
        !            28:    CharNo = 0;
        !            29:    LineNo = 1;
        !            30: 
        !            31:    /* Allocate space for input buffer */
        !            32:    in_buf_size = InBufSize;
        !            33:    in_buf = (char *) alloc(in_buf_size);
        !            34:    in_store = (char *) alloc(in_buf_size);
        !            35:    
        !            36:    argv = v;
        !            37:    argb = b;
        !            38:    argc = n;
        !            39: 
        !            40:    if (argb<argc) {
        !            41:        /* Open first cif file */
        !            42:        if (NULL == (in_file[0] = fopen(argv[argb],"r"))) {
        !            43:                perror(argv[argb]);
        !            44:                exit(1);
        !            45:                }
        !            46:        CurrentFile = FileName[0] = Concat(argv[argb],0);
        !            47:        return(1);
        !            48:         }
        !            49:      else {
        !            50:        /* Error if no file to read */
        !            51:        fprintf(stderr,"Usage: %s options file1.cif file2.cif ...\n",argv[0]);
        !            52:        exit(0);
        !            53:        }
        !            54:    }
        !            55: 
        !            56: Next()
        !            57: /* Next gets the next file to be read,
        !            58:  * and returns 0. It returns 1 when there
        !            59:  * are no files left.
        !            60:  */
        !            61: {
        !            62:     fclose(in_file[FStackPtr]);
        !            63:     Free(FileName[FStackPtr]);
        !            64:     /* Check to see that no files are stacked up. If there are
        !            65:      * some open them before reading next file */
        !            66:     if (FStackPtr == 0) {
        !            67:        if (++argb < argc) {
        !            68:                if (NULL == (in_file[0] = fopen(argv[argb],"r"))) {
        !            69:                        fprintf(stderr,"Can't read %s\n",argv[argb]);
        !            70:                        perror("");
        !            71:                        exit();
        !            72:                        }
        !            73:                if(debug>1) fprintf(stderr,"Begin next file: %s\n",argv[argb]);
        !            74:                CurrentFile = FileName[0] = Concat(argv[argb],0);
        !            75:                return(0);
        !            76:                }
        !            77:            else {
        !            78:                return(1);
        !            79:                }
        !            80:        }
        !            81:       else {
        !            82:        FStackPtr--;
        !            83:        CurrentFile = FileName[FStackPtr];
        !            84:        if(debug>1) fprintf(stderr,"Finnished with file %s, resume file %s\n",FileName[FStackPtr+1],CurrentFile);
        !            85:        return(0);
        !            86:        }
        !            87:    }
        !            88: 
        !            89: Include(s)
        !            90: char *s;
        !            91: /* Open file 's', stack current file */
        !            92: {
        !            93:    if (debug>1) fprintf(stderr,"File switch from %s to %s\n",CurrentFile,s);
        !            94:    FStackPtr++;
        !            95:    if (FStackPtr < FStackSize) {
        !            96:        if (NULL == (in_file[FStackPtr] = fopen(s,"r"))) {
        !            97:                FStackPtr--;
        !            98:                fprintf(stderr,"Unable to read %s which was called from %s",s,CurrentFile);
        !            99:                fprintf(stderr,"---Include command ignored\n");
        !           100:                perror("");
        !           101:                }
        !           102:           else {
        !           103:                CurrentFile = FileName[FStackPtr] = Concat(s,0);
        !           104:                }
        !           105:        }
        !           106:       else {
        !           107:         fprintf(stderr,"Can't open %s---Too many files already open\n",s);
        !           108:         FStackPtr--;
        !           109:         }
        !           110:    }
        !           111: 
        !           112: input()
        !           113: /* Return next input character */
        !           114: {
        !           115:    char ch;
        !           116:    int i;
        !           117:    
        !           118:    if(EOF == (ch = getc(in_file[FStackPtr]))) ch=0;
        !           119:    if (0 <= CharNo && CharNo < in_buf_size) in_buf[CharNo] = ch;
        !           120:        else 
        !           121:           if (CharNo >= in_buf_size) {
        !           122:              
        !           123:              /* Buffer is too small - Double its size */
        !           124:               if(debug>1) fprintf(stderr,"Increase input buffer size to %d\n",2*in_buf_size);
        !           125:               in_buf_size = in_buf_size*2;
        !           126:               in_buf = (char *) expand(in_buf,in_buf_size);
        !           127:               in_store = (char *) expand(in_store,in_buf_size);
        !           128:               in_buf[CharNo] = ch;
        !           129:               }
        !           130:    CharNo++;
        !           131:    if (ch == '\n') {
        !           132:        if ((list || (ErrorList != NIL)) && (! bad)) {
        !           133:                /* Print the line if we are in listing mode or
        !           134:                 * there is an error on that line       */
        !           135:                fprintf(stderr,"%6d    ",LineNo);
        !           136:                for(i=0; i<CharNo; i++) if (i < in_buf_size) fprintf(stderr,"%c",in_buf[i]);
        !           137:                OldLength = CharNo;
        !           138:                maxlines = LineNo;
        !           139:                bad++;
        !           140:                }
        !           141:        if(!bad)  Store();
        !           142:        LineNo++;
        !           143:        CharNo = 0;
        !           144:        PrintError();
        !           145:        if(--bad < 0) bad = 0;
        !           146:        }
        !           147:    return(ch);
        !           148:    }
        !           149: 
        !           150: unput(c)
        !           151: char c;
        !           152: /* Put 'c' back into the input stream */
        !           153: {
        !           154:    CharNo--;
        !           155:    if (c == '\n') {
        !           156:        LineNo--;
        !           157:        /* bad indicates how many lines have been put back */
        !           158:        bad++;
        !           159:        }
        !           160:    ungetc(c,in_file[FStackPtr]);
        !           161:    return;
        !           162:    }
        !           163: 
        !           164: Store()
        !           165: /* This function saves the last input line */
        !           166: {
        !           167:     char *q;
        !           168:     LineLevel = LineNo;
        !           169:     OldLength = CharNo;
        !           170:     q = in_store;
        !           171:     in_store = in_buf;
        !           172:     in_buf = q;
        !           173:     return;
        !           174:     }
        !           175: 
        !           176: 
        !           177: PrintLine(n)
        !           178: int n;
        !           179: /* If line n is currently available 'PrintLine' will print it */
        !           180: {
        !           181:    int i;
        !           182:    if((LineLevel == n) && (0 < n)) {
        !           183:        fprintf(stderr,"%6d    ",n);
        !           184:        for(i=0; i<OldLength;i++) fprintf(stderr,"%c",in_store[i]);
        !           185:        bad++;
        !           186:        maxlines = n;
        !           187:        LineLevel = -1;
        !           188:        }
        !           189:    }

unix.superglobalmegacorp.com

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