From 36689f190cd25b99bc4003b5c541783364419cd7 Mon Sep 17 00:00:00 2001 From: xonix Date: Thu, 21 Dec 2023 20:46:02 +0200 Subject: [PATCH] auto-format using the latest intellij-awk : adjust minify.awk --- makesure_candidate | 130 ++++++++++++++++++++++----------------------- minify.awk | 20 +++++-- 2 files changed, 80 insertions(+), 70 deletions(-) diff --git a/makesure_candidate b/makesure_candidate index a89d057..b6e9802 100755 --- a/makesure_candidate +++ b/makesure_candidate @@ -13,7 +13,7 @@ BEGIN { Mode="prelude" srand() prepareArgs() - MyDirScript="MYDIR=" quoteArg(getMyDir(ARGV[1]))";export MYDIR;cd \"$MYDIR\"" + MyDirScript="MYDIR="quoteArg(getMyDir(ARGV[1]))";export MYDIR;cd \"$MYDIR\"" Error="" makesure()} function makesure(i){ @@ -29,7 +29,7 @@ function makesure(i){ else if("@reached_if"==$1)handleReachedIf() else if("@lib"==$1)handleLib() else if("@use_lib"==$1)handleUseLib() - else if($1~/^@/)addError("Unknown directive: " $1) + else if($1~/^@/)addError("Unknown directive: "$1) else handleCodeLine($0) for(i=1;i<10;i++)$i=""} doWork() @@ -49,7 +49,7 @@ function prepareArgs(i,arg){ arrPush(ArgGoals,arg) delete ARGV[i]} if("-h"in Args||"--help"in Args){ - print"makesure ver. " Version + print"makesure ver. "Version print"Usage: makesure [options...] [-f buildfile] [goals...]" print" -f,--file buildfile" print" set buildfile to use (default Makesurefile)" @@ -75,7 +75,7 @@ function prepareArgs(i,arg){ if(isFile(ARGV[1]"/Makesurefile")) ARGV[1]=ARGV[1]"/Makesurefile" else - die("makesure file not found: " ARGV[1])} + die("makesure file not found: "ARGV[1])} if("-s"in Args||"--silent"in Args) Options["silent"] if("-x"in Args||"--tracing"in Args) @@ -96,7 +96,7 @@ function handleOptions(i){ addError("Provide at least one option") for(i=2;i<=NF;i++){ if(!($i in SupportedOptions)) - addError("Option '\''" $i"'\'' is not supported") + addError("Option '\''"$i"'\'' is not supported") Options[$i]}} function handleDefine(){ started("define") @@ -104,14 +104,14 @@ function handleDefine(){ addError("Invalid @define syntax, should be @define VAR_NAME '\''value'\''") return} if($2 !~ /^[A-Za-z_][A-Za-z0-9_]*$/){ - addError("Wrong variable name: '\''" $2"'\''") + addError("Wrong variable name: '\''"$2"'\''") return} if(!($2 in DefineOverrides)) Vars[$2]=$3} function handleShell(){ checkPreludeOnly() if(!((Shell=$2)in SupportedShells)) - addError("Shell '\''" Shell"'\'' is not supported")} + addError("Shell '\''"Shell"'\'' is not supported")} function timingOn(){ return"timing"in Options&&!("silent"in Options)} function started(mode){ @@ -119,7 +119,7 @@ function started(mode){ function handleLib(libName){ started("lib") if((libName=$2)in Lib){ - addError("Lib '\''" libName"'\'' is already defined")} + addError("Lib '\''"libName"'\'' is already defined")} arrPush(LibNames,libName) Lib[libName]} function handleUseLib(i){ @@ -143,13 +143,13 @@ function handleGoal(i,goalName){ GoalParams[goalName,GoalParamsCnt[goalName]++]=validateParamName($i) }else if(NF>2)addError("nothing allowed after goal name")} function validateParamName(p){ - if(p !~ /^[A-Z_][A-Z0-9_]*$/)addError("@param name should match /^[A-Z_][A-Z0-9_]*$/: '\''" p"'\''") + if(p !~ /^[A-Z_][A-Z0-9_]*$/)addError("@param name should match /^[A-Z_][A-Z0-9_]*$/: '\''"p"'\''") return p} function registerGoal(priv,goalName){ if(""==goalName||"@params"==goalName) addError("Goal must have a name") else if(goalName in GoalsByName) - addError("Goal " quote2(goalName,1)" is already defined") + addError("Goal "quote2(goalName,1)" is already defined") else { arrPush(GoalNames,goalName) GoalsByName[goalName]=priv @@ -159,9 +159,9 @@ function calcGlob(goalName,pattern,script,file){ GlobCnt=0 GlobGoalName=goalName delete GlobFiles - script=MyDirScript";for f in " pattern";do test -e \"$f\"&&echo \"$f\"||:;done" + script=MyDirScript";for f in "pattern";do test -e \"$f\"&&echo \"$f\"||:;done" if("sh"!=Shell) - script=Shell" -c " quoteArg(script) + script=Shell" -c "quoteArg(script) while((script|getline file)>0){ GlobCnt++ arrPush(GlobFiles,file)} @@ -204,7 +204,7 @@ function handleDoc(i){ registerDoc(globGoal(i))}} function registerDoc(goalName){ if(goalName in Doc) - addError("Multiple " $1" not allowed for a goal") + addError("Multiple "$1" not allowed for a goal") $1="" Doc[goalName]=trim($0)} function handleDependsOn(i){ @@ -241,10 +241,10 @@ function handleReachedIf(i){ for(i=0;i " loop[2])} + die("There is a loop in goal dependencies via "loop[1]" -> "loop[2])} function isCodeAllowed(){return"goal"==Mode||"goal_glob"==Mode||"lib"==Mode} function isPrelude(){return"prelude"==Mode} -function checkPreludeOnly(){if(!isPrelude())addError("Only use " $1" in prelude")} -function checkGoalOnly(){if("goal"!=Mode&&"goal_glob"!=Mode)addError("Only use " $1" in @goal")} +function checkPreludeOnly(){if(!isPrelude())addError("Only use "$1" in prelude")} +function checkGoalOnly(){if("goal"!=Mode&&"goal_glob"!=Mode)addError("Only use "$1" in @goal")} function currentGoalName(){return arrLast(GoalNames)} function currentLibName(){return arrLast(LibNames)} function realExit(code){ exit code} -function addError(err,n){if(!n)n=NR;Error=addL(Error,err":\n" ARGV[1]":" n": " Lines[n])} +function addError(err,n){if(!n)n=NR;Error=addL(Error,err":\n"ARGV[1]":"n": "Lines[n])} function addErrorDedup(err,n){if((err,n)in AddedErrors)return;AddedErrors[err,n];addError(err,n)} function die(msg,out){ out="cat 1>&2" @@ -364,20 +364,20 @@ function die(msg,out){ function checkConditionReached(goalName,conditionStr,script){ script=getPreludeCode() if(goalName in GoalToLib) - script=script"\n" Lib[GoalToLib[goalName]] - script=script"\n" conditionStr + script=script"\n"Lib[GoalToLib[goalName]] + script=script"\n"conditionStr return shellExec(script,goalName"@reached_if")==0} function shellExec(script,comment,res){ if("tracing"in Options){ - script=": " quoteArg(comment)"\n" script - script=Shell" -x -e -c " quoteArg(script) + script=": "quoteArg(comment)"\n"script + script=Shell" -x -e -c "quoteArg(script) }else - script=Shell" -e -c " quoteArg(script) - script="trap '\''exit 7'\'' INT;" script + script=Shell" -e -c "quoteArg(script) + script="trap '\''exit 7'\'' INT;"script res=system(script) return res} function getMyDir(makesurefilePath){ - return executeGetLine("cd \"$(dirname " quoteArg(makesurefilePath)")\";pwd")} + return executeGetLine("cd \"$(dirname "quoteArg(makesurefilePath)")\";pwd")} function handleCodeLine(line){ if(!isCodeAllowed()&&line !~ /^[ \t]*#/&&trim(line)!=""){ if(!ShellInPreludeErrorShown++) @@ -443,15 +443,15 @@ function instantiate(goal,args,newArgs,i,j,depArg,depArgType,dep,goalNameInstant copyKey(goal,goalNameInstantiated,ReachedIf) copyKey(goal,goalNameInstantiated,GoalToLib) for(i in args) - argsCode=addL(argsCode,i"=" quoteArg(args[i])) + argsCode=addL(argsCode,i"="quoteArg(args[i])) CodePre[goalNameInstantiated]=addL(CodePre[goalNameInstantiated],argsCode) if(goalNameInstantiated in ReachedIf) - ReachedIf[goalNameInstantiated]=argsCode"\n" ReachedIf[goalNameInstantiated]} + ReachedIf[goalNameInstantiated]=argsCode"\n"ReachedIf[goalNameInstantiated]} for(i=0;i " newVer - }else print"you have latest version " Version" installed"} + if(!ok("cp "tmp" "quoteArg(Prog))) + err="can'\''t overwrite "Prog + else print"updated "Version" -> "newVer + }else print"you have latest version "Version" installed"} rm(tmp) if(err)die(err)} function renderDuration(deltaMillis,\ @@ -514,11 +514,11 @@ function renderDuration(deltaMillis,\ secS=deltaSec>0?deltaSec" s":"" secSI=deltaSec>0?int(deltaSec)" s":"" if(dayS!="") - res=dayS" " (hrS==""?"0 h":hrS) + res=dayS" "(hrS==""?"0 h":hrS) else if(deltaHr>0) - res=hrS" " (minS==""?"0 m":minS) + res=hrS" "(minS==""?"0 m":minS) else if(deltaMin>0) - res=minS" " (secSI==""?"0 s":secSI) + res=minS" "(secSI==""?"0 s":secSI) else res=deltaSec>0?secS:"0 s" return res} @@ -526,14 +526,14 @@ function executeGetLine(script,res){ script|getline res closeErr(script) return res} -function closeErr(script){if(close(script)!=0)die("Error executing: " script)} +function closeErr(script){if(close(script)!=0)die("Error executing: "script)} function dl(url,dest,verbose){ verbose="VERBOSE"in ENVIRON if(commandExists("wget")){ - if(!ok("wget " (verbose?"":"-q")" " quoteArg(url)" -O" quoteArg(dest))) + if(!ok("wget "(verbose?"":"-q")" "quoteArg(url)" -O"quoteArg(dest))) return"error with wget" }else if(commandExists("curl")){ - if(!ok("curl " (verbose?"":"-s")" " quoteArg(url)" -o " quoteArg(dest))) + if(!ok("curl "(verbose?"":"-s")" "quoteArg(url)" -o "quoteArg(dest))) return"error with curl" }else return"wget/curl not found"} function natOrder(s1,s2,i1,i2,c1,c2,n1,n2){ @@ -600,7 +600,7 @@ function parseCli_2(line,vars,res,pos,c,c1,isDoll,q,var,inDef,defVal,val,w,i){ for(;(c=substr(line,pos+1,1))~/[_A-Za-z0-9]/;pos++) var=var c if(var !~ /^[_A-Za-z][_A-Za-z0-9]*$/) - return"wrong var: '\''" var"'\''" + return"wrong var: '\''"var"'\''" w=(w)((val=var in vars?vars[var]:ENVIRON[var])!=""?val:defVal) continue} w=w c} @@ -613,13 +613,13 @@ function parseCli_2(line,vars,res,pos,c,c1,isDoll,q,var,inDef,defVal,val,w,i){ while((c=substr(line,++pos,1))!=""&&c!=" "&&c!="\t"){ w=w c} if(w !~ /^[_A-Za-z0-9@.]+$/) - return"wrong unquoted: '\''" w"'\''" + return"wrong unquoted: '\''"w"'\''" res[i=+res[-7]++,"quote"]="u" res[i]=w}}}} function reparseCli(res,i,err){ err=parseCli_2($0,Vars,res) if(err){ - addError("Syntax error: " err) + addError("Syntax error: "err) return -1} $0="" for(i=NF=0;i in res;i++){ @@ -627,18 +627,18 @@ function reparseCli(res,i,err){ Quotes[NF]=res[i,"quote"]} for(i=2;i<=NF;i++) if("\""==Quotes[i]&&!("@define"==$1&&3==i||"@depends_on"==$1&&"@args"==$3&&i>3)){ - addError("Wrong quoting: " $i) + addError("Wrong quoting: "$i) return -1} return 0} function quote2(s,force){ if(index(s,"'\''")){ gsub(/\\/,"\\\\",s) gsub(/'\''/,"\\'\''",s) - return"$'\''" s"'\''" + return"$'\''"s"'\''" }else - return force||s~/[^a-zA-Z0-9.,@_\/=+-]/?"'\''" s"'\''":s} + return force||s~/[^a-zA-Z0-9.,@_\/=+-]/?"'\''"s"'\''":s} function addLine(target,line){target[0]=addL(target[0],line)} -function addL(s,l){return s?s"\n" l:l} +function addL(s,l){return s?s"\n"l:l} function arrPush(arr,elt){arr[arr[-7]++]=elt} function arrLen(arr){return +arr[-7]} function arrDel(arr,v,l,i,e,resArr){ @@ -650,10 +650,10 @@ function arrDel(arr,v,l,i,e,resArr){ for(i in resArr) arr[i]=resArr[i]} function arrLast(arr,l){return (l=arrLen(arr))>0?arr[l-1]:""} -function commandExists(cmd){return ok("command -v " cmd" >/dev/null")} +function commandExists(cmd){return ok("command -v "cmd" >/dev/null")} function ok(cmd){return system(cmd)==0} -function isFile(path){return ok("test -f " quoteArg(path))} -function rm(f){system("rm " quoteArg(f))} -function quoteArg(a){gsub("'\''","'\''\\'\'''\''",a);return"'\''" a"'\''"} +function isFile(path){return ok("test -f "quoteArg(path))} +function rm(f){system("rm "quoteArg(f))} +function quoteArg(a){gsub("'\''","'\''\\'\'''\''",a);return"'\''"a"'\''"} function trim(s){sub(/^[ \t\r\n]+/,"",s);sub(/[ \t\r\n]+$/,"",s);return s} function copyKey(keySrc,keyDst,arr){if(keySrc in arr)arr[keyDst]=arr[keySrc]}' Makesurefile "$@" diff --git a/minify.awk b/minify.awk index 000daab..1251e8e 100644 --- a/minify.awk +++ b/minify.awk @@ -1,23 +1,24 @@ BEGIN { Q = "'" } -function trim(s) { sub(/^[ \t\r\n]+/, "", s); sub(/[ \t\r\n]+$/, "", s); return s } + /^BEGIN/ { in_begin = 1 } in_begin && /^}/ { in_begin = 0 } in_begin && $1 ~ /^delete/{ next } -{ if (!/"#"/ && !/\*#\// && !/\*\(#/) gsub("[ \t\r\n]*#.*$", "") +{ minifyLine() } + +function minifyLine( l) { + if (!/"#"/ && !/\*#\// && !/\*\(#/) gsub("[ \t\r\n]*#.*$", "") gsub(/ == /, "==") gsub(/ = /, "=") gsub(/ != /, "!=") gsub(/ >= /, ">=") gsub(/ <= /, "<=") gsub(/; +/, ";") - gsubKeepStrings(", +", ",") gsub(/ ~ /, "~") gsub(/ > /, ">") gsub(/ < /, "<") gsub(/ \/ /, "/") gsub(/ \* /, "*") gsub(/ \+ /, "+") - gsubKeepStrings(" - ", "-") gsub(/ \|\| /, "||") gsub(/ \| /, "|") if (/ \? /) gsub(/ : /, ":") @@ -31,10 +32,12 @@ in_begin && $1 ~ /^delete/{ next } gsub(/[{] +/, "{") gsub(/} +/, "}") gsub(/[)] +/, ")") - gsubKeepStrings("] +", "]") gsub(/print +"/, "print\"") gsub(/printf +"/, "printf\"") if (!/^ +}/) gsub(/ +}/, "}") + gsubKeepStrings(", +", ",") + gsubKeepStrings(" *- ", "-") + gsubKeepStrings("] +", "]") gsubKeepStrings("^ +in ", "in ") gsubKeepStrings(" +$", "") gsub(Q, Q "\\" Q Q) @@ -55,6 +58,9 @@ function gsubKeepStrings(regex, replacement, nonString,s,isString,i,c) { gsub(regex, replacement, nonString) s = s nonString nonString = "" + } else { + # skip whitespaces after " + while (substr($0,i+1,1) == " ") i++ } s = s c # append this " isString = !isString @@ -68,6 +74,10 @@ function gsubKeepStrings(regex, replacement, nonString,s,isString,i,c) { s = s nonString $0 = s } +function trim(s) { sub(/^[ \t\r\n]+/, "", s); sub(/[ \t\r\n]+$/, "", s); return s } #BEGIN { $0 = "aaa\"aaa\"bbbaaa"; gsubKeepStrings("aaa","AAA"); print } #BEGIN { $0 = "aaa\"aaa\\\"aaa\"bbbaaa"; gsubKeepStrings("aaa","AAA"); print } #BEGIN { $0 = "if (\"-h\" in Args || \"--help\" in Args) {"; gsubKeepStrings("^ +in", "in"); print } +#BEGIN { $0 = " if (arg == \"-f\" || arg == \"--file\") {"; minifyLine() } +#BEGIN { $0 = " res = dayS \" \" (hrS == \"\" ? \"0 h\" : hrS)"; minifyLine() } +#BEGIN { $0 = " quicksort(GlobFiles, 0, arrLen(GlobFiles) - 1)"; minifyLine() }