home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 333_02 / debug.c < prev    next >
C/C++ Source or Header  |  1989-04-22  |  14KB  |  623 lines

  1. /*
  2.    Debug.c -- Various debugging routines
  3.   
  4.    Copyright (C) 1986 Free Software Foundation
  5.      Written by Jay Fenlason, December 1986
  6.   
  7.  */
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #include "awk.h"
  14.  
  15.  
  16.  
  17. STATIC char *    NEAR PASCAL    get_builtin_name(NODE *ptr);
  18. STATIC void    NEAR PASCAL    deal_with_curls(NODE *ptr);
  19.  
  20.  
  21. #ifndef FAST
  22.  
  23. /* This is all debugging stuff.  Ignore it and maybe it'll go away. */
  24.  
  25. struct builtin_names
  26. {
  27.     NODE    *(PASCAL *pptr)(NODE *);
  28.     char    *name;
  29. };
  30.  
  31. static struct builtin_names    bnames[] =
  32.                 {
  33.                     { do_atan2,   "atan2"   },
  34.                     { do_close,   "close"   },
  35.                     { do_cos,      "cos"     },
  36.                     { do_bp,      "bp"        },
  37.                     { do_exp,      "exp"     },
  38.                     { do_getline, "getline" },
  39.                     { do_gsub,      "gsub"    },
  40.                     { do_index,   "index"   },
  41.                     { do_int,      "int"     },
  42.                     { do_length,  "length"  },
  43.                     { do_log,      "log"     },
  44.                     { do_lower,   "lower"   },
  45.                     { do_match,   "match"   },
  46.                     { do_prvars,  "prvars"  },
  47.                     { do_rand,      "rand"    },
  48.                     { do_reverse, "reverse" },
  49.                     { do_sin,      "sin"     },
  50.                     { do_split,   "split"   },
  51.                     { do_sprintf, "sprintf" },
  52.                     { do_sqrt,      "sqrt"    },
  53.                     { do_srand,   "srand"   },
  54.                     { do_sub,      "sub"     },
  55.                     { do_substr,  "substr"  },
  56.                     { do_system,  "system"  },
  57.                     { do_upper,   "upper"   }
  58.                 };
  59.  
  60. static char   *nnames[] = {
  61.                 "ILLEGAL NODE",           /*  0 */
  62.                 "EXPONENTIAL",           /*  1 */
  63.                 "TIMES",               /*  2 */
  64.                 "DIVIDE",               /*  3 */
  65.                 "MOD",               /*  4 */
  66.                 "PLUS",               /*  5 */
  67.                 "MINUS",               /*  6 */
  68.                 "COND PAIR",           /*  7 */
  69.                 "SUBSCRIPT",           /*  8 */
  70.                 "CONCAT",               /*  9 */
  71.                 "++PRE",               /* 10 */
  72.                 "--PRE",               /* 11 */
  73.                 "POST++",               /* 12 */
  74.                 "POST--",               /* 13 */
  75.                 "UMINUS",               /* 14 */
  76.                 "FIELD",               /* 15 */
  77.                 "ASSIGN",               /* 16 */
  78.                 "^=",               /* 17 */
  79.                 "*=",               /* 18 */
  80.                 "/=",               /* 19 */
  81.                 "%=",               /* 20 */
  82.                 "+=",               /* 21 */
  83.                 "-=",               /* 22 */
  84.                 "AND",               /* 23 */
  85.                 "OR",               /* 24 */
  86.                 "EQUAL",               /* 25 */
  87.                 "!=",               /* 26 */
  88.                 "LESS",               /* 27 */
  89.                 "GREATER",               /* 28 */
  90.                 "<=",               /* 29 */
  91.                 ">=",               /* 30 */
  92.                 "NOT",               /* 31 */
  93.                 "MATCH",               /* 32 */
  94.                 "NOMATCH",               /* 33 */
  95.                 "STRING",               /* 34 */
  96.                 "TMPSTRING",           /* 35 */
  97.                 "NUMBER",               /* 36 */
  98.                 "RULE LIST",           /* 37 */
  99.                 "RULE NODE",           /* 38 */
  100.                 "STMT LIST",           /* 39 */
  101.                 "IF BRANCHES",           /* 40 */
  102.                 "EXP LIST",            /* 41 */
  103.                 "BEGIN",               /* 42 */
  104.                 "END",               /* 43 */
  105.                 "IF",               /* 44 */
  106.                 "WHILE",               /* 45 */
  107.                 "FOR",               /* 46 */
  108.                 "ARRAY FOR",           /* 47 */
  109.                 "BREAK",               /* 48 */
  110.                 "CONTINUE",            /* 49 */
  111.                 "PRINT",               /* 50 */
  112.                 "PRINTF",               /* 51 */
  113.                 "NEXT",               /* 52 */
  114.                 "EXIT",               /* 53 */
  115.                 "DELETE",               /* 54 */
  116.                 "GETLINE",               /* 55 */
  117.                 "REDIRECT <",           /* 56 */
  118.                 "REDIRECT >",           /* 57 */
  119.                 "REDIRECT >>",           /* 58 */
  120.                 "REDIRECT |",           /* 59 */
  121.                 "VARIABLE",            /* 60 */
  122.                 "VARRAY",               /* 61 */
  123.                 "BUILTIN",               /* 62 */
  124.                 "LINE RANGE",           /* 63 */
  125.                 "COND EXP",            /* 64 */
  126.                 "CONDEXP BRANCHES",        /* 65 */
  127.                 "REGEXP",               /* 66 */
  128.                 "MEMBER COND"           /* 67 */
  129.               };
  130.  
  131.  
  132. void PASCAL ptree(NODE *n)
  133. {
  134.     print_parse_tree((NODE *) n);
  135.     return;
  136. }
  137.  
  138.  
  139. void PASCAL pt(void)
  140. {
  141.     auto     NODE    *x;
  142.  
  143.     scanf("%p", (FPTR) &x);
  144.     printf("0x%p\n", (FPTR) x);
  145.     print_parse_tree(x);
  146.     fflush(stdout);
  147.     return;
  148. }
  149.  
  150. static          depth = 0;
  151.  
  152. void PASCAL print_parse_tree(register NODE *ptr)
  153. {
  154.     register int    n;
  155.  
  156.     if (!ptr)
  157.     {
  158.     printf("NULL\n");
  159.     return;
  160.     }
  161.  
  162.     if (ptr->type < 0 || ptr->type > MAXDIM(nnames))
  163.     {
  164.     printf("(0x%p Type %d??)\n", (FPTR) ptr, ptr->type);
  165.     return;
  166.     }
  167.  
  168.     printf("(%d)%*s", depth, depth, "");
  169.     switch (ptr->type)
  170.     {
  171.     case NODE_REGEXP:
  172.         printf("(0x%p REG EXPRESSION \"%.*s\"\n",
  173.            (FPTR) ptr, ptr->rereg->used, ptr->rereg->buffer);
  174.         return;
  175.     case NODE_STRING:
  176.     case NODE_TEMP_STRING:
  177.         printf("(0x%p STRING \"%.*s\")\n", (FPTR) ptr, ptr->stlen,
  178.            ptr->stptr);
  179.         return;
  180.     case NODE_NUMBER:
  181.         printf("(0x%p NUMBER %g)\n", (FPTR) ptr, ptr->numbr);
  182.         return;
  183.     case NODE_VAR_ARRAY:
  184.         printf("(0x%p ARRAY OF %d)\n", (FPTR) ptr, ptr->arrsiz);
  185.         for (n = 0; n < ptr->arrsiz; n++)
  186.         {
  187.         printf("'");
  188.         print_simple((ptr->array)[n * 2], stdout);
  189.         printf("' is '");
  190.         print_simple((ptr->array)[n * 2 + 1], stdout);
  191.         printf("'\n");
  192.         }
  193.         return;
  194.     }
  195.     if (ptr->lnode)
  196.     printf("0x%p = <--", (FPTR) ptr->lnode);
  197.     else
  198.     printf("NULL = <--");
  199.     if (NODE_BUILTIN == ptr->type)
  200.     printf("(0x%p %s(%s) [%d])", (FPTR) ptr, nnames[ptr->type],
  201.                       get_builtin_name(ptr), ptr->type);
  202.     else
  203.     printf("(0x%p %s [%d])", (FPTR) ptr, nnames[ptr->type], ptr->type);
  204.     if (ptr->rnode)
  205.     printf("--> = 0x%p", (FPTR) ptr->rnode);
  206.     else
  207.     printf("--> = NULL");
  208.     printf("\n");
  209.     depth++;
  210.     if (ptr->lnode)
  211.     print_parse_tree(ptr->lnode);
  212.     switch (ptr->type)
  213.     {
  214.     case NODE_LINE_RANGE:      /* jfw */
  215.         break;
  216.     case NODE_BUILTIN:
  217.         printf("BUILTIN: %p\n", (FPTR) ptr->proc);
  218.         break;
  219.     case NODE_K_FOR:
  220.     case NODE_K_ARRAYFOR:
  221.         printf("(%s:)\n", nnames[ptr->type]);
  222.         print_parse_tree(ptr->forloop->init);
  223.         printf("looping:\n");
  224.         print_parse_tree(ptr->forloop->cond);
  225.         printf("doing:\n");
  226.         print_parse_tree(ptr->forloop->incr);
  227.         break;
  228.     default:
  229.         if (ptr->rnode)
  230.         print_parse_tree(ptr->rnode);
  231.         break;
  232.     }
  233.     --depth;
  234. }
  235.  
  236.  
  237. STATIC char * NEAR PASCAL get_builtin_name(NODE *ptr)
  238. {
  239.     register int    n;
  240.  
  241.     for (n = MAXDIM(bnames) - 1; n >= 0; ++n)
  242.     {
  243.     if (ptr->proc == bnames[n].pptr)
  244.         break;
  245.     }
  246.     if (n >= 0)
  247.        return(bnames[n].name);
  248.     return("??");
  249. }
  250.  
  251.  
  252. /* print out all the variables in the world */
  253.  
  254. void PASCAL dump_vars(void)
  255. {
  256.     register int     n;
  257.     register HASHNODE    *buc;
  258.  
  259.     printf("Fields:");
  260.     dump_fields();
  261.     printf("Vars:\n");
  262.     for (n = 0; n < HASHSIZE; n++)
  263.     {
  264.     for (buc = variables[n]; buc; buc = buc->next)
  265.     {
  266.         printf("-----> '%.*s': ", buc->length, buc->name);
  267.         print_simple(buc->value->var_value, stdout);
  268.         printf(":");
  269.         print_parse_tree(buc->value->lnode);
  270.     }
  271.     }
  272.     printf("End\n");
  273.     return;
  274. }
  275.  
  276.  
  277. void PASCAL dump_fields(void)
  278. {
  279.     register NODE      **p;
  280.     register int     n;
  281.  
  282.     printf("%d fields\n", f_arr_siz);
  283.     for (n = 0, p = &fields_arr[0]; n < f_arr_siz; n++, p++)
  284.     {
  285.     printf("$%d is '", n);
  286.     print_simple(*p, stdout);
  287.     printf("'\n");
  288.     }
  289.     return;
  290. }
  291.  
  292.  
  293. void print_debug(char *str, ...)
  294. {
  295.     auto     va_list        ap;
  296.  
  297.     if (debugging)
  298.     {
  299.     va_start(ap, str);
  300.     vprintf(str, ap);
  301.     printf("\n");
  302.     va_end(ap);
  303.     }
  304.     return;
  305. }
  306.  
  307.  
  308. int             indent = 0;
  309.  
  310. void PASCAL print_a_node(NODE *ptr)
  311. {
  312.     auto     NODE        *p1;
  313.     auto     char        *str, *str2;
  314.     auto     int         n;
  315.     auto     HASHNODE        *buc;
  316.  
  317.     if (!ptr)
  318.     return;            /* don't print null ptrs */
  319.     switch (ptr->type)
  320.     {
  321.     case NODE_NUMBER:
  322.         printf("%g", ptr->numbr);
  323.         return;
  324.     case NODE_STRING:
  325.         printf("\"%.*s\"", ptr->stlen, ptr->stptr);
  326.         return;
  327.     case NODE_EXPONENTIAL:
  328.         str = "^";
  329.         goto pr_twoop;
  330.     case NODE_TIMES:
  331.         str = "*";
  332.         goto pr_twoop;
  333.     case NODE_QUOTIENT:
  334.         str = "/";
  335.         goto pr_twoop;
  336.     case NODE_MOD:
  337.         str = "%";
  338.         goto pr_twoop;
  339.     case NODE_PLUS:
  340.         str = "+";
  341.         goto pr_twoop;
  342.     case NODE_MINUS:
  343.         str = "-";
  344.         goto pr_twoop;
  345.     case NODE_CONCAT:
  346.         str = " ";
  347.         goto pr_twoop;
  348.     case NODE_ASSIGN:
  349.         str = "=";
  350.         goto pr_twoop;
  351.     case NODE_ASSIGN_EXPONENTIAL:
  352.         str = "^=";
  353.         goto pr_twoop;
  354.     case NODE_ASSIGN_TIMES:
  355.         str = "*=";
  356.         goto pr_twoop;
  357.     case NODE_ASSIGN_QUOTIENT:
  358.         str = "/=";
  359.         goto pr_twoop;
  360.     case NODE_ASSIGN_MOD:
  361.         str = "%=";
  362.         goto pr_twoop;
  363.     case NODE_ASSIGN_PLUS:
  364.         str = "+=";
  365.         goto pr_twoop;
  366.     case NODE_ASSIGN_MINUS:
  367.         str = "-=";
  368.         goto pr_twoop;
  369.     case NODE_AND:
  370.         str = "&&";
  371.         goto pr_twoop;
  372.     case NODE_OR:
  373.         str = "||";
  374.         goto pr_twoop;
  375.     case NODE_EQUAL:
  376.         str = "==";
  377.         goto pr_twoop;
  378.     case NODE_NOTEQUAL:
  379.         str = "!=";
  380.         goto pr_twoop;
  381.     case NODE_LESS:
  382.         str = "<";
  383.         goto pr_twoop;
  384.     case NODE_GREATER:
  385.         str = ">";
  386.         goto pr_twoop;
  387.     case NODE_LEQ:
  388.         str = "<=";
  389.         goto pr_twoop;
  390.     case NODE_GEQ:
  391.         str = ">=";
  392. pr_twoop:
  393.         print_a_node(ptr->lnode);
  394.         printf("%s", str);
  395.         print_a_node(ptr->rnode);
  396.         return;
  397.  
  398.     case NODE_NOT:
  399.         str = "!";
  400.         str2 = "";
  401.         goto pr_oneop;
  402.     case NODE_FIELD_SPEC:
  403.         str = "$(";
  404.         str2 = ")";
  405.         goto pr_oneop;
  406.     case NODE_POSTINCREMENT:
  407.         str = "";
  408.         str2 = "++";
  409.         goto pr_oneop;
  410.     case NODE_POSTDECREMENT:
  411.         str = "";
  412.         str2 = "--";
  413.         goto pr_oneop;
  414.     case NODE_PREINCREMENT:
  415.         str = "++";
  416.         str2 = "";
  417.         goto pr_oneop;
  418.     case NODE_PREDECREMENT:
  419.         str = "--";
  420.         str2 = "";
  421.         goto pr_oneop;
  422. pr_oneop:
  423.         printf(str);
  424.         print_a_node(ptr->subnode);
  425.         printf(str2);
  426.         return;
  427.     case NODE_EXPRESSION_LIST:
  428.         print_a_node(ptr->lnode);
  429.         if (ptr->rnode)
  430.         {
  431.         printf(",");
  432.         print_a_node(ptr->rnode);
  433.         }
  434.         return;
  435.  
  436.     case NODE_VAR:
  437.         for (n = 0; n < HASHSIZE; n++)
  438.         {
  439.         for (buc = variables[n]; buc; buc = buc->next)
  440.         {
  441.             if (buc->value == ptr)
  442.             {
  443.             printf("%.*s", buc->length, buc->name);
  444.             n = HASHSIZE;
  445.             break;
  446.             }
  447.         }
  448.         }
  449.         return;
  450.     case NODE_SUBSCRIPT:
  451.         print_a_node(ptr->lnode);
  452.         printf("[");
  453.         print_a_node(ptr->rnode);
  454.         printf("]");
  455.         return;
  456.     case NODE_BUILTIN:
  457.         printf("some_builtin(");
  458.         print_a_node(ptr->subnode);
  459.         printf(")");
  460.         return;
  461.     case NODE_STATEMENT_LIST:
  462.         printf("{\n");
  463.         indent++;
  464.         for (n = indent; n; --n)
  465.         printf("  ");
  466.         while (ptr)
  467.         {
  468.         print_maybe_semi(ptr->lnode);
  469.         if (ptr->rnode)
  470.             for (n = indent; n; --n)
  471.             printf("  ");
  472.         ptr = ptr->rnode;
  473.         }
  474.         --indent;
  475.         for (n = indent; n; --n)
  476.         printf("  ");
  477.         printf("}\n");
  478.         for (n = indent; n; --n)
  479.         printf("  ");
  480.         return;
  481.     case NODE_K_IF:
  482.         printf("if(");
  483.         print_a_node(ptr->lnode);
  484.         printf(") ");
  485.         ptr = ptr->rnode;
  486.         if (ptr->lnode->type == NODE_STATEMENT_LIST)
  487.         {
  488.         printf("{\n");
  489.         indent++;
  490.         for (p1 = ptr->lnode; p1; p1 = p1->rnode)
  491.         {
  492.             for (n = indent; n; --n)
  493.             printf("  ");
  494.             print_maybe_semi(p1->lnode);
  495.         }
  496.         --indent;
  497.         for (n = indent; n; --n)
  498.             printf("  ");
  499.         if (ptr->rnode)
  500.         {
  501.             printf("} else ");
  502.         }
  503.         else
  504.         {
  505.             printf("}\n");
  506.             return;
  507.         }
  508.         }
  509.         else
  510.         {
  511.         print_maybe_semi(ptr->lnode);
  512.         if (ptr->rnode)
  513.         {
  514.             for (n = indent; n; --n)
  515.             printf("  ");
  516.             printf("else ");
  517.         }
  518.         else
  519.             return;
  520.         }
  521.         if (!ptr->rnode)
  522.         return;
  523.         deal_with_curls(ptr->rnode);
  524.         return;
  525.     case NODE_K_FOR:
  526.         printf("for(");
  527.         print_a_node(ptr->forloop->init);
  528.         printf(";");
  529.         print_a_node(ptr->forloop->cond);
  530.         printf(";");
  531.         print_a_node(ptr->forloop->incr);
  532.         printf(") ");
  533.         deal_with_curls(ptr->forsub);
  534.         return;
  535.     case NODE_K_ARRAYFOR:
  536.         printf("for(");
  537.         print_a_node(ptr->forloop->init);
  538.         printf(" in ");
  539.         print_a_node(ptr->forloop->incr);
  540.         printf(") ");
  541.         deal_with_curls(ptr->forsub);
  542.         return;
  543.     case NODE_K_PRINTF:
  544.         printf("printf(");
  545.         print_a_node(ptr->lnode);
  546.         printf(")");
  547.         return;
  548.     case NODE_K_PRINT:
  549.         printf("print(");
  550.         print_a_node(ptr->lnode);
  551.         printf(")");
  552.         return;
  553.     case NODE_K_NEXT:
  554.         printf("next");
  555.         return;
  556.     case NODE_K_BREAK:
  557.         printf("break");
  558.         return;
  559.     default:
  560.         print_parse_tree(ptr);
  561.     }
  562.     return;
  563. }
  564.  
  565.  
  566. void PASCAL print_maybe_semi(NODE *ptr)
  567. {
  568.     print_a_node(ptr);
  569.     switch (ptr->type)
  570.     {
  571.     case NODE_K_IF:
  572.     case NODE_K_FOR:
  573.     case NODE_K_ARRAYFOR:
  574.     case NODE_STATEMENT_LIST:
  575.         break;
  576.     default:
  577.         printf(";\n");
  578.         break;
  579.     }
  580.     return;
  581. }
  582.  
  583.  
  584. STATIC void NEAR PASCAL deal_with_curls(NODE *ptr)
  585. {
  586.     auto     int    n;
  587.  
  588.     if (ptr->type == NODE_STATEMENT_LIST)
  589.     {
  590.     printf("{\n");
  591.     indent++;
  592.     while (ptr)
  593.     {
  594.         for (n = indent; n; --n)
  595.         printf("  ");
  596.         print_maybe_semi(ptr->lnode);
  597.         ptr = ptr->rnode;
  598.     }
  599.     --indent;
  600.     for (n = indent; n; --n)
  601.         printf("  ");
  602.     printf("}\n");
  603.     }
  604.     else
  605.     print_maybe_semi(ptr);
  606.     return;
  607. }
  608.  
  609.  
  610. NODE * PASCAL do_prvars(NODE *dummy)
  611. {
  612.     dump_vars();
  613.     return(Nnull_string);
  614. }
  615.  
  616.  
  617. NODE * PASCAL do_bp(NODE *dummy)
  618. {
  619.     return(Nnull_string);
  620. }
  621.  
  622. #endif /* FAST */
  623.