home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume9 / teco / part01 / te_utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-03-11  |  5.8 KB  |  251 lines

  1. /* TECO for Ultrix   Copyright 1986 Matt Fichtenbaum                        */
  2. /* This program and its components belong to GenRad Inc, Concord MA 01742    */
  3. /* They may be copied if this copyright notice is included                    */
  4.  
  5. /* te_utils.c utility subroutines  10/28/85 */
  6.  
  7. #include "te_defs.h"
  8.  
  9. /* routines to handle storage */
  10. /* get a buffcell */
  11. /* if there are no buffcells available, call malloc for more storage */
  12.  
  13. struct buffcell *get_bcell()
  14.     {
  15.     char *malloc();
  16.     struct buffcell *p;
  17.     int i;
  18.  
  19.     if (freebuff == NULL)
  20.         {
  21.         p = (struct buffcell *) malloc(BLOCKSIZE);
  22.         if (!p) ERROR(E_MEM);
  23.         else
  24.             {
  25.             freebuff =  p;                    /* take the given address as storage */
  26.             for (i = 0; i < (BLOCKSIZE/sizeof(struct buffcell)) - 1; i++)    /* for all cells in the new block */
  27.                 (p+i)->f = p+i+1;            /* chain the forward pointers together */
  28.             (p+i)->f = NULL;                /* make the last one's forward pointer NULL */
  29.             }
  30.         }
  31.  
  32.     p = freebuff;                /* cut out head of "free" list */
  33.     freebuff = freebuff->f;
  34.     p->f = p->b = NULL;
  35.     return(p);
  36.     }
  37.  
  38.  
  39. /* free a list of buffcells */
  40. free_blist(p)
  41.     struct buffcell *p;
  42.     {
  43.     struct buffcell *t;
  44.  
  45.     if (p != NULL)
  46.     {
  47.         for (t = p; t -> f != NULL; t = t -> f); /* find end of ret'd list */
  48.         t->f = freebuff;        /* put ret'd list at head of "free" list */
  49.         freebuff = p;
  50.         }
  51.     }
  52. /* free a list of buffcells to the "delayed free" list */
  53. dly_free_blist(p)
  54.     struct buffcell *p;
  55.     {
  56.     struct buffcell *t;
  57.  
  58.     if (p != NULL)
  59.     {
  60.         for (t = p; t -> f != NULL; t = t -> f); /* find end of ret'd list */
  61.         t->f = dly_freebuff;        /* put ret'd list at head of "free" list */
  62.         dly_freebuff = p;
  63.         }
  64.     }
  65.  
  66.  
  67.  
  68. /* get a cell */
  69. /* if there are no cells available, get a buffcell and make more */
  70.  
  71. struct qp *get_dcell()
  72.     {
  73.     struct qp *t;
  74.     int i;
  75.  
  76.     if (freedcell == NULL)
  77.         {
  78.         t = freedcell = (struct qp *) get_bcell();    /* get a buffcell */
  79.         for (i = 0; i < (sizeof(struct buffcell)/sizeof(struct qp)) - 1; i++)
  80.             {
  81.             (t+i)->f = t+i+1;    /* chain the fwd pointers together */
  82.             (t+i)->f = NULL;    /* make the last one's forward pointer NULL */
  83.             }
  84.         }
  85.  
  86.     t = freedcell;                /* cut out head of "free" list */
  87.     freedcell = (struct qp *) freedcell->f;
  88.     t->f =  NULL;
  89.     return(t);
  90.     }
  91.  
  92.  
  93. /* free a list of cells */
  94. free_dlist(p)
  95.     struct qp *p;
  96.     {
  97.     struct qp *t;
  98.  
  99.     if (p != NULL)
  100.         {
  101.         for (t = p; t->f != NULL; t = t->f); /* find end of ret'd list */
  102.         t->f = freedcell;    /* put ret'd list at head of "free" list */
  103.         freedcell = p;
  104.         }
  105.     }
  106. /* build a buffer:  called with address of a qh */
  107. /* if no buffer there, get a cell and link it in */
  108. make_buffer(p)
  109.     struct qh *p;
  110.     {
  111.     if (!(p->f))
  112.         {
  113.         p->f = get_bcell();
  114.         p->f->b = (struct buffcell *) p;
  115.         }
  116.     }
  117.  
  118.  
  119. /* routines to advance one character forward or backward */
  120. /* argument is the address of a qp */
  121. /* fwdc, backc return 1 if success, 0 if beyond extremes of buffer) */
  122. /* fwdcx extends buffer if failure */
  123.  
  124. int fwdc(arg)
  125.     struct qp *arg;
  126.     {
  127.     if ((*arg).c >= CELLSIZE-1)        /* test char count for max */
  128.         {
  129.         if ((*arg).p->f == NULL) return(0);        /* last cell: fail */
  130.         else
  131.             {
  132.             (*arg).p = (*arg).p->f;    /* chain through list */
  133.             (*arg).c = 0;            /* and reset char count */
  134.             }
  135.         }
  136.     else ++(*arg).c;                /* otherwise just incr char count */
  137.     ++(*arg).dot;
  138.     return(1);
  139.     }
  140.  
  141. fwdcx(arg)
  142.     struct qp *arg;
  143.     {
  144.     if ((*arg).c >= CELLSIZE-1)        /* test char count for max */
  145.         {
  146.         if ((*arg).p->f == NULL)    /* last cell: extend */
  147.             {
  148.             (*arg).p->f = get_bcell();
  149.             (*arg).p->f->b = (*arg).p;
  150.             }
  151.         (*arg).p = (*arg).p->f;        /* chain through list */
  152.         (*arg).c = 0;                /* and reset char count */
  153.         }
  154.     else ++(*arg).c;                /* otherwise just incr char count */
  155.     ++(*arg).dot;
  156.     return(1);
  157.     }
  158. int backc(arg)
  159.     struct qp *arg;
  160.     {
  161.     if ((*arg).c <= 0)            /* test char count for min */
  162.         {
  163.         if ((*arg).p->b->b == NULL) return(0);        /* first cell: fail */
  164.         else
  165.             {
  166.             (*arg).p = (*arg).p->b;    /* chain through list */
  167.             (*arg).c = CELLSIZE-1;    /* reset char count */
  168.             }
  169.         }
  170.     else --(*arg).c;                /* otherwise just decr char count */
  171.     --(*arg).dot;
  172.     return(1);
  173.     }
  174.  
  175.  
  176. /* set up a pointer to a particular text buffer position */
  177.  
  178. set_pointer(pos, ptr)    /* first arg is position, 2nd is addr of pointer */
  179.     int pos;
  180.     struct qp *ptr;
  181.     {
  182.     struct buffcell *t;
  183.     int i;
  184.  
  185.     if (!buff.f)
  186.         {
  187.         buff.f = get_bcell();            /* if no text buffer, make one    */
  188.         buff.f->b = (struct buffcell *) &buff;
  189.         }
  190.     for (i = pos/CELLSIZE, t = buff.f; (i > 0) && (t->f != NULL); i--) t = t->f;
  191.     ptr->p = t;
  192.     ptr->c = pos % CELLSIZE;
  193.     ptr->dot = pos;
  194.     ptr->z = z;
  195.     }
  196. /* routines to get next character from command buffer        */
  197. /* getcmdc0, when reading beyond command string, pops        */
  198. /* macro stack and continues.                                */
  199. /* getcmdc, in similar circumstances, reports an error        */
  200. /* if pushcmdc() has returned any chars, read them first    */
  201. /* routines type characters as read, if argument != 0        */
  202.  
  203. char getcmdc0(trace)
  204.     {
  205.     while (cptr.dot >= cptr.z)        /* if at end of this level, pop macro stack */
  206.         {
  207.         if (--msp < &mstack[0])        /* pop stack; if top level        */
  208.             {
  209.             msp = &mstack[0];        /* restore stack pointer    */
  210.             cmdc = ESC;                /* return an ESC (ignored)        */
  211.             exitflag = 1;            /* set to terminate execution    */
  212.             return(cmdc);            /* exit "while" and return */
  213.             }
  214.         }
  215.     cmdc = cptr.p->ch[cptr.c++];        /* get char */
  216.     ++cptr.dot;                            /* increment character count */
  217.     if (trace) type_char(cmdc);            /* trace */
  218.     if (cptr.c > CELLSIZE-1)            /* and chain if need be */
  219.         {
  220.         cptr.p = cptr.p->f;
  221.         cptr.c = 0;
  222.         }
  223.     return(cmdc);
  224.     }
  225.  
  226.  
  227. char getcmdc(trace)
  228.     {
  229.     if (cptr.dot++ >= cptr.z) ERROR((msp <= &mstack[0]) ? E_UTC : E_UTM);
  230.     else
  231.         {
  232.         cmdc = cptr.p->ch[cptr.c++];    /* get char */
  233.         if (trace) type_char(cmdc);        /* trace */
  234.         if (cptr.c > CELLSIZE-1)        /* and chain if need be */
  235.             {
  236.             cptr.p = cptr.p->f;
  237.             cptr.c = 0;
  238.             }
  239.         }
  240.     return(cmdc);
  241.     }
  242.  
  243.  
  244. /* peek at next char in command string, return 1 if it is equal (case independent) to argument */
  245. int peekcmdc(arg)
  246.     char arg;
  247.     {
  248.     return(((cptr.dot < cptr.z) && (mapch_l[cptr.p->ch[cptr.c]] == mapch_l[arg])) ? 1 : 0);
  249.     }
  250.  
  251.