Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtksh / genlib.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: genlib.c /main/5 1996/09/27 19:01:37 drk $ */
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 #include "stdio.h"
34 #include <sys/types.h>
35
36 #ifdef DYNLIB
37 #include <dlfcn.h>
38 #endif
39 #ifdef HPUX_DYNLIB
40 #include <dl.h>
41 #endif
42
43 #include <string.h>
44 #include <search.h>
45 #include <ctype.h>
46 #include "docall.h"
47 #include "exksh.h"
48 #include "misc.h"
49 #include "xmdtksym.h"
50 #include "msgs.h"
51
52
53 static char * ReturnUsageMsg(
54                        int msgIndex);
55
56
57 struct libstruct *All_libs;
58 static int Nlibs;
59 int sh_errno;
60
61 static struct usage {
62         char *funcname;
63         int msgId;
64 } Xk_usage[] = {
65    "call",        0,
66    "define",      1,
67    "deflist",     2,
68    "deref",       3,
69    "field_comp",  4,
70    "field_get",   5,
71    "finddef",     6,
72    "findsym",     7,
73    "sizeof",      8,
74    "struct",      9,
75    "structlist", 10,
76    "symbolic",   11,
77    "typedef",    12,
78 };
79
80
81 static char *
82 ReturnUsageMsg(
83         int msgId )
84 {
85    char * errmsg;
86
87    switch (msgId)
88    {
89       case 0: errmsg = GETMESSAGE(9,1, "call [-F] [-n] [-r] function [arg] ... [++] [argModifier ...]");
90               return(errmsg);
91       case 1: errmsg = GETMESSAGE(9,2, "define [-R] name value");
92               return(errmsg);
93       case 2: errmsg = GETMESSAGE(9,3, "deflist [-p prefix] address");
94               return(errmsg);
95       case 3: errmsg = GETMESSAGE(9,4, "deref [-p] [-l] [-len] address [variable]");
96               return(errmsg);
97       case 4: errmsg = GETMESSAGE(9,5, "field_comp type address [criterion ...]");
98               return(errmsg);
99       case 5: errmsg = GETMESSAGE(9,6, "field_get [-v variable] type address [fieldName ...]");
100               return(errmsg);
101       case 6: errmsg = GETMESSAGE(9,7, "finddef definitionName [variable]");
102               return(errmsg);
103       case 7: errmsg = GETMESSAGE(9,8, "findsym symbolName [variable]");
104               return(errmsg);
105       case 8: errmsg = GETMESSAGE(9,9, "sizeof typeName [variable]");
106               return(errmsg);
107       case 9: errmsg = GETMESSAGE(9,10,"struct [-R] name fieldName[:type] ...");
108               return(errmsg);
109       case 10: errmsg = GETMESSAGE(9,11, "structlist [-i id] [-p prefix] address");
110               return(errmsg);
111       case 11: errmsg = GETMESSAGE(9,12, "symbolic [-m] -t type ... symbolic ...");
112               return(errmsg);
113       case 12: errmsg = GETMESSAGE(9,13,"typedef [-R] typeDescriptor typeName");
114               return(errmsg);
115    }
116
117    return("");
118 }
119
120 int
121 xk_usage(
122         char *funcname )
123 {
124    int i;
125    char * errhdr;
126    char * errmsg;
127
128    for (i = 0; i < sizeof(Xk_usage) / sizeof(struct usage); i++)
129    {
130       if (!funcname)
131       {
132          errmsg = strdup(ReturnUsageMsg(Xk_usage[i].msgId));
133          ALTPUTS(errmsg);
134          free(errmsg);
135       }
136       else if (!funcname || (strcmp(funcname, Xk_usage[i].funcname) == 0))
137       {
138          errhdr = strdup(GETMESSAGE(9,14, "Usage: %s"));
139          errmsg = strdup(ReturnUsageMsg(Xk_usage[i].msgId));
140          printerrf("", errhdr, errmsg, NULL, NULL, NULL, NULL, NULL, NULL, 
141                    NULL);
142          free(errhdr);
143          free(errmsg);
144          return(SH_SUCC);
145       }
146    }
147    return(funcname ? SH_FAIL : SH_SUCC);
148 }
149
150 unsigned long
151 fsym(
152         char *str,
153         int lib )
154 {
155 #if defined(STATICLIB) || ( !defined(DYNLIB) && !defined(HPUX_DYNLIB))
156         struct symarray dummy;
157 #endif
158 #if defined(DYNLIB)
159         int i, j;
160 #endif
161         void *found;
162
163 #if defined(STATICLIB) || (!defined(DYNLIB) && !defined(HPUX_DYNLIB))
164         dummy.str = str;       
165         
166 if ((found = (void *) bsearch((char *) &dummy, Symarray,
167         Symsize-1, sizeof(struct symarray), symcomp)) != NULL)
168                 return(((struct symarray *) found)->addr);
169 #endif /* STATICLIB */
170
171 #ifdef DYNLIB
172         for (i = 0; i < Nlibs; i++)
173                 for (j = 0; j < All_libs[i].nlibs; j++)
174                         if ((found = dlsym(All_libs[i].libs[j].handle,
175 str)) != NULL)
176                                 return((unsigned long) found);
177 #endif /* DYNLIB */
178 #ifdef HPUX_DYNLIB
179         {
180                 shl_t handle;
181
182                 handle = NULL;
183                 if ((shl_findsym(&handle, str, TYPE_PROCEDURE, &found)) == 0)
184                         return((unsigned long) found);
185                 if ((shl_findsym(&handle, str, TYPE_DATA, &found)) == 0)
186                         return((unsigned long) found);
187                 handle = PROG_HANDLE;
188                 if ((shl_findsym(&handle, str, TYPE_PROCEDURE, &found)) == 0)
189                         return((unsigned long) found);
190                 if ((shl_findsym(&handle, str, TYPE_DATA, &found)) == 0)
191                         return((unsigned long) found);
192         }
193 #endif /* HPUX_DYNLIB */
194         return(NULL);
195 }
196
197
198
199 int
200 do_findsym(
201         int argc,
202         char **argv )
203 {
204         unsigned long found;
205         struct symarray dummy;
206         char * errmsg;
207
208         if (argc == 1) {
209                 XK_USAGE(argv[0]);
210         }
211         if ((found = fsym(argv[1], -1)) != NULL) {
212                 if (argc >= 3) {
213                         char buf[50];
214
215                         sprintf(buf, "%s=0x%lx", argv[2], found);
216                         env_set(buf);
217                 }
218                 else {
219                         sprintf(xk_ret_buffer, "0x%lx", found);
220                         xk_ret_buf = xk_ret_buffer;
221                 }
222         }
223         else {
224                 errmsg = strdup(GetSharedMsg(DT_UNDEF_SYMBOL));
225                 printerrf(argv[0], errmsg, argv[1], NULL, NULL,
226                           NULL, NULL, NULL, NULL, NULL);
227                 free(errmsg);
228                 return(SH_FAIL);
229         }
230         return(SH_SUCC);
231 }
232
233 #ifndef SPRINTF_RET_LEN
234 /*
235  * SYSTEM V sprintf() returns the length of the buffer, other versions
236  * of the UNIX System don't.  So, if the SPRINTF_RET_LEN flag is not true,
237  * then we define an alternate function, lsprintf(), which has the SYSV
238  * behavior. Otherwise, lsprintf is #defined in exksh.h to be the
239  * same as sprintf.
240  */
241
242 int
243 lsprintf(
244         char *buf,
245         char *fmt,
246         unsigned long arg1,
247         unsigned long arg2,
248         unsigned long arg3,
249         unsigned long arg4,
250         unsigned long arg5,
251         unsigned long arg6,
252         unsigned long arg7 )
253
254 {
255         sprintf(buf, fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
256         return(strlen(buf));
257 }
258 #endif