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 #include "rcv.h"
42*7c478bd9Sstevel@tonic-gate #include <locale.h>
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley
46*7c478bd9Sstevel@tonic-gate * mail program
47*7c478bd9Sstevel@tonic-gate *
48*7c478bd9Sstevel@tonic-gate * Variable handling stuff.
49*7c478bd9Sstevel@tonic-gate */
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate static struct var *lookup(char name[]);
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate /*
54*7c478bd9Sstevel@tonic-gate * Assign a value to a variable.
55*7c478bd9Sstevel@tonic-gate */
56*7c478bd9Sstevel@tonic-gate void
assign(char name[],char value[])57*7c478bd9Sstevel@tonic-gate assign(char name[], char value[])
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate register struct var *vp;
60*7c478bd9Sstevel@tonic-gate register int h;
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate if (name[0]=='-')
63*7c478bd9Sstevel@tonic-gate deassign(name+1);
64*7c478bd9Sstevel@tonic-gate else if (name[0]=='n' && name[1]=='o')
65*7c478bd9Sstevel@tonic-gate deassign(name+2);
66*7c478bd9Sstevel@tonic-gate else {
67*7c478bd9Sstevel@tonic-gate h = hash(name);
68*7c478bd9Sstevel@tonic-gate vp = lookup(name);
69*7c478bd9Sstevel@tonic-gate if (vp == NOVAR) {
70*7c478bd9Sstevel@tonic-gate if ((vp = (struct var *)
71*7c478bd9Sstevel@tonic-gate calloc(sizeof (*vp), 1)) == NULL)
72*7c478bd9Sstevel@tonic-gate panic("Out of memory");
73*7c478bd9Sstevel@tonic-gate vp->v_name = vcopy(name);
74*7c478bd9Sstevel@tonic-gate vp->v_link = variables[h];
75*7c478bd9Sstevel@tonic-gate variables[h] = vp;
76*7c478bd9Sstevel@tonic-gate } else
77*7c478bd9Sstevel@tonic-gate vfree(vp->v_value);
78*7c478bd9Sstevel@tonic-gate vp->v_value = vcopy(value);
79*7c478bd9Sstevel@tonic-gate /*
80*7c478bd9Sstevel@tonic-gate * for efficiency, intercept certain assignments here
81*7c478bd9Sstevel@tonic-gate */
82*7c478bd9Sstevel@tonic-gate if (strcmp(name, "prompt")==0)
83*7c478bd9Sstevel@tonic-gate prompt = vp->v_value;
84*7c478bd9Sstevel@tonic-gate else if (strcmp(name, "debug")==0)
85*7c478bd9Sstevel@tonic-gate debug = 1;
86*7c478bd9Sstevel@tonic-gate if (debug) fprintf(stderr, "assign(%s)=%s\n", vp->v_name, vp->v_value);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate int
deassign(register char * s)91*7c478bd9Sstevel@tonic-gate deassign(register char *s)
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate register struct var *vp, *vp2;
94*7c478bd9Sstevel@tonic-gate register int h;
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate if ((vp2 = lookup(s)) == NOVAR) {
97*7c478bd9Sstevel@tonic-gate if (!sourcing) {
98*7c478bd9Sstevel@tonic-gate printf(gettext("\"%s\": undefined variable\n"), s);
99*7c478bd9Sstevel@tonic-gate return(1);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate return(0);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate if (debug) fprintf(stderr, "deassign(%s)\n", s);
104*7c478bd9Sstevel@tonic-gate if (strcmp(s, "prompt")==0)
105*7c478bd9Sstevel@tonic-gate prompt = NOSTR;
106*7c478bd9Sstevel@tonic-gate else if (strcmp(s, "debug")==0)
107*7c478bd9Sstevel@tonic-gate debug = 0;
108*7c478bd9Sstevel@tonic-gate h = hash(s);
109*7c478bd9Sstevel@tonic-gate if (vp2 == variables[h]) {
110*7c478bd9Sstevel@tonic-gate variables[h] = variables[h]->v_link;
111*7c478bd9Sstevel@tonic-gate vfree(vp2->v_name);
112*7c478bd9Sstevel@tonic-gate vfree(vp2->v_value);
113*7c478bd9Sstevel@tonic-gate free(vp2);
114*7c478bd9Sstevel@tonic-gate return(0);
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
117*7c478bd9Sstevel@tonic-gate ;
118*7c478bd9Sstevel@tonic-gate vp->v_link = vp2->v_link;
119*7c478bd9Sstevel@tonic-gate vfree(vp2->v_name);
120*7c478bd9Sstevel@tonic-gate vfree(vp2->v_value);
121*7c478bd9Sstevel@tonic-gate free(vp2);
122*7c478bd9Sstevel@tonic-gate return(0);
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate * Free up a variable string. We do not bother to allocate
127*7c478bd9Sstevel@tonic-gate * strings whose value is "" since they are expected to be frequent.
128*7c478bd9Sstevel@tonic-gate * Thus, we cannot free same!
129*7c478bd9Sstevel@tonic-gate */
130*7c478bd9Sstevel@tonic-gate void
vfree(register char * cp)131*7c478bd9Sstevel@tonic-gate vfree(register char *cp)
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate if (!equal(cp, ""))
134*7c478bd9Sstevel@tonic-gate free(cp);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate /*
138*7c478bd9Sstevel@tonic-gate * Copy a variable value into permanent (ie, not collected after each
139*7c478bd9Sstevel@tonic-gate * command) space. Do not bother to alloc space for ""
140*7c478bd9Sstevel@tonic-gate */
141*7c478bd9Sstevel@tonic-gate
142*7c478bd9Sstevel@tonic-gate char *
vcopy(char str[])143*7c478bd9Sstevel@tonic-gate vcopy(char str[])
144*7c478bd9Sstevel@tonic-gate {
145*7c478bd9Sstevel@tonic-gate register char *top, *cp, *cp2;
146*7c478bd9Sstevel@tonic-gate
147*7c478bd9Sstevel@tonic-gate if (equal(str, ""))
148*7c478bd9Sstevel@tonic-gate return("");
149*7c478bd9Sstevel@tonic-gate if ((top = (char *)calloc(strlen(str)+1, 1)) == NULL)
150*7c478bd9Sstevel@tonic-gate panic("Out of memory");
151*7c478bd9Sstevel@tonic-gate cp = top;
152*7c478bd9Sstevel@tonic-gate cp2 = str;
153*7c478bd9Sstevel@tonic-gate while (*cp++ = *cp2++)
154*7c478bd9Sstevel@tonic-gate ;
155*7c478bd9Sstevel@tonic-gate return(top);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate /*
159*7c478bd9Sstevel@tonic-gate * Get the value of a variable and return it.
160*7c478bd9Sstevel@tonic-gate * Look in the environment if its not available locally.
161*7c478bd9Sstevel@tonic-gate */
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate char *
value(char name[])164*7c478bd9Sstevel@tonic-gate value(char name[])
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate register struct var *vp;
167*7c478bd9Sstevel@tonic-gate register char *cp;
168*7c478bd9Sstevel@tonic-gate
169*7c478bd9Sstevel@tonic-gate if ((vp = lookup(name)) == NOVAR)
170*7c478bd9Sstevel@tonic-gate cp = getenv(name);
171*7c478bd9Sstevel@tonic-gate else
172*7c478bd9Sstevel@tonic-gate cp = vp->v_value;
173*7c478bd9Sstevel@tonic-gate if (debug) fprintf(stderr, "value(%s)=%s\n", name, (cp)?cp:"");
174*7c478bd9Sstevel@tonic-gate return(cp);
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate /*
178*7c478bd9Sstevel@tonic-gate * Locate a variable and return its variable
179*7c478bd9Sstevel@tonic-gate * node.
180*7c478bd9Sstevel@tonic-gate */
181*7c478bd9Sstevel@tonic-gate
182*7c478bd9Sstevel@tonic-gate static struct var *
lookup(char name[])183*7c478bd9Sstevel@tonic-gate lookup(char name[])
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate register struct var *vp;
186*7c478bd9Sstevel@tonic-gate register int h;
187*7c478bd9Sstevel@tonic-gate
188*7c478bd9Sstevel@tonic-gate h = hash(name);
189*7c478bd9Sstevel@tonic-gate for (vp = variables[h]; vp != NOVAR; vp = vp->v_link)
190*7c478bd9Sstevel@tonic-gate if (equal(vp->v_name, name))
191*7c478bd9Sstevel@tonic-gate return(vp);
192*7c478bd9Sstevel@tonic-gate return(NOVAR);
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate
195*7c478bd9Sstevel@tonic-gate /*
196*7c478bd9Sstevel@tonic-gate * Locate a group name and return it.
197*7c478bd9Sstevel@tonic-gate */
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate struct grouphead *
findgroup(char name[])200*7c478bd9Sstevel@tonic-gate findgroup(char name[])
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate register struct grouphead *gh;
203*7c478bd9Sstevel@tonic-gate register int h;
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate h = hash(name);
206*7c478bd9Sstevel@tonic-gate for (gh = groups[h]; gh != NOGRP; gh = gh->g_link)
207*7c478bd9Sstevel@tonic-gate if (equal(gh->g_name, name))
208*7c478bd9Sstevel@tonic-gate return(gh);
209*7c478bd9Sstevel@tonic-gate return(NOGRP);
210*7c478bd9Sstevel@tonic-gate }
211*7c478bd9Sstevel@tonic-gate
212*7c478bd9Sstevel@tonic-gate /*
213*7c478bd9Sstevel@tonic-gate * Print a group out on stdout
214*7c478bd9Sstevel@tonic-gate */
215*7c478bd9Sstevel@tonic-gate void
printgroup(char name[])216*7c478bd9Sstevel@tonic-gate printgroup(char name[])
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate register struct grouphead *gh;
219*7c478bd9Sstevel@tonic-gate register struct mgroup *gp;
220*7c478bd9Sstevel@tonic-gate
221*7c478bd9Sstevel@tonic-gate if ((gh = findgroup(name)) == NOGRP) {
222*7c478bd9Sstevel@tonic-gate printf(gettext("\"%s\": not a group\n"), name);
223*7c478bd9Sstevel@tonic-gate return;
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate printf("%s\t", gh->g_name);
226*7c478bd9Sstevel@tonic-gate for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link)
227*7c478bd9Sstevel@tonic-gate printf(" %s", gp->ge_name);
228*7c478bd9Sstevel@tonic-gate printf("\n");
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate
231*7c478bd9Sstevel@tonic-gate /*
232*7c478bd9Sstevel@tonic-gate * Hash the passed string and return an index into
233*7c478bd9Sstevel@tonic-gate * the variable or group hash table.
234*7c478bd9Sstevel@tonic-gate */
235*7c478bd9Sstevel@tonic-gate
236*7c478bd9Sstevel@tonic-gate int
hash(char name[])237*7c478bd9Sstevel@tonic-gate hash(char name[])
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate register unsigned h;
240*7c478bd9Sstevel@tonic-gate register char *cp;
241*7c478bd9Sstevel@tonic-gate
242*7c478bd9Sstevel@tonic-gate for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
243*7c478bd9Sstevel@tonic-gate ;
244*7c478bd9Sstevel@tonic-gate return(h % HSHSIZE);
245*7c478bd9Sstevel@tonic-gate }
246