Annotation of researchv10no/lbin/mailx/vars.c, revision 1.1

1.1     ! root        1: #ident "@(#)vars.c     1.3 'attmail mail(1) command'"
        !             2: #ident "@(#)mailx:vars.c       1.3.1.1"
        !             3: /*     Copyright (c) 1984 AT&T */
        !             4: /*       All Rights Reserved   */
        !             5: 
        !             6: /*     THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T     */
        !             7: /*     The copyright notice above does not evidence any        */
        !             8: /*     actual or intended publication of such source code.     */
        !             9: 
        !            10: #ident "@(#)mailx:vars.c       1.3"
        !            11: 
        !            12: #include "rcv.h"
        !            13: 
        !            14: /*
        !            15:  * mailx -- a modified version of a University of California at Berkeley
        !            16:  *     mail program
        !            17:  *
        !            18:  * Variable handling stuff.
        !            19:  */
        !            20: 
        !            21: static struct var      *lookup();
        !            22: static void            vfree();
        !            23: 
        !            24: /*
        !            25:  * Assign a value to a variable.
        !            26:  */
        !            27: void
        !            28: assign(name, value)
        !            29:        char name[], value[];
        !            30: {
        !            31:        register struct var *vp;
        !            32:        register int h;
        !            33: 
        !            34:        if (name[0]=='-')
        !            35:                deassign(name+1);
        !            36:        else if (name[0]=='n' && name[1]=='o')
        !            37:                deassign(name+2);
        !            38:        else {
        !            39:                h = hash(name);
        !            40:                vp = lookup(name);
        !            41:                if (vp == NOVAR) {
        !            42:                        vp = (struct var *) calloc(sizeof *vp, 1);
        !            43:                        vp->v_name = vcopy(name);
        !            44:                        vp->v_link = variables[h];
        !            45:                        variables[h] = vp;
        !            46:                } else
        !            47:                        vfree(vp->v_value);
        !            48:                vp->v_value = vcopy(value);
        !            49:                /*
        !            50:                 * for efficiency, intercept certain assignments here
        !            51:                 */
        !            52:                if (strcmp(name, "prompt")==0)
        !            53:                        prompt = vp->v_value;
        !            54:                else if (strcmp(name, "debug")==0)
        !            55:                        debug = 1;
        !            56:                if (debug) fprintf(stderr, "assign(%s)=%s\n", vp->v_name, vp->v_value);
        !            57:        }
        !            58: }
        !            59: 
        !            60: deassign(s)
        !            61: register char *s;
        !            62: {
        !            63:        register struct var *vp, *vp2;
        !            64:        register int h;
        !            65: 
        !            66:        if ((vp2 = lookup(s)) == NOVAR) {
        !            67:                if (!sourcing) {
        !            68:                        printf("\"%s\": undefined variable\n", s);
        !            69:                        return(1);
        !            70:                }
        !            71:                return(0);
        !            72:        }
        !            73:        if (debug) fprintf(stderr, "deassign(%s)\n", s);
        !            74:        if (strcmp(s, "prompt")==0)
        !            75:                prompt = NOSTR;
        !            76:        else if (strcmp(s, "debug")==0)
        !            77:                debug = 0;
        !            78:        h = hash(s);
        !            79:        if (vp2 == variables[h]) {
        !            80:                variables[h] = variables[h]->v_link;
        !            81:                vfree(vp2->v_name);
        !            82:                vfree(vp2->v_value);
        !            83:                free(vp2);
        !            84:                return(0);
        !            85:        }
        !            86:        for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
        !            87:                ;
        !            88:        vp->v_link = vp2->v_link;
        !            89:        vfree(vp2->v_name);
        !            90:        vfree(vp2->v_value);
        !            91:        free(vp2);
        !            92:        return(0);
        !            93: }
        !            94: 
        !            95: /*
        !            96:  * Free up a variable string.  We do not bother to allocate
        !            97:  * strings whose value is "" since they are expected to be frequent.
        !            98:  * Thus, we cannot free same!
        !            99:  */
        !           100: static void
        !           101: vfree(cp)
        !           102:        register char *cp;
        !           103: {
        !           104:        if (!equal(cp, ""))
        !           105:                free(cp);
        !           106: }
        !           107: 
        !           108: /*
        !           109:  * Copy a variable value into permanent (ie, not collected after each
        !           110:  * command) space.  Do not bother to alloc space for ""
        !           111:  */
        !           112: 
        !           113: char *
        !           114: vcopy(str)
        !           115:        char str[];
        !           116: {
        !           117:        register char *top, *cp, *cp2;
        !           118: 
        !           119:        if (equal(str, ""))
        !           120:                return("");
        !           121:        if ((top = calloc(strlen(str)+1, 1)) == NULL)
        !           122:                panic("Out of memory");
        !           123:        cp = top;
        !           124:        cp2 = str;
        !           125:        while (*cp++ = *cp2++)
        !           126:                ;
        !           127:        return(top);
        !           128: }
        !           129: 
        !           130: /*
        !           131:  * Get the value of a variable and return it.
        !           132:  * Look in the environment if its not available locally.
        !           133:  */
        !           134: 
        !           135: char *
        !           136: value(name)
        !           137:        char name[];
        !           138: {
        !           139:        register struct var *vp;
        !           140:        register char *cp;
        !           141: 
        !           142:        if ((vp = lookup(name)) == NOVAR)
        !           143:                cp = getenv(name);
        !           144:        else
        !           145:                cp = vp->v_value;
        !           146:        if (debug) fprintf(stderr, "value(%s)=%s\n", name, (cp)?cp:"");
        !           147:        return(cp);
        !           148: }
        !           149: 
        !           150: /*
        !           151:  * Locate a variable and return its variable
        !           152:  * node.
        !           153:  */
        !           154: 
        !           155: static struct var *
        !           156: lookup(name)
        !           157:        char name[];
        !           158: {
        !           159:        register struct var *vp;
        !           160:        register int h;
        !           161: 
        !           162:        h = hash(name);
        !           163:        for (vp = variables[h]; vp != NOVAR; vp = vp->v_link)
        !           164:                if (equal(vp->v_name, name))
        !           165:                        return(vp);
        !           166:        return(NOVAR);
        !           167: }
        !           168: 
        !           169: /*
        !           170:  * Locate a group name and return it.
        !           171:  */
        !           172: 
        !           173: struct grouphead *
        !           174: findgroup(name)
        !           175:        char name[];
        !           176: {
        !           177:        register struct grouphead *gh;
        !           178:        register int h;
        !           179: 
        !           180:        h = hash(name);
        !           181:        for (gh = groups[h]; gh != NOGRP; gh = gh->g_link)
        !           182:                if (equal(gh->g_name, name))
        !           183:                        return(gh);
        !           184:        return(NOGRP);
        !           185: }
        !           186: 
        !           187: /*
        !           188:  * Print a group out on stdout
        !           189:  */
        !           190: void
        !           191: printgroup(name)
        !           192:        char name[];
        !           193: {
        !           194:        register struct grouphead *gh;
        !           195:        register struct mgroup *gp;
        !           196: 
        !           197:        if ((gh = findgroup(name)) == NOGRP) {
        !           198:                printf("\"%s\": not a group\n", name);
        !           199:                return;
        !           200:        }
        !           201:        printf("%s\t", gh->g_name);
        !           202:        for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link)
        !           203:                printf(" %s", gp->ge_name);
        !           204:        printf("\n");
        !           205: }
        !           206: 
        !           207: /*
        !           208:  * Hash the passed string and return an index into
        !           209:  * the variable or group hash table.
        !           210:  */
        !           211: 
        !           212: hash(name)
        !           213:        char name[];
        !           214: {
        !           215:        register int h;
        !           216:        register char *cp;
        !           217: 
        !           218:        for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
        !           219:                ;
        !           220:        if (h < 0)
        !           221:                h = -h;
        !           222:        if (h < 0)
        !           223:                h = 0;
        !           224:        return(h % HSHSIZE);
        !           225: }

unix.superglobalmegacorp.com

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