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