home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume2 / basic / part2 / newbs / bslib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  1.5 KB  |  95 lines

  1. /* bslib.c -- subroutine library, routines useful anywhere.
  2.  */
  3.  
  4. #include "bsdefs.h"
  5.  
  6. FILE *bsin = stdin;
  7.  
  8. /* blcpy -- copies a block of memory (l bytes) from s to d.
  9.  */
  10. blcpy(d,s,l)
  11. char *d,*s;
  12. int l;
  13. {
  14.     for(; l >= 0; (l--)) *(d++) = *(s++);
  15. }
  16.  
  17. /* Input routines.  These routines buffer input a line at a time into
  18.  * ibuf.  Unputted input goes to pbbuf, and gets read before things in
  19.  * ibuf, if anything in pbbuf.
  20.  */
  21.  
  22. char pbbuf[CSTKSIZ],ibuf[BFSIZ];
  23.  
  24. int iptr = -1;
  25. int pbptr = -1;
  26.  
  27. char input()
  28. {
  29.     if(pbptr > -1)
  30.     return(pbbuf[pbptr--]);
  31.     else {
  32.     if(ibuf[iptr] == '\0') rdlin(bsin);
  33.     if(ibuf[iptr]!='\0' && !feof(bsin))
  34.         return(ibuf[iptr++]);
  35.     else
  36.         return(0);
  37.     }
  38. }
  39.  
  40. rdlin(f) FILE *f;
  41. {
  42.     char c;
  43.  
  44.     iptr = 0;
  45.     for(c=fgetc(f); c!='\n' && c!=EOF; c=fgetc(f)) ibuf[iptr++] = c;
  46.     ibuf[iptr++] = c;
  47.     ibuf[iptr++] = '\0';
  48.     iptr = 0;
  49. }
  50.  
  51. unput(c) char c;
  52. { pbbuf[++pbptr] = c; }
  53.  
  54. /* myalloc() -- allocate, checking for out of memory.
  55.  */
  56. char *myalloc(nb)
  57. int nb;
  58. {
  59.     char *rval;
  60.     rval = malloc(nb);
  61. /*
  62.     printf("myalloc:tos:%o,rv:%o,nb:%d,e:%o\n",&rval,rval,nb,sbrk(0));
  63. */
  64.     if(rval == 0) {
  65.     fprintf(stderr,"myalloc: out of memory\n");
  66.     exit(1);
  67.     }
  68.     return(rval);
  69. }
  70.  
  71.  
  72.  
  73. /* Stack routines.  Very simple. */
  74.  
  75. union value stack[STKSIZ];
  76. int stackp = -1;
  77.  
  78. push(i) union value i;
  79. {
  80.     stack[++stackp] = i;
  81. }
  82.  
  83. union value pop()
  84. {
  85.     return(stack[stackp--]);
  86. }
  87.  
  88. /* Mark stack.  Also very simple. */
  89. int mstack[5];
  90. int mstkp = -1;
  91. mpush()
  92. { mstack[++mstkp] = stackp; }
  93. mpop()
  94. { stackp = mstack[mstkp--]; }
  95.