rwx/sh/core/code.awk

234 lines
6.2 KiB
Awk
Raw Normal View History

2025-07-13 20:43:18 +02:00
function alias_eval(alias, name, type) {
text = alias "() { "
if (type == "function") {
text = text name " \"${@}\""
} else if (type == "variable") {
text = text "echo \"${" name "}\""
}
print text "; }"
2025-07-13 18:56:54 +02:00
}
2025-07-06 03:16:41 +02:00
function append(line) {
if (doc) {
doc = doc "\n"
}
doc = doc line
}
2025-07-13 18:56:54 +02:00
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])
2025-07-13 22:26:56 +02:00
} else if (type == "doc") {
split(string, array, "#")
return trim(array[2])
2025-07-13 18:56:54 +02:00
} else if (type == "function") {
2025-07-13 21:33:02 +02:00
split(string, array, "(")
2025-07-13 18:56:54 +02:00
return trim(array[1])
2025-07-13 20:24:07 +02:00
} else if (type == "module") {
split(string, array, "#\\.")
return trim(array[2])
2025-07-13 22:15:42 +02:00
} else if (type == "shebang") {
split(string, array, "#!")
return trim(array[2])
2025-07-13 18:56:54 +02:00
} else if ((type == "constant") || (type == "variable")) {
split(string, array, "=")
return trim(array[1])
}
2025-07-11 15:52:38 +02:00
}
2025-07-06 09:42:23 +02:00
function output(name, type) {
2025-07-06 09:49:00 +02:00
print "↙ " type
print name
2025-07-06 03:16:41 +02:00
print doc
exit
}
function reset() {
2025-07-06 10:15:43 +02:00
if (f == "") {
doc = ""
}
2025-07-06 03:16:41 +02:00
}
2025-07-13 18:56:54 +02:00
function trim(string) {
text = string
sub("^[\t ]*", "", text)
sub("[\t ]*$", "", text)
return text
}
2025-07-06 03:16:41 +02:00
BEGIN {
RE_ANY = "(.*)"
2025-07-08 17:01:10 +02:00
RE_BEGIN = "^"
2025-07-06 05:31:51 +02:00
RE_CONST = "([_A-Z][_0-9A-Z]*)"
RE_SET = "=.*"
2025-07-06 03:16:41 +02:00
RE_SPACE = "[[:space:]]"
2025-07-10 22:43:25 +02:00
RE_TSK = "(FIXME|TODO)"
2025-07-06 05:31:51 +02:00
RE_VAR = "([_a-z][_0-9a-z]*)"
2025-07-06 03:16:41 +02:00
2025-07-08 17:01:10 +02:00
RE_SPACES = RE_SPACE "*"
2025-07-06 03:16:41 +02:00
RE_END = RE_SPACES "$"
RE_FUNC = RE_SPACES "\\(" RE_SPACES "\\)" RE_SPACES "{"
2025-07-11 03:03:15 +02:00
re["alias"] = RE_BEGIN "#=" RE_SPACES RE_VAR RE_END
2025-07-11 23:14:57 +02:00
re["binary"] = RE_BEGIN "#\\|" RE_SPACES RE_VAR RE_END
2025-07-06 09:32:52 +02:00
RE_CLOSE = RE_BEGIN "}" RE_SPACES RE_END
2025-07-11 23:34:46 +02:00
re["command"] = RE_BEGIN "#/" RE_SPACES RE_VAR RE_END
2025-07-11 00:22:18 +02:00
RE_COMMENT = RE_BEGIN "#" RE_ANY RE_END
2025-07-11 14:03:25 +02:00
re["constant"] = RE_BEGIN RE_CONST RE_SET RE_END
2025-07-06 10:18:18 +02:00
RE_DOC = RE_BEGIN RE_SPACES "#" RE_SPACE RE_ANY RE_END
2025-07-11 03:17:22 +02:00
re["function"] = RE_BEGIN RE_VAR RE_FUNC RE_END
2025-07-10 22:43:25 +02:00
RE_MODULE = RE_BEGIN "#\\." RE_SPACES RE_ANY RE_END
2025-07-06 03:16:41 +02:00
RE_SHEBANG = RE_BEGIN "#!" RE_SPACES RE_ANY RE_END
2025-07-10 22:43:25 +02:00
RE_TASK = RE_BEGIN RE_SPACES "#" RE_SPACES RE_TSK RE_ANY RE_END
2025-07-11 03:31:24 +02:00
re["variable"] = RE_BEGIN RE_VAR RE_SET RE_END
2025-07-06 03:16:41 +02:00
2025-07-06 10:15:43 +02:00
f = ""
2025-07-08 21:23:49 +02:00
match_alias = 0
match_command = 0
2025-07-08 21:25:44 +02:00
match_module = 0
2025-07-11 00:22:18 +02:00
match_task = 0
2025-07-06 03:16:41 +02:00
reset()
}
{
2025-07-11 03:03:15 +02:00
if (action == "filter") {
2025-07-13 18:56:54 +02:00
if (match($0, re[target])) {
unique[extract($0, target)] = ""
2025-07-07 02:10:05 +02:00
}
2025-07-10 22:43:25 +02:00
} else if (action == "tasks") {
2025-07-13 20:24:07 +02:00
if (match($0, RE_MODULE)) {
2025-07-11 00:22:18 +02:00
if (output_tasks) {
print ""
print output_module
print output_tasks
output_tasks = ""
}
doc = ""
match_task = 0
2025-07-13 20:24:07 +02:00
output_module = ". " extract($0, "module")
} else if (match($0, RE_TASK)) {
2025-07-11 15:49:23 +02:00
if (target) {
2025-07-13 20:24:07 +02:00
if (target == extract($0, "task")) {
2025-07-11 15:49:23 +02:00
match_task = 1
}
} else {
match_task = 1
}
2025-07-11 00:22:18 +02:00
append($0)
2025-07-13 20:24:07 +02:00
} else if (match($0, RE_COMMENT)) {
2025-07-11 00:22:18 +02:00
append($0)
} else {
if (match_task) {
output_tasks = output_tasks "\n" doc
}
doc = ""
match_task = 0
2025-07-10 22:43:25 +02:00
}
2025-07-13 21:57:38 +02:00
} else if (action == "eval") {
if (match($0, re[target])) {
append(extract($0, target))
2025-07-13 21:39:54 +02:00
} else if (match($0, re["function"])) {
2025-07-08 16:06:51 +02:00
split(doc, array, "\n")
for (item in array) {
2025-07-13 21:39:54 +02:00
alias_eval(array[item], extract($0, "function"), "function")
2025-07-06 06:41:44 +02:00
}
2025-07-06 05:31:51 +02:00
reset()
2025-07-06 03:16:41 +02:00
} else {
reset()
}
2025-07-13 22:04:54 +02:00
} else if (action == "function") {
if (match($0, re["command"])) {
append(extract($0, "command"))
} else if (match($0, re["function"])) {
2025-07-08 21:18:50 +02:00
split(doc, array, "\n")
for (item in array) {
2025-07-08 21:35:30 +02:00
if (array[item] == target) {
2025-07-13 22:04:54 +02:00
print extract($0, "function")
2025-07-08 21:18:50 +02:00
exit
}
}
reset()
} else {
reset()
}
2025-07-06 06:41:44 +02:00
} else if (action == "doc") {
# doc
2025-07-13 22:15:42 +02:00
if (match($0, RE_SHEBANG)) {
append("! " extract($0, "shebang"))
2025-07-13 22:26:56 +02:00
} else if (match($0, RE_DOC)) {
2025-07-06 10:20:52 +02:00
if (f) {
append($0)
} else {
2025-07-13 22:26:56 +02:00
append(extract($0, "doc"))
2025-07-06 10:20:52 +02:00
}
2025-07-13 23:22:45 +02:00
} else if (match($0, re["alias"])) {
matched = extract($0, "alias")
append("= " matched)
if (matched == target) {
2025-07-08 21:21:49 +02:00
match_alias = 1
2025-07-06 06:41:44 +02:00
}
2025-07-13 23:22:45 +02:00
} else if (match($0, re["command"])) {
matched = extract($0, "command")
append("/ " matched)
if (matched == target) {
2025-07-08 21:23:49 +02:00
match_command = 1
2025-07-08 00:05:07 +02:00
}
2025-07-06 06:41:44 +02:00
# set
2025-07-13 23:46:47 +02:00
} else if (match($0, re["constant"])) {
matched = extract($0, "constant")
if (matched == target) {
output(matched, "constant")
2025-07-06 06:41:44 +02:00
} else {
reset()
}
2025-07-13 23:46:47 +02:00
} else if (match($0, re["variable"])) {
matched = extract($0, "variable")
if (matched == target) {
output(matched, "variable")
2025-07-06 06:41:44 +02:00
} else {
reset()
}
# others
} else if (match($0, RE_MODULE, m)) {
2025-07-06 03:16:41 +02:00
reset()
2025-07-06 06:41:44 +02:00
if (m[1] == target) {
2025-07-08 21:25:44 +02:00
match_module = 1
2025-07-06 06:41:44 +02:00
}
2025-07-11 03:17:22 +02:00
} else if (match($0, re["function"], m)) {
2025-07-06 10:15:43 +02:00
f = m[1]
} else if (match($0, RE_CLOSE, m)) {
2025-07-08 21:21:49 +02:00
if (match_alias) {
2025-07-06 09:36:06 +02:00
print "= " target
2025-07-06 10:15:43 +02:00
output(f, "function")
2025-07-08 21:23:49 +02:00
} else if (match_command) {
2025-07-08 01:43:30 +02:00
print "/ " target
output(f, "function")
2025-07-06 10:15:43 +02:00
} else if (f == target) {
output(f, "function")
2025-07-06 06:41:44 +02:00
} else {
2025-07-06 10:15:43 +02:00
f = ""
2025-07-06 06:41:44 +02:00
reset()
}
2025-07-06 03:24:23 +02:00
} else {
2025-07-08 21:25:44 +02:00
if (match_module) {
2025-07-06 09:42:23 +02:00
output(target, "module")
2025-07-06 06:41:44 +02:00
} else {
reset()
}
2025-07-06 03:24:23 +02:00
}
2025-07-06 03:16:41 +02:00
}
}
2025-07-08 15:26:34 +02:00
END {
2025-07-11 23:00:49 +02:00
if (action == "filter") {
for (item in unique) {
print item
}
2025-07-08 15:26:34 +02:00
}
}