home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 221_01 / cc41.c < prev    next >
Text File  |  1979-12-31  |  5KB  |  312 lines

  1. /* >>>>>> start of cc4 <<<<<<< */
  2.  
  3. keepch(c)
  4.  char c;
  5. { mline[mptr]=c;
  6.  if(mptr<mpmax)mptr++;
  7.  return (c);
  8. }
  9. preprocess()
  10. { int k;
  11.  char c,sname[namesize];
  12.  if(cmode==0)return;
  13.  mptr=lptr=0;
  14.  while(ch())
  15.   {if((ch()==' ')||(ch()==9))
  16.    {keepch(' ');
  17.    while((ch()==' ')|
  18.     (ch()==9))
  19.     gch();
  20.    }
  21.   else if(ch()=='"')
  22.    {keepch(ch());
  23.    gch();
  24.    while(ch()!='"')
  25.     {if(ch()==0)
  26.       {errrpt("missing quote");
  27.       break;
  28.       }
  29.     keepch(gch());
  30.     }
  31.    gch();
  32.    keepch('"');
  33.    }
  34.   else if(ch()==39)
  35.    {keepch(39);
  36.    gch();
  37.    while(ch()!=39)
  38.     {if(ch()==0)
  39.       {errrpt("missing apostrophe");
  40.       break;
  41.       }
  42.     keepch(gch());
  43.     }
  44.    gch();
  45.    keepch(39);
  46.    }
  47.   else if((ch()=='/')&&(nch()=='*'))
  48.    {inchar();inchar();
  49.    while(((ch()=='*')&
  50.     (nch()=='/'))==0)
  51.     {if(ch()) inchar();
  52.     else {
  53.      inline();
  54.      if(eof)break;
  55.      }
  56.     }
  57.    inchar();inchar();
  58.    }
  59.   else if(an(ch()))
  60.    {k=0;
  61.    while(an(ch()))
  62.     {if(k<namemax)sname[k++]=ch();
  63.     gch();
  64.     }
  65.    sname[k]=0;
  66.    if(k=findmac(sname))
  67.     while(c=macq[k++])
  68.      keepch(c);
  69.    else
  70.     {k=0;
  71.     while(c=sname[k++])
  72.      keepch(c);
  73.     }
  74.    }
  75.   else keepch(gch());
  76.   }
  77.  keepch(0);
  78.  if(mptr>=mpmax)errrpt("line too long");
  79.  lptr=mptr=0;
  80.  while(line[lptr++]=mline[mptr++]);
  81.  lptr=0;
  82.  }
  83. addmac()
  84. { char sname[namesize];
  85.  if(symname(sname)==0)
  86.   {illname();
  87.   kill();
  88.   return;
  89.   }
  90.  addglb(sname,MACRO,0,macptr,MACRO);
  91.  while(ch()==' ' || ch()==9) gch();
  92.  while(putmac(gch()));
  93.  if(macptr>=macmax)errrpt("macro table full");
  94.  }
  95. putmac(c)
  96.  char c;
  97. { macq[macptr]=c;
  98.  if(macptr<=macmax)++macptr;
  99.  return (c);
  100. }
  101. findmac(sname) char *sname; {
  102.   if((findglb(sname)!=0)&&(cptr[ident]==MACRO))
  103.     return(getint(cptr+offset,offsize));
  104.   return (0);
  105. }
  106.  
  107. outbyte(c) char c; {
  108.   if(c==0) return(0);
  109.   if(stagenext) {
  110.     if(stagenext==stagelast) {
  111.       errrpt("staging buffer overflow");
  112.       return 0;
  113.       }
  114.     else *stagenext++ = c;
  115.     }
  116.   else cout(c,output);
  117.   return c;
  118.   }
  119.  
  120. cout(c, fd) char c; int fd; {
  121.   if(putc(c, fd)==EOF) xout();
  122.   }
  123.  
  124. sout(string, fd) char *string; int fd; {
  125.  if(fputs(string, fd)==EOF) xout();
  126.  }
  127.  
  128. xout() {
  129.  fputs("output error\n", stderr);
  130.  exit(ERRCODE);
  131.  }
  132.  
  133. fputs(string, fp) char *string; int fp; {
  134.  char c;
  135.  while(c = *string++) if((c=putc(c, fp)) <= NULL) return(c);
  136.  }
  137.  
  138. outstr(ptr)
  139.  char ptr[];
  140.  {
  141.  int k;
  142.  k=0;
  143.  while(outbyte(ptr[k++]));
  144.  }
  145. cnl() /* print CR to the console */
  146. #ifdef VMS
  147.  {putchar(EOL);}
  148. #else
  149.  {putchar(CR);}
  150. #endif
  151.  
  152. nl()
  153.  {outbyte(EOL);}
  154.  
  155. tab()
  156. #ifdef TAB
  157.  {outbyte(TAB);}
  158. #else
  159.  {outbyte(' ');}
  160. #endif
  161.  
  162. errrpt(ptr)
  163.  char ptr[];
  164.  {
  165.  int k;
  166. /*
  167. **  output to console and stop on request implemented
  168. **  Dieter H. Flunkert   04-may-86
  169. */
  170.  if(stdout!=output) {
  171.    putc('\n',stderr);  
  172.    fputs(line,stderr);
  173.    fputs("\n*****  ",stderr);
  174.    fputs(ptr,stderr);
  175.    fputs("  *****\n",stderr);
  176.  }
  177.  if(pause) {
  178.     fputs("\n? ",stderr);
  179.     if(getc(stderr)==BREAK) exit(0);
  180.  }
  181.  comment();outstr(line);nl();comment();
  182.  k=0;
  183.  while(k<lptr)
  184.   {if(line[k]==9) tab();
  185.    else outbyte(' ');
  186.   ++k;
  187.   }
  188.  outbyte('^');
  189.  nl();comment();outstr("******  ");
  190.  outstr(ptr);
  191.  outstr("  ******");
  192.  nl();
  193.  ++errcnt;
  194.  }
  195. ol(ptr)
  196.  char ptr[];
  197. {
  198.  ot(ptr);
  199.  nl();
  200. }
  201. ot(ptr)
  202.  char ptr[];
  203. {
  204.  tab();
  205.  outstr(ptr);
  206. }
  207. streq(str1,str2)
  208.  char str1[],str2[];
  209.  {
  210.  int k;
  211.  k=0;
  212.  while (*str2)
  213.   {if ((*str1)!=(*str2)) return (0);
  214.   ++k; ++str1; ++str2;
  215.   }
  216.  return (k);
  217.  }
  218. astreq(str1,str2,len)
  219.  char str1[],str2[];int len;
  220.  {
  221.  int k;
  222.  k=0;
  223.  while (k<len)
  224.   {if ((*str1)!=(*str2))break;
  225.   if(*str1==0)break;
  226.   if(*str2==0)break;
  227.   ++k; ++str1; ++str2;
  228.   }
  229.  if (an(*str1))return (0);
  230.  if (an(*str2))return (0);
  231.  return (k);
  232.  }
  233. match(lit)
  234.  char *lit;
  235.  {
  236.  int k;
  237.  blanks();
  238.  if (k=streq(line+lptr,lit))
  239.   {lptr=lptr+k;
  240.   return (1);
  241.   }
  242.   return (0);
  243. }
  244. amatch(lit,len)
  245.  char *lit;int len;
  246.  {
  247.  int k;
  248.  blanks();
  249.  if (k=astreq(line+lptr,lit,len))
  250.   {lptr=lptr+k;
  251.   while(an(ch())) inbyte();
  252.   return (1);
  253.   }
  254.  return (0);
  255.  }
  256. blanks()
  257.  {while(1)
  258.   {while(ch()==0)
  259.    {inline();
  260.    preprocess();
  261.    if(eof)break;
  262.    }
  263.   if(ch()==' ')gch();
  264.   else if(ch()==9)gch();
  265.   else return;
  266.   }
  267.  }
  268. PDBody( item)
  269. int item;
  270.     {
  271.     char chtr;
  272.     if ( item != 0)
  273.  {
  274.  chtr = ( item % 10) + '0'; /*Calculate rightmost character*/
  275.  PDBody( item / 10);
  276.  outbyte(chtr);  
  277.  }
  278.     }
  279.  
  280. setstage(before, start) int *before, *start; {
  281.   if((*before=stagenext)==0) stagenext=stage;
  282.   *start=stagenext;
  283.   }
  284.  
  285. clearstage(before, start) char *before, *start; {
  286.   *stagenext=0;
  287.   if(stagenext=before) return;
  288.   if(start) {
  289. #ifdef OPTIMIZE
  290.     peephole(start);
  291. #else
  292.     sout(start,output);
  293. #endif
  294.     }
  295.   }
  296.  
  297. outdec(item)
  298. int item;
  299. {
  300.     if (item == 0)
  301.  outbyte( '0');
  302.     else if (item == -32768)
  303.  outstr( "-32768");
  304.     else if (item < 0)
  305.  {
  306.  outbyte( '-');
  307.  PDBody( -item);
  308.  }
  309.     else
  310.  PDBody( item);
  311. }
  312.