2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001,
4 Free Software Foundation, Inc.
5 Contributed by steve chamberlain @cygnus
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
23 /* Yet another way of extracting documentation from source.
24 No, I haven't finished it yet, but I hope you people like it better
29 Basically, this is a sort of string forth, maybe we should call it
32 You define new words thus:
33 : <newword> <oldwords> ;
37 /* Primitives provided by the program:
39 Two stacks are provided, a string stack and an integer stack.
41 Internal state variables:
42 internal_wanted - indicates whether `-i' was passed
43 internal_mode - user-settable
47 ! - pop top of integer stack for address, pop next for value; store
48 @ - treat value on integer stack as the address of an integer; push
49 that integer on the integer stack after popping the "address"
50 hello - print "hello\n" to stdout
51 stdout - put stdout marker on TOS
52 stderr - put stderr marker on TOS
53 print - print TOS-1 on TOS (eg: "hello\n" stdout print)
56 copy_past_newline - append input, up to and including newline into TOS
60 remchar - delete last character from TOS
62 do_fancy_stuff - translate <<foo>> to @code{foo} in TOS
63 bulletize - if "o" lines found, prepend @itemize @bullet to TOS
64 and @item to each "o" line; append @end itemize
65 courierize - put @example around . and | lines, translate {* *} { }
68 outputdots - strip out lines without leading dots
69 paramstuff - convert full declaration into "PARAMS" form if not already
70 maybecatstr - do catstr if internal_mode == internal_wanted, discard
72 translatecomments - turn {* and *} into comment delimiters
73 kill_bogus_lines - get rid of extra newlines
75 internalmode - pop from integer stack, set `internalmode' to that value
76 print_stack_level - print current stack depth to stderr
77 strip_trailing_newlines - go ahead, guess...
78 [quoted string] - push string onto string stack
79 [word starting with digit] - push atol(str) onto integer stack
81 A command must be all upper-case, and alone on a line.
99 /* Here is a string type ... */
101 typedef struct buffer
104 unsigned long write_idx
;
109 static void init_string_with_size (string_type
*, unsigned int);
110 static void init_string (string_type
*);
111 static int find (string_type
*, char *);
112 static void write_buffer (string_type
*, FILE *);
113 static void delete_string (string_type
*);
114 static char *addr (string_type
*, unsigned int);
115 static char at (string_type
*, unsigned int);
116 static void catchar (string_type
*, int);
117 static void overwrite_string (string_type
*, string_type
*);
118 static void catbuf (string_type
*, char *, unsigned int);
119 static void cattext (string_type
*, char *);
120 static void catstr (string_type
*, string_type
*);
124 init_string_with_size (buffer
, size
)
128 buffer
->write_idx
= 0;
130 buffer
->ptr
= malloc (size
);
137 init_string_with_size (buffer
, DEF_SIZE
);
148 for (i
= 0; i
< str
->write_idx
&& *p
; i
++)
150 if (*p
== str
->ptr
[i
])
159 write_buffer (buffer
, f
)
163 fwrite (buffer
->ptr
, buffer
->write_idx
, 1, f
);
167 delete_string (buffer
)
178 return buffer
->ptr
+ idx
;
186 if (pos
>= buffer
->write_idx
)
188 return buffer
->ptr
[pos
];
196 if (buffer
->write_idx
== buffer
->size
)
199 buffer
->ptr
= realloc (buffer
->ptr
, buffer
->size
);
202 buffer
->ptr
[buffer
->write_idx
++] = ch
;
206 overwrite_string (dst
, src
)
211 dst
->size
= src
->size
;
212 dst
->write_idx
= src
->write_idx
;
217 catbuf (buffer
, buf
, len
)
222 if (buffer
->write_idx
+ len
>= buffer
->size
)
224 while (buffer
->write_idx
+ len
>= buffer
->size
)
226 buffer
->ptr
= realloc (buffer
->ptr
, buffer
->size
);
228 memcpy (buffer
->ptr
+ buffer
->write_idx
, buf
, len
);
229 buffer
->write_idx
+= len
;
233 cattext (buffer
, string
)
237 catbuf (buffer
, string
, (unsigned int) strlen (string
));
245 catbuf (dst
, src
->ptr
, src
->write_idx
);
249 skip_white_and_stars (src
, idx
)
254 while ((c
= at (src
, idx
)),
255 isspace ((unsigned char) c
)
257 /* Don't skip past end-of-comment or star as first
258 character on its line. */
259 && at (src
, idx
+1) != '/'
260 && at (src
, idx
-1) != '\n'))
265 /***********************************************************************/
267 string_type stack
[STACK
];
270 unsigned int idx
= 0; /* Pos in input buffer */
271 string_type
*ptr
; /* and the buffer */
272 typedef void (*stinst_type
)();
274 stinst_type sstack
[STACK
];
275 stinst_type
*ssp
= &sstack
[0];
277 long *isp
= &istack
[0];
279 typedef int *word_type
;
284 struct dict_struct
*next
;
291 typedef struct dict_struct dict_type
;
297 fprintf (stderr
, "%s\n", msg
);
305 die ("underflow in string stack");
306 if (tos
>= stack
+ STACK
)
307 die ("overflow in string stack");
314 die ("underflow in integer stack");
315 if (isp
>= istack
+ STACK
)
316 die ("overflow in integer stack");
320 static void exec (dict_type
*);
321 static void call (void);
322 static void remchar (void), strip_trailing_newlines (void), push_number (void);
323 static void push_text (void);
324 static void remove_noncomments (string_type
*, string_type
*);
325 static void print_stack_level (void);
326 static void paramstuff (void), translatecomments (void);
327 static void outputdots (void), courierize (void), bulletize (void);
328 static void do_fancy_stuff (void);
329 static int iscommand (string_type
*, unsigned int);
330 static int copy_past_newline (string_type
*, unsigned int, string_type
*);
331 static void icopy_past_newline (void), kill_bogus_lines (void), indent (void);
332 static void get_stuff_in_command (void), swap (void), other_dup (void);
333 static void drop (void), idrop (void);
334 static void icatstr (void), skip_past_newline (void), internalmode (void);
335 static void maybecatstr (void);
336 static char *nextword (char *, char **);
337 dict_type
*lookup_word (char *);
338 static void perform (void);
339 dict_type
*newentry (char *);
340 unsigned int add_to_definition (dict_type
*, stinst_type
);
341 void add_intrinsic (char *, void (*)());
342 void add_var (char *);
343 void compile (char *);
344 static void bang (void);
345 static void atsign (void);
346 static void hello (void);
347 static void stdout_ (void);
348 static void stderr_ (void);
349 static void print (void);
350 static void read_in (string_type
*, FILE *);
351 static void usage (void);
352 static void chew_exit (void);
367 stinst_type
*oldpc
= pc
;
369 e
= (dict_type
*) (pc
[1]);
383 strip_trailing_newlines ()
385 while ((isspace ((unsigned char) at (tos
, tos
->write_idx
- 1))
386 || at (tos
, tos
->write_idx
- 1) == '\n')
387 && tos
->write_idx
> 0)
409 cattext (tos
, *((char **) pc
));
413 /* This function removes everything not inside comments starting on
414 the first char of the line from the string, also when copying
415 comments, removes blank space and leading *'s.
416 Blank lines are turned into one blank line. */
419 remove_noncomments (src
, dst
)
423 unsigned int idx
= 0;
425 while (at (src
, idx
))
427 /* Now see if we have a comment at the start of the line. */
428 if (at (src
, idx
) == '\n'
429 && at (src
, idx
+ 1) == '/'
430 && at (src
, idx
+ 2) == '*')
434 idx
= skip_white_and_stars (src
, idx
);
436 /* Remove leading dot */
437 if (at (src
, idx
) == '.')
440 /* Copy to the end of the line, or till the end of the
442 while (at (src
, idx
))
444 if (at (src
, idx
) == '\n')
446 /* end of line, echo and scrape of leading blanks */
447 if (at (src
, idx
+ 1) == '\n')
451 idx
= skip_white_and_stars (src
, idx
);
453 else if (at (src
, idx
) == '*' && at (src
, idx
+ 1) == '/')
456 cattext (dst
, "\nENDDD\n");
461 catchar (dst
, at (src
, idx
));
474 fprintf (stderr
, "current string stack depth = %d, ", tos
- stack
);
475 fprintf (stderr
, "current integer stack depth = %d\n", isp
- istack
);
483 name PARAMS ((stuff));
499 /* Make sure that it's not already param'd or proto'd. */
501 || find (tos
, "PARAMS") || find (tos
, "PROTO") || !find (tos
, "("))
507 /* Find the open paren. */
508 for (openp
= 0; at (tos
, openp
) != '(' && at (tos
, openp
); openp
++)
512 /* Step back to the fname. */
514 while (fname
&& isspace ((unsigned char) at (tos
, fname
)))
517 && !isspace ((unsigned char) at (tos
,fname
))
518 && at (tos
,fname
) != '*')
523 /* Output type, omitting trailing whitespace character(s), if
525 for (len
= fname
; 0 < len
; len
--)
527 if (!isspace ((unsigned char) at (tos
, len
- 1)))
530 for (idx
= 0; idx
< len
; idx
++)
531 catchar (&out
, at (tos
, idx
));
533 cattext (&out
, "\n"); /* Insert a newline between type and fnname */
535 /* Output function name, omitting trailing whitespace
536 character(s), if any. */
537 for (len
= openp
; 0 < len
; len
--)
539 if (!isspace ((unsigned char) at (tos
, len
- 1)))
542 for (idx
= fname
; idx
< len
; idx
++)
543 catchar (&out
, at (tos
, idx
));
545 cattext (&out
, " PARAMS (");
547 for (idx
= openp
; at (tos
, idx
) && at (tos
, idx
) != ';'; idx
++)
548 catchar (&out
, at (tos
, idx
));
550 cattext (&out
, ");\n\n");
552 overwrite_string (tos
, &out
);
558 and *} into comments */
563 unsigned int idx
= 0;
567 while (at (tos
, idx
))
569 if (at (tos
, idx
) == '{' && at (tos
, idx
+ 1) == '*')
571 cattext (&out
, "/*");
574 else if (at (tos
, idx
) == '*' && at (tos
, idx
+ 1) == '}')
576 cattext (&out
, "*/");
581 catchar (&out
, at (tos
, idx
));
586 overwrite_string (tos
, &out
);
591 /* Mod tos so that only lines with leading dots remain */
595 unsigned int idx
= 0;
599 while (at (tos
, idx
))
601 if (at (tos
, idx
) == '\n' && at (tos
, idx
+ 1) == '.')
606 while ((c
= at (tos
, idx
)) && c
!= '\n')
608 if (c
== '{' && at (tos
, idx
+ 1) == '*')
610 cattext (&out
, "/*");
613 else if (c
== '*' && at (tos
, idx
+ 1) == '}')
615 cattext (&out
, "*/");
624 catchar (&out
, '\n');
632 overwrite_string (tos
, &out
);
636 /* Find lines starting with . and | and put example around them on tos */
641 unsigned int idx
= 0;
646 while (at (tos
, idx
))
648 if (at (tos
, idx
) == '\n'
649 && (at (tos
, idx
+1 ) == '.'
650 || at (tos
, idx
+ 1) == '|'))
652 cattext (&out
, "\n@example\n");
657 while (at (tos
, idx
) && at (tos
, idx
) != '\n')
661 /* We are inside {} parameters of some command;
662 Just pass through until matching brace. */
663 if (at (tos
, idx
) == '{')
665 else if (at (tos
, idx
) == '}')
668 else if (command
!= 0)
670 if (at (tos
, idx
) == '{')
672 else if (!islower ((unsigned char) at (tos
, idx
)))
675 else if (at (tos
, idx
) == '@'
676 && islower ((unsigned char) at (tos
, idx
+ 1)))
680 else if (at (tos
, idx
) == '{' && at (tos
, idx
+ 1) == '*')
682 cattext (&out
, "/*");
686 else if (at (tos
, idx
) == '*' && at (tos
, idx
+ 1) == '}')
688 cattext (&out
, "*/");
692 else if (at (tos
, idx
) == '{'
693 || at (tos
, idx
) == '}')
698 catchar (&out
, at (tos
, idx
));
701 catchar (&out
, '\n');
703 while (at (tos
, idx
) == '\n'
704 && ((at (tos
, idx
+ 1) == '.')
705 || (at (tos
, idx
+ 1) == '|')))
707 cattext (&out
, "@end example");
711 catchar (&out
, at (tos
, idx
));
716 overwrite_string (tos
, &out
);
720 /* Finds any lines starting with "o ", if there are any, then turns
721 on @itemize @bullet, and @items each of them. Then ends with @end
722 itemize, inplace at TOS*/
727 unsigned int idx
= 0;
732 while (at (tos
, idx
))
734 if (at (tos
, idx
) == '@'
735 && at (tos
, idx
+ 1) == '*')
740 else if (at (tos
, idx
) == '\n'
741 && at (tos
, idx
+ 1) == 'o'
742 && isspace ((unsigned char) at (tos
, idx
+ 2)))
746 cattext (&out
, "\n@itemize @bullet\n");
750 cattext (&out
, "\n@item\n");
755 catchar (&out
, at (tos
, idx
));
756 if (on
&& at (tos
, idx
) == '\n'
757 && at (tos
, idx
+ 1) == '\n'
758 && at (tos
, idx
+ 2) != 'o')
760 cattext (&out
, "@end itemize");
769 cattext (&out
, "@end itemize\n");
777 /* Turn <<foo>> into @code{foo} in place at TOS*/
782 unsigned int idx
= 0;
785 while (at (tos
, idx
))
787 if (at (tos
, idx
) == '<'
788 && at (tos
, idx
+ 1) == '<'
789 && !isspace ((unsigned char) at (tos
, idx
+ 2)))
791 /* This qualifies as a << startup. */
793 cattext (&out
, "@code{");
795 && at (tos
, idx
) != '>' )
797 catchar (&out
, at (tos
, idx
));
806 catchar (&out
, at (tos
, idx
));
816 /* A command is all upper case,and alone on a line. */
823 unsigned int len
= 0;
824 while (at (ptr
, idx
))
826 if (isupper ((unsigned char) at (ptr
, idx
))
827 || at (ptr
, idx
) == ' ' || at (ptr
, idx
) == '_')
832 else if (at (ptr
, idx
) == '\n')
845 copy_past_newline (ptr
, idx
, dst
)
852 while (at (ptr
, idx
) && at (ptr
, idx
) != '\n')
854 if (at (ptr
, idx
) == '\t')
856 /* Expand tabs. Neither makeinfo nor TeX can cope well with
860 while (++column
& 7);
864 catchar (dst
, at (ptr
, idx
));
870 catchar (dst
, at (ptr
, idx
));
877 icopy_past_newline ()
882 idx
= copy_past_newline (ptr
, idx
, tos
);
887 Take the string at the top of the stack, do some prettying. */
900 /* Drop leading nl. */
901 while (at (tos
, idx
) == '\n')
907 /* If the first char is a '.' prepend a newline so that it is
908 recognized properly later. */
909 if (at (tos
, idx
) == '.')
910 catchar (&out
, '\n');
912 /* Find the last char. */
913 while (at (tos
, idx
))
918 /* Find the last non white before the nl. */
921 while (idx
&& isspace ((unsigned char) at (tos
, idx
)))
925 /* Copy buffer upto last char, but blank lines before and after
931 if (at (tos
, c
) == '\n'
932 && at (tos
, c
+ 1) == '\n'
933 && at (tos
, c
+ 2) == '.')
935 /* Ignore two newlines before a dot. */
938 else if (at (tos
, c
) == '.' && sl
)
940 /* remember that this line started with a dot. */
943 else if (at (tos
, c
) == '\n'
944 && at (tos
, c
+ 1) == '\n'
948 /* Ignore two newlines when last line was dot. */
951 catchar (&out
, at (tos
, c
));
952 if (at (tos
, c
) == '\n')
969 catchar (&out
, '\n');
984 while (at (tos
, idx
))
986 switch (at (tos
, idx
))
989 cattext (&out
, "\n");
991 if (tab
&& at (tos
, idx
))
1000 cattext (&out
, " ");
1002 cattext (&out
, "(");
1007 cattext (&out
, ")");
1013 catchar (&out
, at (tos
, idx
));
1022 delete_string (tos
);
1028 get_stuff_in_command ()
1034 while (at (ptr
, idx
))
1036 if (iscommand (ptr
, idx
))
1038 idx
= copy_past_newline (ptr
, idx
, tos
);
1060 catstr (tos
, tos
- 1);
1085 catstr (tos
, tos
+ 1);
1086 delete_string (tos
+ 1);
1091 skip_past_newline ()
1093 while (at (ptr
, idx
)
1094 && at (ptr
, idx
) != '\n')
1103 internal_mode
= *(isp
);
1112 if (internal_wanted
== internal_mode
)
1114 catstr (tos
- 1, tos
);
1116 delete_string (tos
);
1123 nextword (string
, word
)
1134 while (isspace ((unsigned char) *string
) || *string
== '-')
1138 while (*string
&& *string
!= '\n')
1150 word_start
= string
;
1157 if (*string
== '\\')
1163 while (*string
!= '"');
1167 while (!isspace ((unsigned char) *string
))
1175 *word
= malloc (length
+ 1);
1180 for (idx
= 0; idx
< length
; idx
++)
1182 if (src
[idx
] == '\\')
1183 switch (src
[idx
+ 1])
1191 *dst
++ = src
[idx
+ 1];
1215 dict_type
*ptr
= root
;
1218 if (strcmp (ptr
->word
, word
) == 0)
1223 fprintf (stderr
, "Can't find %s\n", word
);
1232 while (at (ptr
, idx
))
1234 /* It's worth looking through the command list. */
1235 if (iscommand (ptr
, idx
))
1240 (void) nextword (addr (ptr
, idx
), &next
);
1242 word
= lookup_word (next
);
1251 fprintf (stderr
, "warning, %s is not recognised\n", next
);
1252 skip_past_newline ();
1257 skip_past_newline ();
1265 dict_type
*new = (dict_type
*) malloc (sizeof (dict_type
));
1269 new->code
= (stinst_type
*) malloc (sizeof (stinst_type
));
1270 new->code_length
= 1;
1276 add_to_definition (entry
, word
)
1280 if (entry
->code_end
== entry
->code_length
)
1282 entry
->code_length
+= 2;
1284 (stinst_type
*) realloc ((char *) (entry
->code
),
1285 entry
->code_length
* sizeof (word_type
));
1287 entry
->code
[entry
->code_end
] = word
;
1289 return entry
->code_end
++;
1293 add_intrinsic (name
, func
)
1297 dict_type
*new = newentry (name
);
1298 add_to_definition (new, func
);
1299 add_to_definition (new, 0);
1306 dict_type
*new = newentry (name
);
1307 add_to_definition (new, push_number
);
1308 add_to_definition (new, (stinst_type
) (&(new->var
)));
1309 add_to_definition (new, 0);
1316 /* Add words to the dictionary. */
1318 string
= nextword (string
, &word
);
1319 while (string
&& *string
&& word
[0])
1321 if (strcmp (word
, "var") == 0)
1323 string
= nextword (string
, &word
);
1326 string
= nextword (string
, &word
);
1328 else if (word
[0] == ':')
1331 /* Compile a word and add to dictionary. */
1332 string
= nextword (string
, &word
);
1334 ptr
= newentry (word
);
1335 string
= nextword (string
, &word
);
1336 while (word
[0] != ';')
1341 /* got a string, embed magic push string
1343 add_to_definition (ptr
, push_text
);
1344 add_to_definition (ptr
, (stinst_type
) (word
+ 1));
1356 /* Got a number, embedd the magic push number
1358 add_to_definition (ptr
, push_number
);
1359 add_to_definition (ptr
, (stinst_type
) atol (word
));
1362 add_to_definition (ptr
, call
);
1363 add_to_definition (ptr
, (stinst_type
) lookup_word (word
));
1366 string
= nextword (string
, &word
);
1368 add_to_definition (ptr
, 0);
1369 string
= nextword (string
, &word
);
1373 fprintf (stderr
, "syntax error at %s\n", string
- 1);
1381 *(long *) ((isp
[0])) = isp
[-1];
1390 isp
[0] = *(long *) (isp
[0]);
1423 write_buffer (tos
, stdout
);
1425 write_buffer (tos
, stderr
);
1427 fprintf (stderr
, "print: illegal print destination `%ld'\n", *isp
);
1444 r
= fread (buff
, 1, sizeof (buff
), file
);
1445 catbuf (str
, buff
, r
);
1450 catbuf (str
, buff
, 1);
1456 fprintf (stderr
, "usage: -[d|i|g] <file >file\n");
1460 /* There is no reliable way to declare exit. Sometimes it returns
1461 int, and sometimes it returns void. Sometimes it changes between
1462 OS releases. Trying to get it declared correctly in the hosts file
1463 is a pointless waste of time. */
1480 init_string (&buffer
);
1481 init_string (&pptr
);
1482 init_string (stack
+ 0);
1486 add_intrinsic ("push_text", push_text
);
1487 add_intrinsic ("!", bang
);
1488 add_intrinsic ("@", atsign
);
1489 add_intrinsic ("hello", hello
);
1490 add_intrinsic ("stdout", stdout_
);
1491 add_intrinsic ("stderr", stderr_
);
1492 add_intrinsic ("print", print
);
1493 add_intrinsic ("skip_past_newline", skip_past_newline
);
1494 add_intrinsic ("catstr", icatstr
);
1495 add_intrinsic ("copy_past_newline", icopy_past_newline
);
1496 add_intrinsic ("dup", other_dup
);
1497 add_intrinsic ("drop", drop
);
1498 add_intrinsic ("idrop", idrop
);
1499 add_intrinsic ("remchar", remchar
);
1500 add_intrinsic ("get_stuff_in_command", get_stuff_in_command
);
1501 add_intrinsic ("do_fancy_stuff", do_fancy_stuff
);
1502 add_intrinsic ("bulletize", bulletize
);
1503 add_intrinsic ("courierize", courierize
);
1504 /* If the following line gives an error, exit() is not declared in the
1505 ../hosts/foo.h file for this host. Fix it there, not here! */
1506 /* No, don't fix it anywhere; see comment on chew_exit--Ian Taylor. */
1507 add_intrinsic ("exit", chew_exit
);
1508 add_intrinsic ("swap", swap
);
1509 add_intrinsic ("outputdots", outputdots
);
1510 add_intrinsic ("paramstuff", paramstuff
);
1511 add_intrinsic ("maybecatstr", maybecatstr
);
1512 add_intrinsic ("translatecomments", translatecomments
);
1513 add_intrinsic ("kill_bogus_lines", kill_bogus_lines
);
1514 add_intrinsic ("indent", indent
);
1515 add_intrinsic ("internalmode", internalmode
);
1516 add_intrinsic ("print_stack_level", print_stack_level
);
1517 add_intrinsic ("strip_trailing_newlines", strip_trailing_newlines
);
1519 /* Put a nl at the start. */
1520 catchar (&buffer
, '\n');
1522 read_in (&buffer
, stdin
);
1523 remove_noncomments (&buffer
, ptr
);
1524 for (i
= 1; i
< (unsigned int) ac
; i
++)
1526 if (av
[i
][0] == '-')
1528 if (av
[i
][1] == 'f')
1534 f
= fopen (av
[i
+ 1], "r");
1537 fprintf (stderr
, "Can't open the input file %s\n",
1546 else if (av
[i
][1] == 'i')
1548 internal_wanted
= 1;
1550 else if (av
[i
][1] == 'w')
1558 write_buffer (stack
+ 0, stdout
);
1561 fprintf (stderr
, "finishing with current stack level %d\n",
This page took 0.060985 seconds and 4 git commands to generate.