xref: /original-bsd/usr.bin/mail/vars.c (revision fbed46ce)
1 #
2 
3 #include "rcv.h"
4 
5 /*
6  * Mail -- a mail program
7  *
8  * Variable handling stuff.
9  */
10 
11 static char *SccsId = "@(#)vars.c	2.2 09/08/81";
12 
13 /*
14  * Assign a value to a variable.
15  */
16 
17 assign(name, value)
18 	char name[], value[];
19 {
20 	register struct var *vp;
21 	register int h;
22 
23 	h = hash(name);
24 	vp = lookup(name);
25 	if (vp == NOVAR) {
26 		vp = (struct var *) calloc(sizeof *vp, 1);
27 		vp->v_name = vcopy(name);
28 		vp->v_link = variables[h];
29 		variables[h] = vp;
30 	}
31 	else
32 		vfree(vp->v_value);
33 	vp->v_value = vcopy(value);
34 }
35 
36 /*
37  * Free up a variable string.  We do not bother to allocate
38  * strings whose value is "" since they are expected to be frequent.
39  * Thus, we cannot free same!
40  */
41 
42 vfree(cp)
43 	register char *cp;
44 {
45 	if (!equal(cp, ""))
46 		cfree(cp);
47 }
48 
49 /*
50  * Copy a variable value into permanent (ie, not collected after each
51  * command) space.  Do not bother to alloc space for ""
52  */
53 
54 char *
55 vcopy(str)
56 	char str[];
57 {
58 	register char *top, *cp, *cp2;
59 
60 	if (equal(str, ""))
61 		return("");
62 	top = calloc(strlen(str)+1, 1);
63 	cp = top;
64 	cp2 = str;
65 	while (*cp++ = *cp2++)
66 		;
67 	return(top);
68 }
69 
70 /*
71  * Get the value of a variable and return it.
72  * Look in the environment if its not available locally.
73  */
74 
75 char *
76 value(name)
77 	char name[];
78 {
79 	register struct var *vp;
80 
81 	if ((vp = lookup(name)) == NOVAR)
82 		return(getenv(name));
83 	return(vp->v_value);
84 }
85 
86 /*
87  * Locate a variable and return its variable
88  * node.
89  */
90 
91 struct var *
92 lookup(name)
93 	char name[];
94 {
95 	register struct var *vp;
96 	register int h;
97 
98 	h = hash(name);
99 	for (vp = variables[h]; vp != NOVAR; vp = vp->v_link)
100 		if (equal(vp->v_name, name))
101 			return(vp);
102 	return(NOVAR);
103 }
104 
105 /*
106  * Locate a group name and return it.
107  */
108 
109 struct grouphead *
110 findgroup(name)
111 	char name[];
112 {
113 	register struct grouphead *gh;
114 	register int h;
115 
116 	h = hash(name);
117 	for (gh = groups[h]; gh != NOGRP; gh = gh->g_link)
118 		if (equal(gh->g_name, name))
119 			return(gh);
120 	return(NOGRP);
121 }
122 
123 /*
124  * Print a group out on stdout
125  */
126 
127 printgroup(name)
128 	char name[];
129 {
130 	register struct grouphead *gh;
131 	register struct group *gp;
132 
133 	if ((gh = findgroup(name)) == NOGRP) {
134 		printf("\"%s\": not a group\n", name);
135 		return;
136 	}
137 	printf("%s\t", gh->g_name);
138 	for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link)
139 		printf(" %s", gp->ge_name);
140 	printf("\n");
141 }
142 
143 /*
144  * Hash the passed string and return an index into
145  * the variable or group hash table.
146  */
147 
148 hash(name)
149 	char name[];
150 {
151 	register int h;
152 	register char *cp;
153 
154 	for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
155 		;
156 	if (h < 0)
157 		h = -h;
158 	if (h < 0)
159 		h = 0;
160 	return(h % HSHSIZE);
161 }
162