Merge branch 'master' into autotools-conversion
[oweals/cde.git] / cde / programs / dtksh / extra.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: extra.c /main/4 1995/11/01 15:54:55 rswiston $ */
24
25 /*      Copyright (c) 1991, 1992 UNIX System Laboratories, Inc. */
26 /*      All Rights Reserved     */
27
28 /*      THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF    */
29 /*      UNIX System Laboratories, Inc.                  */
30 /*      The copyright notice above does not evidence any       */
31 /*      actual or intended publication of such source code.    */
32
33
34 #include        "defs.h"
35 #include        "shell.h"
36 #include        "name.h"
37 #include        "stdio.h"
38 #include        "msgs.h"
39
40
41 void
42 env_set(
43         char *var )
44 {
45         (void)nv_open(var, sh.var_tree, NV_ASSIGN);
46 }
47
48 void
49 env_set_gbl(
50         char *vareqval )
51 {
52         env_set(vareqval);
53 }
54
55 char *
56 env_get(
57         char *var )
58 {
59         Namval_t *np;
60         char *val;
61
62         np = nv_search(var,sh.var_tree,0);
63         if (np && (val = nv_getval(np)))
64                 return(val);
65         return(NULL);
66 }
67
68
69 int
70 ksh_eval(
71         char *cmd )
72 {
73         sh_eval(sfopen(NIL(Sfile_t*),cmd,"s"),0);
74         sfsync(sh.outpool);
75         return(sh.exitval);
76 }
77
78 void
79 env_set_var(
80         char *var,
81         char *val )
82 {
83         int len;
84         char tmp[512];
85         char *set = &tmp[0];
86
87         if ((len = strlen(var) + strlen(val?val:"") + 2) > sizeof(tmp)) /* 11/06 CHANGED */
88                 set = malloc(len);
89         strcpy(set, var);
90         strcat(set, "=");
91         strcat(set, val?val:"");                        /* 11/06 CHANGED */
92         env_set(set);
93         if (set != &tmp[0])
94                 free(set);
95 }
96
97 void
98 env_blank(
99         char *var )
100 {
101         env_set_var(var, "");
102 }
103
104 void
105 printerr(
106         char *cmd,
107         char *msg1,
108         char *msg2 )
109 {
110         if (msg1 == NULL)
111                 msg1 = "";
112         if (msg2 == NULL)
113                 msg2 = "";
114         if (cmd && (strlen(cmd) > 0))
115            printf( "%s: %s %s\n", cmd, msg1, msg2);
116         else
117            printf( "%s %s\n", msg1, msg2);
118 }
119
120 void
121 printerrf(
122         char *cmd,
123         char *fmt,
124         char *arg0,
125         char *arg1,
126         char *arg2,
127         char *arg3,
128         char *arg4,
129         char *arg5,
130         char *arg6,
131         char *arg7 )
132 {
133         char buf[2048];
134         if (arg0 == NULL)
135                 arg0 = "";
136         if (arg1 == NULL)
137                 arg1 = "";
138         if (arg2 == NULL)
139                 arg2 = "";
140         if (arg3 == NULL)
141                 arg3 = "";
142         if (arg4 == NULL)
143                 arg4 = "";
144         if (arg5 == NULL)
145                 arg5 = "";
146         if (arg6 == NULL)
147                 arg6 = "";
148         if (arg7 == NULL)
149                 arg7 = "";
150
151         sprintf(buf, fmt, arg0, arg1, arg2, arg3,arg4, arg5, arg6, arg7);
152         if (cmd && (strlen(cmd) > 0))
153            printf("%s: %s\n", cmd, buf);
154         else
155            printf("%s\n", buf);
156 }