######################################################################## # Convert File 1 to Microsoft Visual Basic calls, structures and defs # Use pointers listed in PtrLst.awk and functions listed in ExpFnc.awk # # / Equals \057 # [ Equals \133 # \ Equals \134 # ] Equals \135 # * Equals \052 # BEGIN { #################################################################### # Structure Array padding size parameters #################################################################### SAPSiz["CPB_SIZE"] = 0 SAPSiz["DCB_SIZE"] = 0 SAPSiz["CSB_SIZE"] = 0 SAPSiz["RWB_SIZE"] = 0 SAPSiz["XRWB_SIZE"] = 0 SAPSiz["CAR_SIZE"] = 0 SAPSiz["ADCCB_SIZE"] = 0 SAPSiz["DACCB_SIZE"] = 0 #################################################################### # Data type translations #################################################################### VarXlt["long"] = "Long" VarXlt["unsigned"] = "Integer" VarXlt["int"] = "Integer" VarXlt["short"] = "Integer" VarXlt["word"] = "Integer" VarXlt["byte"] = "String" VarXlt["char"] = "String" VarXlt["void"] = "Any" #################################################################### # Scan for list of structure and typedef names, trim trailing blanks #################################################################### TypCur = 0 TypCnt = 0 TypFil = ARGV[1] while (getline TypLin < TypFil > 0) { TypLin = ComBlk(TypLin, TypFil, 0) ################################################################ # Scan typedef's with structures ################################################################ if (((TypLin ~ /typedef/) && (TypLin ~ /struct/)) && (TypLin !~ /;/)) { while ((TypLin !~ /}/) && (getline TypLin < TypFil > 0)) {} TokCnt = split(TypLin, TokArr) for (i = 1; i <= TokCnt; i++) if ((TokArr[i] ~ /}/) && (i+1 <= TokCnt)) { TypArr[TypCnt] = prefix(TokArr[i+1], ";") gsub (/ *$/, "", TypArr[TypCnt]) if (length (TypArr[TypCnt])) VarXlt[TypArr[TypCnt]] = TypArr[TypCnt++] break } } ################################################################ # Scan typedef's without full structures ################################################################ else if ((TypLin ~ /typedef/) && (TypLin ~ /struct/) && (TypLin ~ /;/)) { TokCnt = split(TypLin, TokArr) TypArr[TypCnt] = prefix(TokArr[TokCnt], ";") gsub (/ *$/, "", TypArr[TypCnt]) if (length (TypArr[TypCnt])) VarXlt[TypArr[TypCnt]] = TypArr[TypCnt++] } } close (TypFil) #################################################################### # Load list of function names to delete, trim trailing blanks #################################################################### FncCnt = 0 while (getline FncArr[FncCnt] < "FncDel.awk" > 0) { gsub (/ *$/, "", FncArr[FncCnt]) if (length (FncArr[FncCnt])) FncCnt++ } #################################################################### # Load list of definitions to delete, trim trailing blanks #################################################################### DefCnt = 0 while (getline DefArr[DefCnt] < "DefDel.awk" > 0) { gsub (/ *$/, "", DefArr[DefCnt]) if (length (DefArr[DefCnt])) DefCnt++ } #################################################################### # Add list of OF_ definitions #################################################################### print "'" print "' File Operation OF_ Definitions" print "'" while (getline SrcLin < "OF_MVB.AWK" > 0) { if (SrcLin ~ "define +OF_") { SrcLin = HexRep(SrcLin) SrcLin = DefRep(SrcLin) print SrcLin } } } ######################################################################## ######################################################################## { while (getline < ARGV[1] > 0) { if (length ($0)) { $0 = FncDel($0, ARGV[1], FncArr, FncCnt) $0 = DefDel($0, ARGV[1], DefArr, DefCnt) $0 = DirDel($0, ARGV[1]) $0 = ComBlk($0, ARGV[1], 1) $0 = HexRep($0) $0 = DefRep($0) $0 = TypRep($0, ARGV[1]) $0 = FncRep($0, ARGV[1], FncArr, FncCnt) } print $0 } } ######################################################################## # Remove compiler directives ######################################################################## function DirDel(SrcLin, InpFil) { if ((SrcLin ~ /#ifdef/) || (SrcLin ~ /#ifndef/)) { while (getline SrcLin < InpFil > 0) if (SrcLin ~ /#endif/) { getline SrcLin < InpFil break } } else if (SrcLin ~ /#pragma/) { getline SrcLin < InpFil } return (SrcLin) } ######################################################################## # Reformat comments ######################################################################## function ComBlk(SrcLin, InpFil, OutFlg) { if (SrcLin ~ /\057\052/) { if (SrcLin ~ /\052\057/) return (ComLin(SrcLin)) ################################################################ # Multi line comment ################################################################ gsub("\057\\*", "'", SrcLin) if (OutFlg) print SrcLin while (getline SrcLin < InpFil > 0) { if (SrcLin ~ /\052\057/) { gsub("\\*\\/", "", SrcLin) SrcLin = "'" SrcLin break } else if (OutFlg) print "'" SrcLin } } return (SrcLin) } function ComLin(SrcLin) { gsub("\057\\*", "'", SrcLin) gsub("\\*\\/", "", SrcLin) return (SrcLin) } ######################################################################## # Replace all 0x's with &H's ######################################################################## function HexRep(SrcLin) { if (SrcLin !~ " 0x") return (SrcLin) TokCnt = split(SrcLin, TokArr) for (i = 1; i <= TokCnt; i++) if (TokArr[i] ~ "0x") { HexVal = TokArr[i] gsub("0x", "A_AMP_AH", HexVal) gsub(/l|L/, "A_AMP_A", HexVal) gsub(TokArr[i], HexVal, SrcLin) gsub("A_AMP_A", "\\&", SrcLin) break } return (SrcLin) } ######################################################################## # Replace all #defines with Const = ######################################################################## function DefDel(SrcLin, InpFil, LstArr, LstCnt, i) { if (SrcLin ~ /#define /) { TstLin = SrcLin gsub(" +", " ", TstLin) TokCnt = split(TstLin, TokArr, /[ \\(]/) for (i = 0; i < LstCnt; i++) if (TokArr[2] == LstArr[i]) { getline SrcLin < InpFil SrcLin = DefDel(SrcLin, InpFil, LstArr, LstCnt) break } } return (SrcLin) } function DefRep(SrcLin) { if (SrcLin ~ /#define /) { TokCnt = split(SrcLin, TokArr) for (i = 1; i <= TokCnt; i++) if (TokArr[i] ~ /#define/) { gsub("#define ", "Global Const ", SrcLin) gsub(TokArr[i+1], TokArr[i+1] " =", SrcLin) gsub(" *", "", TokArr[i+1]) SAPSiz[TokArr[i+1]] = TokArr[i+2] break } } return (SrcLin) } ######################################################################## # Replace typedef's and structure ######################################################################## function TypRep(SrcLin, InpFil) { CInLin = prefix(SrcLin, "'") if ((CInLin ~ /typedef/) || ((CInLin ~ /struct/) && (CInLin !~ "\\("))) { ################################################################ # typedef struct x var case ################################################################ if (CInLin ~ /;/) return ("") ################################################################ # typedef struct { } x case ################################################################ print "Type" " " TypArr[TypCur++] while ((prefix(SrcLin, "'") !~ /{/) && (getline SrcLin < InpFil > 0)) {} while (getline SrcLin < InpFil > 0) { if (SrcLin ~ /}/) { SrcLin = "End Type" break } else { SrcLin = ComBlk(SrcLin, InpFil, 1) SrcPre = prefix(SrcLin, "'") SrcSuf = suffix(SrcLin, "'") SrcLin = VarRep(SrcPre, SrcSuf) print SrcLin } } } return (SrcLin) } ######################################################################## # Replace variable types ######################################################################## function VarRep(InsStr, ComStr) { #################################################################### # Assume 1 byte char strings #################################################################### VarLen = 1 #################################################################### # Removed unsigned & short int #################################################################### gsub(" +", " ", InsStr) gsub("short int", "short", InsStr) gsub("long int", "long", InsStr) gsub("unsigned", "", InsStr) #################################################################### # Remove extra spaces and semicolons # Compress array indices # Tokenize #################################################################### gsub(";", " ", InsStr) gsub(" +", " ", InsStr) gsub(" *\\[", "[", InsStr) gsub("\\[", "B_OPN_B", InsStr) gsub("\\]", "B_CLS_B", InsStr) if (!(TokCnt = split(InsStr, TokArr))) return (InsStr ComStr) #################################################################### # Check for arrays #################################################################### if (TokArr[2] ~ "B_OPN_B") { ################################################################ # Isolate array size ################################################################ VarLen = suffix(TokArr[2], "B_OPN_B") gsub("B_OPN_B", "", VarLen) gsub("B_CLS_B", "", VarLen) gsub(TokArr[2], prefix (TokArr[2], "B_OPN_B"), InsStr) TokArr[2] = prefix (TokArr[2], "B_OPN_B") ################################################################ # Reduce to numeric value if possible ################################################################ if (VarLen ~ "\\-|\\+") { if (VarLen ~ "\\-") { ConVal = prefix (VarLen, "-") SAPVal = suffix (VarLen, "-") } if (VarLen ~ "\\+") { ConVal = prefix (VarLen, "+") SAPVal = suffix (VarLen, "+") } if (ConVal in SAPSiz) VarLen = SAPSiz[ConVal] + SAPVal } } #################################################################### # Replace and align "As typename" #################################################################### gsub(TokArr[2], "X_VAR_X", InsStr) if (length (TokArr[2]) >= 16) PadCnt = 8 - (length (TokArr[2]) % 8) else PadCnt = 16 - length (TokArr[2]) gsub(TokArr[1], TokArr[2] padstr(PadCnt), InsStr) if ((VarXlt[TokArr[1]] ~ "String") && (VarLen != 0)) gsub("X_VAR_X", " As " VarXlt[TokArr[1]] "*" VarLen, InsStr) else gsub("X_VAR_X", " As " VarXlt[TokArr[1]], InsStr) #################################################################### # Replace and align comment #################################################################### if (length(ComStr)) { gsub(/ *$/, "", InsStr) if (length (InsStr) >= 32) PadCnt = 8 - (length (InsStr) % 8) else PadCnt = 32 - length (InsStr) InsStr = InsStr padstr(PadCnt) ComStr } return (InsStr) } ######################################################################## # Delete/ Replace Function calls ######################################################################## function FncDel(SrcLin, InpFil, LstArr, LstCnt, i) { if (SrcLin ~ "\\(") for (i = 0; i < LstCnt; i++) if (SrcLin ~ LstArr[i]) { ################################################################ ################################################################ while ((SrcLin !~ ";") && (getline SrcLin < InpFil > 0)) {} getline SrcLin < InpFil SrcLin = FncDel(SrcLin, InpFil, LstArr, LstCnt) break } return (SrcLin) } function FncRep(SrcLin, InpFil, LstArr, LstCnt) { if ((SrcLin ~ "\\(") && (SrcLin ~ "far pascal")) { ################################################################ ################################################################ while ((SrcLin !~ ";") && (getline CntLin < InpFil > 0)) { SrcLin = SrcLin CntLin } ################################################################ ################################################################ InsStr = prefix(SrcLin, "'") ComStr = suffix(SrcLin, "'") ################################################################ # Remove unsigned, short int, true void, single char, ByRef or ByVal ################################################################ gsub("unsigned", "", InsStr) gsub("short int", "short", InsStr) gsub("long int", "long", InsStr) gsub("far pascal", "", InsStr) gsub("^char +far +\\*", "long", InsStr) gsub("char *[\\,]", "int,", InsStr) gsub("char *[\\)]", "int)", InsStr) gsub("far \\*", "B_BYR_B", InsStr) gsub("\\(void\\)", "()", InsStr) ################################################################ ################################################################ gsub(";", " ", InsStr) gsub(" +", " ", InsStr) gsub("^ *", "", InsStr) gsub(" *$", "", InsStr) gsub("\\(", "(P_OPN_P", InsStr) ################################################################ ################################################################ if (!(TokCnt = split(InsStr, TokArr, "[ \\(]"))) return (InsStr ComStr) if (TokArr[1] ~ "void") TokArr[1] = "Declare Sub" else { TokArr[++TokCnt] = "As " VarXlt[TokArr[1]] TokArr[1] = "Declare Function" } TokArr[2] = TokArr[2] " Lib \"DLGTIF.DLL\"" InsStr = join(TokArr, TokCnt, " ") gsub("P_OPN_P", "(", InsStr) ################################################################ ################################################################ gsub("\\)", ")P_CLS_P", InsStr) if (!(TokCnt = split(InsStr, TokArr, "[,\\(\\)]"))) return (InsStr ComStr) VarNum = 1 for (j=2; j<=TokCnt; j++) { if (TokArr[j] ~ "P_CLS_P") break if (TokArr[j] ~ "B_BYR_B") { gsub("B_BYR_B", "", TokArr[j]) if (TokArr[j] ~ "char") ByVPre = "ByVal " else ByVPre = "" } else { ByVPre = "ByVal " } if (length (TokArr[j])) { gsub("^ *", "", TokArr[j]) gsub(" *$", "", TokArr[j]) if (TokArr[j] in VarXlt) TokArr[j] = VarXlt[TokArr[j]] TokArr[j] = "Prm" VarNum++ " As " TokArr[j] TokArr[j] = ByVPre TokArr[j] "," } } TokArr[1] = TokArr[1] "(" ################################################################ ################################################################ InsStr = join(TokArr, TokCnt, " ") gsub(",.P_CLS_P", "P_CLS_P", InsStr) gsub("P_CLS_P", ")", InsStr) gsub(" +", " ", InsStr) SrcLin = InsStr ComStr } return (SrcLin) } ######################################################################## # General purpose functions ######################################################################## function prefix(str, sep) { if (str !~ sep) return (str) return substr(str, 1, index(str, sep) - 1) } function suffix(str, sep) { if (str !~ sep) return ("") return substr(str, index(str, sep)) } function padstr(SpcCnt) { return (substr(" ", 0, SpcCnt)) } function join(StrArr, StrCnt, FldSep) { CatStr = "" while (StrCnt > 0) CatStr = StrArr[StrCnt--] FldSep CatStr return (CatStr) }