[BACK]Return to value.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tip

Annotation of src/usr.bin/tip/value.c, Revision 1.16

1.16    ! moritz      1: /*     $OpenBSD: value.c,v 1.15 2007/02/20 01:44:16 ray Exp $  */
1.4       millert     2: /*     $NetBSD: value.c,v 1.6 1997/02/11 09:24:09 mrg Exp $    */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1983, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.10      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: #if 0
                     35: static char sccsid[] = "@(#)value.c    8.1 (Berkeley) 6/6/93";
                     36: #endif
1.16    ! moritz     37: static const char rcsid[] = "$OpenBSD: value.c,v 1.15 2007/02/20 01:44:16 ray Exp $";
1.1       deraadt    38: #endif /* not lint */
                     39:
                     40: #include "tip.h"
                     41:
                     42: #define MIDDLE 35
                     43:
1.11      deraadt    44: static value_t *vlookup(char *);
1.13      moritz     45: static void vassign(value_t *, char *);
                     46: static void vtoken(char *);
                     47: static void vprint(value_t *);
                     48: static int vaccess(unsigned int, unsigned int);
                     49: static char *vinterp(char *, int);
                     50:
1.14      moritz     51: static size_t col = 0;
1.1       deraadt    52:
                     53: /*
                     54:  * Variable manipulation
                     55:  */
1.5       deraadt    56: void
1.11      deraadt    57: vinit(void)
1.1       deraadt    58: {
1.11      deraadt    59:        char file[FILENAME_MAX], *cp;
1.16    ! moritz     60:        int written;
1.7       millert    61:        value_t *p;
1.11      deraadt    62:        FILE *fp;
1.1       deraadt    63:
                     64:        for (p = vtable; p->v_name != NULL; p++) {
                     65:                if (p->v_type&ENVIRON)
1.5       deraadt    66:                        if ((cp = getenv(p->v_name)))
1.1       deraadt    67:                                p->v_value = cp;
                     68:                if (p->v_type&IREMOTE)
1.4       millert    69:                        setnumber(p->v_value, *address(p->v_value));
1.1       deraadt    70:        }
                     71:        /*
                     72:         * Read the .tiprc file in the HOME directory
                     73:         *  for sets
                     74:         */
1.16    ! moritz     75:        written = snprintf(file, sizeof(file), "%s/.tiprc", value(HOME));
        !            76:        if (written < 0 || written >= sizeof(file)) {
1.4       millert    77:                (void)fprintf(stderr, "Home directory path too long: %s\n",
1.11      deraadt    78:                    value(HOME));
1.3       millert    79:        } else {
1.11      deraadt    80:                if ((fp = fopen(file, "r")) != NULL) {
1.7       millert    81:                        char *tp;
1.3       millert    82:
1.15      ray        83:                        while (fgets(file, sizeof(file), fp) != NULL) {
1.3       millert    84:                                if (vflag)
                     85:                                        printf("set %s", file);
1.5       deraadt    86:                                if ((tp = strrchr(file, '\n')))
1.3       millert    87:                                        *tp = '\0';
                     88:                                vlex(file);
                     89:                        }
1.11      deraadt    90:                        fclose(fp);
1.1       deraadt    91:                }
                     92:        }
                     93:        /*
                     94:         * To allow definition of exception prior to fork
                     95:         */
                     96:        vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
                     97: }
                     98:
                     99: /*VARARGS1*/
1.13      moritz    100: static void
1.11      deraadt   101: vassign(value_t *p, char *v)
1.1       deraadt   102: {
                    103:        if (!vaccess(p->v_access, WRITE)) {
                    104:                printf("access denied\r\n");
                    105:                return;
                    106:        }
1.11      deraadt   107:
1.1       deraadt   108:        switch (p->v_type&TMASK) {
                    109:        case STRING:
                    110:                if (p->v_value && equal(p->v_value, v))
                    111:                        return;
                    112:                if (!(p->v_type&(ENVIRON|INIT)))
                    113:                        free(p->v_value);
1.4       millert   114:                if ((p->v_value = strdup(v)) == NOSTR) {
1.1       deraadt   115:                        printf("out of core\r\n");
                    116:                        return;
                    117:                }
                    118:                p->v_type &= ~(ENVIRON|INIT);
                    119:                break;
                    120:        case NUMBER:
                    121:                if (number(p->v_value) == number(v))
                    122:                        return;
1.4       millert   123:                setnumber(p->v_value, number(v));
1.1       deraadt   124:                break;
                    125:        case BOOL:
                    126:                if (boolean(p->v_value) == (*v != '!'))
                    127:                        return;
1.4       millert   128:                setboolean(p->v_value, (*v != '!'));
1.1       deraadt   129:                break;
                    130:        case CHAR:
                    131:                if (character(p->v_value) == *v)
                    132:                        return;
1.4       millert   133:                setcharacter(p->v_value, *v);
1.1       deraadt   134:        }
                    135:        p->v_access |= CHANGED;
                    136: }
                    137:
1.5       deraadt   138: void
1.11      deraadt   139: vlex(char *s)
1.1       deraadt   140: {
1.7       millert   141:        value_t *p;
1.11      deraadt   142:        char *cp;
1.1       deraadt   143:
                    144:        if (equal(s, "all")) {
                    145:                for (p = vtable; p->v_name; p++)
                    146:                        if (vaccess(p->v_access, READ))
                    147:                                vprint(p);
                    148:        } else {
                    149:                do {
1.5       deraadt   150:                        if ((cp = vinterp(s, ' ')))
1.1       deraadt   151:                                cp++;
                    152:                        vtoken(s);
                    153:                        s = cp;
                    154:                } while (s);
                    155:        }
                    156:        if (col > 0) {
                    157:                printf("\r\n");
                    158:                col = 0;
                    159:        }
                    160: }
                    161:
                    162: static void
1.11      deraadt   163: vtoken(char *s)
1.1       deraadt   164: {
1.7       millert   165:        value_t *p;
                    166:        char *cp;
1.1       deraadt   167:
1.5       deraadt   168:        if ((cp = strchr(s, '='))) {
1.1       deraadt   169:                *cp = '\0';
1.5       deraadt   170:                if ((p = vlookup(s))) {
1.1       deraadt   171:                        cp++;
                    172:                        if (p->v_type&NUMBER)
1.11      deraadt   173:                                vassign(p, (char *)atoi(cp));
1.1       deraadt   174:                        else {
                    175:                                if (strcmp(s, "record") == 0)
                    176:                                        cp = expand(cp);
                    177:                                vassign(p, cp);
                    178:                        }
                    179:                        return;
                    180:                }
1.5       deraadt   181:        } else if ((cp = strchr(s, '?'))) {
1.1       deraadt   182:                *cp = '\0';
                    183:                if ((p = vlookup(s)) && vaccess(p->v_access, READ)) {
                    184:                        vprint(p);
                    185:                        return;
                    186:                }
                    187:        } else {
                    188:                if (*s != '!')
                    189:                        p = vlookup(s);
                    190:                else
                    191:                        p = vlookup(s+1);
                    192:                if (p != NOVAL) {
                    193:                        vassign(p, s);
                    194:                        return;
                    195:                }
                    196:        }
                    197:        printf("%s: unknown variable\r\n", s);
                    198: }
                    199:
                    200: static void
1.11      deraadt   201: vprint(value_t *p)
1.1       deraadt   202: {
1.7       millert   203:        char *cp;
1.1       deraadt   204:
                    205:        if (col > 0 && col < MIDDLE)
                    206:                while (col++ < MIDDLE)
                    207:                        putchar(' ');
                    208:        col += size(p->v_name);
                    209:        switch (p->v_type&TMASK) {
                    210:
                    211:        case BOOL:
                    212:                if (boolean(p->v_value) == FALSE) {
                    213:                        col++;
                    214:                        putchar('!');
                    215:                }
                    216:                printf("%s", p->v_name);
                    217:                break;
                    218:
                    219:        case STRING:
                    220:                printf("%s=", p->v_name);
                    221:                col++;
                    222:                if (p->v_value) {
1.12      moritz    223:                        cp = interp(p->v_value);
1.1       deraadt   224:                        col += size(cp);
                    225:                        printf("%s", cp);
                    226:                }
                    227:                break;
                    228:
                    229:        case NUMBER:
                    230:                col += 6;
1.5       deraadt   231:                printf("%s=%-5ld", p->v_name, number(p->v_value));
1.1       deraadt   232:                break;
                    233:
                    234:        case CHAR:
                    235:                printf("%s=", p->v_name);
                    236:                col++;
                    237:                if (p->v_value) {
                    238:                        cp = ctrl(character(p->v_value));
                    239:                        col += size(cp);
                    240:                        printf("%s", cp);
                    241:                }
                    242:                break;
                    243:        }
                    244:        if (col >= MIDDLE) {
                    245:                col = 0;
                    246:                printf("\r\n");
                    247:                return;
                    248:        }
                    249: }
                    250:
                    251: static int
1.11      deraadt   252: vaccess(unsigned int mode, unsigned int rw)
1.1       deraadt   253: {
                    254:        if (mode & (rw<<PUBLIC))
                    255:                return (1);
                    256:        if (mode & (rw<<PRIVATE))
                    257:                return (1);
                    258:        return ((mode & (rw<<ROOT)) && getuid() == 0);
                    259: }
                    260:
                    261: static value_t *
1.11      deraadt   262: vlookup(char *s)
1.1       deraadt   263: {
1.7       millert   264:        value_t *p;
1.1       deraadt   265:
                    266:        for (p = vtable; p->v_name; p++)
                    267:                if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
                    268:                        return (p);
                    269:        return (NULL);
                    270: }
                    271:
1.13      moritz    272: static char *
1.11      deraadt   273: vinterp(char *s, int stop)
1.1       deraadt   274: {
1.7       millert   275:        char *p = s, c;
1.1       deraadt   276:        int num;
                    277:
1.11      deraadt   278:        while ((c = *s++) && c != stop) {
1.1       deraadt   279:                switch (c) {
                    280:
                    281:                case '^':
                    282:                        if (*s)
                    283:                                *p++ = *s++ - 0100;
                    284:                        else
                    285:                                *p++ = c;
                    286:                        break;
                    287:
                    288:                case '\\':
                    289:                        num = 0;
                    290:                        c = *s++;
                    291:                        if (c >= '0' && c <= '7')
                    292:                                num = (num<<3)+(c-'0');
                    293:                        else {
1.7       millert   294:                                char *q = "n\nr\rt\tb\bf\f";
1.1       deraadt   295:
                    296:                                for (; *q; q++)
                    297:                                        if (c == *q++) {
                    298:                                                *p++ = *q;
                    299:                                                goto cont;
                    300:                                        }
                    301:                                *p++ = c;
                    302:                        cont:
                    303:                                break;
                    304:                        }
                    305:                        if ((c = *s++) >= '0' && c <= '7') {
                    306:                                num = (num<<3)+(c-'0');
                    307:                                if ((c = *s++) >= '0' && c <= '7')
                    308:                                        num = (num<<3)+(c-'0');
                    309:                                else
                    310:                                        s--;
                    311:                        } else
                    312:                                s--;
                    313:                        *p++ = num;
                    314:                        break;
                    315:
                    316:                default:
                    317:                        *p++ = c;
                    318:                }
1.11      deraadt   319:        }
1.1       deraadt   320:        *p = '\0';
                    321:        return (c == stop ? s-1 : NULL);
                    322: }
                    323:
                    324: /*
                    325:  * assign variable s with value v (for NUMBER or STRING or CHAR types)
                    326:  */
1.5       deraadt   327: int
1.11      deraadt   328: vstring(char *s, char *v)
1.1       deraadt   329: {
1.7       millert   330:        value_t *p;
1.1       deraadt   331:
1.11      deraadt   332:        p = vlookup(s);
1.1       deraadt   333:        if (p == 0)
                    334:                return (1);
                    335:        if (p->v_type&NUMBER)
1.11      deraadt   336:                vassign(p, (char *)atoi(v));
1.1       deraadt   337:        else {
                    338:                if (strcmp(s, "record") == 0)
                    339:                        v = expand(v);
                    340:                vassign(p, v);
                    341:        }
                    342:        return (0);
                    343: }