home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / bfd / doc / chew.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-02  |  27.6 KB  |  1,493 lines

  1. /* chew
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Contributed by steve chamberlain @cygnus
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Yet another way of extracting documentation from source.
  22.    No, I haven't finished it yet, but I hope you people like it better
  23.    than the old way
  24.   
  25.    sac
  26.  
  27.    Basically, this is a sort of string forth, maybe we should call it
  28.    struth?
  29.  
  30.    You define new words thus:
  31.    : <newword> <oldwords> ;
  32.  
  33. */
  34.  
  35. /* Primitives provided by the program:
  36.  
  37.    Two stacks are provided, a string stack and an integer stack.
  38.  
  39.    Internal state variables:
  40.     internal_wanted - indicates whether `-i' was passed
  41.     internal_mode - user-settable
  42.  
  43.    Commands:
  44.     push_text
  45.     ! - pop top of integer stack for address, pop next for value; store
  46.     @ - treat value on integer stack as the address of an integer; push
  47.         that integer on the integer stack after popping the "address"
  48.     hello - print "hello\n" to stdout
  49.     skip_past_newline
  50.     catstr - fn icatstr
  51.     copy_past_newline - append input, up to and including newline into TOS
  52.     dup - fn other_dup
  53.     remchar - delete last character from TOS
  54.     get_stuff_in_command
  55.     do_fancy_stuff - translate <<foo>> to @code{foo} in TOS
  56.     bulletize - if "o" lines found, prepend @itemize @bullet to TOS
  57.         and @item to each "o" line; append @end itemize
  58.     courierize - put @example around . and | lines, translate {* *} { }
  59.     exit - fn chew_exit
  60.     swap
  61.     outputdots - strip out lines without leading dots
  62.     paramstuff - convert full declaration into "PARAMS" form if not already
  63.     maybecatstr - do catstr if internal_mode == internal_wanted, discard
  64.         value in any case
  65.     translatecomments - turn {* and *} into comment delimiters
  66.     kill_bogus_lines - get rid of extra newlines
  67.     indent
  68.     internalmode - pop from integer stack, set `internalmode' to that value
  69.     print_stack_level - print current stack depth to stderr
  70.     strip_trailing_newlines - go ahead, guess...
  71.     [quoted string] - push string onto string stack
  72.     [word starting with digit] - push atol(str) onto integer stack
  73.  
  74.    A command must be all upper-case, and alone on a line.
  75.  
  76.    Foo.  */
  77.  
  78.  
  79. #include <ansidecl.h>
  80. #include "sysdep.h"
  81. #include <assert.h>
  82.  
  83. #define DEF_SIZE 5000
  84. #define STACK 50
  85.  
  86. int internal_wanted;
  87. int internal_mode;
  88.  
  89. int warning;
  90.  
  91. /* Here is a string type ... */
  92.  
  93. typedef struct buffer 
  94. {
  95.   char *ptr;
  96.   unsigned long write_idx;
  97.   unsigned long size;
  98. } string_type;
  99.  
  100.  
  101. #ifdef __STDC__
  102. static void init_string_with_size (string_type *, unsigned int);
  103. static void init_string (string_type *);
  104. static int find (string_type *, char *);
  105. static void write_buffer (string_type *);
  106. static void delete_string (string_type *);
  107. static char *addr (string_type *, unsigned int);
  108. static char at (string_type *, unsigned int);
  109. static void catchar (string_type *, int);
  110. static void overwrite_string (string_type *, string_type *);
  111. static void catbuf (string_type *, char *, unsigned int);
  112. static void cattext (string_type *, char *);
  113. static void catstr (string_type *, string_type *);
  114. static unsigned int skip_white_and_starts (string_type *, unsigned int);
  115. #endif
  116.  
  117.  
  118. static void DEFUN(init_string_with_size,(buffer, size),
  119.        string_type *buffer AND
  120.        unsigned int size )
  121. {
  122.     buffer->write_idx = 0;
  123.     buffer->size = size;
  124.     buffer->ptr = malloc(size);
  125. }
  126.  
  127. static void DEFUN(init_string,(buffer),
  128.        string_type *buffer)
  129. {
  130.     init_string_with_size(buffer, DEF_SIZE);
  131.  
  132. }
  133.  
  134. static int DEFUN(find, (str, what),
  135.       string_type *str AND
  136.       char *what)
  137. {
  138.     unsigned int i;
  139.     char *p;
  140.     p = what;
  141.     for (i = 0; i < str->write_idx && *p; i++) 
  142.     {
  143.     if (*p == str->ptr[i])
  144.      p++;
  145.     else
  146.      p = what;
  147.     }
  148.     return (*p == 0);
  149.     
  150. }
  151.  
  152. static void DEFUN(write_buffer,(buffer),
  153.        string_type *buffer)
  154. {
  155.     fwrite(buffer->ptr, buffer->write_idx, 1, stdout);
  156. }
  157.  
  158.  
  159. static void DEFUN(delete_string,(buffer),
  160.        string_type *buffer)
  161. {
  162.     free(buffer->ptr);
  163. }
  164.  
  165.  
  166. static char *DEFUN(addr, (buffer, idx),
  167.         string_type *buffer AND
  168.         unsigned int idx)
  169. {
  170.     return buffer->ptr + idx;
  171. }
  172.  
  173. static char DEFUN(at,(buffer, pos),
  174.        string_type *buffer AND
  175.        unsigned int pos) 
  176. {
  177.   if (pos >= buffer->write_idx) 
  178.     return 0;
  179.   return buffer->ptr[pos];
  180. }
  181.  
  182. static void DEFUN(catchar,(buffer, ch), 
  183.        string_type *buffer AND
  184.        int ch)
  185. {
  186.   if (buffer->write_idx == buffer->size) 
  187.     {
  188.       buffer->size *=2;
  189.       buffer->ptr = realloc(buffer->ptr, buffer->size);
  190.     }
  191.  
  192.   buffer->ptr[buffer->write_idx ++ ] = ch;
  193. }
  194.  
  195.  
  196. static void DEFUN(overwrite_string,(dst,   src),
  197.        string_type *dst AND
  198.        string_type *src)
  199. {
  200.     free(dst->ptr);
  201.     dst->size = src->size;
  202.     dst->write_idx = src->write_idx;
  203.     dst->ptr = src->ptr;
  204. }
  205.  
  206. static void DEFUN(catbuf,(buffer, buf, len),
  207.        string_type *buffer AND
  208.        char *buf AND
  209.        unsigned int len)
  210. {
  211.   if (buffer->write_idx + len >= buffer->size)
  212.     {
  213.       while (buffer->write_idx + len >= buffer->size)
  214.     buffer->size *= 2;
  215.       buffer->ptr = realloc (buffer->ptr, buffer->size);
  216.     }
  217.   memcpy (buffer->ptr + buffer->write_idx, buf, len);
  218.   buffer->write_idx += len;
  219. }
  220.  
  221. static void DEFUN(cattext,(buffer, string),
  222.        string_type *buffer AND
  223.        char *string)
  224. {
  225.   catbuf (buffer, string, (unsigned int) strlen (string));
  226. }
  227.  
  228. static void DEFUN(catstr,(dst, src),
  229.        string_type *dst AND
  230.        string_type *src)
  231. {
  232.   catbuf (dst, src->ptr, src->write_idx);
  233. }
  234.  
  235.  
  236. static unsigned int 
  237. DEFUN(skip_white_and_stars,(src, idx),
  238.       string_type *src AND
  239.       unsigned int idx)
  240. {
  241.   char c;
  242.   while ((c = at(src,idx)),
  243.      isspace (c)
  244.      || (c == '*'
  245.          /* Don't skip past end-of-comment or star as first
  246.         character on its line.  */
  247.          && at(src,idx +1) != '/'
  248.          && at(src,idx -1) != '\n')) 
  249.     idx++;
  250.   return idx;
  251. }
  252.  
  253. /***********************************************************************/
  254.  
  255.  
  256. string_type stack[STACK];
  257. string_type *tos;
  258.  
  259. unsigned int idx = 0; /* Pos in input buffer */
  260. string_type *ptr; /* and the buffer */
  261. typedef void (*stinst_type)();
  262. stinst_type *pc;
  263. stinst_type sstack[STACK];
  264. stinst_type *ssp = &sstack[0];
  265. long istack[STACK];
  266. long *isp = &istack[0];
  267.  
  268. typedef int *word_type;
  269.  
  270.  
  271.  
  272. struct dict_struct
  273. {
  274.     char *word;
  275.     struct dict_struct *next;
  276.     stinst_type *code;
  277.     int code_length;
  278.     int code_end;
  279.     int var;
  280.     
  281. };
  282. typedef struct dict_struct dict_type;
  283. #define WORD(x) static void x()
  284.  
  285. static void
  286. die (msg)
  287.      char *msg;
  288. {
  289.   fprintf (stderr, "%s\n", msg);
  290.   exit (1);
  291. }
  292.  
  293. static void
  294. check_range ()
  295. {
  296.   if (tos < stack)
  297.     die ("underflow in string stack");
  298.   if (tos >= stack + STACK)
  299.     die ("overflow in string stack");
  300. }
  301.  
  302. static void
  303. icheck_range ()
  304. {
  305.   if (isp < istack)
  306.     die ("underflow in integer stack");
  307.   if (isp >= istack + STACK)
  308.     die ("overflow in integer stack");
  309. }
  310.  
  311. #ifdef __STDC__
  312. static void exec (dict_type *);
  313. static void call (void);
  314. static void remchar (void), strip_trailing_newlines (void), push_number (void);
  315. static void push_text (void);
  316. static void remove_noncomments (string_type *, string_type *);
  317. static void print_stack_level (void);
  318. static void paramstuff (void), translatecomments (void), manglecomments (void);
  319. static void outputdots (void), courierize (void), bulletize (void);
  320. static void do_fancy_stuff (void);
  321. static int iscommand (string_type *, unsigned int);
  322. static int copy_past_newline (string_type *, unsigned int, string_type *);
  323. static void icopy_past_newline (void), kill_bogus_lines (void), indent (void);
  324. static void get_stuff_in_command (void), swap (void), other_dup (void);
  325. static void icatstr (void), skip_past_newline (void), internalmode (void);
  326. static void maybecatstr (void);
  327. static char *nextword (char *, char **);
  328. dict_type *lookup_word (char *);
  329. static void perform (void);
  330. dict_type *newentry (char *);
  331. unsigned int add_to_definition (dict_type *, stinst_type);
  332. void add_intrinsic (char *, void (*)());
  333. void add_var (char *);
  334. void compile (char *);
  335. static void bang (void);
  336. static void atsign (void);
  337. static void hello (void);
  338. static void read_in (string_type *, FILE *);
  339. static void usage (void);
  340. static void chew_exit (void);
  341. #endif
  342.  
  343. static void DEFUN(exec,(word),
  344.           dict_type *word)
  345. {
  346.   pc = word->code;
  347.   while (*pc) 
  348.     (*pc)();
  349. }
  350. WORD(call)
  351. {
  352.     stinst_type *oldpc = pc;
  353.     dict_type *e;
  354.     e =  (dict_type *)(pc [1]);
  355.     exec(e);
  356.     pc = oldpc + 2;
  357.     
  358. }
  359.  
  360. WORD(remchar)
  361. {
  362.   if (tos->write_idx)
  363.     tos->write_idx--;    
  364.   pc++;
  365. }
  366.  
  367. static void
  368. strip_trailing_newlines ()
  369. {
  370.   while ((isspace (at (tos, tos->write_idx - 1))
  371.       || at (tos, tos->write_idx - 1) == '\n')
  372.      && tos->write_idx > 0)
  373.     tos->write_idx--;
  374.   pc++;
  375. }
  376.  
  377. WORD(push_number)
  378. {
  379.     isp++;
  380.     icheck_range ();
  381.     pc++;
  382.     *isp = (long)(*pc);
  383.     pc++;
  384. }
  385.  
  386. WORD(push_text)
  387. {
  388.     tos++;
  389.     check_range ();
  390.     init_string(tos);
  391.     pc++;
  392.     cattext(tos,*((char **)pc));
  393.     pc++;
  394.     
  395. }
  396.  
  397.  
  398. /* This function removes everything not inside comments starting on
  399.    the first char of the line from the  string, also when copying
  400.    comments, removes blank space and leading *'s.
  401.    Blank lines are turned into one blank line.  */
  402.  
  403. static void 
  404. DEFUN(remove_noncomments,(src,dst),
  405.        string_type *src AND
  406.        string_type *dst)
  407. {
  408.     unsigned int idx = 0;
  409.     
  410.     while (at(src,idx)) 
  411.     {
  412.     /* Now see if we have a comment at the start of the line */
  413.     if (at(src,idx) == '\n' 
  414.         && at(src,idx+1) ==  '/' 
  415.         && at(src,idx+2) == '*') 
  416.     {
  417.         idx+=3;
  418.         
  419.         idx = skip_white_and_stars(src,idx);
  420.  
  421.         /* Remove leading dot */
  422.         if (at(src, idx) == '.')
  423.          idx++;
  424.         
  425.         /* Copy to the end of the line, or till the end of the
  426.            comment */
  427.         while (at(src, idx))
  428.         {
  429.         if (at(src, idx) == '\n') 
  430.         {
  431.             /* end of line, echo and scrape of leading blanks  */
  432.             if (at(src,idx +1) == '\n')
  433.              catchar(dst,'\n');
  434.             catchar(dst,'\n');
  435.             idx++;
  436.             idx =   skip_white_and_stars(src, idx);
  437.         }
  438.         else if (at(src, idx) == '*' && at(src,idx+1) == '/') 
  439.         {
  440.             idx +=2 ;
  441.             cattext(dst,"\nENDDD\n");
  442.             break;
  443.         }
  444.         else 
  445.         {
  446.             catchar(dst, at(src, idx));
  447.             idx++;
  448.         }
  449.         }
  450.     }
  451.     else idx++;
  452.     }
  453. }
  454.  
  455. static void
  456. print_stack_level ()
  457. {
  458.   fprintf (stderr, "current string stack depth = %d, ", tos - stack);
  459.   fprintf (stderr, "current integer stack depth = %d\n", isp - istack);
  460.   pc++;
  461. }
  462.  
  463. /* turn:
  464.      foobar name(stuff);
  465.    into:
  466.      foobar
  467.      name PARAMS ((stuff));
  468.    and a blank line.
  469.  */
  470.  
  471. static void
  472. DEFUN_VOID(paramstuff)
  473. {
  474.     unsigned int openp;
  475.     unsigned int fname;
  476.     unsigned int idx;
  477.     string_type out;
  478.     init_string(&out);
  479.     
  480.  
  481.     /* make sure that it's not already param'd or proto'd */
  482.     if(find(tos,"PARAMS") || find(tos,"PROTO") || !find(tos,"(")) {
  483.         catstr(&out,tos);
  484.     }
  485.     else 
  486.     {
  487.     /* Find the open paren */
  488.     for (openp = 0; at(tos, openp) != '('  && at(tos,openp); openp++)
  489.      ;
  490.  
  491.     fname = openp;
  492.     /* Step back to the fname */
  493.     fname--;
  494.     while (fname && isspace(at(tos, fname)))
  495.      fname --;
  496.     while (fname && !isspace(at(tos,fname)) && at(tos,fname) != '*')
  497.      fname--;
  498.  
  499.     fname++;
  500.     
  501.     for (idx = 0; idx < fname; idx++)     /* Output type */
  502.     {
  503.         catchar(&out, at(tos,idx));
  504.     }
  505.     
  506.         cattext(&out, "\n");    /* Insert a newline between type and fnname */
  507.  
  508.     for (idx = fname; idx < openp; idx++)         /* Output fnname */
  509.     {
  510.         catchar(&out, at(tos,idx));
  511.     }
  512.  
  513.     cattext(&out," PARAMS (");
  514.  
  515.     while (at(tos,idx) && at(tos,idx) !=';') 
  516.     {
  517.         catchar(&out, at(tos, idx));
  518.         idx++;
  519.     }
  520.     cattext(&out,");\n\n");
  521.     }
  522.     overwrite_string(tos, &out);    
  523.     pc++;
  524.     
  525. }
  526.  
  527.  
  528.  
  529. /* turn {*
  530.    and *} into comments */
  531.  
  532. WORD(translatecomments)
  533. {
  534.     unsigned int idx = 0;
  535.     string_type out;
  536.     init_string(&out);
  537.     
  538.     while (at(tos, idx)) 
  539.     {
  540.     if (at(tos,idx) == '{' && at(tos,idx+1) =='*') 
  541.     {
  542.         cattext(&out,"/*");
  543.         idx+=2;
  544.     }
  545.     else if (at(tos,idx) == '*' && at(tos,idx+1) =='}') 
  546.     {
  547.         cattext(&out,"*/");
  548.         idx+=2;
  549.     }
  550.     else  
  551.     {
  552.         catchar(&out, at(tos, idx));
  553.         idx++;
  554.     }
  555.     }
  556.  
  557.  
  558.     overwrite_string(tos, &out);
  559.     
  560.     pc++;
  561.     
  562. }
  563.  
  564. /* turn everything not starting with a . into a comment */
  565.  
  566. WORD(manglecomments)
  567. {
  568.     unsigned int idx = 0;
  569.     string_type out;
  570.     init_string(&out);
  571.     
  572.     while (at(tos, idx)) 
  573.     {
  574.     if (at(tos,idx) == '\n' && at(tos,idx+1) =='*') 
  575.     {
  576.         cattext(&out,"    /*");
  577.         idx+=2;
  578.     }
  579.     else if (at(tos,idx) == '*' && at(tos,idx+1) =='}') 
  580.     {
  581.         cattext(&out,"*/");
  582.         idx+=2;
  583.     }
  584.     else  
  585.     {
  586.         catchar(&out, at(tos, idx));
  587.         idx++;
  588.     }
  589.     }
  590.  
  591.  
  592.     overwrite_string(tos, &out);
  593.     
  594.     pc++;
  595.     
  596. }
  597.  
  598. /* Mod tos so that only lines with leading dots remain */
  599. static void
  600. DEFUN_VOID(outputdots)
  601. {
  602.     unsigned int idx = 0;
  603.     string_type out;
  604.     init_string(&out);
  605.     
  606.     while (at(tos, idx)) 
  607.     {
  608.     if (at(tos, idx) == '\n' && at(tos, idx+1) == '.') 
  609.     {
  610.       char c, c2;
  611.       idx += 2;
  612.         
  613.         while ((c = at(tos, idx)) && c != '\n')
  614.         {
  615.           if (c == '{' && at(tos,idx+1) =='*') 
  616.         {
  617.             cattext(&out," /*");
  618.             idx+=2;
  619.         }
  620.           else if (c == '*' && at(tos,idx+1) =='}') 
  621.         {
  622.             cattext(&out,"*/");
  623.             idx+=2;
  624.         }
  625.           else
  626.         {
  627.             catchar(&out, c);
  628.             idx++;
  629.         }
  630.         }
  631.         catchar(&out,'\n');
  632.     }
  633.     else 
  634.     {
  635.         idx++;
  636.     }
  637.     }    
  638.  
  639.     overwrite_string(tos, &out);
  640.     pc++;
  641.     
  642. }
  643.  
  644. /* Find lines starting with . and | and put example around them on tos */
  645. WORD(courierize)
  646. {
  647.     string_type out;
  648.     unsigned int idx = 0;
  649.     int command = 0;
  650.     
  651.     init_string(&out);
  652.     
  653.     while (at(tos, idx)) 
  654.     {
  655.     if (at(tos, idx) == '\n' 
  656.         && (at(tos, idx +1 ) == '.'
  657.         || at(tos,idx+1) == '|')) 
  658.     {
  659.         cattext(&out,"\n@example\n");
  660.         do 
  661.         {
  662.         idx += 2;
  663.         
  664.         while (at(tos, idx) && at(tos, idx)!='\n')
  665.         {
  666.             if (at(tos,idx)=='{' && at(tos,idx+1) =='*') 
  667.             {
  668.             cattext(&out," /*");
  669.             idx+=2;
  670.             }
  671.             else if (at(tos,idx)=='*' && at(tos,idx+1) =='}') 
  672.             {
  673.             cattext(&out,"*/");
  674.             idx+=2;
  675.             }
  676.                 else if (at(tos,idx) == '{' && !command)
  677.             {
  678.             cattext(&out,"@{");
  679.             idx++;
  680.             }
  681.                 else if (at(tos,idx) == '}' && !command)
  682.             {
  683.             cattext(&out,"@}");
  684.             idx++;
  685.             }
  686.             else 
  687.             {
  688.             if (at(tos,idx) == '@')
  689.                 command = 1;
  690.             else if (isspace(at(tos,idx)) || at(tos,idx) == '}')
  691.                 command = 0;
  692.             catchar(&out, at(tos, idx));
  693.             idx++;
  694.             }
  695.             
  696.         }
  697.         catchar(&out,'\n');
  698.         }  
  699.         while (at(tos, idx) == '\n' 
  700.            && (at(tos, idx+1) == '.')
  701.            || (at(tos,idx+1) == '|'));
  702.         cattext(&out,"@end example");
  703.     }
  704.     else 
  705.     {    
  706.         catchar(&out, at(tos, idx));
  707.         idx++;
  708.     }
  709.     }    
  710.  
  711.     overwrite_string(tos, &out);
  712.     pc++;
  713.  
  714.     
  715. }
  716.  
  717. /* Finds any lines starting with "o ", if there are any, then turns
  718.    on @itemize @bullet, and @items each of them. Then ends with @end
  719.    itemize, inplace at TOS*/
  720.  
  721.  
  722. WORD(bulletize)
  723. {
  724.     unsigned int idx = 0;
  725.     int on = 0;
  726.     string_type out;
  727.     init_string(&out);
  728.     
  729.     while (at(tos, idx)) {
  730.     if (at(tos, idx) == '@' &&
  731.         at(tos, idx+1) == '*') 
  732.     {
  733.       cattext(&out,"*");
  734.       idx+=2;
  735.     }
  736.     
  737. else
  738.         if (at(tos, idx) == '\n' &&
  739.         at(tos, idx+1) == 'o' &&
  740.         isspace(at(tos, idx +2)))
  741.         {
  742.         if (!on) 
  743.         {
  744.             cattext(&out,"\n@itemize @bullet\n");
  745.             on = 1;
  746.             
  747.         }
  748.         cattext(&out,"\n@item\n");
  749.         idx+=3;
  750.         }
  751.         else 
  752.         {
  753.         catchar(&out, at(tos, idx));
  754.         if (on && at(tos, idx) == '\n' &&
  755.             at(tos, idx+1) == '\n' &&
  756.             at(tos, idx+2) != 'o')
  757.         {
  758.             cattext(&out, "@end itemize");
  759.             on = 0;
  760.         }
  761.         idx++;
  762.         
  763.         }
  764.     }
  765.     if (on) 
  766.     {
  767.     cattext(&out,"@end itemize\n");
  768.     }    
  769.  
  770.     delete_string(tos);
  771.     *tos = out;
  772.     pc++;
  773.     
  774. }
  775.  
  776. /* Turn <<foo>> into @code{foo} in place at TOS*/
  777.    
  778.  
  779. WORD(do_fancy_stuff)
  780. {
  781.     unsigned int idx = 0;
  782.     string_type out;
  783.     init_string(&out);
  784.     while (at(tos, idx)) 
  785.     {
  786.     if (at(tos, idx) == '<' 
  787.         && at(tos, idx+1) == '<'
  788.         && !isspace(at(tos,idx + 2))) 
  789.     {
  790.         /* This qualifies as a << startup */
  791.         idx +=2;
  792.         cattext(&out,"@code{");
  793.         while(at(tos,idx) &&
  794.           at(tos,idx) != '>' )
  795.         {
  796.         catchar(&out, at(tos, idx));
  797.         idx++;
  798.         
  799.         }
  800.         cattext(&out,"}");
  801.         idx+=2;
  802.     }
  803.     else 
  804.     {
  805.         catchar(&out, at(tos, idx));
  806.         idx++;
  807.     }
  808.     }
  809.     delete_string(tos);
  810.     *tos = out;
  811.     pc++;
  812.     
  813. }
  814. /* A command is all upper case,and alone on a line */
  815. static int 
  816. DEFUN( iscommand,(ptr, idx),
  817.       string_type *ptr AND
  818.       unsigned int idx)
  819. {
  820.     unsigned int len = 0;
  821.     while (at(ptr,idx)) {
  822.         if (isupper(at(ptr,idx)) || at(ptr,idx) == ' ' ||
  823.         at(ptr,idx) == '_') 
  824.         {
  825.          len++;
  826.          idx++;
  827.      }
  828.         else if(at(ptr,idx) == '\n')
  829.         {
  830.         if (len > 3) return 1;
  831.         return 0;
  832.         }
  833.         else return 0;
  834.     }
  835.     return 0;
  836.  
  837. }
  838.  
  839.  
  840. DEFUN(copy_past_newline,(ptr, idx, dst),
  841.       string_type *ptr AND
  842.       unsigned int idx AND
  843.       string_type *dst)
  844. {
  845.     while (at(ptr, idx) && at(ptr, idx) != '\n') 
  846.     {
  847.     catchar(dst, at(ptr, idx));
  848.     idx++;
  849.     
  850.     }    
  851.     catchar(dst, at(ptr, idx));
  852.     idx++;
  853.     return idx;
  854.  
  855. }
  856.  
  857. WORD(icopy_past_newline)
  858. {
  859.     tos++;
  860.     check_range ();
  861.     init_string(tos);
  862.     idx = copy_past_newline(ptr, idx, tos);
  863.     pc++;    
  864. }
  865.  
  866. /* indent
  867.    Take the string at the top of the stack, do some prettying */
  868.  
  869.  
  870. WORD(kill_bogus_lines)
  871. {
  872.     int sl ;
  873.     
  874.     int nl = 0;
  875.     int idx = 0;
  876.     int c;
  877.     int dot = 0    ;
  878.     
  879.     string_type out;    
  880.     init_string(&out);
  881.     /* Drop leading nl */
  882.     while (at(tos,idx) == '\n')
  883.     {
  884.     idx++;
  885.     }
  886.     c = idx;
  887.     
  888.     /* Find the last char */
  889.     while (at(tos,idx))
  890.     {
  891.     idx++;
  892.     }
  893.     
  894.     /* find the last non white before the nl */
  895.     idx--;
  896.     
  897.     while (idx && isspace(at(tos,idx)))
  898.      idx--;
  899.     idx++;
  900.     
  901.     /* Copy buffer upto last char, but blank lines before and after
  902.        dots don't count */
  903.     sl = 1;
  904.  
  905.     while (c < idx)
  906.     {
  907.     if (at(tos,c) == '\n' 
  908.         && at(tos,c+1) == '\n'
  909.         && at(tos,c+2) == '.') 
  910.     {
  911.         /* Ignore two newlines before a dot*/
  912.         c++;
  913.     }
  914.     else if (at(tos,c) == '.' && sl)
  915.     {
  916.         /* remember that this line started with a dot */
  917.         dot=2;
  918.     }
  919.     else if (at(tos,c) == '\n' 
  920.          && at(tos,c+1) == '\n'
  921.          && dot)
  922.     {
  923.         c++;
  924.         /* Ignore two newlines when last line was dot */
  925.     }
  926.  
  927.     catchar(&out, at(tos,c));
  928.     if (at(tos,c) == '\n')
  929.     {
  930.         sl = 1;
  931.         
  932.         if (dot == 2)dot=1;else dot = 0;
  933.     }
  934.     
  935.     c++;    
  936.  
  937.     }
  938.     
  939.     /* Append nl*/
  940.     catchar(&out, '\n');
  941.     pc++;
  942.     delete_string(tos);
  943.     *tos = out;
  944.     
  945.     
  946. }
  947.  
  948. WORD(indent)
  949. {
  950.     string_type out;
  951.     int tab = 0;
  952.     int idx = 0;
  953.     int ol =0;
  954.     init_string(&out);
  955.     while (at(tos,idx)) {
  956.         switch (at(tos,idx)) 
  957.         {
  958.           case '\n':
  959.         cattext(&out,"\n");
  960.         idx++;
  961.         if (tab) 
  962.         {
  963.             cattext(&out,"    ");
  964.         }
  965.         ol = 0;
  966.         break;
  967.           case '(':
  968.         tab++;
  969.         if (ol == 0)
  970.             cattext(&out,"   ");
  971.         idx++;
  972.         cattext(&out,"(");
  973.         ol = 1;
  974.         break;
  975.           case ')':
  976.         tab--;
  977.         cattext(&out,")");
  978.         idx++;
  979.         ol=1;
  980.         
  981.         break;
  982.           default:
  983.         catchar(&out,at(tos,idx));
  984.         ol=1;
  985.         
  986.         idx++;
  987.         break;
  988.         }
  989.     }    
  990.  
  991.     pc++;
  992.     delete_string(tos);
  993.     *tos = out;
  994.  
  995. }
  996.  
  997.  
  998. WORD(get_stuff_in_command)
  999. {
  1000.     tos++;
  1001.     check_range ();
  1002.     init_string(tos);
  1003.  
  1004.     while (at(ptr, idx)) {
  1005.         if (iscommand(ptr, idx))  break;
  1006.         idx =   copy_past_newline(ptr, idx, tos);
  1007.     }
  1008.     pc++;    
  1009. }
  1010.  
  1011. WORD(swap)
  1012. {
  1013.     string_type t;
  1014.     
  1015.     t = tos[0];
  1016.     tos[0] = tos[-1];
  1017.     tos[-1] =t; 
  1018.     pc++;
  1019.     
  1020. }
  1021.  
  1022. WORD(other_dup)
  1023. {
  1024.     tos++;
  1025.     check_range ();
  1026.     init_string(tos);
  1027.     catstr(tos, tos-1);
  1028.     pc++;
  1029. }
  1030.  
  1031.  
  1032.  
  1033. WORD(icatstr)
  1034. {
  1035.     tos--;
  1036.     check_range ();
  1037.     catstr(tos, tos+1);
  1038.     delete_string(tos+1);
  1039.     pc++;
  1040. }
  1041.  
  1042. WORD(skip_past_newline)
  1043. {
  1044.     while (at(ptr,idx) 
  1045.        && at(ptr,idx) != '\n')
  1046.      idx++;
  1047.     idx++;
  1048.     pc++;
  1049. }
  1050.  
  1051.  
  1052. WORD(internalmode)
  1053. {
  1054.     internal_mode = *(isp);
  1055.     isp--;
  1056.     icheck_range ();
  1057.     pc++;
  1058. }
  1059.  
  1060. WORD(maybecatstr)
  1061. {
  1062.     if (internal_wanted == internal_mode) 
  1063.     {
  1064.     catstr(tos-1, tos);
  1065.     }
  1066.     delete_string(tos);
  1067.     tos--;
  1068.     check_range ();
  1069.     pc++;
  1070. }
  1071.  
  1072. char *
  1073. DEFUN(nextword,(string, word),
  1074.       char *string AND
  1075.       char **word)
  1076. {
  1077.     char *word_start;
  1078.     int idx;
  1079.     char *dst;
  1080.     char *src;
  1081.     
  1082.     int length = 0;
  1083.     
  1084.     while (isspace(*string) || *string == '-') {
  1085.         if (*string == '-') 
  1086.         {
  1087.         while (*string && *string != '\n') 
  1088.          string++;
  1089.         
  1090.         }
  1091.         else {
  1092.             string++;
  1093.         }
  1094.     }
  1095.     if (!*string) return 0;
  1096.     
  1097.     word_start = string;
  1098.     if (*string == '"') 
  1099.       {
  1100.     do
  1101.       {
  1102.         string++;
  1103.         length++;
  1104.         if (*string == '\\')
  1105.           {
  1106.         string += 2;
  1107.         length += 2;
  1108.           }
  1109.       }
  1110.     while (*string != '"');
  1111.       }
  1112.     else     
  1113.       {
  1114.     while (!isspace(*string)) 
  1115.     {
  1116.         string++;
  1117.         length++;
  1118.     
  1119.     }
  1120.     }
  1121.     
  1122.     *word = malloc(length + 1);
  1123.  
  1124.     dst = *word;
  1125.     src = word_start;
  1126.  
  1127.  
  1128.     for (idx= 0; idx < length; idx++) 
  1129.       {
  1130.     if (src[idx] == '\\')
  1131.       switch (src[idx+1])
  1132.         {
  1133.         case 'n':
  1134.           *dst++ = '\n';
  1135.           idx++;
  1136.           break;
  1137.         case '"':
  1138.         case '\\':
  1139.           *dst++ = src[idx+1];
  1140.           idx++;
  1141.           break;
  1142.         default:
  1143.           *dst++ = '\\';
  1144.           break;
  1145.         }
  1146.     else
  1147.       *dst++ = src[idx];
  1148.     }
  1149.     *dst++ = 0;
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.     if(*string)    
  1156.      return string + 1;
  1157.     else 
  1158.      return 0;
  1159.     
  1160. }
  1161. dict_type *root;
  1162. dict_type *
  1163. DEFUN(lookup_word,(word),
  1164.       char *word)
  1165. {
  1166.   dict_type *ptr = root;
  1167.   while (ptr) {
  1168.       if (strcmp(ptr->word, word) == 0) return ptr;
  1169.       ptr = ptr->next;
  1170.         
  1171.     }
  1172.   if (warning)
  1173.    fprintf(stderr,"Can't find %s\n",word);
  1174.   return 0;
  1175.     
  1176.     
  1177. }
  1178.  
  1179. static void DEFUN_VOID(perform)
  1180. {
  1181.   tos = stack;
  1182.     
  1183.   while (at(ptr, idx)) {
  1184.       /* It's worth looking through the command list */
  1185.       if (iscommand(ptr, idx))
  1186.       {
  1187.     unsigned int i;
  1188.     int found = 0;
  1189.  
  1190.     char *next;
  1191.     dict_type *word ;
  1192.         
  1193.     (void)        nextword(addr(ptr, idx), &next);
  1194.  
  1195.  
  1196.     word = lookup_word(next);
  1197.  
  1198.  
  1199.         
  1200.  
  1201.     if (word) 
  1202.     {
  1203.       exec(word);
  1204.     }
  1205.     else
  1206.     {
  1207.       if (warning)
  1208.        fprintf(stderr,"warning, %s is not recognised\n",  next);
  1209.       skip_past_newline();
  1210.     }
  1211.         
  1212.       }
  1213.       else skip_past_newline();
  1214.  
  1215.     }
  1216. }
  1217.  
  1218. dict_type *
  1219. DEFUN(newentry,(word),
  1220.       char *word)
  1221. {
  1222.     dict_type *new = (dict_type *)malloc(sizeof(dict_type));
  1223.     new->word = word;
  1224.     new->next = root;
  1225.     root = new;
  1226.     new->code = (stinst_type *)malloc(sizeof(stinst_type ));
  1227.     new->code_length = 1;
  1228.     new->code_end = 0;
  1229.     return new;
  1230.     
  1231. }
  1232.  
  1233.  
  1234. unsigned int
  1235. DEFUN(add_to_definition,(entry, word), 
  1236.       dict_type *entry AND
  1237.       stinst_type word)
  1238. {
  1239.     if (entry->code_end == entry->code_length) 
  1240.     {
  1241.     entry->code_length += 2;
  1242.     entry->code =
  1243.      (stinst_type *) realloc((char *)(entry->code),
  1244.                    entry->code_length *sizeof(word_type));
  1245.     }
  1246.     entry->code[entry->code_end] = word;
  1247.     
  1248. return     entry->code_end++;  
  1249. }
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257. void
  1258. DEFUN(add_intrinsic,(name, func),
  1259.       char *name AND
  1260.       void (*func)())
  1261. {
  1262.     dict_type *new = newentry(name);
  1263.     add_to_definition(new, func);
  1264.     add_to_definition(new, 0);
  1265. }
  1266.  
  1267. void
  1268. DEFUN(add_var,(name),
  1269.       char *name)
  1270. {
  1271.     dict_type *new = newentry(name);
  1272.     add_to_definition(new, push_number);
  1273.     add_to_definition(new, (stinst_type)(&(new->var)));
  1274.     add_to_definition(new,0);
  1275. }
  1276.  
  1277.  
  1278. void 
  1279. DEFUN(compile, (string), 
  1280.       char *string)
  1281. {
  1282.     int jstack[STACK];
  1283.     int *jptr = jstack;
  1284.     /* add words to the dictionary */
  1285.     char *word;
  1286.     string = nextword(string, &word);
  1287.     while (string && *string && word[0]) 
  1288.     {
  1289.     if (strcmp(word,"var")==0) 
  1290.     {
  1291.  string=nextword(string, &word);
  1292.       
  1293.       add_var(word);
  1294.  string=nextword(string, &word);
  1295.     }
  1296. else    
  1297.         
  1298.     if (word[0] == ':')
  1299.     {
  1300.         dict_type *ptr;
  1301.         /* Compile a word and add to dictionary */
  1302.         string = nextword(string, &word);
  1303.         
  1304.         ptr = newentry(word);
  1305.         string = nextword(string, &word);
  1306.         while (word[0] != ';' ) 
  1307.         {
  1308.          switch (word[0]) 
  1309.          {
  1310.            case '"':
  1311.              /* got a string, embed magic push string
  1312.             function */
  1313.              add_to_definition(ptr, push_text);
  1314.              add_to_definition(ptr, (stinst_type)(word+1));
  1315.              break;
  1316.            case '0':
  1317.            case '1':
  1318.            case '2':
  1319.            case '3':
  1320.            case '4':
  1321.            case '5':
  1322.            case '6':
  1323.            case '7':
  1324.            case '8':
  1325.            case '9':
  1326.              /* Got a number, embedd the magic push number
  1327.             function */
  1328.              add_to_definition(ptr, push_number);
  1329.              add_to_definition(ptr, (stinst_type)atol(word));
  1330.              break;
  1331.            default:
  1332.              add_to_definition(ptr, call);
  1333.              add_to_definition(ptr, (stinst_type)lookup_word(word));
  1334.          }
  1335.  
  1336.         string = nextword(string, &word);             
  1337.         }
  1338.         add_to_definition(ptr,0);
  1339.         string = nextword(string, &word);
  1340.     }
  1341.     else 
  1342.     {
  1343.         fprintf(stderr,"syntax error at %s\n",string-1);
  1344.     }        
  1345.     }
  1346.  
  1347. }
  1348.  
  1349.  
  1350. static void DEFUN_VOID(bang)
  1351. {
  1352.   *(long *)((isp[0])) = isp[-1];
  1353.   isp-=2;
  1354.   icheck_range ();
  1355.   pc++;
  1356. }
  1357.  
  1358. WORD(atsign)
  1359. {
  1360.     isp[0] = *(long *)(isp[0]);
  1361.     pc++;
  1362. }
  1363.  
  1364. WORD(hello)
  1365. {
  1366.   printf("hello\n");
  1367.   pc++;    
  1368. }
  1369.  
  1370.  
  1371.  
  1372. static void DEFUN(read_in, (str, file), 
  1373.        string_type *str AND
  1374.           FILE *file)
  1375. {
  1376.     char buff[10000];    
  1377.     unsigned int r;
  1378.     do 
  1379.     {
  1380.     r = fread(buff, 1, sizeof(buff), file);
  1381.     catbuf(str, buff, r);
  1382.     }
  1383.     while (r);
  1384.     buff[0] = 0;
  1385.     
  1386.     catbuf(str, buff,1);
  1387. }
  1388.  
  1389.  
  1390. static void DEFUN_VOID(usage)
  1391. {
  1392.     fprintf(stderr,"usage: -[d|i|g] <file >file\n");
  1393.     exit(33);    
  1394. }
  1395.  
  1396. /* There is no reliable way to declare exit.  Sometimes it returns
  1397.    int, and sometimes it returns void.  Sometimes it changes between
  1398.    OS releases.  Trying to get it declared correctly in the hosts file
  1399.    is a pointless waste of time.  */
  1400.  
  1401. static void
  1402. chew_exit ()
  1403. {
  1404.   exit (0);
  1405. }
  1406.  
  1407. int DEFUN(main,(ac,av),
  1408. int ac AND
  1409. char *av[])
  1410. {
  1411.   unsigned int i;
  1412.   string_type buffer;
  1413.   string_type pptr;
  1414.  
  1415.   init_string(&buffer);
  1416.   init_string(&pptr);
  1417.   init_string(stack+0);
  1418.   tos=stack+1;
  1419.   ptr = &pptr;
  1420.     
  1421.   add_intrinsic("push_text", push_text);
  1422.   add_intrinsic("!", bang);
  1423.   add_intrinsic("@", atsign);
  1424.   add_intrinsic("hello",hello);    
  1425.   add_intrinsic("skip_past_newline", skip_past_newline );
  1426.   add_intrinsic("catstr", icatstr );
  1427.   add_intrinsic("copy_past_newline", icopy_past_newline );
  1428.   add_intrinsic("dup", other_dup );
  1429.   add_intrinsic("remchar", remchar );
  1430.   add_intrinsic("get_stuff_in_command", get_stuff_in_command );
  1431.   add_intrinsic("do_fancy_stuff", do_fancy_stuff );
  1432.   add_intrinsic("bulletize", bulletize );
  1433.   add_intrinsic("courierize", courierize );
  1434.   /* If the following line gives an error, exit() is not declared in the
  1435.      ../hosts/foo.h file for this host.  Fix it there, not here!  */
  1436.   /* No, don't fix it anywhere; see comment on chew_exit--Ian Taylor.  */
  1437.   add_intrinsic("exit", chew_exit );
  1438.   add_intrinsic("swap", swap );
  1439.   add_intrinsic("outputdots", outputdots );
  1440.   add_intrinsic("paramstuff", paramstuff );
  1441.   add_intrinsic("maybecatstr", maybecatstr );
  1442.   add_intrinsic("translatecomments", translatecomments );
  1443.   add_intrinsic("kill_bogus_lines", kill_bogus_lines);
  1444.   add_intrinsic("indent", indent);
  1445.   add_intrinsic("internalmode", internalmode);
  1446.   add_intrinsic("print_stack_level", print_stack_level);
  1447.   add_intrinsic("strip_trailing_newlines", strip_trailing_newlines);
  1448.     
  1449.   /* Put a nl at the start */
  1450.   catchar(&buffer,'\n');
  1451.  
  1452.   read_in(&buffer, stdin); 
  1453.   remove_noncomments(&buffer, ptr);
  1454.   for (i= 1; i < ac; i++) 
  1455.   {
  1456.     if (av[i][0] == '-')
  1457.     {
  1458.       if (av[i][1] == 'f')
  1459.       {
  1460.     string_type b;
  1461.     FILE *f;
  1462.     init_string(&b);
  1463.  
  1464.     f  = fopen(av[i+1],"r");
  1465.     if (!f) 
  1466.     {
  1467.       fprintf(stderr,"Can't open the input file %s\n",av[i+1]);
  1468.       return 33;
  1469.     }
  1470.  
  1471.     read_in(&b, f);
  1472.     compile(b.ptr);
  1473.     perform();    
  1474.       }
  1475.       else if (av[i][1] == 'i') 
  1476.       {
  1477.     internal_wanted = 1;
  1478.       }
  1479.       else if (av[i][1] == 'w') 
  1480.       {
  1481.     warning = 1;
  1482.       }
  1483.     }
  1484.   }      
  1485.   write_buffer(stack+0);
  1486.   if (tos != stack)
  1487.     {
  1488.       fprintf (stderr, "finishing with current stack level %d\n", tos - stack);
  1489.       return 1;
  1490.     }
  1491.   return 0;
  1492. }
  1493.