home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / BCDASM.ZIP / BCDASM / AWK / PROTO.AWK < prev   
Encoding:
AWK Script  |  1997-06-03  |  3.0 KB  |  110 lines

  1. # AWK script to output prototype definitions for assembly language
  2. # procedures declared with PROC. Optionally output interface-
  3. # related assembler directives.  Morten Elling, Mar 1996, May 1997.
  4. #
  5. # Usage: awk -f proto.awk [LIST2=1 [IDEAL=1]] file.asm [>output.fil]
  6. #    LIST2=1: output lines with certain assembler directives
  7. #    IDEAL=1: assume Ideal syntax for LIST2 directives
  8. #
  9. #
  10. # Note: Understands TASM and MASM's extended PROC syntax, also
  11. #    in TASM's Ideal mode, but is otherwise ignorant.
  12. # ToDo:    Strip ARG equate if present (TASM only).
  13.  
  14.  
  15. #### Make set array from string
  16. function zsplit(str, arr     ,i, j ,temp) {    # i,j,temp = locals
  17.     j = split(str, temp);
  18.     for (i = 1; i <= j; i++)  arr[temp[i]] = 1;
  19. }
  20.  
  21.  
  22. BEGIN { # Initialize
  23.  
  24.     # Lines with specified directives are output if LIST2 > 0
  25.     if (IDEAL) {    # Keyword is 1st token on a line
  26.     list1dir = "model public global extrn procdesc proctype typedef";
  27.     list2dir = "";
  28.     } else {
  29.     list1dir = "model .model public global extrn externdef" \
  30.            " extern dosseg ";
  31.     list2dir = "procdesc proctype typedef proto";
  32.     }
  33.     zsplit(list1dir, list1set);
  34.     zsplit(list2dir, list2set);
  35. }
  36. { # Main loop begins
  37.  
  38. ##### Skip comments
  39. if ($0 ~ /^;/)  next;
  40.  
  41. ##### Combine "\"-ended lines to one
  42. if (prevLn != "") {
  43.     sub(/^[ \t]+/, "");        # Left-trim
  44.     $0 = prevLn " " $0;         # Append to previous line
  45.     prevLn = "";
  46. }
  47.     gsub(/;.*$/, "");        # Remove ";" thru eol
  48. if ($0 ~ /\\.*$/) {            # Remove "\" thru eol
  49.     gsub(/\\.*$/, "");
  50.     prevLn = $0;
  51.     next;                # Skip to next line
  52. }
  53. tok1 = tolower($1);
  54. tok2 = tolower($2);
  55.  
  56. ##### Support for TASM's separate-line ARG
  57. if (inProc) {
  58.     if (tok1 == "uses")  next;    # Skip to next line
  59.     if (tok1 == "local") next;
  60.     inProc = 0;
  61.     if (tok1 == "arg") {
  62.        gsub(/\t/, " ");        # Replace tabs with spaces
  63.        $1 = "";            # Strip off "ARG"
  64.        # Append args to prototype
  65.        protos[count] = protos[count] " " $0;
  66.        next;
  67.     }
  68. }
  69. inProc = 0;
  70.  
  71. ##### Output optional extras
  72. if (LIST2) {
  73.     if (tok1 in list1set) print;
  74.     if (tok2 in list2set) print;
  75. }
  76.  
  77. ##### Handle PROC
  78. if ( tok2 == "proc" || tok1 == "proc" ) {
  79.    # Support for MASM's comma-ended multi-line arg construct
  80.     if ( match($0, /,[ \t]*$/) ) {
  81.         prevLn = $0;
  82.         next;
  83.     }
  84.  
  85.    # Do string replacements
  86.     # print $0            # Un-comment for debug output
  87.     gsub(/\t/, " ");        # Replace tabs with spaces
  88.     # Replace " uses uses_list," with ""
  89.     if (!sub(/ (U|u)(S|s)(E|e)(S|s) [ a-zA-Z]*,/, "", $0 ))
  90.          sub(/ (U|u)(S|s)(E|e)(S|s) [ a-zA-Z]*$/, "", $0 );
  91.     # TASM only: strip off " returns arg_list" at eol
  92.     sub(/ (R|r)(E|e)(T|t)(U|u)(R|r)(N|n)(S|s) [ a-zA-Z0-9_$?:]+$/, "", $0);
  93.     # Replace "proc" with "procdesc"
  94.     if (tok1 == "proc")
  95.        { $1 = "procdesc\t" }    # Ideal syntax
  96.     else if (length($1) < 7)
  97.        { $2 = "\t\tprocdesc" }
  98.     else { $2 = "\tprocdesc" };
  99.  
  100.    # Remember prototype definition
  101.     protos[++count] = $0;
  102.     inProc = 1;            # Flag looking for "ARG"
  103. }
  104. } ##### Main loop ends
  105. END {
  106.     printf("; procdesc equ proto\n");
  107.     for (i = 1; i <= count; i++)
  108.        printf("%s\n", protos[i]);    # Output results
  109. } # eof