Annotation of cci/usr/src/bin/cdb/files.c, revision 1.1

1.1     ! root        1: static char sccsid[] = "@(#)files.c    2.7";
        !             2: 
        !             3: #include <stdio.h>
        !             4: #include "cdb.h"
        !             5: #include "macdefs.h"
        !             6: 
        !             7: 
        !             8: FILE   *vfpSrc;        /* source file of current interest */
        !             9: export int     vfnDecl;        /* declarations file of current interest */
        !            10: 
        !            11: export FLAGT   vfPascal;       /* if true, vifd is a pascal source file */
        !            12: export int     vcbTot; /* used for debugging - # bytes allocated at present */
        !            13: 
        !            14: #define idirMax        16      /* maximum number of alternate directories */
        !            15: local char     *vrgSbDir[idirMax];     /* a list of directory names */
        !            16: 
        !            17: local short    vidirMac;
        !            18: 
        !            19: 
        !            20: 
        !            21: /* A D D   D I R */
        !            22: 
        !            23: export void AddDir(sbDir)
        !            24: char   *sbDir;
        !            25: {
        !            26:        if (vidirMac >= idirMax)
        !            27:                UError("No more room for directories");
        !            28:        vrgSbDir[vidirMac] = malloc(strlen(sbDir)+1);
        !            29:        strcpy(vrgSbDir[vidirMac], sbDir);
        !            30:        vidirMac++;
        !            31: } /* AddDir */
        !            32: 
        !            33: 
        !            34: /* L I S T   D I R */
        !            35: 
        !            36: export void ListDir()
        !            37: {
        !            38:        short   i;
        !            39:        for (i=0; i < vidirMac; i++)
        !            40:                printf("%s\n", vrgSbDir[i]);
        !            41: } /* ListDir */
        !            42: 
        !            43: 
        !            44: /* F P   F   S B */
        !            45: 
        !            46: local FILE *FpFSb(sbFile, sbFlags)
        !            47: char   *sbFile, *sbFlags;
        !            48: {
        !            49:        short   i;
        !            50:        char    sbTemp[80];
        !            51:        FILE    *fp;
        !            52: 
        !            53:        fp = NULL;
        !            54:        sbTemp[0] = 0;
        !            55:        if ((sbFile[0] != '/') AND (vidirMac > 0)) {
        !            56:                for (i=0; i < vidirMac; i++) {
        !            57:                        strcpy(sbTemp, vrgSbDir[i]);
        !            58:                        strcat(sbTemp, "/");
        !            59:                        strcat(sbTemp, sbFile);
        !            60:                        /* try to open it */
        !            61:                        fp = fopen(sbTemp, sbFlags);
        !            62:                        if (fp != NULL)
        !            63:                                break;
        !            64:                } /* for */
        !            65:        } /* if */
        !            66: 
        !            67:        if (fp == NULL) {
        !            68:                /* failed elsewhere, exist locally? */
        !            69:                strcpy(sbTemp, sbFile);
        !            70:                fp = fopen(sbTemp, sbFlags);
        !            71:        }
        !            72:        if (fp != NULL)
        !            73:                strcpy(sbFile, sbTemp);
        !            74:        return(fp);
        !            75: } /* FpFSb */
        !            76: 
        !            77: 
        !            78: /* S E T   R G   L N */
        !            79: 
        !            80: local void SetRgLn(ifdIn, fp)
        !            81: int    ifdIn;
        !            82: FILE   *fp;
        !            83: {
        !            84:        int             cbNew;
        !            85:        uint    *rgLn;
        !            86:        char    buf[200];
        !            87:        int             ifd, iln, ilnMac;
        !            88: 
        !            89:        iln = 0;
        !            90:        rewind(fp);
        !            91:        while (fgets(buf, 200, fp) != NULL)
        !            92:                iln++;
        !            93:        iln++;  /* this array is 1 based, not zero */
        !            94:        cbNew = iln * CBINT;
        !            95:        rgLn = (uint *) malloc(cbNew);  /* get enough space for line array */
        !            96:        if (rgLn == 0) {
        !            97:                for (ifd=0; ifd<vifdMac; ifd++) {
        !            98:                        if (vrgFd[ifd].ilnMac != ilnNil) {
        !            99:                                free(vrgFd[ifd].rgLn);
        !           100:                                vcbTot -= vrgFd[ifd].ilnMac * 2;
        !           101:                                vrgFd[ifd].ilnMac = ilnNil;
        !           102:                                rgLn = (uint *) malloc(cbNew);  /* try again */
        !           103:                                if (rgLn != 0)
        !           104:                                        break;
        !           105:                        } /* if */
        !           106:                } /* for */
        !           107:                if (ifd >= vifdMac)
        !           108:                        Panic("Memory problems in SetRgLn");
        !           109:        } /* if */
        !           110:        vcbTot += cbNew;
        !           111:        vrgFd[ifdIn].ilnMac = iln;
        !           112:        vrgFd[ifdIn].rgLn = rgLn;
        !           113:        rewind(fp);     /* once more, from the top */
        !           114:        ilnMac = iln;
        !           115:        for (iln=1; iln < ilnMac; iln++) {
        !           116:                rgLn[iln] = ftell(fp);
        !           117:                if (fgets(buf, 200, fp) == NULL)
        !           118:                        break;
        !           119:        } /* for */
        !           120: } /* SetRgLn */
        !           121: 
        !           122: 
        !           123: /* I P D   F   A D R */
        !           124: 
        !           125: export int IpdFAdr(adr)
        !           126: ADRT   adr;
        !           127: {
        !           128:        int     ipd;
        !           129: 
        !           130:        if (vrgPd[vipd].adrStart <= adr
        !           131:            AND vrgPd[vipd+1].adrStart > adr)
        !           132:                return(vipd);
        !           133: 
        !           134:        for (ipd=0; ipd < vipdMac; ipd++) {
        !           135:                if (vrgPd[ipd].adrStart >= adr) {
        !           136:                        if (vrgPd[ipd].adrStart > adr)
        !           137:                                ipd--;
        !           138:                        return(ipd);
        !           139:                } /* if */
        !           140:        } /* for */
        !           141:        return(ipdNil);
        !           142: } /* IpdFAdr */
        !           143: 
        !           144: 
        !           145: /* I P D   F   N A M E */
        !           146: 
        !           147: export int IpdFName(sbProc)
        !           148: char   *sbProc;
        !           149: {
        !           150:        int             ipd;
        !           151: 
        !           152:        if ((vipd != ipdNil)
        !           153:            AND (FSbCmp(vrgPd[vipd].sbProc, sbProc)) )
        !           154:                return(vipd);
        !           155: 
        !           156:        for (ipd=0; ipd < vipdMac; ipd++)       /* try for an EXACT match first */
        !           157:                if (FSbCmp(vrgPd[ipd].sbProc, sbProc))
        !           158:                        return(ipd);
        !           159: 
        !           160:        for (ipd=0; ipd < vipdMac; ipd++)       /* no good, go for approximate */
        !           161:                if (FProcCmp(vrgPd[ipd].sbProc, sbProc))
        !           162:                        return(ipd);
        !           163: 
        !           164:        return(ipdNil);
        !           165: } /* IpdFName */
        !           166: 
        !           167: 
        !           168: /* O P E N   I P D */
        !           169: 
        !           170: export void OpenIpd(ipd, fForce)
        !           171: int    ipd;
        !           172: FLAGT  fForce;
        !           173: {
        !           174:        if (ipd == ipdNil) {
        !           175:                vipd = ipdNil;
        !           176:                return;
        !           177:        } /* if */
        !           178: 
        !           179:        if (!fForce & (ipd == vipd))
        !           180:                return;
        !           181: 
        !           182:        IfdFOpen(IfdFAdr(vrgPd[ipd].adrStart)); /* open parent file */
        !           183:        viln = IlnFIsym(vrgPd[ipd].isym+1);     /* find line #, start with next sym */
        !           184:        if (viln == ilnNil) {
        !           185:                printf("WARNING: %s does not appear to have line symbols\n",
        !           186:                vrgFd[vifd].sbFile);
        !           187: #ifdef BSD41
        !           188:        } 
        !           189:        else {
        !           190:                if (!vfPascal) {
        !           191:                        visym++;        /* we actually want the NEXT one for berkeley C */
        !           192:                        viln = IlnFIsym(visym);
        !           193:                }
        !           194: #endif
        !           195:        } /* if */
        !           196:        vipd = ipd;
        !           197:        vslop = 0;
        !           198: } /* OpenIpd */
        !           199: 
        !           200: 
        !           201: /* I F D   F   O P E N */
        !           202: 
        !           203: export int IfdFOpen(ifd)
        !           204: int    ifd;
        !           205: {
        !           206:        char    sbTemp[80], *sbT;
        !           207:        long    mtimeSrc;
        !           208:        pFDR    fd;
        !           209:        FILE    *fp;
        !           210: 
        !           211:        /* we use vifdTemp for `un-related' files */
        !           212:        if ( (ifd == vifdTemp) AND (vrgFd[vifdTemp].ilnMac != ilnNil) ) {
        !           213:                /* ie. there was already a file connected with vifdTemp */
        !           214:                free(vrgFd[vifdTemp].rgLn);
        !           215:                vcbTot -= vrgFd[vifdTemp].ilnMac * 2;
        !           216:                vrgFd[vifdTemp].ilnMac = ilnNil;
        !           217:                vifd = vifdNil;
        !           218:        } /* if */
        !           219: 
        !           220:        if (ifd < 0 OR ifd >= vifdMac) {
        !           221:                vifd = vifdNil;
        !           222:                return(vifdNil);
        !           223:        } /* if */
        !           224: 
        !           225:        if (ifd == vifd)
        !           226:                return(ifd);
        !           227: 
        !           228:        /* let's get rid of currently open stuff */
        !           229:        if (vfpSrc != NULL) {
        !           230:                fclose(vfpSrc); /* there is a currently open file */
        !           231:                vfpSrc = NULL;
        !           232:        } /* if */
        !           233:        vifd = vifdNil;
        !           234: #ifdef SYSIII
        !           235:        if (vfnDecl != fnNil) {
        !           236:                close(vfnDecl);
        !           237:                vfnDecl = fnNil;
        !           238:                vityMac = 0;
        !           239:        } /* if */
        !           240: #endif
        !           241: 
        !           242:        fd = vrgFd + ifd;
        !           243:        strcpy(sbTemp, (ifd==vifdTemp) ? vsbFileTemp : fd->sbFile);
        !           244:        fp = FpFSb(sbTemp, "r");
        !           245:        if (fp == NULL) {
        !           246:                if (ifd != vifdTemp)
        !           247:                        UError("Cannot open `%s'\n", sbTemp);
        !           248:                else
        !           249:                        return(vifdNil);
        !           250:        } /* if */
        !           251:        vfpSrc = fp;
        !           252:        vipd = vrgFd[ifd].ipd;
        !           253:        viln = 1;
        !           254:        vslop = 0;
        !           255:        vifd = ifd;
        !           256: 
        !           257:        if (fd->ilnMac == ilnNil) {
        !           258:                /* we need to see its age and build line array */
        !           259:                mtimeSrc = AgeFFn(fileno(vfpSrc));
        !           260:                if (!fd->fWarned AND mtimeSrc > vmtimeSym)
        !           261:                        printf("WARNING: %s is younger than %s\n", sbTemp, vsbSymfile);
        !           262:                SetRgLn(ifd, vfpSrc);   /* figure out line {nings */
        !           263:        } /* if */
        !           264: 
        !           265: #ifdef BSD41
        !           266:        sbT = strrchr(sbTemp, '.') + 1;
        !           267:        if (*sbT == 'p') {
        !           268:                /* it would appear to be a pascal source file */
        !           269:                vfPascal = true;
        !           270:        } 
        !           271:        else {
        !           272:                vfPascal = false;
        !           273:        }
        !           274: #endif
        !           275: #ifdef SYSIII
        !           276:        if (fd->fHasDecl) {     /* see if we can find a symbols file */
        !           277:                sbT = strrchr(sbTemp, 'c');
        !           278:                *sbT = 'd';
        !           279:                if ((vfnDecl = open(sbTemp, O_RDONLY)) < 0) {
        !           280:                        vfnDecl = fnNil;
        !           281:                        fd->fHasDecl = false;
        !           282:                } 
        !           283:                else {
        !           284:                        if (!fd->fWarned AND mtimeSrc > AgeFFn(vfnDecl)) {
        !           285:                                *sbT = chNull;
        !           286:                                printf("WARNING: %sc is younger than %sd\n", sbTemp, sbTemp);
        !           287:                        } /* if */
        !           288:                        vityMac = CblFFn(vfnDecl) / cbTYR;
        !           289:                        InitTyCache();
        !           290:                } /* if */
        !           291:        } /* if */
        !           292: #endif
        !           293:        fd->fWarned = true;
        !           294:        return(ifd);
        !           295: } /* IfdFOpen */
        !           296: 
        !           297: 
        !           298: /* I F D   F   A D R */
        !           299: 
        !           300: export int IfdFAdr(adr)
        !           301: ADRT   adr;
        !           302: {
        !           303:        int             ifd;
        !           304: 
        !           305:        if ((vifd != vifdNil)
        !           306:            AND (vrgFd[vifd].adrStart <= adr)
        !           307:            AND (vrgFd[vifd+1].adrStart > adr))
        !           308:                return(vifd);
        !           309: 
        !           310:        for (ifd=0; ifd < vifdMac; ifd++) {
        !           311:                if (vrgFd[ifd].adrStart >= adr) {
        !           312:                        if (vrgFd[ifd].adrStart > adr)
        !           313:                                ifd--;
        !           314:                        return(ifd);
        !           315:                } /* if */
        !           316:        } /* for */
        !           317:        return(vifdNil);
        !           318: } /* IfdFAdr */
        !           319: 
        !           320: 
        !           321: /* I F D   F   N A M E */
        !           322: 
        !           323: export int IfdFName(sbFile)
        !           324: char   *sbFile;
        !           325: {
        !           326:        int     ifd;
        !           327: 
        !           328:        if ((vifd != vifdNil)
        !           329:            AND FSbCmp(vrgFd[vifd].sbFile, sbFile))
        !           330:                return(vifd);
        !           331: 
        !           332:        for (ifd=0; ifd < vifdMac; ifd++) {
        !           333:                if (FSbCmp(vrgFd[ifd].sbFile, sbFile)) {
        !           334:                        return(ifd);
        !           335:                } /* if */
        !           336:        } /* for */
        !           337:        return(vifdNil);
        !           338: } /* IfdFName */
        !           339: 
        !           340: 
        !           341: /* I F D   F   I P D */
        !           342: 
        !           343: export int IfdFIpd(ipd)
        !           344: int    ipd;
        !           345: {
        !           346:        int             ifd;
        !           347:        pFDR    fd;
        !           348: 
        !           349:        for (fd=vrgFd, ifd=0; ifd <= vifdMac; fd++, ifd++)
        !           350:                if (fd->ipd < ipd)
        !           351:                        return(ifd);
        !           352:        return(vifdNil);
        !           353: } /* IfdFIpd */
        !           354: 
        !           355: 
        !           356: /* P R I N T   L I N E */
        !           357: 
        !           358: export void PrintLine(iln, count, fPrintLn)
        !           359: int    iln, count;
        !           360: FLAGT  fPrintLn;
        !           361: {
        !           362:        long    offset;
        !           363:        char    linebuf[200];
        !           364: 
        !           365:        if (vifd == vifdNil)
        !           366:                UError("I have no source file for this address");
        !           367:        if (iln == ilnNil)
        !           368:                iln = 1;
        !           369:        iln = Min(iln, vrgFd[vifd].ilnMac);
        !           370:        count = Min(vrgFd[vifd].ilnMac-iln, count);
        !           371:        offset = vrgFd[vifd].rgLn[iln];
        !           372:        if (fseek(vfpSrc, offset, 0)) {
        !           373:                Panic("Bad seek in PrintLine");
        !           374:        } /* if */
        !           375:        while (count--) {
        !           376:                if (fgets(linebuf, 200, vfpSrc) == NULL)
        !           377:                        Panic("Bad read in PrintLine");
        !           378:                if (fPrintLn)
        !           379:                        printf("%3d:", iln);
        !           380:                printf("        %.72s", linebuf);
        !           381:                iln++;
        !           382:        } /* while */
        !           383:        viln = iln - 1; /* set to last line printed */
        !           384: } /* PrintLine */
        !           385: 
        !           386: 
        !           387: /* I   F   S B   S B */
        !           388: 
        !           389: export int IFSbSb(sbLine, sbTarget)
        !           390: char   *sbLine, *sbTarget;
        !           391: {
        !           392:        int     cbLine, cbTarget, i;
        !           393: 
        !           394:        i = 0;  /* the index of sbTarget in sbLine */
        !           395:        cbLine = strlen(sbLine);
        !           396:        cbTarget = strlen(sbTarget);
        !           397:        while (cbLine >= cbTarget) {
        !           398:                if (FHdrCmp(sbTarget, sbLine))
        !           399:                        return(i);
        !           400:                i++;
        !           401:                sbLine++;
        !           402:                cbLine--;
        !           403:        } /* while */
        !           404:        return(-1);
        !           405: } /* IFSbSb */
        !           406: 
        !           407: 
        !           408: /* F I N D */
        !           409: 
        !           410: export void Find(target, fBack)
        !           411: char   *target;
        !           412: FLAGT  fBack;
        !           413: {
        !           414:        int             lineinc, count, iln, ilnStart, ilnMac;
        !           415:        uint    *rgLn;
        !           416:        char    linebuf[200];
        !           417: 
        !           418:        iln = viln;
        !           419:        ilnMac = vrgFd[vifd].ilnMac;
        !           420:        rgLn = vrgFd[vifd].rgLn;
        !           421:        if (fBack) {
        !           422:                lineinc = -1;
        !           423:                iln--;  /* for backup */
        !           424:                count = iln;
        !           425:        } 
        !           426:        else {
        !           427:                lineinc = 1;
        !           428:                iln++;  /* start at next line */
        !           429:                count = ilnMac - iln;
        !           430:        } /* if */
        !           431:        ilnStart = iln;
        !           432: 
        !           433:        if (fseek(vfpSrc, lengthen(rgLn[iln]), 0))
        !           434:                Panic("Bad seek in Find - offset %d",rgLn[iln]);
        !           435:        while (count--) {
        !           436:                if (fBack)
        !           437:                        if (fseek(vfpSrc, lengthen(rgLn[iln]), 0))
        !           438:                                Panic("Bad seek in Find - offset %d",rgLn[iln]);
        !           439:                if (fgets(linebuf, 200, vfpSrc) == NULL)
        !           440:                        Panic("Bad read in Find");
        !           441:                if (IFSbSb(linebuf, target) >= 0)
        !           442:                        goto winner;
        !           443:                iln += lineinc;
        !           444:        } /* while */
        !           445: 
        !           446:        /* that didn't work, try it from the top of file */
        !           447:        if (fBack) {
        !           448:                iln = ilnMac - 1;
        !           449:                count = ilnMac - ilnStart - 1;
        !           450:        } 
        !           451:        else {
        !           452:                iln = 1;
        !           453:                count = ilnStart;
        !           454:        } /* if */
        !           455:        if (fseek(vfpSrc, lengthen(rgLn[iln]), 0))
        !           456:                Panic("Bad seek in Find");
        !           457:        while (count--) {
        !           458:                if (fBack)
        !           459:                        if (fseek(vfpSrc, lengthen(rgLn[iln]), 0))
        !           460:                                Panic("Bad seek in Find");
        !           461:                if (fgets(linebuf, 200, vfpSrc) == NULL)
        !           462:                        Panic("Bad read in Find");
        !           463:                if (IFSbSb(linebuf, target) >= 0)
        !           464:                        goto winner;
        !           465:                iln += lineinc;
        !           466:        } /* while */
        !           467:        UError("No Match - %s", target);
        !           468: 
        !           469: winner:
        !           470:        PrintLine(iln, 1, true);
        !           471: } /* Find */
        !           472: 
        !           473: 
        !           474: /* P R I N T   P O S */
        !           475: 
        !           476: export void PrintPos(adr, fmt)
        !           477: ADRT   adr;
        !           478: int    fmt;
        !           479: {
        !           480:        int     ifd, iln, slop, ifdSave, ipdSave, ilnSave, slopSave;
        !           481: 
        !           482:        if (fmt & fmtSave) {
        !           483:                ifdSave = vifd;
        !           484:                ipdSave = vipd;
        !           485:                ilnSave = viln;
        !           486:                slopSave = vslop;
        !           487:        } /* if */
        !           488: 
        !           489:        if (adr != adrNil) {
        !           490:                IfdLnFAdr(adr, isym0, &ifd, &iln, &slop);
        !           491:                if (ifd != vifdNil) {
        !           492:                        IfdFOpen(ifd);
        !           493:                        OpenIpd(IpdFAdr(adr), false);
        !           494:                        vslop = slop;
        !           495:                        viln = iln;
        !           496:                } 
        !           497:                else {
        !           498:                        vifd = vifdNil;
        !           499:                } /* if */
        !           500:                if (fmt == fmtNil)
        !           501:                        return;
        !           502:        } /* if */
        !           503: 
        !           504:        if (vifd == vifdNil) {
        !           505:                LabelFAdr(adr); /* sets vsymCur to nearest label */
        !           506:                if (fmt & fmtFile)
        !           507:                        printf("file unknown:");
        !           508:                printf("**%s",SbInCore(vsymCur->sbSym));
        !           509:                if (vsymCur->value != adr)
        !           510:                        printf("+%s",SbFAdr(lengthen(adr-vsymCur->value), true));
        !           511:                putchar('\n');  
        !           512:                goto cleanup;
        !           513:        } /* if */
        !           514: 
        !           515:        if (fmt & fmtFile) {
        !           516:                printf("%s:", vrgFd[vifd].sbFile);
        !           517:        } /* if */
        !           518: 
        !           519:        if (fmt & fmtProc) {
        !           520:                if (vipd != ipdNil) {
        !           521:                        printf("%s:",vrgPd[vipd].sbProc);
        !           522:                } 
        !           523:                else {
        !           524:                        printf("proc??:");
        !           525:                } /* if */
        !           526:        } /* if */
        !           527: 
        !           528:        if (fmt & fmtLn) {
        !           529:                printf("%d", viln);
        !           530:                if (vslop != 0)
        !           531:                        printf("+%s",SbFAdr(lengthen(vslop), true));
        !           532:                putchar(':');
        !           533:        } /* if */
        !           534: 
        !           535:        if (fmt & fmtPrint)
        !           536:                PrintLine(viln, 1, false);
        !           537:        else {
        !           538:                printf(" ");  /* takes out last `:' */
        !           539:                if (fmt & fmtEol)
        !           540:                        putchar('\n');
        !           541:        } /* if */
        !           542: 
        !           543: cleanup:
        !           544:        if (fmt & fmtSave) {
        !           545:                vifd = vifdNil;
        !           546:                IfdFOpen(ifdSave);
        !           547:                vipd = ipdSave;
        !           548:                viln = ilnSave;
        !           549:                vslop = slopSave;
        !           550:        } /* if */
        !           551: } /* PrintPos */

unix.superglobalmegacorp.com

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