1*10d63b7dSRichard Lowe /*
2*10d63b7dSRichard Lowe * CDDL HEADER START
3*10d63b7dSRichard Lowe *
4*10d63b7dSRichard Lowe * The contents of this file are subject to the terms of the
5*10d63b7dSRichard Lowe * Common Development and Distribution License (the "License").
6*10d63b7dSRichard Lowe * You may not use this file except in compliance with the License.
7*10d63b7dSRichard Lowe *
8*10d63b7dSRichard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10d63b7dSRichard Lowe * or http://www.opensolaris.org/os/licensing.
10*10d63b7dSRichard Lowe * See the License for the specific language governing permissions
11*10d63b7dSRichard Lowe * and limitations under the License.
12*10d63b7dSRichard Lowe *
13*10d63b7dSRichard Lowe * When distributing Covered Code, include this CDDL HEADER in each
14*10d63b7dSRichard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10d63b7dSRichard Lowe * If applicable, add the following below this CDDL HEADER, with the
16*10d63b7dSRichard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
17*10d63b7dSRichard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
18*10d63b7dSRichard Lowe *
19*10d63b7dSRichard Lowe * CDDL HEADER END
20*10d63b7dSRichard Lowe */
21*10d63b7dSRichard Lowe /*
22*10d63b7dSRichard Lowe * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
23*10d63b7dSRichard Lowe * Use is subject to license terms.
24*10d63b7dSRichard Lowe */
25*10d63b7dSRichard Lowe
26*10d63b7dSRichard Lowe /*
27*10d63b7dSRichard Lowe * macro.cc
28*10d63b7dSRichard Lowe *
29*10d63b7dSRichard Lowe * Handle expansion of make macros
30*10d63b7dSRichard Lowe */
31*10d63b7dSRichard Lowe
32*10d63b7dSRichard Lowe /*
33*10d63b7dSRichard Lowe * Included files
34*10d63b7dSRichard Lowe */
35*10d63b7dSRichard Lowe #include <mk/defs.h>
36*10d63b7dSRichard Lowe #include <mksh/macro.h> /* getvar(), expand_value() */
37*10d63b7dSRichard Lowe #include <mksh/misc.h> /* getmem() */
38*10d63b7dSRichard Lowe
39*10d63b7dSRichard Lowe /*
40*10d63b7dSRichard Lowe * Defined macros
41*10d63b7dSRichard Lowe */
42*10d63b7dSRichard Lowe
43*10d63b7dSRichard Lowe /*
44*10d63b7dSRichard Lowe * typedefs & structs
45*10d63b7dSRichard Lowe */
46*10d63b7dSRichard Lowe
47*10d63b7dSRichard Lowe /*
48*10d63b7dSRichard Lowe * Static variables
49*10d63b7dSRichard Lowe */
50*10d63b7dSRichard Lowe
51*10d63b7dSRichard Lowe /*
52*10d63b7dSRichard Lowe * File table of contents
53*10d63b7dSRichard Lowe */
54*10d63b7dSRichard Lowe
55*10d63b7dSRichard Lowe void
setvar_append(register Name name,register Name value)56*10d63b7dSRichard Lowe setvar_append(register Name name, register Name value)
57*10d63b7dSRichard Lowe {
58*10d63b7dSRichard Lowe register Property macro_apx = get_prop(name->prop, macro_append_prop);
59*10d63b7dSRichard Lowe register Property macro = get_prop(name->prop, macro_prop);
60*10d63b7dSRichard Lowe int length;
61*10d63b7dSRichard Lowe String_rec destination;
62*10d63b7dSRichard Lowe wchar_t buffer[STRING_BUFFER_LENGTH];
63*10d63b7dSRichard Lowe register Chain chain;
64*10d63b7dSRichard Lowe Name val = NULL;
65*10d63b7dSRichard Lowe
66*10d63b7dSRichard Lowe if(macro_apx == NULL) {
67*10d63b7dSRichard Lowe macro_apx = append_prop(name, macro_append_prop);
68*10d63b7dSRichard Lowe if(macro != NULL) {
69*10d63b7dSRichard Lowe macro_apx->body.macro_appendix.value = macro->body.macro.value;
70*10d63b7dSRichard Lowe }
71*10d63b7dSRichard Lowe }
72*10d63b7dSRichard Lowe
73*10d63b7dSRichard Lowe val = macro_apx->body.macro_appendix.value_to_append;
74*10d63b7dSRichard Lowe
75*10d63b7dSRichard Lowe INIT_STRING_FROM_STACK(destination, buffer);
76*10d63b7dSRichard Lowe buffer[0] = 0;
77*10d63b7dSRichard Lowe if (val != NULL) {
78*10d63b7dSRichard Lowe APPEND_NAME(val,
79*10d63b7dSRichard Lowe &destination,
80*10d63b7dSRichard Lowe (int) val->hash.length);
81*10d63b7dSRichard Lowe if (value != NULL) {
82*10d63b7dSRichard Lowe MBTOWC(wcs_buffer, " ");
83*10d63b7dSRichard Lowe append_char(wcs_buffer[0], &destination);
84*10d63b7dSRichard Lowe }
85*10d63b7dSRichard Lowe }
86*10d63b7dSRichard Lowe if (value != NULL) {
87*10d63b7dSRichard Lowe APPEND_NAME(value,
88*10d63b7dSRichard Lowe &destination,
89*10d63b7dSRichard Lowe (int) value->hash.length);
90*10d63b7dSRichard Lowe }
91*10d63b7dSRichard Lowe value = GETNAME(destination.buffer.start, FIND_LENGTH);
92*10d63b7dSRichard Lowe if (destination.free_after_use) {
93*10d63b7dSRichard Lowe retmem(destination.buffer.start);
94*10d63b7dSRichard Lowe }
95*10d63b7dSRichard Lowe macro_apx->body.macro_appendix.value_to_append = value;
96*10d63b7dSRichard Lowe
97*10d63b7dSRichard Lowe SETVAR(name, empty_name, true);
98*10d63b7dSRichard Lowe }
99*10d63b7dSRichard Lowe
100*10d63b7dSRichard Lowe /*
101*10d63b7dSRichard Lowe * setvar_envvar()
102*10d63b7dSRichard Lowe *
103*10d63b7dSRichard Lowe * This function scans the list of environment variables that have
104*10d63b7dSRichard Lowe * dynamic values and sets them.
105*10d63b7dSRichard Lowe *
106*10d63b7dSRichard Lowe * Parameters:
107*10d63b7dSRichard Lowe *
108*10d63b7dSRichard Lowe * Global variables used:
109*10d63b7dSRichard Lowe * envvar A list of environment vars with $ in value
110*10d63b7dSRichard Lowe */
111*10d63b7dSRichard Lowe void
setvar_envvar(void)112*10d63b7dSRichard Lowe setvar_envvar(void)
113*10d63b7dSRichard Lowe {
114*10d63b7dSRichard Lowe wchar_t buffer[STRING_BUFFER_LENGTH];
115*10d63b7dSRichard Lowe int length;
116*10d63b7dSRichard Lowe register char *mbs, *tmp_mbs_buffer = NULL;
117*10d63b7dSRichard Lowe register char *env, *tmp_mbs_buffer2 = NULL;
118*10d63b7dSRichard Lowe Envvar p;
119*10d63b7dSRichard Lowe String_rec value;
120*10d63b7dSRichard Lowe
121*10d63b7dSRichard Lowe for (p = envvar; p != NULL; p = p->next) {
122*10d63b7dSRichard Lowe if (p->already_put
123*10d63b7dSRichard Lowe ) {
124*10d63b7dSRichard Lowe continue;
125*10d63b7dSRichard Lowe }
126*10d63b7dSRichard Lowe INIT_STRING_FROM_STACK(value, buffer);
127*10d63b7dSRichard Lowe expand_value(p->value, &value, false);
128*10d63b7dSRichard Lowe if ((length = wcslen(value.buffer.start)) >= MAXPATHLEN) {
129*10d63b7dSRichard Lowe mbs = tmp_mbs_buffer = getmem((length + 1) * MB_LEN_MAX);
130*10d63b7dSRichard Lowe (void) wcstombs(mbs,
131*10d63b7dSRichard Lowe value.buffer.start,
132*10d63b7dSRichard Lowe (length + 1) * MB_LEN_MAX);
133*10d63b7dSRichard Lowe } else {
134*10d63b7dSRichard Lowe mbs = mbs_buffer;
135*10d63b7dSRichard Lowe WCSTOMBS(mbs, value.buffer.start);
136*10d63b7dSRichard Lowe }
137*10d63b7dSRichard Lowe length = 2 + strlen(p->name->string_mb) + strlen(mbs);
138*10d63b7dSRichard Lowe if (!p->already_put || length > (MAXPATHLEN * MB_LEN_MAX)) {
139*10d63b7dSRichard Lowe env = tmp_mbs_buffer2 = getmem(length);
140*10d63b7dSRichard Lowe } else {
141*10d63b7dSRichard Lowe env = mbs_buffer2;
142*10d63b7dSRichard Lowe }
143*10d63b7dSRichard Lowe (void) sprintf(env,
144*10d63b7dSRichard Lowe "%s=%s",
145*10d63b7dSRichard Lowe p->name->string_mb,
146*10d63b7dSRichard Lowe mbs);
147*10d63b7dSRichard Lowe if (!p->already_put) {
148*10d63b7dSRichard Lowe (void) putenv(env);
149*10d63b7dSRichard Lowe p->already_put = true;
150*10d63b7dSRichard Lowe if (p->env_string) {
151*10d63b7dSRichard Lowe retmem_mb(p->env_string);
152*10d63b7dSRichard Lowe }
153*10d63b7dSRichard Lowe p->env_string = env;
154*10d63b7dSRichard Lowe tmp_mbs_buffer2 = NULL; // We should not return this memory now
155*10d63b7dSRichard Lowe }
156*10d63b7dSRichard Lowe if (tmp_mbs_buffer2) {
157*10d63b7dSRichard Lowe retmem_mb(tmp_mbs_buffer2);
158*10d63b7dSRichard Lowe tmp_mbs_buffer2 = NULL;
159*10d63b7dSRichard Lowe }
160*10d63b7dSRichard Lowe if (tmp_mbs_buffer) {
161*10d63b7dSRichard Lowe retmem_mb(tmp_mbs_buffer);
162*10d63b7dSRichard Lowe tmp_mbs_buffer = NULL;
163*10d63b7dSRichard Lowe }
164*10d63b7dSRichard Lowe }
165*10d63b7dSRichard Lowe }
166*10d63b7dSRichard Lowe
167*10d63b7dSRichard Lowe
168