symbolic.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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: symbolic.c /main/4 1995/11/01 15:56:54 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 "name.h"
  31. #include "shell.h"
  32. #include "stdio.h"
  33. #include "exksh.h" /* which includes sys/types.h */
  34. #include "struct.h" /* which includes sys/types.h */
  35. #include <string.h>
  36. #include "msgs.h"
  37. struct symlist *Symlist = NULL;
  38. int Nsymlist;
  39. /*
  40. ** There is an implicit dirty trick going on here. It is effective,
  41. ** efficient and will work anywhere but it is tricky. A memtbl has
  42. ** strings in it. The fact that a byte-by-byte comparison is being
  43. ** done on a memtbl means that pointers are being compared. This
  44. ** means that EVERY UNIQUE MEMTBL SHOULD HAVE SOME UNIQUE FIELD (i.e.
  45. ** in this case, the string for the name field). If somebody uses
  46. ** an algorithm in do_struct() that saves string space (by seeing if
  47. ** the same string is lying around) this code will break and an ID
  48. ** field will be necessary to maintain uniqueness.
  49. */
  50. struct symlist *
  51. fsymbolic(
  52. struct memtbl *tbl )
  53. {
  54. int i;
  55. for (i = 0; i < Nsymlist; i++)
  56. if (memcmp(tbl, &Symlist[i].tbl, sizeof(struct memtbl)) == 0)
  57. return(Symlist + i);
  58. return(NULL);
  59. }
  60. int
  61. do_symbolic(
  62. int argc,
  63. char **argv )
  64. {
  65. int i, nsyms, isflag;
  66. unsigned long j;
  67. struct symarray syms[50];
  68. struct memtbl *tbl;
  69. char *p;
  70. char *type = NULL;
  71. char * errmsg;
  72. nsyms = 0;
  73. isflag = 0;
  74. for (i = 1; (i < argc) && argv[i]; i++) {
  75. if (argv[i][0] == '-') {
  76. for (j = 1; argv[i][j]; j++) {
  77. switch(argv[i][j]) {
  78. case 'm':
  79. isflag = 1;
  80. break;
  81. case 't':
  82. if (argv[i][j + 1])
  83. type = argv[i] + j + 1;
  84. else
  85. type = argv[++i];
  86. j = strlen(argv[i]) - 1;
  87. break;
  88. }
  89. }
  90. }
  91. else {
  92. syms[nsyms++].str = argv[i];
  93. if (nsyms == 50)
  94. break;
  95. }
  96. }
  97. if ((type == NULL) || (!nsyms)) {
  98. XK_USAGE(argv[0]);
  99. }
  100. if (p = strchr(type, '.')) {
  101. *p = '\0';
  102. if ((tbl = all_tbl_search(type, 0)) == NULL) {
  103. *p = '.';
  104. errmsg = strdup(GetSharedMsg(DT_UNDEF_TYPE));
  105. printerrf(argv[0], errmsg, type, NULL, NULL,
  106. NULL, NULL, NULL, NULL, NULL);
  107. free(errmsg);
  108. return(SH_FAIL);
  109. }
  110. if ((tbl = ffind(tbl, p + 1, NULL)) == NULL) {
  111. errmsg=strdup(GETMESSAGE(13,1,
  112. "Unable to locate a field named '%s' for the type '%s'"));
  113. printerrf(argv[0], errmsg, p + 1, type, NULL,
  114. NULL, NULL, NULL, NULL, NULL);
  115. free(errmsg);
  116. *p = '.';
  117. return(SH_FAIL);
  118. }
  119. *p = '.';
  120. }
  121. else if ((tbl = all_tbl_search(type, 0)) == NULL) {
  122. errmsg = strdup(GetSharedMsg(DT_UNDEF_TYPE));
  123. printerrf(argv[0], errmsg, type, NULL, NULL,
  124. NULL, NULL, NULL, NULL, NULL);
  125. free(errmsg);
  126. return(SH_FAIL);
  127. }
  128. for (i = 0; i < nsyms; i++) {
  129. if (!fdef(syms[i].str, &j)) {
  130. errmsg=strdup(GETMESSAGE(13,2,
  131. "The name '%s' has not been defined"));
  132. printerrf(argv[0], errmsg, syms[i].str,
  133. NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  134. free(errmsg);
  135. return(SH_FAIL);
  136. }
  137. syms[i].str = strdup(syms[i].str);
  138. syms[i].addr = j;
  139. }
  140. add_symbolic(isflag, tbl, syms, nsyms);
  141. return(SH_SUCC);
  142. }
  143. int
  144. add_symbolic(
  145. int isflag,
  146. struct memtbl *tbl,
  147. struct symarray *syms,
  148. int nsyms )
  149. {
  150. struct symlist *symptr;
  151. if ((symptr = fsymbolic(tbl)) == NULL) {
  152. if (!Symlist)
  153. Symlist = (struct symlist *) malloc((Nsymlist + 1) * sizeof(struct symlist));
  154. else
  155. Symlist = (struct symlist *) realloc(Symlist, (Nsymlist + 1) * sizeof(struct symlist));
  156. if (!Symlist)
  157. return(SH_FAIL);
  158. symptr = Symlist + Nsymlist;
  159. Nsymlist++;
  160. }
  161. else
  162. free(symptr->syms);
  163. symptr->tbl = *tbl;
  164. symptr->nsyms = nsyms;
  165. symptr->isflag = isflag;
  166. symptr->syms = (struct symarray *) malloc(nsyms * sizeof(struct symarray));
  167. memcpy(symptr->syms, syms, nsyms * sizeof(struct symarray));
  168. }