################################################################################
# AWK program to extract all macros of an ASM8/ASM11 assembly language program
# Author : Tony Papadimitriou <tonyp@acm.org>
# Usage  : (G)AWK -f showmacros.awk filestoexamine [> output]
# History: 2010.09.01 - Original
#        : 2010.11.01 - Disallowed local macros
#        :              Added #IFNOMDEF .. #ENDIF around each macro
################################################################################

function Separator(nl) {
 print nl ";******************************************************************************"
}

BEGIN { TotalMacros = 0; Separator(""); print "; START OF MACROS"; Separator("") }
END { print "; END OF MACROS (Total macros: " TotalMacros ")"; Separator("") }

match(toupper($0),/^\?[^ ]* +MACRO/) { next }   #Skip local macros (comment out to include them)

tolower($2)=="macro",tolower($1)=="endm" {
  if (tolower($2) == "macro") {
    print "; Macro: " $1 " [inside '" FILENAME "']\n"
    print "#ifnomdef " $1 "\n"
    TotalMacros++
    line = 0
    }
  line += 1
  print #line ": " $0 #remove comment from #line to show line numbers
  if (tolower($1) == "endm") {
    print "#endif"
    Separator("\n")
  }
}