EnumSet*.class: Regenerate
[official-gcc.git] / gcc / tree-browser.c
blob89c3bf3a307c8e713fbc5e0710963d22eb2f9fd7
1 /* Tree browser.
2 Copyright (C) 2002, 2003, 2004, 2007 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <s.pop@laposte.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tree-inline.h"
27 #include "diagnostic.h"
28 #include "hashtab.h"
31 #define TB_OUT_FILE stdout
32 #define TB_IN_FILE stdin
33 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
34 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
37 /* Structures for handling Tree Browser's commands. */
38 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
39 enum TB_Comm_code {
40 #include "tree-browser.def"
41 TB_UNUSED_COMMAND
43 #undef DEFTBCODE
44 typedef enum TB_Comm_code TB_CODE;
46 struct tb_command {
47 const char *help_msg;
48 const char *comm_text;
49 size_t comm_len;
50 TB_CODE comm_code;
53 #define DEFTBCODE(code, str, help) { help, str, sizeof(str) - 1, code },
54 static const struct tb_command tb_commands[] =
56 #include "tree-browser.def"
58 #undef DEFTBCODE
60 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
61 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
62 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
63 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
66 /* Next structure is for parsing TREE_CODEs. */
67 struct tb_tree_code {
68 enum tree_code code;
69 const char *code_string;
70 size_t code_string_len;
73 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
74 static const struct tb_tree_code tb_tree_codes[] =
76 #include "tree.def"
78 #undef DEFTREECODE
80 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
81 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
82 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
85 /* Function declarations. */
87 static long TB_getline (char **, long *, FILE *);
88 static TB_CODE TB_get_command (char *);
89 static enum tree_code TB_get_tree_code (char *);
90 static tree find_node_with_code (tree *, int *, void *);
91 static tree store_child_info (tree *, int *, void *);
92 static void TB_update_up (tree);
93 static tree TB_current_chain_node (tree);
94 static tree TB_prev_expr (tree);
95 static tree TB_next_expr (tree);
96 static tree TB_up_expr (tree);
97 static tree TB_first_in_bind (tree);
98 static tree TB_last_in_bind (tree);
99 static int TB_parent_eq (const void *, const void *);
100 static tree TB_history_prev (void);
102 /* FIXME: To be declared in a .h file. */
103 void browse_tree (tree);
105 /* Static variables. */
106 static htab_t TB_up_ht;
107 static tree TB_history_stack = NULL_TREE;
108 static int TB_verbose = 1;
111 /* Entry point in the Tree Browser. */
113 void
114 browse_tree (tree begin)
116 tree head;
117 TB_CODE tbc = TB_UNUSED_COMMAND;
118 ssize_t rd;
119 char *input = NULL;
120 long input_size = 0;
122 fprintf (TB_OUT_FILE, "\nTree Browser\n");
124 #define TB_SET_HEAD(N) do { \
125 TB_history_stack = tree_cons (NULL_TREE, (N), TB_history_stack); \
126 head = N; \
127 if (TB_verbose) \
128 if (head) \
130 print_generic_expr (TB_OUT_FILE, head, 0); \
131 fprintf (TB_OUT_FILE, "\n"); \
133 } while (0)
135 TB_SET_HEAD (begin);
137 /* Store in a hashtable information about previous and upper statements. */
139 TB_up_ht = htab_create (1023, htab_hash_pointer, &TB_parent_eq, NULL);
140 TB_update_up (head);
143 while (24)
145 fprintf (TB_OUT_FILE, "TB> ");
146 rd = TB_getline (&input, &input_size, TB_IN_FILE);
148 if (rd == -1)
149 /* EOF. */
150 goto ret;
152 if (rd != 1)
153 /* Get a new command. Otherwise the user just pressed enter, and thus
154 she expects the last command to be reexecuted. */
155 tbc = TB_get_command (input);
157 switch (tbc)
159 case TB_UPDATE_UP:
160 TB_update_up (head);
161 break;
163 case TB_MAX:
164 if (head && (INTEGRAL_TYPE_P (head)
165 || TREE_CODE (head) == REAL_TYPE
166 || TREE_CODE (head) == FIXED_POINT_TYPE))
167 TB_SET_HEAD (TYPE_MAX_VALUE (head));
168 else
169 TB_WF;
170 break;
172 case TB_MIN:
173 if (head && (INTEGRAL_TYPE_P (head)
174 || TREE_CODE (head) == REAL_TYPE
175 || TREE_CODE (head) == FIXED_POINT_TYPE))
176 TB_SET_HEAD (TYPE_MIN_VALUE (head));
177 else
178 TB_WF;
179 break;
181 case TB_ELT:
182 if (head && TREE_CODE (head) == TREE_VEC)
184 /* This command takes another argument: the element number:
185 for example "elt 1". */
186 TB_NIY;
188 else if (head && TREE_CODE (head) == VECTOR_CST)
190 /* This command takes another argument: the element number:
191 for example "elt 1". */
192 TB_NIY;
194 else
195 TB_WF;
196 break;
198 case TB_VALUE:
199 if (head && TREE_CODE (head) == TREE_LIST)
200 TB_SET_HEAD (TREE_VALUE (head));
201 else
202 TB_WF;
203 break;
205 case TB_PURPOSE:
206 if (head && TREE_CODE (head) == TREE_LIST)
207 TB_SET_HEAD (TREE_PURPOSE (head));
208 else
209 TB_WF;
210 break;
212 case TB_IMAG:
213 if (head && TREE_CODE (head) == COMPLEX_CST)
214 TB_SET_HEAD (TREE_IMAGPART (head));
215 else
216 TB_WF;
217 break;
219 case TB_REAL:
220 if (head && TREE_CODE (head) == COMPLEX_CST)
221 TB_SET_HEAD (TREE_REALPART (head));
222 else
223 TB_WF;
224 break;
226 case TB_BLOCK:
227 if (head && TREE_CODE (head) == BIND_EXPR)
228 TB_SET_HEAD (TREE_OPERAND (head, 2));
229 else
230 TB_WF;
231 break;
233 case TB_SUBBLOCKS:
234 if (head && TREE_CODE (head) == BLOCK)
235 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
236 else
237 TB_WF;
238 break;
240 case TB_SUPERCONTEXT:
241 if (head && TREE_CODE (head) == BLOCK)
242 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
243 else
244 TB_WF;
245 break;
247 case TB_VARS:
248 if (head && TREE_CODE (head) == BLOCK)
249 TB_SET_HEAD (BLOCK_VARS (head));
250 else if (head && TREE_CODE (head) == BIND_EXPR)
251 TB_SET_HEAD (TREE_OPERAND (head, 0));
252 else
253 TB_WF;
254 break;
256 case TB_REFERENCE_TO_THIS:
257 if (head && TYPE_P (head))
258 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
259 else
260 TB_WF;
261 break;
263 case TB_POINTER_TO_THIS:
264 if (head && TYPE_P (head))
265 TB_SET_HEAD (TYPE_POINTER_TO (head));
266 else
267 TB_WF;
268 break;
270 case TB_BASETYPE:
271 if (head && TREE_CODE (head) == OFFSET_TYPE)
272 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
273 else
274 TB_WF;
275 break;
277 case TB_ARG_TYPES:
278 if (head && (TREE_CODE (head) == FUNCTION_TYPE
279 || TREE_CODE (head) == METHOD_TYPE))
280 TB_SET_HEAD (TYPE_ARG_TYPES (head));
281 else
282 TB_WF;
283 break;
285 case TB_METHOD_BASE_TYPE:
286 if (head && (TREE_CODE (head) == FUNCTION_TYPE
287 || TREE_CODE (head) == METHOD_TYPE)
288 && TYPE_METHOD_BASETYPE (head))
289 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
290 else
291 TB_WF;
292 break;
294 case TB_FIELDS:
295 if (head && (TREE_CODE (head) == RECORD_TYPE
296 || TREE_CODE (head) == UNION_TYPE
297 || TREE_CODE (head) == QUAL_UNION_TYPE))
298 TB_SET_HEAD (TYPE_FIELDS (head));
299 else
300 TB_WF;
301 break;
303 case TB_DOMAIN:
304 if (head && TREE_CODE (head) == ARRAY_TYPE)
305 TB_SET_HEAD (TYPE_DOMAIN (head));
306 else
307 TB_WF;
308 break;
310 case TB_VALUES:
311 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
312 TB_SET_HEAD (TYPE_VALUES (head));
313 else
314 TB_WF;
315 break;
317 case TB_ARG_TYPE:
318 if (head && TREE_CODE (head) == PARM_DECL)
319 TB_SET_HEAD (DECL_ARG_TYPE (head));
320 else
321 TB_WF;
322 break;
324 case TB_INITIAL:
325 if (head && DECL_P (head))
326 TB_SET_HEAD (DECL_INITIAL (head));
327 else
328 TB_WF;
329 break;
331 case TB_RESULT:
332 if (head && DECL_P (head))
333 TB_SET_HEAD (DECL_RESULT_FLD (head));
334 else
335 TB_WF;
336 break;
338 case TB_ARGUMENTS:
339 if (head && DECL_P (head))
340 TB_SET_HEAD (DECL_ARGUMENTS (head));
341 else
342 TB_WF;
343 break;
345 case TB_ABSTRACT_ORIGIN:
346 if (head && DECL_P (head))
347 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
348 else if (head && TREE_CODE (head) == BLOCK)
349 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
350 else
351 TB_WF;
352 break;
354 case TB_ATTRIBUTES:
355 if (head && DECL_P (head))
356 TB_SET_HEAD (DECL_ATTRIBUTES (head));
357 else if (head && TYPE_P (head))
358 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
359 else
360 TB_WF;
361 break;
363 case TB_CONTEXT:
364 if (head && DECL_P (head))
365 TB_SET_HEAD (DECL_CONTEXT (head));
366 else if (head && TYPE_P (head)
367 && TYPE_CONTEXT (head))
368 TB_SET_HEAD (TYPE_CONTEXT (head));
369 else
370 TB_WF;
371 break;
373 case TB_OFFSET:
374 if (head && TREE_CODE (head) == FIELD_DECL)
375 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
376 else
377 TB_WF;
378 break;
380 case TB_BIT_OFFSET:
381 if (head && TREE_CODE (head) == FIELD_DECL)
382 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
383 else
384 TB_WF;
385 break;
387 case TB_UNIT_SIZE:
388 if (head && DECL_P (head))
389 TB_SET_HEAD (DECL_SIZE_UNIT (head));
390 else if (head && TYPE_P (head))
391 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
392 else
393 TB_WF;
394 break;
396 case TB_SIZE:
397 if (head && DECL_P (head))
398 TB_SET_HEAD (DECL_SIZE (head));
399 else if (head && TYPE_P (head))
400 TB_SET_HEAD (TYPE_SIZE (head));
401 else
402 TB_WF;
403 break;
405 case TB_TYPE:
406 if (head && TREE_TYPE (head))
407 TB_SET_HEAD (TREE_TYPE (head));
408 else
409 TB_WF;
410 break;
412 case TB_DECL_SAVED_TREE:
413 if (head && TREE_CODE (head) == FUNCTION_DECL
414 && DECL_SAVED_TREE (head))
415 TB_SET_HEAD (DECL_SAVED_TREE (head));
416 else
417 TB_WF;
418 break;
420 case TB_BODY:
421 if (head && TREE_CODE (head) == BIND_EXPR)
422 TB_SET_HEAD (TREE_OPERAND (head, 1));
423 else
424 TB_WF;
425 break;
427 case TB_CHILD_0:
428 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
429 TB_SET_HEAD (TREE_OPERAND (head, 0));
430 else
431 TB_WF;
432 break;
434 case TB_CHILD_1:
435 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
436 TB_SET_HEAD (TREE_OPERAND (head, 1));
437 else
438 TB_WF;
439 break;
441 case TB_CHILD_2:
442 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
443 TB_SET_HEAD (TREE_OPERAND (head, 2));
444 else
445 TB_WF;
446 break;
448 case TB_CHILD_3:
449 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
450 TB_SET_HEAD (TREE_OPERAND (head, 3));
451 else
452 TB_WF;
453 break;
455 case TB_PRINT:
456 if (head)
457 debug_tree (head);
458 else
459 TB_WF;
460 break;
462 case TB_PRETTY_PRINT:
463 if (head)
465 print_generic_stmt (TB_OUT_FILE, head, 0);
466 fprintf (TB_OUT_FILE, "\n");
468 else
469 TB_WF;
470 break;
472 case TB_SEARCH_NAME:
474 break;
476 case TB_SEARCH_CODE:
478 enum tree_code code;
479 char *arg_text;
481 arg_text = strchr (input, ' ');
482 if (arg_text == NULL)
484 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
485 break;
487 code = TB_get_tree_code (arg_text + 1);
489 /* Search in the subtree a node with the given code. */
491 tree res;
493 res = walk_tree (&head, find_node_with_code, &code, NULL);
494 if (res == NULL_TREE)
496 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
498 else
500 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
501 TB_SET_HEAD (res);
504 break;
507 #define TB_MOVE_HEAD(FCT) do { \
508 if (head) \
510 tree t; \
511 t = FCT (head); \
512 if (t) \
513 TB_SET_HEAD (t); \
514 else \
515 TB_WF; \
517 else \
518 TB_WF; \
519 } while (0)
521 case TB_FIRST:
522 TB_MOVE_HEAD (TB_first_in_bind);
523 break;
525 case TB_LAST:
526 TB_MOVE_HEAD (TB_last_in_bind);
527 break;
529 case TB_UP:
530 TB_MOVE_HEAD (TB_up_expr);
531 break;
533 case TB_PREV:
534 TB_MOVE_HEAD (TB_prev_expr);
535 break;
537 case TB_NEXT:
538 TB_MOVE_HEAD (TB_next_expr);
539 break;
541 case TB_HPREV:
542 /* This command is a little bit special, since it deals with history
543 stack. For this reason it should keep the "head = ..." statement
544 and not use TB_MOVE_HEAD. */
545 if (head)
547 tree t;
548 t = TB_history_prev ();
549 if (t)
551 head = t;
552 if (TB_verbose)
554 print_generic_expr (TB_OUT_FILE, head, 0);
555 fprintf (TB_OUT_FILE, "\n");
558 else
559 TB_WF;
561 else
562 TB_WF;
563 break;
565 case TB_CHAIN:
566 /* Don't go further if it's the last node in this chain. */
567 if (head && TREE_CODE (head) == BLOCK)
568 TB_SET_HEAD (BLOCK_CHAIN (head));
569 else if (head && TREE_CHAIN (head))
570 TB_SET_HEAD (TREE_CHAIN (head));
571 else
572 TB_WF;
573 break;
575 case TB_FUN:
576 /* Go up to the current function declaration. */
577 TB_SET_HEAD (current_function_decl);
578 fprintf (TB_OUT_FILE, "Current function declaration.\n");
579 break;
581 case TB_HELP:
582 /* Display a help message. */
584 int i;
585 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
586 for (i = 0; i < TB_UNUSED_COMMAND; i++)
588 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
591 break;
593 case TB_VERBOSE:
594 if (TB_verbose == 0)
596 TB_verbose = 1;
597 fprintf (TB_OUT_FILE, "Verbose on.\n");
599 else
601 TB_verbose = 0;
602 fprintf (TB_OUT_FILE, "Verbose off.\n");
604 break;
606 case TB_EXIT:
607 case TB_QUIT:
608 /* Just exit from this function. */
609 goto ret;
611 default:
612 TB_NIY;
616 ret:;
617 htab_delete (TB_up_ht);
618 return;
622 /* Search the first node in this BIND_EXPR. */
624 static tree
625 TB_first_in_bind (tree node)
627 tree t;
629 if (node == NULL_TREE)
630 return NULL_TREE;
632 while ((t = TB_prev_expr (node)))
633 node = t;
635 return node;
638 /* Search the last node in this BIND_EXPR. */
640 static tree
641 TB_last_in_bind (tree node)
643 tree t;
645 if (node == NULL_TREE)
646 return NULL_TREE;
648 while ((t = TB_next_expr (node)))
649 node = t;
651 return node;
654 /* Search the parent expression for this node. */
656 static tree
657 TB_up_expr (tree node)
659 tree res;
660 if (node == NULL_TREE)
661 return NULL_TREE;
663 res = (tree) htab_find (TB_up_ht, node);
664 return res;
667 /* Search the previous expression in this BIND_EXPR. */
669 static tree
670 TB_prev_expr (tree node)
672 node = TB_current_chain_node (node);
674 if (node == NULL_TREE)
675 return NULL_TREE;
677 node = TB_up_expr (node);
678 if (node && TREE_CODE (node) == COMPOUND_EXPR)
679 return node;
680 else
681 return NULL_TREE;
684 /* Search the next expression in this BIND_EXPR. */
686 static tree
687 TB_next_expr (tree node)
689 node = TB_current_chain_node (node);
691 if (node == NULL_TREE)
692 return NULL_TREE;
694 node = TREE_OPERAND (node, 1);
695 return node;
698 static tree
699 TB_current_chain_node (tree node)
701 if (node == NULL_TREE)
702 return NULL_TREE;
704 if (TREE_CODE (node) == COMPOUND_EXPR)
705 return node;
707 node = TB_up_expr (node);
708 if (node)
710 if (TREE_CODE (node) == COMPOUND_EXPR)
711 return node;
713 node = TB_up_expr (node);
714 if (TREE_CODE (node) == COMPOUND_EXPR)
715 return node;
718 return NULL_TREE;
721 /* For each node store in its children nodes that the current node is their
722 parent. This function is used by walk_tree. */
724 static tree
725 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
726 void *data ATTRIBUTE_UNUSED)
728 tree node;
729 void **slot;
731 node = *tp;
733 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
734 if (EXPR_P (node))
736 int n = TREE_OPERAND_LENGTH (node);
737 int i;
738 for (i = 0; i < n; i++)
740 tree op = TREE_OPERAND (node, i);
741 slot = htab_find_slot (TB_up_ht, op, INSERT);
742 *slot = (void *) node;
746 /* Never stop walk_tree. */
747 return NULL_TREE;
750 /* Function used in TB_up_ht. */
752 static int
753 TB_parent_eq (const void *p1, const void *p2)
755 const_tree const node = (const_tree)p2;
756 const_tree const parent = (const_tree) p1;
758 if (p1 == NULL || p2 == NULL)
759 return 0;
761 if (EXPR_P (parent))
763 int n = TREE_OPERAND_LENGTH (parent);
764 int i;
765 for (i = 0; i < n; i++)
766 if (node == TREE_OPERAND (parent, i))
767 return 1;
769 return 0;
772 /* Update information about upper expressions in the hash table. */
774 static void
775 TB_update_up (tree node)
777 while (node)
779 walk_tree (&node, store_child_info, NULL, NULL);
781 /* Walk function's body. */
782 if (TREE_CODE (node) == FUNCTION_DECL)
783 if (DECL_SAVED_TREE (node))
784 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
786 /* Walk rest of the chain. */
787 node = TREE_CHAIN (node);
789 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
792 /* Parse the input string for determining the command the user asked for. */
794 static TB_CODE
795 TB_get_command (char *input)
797 unsigned int mn, size_tok;
798 int comp;
799 char *space;
801 space = strchr (input, ' ');
802 if (space != NULL)
803 size_tok = strlen (input) - strlen (space);
804 else
805 size_tok = strlen (input) - 1;
807 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
809 if (size_tok != TB_COMMAND_LEN (mn))
810 continue;
812 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
813 if (comp == 0)
814 /* Here we just determined the command. If this command takes
815 an argument, then the argument is determined later. */
816 return TB_COMMAND_CODE (mn);
819 /* Not a valid command. */
820 return TB_UNUSED_COMMAND;
823 /* Parse the input string for determining the tree code. */
825 static enum tree_code
826 TB_get_tree_code (char *input)
828 unsigned int mn, size_tok;
829 int comp;
830 char *space;
832 space = strchr (input, ' ');
833 if (space != NULL)
834 size_tok = strlen (input) - strlen (space);
835 else
836 size_tok = strlen (input) - 1;
838 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
840 if (size_tok != TB_TREE_CODE_LEN (mn))
841 continue;
843 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
844 if (comp == 0)
846 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
847 return TB_TREE_CODE (mn);
851 /* This isn't a valid code. */
852 return LAST_AND_UNUSED_TREE_CODE;
855 /* Find a node with a given code. This function is used as an argument to
856 walk_tree. */
858 static tree
859 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
860 void *data)
862 enum tree_code *code;
863 code = (enum tree_code *) data;
864 if (*code == TREE_CODE (*tp))
865 return *tp;
867 return NULL_TREE;
870 /* Returns a pointer to the last visited node. */
872 static tree
873 TB_history_prev (void)
875 if (TB_history_stack)
877 TB_history_stack = TREE_CHAIN (TB_history_stack);
878 if (TB_history_stack)
879 return TREE_VALUE (TB_history_stack);
881 return NULL_TREE;
884 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
885 (and null-terminate it). *LINEPTR is a pointer returned from malloc
886 (or NULL), pointing to *N characters of space. It is realloc'd as
887 necessary. Returns the number of characters read (not including the
888 null terminator), or -1 on error or EOF.
889 This function comes from sed (and is supposed to be a portable version
890 of getline). */
892 static long
893 TB_getline (char **lineptr, long *n, FILE *stream)
895 char *line, *p;
896 long size, copy;
898 if (lineptr == NULL || n == NULL)
900 errno = EINVAL;
901 return -1;
904 if (ferror (stream))
905 return -1;
907 /* Make sure we have a line buffer to start with. */
908 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
910 #ifndef MAX_CANON
911 #define MAX_CANON 256
912 #endif
913 line = (char *) xrealloc (*lineptr, MAX_CANON);
914 if (line == NULL)
915 return -1;
916 *lineptr = line;
917 *n = MAX_CANON;
920 line = *lineptr;
921 size = *n;
923 copy = size;
924 p = line;
926 while (1)
928 long len;
930 while (--copy > 0)
932 register int c = getc (stream);
933 if (c == EOF)
934 goto lose;
935 else if ((*p++ = c) == '\n')
936 goto win;
939 /* Need to enlarge the line buffer. */
940 len = p - line;
941 size *= 2;
942 line = (char *) xrealloc (line, size);
943 if (line == NULL)
944 goto lose;
945 *lineptr = line;
946 *n = size;
947 p = line + len;
948 copy = size - len;
951 lose:
952 if (p == *lineptr)
953 return -1;
955 /* Return a partial line since we got an error in the middle. */
956 win:
957 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
958 if (p - 2 >= *lineptr && p[-2] == '\r')
959 p[-2] = p[-1], --p;
960 #endif
961 *p = '\0';
962 return p - *lineptr;