Annotation of 43BSDReno/games/trek/getpar.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1980 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted provided
                      6:  * that: (1) source distributions retain this entire copyright notice and
                      7:  * comment, and (2) distributions including binaries display the following
                      8:  * acknowledgement:  ``This product includes software developed by the
                      9:  * University of California, Berkeley and its contributors'' in the
                     10:  * documentation or other materials provided with the distribution and in
                     11:  * all advertising materials mentioning features or use of this software.
                     12:  * Neither the name of the University nor the names of its contributors may
                     13:  * be used to endorse or promote products derived from this software without
                     14:  * specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     16:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     17:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: static char sccsid[] = "@(#)getpar.c   4.8 (Berkeley) 6/1/90";
                     22: #endif /* not lint */
                     23: 
                     24: # include      <stdio.h>
                     25: # include      "getpar.h"
                     26: 
                     27: /**
                     28:  **    get integer parameter
                     29:  **/
                     30: 
                     31: getintpar(s)
                     32: char   *s;
                     33: {
                     34:        register int    i;
                     35:        int             n;
                     36: 
                     37:        while (1)
                     38:        {
                     39:                if (testnl() && s)
                     40:                        printf("%s: ", s);
                     41:                i = scanf("%d", &n);
                     42:                if (i < 0)
                     43:                        exit(1);
                     44:                if (i > 0 && testterm())
                     45:                        return (n);
                     46:                printf("invalid input; please enter an integer\n");
                     47:                skiptonl(0);
                     48:        }
                     49: }
                     50: 
                     51: /**
                     52:  **    get floating parameter
                     53:  **/
                     54: 
                     55: double getfltpar(s)
                     56: char   *s;
                     57: {
                     58:        register int            i;
                     59:        double                  d;
                     60: 
                     61:        while (1)
                     62:        {
                     63:                if (testnl() && s)
                     64:                        printf("%s: ", s);
                     65:                i = scanf("%lf", &d);
                     66:                if (i < 0)
                     67:                        exit(1);
                     68:                if (i > 0 && testterm())
                     69:                        return (d);
                     70:                printf("invalid input; please enter a double\n");
                     71:                skiptonl(0);
                     72:        }
                     73: }
                     74: 
                     75: /**
                     76:  **    get yes/no parameter
                     77:  **/
                     78: 
                     79: struct cvntab  Yntab[] =
                     80: {
                     81:        "y",    "es",   (int (*)())1,   0,
                     82:        "n",    "o",    (int (*)())0,   0,
                     83:        0
                     84: };
                     85: 
                     86: getynpar(s)
                     87: char   *s;
                     88: {
                     89:        struct cvntab           *r;
                     90: 
                     91:        r = getcodpar(s, Yntab);
                     92:        return ((int) r->value);
                     93: }
                     94: 
                     95: 
                     96: /**
                     97:  **    get coded parameter
                     98:  **/
                     99: 
                    100: struct cvntab *getcodpar(s, tab)
                    101: char           *s;
                    102: struct cvntab  tab[];
                    103: {
                    104:        char                            input[100];
                    105:        register struct cvntab          *r;
                    106:        int                             flag;
                    107:        register char                   *p, *q;
                    108:        int                             c;
                    109:        int                             f;
                    110: 
                    111:        flag = 0;
                    112:        while (1)
                    113:        {
                    114:                flag |= (f = testnl());
                    115:                if (flag)
                    116:                        printf("%s: ", s);
                    117:                if (f)
                    118:                        cgetc(0);               /* throw out the newline */
                    119:                scanf("%*[ \t;]");
                    120:                if ((c = scanf("%[^ \t;\n]", input)) < 0)
                    121:                        exit(1);
                    122:                if (c == 0)
                    123:                        continue;
                    124:                flag = 1;
                    125: 
                    126:                /* if command list, print four per line */
                    127:                if (input[0] == '?' && input[1] == 0)
                    128:                {
                    129:                        c = 4;
                    130:                        for (r = tab; r->abrev; r++)
                    131:                        {
                    132:                                concat(r->abrev, r->full, input);
                    133:                                printf("%14.14s", input);
                    134:                                if (--c > 0)
                    135:                                        continue;
                    136:                                c = 4;
                    137:                                printf("\n");
                    138:                        }
                    139:                        if (c != 4)
                    140:                                printf("\n");
                    141:                        continue;
                    142:                }
                    143: 
                    144:                /* search for in table */
                    145:                for (r = tab; r->abrev; r++)
                    146:                {
                    147:                        p = input;
                    148:                        for (q = r->abrev; *q; q++)
                    149:                                if (*p++ != *q)
                    150:                                        break;
                    151:                        if (!*q)
                    152:                        {
                    153:                                for (q = r->full; *p && *q; q++, p++)
                    154:                                        if (*p != *q)
                    155:                                                break;
                    156:                                if (!*p || !*q)
                    157:                                        break;
                    158:                        }
                    159:                }
                    160: 
                    161:                /* check for not found */
                    162:                if (!r->abrev)
                    163:                {
                    164:                        printf("invalid input; ? for valid inputs\n");
                    165:                        skiptonl(0);
                    166:                }
                    167:                else
                    168:                        return (r);
                    169:        }
                    170: }
                    171: 
                    172: 
                    173: /**
                    174:  **    get string parameter
                    175:  **/
                    176: 
                    177: getstrpar(s, r, l, t)
                    178: char   *s;
                    179: char   *r;
                    180: int    l;
                    181: char   *t;
                    182: {
                    183:        register int    i;
                    184:        char            format[20];
                    185:        register int    f;
                    186: 
                    187:        if (t == 0)
                    188:                t = " \t\n;";
                    189:        (void)sprintf(format, "%%%d[^%s]", l, t);
                    190:        while (1)
                    191:        {
                    192:                if ((f = testnl()) && s)
                    193:                        printf("%s: ", s);
                    194:                if (f)
                    195:                        cgetc(0);
                    196:                scanf("%*[\t ;]");
                    197:                i = scanf(format, r);
                    198:                if (i < 0)
                    199:                        exit(1);
                    200:                if (i != 0)
                    201:                        return;
                    202:        }
                    203: }
                    204: 
                    205: 
                    206: /**
                    207:  **    test if newline is next valid character
                    208:  **/
                    209: 
                    210: testnl()
                    211: {
                    212:        register char           c;
                    213: 
                    214:        while ((c = cgetc(0)) != '\n')
                    215:                if ((c >= '0' && c <= '9') || c == '.' || c == '!' ||
                    216:                                (c >= 'A' && c <= 'Z') ||
                    217:                                (c >= 'a' && c <= 'z') || c == '-')
                    218:                {
                    219:                        ungetc(c, stdin);
                    220:                        return(0);
                    221:                }
                    222:        ungetc(c, stdin);
                    223:        return (1);
                    224: }
                    225: 
                    226: 
                    227: /**
                    228:  **    scan for newline
                    229:  **/
                    230: 
                    231: skiptonl(c)
                    232: char   c;
                    233: {
                    234:        while (c != '\n')
                    235:                if (!(c = cgetc(0)))
                    236:                        return;
                    237:        ungetc('\n', stdin);
                    238:        return;
                    239: }
                    240: 
                    241: 
                    242: /**
                    243:  **    test for valid terminator
                    244:  **/
                    245: 
                    246: testterm()
                    247: {
                    248:        register char           c;
                    249: 
                    250:        if (!(c = cgetc(0)))
                    251:                return (1);
                    252:        if (c == '.')
                    253:                return (0);
                    254:        if (c == '\n' || c == ';')
                    255:                ungetc(c, stdin);
                    256:        return (1);
                    257: }
                    258: 
                    259: 
                    260: /*
                    261: **  TEST FOR SPECIFIED DELIMETER
                    262: **
                    263: **     The standard input is scanned for the parameter.  If found,
                    264: **     it is thrown away and non-zero is returned.  If not found,
                    265: **     zero is returned.
                    266: */
                    267: 
                    268: readdelim(d)
                    269: char   d;
                    270: {
                    271:        register char   c;
                    272: 
                    273:        while (c = cgetc(0))
                    274:        {
                    275:                if (c == d)
                    276:                        return (1);
                    277:                if (c == ' ')
                    278:                        continue;
                    279:                ungetc(c, stdin);
                    280:                break;
                    281:        }
                    282:        return (0);
                    283: }

unix.superglobalmegacorp.com

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