home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / lbl / src / build.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.3 KB  |  123 lines

  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.  
  10. /* build.c:
  11.  *    routines to build new labels and label-types
  12.  */
  13.  
  14. #include    <lbl.h>
  15. #include    <ctype.h>
  16.  
  17. extern char    *def_format;
  18. extern char    *filename;
  19. extern long    fileline;
  20. extern type    *typetable;
  21.  
  22. char *alloc(nbytes)
  23.     us int    nbytes;
  24. {
  25.     char    *malloc();
  26.     char    *ptr = malloc(nbytes);
  27.  
  28.     if (ptr==NULL)
  29.         fatal("ran out of memory space");
  30.     return ptr;
  31. }
  32.  
  33. char *
  34. copy(str)
  35.     char    *str;
  36. {
  37.     char    *strcpy();
  38.     us int    strlen();
  39.  
  40.     char    *s = alloc(strlen(str)+1);
  41.  
  42.     return strcpy(s, str);
  43. }
  44.  
  45. addlbl(lbltype, lbllevel, lblname)
  46.     char    *lbltype;
  47.     char    *lbllevel;
  48.     char    *lblname;
  49. {
  50.     type    *tp = findtype(lbltype, 1);
  51.     label    *lp;
  52.     label    *last;
  53.     us int    bottom;
  54.     us int    indx;
  55.  
  56.     if (!isdigit(lbllevel[0]))
  57.     {
  58.         error("non-numeric index level");
  59.         return;
  60.     }
  61.     if ((bottom = atoi(lbllevel)) == 0 || bottom > NLEVELS)
  62.     {
  63.         error("index level must be in range 1-%u", NLEVELS);
  64.         return;
  65.     }
  66.     bottom--;
  67.  
  68.     ++(tp->t_levels[bottom]);
  69.     for (indx = bottom+1; indx < NLEVELS; indx++)
  70.         tp->t_levels[indx] = 0;
  71.  
  72.     if (strcmp(lblname, "*") != 0)
  73.     {
  74.         lp = findlabel(tp, lblname);
  75.  
  76.         if (lp != NULL)
  77.         {
  78.             error("redefinition of %s ignored", lblname);
  79.             return;
  80.         }
  81.         lp = (label *) alloc(sizeof(label));
  82.  
  83.         lp->l_name = copy(lblname);
  84.         lp->l_type = tp;
  85.         for (indx = 0; indx < bottom; indx++)
  86.             lp->l_levels[indx] = tp->t_levels[indx];
  87.         lp->l_levels[bottom] = tp->t_levels[bottom];
  88.         lp->l_bottom = bottom;
  89.         lp->l_file = filename;
  90.         lp->l_line = fileline;
  91.         lp->l_next = NULL;
  92.         /* Add to end of list, so that verbose listing comes out
  93.          * in order
  94.          */
  95.         if (tp->t_labels == NULL)
  96.             tp->t_labels = lp;
  97.         else
  98.         {
  99.             for (last = tp->t_labels; last->l_next != NULL;
  100.                             last = last->l_next)
  101.                 ;
  102.             last->l_next = lp;
  103.         }
  104.     }
  105. }
  106.  
  107. type *
  108. addtype(name)
  109.     char    *name;
  110. {
  111.     type        *tp = (type *) alloc(sizeof(type));
  112.     us int        indx;
  113.  
  114.     tp->t_name = copy(name);
  115.     for (indx = 0; indx < NLEVELS; indx++)
  116.         tp->t_levels[indx] = 0;
  117.     tp->t_format = def_format;
  118.     tp->t_labels = NULL;
  119.     tp->t_next = typetable;
  120.     typetable = tp;
  121.     return tp;
  122. }
  123.