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