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
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]
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
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
41 #pragma ident "%Z%%M% %I% %E% SMI"
47 * mailx -- a modified version of a University of California at Berkeley
50 * Variable handling stuff.
53 static struct var
*lookup(char name
[]);
56 * Assign a value to a variable.
59 assign(char name
[], char value
[])
61 register struct var
*vp
;
66 else if (name
[0]=='n' && name
[1]=='o')
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
];
80 vp
->v_value
= vcopy(value
);
82 * for efficiency, intercept certain assignments here
84 if (strcmp(name
, "prompt")==0)
86 else if (strcmp(name
, "debug")==0)
88 if (debug
) fprintf(stderr
, "assign(%s)=%s\n", vp
->v_name
, vp
->v_value
);
93 deassign(register char *s
)
95 register struct var
*vp
, *vp2
;
98 if ((vp2
= lookup(s
)) == NOVAR
) {
100 printf(gettext("\"%s\": undefined variable\n"), s
);
105 if (debug
) fprintf(stderr
, "deassign(%s)\n", s
);
106 if (strcmp(s
, "prompt")==0)
108 else if (strcmp(s
, "debug")==0)
111 if (vp2
== variables
[h
]) {
112 variables
[h
] = variables
[h
]->v_link
;
118 for (vp
= variables
[h
]; vp
->v_link
!= vp2
; vp
= vp
->v_link
)
120 vp
->v_link
= vp2
->v_link
;
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!
133 vfree(register char *cp
)
140 * Copy a variable value into permanent (ie, not collected after each
141 * command) space. Do not bother to alloc space for ""
147 register char *top
, *cp
, *cp2
;
151 if ((top
= (char *)calloc(strlen(str
)+1, 1)) == NULL
)
152 panic("Out of memory");
155 while (*cp
++ = *cp2
++)
161 * Get the value of a variable and return it.
162 * Look in the environment if its not available locally.
168 register struct var
*vp
;
171 if ((vp
= lookup(name
)) == NOVAR
)
175 if (debug
) fprintf(stderr
, "value(%s)=%s\n", name
, (cp
)?cp
:"");
180 * Locate a variable and return its variable
187 register struct var
*vp
;
191 for (vp
= variables
[h
]; vp
!= NOVAR
; vp
= vp
->v_link
)
192 if (equal(vp
->v_name
, name
))
198 * Locate a group name and return it.
202 findgroup(char name
[])
204 register struct grouphead
*gh
;
208 for (gh
= groups
[h
]; gh
!= NOGRP
; gh
= gh
->g_link
)
209 if (equal(gh
->g_name
, name
))
215 * Print a group out on stdout
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
);
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
);
234 * Hash the passed string and return an index into
235 * the variable or group hash table.
244 for (cp
= name
, h
= 0; *cp
; h
= (h
<< 2) + *cp
++)