home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / gawk2.0.t.Z / gawk2.0.t / awk6.c < prev    next >
Text File  |  1989-01-01  |  13KB  |  632 lines

  1. /*
  2.  * awk6.c -- Various debugging routines 
  3.  *
  4.  * Copyright (C) 1986 Free Software Foundation Written by Jay Fenlason, December
  5.  * 1986 
  6.  *
  7.  * $Log:    awk6.c,v $
  8.  * Revision 1.8  88/11/22  13:51:34  david
  9.  * Arnold: changes for case-insensitive matching
  10.  * 
  11.  * Revision 1.7  88/11/15  10:28:08  david
  12.  * Arnold: minor cleanup
  13.  * 
  14.  * Revision 1.6  88/11/01  12:20:46  david
  15.  * small improvements to debugging code
  16.  * 
  17.  * Revision 1.5  88/10/17  20:53:37  david
  18.  * purge FAST
  19.  * 
  20.  * Revision 1.4  88/05/31  09:56:39  david
  21.  * oops! fix to last change
  22.  * 
  23.  * Revision 1.3  88/05/31  09:25:48  david
  24.  * expunge Node_local_var
  25.  * 
  26.  * Revision 1.2  88/04/15  13:15:47  david
  27.  * brought slightly up-to-date
  28.  * 
  29.  * Revision 1.1  88/04/08  15:14:38  david
  30.  * Initial revision
  31.  *  Revision 1.5  88/04/08  14:48:39  david changes from
  32.  * Arnold Robbins 
  33.  *
  34.  * Revision 1.4  88/03/28  14:13:57  david *** empty log message *** 
  35.  *
  36.  * Revision 1.3  88/03/18  21:00:15  david Baseline -- hoefully all the
  37.  * functionality of the new awk added. Just debugging and tuning to do. 
  38.  *
  39.  * Revision 1.2  87/11/19  14:41:07  david trying to keep it up to date with
  40.  * changes elsewhere ... 
  41.  *
  42.  * Revision 1.1  87/10/27  15:23:36  david Initial revision 
  43.  *
  44.  */
  45.  
  46. /*
  47.  * GAWK is distributed in the hope that it will be useful, but WITHOUT ANY
  48.  * WARRANTY.  No author or distributor accepts responsibility to anyone for
  49.  * the consequences of using it or for whether it serves any particular
  50.  * purpose or works at all, unless he says so in writing. Refer to the GAWK
  51.  * General Public License for full details. 
  52.  *
  53.  * Everyone is granted permission to copy, modify and redistribute GAWK, but
  54.  * only under the conditions described in the GAWK General Public License.  A
  55.  * copy of this license is supposed to have been given to you along with GAWK
  56.  * so you can know your rights and responsibilities.  It should be in a file
  57.  * named COPYING.  Among other things, the copyright notice and this notice
  58.  * must be preserved on all copies. 
  59.  *
  60.  * In other words, go ahead and share GAWK, but don't try to stop anyone else
  61.  * from sharing it farther.  Help stamp out software hoarding! 
  62.  */
  63. #include "awk.h"
  64.  
  65. #ifdef DEBUG
  66.  
  67. extern NODE **fields_arr;
  68.  
  69.  
  70. /* This is all debugging stuff.  Ignore it and maybe it'll go away. */
  71.  
  72. /*
  73.  * Some of it could be turned into a really cute trace command, if anyone
  74.  * wants to.  
  75.  */
  76. char *nnames[] = {
  77.           "Illegal Node",
  78.           "Times", "Divide", "Mod", "Plus", "Minus",
  79.           "Cond-pair", "Subscript", "Concat",
  80.           "++Pre", "--Pre", "Post++",
  81.           "Post--", "Uminus", "Field",
  82.           "Assign", "*=", "/=", "%=",
  83.           "+=", "-=",
  84.           "And", "Or",
  85.           "Equal", "!=", "Less", "Greater", "<=", ">=",
  86.           "Not",
  87.           "Match", "Nomatch",
  88.           "String", "TmpString", "Number",
  89.       "Rule_list", "Rule_node", "State_list", "If_branches", "Exp_list",
  90.           "Param_list",
  91.           "BEGIN", "END", "IF", "WHILE",
  92.           "FOR",
  93.           "arrayfor", "BREAK", "CONTINUE", "PRINT", "PRINTF",
  94.  
  95.           "next", "exit", "DO", "RETURN", "DELETE",
  96.           "redirect", "Append", "pipe", "Pipe in",
  97.           "redirect input", "variable", "Varray",
  98.           "builtin", "Line-range",
  99.           "In_Array", "FUNCTION", "function def", "function call",
  100.           "local variable",
  101.           "getline", "sub", "gsub", "match", "?:",
  102.           "^", "^=", "/regexp/", "Str_num",
  103.           "~~", "!~~",
  104. };
  105.  
  106. ptree(n)
  107. {
  108.     print_parse_tree((NODE *) n);
  109. }
  110.  
  111. pt()
  112. {
  113.     int x;
  114.  
  115.     (void) scanf("%x", &x);
  116.     printf("0x%x\n", x);
  117.     print_parse_tree((NODE *) x);
  118.     fflush(stdout);
  119. }
  120.  
  121. static depth = 0;
  122.  
  123. print_parse_tree(ptr)
  124. NODE *ptr;
  125. {
  126. #ifdef OSK
  127.     char buf[200];
  128. #endif
  129.     if (!ptr) {
  130.         printf("NULL\n");
  131.         return;
  132.     }
  133.     if ((int) (ptr->type) < 0 || (int) (ptr->type) > sizeof(nnames) / sizeof(nnames[0])) {
  134.         printf("(0x%x Type %d??)\n", ptr, ptr->type);
  135.         return;
  136.     }
  137. #ifdef OSK
  138.     sprintf(buf,"(%%d)%%%ds", depth);
  139.     printf(buf, depth, "");
  140.     switch ((int) ptr->type) {
  141.     case (int) Node_val:
  142.         printf("(0x%x Value ", ptr);
  143.         if (ptr->flags&STR) {
  144.             sprintf(buf,"str: \"%%.%ds\" ", ptr->stlen);
  145.             printf(buf , ptr->stptr);
  146.         }
  147.         if (ptr->flags&NUM)
  148.             printf("num: %g", ptr->numbr);
  149.         printf(")\n");
  150.         return;
  151. #else
  152.     printf("(%d)%*s", depth, depth, "");
  153.     switch ((int) ptr->type) {
  154.     case (int) Node_val:
  155.         printf("(0x%x Value ", ptr);
  156.         if (ptr->flags&STR)
  157.             printf("str: \"%.*s\" ", ptr->stlen, ptr->stptr);
  158.         if (ptr->flags&NUM)
  159.             printf("num: %g", ptr->numbr);
  160.         printf(")\n");
  161.         return;
  162. #endif
  163.     case (int) Node_var_array:
  164.         {
  165.         struct search *l;
  166.         NODE **assoc_lookup();
  167.  
  168.         printf("(0x%x Array)\n", ptr);
  169.         for (l = assoc_scan(ptr); l; l = assoc_next(l)) {
  170.             printf("\tindex: ");
  171.             print_parse_tree(l->retval);
  172.             printf("\tvalue: ");
  173.             print_parse_tree(*assoc_lookup(ptr, l->retval));
  174.             printf("\n");
  175.         }
  176.         return;
  177.         }
  178.     case Node_param_list:
  179.         printf("(0x%x Local variable %s)\n", ptr, ptr->param);
  180.         if (ptr->rnode)
  181.             print_parse_tree(ptr->rnode);
  182.         return;
  183.     }
  184.     if (ptr->lnode)
  185.         printf("0x%x = left<--", ptr->lnode);
  186.     printf("(0x%x %s.%d)", ptr, nnames[(int) (ptr->type)], ptr->type);
  187.     if (ptr->rnode)
  188.         printf("-->right = 0x%x", ptr->rnode);
  189.     printf("\n");
  190.     depth++;
  191.     if (ptr->lnode)
  192.         print_parse_tree(ptr->lnode);
  193.     switch ((int) ptr->type) {
  194.     case (int) Node_line_range:
  195.     case (int) Node_match:
  196.     case (int) Node_nomatch:
  197.     case (int) Node_case_match:
  198.     case (int) Node_case_nomatch:
  199.         break;
  200.     case (int) Node_builtin:
  201.         printf("Builtin: %d\n", ptr->proc);
  202.         break;
  203.     case (int) Node_K_for:
  204.     case (int) Node_K_arrayfor:
  205.         printf("(%s:)\n", nnames[(int) (ptr->type)]);
  206.         print_parse_tree(ptr->forloop->init);
  207.         printf("looping:\n");
  208.         print_parse_tree(ptr->forloop->cond);
  209.         printf("doing:\n");
  210.         print_parse_tree(ptr->forloop->incr);
  211.         break;
  212.     default:
  213.         if (ptr->rnode)
  214.             print_parse_tree(ptr->rnode);
  215.         break;
  216.     }
  217.     --depth;
  218. }
  219.  
  220.  
  221. /*
  222.  * print out all the variables in the world 
  223.  */
  224.  
  225. dump_vars()
  226. {
  227.     register int n;
  228.     register HASHNODE *buc;
  229.  
  230. #ifdef notdef
  231.     printf("Fields:");
  232.     dump_fields();
  233. #endif
  234.     printf("Vars:\n");
  235. #ifdef OSK
  236.     for (n = 0; n < HASHSIZE; n++) {
  237.         for (buc = variables[n]; buc; buc = buc->next) {
  238.             char buf[20];
  239.             sprintf(buf,"'%%.%ds': ", buc->length);
  240.             printf(buf, buc->name);
  241.             print_parse_tree(buc->value);
  242.             /* print_parse_tree(buc->value); */
  243.         }
  244.     }
  245. #else
  246.     for (n = 0; n < HASHSIZE; n++) {
  247.         for (buc = variables[n]; buc; buc = buc->next) {
  248.             printf("'%.*s': ", buc->length, buc->name);
  249.             print_parse_tree(buc->value);
  250.             /* print_parse_tree(buc->value); */
  251.         }
  252.     }
  253. #endif
  254.     printf("End\n");
  255. }
  256.  
  257. #ifdef notdef
  258. dump_fields()
  259. {
  260.     register NODE **p;
  261.     register int n;
  262.  
  263.     printf("%d fields\n", f_arr_siz);
  264.     for (n = 0, p = &fields_arr[0]; n < f_arr_siz; n++, p++) {
  265.         printf("$%d is '", n);
  266.         print_simple(*p, stdout);
  267.         printf("'\n");
  268.     }
  269. }
  270. #endif
  271.  
  272. /* VARARGS1 */
  273. print_debug(str, n)
  274. char *str;
  275. {
  276.     extern int debugging;
  277.  
  278.     if (debugging)
  279.         printf("%s:0x%x\n", str, n);
  280. }
  281.  
  282. int indent = 0;
  283.  
  284. print_a_node(ptr)
  285. NODE *ptr;
  286. {
  287.     NODE *p1;
  288.     char *str, *str2;
  289.     int n;
  290.     HASHNODE *buc;
  291.  
  292.     if (!ptr)
  293.         return;        /* don't print null ptrs */
  294.     switch (ptr->type) {
  295.     case Node_val:
  296.         if (ptr->flags&NUM)
  297.             printf("%g", ptr->numbr);
  298.         else
  299. #ifdef OSK
  300.             {
  301.                 char buf[30];
  302.                 sprintf(buf,"\"%%.%ds\"", ptr->stlen);
  303.                 printf( buf, ptr->stptr);
  304.             }
  305. #else
  306.             printf("\"%.*s\"", ptr->stlen, ptr->stptr);
  307. #endif
  308.         return;
  309.     case Node_times:
  310.         str = "*";
  311.         goto pr_twoop;
  312.     case Node_quotient:
  313.         str = "/";
  314.         goto pr_twoop;
  315.     case Node_mod:
  316.         str = "%";
  317.         goto pr_twoop;
  318.     case Node_plus:
  319.         str = "+";
  320.         goto pr_twoop;
  321.     case Node_minus:
  322.         str = "-";
  323.         goto pr_twoop;
  324.     case Node_exp:
  325.         str = "^";
  326.         goto pr_twoop;
  327.     case Node_concat:
  328.         str = " ";
  329.         goto pr_twoop;
  330.     case Node_assign:
  331.         str = "=";
  332.         goto pr_twoop;
  333.     case Node_assign_times:
  334.         str = "*=";
  335.         goto pr_twoop;
  336.     case Node_assign_quotient:
  337.         str = "/=";
  338.         goto pr_twoop;
  339.     case Node_assign_mod:
  340.         str = "%=";
  341.         goto pr_twoop;
  342.     case Node_assign_plus:
  343.         str = "+=";
  344.         goto pr_twoop;
  345.     case Node_assign_minus:
  346.         str = "-=";
  347.         goto pr_twoop;
  348.     case Node_assign_exp:
  349.         str = "^=";
  350.         goto pr_twoop;
  351.     case Node_and:
  352.         str = "&&";
  353.         goto pr_twoop;
  354.     case Node_or:
  355.         str = "||";
  356.         goto pr_twoop;
  357.     case Node_equal:
  358.         str = "==";
  359.         goto pr_twoop;
  360.     case Node_notequal:
  361.         str = "!=";
  362.         goto pr_twoop;
  363.     case Node_less:
  364.         str = "<";
  365.         goto pr_twoop;
  366.     case Node_greater:
  367.         str = ">";
  368.         goto pr_twoop;
  369.     case Node_leq:
  370.         str = "<=";
  371.         goto pr_twoop;
  372.     case Node_geq:
  373.         str = ">=";
  374.         goto pr_twoop;
  375.  
  376. pr_twoop:
  377.         print_a_node(ptr->lnode);
  378.         printf("%s", str);
  379.         print_a_node(ptr->rnode);
  380.         return;
  381.  
  382.     case Node_not:
  383.         str = "!";
  384.         str2 = "";
  385.         goto pr_oneop;
  386.     case Node_field_spec:
  387.         str = "$(";
  388.         str2 = ")";
  389.         goto pr_oneop;
  390.     case Node_postincrement:
  391.         str = "";
  392.         str2 = "++";
  393.         goto pr_oneop;
  394.     case Node_postdecrement:
  395.         str = "";
  396.         str2 = "--";
  397.         goto pr_oneop;
  398.     case Node_preincrement:
  399.         str = "++";
  400.         str2 = "";
  401.         goto pr_oneop;
  402.     case Node_predecrement:
  403.         str = "--";
  404.         str2 = "";
  405.         goto pr_oneop;
  406. pr_oneop:
  407.         printf(str);
  408.         print_a_node(ptr->subnode);
  409.         printf(str2);
  410.         return;
  411.  
  412.     case Node_expression_list:
  413.         print_a_node(ptr->lnode);
  414.         if (ptr->rnode) {
  415.             printf(",");
  416.             print_a_node(ptr->rnode);
  417.         }
  418.         return;
  419.  
  420.     case Node_var:
  421.         for (n = 0; n < HASHSIZE; n++) {
  422.             for (buc = variables[n]; buc; buc = buc->next) {
  423.                 if (buc->value == ptr) {
  424. #ifdef OSK
  425.                     char buf[20];
  426.                     sprintf(buf,"%%.%ds", buc->length);
  427.                     printf(buf, buc->name);
  428. #else
  429.                     printf("%.*s", buc->length, buc->name);
  430. #endif
  431.                     n = HASHSIZE;
  432.                     break;
  433.                 }
  434.             }
  435.         }
  436.         return;
  437.     case Node_subscript:
  438.         print_a_node(ptr->lnode);
  439.         printf("[");
  440.         print_a_node(ptr->rnode);
  441.         printf("]");
  442.         return;
  443.     case Node_builtin:
  444.         printf("some_builtin(");
  445.         print_a_node(ptr->subnode);
  446.         printf(")");
  447.         return;
  448.  
  449.     case Node_statement_list:
  450.         printf("{\n");
  451.         indent++;
  452.         for (n = indent; n; --n)
  453.             printf("  ");
  454.         while (ptr) {
  455.             print_maybe_semi(ptr->lnode);
  456.             if (ptr->rnode)
  457.                 for (n = indent; n; --n)
  458.                     printf("  ");
  459.             ptr = ptr->rnode;
  460.         }
  461.         --indent;
  462.         for (n = indent; n; --n)
  463.             printf("  ");
  464.         printf("}\n");
  465.         for (n = indent; n; --n)
  466.             printf("  ");
  467.         return;
  468.  
  469.     case Node_K_if:
  470.         printf("if(");
  471.         print_a_node(ptr->lnode);
  472.         printf(") ");
  473.         ptr = ptr->rnode;
  474.         if (ptr->lnode->type == Node_statement_list) {
  475.             printf("{\n");
  476.             indent++;
  477.             for (p1 = ptr->lnode; p1; p1 = p1->rnode) {
  478.                 for (n = indent; n; --n)
  479.                     printf("  ");
  480.                 print_maybe_semi(p1->lnode);
  481.             }
  482.             --indent;
  483.             for (n = indent; n; --n)
  484.                 printf("  ");
  485.             if (ptr->rnode) {
  486.                 printf("} else ");
  487.             } else {
  488.                 printf("}\n");
  489.                 return;
  490.             }
  491.         } else {
  492.             print_maybe_semi(ptr->lnode);
  493.             if (ptr->rnode) {
  494.                 for (n = indent; n; --n)
  495.                     printf("  ");
  496.                 printf("else ");
  497.             } else
  498.                 return;
  499.         }
  500.         if (!ptr->rnode)
  501.             return;
  502.         deal_with_curls(ptr->rnode);
  503.         return;
  504.  
  505.     case Node_K_while:
  506.         printf("while(");
  507.         print_a_node(ptr->lnode);
  508.         printf(") ");
  509.         deal_with_curls(ptr->rnode);
  510.         return;
  511.  
  512.     case Node_K_do:
  513.         printf("do ");
  514.         deal_with_curls(ptr->rnode);
  515.         printf("while(");
  516.         print_a_node(ptr->lnode);
  517.         printf(") ");
  518.         return;
  519.  
  520.     case Node_K_for:
  521.         printf("for(");
  522.         print_a_node(ptr->forloop->init);
  523.         printf(";");
  524.         print_a_node(ptr->forloop->cond);
  525.         printf(";");
  526.         print_a_node(ptr->forloop->incr);
  527.         printf(") ");
  528.         deal_with_curls(ptr->forsub);
  529.         return;
  530.     case Node_K_arrayfor:
  531.         printf("for(");
  532.         print_a_node(ptr->forloop->init);
  533.         printf(" in ");
  534.         print_a_node(ptr->forloop->incr);
  535.         printf(") ");
  536.         deal_with_curls(ptr->forsub);
  537.         return;
  538.  
  539.     case Node_K_printf:
  540.         printf("printf(");
  541.         print_a_node(ptr->lnode);
  542.         printf(")");
  543.         return;
  544.     case Node_K_print:
  545.         printf("print(");
  546.         print_a_node(ptr->lnode);
  547.         printf(")");
  548.         return;
  549.     case Node_K_next:
  550.         printf("next");
  551.         return;
  552.     case Node_K_break:
  553.         printf("break");
  554.         return;
  555.     case Node_K_delete:
  556.         printf("delete ");
  557.         print_a_node(ptr->lnode);
  558.         return;
  559.     case Node_func:
  560.         printf("function %s (", ptr->lnode->param);
  561.         if (ptr->lnode->rnode)
  562.             print_a_node(ptr->lnode->rnode);
  563.         printf(")\n");
  564.         print_a_node(ptr->rnode);
  565.         return;
  566.     case Node_param_list:
  567.         printf("%s", ptr->param);
  568.         if (ptr->rnode) {
  569.             printf(", ");
  570.             print_a_node(ptr->rnode);
  571.         }
  572.         return;
  573.     default:
  574.         print_parse_tree(ptr);
  575.         return;
  576.     }
  577. }
  578.  
  579. print_maybe_semi(ptr)
  580. NODE *ptr;
  581. {
  582.     print_a_node(ptr);
  583.     switch (ptr->type) {
  584.     case Node_K_if:
  585.     case Node_K_for:
  586.     case Node_K_arrayfor:
  587.     case Node_statement_list:
  588.         break;
  589.     default:
  590.         printf(";\n");
  591.         break;
  592.     }
  593. }
  594.  
  595. deal_with_curls(ptr)
  596. NODE *ptr;
  597. {
  598.     int n;
  599.  
  600.     if (ptr->type == Node_statement_list) {
  601.         printf("{\n");
  602.         indent++;
  603.         while (ptr) {
  604.             for (n = indent; n; --n)
  605.                 printf("  ");
  606.             print_maybe_semi(ptr->lnode);
  607.             ptr = ptr->rnode;
  608.         }
  609.         --indent;
  610.         for (n = indent; n; --n)
  611.             printf("  ");
  612.         printf("}\n");
  613.     } else {
  614.         print_maybe_semi(ptr);
  615.     }
  616. }
  617.  
  618. NODE *
  619. do_prvars()
  620. {
  621.     dump_vars();
  622.     return Nnull_string;
  623. }
  624.  
  625. NODE *
  626. do_bp()
  627. {
  628.     return Nnull_string;
  629. }
  630.  
  631. #endif
  632.