Annotation of researchv10dc/cmd/troff/t11.c, revision 1.1

1.1     ! root        1: #include "tdef.h"
        !             2: #include "fns.h"
        !             3: #include "ext.h"
        !             4: 
        !             5: #define        MAXCH NCHARS            /* maximum number of global char names */
        !             6: char   *chnames[MAXCH];        /* chnames[n-ALPHABET] -> name of char n */
        !             7: int    nchnames;               /* number of Cxy names currently seen */
        !             8: 
        !             9: #define        MAXPS   100             /* max number of point sizes */
        !            10: int    pstab[MAXPS];           /* point sizes */
        !            11: int    nsizes;                 /* number in DESC */
        !            12: 
        !            13: Font   fonts[MAXFONTS+1];      /* font info + ptr to width info */
        !            14: 
        !            15: 
        !            16: #define        skipline(f)     while (getc(f) != '\n')
        !            17: 
        !            18: getdesc(char *name)
        !            19: {
        !            20:        FILE *fin;
        !            21:        char cmd[100], s[100];
        !            22:        int i, v;
        !            23: 
        !            24:        if ((fin = fopen(name, "r")) == NULL)
        !            25:                return -1;
        !            26:        while (fscanf(fin, "%s", cmd) != EOF) {
        !            27:                if (strcmp(cmd, "res") == 0) {
        !            28:                        fscanf(fin, "%d", &Inch);
        !            29:                } else if (strcmp(cmd, "hor") == 0) {
        !            30:                        fscanf(fin, "%d", &Hor);
        !            31:                } else if (strcmp(cmd, "vert") == 0) {
        !            32:                        fscanf(fin, "%d", &Vert);
        !            33:                } else if (strcmp(cmd, "unitwidth") == 0) {
        !            34:                        fscanf(fin, "%d", &Unitwidth);
        !            35:                } else if (strcmp(cmd, "sizes") == 0) {
        !            36:                        nsizes = 0;
        !            37:                        while (fscanf(fin, "%d", &v) != EOF && v != 0 && nsizes < MAXPS)
        !            38:                                pstab[nsizes++] = v;
        !            39:                } else if (strcmp(cmd, "fonts") == 0) {
        !            40:                        fscanf(fin, "%d", &nfonts);
        !            41:                        for (i = 1; i <= nfonts; i++) {
        !            42:                                fscanf(fin, "%s", s);
        !            43:                                fontlab[i] = PAIR(s[0], s[1]);
        !            44:                        }
        !            45:                } else if (strcmp(cmd, "charset") == 0) {       /* add any names */
        !            46:                        while (fscanf(fin, "%s", s) != EOF)
        !            47:                                chadd(s);
        !            48:                        break;
        !            49:                }
        !            50:                /* else 
        !            51:                        just skip anything else */
        !            52:                skipline(fin);
        !            53:        }
        !            54:        fclose(fin);
        !            55:        return 1;
        !            56: }
        !            57: 
        !            58: #define        eq(s1, s2)      (strcmp(s1, s2) == 0)
        !            59: 
        !            60: static int checkfont(char *name)
        !            61: {              /* in case it's not really a font description file */
        !            62:                /* really paranoid, but consider \f. */
        !            63:        FILE *fp;
        !            64:        char buf[300], buf2[300];
        !            65:        int i, status = -1;
        !            66: 
        !            67:        if ((fp = fopen(name, "r")) == NULL)
        !            68:                return -1;
        !            69:        for (i = 1; i <= 10; i++) {
        !            70:                if (fgets(buf, sizeof buf, fp) == NULL)
        !            71:                        break;
        !            72:                sscanf(buf, "%s", buf2);
        !            73:                if (eq(buf2, "name") || eq(buf2, "fontname") ||
        !            74:                    eq(buf2, "special") || eq(buf2, "charset")) {
        !            75:                        status = 1;
        !            76:                        break;
        !            77:                }
        !            78:        }
        !            79:        fclose(fp);
        !            80:        return status;
        !            81:        
        !            82: }
        !            83: 
        !            84: getfont(char *name, int pos)   /* create width tab for font */
        !            85: {
        !            86:        FILE *fin;
        !            87:        Font *ftemp = &fonts[pos];
        !            88:        Chwid chtemp[MAXCH];
        !            89:        static Chwid chinit;
        !            90:        int i, nw, n, wid, kern, code;
        !            91:        char buf[100], ch[100], s1[100], s2[100], s3[100], cmd[300];
        !            92: 
        !            93:        /* fprintf(stderr, "read font %s onto %d\n", name, pos); */
        !            94:        if (checkfont(name) == -1)
        !            95:                return -1;
        !            96:        if ((fin = fopen(name, "r")) == NULL)
        !            97:                return -1;
        !            98:        for (i = 0; i < ALPHABET; i++)
        !            99:                chtemp[i] = chinit;     /* zero out to begin with */
        !           100:        ftemp->specfont = ftemp->ligfont = 0;
        !           101:        ftemp->defaultwidth = ftemp->spacewidth = Inch * Unitwidth / 72 / 3; /* should be rounded */
        !           102:        while (fscanf(fin, "%s", cmd) != EOF) {
        !           103:                if (strcmp(cmd, "name") == 0)
        !           104:                        fscanf(fin, "%s", ftemp->longname);
        !           105:                else if (strcmp(cmd, "special") == 0)
        !           106:                        ftemp->specfont = 1;
        !           107:                else if (strcmp(cmd, "ligatures") == 0) {
        !           108:                        ftemp->ligfont = getlig(fin);
        !           109:                } else if (strcmp(cmd, "spacewidth") == 0) {
        !           110:                        fscanf(fin, "%d", &ftemp->spacewidth);
        !           111:                } else if (strcmp(cmd, "defaultwidth") == 0) {
        !           112:                        fscanf(fin, "%d", &ftemp->defaultwidth);
        !           113:                } else if (strcmp(cmd, "charset") == 0) {
        !           114:                        skipline(fin);
        !           115:                        nw = ALPHABET;
        !           116:                        while (fgets(buf, sizeof buf, fin) != NULL) {
        !           117:                                sscanf(buf, "%s %s %s %s", ch, s1, s2, s3);
        !           118:                                if (s1[0] != '"') {     /* genuine new character */
        !           119:                                        sscanf(s1, "%d", &wid);
        !           120:                                        sscanf(s2, "%d", &kern);
        !           121:                                        code = strtol(s3, 0, 0);        /* dec/oct/hex */
        !           122:                                }
        !           123:                                /* otherwise it's a synonym for prev character, */
        !           124:                                /* so leave previous values intact */
        !           125: 
        !           126: 
        !           127:                                /* decide what kind of alphabet it might come from here */
        !           128: 
        !           129: 
        !           130:                                if (strlen(ch) == 1) {  /* it's ascii */
        !           131:                                        n = ch[0];      /* origin includes non-graphics */
        !           132:                                        chtemp[n].num = ch[0];
        !           133:                                        chtemp[n].wid = wid;
        !           134:                                        chtemp[n].kern = kern;
        !           135:                                        chtemp[n].code = code;
        !           136:                                } else if (ch[0] == '\\' && ch[1] == '0') {
        !           137:                                        /* \0octal or \0xhex */
        !           138:                                        n = strtol(ch+1, 0, 0);
        !           139:                                        chtemp[n].num = n;
        !           140:                                        chtemp[n].wid = wid;
        !           141:                                        chtemp[n].kern = kern;
        !           142:                                        chtemp[n].code = code;
        !           143:                                } else {
        !           144:                                        if (strcmp(ch, "---") == 0) /* no name */
        !           145:                                                sprintf(ch, "#%d", code);
        !           146:                                        if ((n = chindex(ch)) == -1) /* global? */
        !           147:                                                n = chadd(ch);
        !           148:                                        chtemp[nw].num = n;
        !           149:                                        chtemp[nw].wid = wid;
        !           150:                                        chtemp[nw].kern = kern;
        !           151:                                        chtemp[nw].code = code;
        !           152:                                        nw++;
        !           153:                                }
        !           154:                                /* fprintf(stderr, "font %2.2s char %4.4s num %3d wid %2d code %3d\n",
        !           155:                                        ftemp->longname, ch, n, wid, code);
        !           156:                                */
        !           157:                        }
        !           158:                        break;
        !           159:                }
        !           160:                skipline(fin);
        !           161:        }
        !           162:        fclose(fin);
        !           163:        chtemp[' '].wid = ftemp->spacewidth;    /* width of space on this font */
        !           164:        ftemp->nchars = nw;
        !           165:        if (ftemp->wp)
        !           166:                free(ftemp->wp);        /* god help us if this wasn't allocated */
        !           167:        ftemp->wp = (Chwid *) malloc(nw * sizeof(Chwid));
        !           168:        if (ftemp->wp == NULL)
        !           169:                return -1;
        !           170:        for (i = 0; i < nw; i++)
        !           171:                ftemp->wp[i] = chtemp[i];
        !           172: /*
        !           173:  *     printf("%d chars: ", nw);
        !           174:  *     for (i = 0; i < nw; i++) {
        !           175:  *             if (ftemp->wp[i].num > 0 && ftemp->wp[i].num < ALPHABET)
        !           176:  *                     printf("%c %d ", ftemp->wp[i].num, ftemp->wp[i].wid);
        !           177:  *             else if (i >= ALPHABET)
        !           178:  *                     printf("%d (%s) %d ", ftemp->wp[i].num,
        !           179:  *                             chnames[ftemp->wp[i].num-ALPHABET], ftemp->wp[i].wid);
        !           180:  *     }
        !           181:  *     printf("\n");
        !           182:  */
        !           183:        return 1;
        !           184: }
        !           185: 
        !           186: chindex(char *s)       /* look for s in global character name table */
        !           187: {
        !           188:        int i;
        !           189: 
        !           190:        for (i = 0; i < nchnames; i++)
        !           191:                if (strcmp(s, chnames[i]) == 0)
        !           192:                        return i + ALPHABET;    /* offset by ALPHABET... */
        !           193:        return -1;
        !           194: }
        !           195: 
        !           196: chadd(char *s) /* add s to global character name table;  assume not there */
        !           197: {              /* changes when table format changes */
        !           198:        char *p;
        !           199: 
        !           200:        chnames[nchnames] = p = (char *) malloc(strlen(s)+1);
        !           201:        if (p == NULL) {
        !           202:                ERROR "out of space adding character %s\n", s WARN;
        !           203:                return LEFTHAND;
        !           204:        }
        !           205:        if (nchnames >= NCHARS) {
        !           206:                ERROR "out ot table space adding character %s\n", s WARN;
        !           207:                return LEFTHAND;
        !           208:        }
        !           209:        strcpy(chnames[nchnames], s);
        !           210:        return nchnames++ + ALPHABET;
        !           211: }
        !           212: 
        !           213: char *chname(int n)    /* return string for char with index n */
        !           214: {                      /* changes when table format changes */
        !           215:        return chnames[n-ALPHABET];
        !           216: }
        !           217: 
        !           218: getlig(FILE *fin)      /* pick up ligature list */
        !           219: {
        !           220:        int lig;
        !           221:        char temp[200];
        !           222: 
        !           223:        lig = 0;
        !           224:        while (fscanf(fin, "%s", temp) != EOF && strcmp(temp, "0") != 0) {
        !           225:                if (strcmp(temp, "fi") == 0)
        !           226:                        lig |= LFI;
        !           227:                else if (strcmp(temp, "fl") == 0)
        !           228:                        lig |= LFL;
        !           229:                else if (strcmp(temp, "ff") == 0)
        !           230:                        lig |= LFF;
        !           231:                else if (strcmp(temp, "ffi") == 0)
        !           232:                        lig |= LFFI;
        !           233:                else if (strcmp(temp, "ffl") == 0)
        !           234:                        lig |= LFFL;
        !           235:                else
        !           236:                        fprintf(stderr, "illegal ligature %s ignored\n", temp);
        !           237:        }
        !           238:        return lig;
        !           239: }

unix.superglobalmegacorp.com

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