genlib.c 6.9 KB

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