home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / gawk2.11 / part14 / debug.c < prev    next >
C/C++ Source or Header  |  1990-06-07  |  11KB  |  562 lines

  1. /*
  2.  * debug.c -- Various debugging routines 
  3.  */
  4.  
  5. /* 
  6.  * Copyright (C) 1986, 1988, 1989 the Free Software Foundation, Inc.
  7.  * 
  8.  * This file is part of GAWK, the GNU implementation of the
  9.  * AWK Progamming Language.
  10.  * 
  11.  * GAWK is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 1, or (at your option)
  14.  * any later version.
  15.  * 
  16.  * GAWK is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * 
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with GAWK; see the file COPYING.  If not, write to
  23.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. #include "awk.h"
  27.  
  28. #ifdef DEBUG
  29.  
  30. extern NODE **fields_arr;
  31.  
  32.  
  33. /* This is all debugging stuff.  Ignore it and maybe it'll go away. */
  34.  
  35. /*
  36.  * Some of it could be turned into a really cute trace command, if anyone
  37.  * wants to.  
  38.  */
  39. char *nnames[] = {
  40.     "illegal", "times", "quotient", "mod", "plus",
  41.     "minus", "cond_pair", "subscript", "concat", "exp",
  42.     /* 10 */
  43.     "preincrement", "predecrement", "postincrement", "postdecrement",
  44.     "unary_minus",
  45.     "field_spec", "assign", "assign_times", "assign_quotient", "assign_mod",
  46.     /* 20 */
  47.     "assign_plus", "assign_minus", "assign_exp", "and", "or",
  48.     "equal", "notequal", "less", "greater", "leq",
  49.     /* 30 */
  50.     "geq", "match", "nomatch", "not", "rule_list",
  51.     "rule_node", "statement_list", "if_branches", "expression_list",
  52.     "param_list",
  53.     /* 40 */
  54.     "K_if", "K_while", "K_for", "K_arrayfor", "K_break",
  55.     "K_continue", "K_print", "K_printf", "K_next", "K_exit",
  56.     /* 50 */
  57.     "K_do", "K_return", "K_delete", "K_getline", "K_function",
  58.     "redirect_output", "redirect_append", "redirect_pipe",
  59.     "redirect_pipein", "redirect_input",
  60.     /* 60 */
  61.     "var", "var_array", "val", "builtin", "line_range",
  62.     "in_array", "func", "func_call", "cond_exp", "regex",
  63.     /* 70 */
  64.     "hashnode", "ahash"
  65. };
  66.  
  67. ptree(n)
  68. NODE *n;
  69. {
  70.     print_parse_tree(n);
  71. }
  72.  
  73. pt()
  74. {
  75.     long x;
  76.  
  77.     (void) scanf("%x", &x);
  78.     printf("0x%x\n", x);
  79.     print_parse_tree((NODE *) x);
  80.     fflush(stdout);
  81. }
  82.  
  83. static depth = 0;
  84.  
  85. print_parse_tree(ptr)
  86. NODE *ptr;
  87. {
  88.     if (!ptr) {
  89.         printf("NULL\n");
  90.         return;
  91.     }
  92.     if ((int) (ptr->type) < 0 || (int) (ptr->type) > sizeof(nnames) / sizeof(nnames[0])) {
  93.         printf("(0x%x Type %d??)\n", ptr, ptr->type);
  94.         return;
  95.     }
  96.     printf("(%d)%*s", depth, depth, "");
  97.     switch ((int) ptr->type) {
  98.     case (int) Node_val:
  99.         printf("(0x%x Value ", ptr);
  100.         if (ptr->flags&STR)
  101.             printf("str: \"%.*s\" ", ptr->stlen, ptr->stptr);
  102.         if (ptr->flags&NUM)
  103.             printf("num: %g", ptr->numbr);
  104.         printf(")\n");
  105.         return;
  106.     case (int) Node_var_array:
  107.         {
  108.         struct search *l;
  109.  
  110.         printf("(0x%x Array)\n", ptr);
  111.         for (l = assoc_scan(ptr); l; l = assoc_next(l)) {
  112.             printf("\tindex: ");
  113.             print_parse_tree(l->retval);
  114.             printf("\tvalue: ");
  115.             print_parse_tree(*assoc_lookup(ptr, l->retval));
  116.             printf("\n");
  117.         }
  118.         return;
  119.         }
  120.     case Node_param_list:
  121.         printf("(0x%x Local variable %s)\n", ptr, ptr->param);
  122.         if (ptr->rnode)
  123.             print_parse_tree(ptr->rnode);
  124.         return;
  125.     case Node_regex:
  126.         printf("(0x%x Regular expression %s\n", ptr, ptr->re_text);
  127.         return;
  128.     }
  129.     if (ptr->lnode)
  130.         printf("0x%x = left<--", ptr->lnode);
  131.     printf("(0x%x %s.%d)", ptr, nnames[(int) (ptr->type)], ptr->type);
  132.     if (ptr->rnode)
  133.         printf("-->right = 0x%x", ptr->rnode);
  134.     printf("\n");
  135.     depth++;
  136.     if (ptr->lnode)
  137.         print_parse_tree(ptr->lnode);
  138.     switch ((int) ptr->type) {
  139.     case (int) Node_line_range:
  140.     case (int) Node_match:
  141.     case (int) Node_nomatch:
  142.         break;
  143.     case (int) Node_builtin:
  144.         printf("Builtin: %d\n", ptr->proc);
  145.         break;
  146.     case (int) Node_K_for:
  147.     case (int) Node_K_arrayfor:
  148.         printf("(%s:)\n", nnames[(int) (ptr->type)]);
  149.         print_parse_tree(ptr->forloop->init);
  150.         printf("looping:\n");
  151.         print_parse_tree(ptr->forloop->cond);
  152.         printf("doing:\n");
  153.         print_parse_tree(ptr->forloop->incr);
  154.         break;
  155.     default:
  156.         if (ptr->rnode)
  157.             print_parse_tree(ptr->rnode);
  158.         break;
  159.     }
  160.     --depth;
  161. }
  162.  
  163.  
  164. /*
  165.  * print out all the variables in the world 
  166.  */
  167.  
  168. dump_vars()
  169. {
  170.     register int n;
  171.     register NODE *buc;
  172.  
  173. #ifdef notdef
  174.     printf("Fields:");
  175.     dump_fields();
  176. #endif
  177.     printf("Vars:\n");
  178.     for (n = 0; n < HASHSIZE; n++) {
  179.         for (buc = variables[n]; buc; buc = buc->hnext) {
  180.             printf("'%.*s': ", buc->hlength, buc->hname);
  181.             print_parse_tree(buc->hvalue);
  182.         }
  183.     }
  184.     printf("End\n");
  185. }
  186.  
  187. #ifdef notdef
  188. dump_fields()
  189. {
  190.     register NODE **p;
  191.     register int n;
  192.  
  193.     printf("%d fields\n", f_arr_siz);
  194.     for (n = 0, p = &fields_arr[0]; n < f_arr_siz; n++, p++) {
  195.         printf("$%d is '", n);
  196.         print_simple(*p, stdout);
  197.         printf("'\n");
  198.     }
  199. }
  200. #endif
  201.  
  202. /* VARARGS1 */
  203. print_debug(str, n)
  204. char *str;
  205. {
  206.     extern int debugging;
  207.  
  208.     if (debugging)
  209.         printf("%s:0x%x\n", str, n);
  210. }
  211.  
  212. int indent = 0;
  213.  
  214. print_a_node(ptr)
  215. NODE *ptr;
  216. {
  217.     NODE *p1;
  218.     char *str, *str2;
  219.     int n;
  220.     NODE *buc;
  221.  
  222.     if (!ptr)
  223.         return;        /* don't print null ptrs */
  224.     switch (ptr->type) {
  225.     case Node_val:
  226.         if (ptr->flags&NUM)
  227.             printf("%g", ptr->numbr);
  228.         else
  229.             printf("\"%.*s\"", ptr->stlen, ptr->stptr);
  230.         return;
  231.     case Node_times:
  232.         str = "*";
  233.         goto pr_twoop;
  234.     case Node_quotient:
  235.         str = "/";
  236.         goto pr_twoop;
  237.     case Node_mod:
  238.         str = "%";
  239.         goto pr_twoop;
  240.     case Node_plus:
  241.         str = "+";
  242.         goto pr_twoop;
  243.     case Node_minus:
  244.         str = "-";
  245.         goto pr_twoop;
  246.     case Node_exp:
  247.         str = "^";
  248.         goto pr_twoop;
  249.     case Node_concat:
  250.         str = " ";
  251.         goto pr_twoop;
  252.     case Node_assign:
  253.         str = "=";
  254.         goto pr_twoop;
  255.     case Node_assign_times:
  256.         str = "*=";
  257.         goto pr_twoop;
  258.     case Node_assign_quotient:
  259.         str = "/=";
  260.         goto pr_twoop;
  261.     case Node_assign_mod:
  262.         str = "%=";
  263.         goto pr_twoop;
  264.     case Node_assign_plus:
  265.         str = "+=";
  266.         goto pr_twoop;
  267.     case Node_assign_minus:
  268.         str = "-=";
  269.         goto pr_twoop;
  270.     case Node_assign_exp:
  271.         str = "^=";
  272.         goto pr_twoop;
  273.     case Node_and:
  274.         str = "&&";
  275.         goto pr_twoop;
  276.     case Node_or:
  277.         str = "||";
  278.         goto pr_twoop;
  279.     case Node_equal:
  280.         str = "==";
  281.         goto pr_twoop;
  282.     case Node_notequal:
  283.         str = "!=";
  284.         goto pr_twoop;
  285.     case Node_less:
  286.         str = "<";
  287.         goto pr_twoop;
  288.     case Node_greater:
  289.         str = ">";
  290.         goto pr_twoop;
  291.     case Node_leq:
  292.         str = "<=";
  293.         goto pr_twoop;
  294.     case Node_geq:
  295.         str = ">=";
  296.         goto pr_twoop;
  297.  
  298. pr_twoop:
  299.         print_a_node(ptr->lnode);
  300.         printf("%s", str);
  301.         print_a_node(ptr->rnode);
  302.         return;
  303.  
  304.     case Node_not:
  305.         str = "!";
  306.         str2 = "";
  307.         goto pr_oneop;
  308.     case Node_field_spec:
  309.         str = "$(";
  310.         str2 = ")";
  311.         goto pr_oneop;
  312.     case Node_postincrement:
  313.         str = "";
  314.         str2 = "++";
  315.         goto pr_oneop;
  316.     case Node_postdecrement:
  317.         str = "";
  318.         str2 = "--";
  319.         goto pr_oneop;
  320.     case Node_preincrement:
  321.         str = "++";
  322.         str2 = "";
  323.         goto pr_oneop;
  324.     case Node_predecrement:
  325.         str = "--";
  326.         str2 = "";
  327.         goto pr_oneop;
  328. pr_oneop:
  329.         printf(str);
  330.         print_a_node(ptr->subnode);
  331.         printf(str2);
  332.         return;
  333.  
  334.     case Node_expression_list:
  335.         print_a_node(ptr->lnode);
  336.         if (ptr->rnode) {
  337.             printf(",");
  338.             print_a_node(ptr->rnode);
  339.         }
  340.         return;
  341.  
  342.     case Node_var:
  343.         for (n = 0; n < HASHSIZE; n++) {
  344.             for (buc = variables[n]; buc; buc = buc->hnext) {
  345.                 if (buc->hvalue == ptr) {
  346.                     printf("%.*s", buc->hlength, buc->hname);
  347.                     n = HASHSIZE;
  348.                     break;
  349.                 }
  350.             }
  351.         }
  352.         return;
  353.     case Node_subscript:
  354.         print_a_node(ptr->lnode);
  355.         printf("[");
  356.         print_a_node(ptr->rnode);
  357.         printf("]");
  358.         return;
  359.     case Node_builtin:
  360.         printf("some_builtin(");
  361.         print_a_node(ptr->subnode);
  362.         printf(")");
  363.         return;
  364.  
  365.     case Node_statement_list:
  366.         printf("{\n");
  367.         indent++;
  368.         for (n = indent; n; --n)
  369.             printf("  ");
  370.         while (ptr) {
  371.             print_maybe_semi(ptr->lnode);
  372.             if (ptr->rnode)
  373.                 for (n = indent; n; --n)
  374.                     printf("  ");
  375.             ptr = ptr->rnode;
  376.         }
  377.         --indent;
  378.         for (n = indent; n; --n)
  379.             printf("  ");
  380.         printf("}\n");
  381.         for (n = indent; n; --n)
  382.             printf("  ");
  383.         return;
  384.  
  385.     case Node_K_if:
  386.         printf("if(");
  387.         print_a_node(ptr->lnode);
  388.         printf(") ");
  389.         ptr = ptr->rnode;
  390.         if (ptr->lnode->type == Node_statement_list) {
  391.             printf("{\n");
  392.             indent++;
  393.             for (p1 = ptr->lnode; p1; p1 = p1->rnode) {
  394.                 for (n = indent; n; --n)
  395.                     printf("  ");
  396.                 print_maybe_semi(p1->lnode);
  397.             }
  398.             --indent;
  399.             for (n = indent; n; --n)
  400.                 printf("  ");
  401.             if (ptr->rnode) {
  402.                 printf("} else ");
  403.             } else {
  404.                 printf("}\n");
  405.                 return;
  406.             }
  407.         } else {
  408.             print_maybe_semi(ptr->lnode);
  409.             if (ptr->rnode) {
  410.                 for (n = indent; n; --n)
  411.                     printf("  ");
  412.                 printf("else ");
  413.             } else
  414.                 return;
  415.         }
  416.         if (!ptr->rnode)
  417.             return;
  418.         deal_with_curls(ptr->rnode);
  419.         return;
  420.  
  421.     case Node_K_while:
  422.         printf("while(");
  423.         print_a_node(ptr->lnode);
  424.         printf(") ");
  425.         deal_with_curls(ptr->rnode);
  426.         return;
  427.  
  428.     case Node_K_do:
  429.         printf("do ");
  430.         deal_with_curls(ptr->rnode);
  431.         printf("while(");
  432.         print_a_node(ptr->lnode);
  433.         printf(") ");
  434.         return;
  435.  
  436.     case Node_K_for:
  437.         printf("for(");
  438.         print_a_node(ptr->forloop->init);
  439.         printf(";");
  440.         print_a_node(ptr->forloop->cond);
  441.         printf(";");
  442.         print_a_node(ptr->forloop->incr);
  443.         printf(") ");
  444.         deal_with_curls(ptr->forsub);
  445.         return;
  446.     case Node_K_arrayfor:
  447.         printf("for(");
  448.         print_a_node(ptr->forloop->init);
  449.         printf(" in ");
  450.         print_a_node(ptr->forloop->incr);
  451.         printf(") ");
  452.         deal_with_curls(ptr->forsub);
  453.         return;
  454.  
  455.     case Node_K_printf:
  456.         printf("printf(");
  457.         print_a_node(ptr->lnode);
  458.         printf(")");
  459.         return;
  460.     case Node_K_print:
  461.         printf("print(");
  462.         print_a_node(ptr->lnode);
  463.         printf(")");
  464.         return;
  465.     case Node_K_next:
  466.         printf("next");
  467.         return;
  468.     case Node_K_break:
  469.         printf("break");
  470.         return;
  471.     case Node_K_delete:
  472.         printf("delete ");
  473.         print_a_node(ptr->lnode);
  474.         return;
  475.     case Node_func:
  476.         printf("function %s (", ptr->lnode->param);
  477.         if (ptr->lnode->rnode)
  478.             print_a_node(ptr->lnode->rnode);
  479.         printf(")\n");
  480.         print_a_node(ptr->rnode);
  481.         return;
  482.     case Node_param_list:
  483.         printf("%s", ptr->param);
  484.         if (ptr->rnode) {
  485.             printf(", ");
  486.             print_a_node(ptr->rnode);
  487.         }
  488.         return;
  489.     default:
  490.         print_parse_tree(ptr);
  491.         return;
  492.     }
  493. }
  494.  
  495. print_maybe_semi(ptr)
  496. NODE *ptr;
  497. {
  498.     print_a_node(ptr);
  499.     switch (ptr->type) {
  500.     case Node_K_if:
  501.     case Node_K_for:
  502.     case Node_K_arrayfor:
  503.     case Node_statement_list:
  504.         break;
  505.     default:
  506.         printf(";\n");
  507.         break;
  508.     }
  509. }
  510.  
  511. deal_with_curls(ptr)
  512. NODE *ptr;
  513. {
  514.     int n;
  515.  
  516.     if (ptr->type == Node_statement_list) {
  517.         printf("{\n");
  518.         indent++;
  519.         while (ptr) {
  520.             for (n = indent; n; --n)
  521.                 printf("  ");
  522.             print_maybe_semi(ptr->lnode);
  523.             ptr = ptr->rnode;
  524.         }
  525.         --indent;
  526.         for (n = indent; n; --n)
  527.             printf("  ");
  528.         printf("}\n");
  529.     } else {
  530.         print_maybe_semi(ptr);
  531.     }
  532. }
  533.  
  534. NODE *
  535. do_prvars()
  536. {
  537.     dump_vars();
  538.     return Nnull_string;
  539. }
  540.  
  541. NODE *
  542. do_bp()
  543. {
  544.     return Nnull_string;
  545. }
  546.  
  547. #endif
  548.  
  549. #ifdef MEMDEBUG
  550.  
  551. #undef free
  552. extern void free();
  553.  
  554. void
  555. do_free(s)
  556. char *s;
  557. {
  558.     free(s);
  559. }
  560.  
  561. #endif
  562.