Annotation of researchv10no/cmd/dict/ldefine.c, revision 1.1.1.1

1.1       root        1: # include <stdio.h>
                      2: # include <ctype.h>
                      3: # include <assert.h>
                      4: #include <libc.h>
                      5: char *dictdir = "/usr/dict/oaldce";
                      6: extern char *ggets(), *fgets(), *strchr();
                      7: char *jbrk=" ";
                      8: main(argc,argv)
                      9:        char *argv[];
                     10: {
                     11: char ln[100];
                     12: char *dict = getenv("OALDCEDIR");
                     13: if(dict==0)
                     14:        dict = dictdir;
                     15: assert( chdir(dict) == NULL);
                     16: argc--; argv++;
                     17: if (argc<=0)
                     18:        {
                     19:        while (ggets(ln,sizeof ln,stdin))
                     20:                lookup(ln);
                     21:        }
                     22: while (argc-- >0)
                     23:        lookup(*argv++);
                     24: }
                     25: lcase (s)
                     26:        char *s;
                     27: {
                     28: for (; *s; s++)
                     29:        if (isupper(*s))
                     30:                *s = tolower(*s);
                     31: }
                     32: char *ltab[] ={
                     33: "a", "b", "c", "d", "e", "f", "g", "h", "ijk", "ijk", "ijk", "l", "m",
                     34: "no", "no", "p", "q", "r", "s", "t", "uvw", "uvw", "uvw", "xyz", "xyz", "xyz"};
                     35: lookup (wd)
                     36:        char *wd;
                     37: {
                     38: int ilet, nwd, yet, ks;
                     39: char fn[10], dbuff[300], xbuff[300];
                     40: FILE *fd;
                     41: while(*wd && isascii(*wd) && isspace(*wd))
                     42:        wd++;
                     43: if(*wd == 0)
                     44:        return;
                     45: lcase(wd);
                     46: ccut(wd);
                     47: ilet = *wd;
                     48: assert (ilet>='a' && ilet <= 'z');
                     49: sprintf(fn, "D%s", ltab[ilet-'a']);
                     50: fd = fopen(fn, "r");
                     51: if (fd==NULL)
                     52:        {
                     53:        fprintf(stderr, "Confused by %s\n", wd);
                     54:        return;
                     55:        }
                     56: binpos (fd, wd);
                     57: yet=0;
                     58: jbrk="";
                     59: while (fgets(dbuff, sizeof dbuff, fd))
                     60:        {
                     61:        if (dbuff[0]!= '\n' && dbuff[0]!= ' ')
                     62:                {
                     63:                if (yet>0) lbrk();
                     64:                strcpy(xbuff, dbuff);
                     65:                lcase(xbuff);
                     66:                ccut(xbuff);
                     67:                ks = scomp(xbuff, wd);
                     68:                if (ks>0)
                     69:                        break;
                     70:                if (ks==0) yet=1;
                     71:                }
                     72:        if (yet>0) dump(dbuff, wd);
                     73:        }
                     74: }
                     75: scomp( dl, wd)
                     76:        char *dl, *wd;
                     77: {
                     78: int n, k;
                     79: n = strlen(wd);
                     80:        k= strncmp(dl, wd,n);
                     81:        if (k==0 && (dl[n]==' ' || dl[n]==','))
                     82:                return(0);
                     83:        return(k==0? 1: k);
                     84: }
                     85: static char cbuff[200];
                     86: static char *cp = cbuff;
                     87: static int col=0;
                     88: dump(s, wd)
                     89:        char *s, *wd;
                     90: {
                     91: int c;
                     92: while (c = *s++)
                     93:        {
                     94:        if (c=='\n')c=' ';
                     95:        if (c!='@')
                     96:                *cp++ = c;
                     97:        else
                     98:                {
                     99:                if (cp[-1]==toupper(wd[0]))
                    100:                        strcpy(cp, wd+1);
                    101:                else
                    102:                        strcpy(cp, wd);
                    103:                cp += strlen(cp);
                    104:                }
                    105:        }
                    106: *cp=0;
                    107: if (strlen(cbuff)<80)
                    108:        {
                    109:        put(jbrk, cbuff);
                    110:        jbrk = " ";
                    111:        cp=cbuff;
                    112:        *cp=0;
                    113:        }
                    114: else
                    115:        {
                    116:        while (strlen(cbuff)>78)
                    117:                {
                    118:                for(cp=cbuff+78; cp>cbuff && *cp!=' '; cp--)
                    119:                        ;
                    120:                *cp++=0;
                    121:                put(jbrk, cbuff);
                    122:                strcpy(cbuff, cp);
                    123:                cp= cbuff+strlen(cbuff);
                    124:                *cp=0;
                    125:                jbrk= " ";
                    126:                }
                    127:        }
                    128: }
                    129: put (s, t)
                    130:        char *s, *t;
                    131: {
                    132: if (*s==' ' && *t==' ')
                    133:        printf("%s\n",t);
                    134: else
                    135:        printf("%s%s\n",s,t);
                    136: }
                    137: lbrk()
                    138: {
                    139: printf("%s\n",cbuff);
                    140: cp=cbuff;
                    141: jbrk = "";
                    142: }
                    143: binpos(f, word)
                    144:        char *word;
                    145:        FILE *f;
                    146: {
                    147: long top, bot, mid;
                    148: int k, c, oc;
                    149: char line[100];
                    150: bot=0;
                    151: fseek(f, 0L, 2);
                    152: top = ftell(f);
                    153: for (;;)
                    154:        {
                    155:        mid = (top+bot)/2;
                    156:        fseek(f, mid, 0);
                    157:        c=0;
                    158:        do {
                    159:                oc=c;
                    160:                c = getc(f);
                    161:                mid++;
                    162:           } while (c!=EOF && ( c !='\n' || oc != '\n'));
                    163:        if (fgets(line, sizeof line, f)==NULL)
                    164:                break;
                    165:        lcase(line);
                    166:        ccut(line);
                    167:        k = strcmp(word, line);
                    168:        if (k<=0)
                    169:                {
                    170:                if (top<=mid)
                    171:                        break;
                    172:                top = mid;
                    173:                continue;
                    174:                }
                    175:        else
                    176:                {
                    177:                bot= mid;
                    178:                continue;
                    179:                }
                    180:        }
                    181: fseek(f, bot, 0);
                    182: if (bot!=0)
                    183: while ( (c=getc(f)) != '\n' && c!= EOF);
                    184: }
                    185: ccut(s)
                    186:        char *s;
                    187: {
                    188: char x[100], *p, *y;
                    189: y=s;
                    190: for(p=x; *s &&  *s!=' '; s++)
                    191:        if (!strchr("-'_()\"", *s))
                    192:                *p++ = *s;
                    193: while (*s)
                    194:        *p++ = *s++;
                    195: *p=0;
                    196: strcpy(y, x);
                    197: }
                    198: 
                    199: char *
                    200: ggets(s,n,f)
                    201: char *s;
                    202: FILE *f;
                    203: {
                    204:        char *p = fgets(s,n,f);
                    205:        char *q;
                    206:        if(p && (q=strchr(s,'\n')))
                    207:                *q = 0;
                    208:        return p;
                    209: }

unix.superglobalmegacorp.com

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