[BACK]Return to fsplit.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / fsplit

Annotation of src/usr.bin/fsplit/fsplit.c, Revision 1.4

1.4     ! deraadt     1: /*     $OpenBSD: fsplit.c,v 1.3 1998/07/10 21:44:51 mickey Exp $       */
1.2       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1983, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Asa Romberger and Jerry Berkman.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: #ifndef lint
                     40: static char copyright[] =
                     41: "@(#) Copyright (c) 1983, 1993\n\
                     42:        The Regents of the University of California.  All rights reserved.\n";
                     43: #endif /* not lint */
                     44:
                     45: #ifndef lint
                     46: /*static char sccsid[] = "from: @(#)fsplit.c   8.1 (Berkeley) 6/6/93";*/
1.4     ! deraadt    47: static char rcsid[] = "$OpenBSD: fsplit.c,v 1.3 1998/07/10 21:44:51 mickey Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: #include <ctype.h>
                     51: #include <stdio.h>
1.4     ! deraadt    52: #include <unistd.h>
1.1       deraadt    53: #include <string.h>
                     54: #include <sys/types.h>
                     55: #include <sys/stat.h>
1.3       mickey     56: #include <err.h>
1.1       deraadt    57:
1.4     ! deraadt    58: void   badparms __P(());
        !            59: void   get_name __P((char *, int));
        !            60: int    lname __P((char *));
        !            61: int    getline __P((void));
        !            62: int    lend __P((void));
        !            63: int    scan_name __P((char *, char *));
        !            64: int    saveit __P((char *));
        !            65:
1.1       deraadt    66: /*
                     67:  *     usage:          fsplit [-e efile] ... [file]
                     68:  *
                     69:  *     split single file containing source for several fortran programs
                     70:  *             and/or subprograms into files each containing one
                     71:  *             subprogram unit.
                     72:  *     each separate file will be named using the corresponding subroutine,
                     73:  *             function, block data or program name if one is found; otherwise
                     74:  *             the name will be of the form mainNNN.f or blkdtaNNN.f .
                     75:  *             If a file of that name exists, it is saved in a name of the
                     76:  *             form zzz000.f .
                     77:  *     If -e option is used, then only those subprograms named in the -e
                     78:  *             option are split off; e.g.:
                     79:  *                     fsplit -esub1 -e sub2 prog.f
                     80:  *             isolates sub1 and sub2 in sub1.f and sub2.f.  The space
                     81:  *             after -e is optional.
                     82:  *
                     83:  *     Modified Feb., 1983 by Jerry Berkman, Computing Services, U.C. Berkeley.
                     84:  *             - added comments
                     85:  *             - more function types: double complex, character*(*), etc.
                     86:  *             - fixed minor bugs
                     87:  *             - instead of all unnamed going into zNNN.f, put mains in
                     88:  *               mainNNN.f, block datas in blkdtaNNN.f, dups in zzzNNN.f .
                     89:  */
                     90:
                     91: #define BSZ 512
                     92: char buf[BSZ];
                     93: FILE *ifp;
                     94: char   x[]="zzz000.f",
                     95:        mainp[]="main000.f",
                     96:        blkp[]="blkdta000.f";
                     97: char *look(), *skiplab(), *functs();
                     98:
                     99: #define TRUE 1
                    100: #define FALSE 0
                    101: int    extr = FALSE,
                    102:        extrknt = -1,
                    103:        extrfnd[100];
                    104: char   extrbuf[1000],
                    105:        *extrnames[100];
                    106: struct stat sbuf;
                    107:
                    108: #define trim(p)        while (*p == ' ' || *p == '\t') p++
                    109:
1.3       mickey    110: int
1.1       deraadt   111: main(argc, argv)
                    112: char **argv;
                    113: {
                    114:        register FILE *ofp;     /* output file */
1.4     ! deraadt   115:        register int rv;        /* 1 if got card in output file, 0 otherwise */
1.1       deraadt   116:        register char *ptr;
                    117:        int nflag,              /* 1 if got name of subprog., 0 otherwise */
                    118:                retval,
                    119:                i;
                    120:        char name[20],
                    121:                *extrptr = extrbuf;
                    122:
                    123:        /*  scan -e options */
                    124:        while ( argc > 1  && argv[1][0] == '-' && argv[1][1] == 'e') {
                    125:                extr = TRUE;
                    126:                ptr = argv[1] + 2;
                    127:                if(!*ptr) {
                    128:                        argc--;
                    129:                        argv++;
                    130:                        if(argc <= 1) badparms();
                    131:                        ptr = argv[1];
                    132:                }
                    133:                extrknt = extrknt + 1;
                    134:                extrnames[extrknt] = extrptr;
                    135:                extrfnd[extrknt] = FALSE;
                    136:                while(*ptr) *extrptr++ = *ptr++;
                    137:                *extrptr++ = 0;
                    138:                argc--;
                    139:                argv++;
                    140:        }
                    141:
                    142:        if (argc > 2)
                    143:                badparms();
                    144:        else if (argc == 2) {
1.3       mickey    145:                if ((ifp = fopen(argv[1], "r")) == NULL)
                    146:                        err(1, argv[1]);
1.1       deraadt   147:        }
                    148:        else
                    149:                ifp = stdin;
                    150:     for(;;) {
                    151:        /* look for a temp file that doesn't correspond to an existing file */
                    152:        get_name(x, 3);
                    153:        ofp = fopen(x, "w");
                    154:        nflag = 0;
                    155:        rv = 0;
                    156:        while (getline() > 0) {
                    157:                rv = 1;
                    158:                fprintf(ofp, "%s", buf);
                    159:                if (lend())             /* look for an 'end' statement */
                    160:                        break;
                    161:                if (nflag == 0)         /* if no name yet, try and find one */
                    162:                        nflag = lname(name);
                    163:        }
                    164:        fclose(ofp);
                    165:        if (rv == 0) {                  /* no lines in file, forget the file */
                    166:                unlink(x);
                    167:                retval = 0;
                    168:                for ( i = 0; i <= extrknt; i++ )
                    169:                        if(!extrfnd[i]) {
                    170:                                retval = 1;
1.3       mickey    171:                                warnx("%s not found", extrnames[i]);
1.1       deraadt   172:                        }
                    173:                exit( retval );
                    174:        }
                    175:        if (nflag) {                    /* rename the file */
                    176:                if(saveit(name)) {
                    177:                        if (stat(name, &sbuf) < 0 ) {
                    178:                                link(x, name);
                    179:                                unlink(x);
                    180:                                printf("%s\n", name);
                    181:                                continue;
                    182:                        } else if (strcmp(name, x) == 0) {
                    183:                                printf("%s\n", x);
                    184:                                continue;
                    185:                        }
                    186:                        printf("%s already exists, put in %s\n", name, x);
                    187:                        continue;
                    188:                } else
                    189:                        unlink(x);
                    190:                        continue;
                    191:        }
                    192:        if(!extr)
                    193:                printf("%s\n", x);
                    194:        else
                    195:                unlink(x);
                    196:     }
                    197: }
                    198:
1.4     ! deraadt   199: void
1.1       deraadt   200: badparms()
                    201: {
1.3       mickey    202:        err(1, "usage:  fsplit [-e efile] ... [file]");
1.1       deraadt   203: }
                    204:
1.4     ! deraadt   205: int
1.1       deraadt   206: saveit(name)
                    207: char *name;
                    208: {
                    209:        int i;
                    210:        char    fname[50],
                    211:                *fptr = fname;
                    212:
                    213:        if(!extr) return(1);
                    214:        while(*name) *fptr++ = *name++;
                    215:        *--fptr = 0;
                    216:        *--fptr = 0;
                    217:        for ( i=0 ; i<=extrknt; i++ )
                    218:                if( strcmp(fname, extrnames[i]) == 0 ) {
                    219:                        extrfnd[i] = TRUE;
                    220:                        return(1);
                    221:                }
                    222:        return(0);
                    223: }
                    224:
1.4     ! deraadt   225: void
1.1       deraadt   226: get_name(name, letters)
                    227: char *name;
                    228: int letters;
                    229: {
                    230:        register char *ptr;
                    231:
                    232:        while (stat(name, &sbuf) >= 0) {
                    233:                for (ptr = name + letters + 2; ptr >= name + letters; ptr--) {
                    234:                        (*ptr)++;
                    235:                        if (*ptr <= '9')
                    236:                                break;
                    237:                        *ptr = '0';
                    238:                }
1.3       mickey    239:                if(ptr < name + letters)
                    240:                        errx(1, "ran out of file names");
1.1       deraadt   241:        }
                    242: }
                    243:
1.4     ! deraadt   244: int
1.1       deraadt   245: getline()
                    246: {
                    247:        register char *ptr;
                    248:
                    249:        for (ptr = buf; ptr < &buf[BSZ]; ) {
                    250:                *ptr = getc(ifp);
                    251:                if (feof(ifp))
                    252:                        return (-1);
                    253:                if (*ptr++ == '\n') {
                    254:                        *ptr = 0;
                    255:                        return (1);
                    256:                }
                    257:        }
                    258:        while (getc(ifp) != '\n' && feof(ifp) == 0) ;
1.3       mickey    259:        warnx("line truncated to %d characters", BSZ);
1.1       deraadt   260:        return (1);
                    261: }
                    262:
                    263: /* return 1 for 'end' alone on card (up to col. 72),  0 otherwise */
1.4     ! deraadt   264: int
1.1       deraadt   265: lend()
                    266: {
                    267:        register char *p;
                    268:
                    269:        if ((p = skiplab(buf)) == 0)
                    270:                return (0);
                    271:        trim(p);
                    272:        if (*p != 'e' && *p != 'E') return(0);
                    273:        p++;
                    274:        trim(p);
                    275:        if (*p != 'n' && *p != 'N') return(0);
                    276:        p++;
                    277:        trim(p);
                    278:        if (*p != 'd' && *p != 'D') return(0);
                    279:        p++;
                    280:        trim(p);
                    281:        if (p - buf >= 72 || *p == '\n')
                    282:                return (1);
                    283:        return (0);
                    284: }
                    285:
                    286: /*             check for keywords for subprograms
                    287:                return 0 if comment card, 1 if found
                    288:                name and put in arg string. invent name for unnamed
                    289:                block datas and main programs.          */
1.4     ! deraadt   290: int
1.1       deraadt   291: lname(s)
                    292: char *s;
                    293: {
                    294: #      define LINESIZE 80
1.4     ! deraadt   295:        register char *ptr, *p;
1.1       deraadt   296:        char    line[LINESIZE], *iptr = line;
                    297:
                    298:        /* first check for comment cards */
                    299:        if(buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*') return(0);
                    300:        ptr = buf;
                    301:        while (*ptr == ' ' || *ptr == '\t') ptr++;
                    302:        if(*ptr == '\n') return(0);
                    303:
                    304:
                    305:        ptr = skiplab(buf);
                    306:        if (ptr == 0)
                    307:                return (0);
                    308:
                    309:
                    310:        /*  copy to buffer and converting to lower case */
                    311:        p = ptr;
                    312:        while (*p && p <= &buf[71] ) {
                    313:           *iptr = isupper(*p) ? tolower(*p) : *p;
                    314:           iptr++;
                    315:           p++;
                    316:        }
                    317:        *iptr = '\n';
                    318:
                    319:        if ((ptr = look(line, "subroutine")) != 0 ||
                    320:            (ptr = look(line, "function")) != 0 ||
                    321:            (ptr = functs(line)) != 0) {
                    322:                if(scan_name(s, ptr)) return(1);
                    323:                strcpy( s, x);
                    324:        } else if((ptr = look(line, "program")) != 0) {
                    325:                if(scan_name(s, ptr)) return(1);
                    326:                get_name( mainp, 4);
                    327:                strcpy( s, mainp);
                    328:        } else if((ptr = look(line, "blockdata")) != 0) {
                    329:                if(scan_name(s, ptr)) return(1);
                    330:                get_name( blkp, 6);
                    331:                strcpy( s, blkp);
                    332:        } else if((ptr = functs(line)) != 0) {
                    333:                if(scan_name(s, ptr)) return(1);
                    334:                strcpy( s, x);
                    335:        } else {
                    336:                get_name( mainp, 4);
                    337:                strcpy( s, mainp);
                    338:        }
                    339:        return(1);
                    340: }
                    341:
1.4     ! deraadt   342: int
1.1       deraadt   343: scan_name(s, ptr)
                    344: char *s, *ptr;
                    345: {
                    346:        char *sptr;
                    347:
                    348:        /* scan off the name */
                    349:        trim(ptr);
                    350:        sptr = s;
                    351:        while (*ptr != '(' && *ptr != '\n') {
                    352:                if (*ptr != ' ' && *ptr != '\t')
                    353:                        *sptr++ = *ptr;
                    354:                ptr++;
                    355:        }
                    356:
                    357:        if (sptr == s) return(0);
                    358:
                    359:        *sptr++ = '.';
                    360:        *sptr++ = 'f';
                    361:        *sptr++ = 0;
                    362:        return(1);
                    363: }
                    364:
                    365: char *functs(p)
                    366: char *p;
                    367: {
                    368:         register char *ptr;
                    369:
                    370: /*      look for typed functions such as: real*8 function,
                    371:                 character*16 function, character*(*) function  */
                    372:
                    373:         if((ptr = look(p,"character")) != 0 ||
                    374:            (ptr = look(p,"logical")) != 0 ||
                    375:            (ptr = look(p,"real")) != 0 ||
                    376:            (ptr = look(p,"integer")) != 0 ||
                    377:            (ptr = look(p,"doubleprecision")) != 0 ||
                    378:            (ptr = look(p,"complex")) != 0 ||
                    379:            (ptr = look(p,"doublecomplex")) != 0 ) {
                    380:                 while ( *ptr == ' ' || *ptr == '\t' || *ptr == '*'
                    381:                        || (*ptr >= '0' && *ptr <= '9')
                    382:                        || *ptr == '(' || *ptr == ')') ptr++;
                    383:                ptr = look(ptr,"function");
                    384:                return(ptr);
                    385:        }
                    386:         else
                    387:                 return(0);
                    388: }
                    389:
                    390: /*     if first 6 col. blank, return ptr to col. 7,
                    391:        if blanks and then tab, return ptr after tab,
                    392:        else return 0 (labelled statement, comment or continuation */
                    393: char *skiplab(p)
                    394: char *p;
                    395: {
                    396:        register char *ptr;
                    397:
                    398:        for (ptr = p; ptr < &p[6]; ptr++) {
                    399:                if (*ptr == ' ')
                    400:                        continue;
                    401:                if (*ptr == '\t') {
                    402:                        ptr++;
                    403:                        break;
                    404:                }
                    405:                return (0);
                    406:        }
                    407:        return (ptr);
                    408: }
                    409:
                    410: /*     return 0 if m doesn't match initial part of s;
                    411:        otherwise return ptr to next char after m in s */
                    412: char *look(s, m)
                    413: char *s, *m;
                    414: {
                    415:        register char *sp, *mp;
                    416:
                    417:        sp = s; mp = m;
                    418:        while (*mp) {
                    419:                trim(sp);
                    420:                if (*sp++ != *mp++)
                    421:                        return (0);
                    422:        }
                    423:        return (sp);
                    424: }