diff --git a/sh/core/code.awk b/sh/core/code.awk index bd443dc..469d8f0 100644 --- a/sh/core/code.awk +++ b/sh/core/code.awk @@ -1,3 +1,11 @@ +function alias_function(alias, name) { + print alias "() { " name " \"${@}\"; }" +} + +function alias_variable(alias, name) { + print alias "() { echo \"${" name "}\"; }" +} + function append(line) { if (doc) { doc = doc "\n" @@ -5,12 +13,20 @@ function append(line) { doc = doc line } -function alias_function(alias, target) { - print alias "() { " target " \"${@}\"; }" -} - -function alias_variable(alias, target) { - print alias "() { echo \"${" target "}\"; }" +function extract(string, type) { + if (type == "alias") { + split(string, array, "#=") + return trim(array[2]) + } else if (type == "command") { + split(string, array, "#/") + return trim(array[2]) + } else if (type == "function") { + split(string, array, "()") + return trim(array[1]) + } else if ((type == "constant") || (type == "variable")) { + split(string, array, "=") + return trim(array[1]) + } } function output(name, type) { @@ -26,6 +42,13 @@ function reset() { } } +function trim(string) { + text = string + sub("^[\t ]*", "", text) + sub("[\t ]*$", "", text) + return text +} + BEGIN { RE_ANY = "(.*)" RE_BEGIN = "^" @@ -63,8 +86,8 @@ BEGIN { { if (action == "filter") { - if (match($0, re[target], m)) { - unique[m[1]] = "" + if (match($0, re[target])) { + unique[extract($0, target)] = "" } } else if (action == "tasks") { if (match($0, RE_MODULE, m)) {