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