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