Link with C++ linker
[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 librararies 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 void *
70 xkhash_init(
71         int num )
72 {
73         return((void *) hashalloc(NULL,0));
74 }
75
76 void
77 xkhash_override(
78         Hash_table_t *tbl,
79         const char *name,
80         void *val )
81 {
82         hashput(tbl, name, val);
83 }
84
85 void *
86 xkhash_find(
87         Hash_table_t *tbl,
88         const char *name )
89 {
90         return(hashget(tbl, name));
91 }
92
93 void
94 xkhash_add(
95         Hash_table_t *tbl,
96         const char *name,
97         char *val )
98 {
99         hashput(tbl, name, val);
100 }
101
102 int
103 ksh_eval(
104         char *cmd )
105 {
106         sh_eval(sfopen(NIL(Sfile_t*),cmd,"s"),0);
107         sfsync(sh.outpool);
108         return(sh.exitval);
109 }
110
111 void
112 env_set_var(
113         char *var,
114         char *val )
115 {
116         register int len;
117         char tmp[512];
118         char *set = &tmp[0];
119
120         if ((len = strlen(var) + strlen(val?val:"") + 2) > sizeof(tmp)) /* 11/06 CHANGED */
121                 set = malloc(len);
122         strcpy(set, var);
123         strcat(set, "=");
124         strcat(set, val?val:"");                        /* 11/06 CHANGED */
125         env_set(set);
126         if (set != &tmp[0])
127                 free(set);
128 }
129
130 void
131 env_blank(
132         char *var )
133 {
134         env_set_var(var, "");
135 }
136
137 void
138 printerr(
139         char *cmd,
140         char *msg1,
141         char *msg2 )
142 {
143         if (msg1 == NULL)
144                 msg1 = "";
145         if (msg2 == NULL)
146                 msg2 = "";
147         if (cmd && (strlen(cmd) > 0))
148            printf( "%s: %s %s\n", cmd, msg1, msg2);
149         else
150            printf( "%s %s\n", msg1, msg2);
151 }
152
153 void
154 printerrf(
155         char *cmd,
156         char *fmt,
157         char *arg0,
158         char *arg1,
159         char *arg2,
160         char *arg3,
161         char *arg4,
162         char *arg5,
163         char *arg6,
164         char *arg7 )
165 {
166         char buf[2048];
167         if (arg0 == NULL)
168                 arg0 = "";
169         if (arg1 == NULL)
170                 arg1 = "";
171         if (arg2 == NULL)
172                 arg2 = "";
173         if (arg3 == NULL)
174                 arg3 = "";
175         if (arg4 == NULL)
176                 arg4 = "";
177         if (arg5 == NULL)
178                 arg5 = "";
179         if (arg6 == NULL)
180                 arg6 = "";
181         if (arg7 == NULL)
182                 arg7 = "";
183
184         sprintf(buf, fmt, arg0, arg1, arg2, arg3,arg4, arg5, arg6, arg7);
185         if (cmd && (strlen(cmd) > 0))
186            printf("%s: %s\n", cmd, buf);
187         else
188            printf("%s\n", buf);
189 }