define.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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: define.c /main/4 1995/11/01 15:51:03 rswiston $ */
  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 "exksh.h" /* which includes sys/types.h */
  32. #include <sys/param.h>
  33. #include <string.h>
  34. #include <search.h>
  35. #include <ctype.h>
  36. #include "misc.h"
  37. #include "docall.h"
  38. #include "basetbl.h"
  39. #include "msgs.h"
  40. static growdef( void ) ;
  41. static int add_deflist(
  42. struct symarray *defptr,
  43. char *prefix) ;
  44. static def_init( void ) ;
  45. static struct symarray *Dyndef = NULL;
  46. static int Ndyndef = 0;
  47. static int Sdyndef = 0;
  48. static char defInited = 0;
  49. static char use[] = "0x%x";
  50. static char use2[] = "%s=0x%x";
  51. struct deflist {
  52. char *prefix;
  53. int size;
  54. struct symarray *defs;
  55. };
  56. struct deflist *Deflist = NULL;
  57. int Ndeflist;
  58. static
  59. growdef( void )
  60. {
  61. int i;
  62. if (!defInited)
  63. def_init();
  64. if (!(Dyndef = (struct symarray *) realloc(Dyndef, (Sdyndef + 20) *
  65. sizeof(struct symarray))))
  66. {
  67. return(SH_FAIL);
  68. }
  69. Deflist->defs = Dyndef;
  70. memset(((char *) Dyndef) + Sdyndef * sizeof(struct symarray), '\0',
  71. 20 * sizeof(struct symarray));
  72. Sdyndef += 20;
  73. }
  74. int
  75. do_define(
  76. int argc,
  77. char **argv )
  78. {
  79. int i, argstart, redo;
  80. char *name;
  81. struct symarray *found, dummy;
  82. if (!defInited)
  83. def_init();
  84. if (argc > 1 && C_PAIR(argv[1], '-', 'R'))
  85. {
  86. redo = 0;
  87. argstart = 2;
  88. }
  89. else
  90. {
  91. argstart = 1;
  92. redo = 1;
  93. }
  94. if ((argstart +1) >= argc)
  95. XK_USAGE(argv[0]);
  96. name = argv[argstart++];
  97. dummy.str = name;
  98. found = (struct symarray *) bsearch((char *) &dummy, Dyndef, Ndyndef,
  99. sizeof(struct symarray), symcomp);
  100. if (found)
  101. {
  102. if (!redo)
  103. return(SH_SUCC);
  104. i = found - Dyndef;
  105. }
  106. else
  107. {
  108. if (Sdyndef == Ndyndef)
  109. growdef();
  110. Ndyndef++;
  111. if (Ndyndef > 1)
  112. for (i = Ndyndef - 1; i > 0; i--)
  113. {
  114. if (strcmp(name, Dyndef[i - 1].str) >= 0)
  115. break;
  116. Dyndef[i] = Dyndef[i - 1];
  117. }
  118. else
  119. i = 0;
  120. Dyndef[i].str = strdup(name);
  121. Deflist->size++;
  122. }
  123. RIF(xk_par_int(argv + argstart, &Dyndef[i].addr, NULL));
  124. return(SH_SUCC);
  125. }
  126. int
  127. fdef(
  128. char *str,
  129. unsigned long *val )
  130. {
  131. struct symarray *found, dummy;
  132. int i;
  133. dummy.str = str;
  134. if (!Deflist)
  135. return(0);
  136. for (i = 0; i < Ndeflist; i++)
  137. {
  138. if (Deflist[i].defs)
  139. {
  140. if (Deflist[i].size < 0)
  141. {
  142. found = (struct symarray *) lfind((char *) &dummy, Deflist[i].defs,
  143. (unsigned int *) &Deflist[i].size, sizeof(struct symarray),
  144. symcomp);
  145. }
  146. else
  147. {
  148. found = (struct symarray *) bsearch((char *) &dummy,
  149. Deflist[i].defs, Deflist[i].size, sizeof(struct symarray),
  150. symcomp);
  151. }
  152. if (found != NULL)
  153. {
  154. *val = found->addr;
  155. return(1);
  156. }
  157. }
  158. }
  159. return(0);
  160. }
  161. int
  162. do_deflist(
  163. int argc,
  164. char **argv )
  165. {
  166. int i, j;
  167. char *prefix = NULL;
  168. struct symarray *defptr = NULL;
  169. char * errmsg;
  170. for (i = 1; (i < argc) && argv[i]; i++)
  171. {
  172. if (argv[i][0] == '-')
  173. {
  174. for (j = 1; argv[i][j]; j++)
  175. {
  176. switch(argv[i][j])
  177. {
  178. case 'p':
  179. {
  180. if (argv[i][j + 1])
  181. {
  182. prefix = argv[i] + j;
  183. j += strlen(prefix) - 2;
  184. }
  185. else
  186. {
  187. prefix = argv[++i];
  188. j = strlen(prefix) - 1;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. else
  195. {
  196. if ((defptr = (struct symarray *) getaddr(argv[i])) == NULL)
  197. {
  198. errmsg=strdup(GETMESSAGE(3,1,
  199. "Unable to locate the definition list '%s'"));
  200. printerrf(argv[0], errmsg, argv[i], NULL, NULL,
  201. NULL, NULL, NULL, NULL, NULL);
  202. free(errmsg);
  203. return(SH_FAIL);
  204. }
  205. }
  206. }
  207. if (defptr == NULL)
  208. {
  209. XK_USAGE(argv[0]);
  210. }
  211. for (i = 0; i < Ndeflist; i++)
  212. if ((Deflist[i].defs == defptr) &&
  213. (!prefix || (strcmp(Deflist[i].prefix, prefix) == 0)))
  214. {
  215. return(SH_SUCC);
  216. }
  217. return(add_deflist(defptr, prefix));
  218. }
  219. static int
  220. add_deflist(
  221. struct symarray *defptr,
  222. char *prefix )
  223. {
  224. int i;
  225. if (!Deflist)
  226. {
  227. Deflist = (struct deflist *) malloc((Ndeflist + 1) *
  228. sizeof(struct deflist));
  229. }
  230. else
  231. {
  232. Deflist = (struct deflist *) realloc(Deflist, (Ndeflist + 1) *
  233. sizeof(struct deflist));
  234. }
  235. if (!Deflist)
  236. return(SH_FAIL);
  237. Deflist[Ndeflist].defs = defptr;
  238. Deflist[Ndeflist].prefix = strdup(prefix);
  239. if (!defptr[0].str)
  240. Deflist[Ndeflist].size = 0;
  241. else
  242. {
  243. for (i = 1; defptr[i].str && defptr[i].str[0]; i++)
  244. if (symcomp((void *) (defptr + i), (void *) (defptr + i - 1)) < 0)
  245. break;
  246. if (!(defptr[i].str && defptr[i].str[0]))
  247. Deflist[Ndeflist].size = i;
  248. else
  249. Deflist[Ndeflist].size = -1;
  250. }
  251. Ndeflist++;
  252. return(SH_SUCC);
  253. }
  254. int
  255. do_finddef(
  256. int argc,
  257. char **argv )
  258. {
  259. unsigned long found;
  260. struct symarray dummy;
  261. char * errmsg;
  262. if (argc < 2)
  263. XK_USAGE(argv[0]);
  264. if (fdef(argv[1], &found))
  265. {
  266. if (argc >= 3)
  267. {
  268. char buf[50];
  269. sprintf(buf, use2, argv[2], found);
  270. env_set(buf);
  271. }
  272. else
  273. {
  274. sprintf(xk_ret_buffer, use, found);
  275. xk_ret_buf = xk_ret_buffer;
  276. }
  277. return(SH_SUCC);
  278. }
  279. errmsg = strdup(GETMESSAGE(3, 2, "Unable to locate the define '%s'"));
  280. printerrf(argv[0], errmsg, argv[1], NULL, NULL, NULL,
  281. NULL, NULL, NULL, NULL);
  282. free(errmsg);
  283. return(SH_FAIL);
  284. }
  285. static
  286. def_init( void )
  287. {
  288. char * errhdr;
  289. char * errmsg;
  290. defInited = 1;
  291. if (!(Dyndef = (struct symarray *) malloc(20 * sizeof(struct symarray))))
  292. {
  293. errhdr = strdup(GetSharedMsg(DT_ERROR));
  294. errmsg = strdup(GetSharedMsg(DT_ALLOC_FAILURE));
  295. printerr(errhdr, errmsg, NULL);
  296. free(errhdr);
  297. free(errmsg);
  298. exit(1);
  299. }
  300. Dyndef[0].str = NULL;
  301. Sdyndef = 20;
  302. Ndyndef = 0;
  303. add_deflist(Dyndef, "dynamic");
  304. add_deflist(basedefs, "base");
  305. }