Annotation of researchv10no/games/atc/aread.c, revision 1.1

1.1     ! root        1: /* aread.c: read and parse an airspace file */
        !             2: 
        !             3: #include <stdio.h>
        !             4: #include <sys/ttyio.h>
        !             5: #include <sys/param.h>
        !             6: #include <sys/timeb.h>
        !             7: #include <ctype.h>
        !             8: #include <signal.h>
        !             9: #include "ahdr.h"
        !            10: 
        !            11: /* The following section reads a file containing info on the
        !            12:  * current airspace.  A sample looks like:
        !            13: Apple1
        !            14:        size: 15x24
        !            15:        airway: 1=(0,13) [20-30] SE 8=(10,23) [10-5]
        !            16:        airway: 0=(4,0) S 9=(4,23)
        !            17:        airway: 2=(14,15) NW 7=(0,1)
        !            18:        airway: 3=(0,9) NE 6=(9,0)
        !            19:        airway: 4=(14,7) SW 5=(0,21)
        !            20:        airport: %=(4,11) S
        !            21:        airport: #=(10,11) NE
        !            22:        navaid: *=(4,5)
        !            23:        navaid: *=(4,17)
        !            24:  * where the airspace id starts in the first column, and everything else
        !            25:  * is indented one tab and has syntax exactly as shown.
        !            26:  * Numbers in square brackets are proportion of entries-exits from-to this
        !            27:  * point.  Default for normal places is 10 and default for airports is 16.
        !            28:  */
        !            29: 
        !            30: /* note: [prob] code is obsolete */
        !            31: 
        !            32: #define I_SIZE 1        /* these are the tokens for the airspace identifiers*/
        !            33: #define I_AIRWAY 2
        !            34: #define I_AIRPORT 3
        !            35: #define I_NAVAID 4
        !            36: 
        !            37: struct namestr
        !            38: {       char *i_name;
        !            39:        int i_token;
        !            40: } name[] =
        !            41: {       "size",         I_SIZE,
        !            42:        "airway",       I_AIRWAY,
        !            43:        "airport",      I_AIRPORT,
        !            44:        "navaid",       I_NAVAID,
        !            45:        0
        !            46: };
        !            47: 
        !            48: extern int npaths;      /* number of paths in the path file */
        !            49: extern int maxflow;
        !            50: extern struct flow fpath[MAXPATHS];
        !            51: 
        !            52: #define BUFSIZE 100
        !            53: 
        !            54: extern struct pstruct entry[EMAX],airport[AMAX],navaid[NMAX];
        !            55: extern char *airspace,*airfile,*flowfile;
        !            56: extern int height,width;
        !            57: extern char screen[MAXWIDTH][MAXHEIGHT];
        !            58: extern int nentry,nairport,nnavaid;
        !            59: extern int inawtot,inaptot,outawtot,outaptot;
        !            60: 
        !            61: char *strchr();
        !            62: 
        !            63: readspace()     /* read the airspace file to get user's desired airspace */
        !            64: {       FILE *asfile;
        !            65:        char lbuf[BUFSIZE]; /* line from the airspace file */
        !            66:        char ibuf[BUFSIZE];  /* airspace identifier */
        !            67:        char *c;
        !            68:        struct namestr *n;
        !            69:        int i,org,dst;
        !            70:        FILE *flowstream;
        !            71:        struct flow *path;
        !            72: 
        !            73:        if ((asfile = fopen(airfile,"r")) == NULL)
        !            74:                err("Cannot open airspace file, %s\n",airfile);
        !            75:        do      /* scan through the file until we get the right airspace */
        !            76:        {       if (fgets(lbuf,BUFSIZE,asfile) == NULL)
        !            77:                    err("Couldn't find airspace %s in %s\n",airspace,airfile);
        !            78:                if (lbuf[0] == '\t') continue; /* not an airspace id */
        !            79:                for (c=lbuf; *c; c++) if (*c == '\n') *c = 0; /*kill newline*/
        !            80:        }
        !            81:        while (strcmp(lbuf,airspace));
        !            82:        /* next consider each line of this airspace in turn */
        !            83:        while (fgets(lbuf,BUFSIZE,asfile) != NULL & lbuf[0] == '\t')
        !            84:        {       for (c=lbuf; *c == '\t' || *c == ' '; c++); /* skip blanks */
        !            85:                strcpy(ibuf,c);
        !            86:                if ((c = strchr(ibuf,':')) == 0)
        !            87:                        err("No colon in %s, line %s",airfile,lbuf);
        !            88:                *c = 0; /* terminate the command id */
        !            89:                for (n = name; n->i_name != 0; n++) /* get symbol */
        !            90:                        if (strcmp(ibuf, n->i_name) == 0) break;
        !            91:                switch(n->i_token)
        !            92:                {   case I_SIZE: sizeread(lbuf); break;
        !            93:                    case I_AIRWAY: airwayread(lbuf); break;
        !            94:                    case I_AIRPORT: apread(lbuf); break;
        !            95:                    case I_NAVAID: navaidread(lbuf); break;
        !            96:                    default: err("Unrecognized id in %s, line %s",
        !            97:                                airfile,lbuf);
        !            98:                }
        !            99:        }
        !           100:        fclose(asfile);
        !           101:        /* now put commas on all the airways */
        !           102:        for (i=0; i<nentry; i++) /* go both ways -- doubles the work */
        !           103:        {       int x,y,dx,dy;
        !           104: 
        !           105:                x = entry[i].p_x;
        !           106:                y = entry[i].p_y;
        !           107:                dx = entry[i].p_dx;
        !           108:                dy = entry[i].p_dy;
        !           109:                while (x>=0 && y>=0 && x<width && y<height)
        !           110:                {       if (screen[x][y] == '.') screen[x][y] = ',';
        !           111:                        x += dx;
        !           112:                        y += dy;
        !           113:                }
        !           114:        }
        !           115:        if (!flowfile)  /* if no flow file specified, look for one */
        !           116:        {       char *s,*t;
        !           117:                static char flowbuf[80];
        !           118:                FILE *test;
        !           119: 
        !           120:                if (*(t = airfile) == '/')      /* full airfile path? */
        !           121:                {       for (s = flowbuf; *s++ = *t++; ); /* copy airfile */
        !           122:                        while (*--s != '/'); /* back to last slash */
        !           123:                        s++;
        !           124:                }
        !           125:                else    s = flowbuf;
        !           126:                for (t = airspace; *t; *s++ = *t++);/*air space*/
        !           127:                for (t = ".flow"; *s++ = *t++; );
        !           128:                if ((test = fopen(flowbuf,"r")) != NULL) /* it's there */
        !           129:                {       fclose(test);
        !           130:                        flowfile = flowbuf;
        !           131:                }
        !           132:        }
        !           133:        if (flowfile)   /* flow control */
        !           134:        {       maxflow = 0;
        !           135:                if ((flowstream = fopen(flowfile,"r")) == NULL)
        !           136:                        err("Cannot open flow control file: %s\n",flowfile);
        !           137:                path = &fpath[npaths = 0];
        !           138:                while (fgets(lbuf,BUFSIZE,flowstream) != NULL)
        !           139:                {       if (lbuf[0] == '\n') continue; /* ignore newlines */
        !           140:                        org = *(c = lbuf);      /* 1st char is origin */
        !           141:                        if (*++c != '-')
        !           142:                                err("Path format: x->y 123\nYou gave me %s\n",
        !           143:                                        lbuf);
        !           144:                        if (*++c != '>')
        !           145:                                err("Path format: x->y 123\nYou gave me %s\n",
        !           146:                                        lbuf);
        !           147:                        dst = *++c;
        !           148:                        /* we need to code these in their flightpath() fmt */
        !           149:                        pcode(org,path,1);
        !           150:                        pcode(dst,path,0);
        !           151:                        path->f_freq = atoi(++c);       /* get relative freq*/
        !           152:                        maxflow += path->f_freq;        /* total frequency */
        !           153: 
        !           154:                        /* read distances for minimal paths */
        !           155:                        while (*++c) if (*c == ' ') break; /* next fld */
        !           156:                        path->f_cmds = (*c == 0? 1 : atoi(c));
        !           157:                        if (*c) while (*++c) if (*c != ' ') break;
        !           158:                        if (*c) while (*++c) if (*c == ' ') break;
        !           159:                        path->f_dist[0] = (*c == 0? 1 : atoi(c));
        !           160:                        for (i=1;i<7;i++) path->f_dist[i] = path->f_dist[0];
        !           161:                        for (i = 7; i < NALT; i++)
        !           162:                        {       if (*c) while (*++c) if (*c != ' ') break;
        !           163:                                if (*c) while (*++c) if (*c == ' ') break;
        !           164:                                if (*c) path->f_dist[i] = atoi(c);
        !           165:                                else path->f_dist[i] = path->f_dist[i-1];
        !           166:                        }
        !           167: 
        !           168:                        path++;
        !           169:                        npaths++;
        !           170:                }
        !           171:                fclose(flowstream);
        !           172:        }
        !           173: }
        !           174: 
        !           175: pcode(c,path,from)   /* find flightplan() entry/exit code for character c */
        !           176: char c;
        !           177: struct flow *path;
        !           178: int from;
        !           179: {       int i;
        !           180: 
        !           181:        for (i=0; i<nentry; i++)
        !           182:                if (c == entry[i].p_sym) break;
        !           183:        if (i<nentry)
        !           184:        {       if (from)
        !           185:                {       path->f_from = i;
        !           186:                        path->f_fair = 0;
        !           187:                }
        !           188:                else
        !           189:                {       path->f_to = i;
        !           190:                        path->f_tair = 0;
        !           191:                }
        !           192:                return;
        !           193:        }
        !           194:        for (i=0; i<nairport; i++)
        !           195:                if (c == airport[i].p_sym) break;
        !           196:        if (i<nairport)
        !           197:        {       if (from)
        !           198:                {       path->f_from = i;
        !           199:                        path->f_fair = 1;
        !           200:                }
        !           201:                else
        !           202:                {       path->f_to = i;
        !           203:                        path->f_tair = 1;
        !           204:                }
        !           205:                return;
        !           206:        }
        !           207:        err("Unrecognized point %c in path file.\n",c);
        !           208: }
        !           209: 
        !           210: 
        !           211: sizeread(buf)
        !           212: char *buf;
        !           213: {       char *c;
        !           214: 
        !           215:        c = strchr(buf,':'); /* previously checked for existence */
        !           216:        width = atoi(++c);
        !           217:        if ((c = strchr(buf,'x')) == 0)
        !           218:                err("A size line must be of the form size: 15x25");
        !           219:        height = atoi(++c);
        !           220:        if (width > MAXWIDTH || width < MINWIDTH)
        !           221:                err("Width must be in the range %d to %d",MINWIDTH,MAXWIDTH);
        !           222:        if (height > MAXHEIGHT || height < MINHEIGHT)
        !           223:             err("Height must be in the range %d to %d",MINHEIGHT,MAXHEIGHT);
        !           224: }
        !           225: 
        !           226: 
        !           227: char *
        !           228: getpoint(buf,p) /* read an entry point, airport, or beacon from file */
        !           229: char *buf;
        !           230: struct pstruct *p;
        !           231: {       char *c;
        !           232:        int i;
        !           233: 
        !           234:        c = strchr(buf,'=');
        !           235:        if (c == 0) return(0);
        !           236:        p->p_sym = *--c;
        !           237:        c = strchr(c,'(');
        !           238:        if (c == 0) return(0);
        !           239:        p->p_x = atoi(++c);
        !           240:        c = strchr(c,',');
        !           241:        if (c == 0) return(0);
        !           242:        p->p_y = atoi(++c);
        !           243:        c = strchr(c,')');
        !           244:        if (c == 0) return(0);
        !           245:        screen[p->p_x][p->p_y] = p->p_sym;
        !           246:        ++c;    /* space or return after ) */
        !           247: /* note: [prob] code is obsolete */
        !           248:        if (*(c+1) == '[') /* if next char is [, then we have a proportion */
        !           249:        {       p->p_inprop = atoi(c+2);
        !           250:                c = strchr(c,'-');
        !           251:                if (c == 0) return(0);
        !           252:                p->p_outprop = atoi(++c);
        !           253:                c = strchr(c,']');
        !           254:                if (c == 0) return(0);
        !           255:                c++;
        !           256:        }
        !           257:        return(c);
        !           258: }
        !           259: 
        !           260: char *
        !           261: getdirec(c,p)
        !           262: char *c;
        !           263: struct pstruct *p;
        !           264: {       while (*c != ' ' && *c && *c != '\n')
        !           265:        {       switch(*c++)
        !           266:                {   case 'N':
        !           267:                        p->p_dy = -1;
        !           268:                        break;
        !           269:                    case 'S':
        !           270:                        p->p_dy = 1;
        !           271:                        break;
        !           272:                    case 'E':
        !           273:                        p->p_dx = 1;
        !           274:                        break;
        !           275:                    case 'W':
        !           276:                        p->p_dx = -1;
        !           277:                        break;
        !           278:                    default : return(0);
        !           279:                }
        !           280:        }
        !           281:        return(c);
        !           282: }
        !           283: 
        !           284: airwayread(buf)
        !           285: char *buf;
        !           286: {       char *c;
        !           287:        struct pstruct *p,*q;
        !           288: 
        !           289:        p = &entry[nentry++];
        !           290:        p->p_inprop = p->p_outprop = 10;
        !           291:        c = getpoint(buf,p);
        !           292:        inawtot += p->p_inprop;
        !           293:        outawtot += p->p_outprop;
        !           294:        if (c == 0) err("Airway syntax error");
        !           295:        c = strchr(c,' ');       /* now get direction */
        !           296:        if (c == 0) err("Airway syntax error");
        !           297:        p->p_dx = p->p_dy = 0;
        !           298:        c = getdirec(++c,p);
        !           299:        if (c == 0) err("Airway direction error");
        !           300:        q = &entry[nentry++];
        !           301:        q->p_inprop = q->p_outprop = 10;
        !           302:        c = getpoint(c,q);
        !           303:        inawtot += q->p_inprop;
        !           304:        outawtot += q->p_outprop;
        !           305:        if (c == 0) err("Airway syntax error");
        !           306:        q->p_dx = -p->p_dx;       /* reverse direction from other entry */
        !           307:        q->p_dy = -p->p_dy;
        !           308: }
        !           309: 
        !           310: apread(buf)
        !           311: char *buf;
        !           312: {       char *c;
        !           313:        struct pstruct *p;
        !           314: 
        !           315:        p = &airport[nairport++];
        !           316:        p->p_inprop = p->p_outprop = 26;
        !           317:        c = getpoint(buf,p);
        !           318:        inaptot += p->p_inprop;
        !           319:        outaptot += p->p_outprop;
        !           320:        if (c == 0) err("Airport syntax error");
        !           321:        p->p_dx = p->p_dy = 0;
        !           322:        c = getdirec(++c,p);
        !           323:        if (c == 0) err("Airport direction syntax error");
        !           324: }
        !           325: 
        !           326: navaidread(buf)
        !           327: char *buf;
        !           328: {
        !           329:        if (getpoint(buf,&navaid[nnavaid++]) == 0)
        !           330:                err("Navaid syntax error");
        !           331: }
        !           332: 
        !           333: 
        !           334: 

unix.superglobalmegacorp.com

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