1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * Copyright 2002 Sun Microsystems, Inc. All rights reserved. 28*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate /* 32*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 33*7c478bd9Sstevel@tonic-gate * The Regents of the University of California 34*7c478bd9Sstevel@tonic-gate * All Rights Reserved 35*7c478bd9Sstevel@tonic-gate * 36*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 37*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 38*7c478bd9Sstevel@tonic-gate * contributors. 39*7c478bd9Sstevel@tonic-gate */ 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate #include "rcv.h" 44*7c478bd9Sstevel@tonic-gate #include <locale.h> 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate /* 47*7c478bd9Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley 48*7c478bd9Sstevel@tonic-gate * mail program 49*7c478bd9Sstevel@tonic-gate * 50*7c478bd9Sstevel@tonic-gate * Variable handling stuff. 51*7c478bd9Sstevel@tonic-gate */ 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate static struct var *lookup(char name[]); 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate /* 56*7c478bd9Sstevel@tonic-gate * Assign a value to a variable. 57*7c478bd9Sstevel@tonic-gate */ 58*7c478bd9Sstevel@tonic-gate void 59*7c478bd9Sstevel@tonic-gate assign(char name[], char value[]) 60*7c478bd9Sstevel@tonic-gate { 61*7c478bd9Sstevel@tonic-gate register struct var *vp; 62*7c478bd9Sstevel@tonic-gate register int h; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate if (name[0]=='-') 65*7c478bd9Sstevel@tonic-gate deassign(name+1); 66*7c478bd9Sstevel@tonic-gate else if (name[0]=='n' && name[1]=='o') 67*7c478bd9Sstevel@tonic-gate deassign(name+2); 68*7c478bd9Sstevel@tonic-gate else { 69*7c478bd9Sstevel@tonic-gate h = hash(name); 70*7c478bd9Sstevel@tonic-gate vp = lookup(name); 71*7c478bd9Sstevel@tonic-gate if (vp == NOVAR) { 72*7c478bd9Sstevel@tonic-gate if ((vp = (struct var *) 73*7c478bd9Sstevel@tonic-gate calloc(sizeof (*vp), 1)) == NULL) 74*7c478bd9Sstevel@tonic-gate panic("Out of memory"); 75*7c478bd9Sstevel@tonic-gate vp->v_name = vcopy(name); 76*7c478bd9Sstevel@tonic-gate vp->v_link = variables[h]; 77*7c478bd9Sstevel@tonic-gate variables[h] = vp; 78*7c478bd9Sstevel@tonic-gate } else 79*7c478bd9Sstevel@tonic-gate vfree(vp->v_value); 80*7c478bd9Sstevel@tonic-gate vp->v_value = vcopy(value); 81*7c478bd9Sstevel@tonic-gate /* 82*7c478bd9Sstevel@tonic-gate * for efficiency, intercept certain assignments here 83*7c478bd9Sstevel@tonic-gate */ 84*7c478bd9Sstevel@tonic-gate if (strcmp(name, "prompt")==0) 85*7c478bd9Sstevel@tonic-gate prompt = vp->v_value; 86*7c478bd9Sstevel@tonic-gate else if (strcmp(name, "debug")==0) 87*7c478bd9Sstevel@tonic-gate debug = 1; 88*7c478bd9Sstevel@tonic-gate if (debug) fprintf(stderr, "assign(%s)=%s\n", vp->v_name, vp->v_value); 89*7c478bd9Sstevel@tonic-gate } 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate int 93*7c478bd9Sstevel@tonic-gate deassign(register char *s) 94*7c478bd9Sstevel@tonic-gate { 95*7c478bd9Sstevel@tonic-gate register struct var *vp, *vp2; 96*7c478bd9Sstevel@tonic-gate register int h; 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate if ((vp2 = lookup(s)) == NOVAR) { 99*7c478bd9Sstevel@tonic-gate if (!sourcing) { 100*7c478bd9Sstevel@tonic-gate printf(gettext("\"%s\": undefined variable\n"), s); 101*7c478bd9Sstevel@tonic-gate return(1); 102*7c478bd9Sstevel@tonic-gate } 103*7c478bd9Sstevel@tonic-gate return(0); 104*7c478bd9Sstevel@tonic-gate } 105*7c478bd9Sstevel@tonic-gate if (debug) fprintf(stderr, "deassign(%s)\n", s); 106*7c478bd9Sstevel@tonic-gate if (strcmp(s, "prompt")==0) 107*7c478bd9Sstevel@tonic-gate prompt = NOSTR; 108*7c478bd9Sstevel@tonic-gate else if (strcmp(s, "debug")==0) 109*7c478bd9Sstevel@tonic-gate debug = 0; 110*7c478bd9Sstevel@tonic-gate h = hash(s); 111*7c478bd9Sstevel@tonic-gate if (vp2 == variables[h]) { 112*7c478bd9Sstevel@tonic-gate variables[h] = variables[h]->v_link; 113*7c478bd9Sstevel@tonic-gate vfree(vp2->v_name); 114*7c478bd9Sstevel@tonic-gate vfree(vp2->v_value); 115*7c478bd9Sstevel@tonic-gate free(vp2); 116*7c478bd9Sstevel@tonic-gate return(0); 117*7c478bd9Sstevel@tonic-gate } 118*7c478bd9Sstevel@tonic-gate for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link) 119*7c478bd9Sstevel@tonic-gate ; 120*7c478bd9Sstevel@tonic-gate vp->v_link = vp2->v_link; 121*7c478bd9Sstevel@tonic-gate vfree(vp2->v_name); 122*7c478bd9Sstevel@tonic-gate vfree(vp2->v_value); 123*7c478bd9Sstevel@tonic-gate free(vp2); 124*7c478bd9Sstevel@tonic-gate return(0); 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate /* 128*7c478bd9Sstevel@tonic-gate * Free up a variable string. We do not bother to allocate 129*7c478bd9Sstevel@tonic-gate * strings whose value is "" since they are expected to be frequent. 130*7c478bd9Sstevel@tonic-gate * Thus, we cannot free same! 131*7c478bd9Sstevel@tonic-gate */ 132*7c478bd9Sstevel@tonic-gate void 133*7c478bd9Sstevel@tonic-gate vfree(register char *cp) 134*7c478bd9Sstevel@tonic-gate { 135*7c478bd9Sstevel@tonic-gate if (!equal(cp, "")) 136*7c478bd9Sstevel@tonic-gate free(cp); 137*7c478bd9Sstevel@tonic-gate } 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate /* 140*7c478bd9Sstevel@tonic-gate * Copy a variable value into permanent (ie, not collected after each 141*7c478bd9Sstevel@tonic-gate * command) space. Do not bother to alloc space for "" 142*7c478bd9Sstevel@tonic-gate */ 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate char * 145*7c478bd9Sstevel@tonic-gate vcopy(char str[]) 146*7c478bd9Sstevel@tonic-gate { 147*7c478bd9Sstevel@tonic-gate register char *top, *cp, *cp2; 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate if (equal(str, "")) 150*7c478bd9Sstevel@tonic-gate return(""); 151*7c478bd9Sstevel@tonic-gate if ((top = (char *)calloc(strlen(str)+1, 1)) == NULL) 152*7c478bd9Sstevel@tonic-gate panic("Out of memory"); 153*7c478bd9Sstevel@tonic-gate cp = top; 154*7c478bd9Sstevel@tonic-gate cp2 = str; 155*7c478bd9Sstevel@tonic-gate while (*cp++ = *cp2++) 156*7c478bd9Sstevel@tonic-gate ; 157*7c478bd9Sstevel@tonic-gate return(top); 158*7c478bd9Sstevel@tonic-gate } 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate /* 161*7c478bd9Sstevel@tonic-gate * Get the value of a variable and return it. 162*7c478bd9Sstevel@tonic-gate * Look in the environment if its not available locally. 163*7c478bd9Sstevel@tonic-gate */ 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate char * 166*7c478bd9Sstevel@tonic-gate value(char name[]) 167*7c478bd9Sstevel@tonic-gate { 168*7c478bd9Sstevel@tonic-gate register struct var *vp; 169*7c478bd9Sstevel@tonic-gate register char *cp; 170*7c478bd9Sstevel@tonic-gate 171*7c478bd9Sstevel@tonic-gate if ((vp = lookup(name)) == NOVAR) 172*7c478bd9Sstevel@tonic-gate cp = getenv(name); 173*7c478bd9Sstevel@tonic-gate else 174*7c478bd9Sstevel@tonic-gate cp = vp->v_value; 175*7c478bd9Sstevel@tonic-gate if (debug) fprintf(stderr, "value(%s)=%s\n", name, (cp)?cp:""); 176*7c478bd9Sstevel@tonic-gate return(cp); 177*7c478bd9Sstevel@tonic-gate } 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate /* 180*7c478bd9Sstevel@tonic-gate * Locate a variable and return its variable 181*7c478bd9Sstevel@tonic-gate * node. 182*7c478bd9Sstevel@tonic-gate */ 183*7c478bd9Sstevel@tonic-gate 184*7c478bd9Sstevel@tonic-gate static struct var * 185*7c478bd9Sstevel@tonic-gate lookup(char name[]) 186*7c478bd9Sstevel@tonic-gate { 187*7c478bd9Sstevel@tonic-gate register struct var *vp; 188*7c478bd9Sstevel@tonic-gate register int h; 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate h = hash(name); 191*7c478bd9Sstevel@tonic-gate for (vp = variables[h]; vp != NOVAR; vp = vp->v_link) 192*7c478bd9Sstevel@tonic-gate if (equal(vp->v_name, name)) 193*7c478bd9Sstevel@tonic-gate return(vp); 194*7c478bd9Sstevel@tonic-gate return(NOVAR); 195*7c478bd9Sstevel@tonic-gate } 196*7c478bd9Sstevel@tonic-gate 197*7c478bd9Sstevel@tonic-gate /* 198*7c478bd9Sstevel@tonic-gate * Locate a group name and return it. 199*7c478bd9Sstevel@tonic-gate */ 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate struct grouphead * 202*7c478bd9Sstevel@tonic-gate findgroup(char name[]) 203*7c478bd9Sstevel@tonic-gate { 204*7c478bd9Sstevel@tonic-gate register struct grouphead *gh; 205*7c478bd9Sstevel@tonic-gate register int h; 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate h = hash(name); 208*7c478bd9Sstevel@tonic-gate for (gh = groups[h]; gh != NOGRP; gh = gh->g_link) 209*7c478bd9Sstevel@tonic-gate if (equal(gh->g_name, name)) 210*7c478bd9Sstevel@tonic-gate return(gh); 211*7c478bd9Sstevel@tonic-gate return(NOGRP); 212*7c478bd9Sstevel@tonic-gate } 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate /* 215*7c478bd9Sstevel@tonic-gate * Print a group out on stdout 216*7c478bd9Sstevel@tonic-gate */ 217*7c478bd9Sstevel@tonic-gate void 218*7c478bd9Sstevel@tonic-gate printgroup(char name[]) 219*7c478bd9Sstevel@tonic-gate { 220*7c478bd9Sstevel@tonic-gate register struct grouphead *gh; 221*7c478bd9Sstevel@tonic-gate register struct mgroup *gp; 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gate if ((gh = findgroup(name)) == NOGRP) { 224*7c478bd9Sstevel@tonic-gate printf(gettext("\"%s\": not a group\n"), name); 225*7c478bd9Sstevel@tonic-gate return; 226*7c478bd9Sstevel@tonic-gate } 227*7c478bd9Sstevel@tonic-gate printf("%s\t", gh->g_name); 228*7c478bd9Sstevel@tonic-gate for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link) 229*7c478bd9Sstevel@tonic-gate printf(" %s", gp->ge_name); 230*7c478bd9Sstevel@tonic-gate printf("\n"); 231*7c478bd9Sstevel@tonic-gate } 232*7c478bd9Sstevel@tonic-gate 233*7c478bd9Sstevel@tonic-gate /* 234*7c478bd9Sstevel@tonic-gate * Hash the passed string and return an index into 235*7c478bd9Sstevel@tonic-gate * the variable or group hash table. 236*7c478bd9Sstevel@tonic-gate */ 237*7c478bd9Sstevel@tonic-gate 238*7c478bd9Sstevel@tonic-gate int 239*7c478bd9Sstevel@tonic-gate hash(char name[]) 240*7c478bd9Sstevel@tonic-gate { 241*7c478bd9Sstevel@tonic-gate register unsigned h; 242*7c478bd9Sstevel@tonic-gate register char *cp; 243*7c478bd9Sstevel@tonic-gate 244*7c478bd9Sstevel@tonic-gate for (cp = name, h = 0; *cp; h = (h << 2) + *cp++) 245*7c478bd9Sstevel@tonic-gate ; 246*7c478bd9Sstevel@tonic-gate return(h % HSHSIZE); 247*7c478bd9Sstevel@tonic-gate } 248