Annotation of 43BSD/contrib/spms/src/lib/pgrep/readmf.c, revision 1.1

1.1     ! root        1: /* $Header$ */
        !             2: 
        !             3: /*
        !             4:  * Author: Peter J. Nicklin
        !             5:  */
        !             6: #include <ctype.h>
        !             7: #include <stdio.h>
        !             8: #include "macro.h"
        !             9: #include "null.h"
        !            10: #include "pgrep.h"
        !            11: #include "slist.h"
        !            12: #include "yesno.h"
        !            13: 
        !            14: #define DEPENDMARK             "###"
        !            15: 
        !            16: static char Iobuf[BUFSIZ];             /* I/O line buffer */
        !            17: static short Continue;                 /* does the line continue? */
        !            18: 
        !            19: /*
        !            20:  * dependmark() returns 1 if the current input line begins with a string
        !            21:  * marking the beginning of generated dependency lines, otherwise zero.
        !            22:  */
        !            23: dependmark()
        !            24: 
        !            25: {
        !            26:        if (EQUAL(Iobuf, DEPENDMARK))
        !            27:                return(1);
        !            28:        return(0);
        !            29: }
        !            30: 
        !            31: 
        !            32: 
        !            33: /*
        !            34:  * findmacro() searchs a line for a macro definition. Returns the name,
        !            35:  * or null if not found.
        !            36:  */
        !            37: char *
        !            38: findmacro(macroname)
        !            39:        char *macroname;                /* macro name receiving buffer */
        !            40: {
        !            41:        register char *bp;              /* buffer pointer */
        !            42:        register char *mp;              /* macro name pointer */
        !            43: 
        !            44:        bp = Iobuf;
        !            45:        mp = macroname;
        !            46:        while (*bp == ' ')
        !            47:                bp++;
        !            48:        if (!isalnum(*bp))
        !            49:                return(NULL);
        !            50:        while(isalnum(*bp))
        !            51:                *mp++ = *bp++;
        !            52:        *mp = '\0';
        !            53:        while(*bp == ' ' || *bp == '\t')
        !            54:                bp++;
        !            55:        if (*bp != '=')
        !            56:                return(NULL);
        !            57:        return(macroname);
        !            58: }
        !            59:        
        !            60: 
        !            61: 
        !            62: /*
        !            63:  * getlin() stores a line from input stream in Iobuf. The string is terminated
        !            64:  * by a newline character which is replaced by a null character. getlin()
        !            65:  * returns Iobuf, or null pointer upon end of file.
        !            66:  */
        !            67: char *
        !            68: getlin(stream)
        !            69:        register FILE *stream;          /* input stream */
        !            70: {
        !            71:        register int c;                 /* current character */
        !            72:        register char *iop;             /* Iobuf pointer */
        !            73: 
        !            74:        iop = Iobuf;
        !            75:        while ((c = getc(stream)) != '\n' && c != EOF)
        !            76:                *iop++ = c;
        !            77:        if (c == EOF && iop == Iobuf)
        !            78:                return(NULL);
        !            79:        if (iop != Iobuf && iop[-1] == '\\')
        !            80:                {
        !            81:                iop[-1] = '\0';
        !            82:                Continue = YES;
        !            83:                }
        !            84:        else    {
        !            85:                iop[0] = '\0';
        !            86:                Continue = NO;
        !            87:                }
        !            88:        return(Iobuf);
        !            89: }
        !            90: 
        !            91: 
        !            92: 
        !            93: /*
        !            94:  * getmacro() loads the body of a macro definition into slist and returns
        !            95:  * a pointer to slist. If the macro definition continues on more than
        !            96:  * one line further lines are fetched from the input stream. Returns NO
        !            97:  * if out of memory, otherwise YES.
        !            98:  */
        !            99: getmacro(slist, stream)
        !           100:        register SLIST *slist;          /* singly-linked list */
        !           101:        register FILE *stream;          /* input stream */
        !           102: {
        !           103:        register char *bp;              /* buffer pointer */
        !           104:        char mdefbuf[MACRODEFSIZE];     /* macro definition buffer */
        !           105:        char *getlin();                 /* get a line from input stream */
        !           106:        char *gettoken();               /* get next token */
        !           107:        char *slappend();               /* append key */
        !           108: 
        !           109:        bp = Iobuf;
        !           110:        while (*bp++ != '=')
        !           111:                continue;
        !           112:        while ((bp = gettoken(mdefbuf, bp)) != NULL)
        !           113:                if (slappend(mdefbuf, slist) == NULL)
        !           114:                        return(NO);
        !           115:        while (Continue == YES)
        !           116:                {
        !           117:                if (getlin(stream) == NULL)
        !           118:                        break;
        !           119:                bp = Iobuf;
        !           120:                while ((bp = gettoken(mdefbuf, bp)) != NULL)
        !           121:                        if (slappend(mdefbuf, slist) == NULL)
        !           122:                                return(NO);
        !           123:                }
        !           124:        return(YES);
        !           125: }
        !           126: 
        !           127: 
        !           128: 
        !           129: /*
        !           130:  * gettoken() copies the next token from token buffer to token. Returns a
        !           131:  * pointer to the first character after the token, or null upon reaching
        !           132:  * the end of the token buffer.
        !           133:  */
        !           134: char *
        !           135: gettoken(token, tp)
        !           136:        register char *token;           /* receiving token */
        !           137:        register char *tp;              /* token buffer pointer */
        !           138: {
        !           139:        while (isspace(*tp) && *tp != '\0')
        !           140:                tp++;
        !           141:        if (*tp == '\0')
        !           142:                {
        !           143:                *token = '\0';
        !           144:                return(NULL);
        !           145:                }
        !           146:        while (!isspace(*tp) && *tp != '\0')
        !           147:                *token++ = *tp++;
        !           148:        *token = '\0';
        !           149:        return(tp);
        !           150: }
        !           151: 
        !           152: 
        !           153: 
        !           154: /*
        !           155:  * readmf() reads a makefile and loads file names from HEADERS and SOURCE
        !           156:  * macros into a singly-linked list. Returns NO on error, otherwise YES.
        !           157:  */
        !           158: readmf(mfname, filelist)
        !           159:        char *mfname;                   /* name of makefile */
        !           160:        SLIST *filelist;                /* file name list */
        !           161: {
        !           162:        char *findmacro();              /* is the line a macro definition? */
        !           163:        char *getlin();                 /* get a line from input stream */
        !           164:        char macroname[MACRONAMSIZE];   /* macro name buffer */
        !           165:        FILE *fopen();                  /* open file */
        !           166:        FILE *fp;                       /* file pointer */
        !           167:        int dependmark();               /* generated dependency line? */
        !           168:        int getmacro();                 /* get macro def from input stream */
        !           169:        SLIST *headlist;                /* header file name list */
        !           170:        SLIST *slinit();                /* initialize list */
        !           171:        SLIST *srclist;                 /* source file name list */
        !           172:        void slsplice();                /* splice lists together */
        !           173: 
        !           174:        headlist = slinit();
        !           175:        srclist = slinit();
        !           176: 
        !           177:        if ((fp = fopen(mfname, "r")) == NULL)
        !           178:                {
        !           179:                pperror(mfname);
        !           180:                return(NO);
        !           181:                }
        !           182:        while (getlin(fp) != NULL)
        !           183:                {
        !           184:                if (dependmark())
        !           185:                        break;
        !           186:                if (findmacro(macroname) != NULL)
        !           187:                        {
        !           188:                        if (EQUAL(macroname, MHEADERS))
        !           189:                                {
        !           190:                                if (getmacro(headlist, fp) == NO)
        !           191:                                        return(NO);
        !           192:                                }
        !           193:                        else if (EQUAL(macroname, MSOURCE))
        !           194:                                {
        !           195:                                if (getmacro(srclist, fp) == NO)
        !           196:                                        return(NO);
        !           197:                                }
        !           198:                        }
        !           199:                }
        !           200:        fclose(fp);
        !           201: 
        !           202:        /* splice header files to file list */
        !           203:        slsplice(filelist, headlist);
        !           204: 
        !           205:        /* splice source files to file list */
        !           206:        slsplice(filelist, srclist);
        !           207: 
        !           208:        return(YES);
        !           209: }

unix.superglobalmegacorp.com

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