build.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* (C) C.D.F. Miller, Heriot-Watt University, March 1984
  2. *
  3. * Permission is hereby given to reproduce or modify this
  4. * software freely, provided that this notice be retained,
  5. * and that no use be made of the software for commercial
  6. * purposes without the express written permission of the
  7. * author.
  8. */
  9. #include <lbl.h>
  10. #include <ctype.h>
  11. #include <stdlib.h>
  12. #include <bsd/string.h>
  13. #include <err.h>
  14. #include <sysexits.h>
  15. #include "build.h"
  16. #include "find.h"
  17. extern char *def_format;
  18. extern char *filename;
  19. extern long fileline;
  20. extern std::list<Type> typehead;
  21. static void *alloc(unsigned long nbytes);
  22. static void *
  23. alloc(unsigned long nbytes)
  24. {
  25. void *ptr = malloc(nbytes);
  26. if (ptr == NULL)
  27. errx(EX_OSERR, "%sran out of memory space", maybe_loc());
  28. return ptr;
  29. }
  30. char *
  31. copy(char *str)
  32. {
  33. char *s = (char *)alloc(strlen(str) + 1);
  34. strlcpy(s, str, strlen(str) + 1);
  35. return s;
  36. }
  37. void
  38. addlbl(char *lbltype, char *lbllevel, char *lblname)
  39. {
  40. Type *tp = findtype(lbltype, 1);
  41. Label *last_next;
  42. int bottom;
  43. int indx;
  44. if (!isdigit(lbllevel[0])) {
  45. warnx("%snon-numeric index level", maybe_loc());
  46. return;
  47. }
  48. if ((bottom = atoi(lbllevel)) <= 0 || bottom > NLEVELS) {
  49. warnx("%sindex level must be in range 1-%u", maybe_loc(),
  50. NLEVELS);
  51. return;
  52. }
  53. bottom--;
  54. ++(tp->t_levels[bottom]);
  55. for (indx = bottom + 1; indx < NLEVELS; indx++)
  56. tp->t_levels[indx] = 0;
  57. if (strcmp(lblname, "*") != 0) {
  58. Label *lp;
  59. lp = typeFindlabel(tp, lblname);
  60. if (lp != NULL) {
  61. warnx("%sredefinition of %s ignored", maybe_loc(),
  62. lblname);
  63. return;
  64. }
  65. lp = (Label *) alloc(sizeof(Label));
  66. lp->l_name = copy(lblname);
  67. lp->l_type = tp;
  68. for (indx = 0; indx < bottom; indx++)
  69. lp->l_levels[indx] = tp->t_levels[indx];
  70. lp->l_levels[bottom] = tp->t_levels[bottom];
  71. lp->l_bottom = bottom;
  72. lp->l_file = filename;
  73. lp->l_line = fileline;
  74. /*
  75. * Add to end of list, so that verbose listing comes out in
  76. * order
  77. */
  78. if (LIST_EMPTY(&(tp->labelhead)))
  79. LIST_INSERT_HEAD(&(tp->labelhead), lp, link);
  80. else {
  81. Label *last;
  82. last = LIST_FIRST(&(tp->labelhead));
  83. last_next = LIST_NEXT(last, link);
  84. while (last_next != NULL) {
  85. last = last_next;
  86. last_next = LIST_NEXT(last, link);
  87. }
  88. LIST_INSERT_AFTER(last, lp, link);
  89. }
  90. }
  91. }
  92. Type *
  93. addtype(char *name)
  94. {
  95. Type *tp = (Type *) alloc(sizeof(Type));
  96. us int indx;
  97. tp->t_name = copy(name);
  98. for (indx = 0; indx < NLEVELS; indx++)
  99. tp->t_levels[indx] = 0;
  100. tp->t_format = def_format;
  101. LIST_INIT(&(tp->labelhead));
  102. LIST_INSERT_HEAD(&typehead, tp, link);
  103. return tp;
  104. }