Skip to content

Commit

Permalink
mandb: fix an infinite loop by a leak variable
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed May 17, 2021
1 parent f599525 commit 0efcb65
Show file tree
Hide file tree
Showing 3 changed files with 333 additions and 65 deletions.
141 changes: 95 additions & 46 deletions lib/core-complete.sh
Expand Up @@ -2744,25 +2744,25 @@ function ble/complete/mandb/search-file {
ble/string#split manpath : "$manpath"
local path
for path in "${manpath[@]}"; do
ble/complete/mandb/search-file/.check "$path/man1/$man.1" && return
ble/complete/mandb/search-file/.check "$path/man1/$man.8" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.1" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.8" && return
if ble/is-function ble/bin/gzip; then
ble/complete/mandb/search-file/.check "$path/man1/$man.1.gz" && return
ble/complete/mandb/search-file/.check "$path/man1/$man.8.gz" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.1.gz" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.8.gz" && return
fi
if ble/is-function ble/bin/bzcat; then
ble/complete/mandb/search-file/.check "$path/man1/$man.1.bz" && return
ble/complete/mandb/search-file/.check "$path/man1/$man.1.bz2" && return
ble/complete/mandb/search-file/.check "$path/man1/$man.8.bz" && return
ble/complete/mandb/search-file/.check "$path/man1/$man.8.bz2" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.1.bz" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.1.bz2" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.8.bz" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.8.bz2" && return
fi
if ble/is-function ble/bin/xzcat; then
ble/complete/mandb/search-file/.check "$path/man1/$man.1.xz" && return
ble/complete/mandb/search-file/.check "$path/man1/$man.8.xz" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.1.xz" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.8.xz" && return
fi
if ble/is-function ble/bin/lzcat; then
ble/complete/mandb/search-file/.check "$path/man1/$man.1.lzma" && return
ble/complete/mandb/search-file/.check "$path/man1/$man.8.lzma" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.1.lzma" && return
ble/complete/mandb/search-file/.check "$path/man1/$command.8.lzma" && return
fi
done
return 1
Expand Down Expand Up @@ -2807,53 +2807,87 @@ function ble/complete/mandb/.generate-cache {
if (type == "man") {
print ".TH __ble_ignore__ 1 __ble_ignore__ __ble_ignore__";
print ".ll 9999"
topic_start = ".TP";
}
mode = "begin";
}
function flush_topic(_, i) {
if (g_keys_count == 0) return;
for (i = 0; i < g_keys_count; i++) {
print "";
print "__ble_key__";
if (type == "man") print ".TP";
if (topic_start != "") print topic_start;
print g_keys[i];
print "";
print "__ble_desc__";
print "";
print g_desc;
print "";
}
g_keys_count = 0;
g_desc = "";
}
#% # .Dd マクロ読み込み命令?
#% # .Nm (mdoc) 記述対象の名前
mode == "begin" && /^\.(Dd|Nm)[[:space:]]/ {
if (type == "man" && /^\.Dd[[:space:]]+\$Mdoc/) topic_start = "";
print $0;
}
function register_key(key) {
g_keys[g_keys_count++] = key;
g_desc = "";
}
mode == "key1" {
register_key($0);
mode = "desc";
next;
}
mode == "keyc" {
g_current_key = g_current_key "\n" $0;
if (/^\.Xc/) {
register_key(g_current_key);
mode = "desc";
}
next;
}
type == "man" && /^\.TP([^_[:alnum:]]|$)/ {
if (g_keys_count && g_desc != "") flush_topic();
mode = "key"; next;
mode = "key1";
next;
}
type == "mdoc" && /^\.It Fl([^_[:alnum:]]|$)/ {
#% # mdoc でも man でもあった
/^\.It Fl([^_[:alnum:]]|$)/ {
if (g_keys_count && g_desc != "") flush_topic();
mode = "key";
sub(/^\.It Fl/, ".Fl");
if ($0 ~ / Xo$/) {
g_current_key = $0;
mode = "keyc"
} else {
register_key($0);
mode = "desc";
}
next;
}
/^\.(S[Ss]|S[Hh]|P[Pp])([^_[:alnum:]]|$)/ { flush_topic(); next; }
/^\.PD([^_[:alnum:]]|$)/ { next; }
mode == "key" {
g_keys[g_keys_count++] = $0;
g_desc = "";
mode = "desc";
next;
}
mode == "desc" {
if (g_desc != "") g_desc = g_desc "\n";
g_desc = g_desc $0;
}
END { flush_topic(); }
' | ble/complete/mandb/convert-mandoc | ble/bin/awk '
' | ble/complete/mandb/convert-mandoc 2>/dev/null | ble/bin/awk '
function process_pair(name, desc) {
if (!(g_name ~ /^-/)) return;
# FS (\034) は特殊文字として使用するので除外する。
#% # FS (\034) は特殊文字として使用するので除外する。
sep = "\034";
if (g_name ~ /\034/) return;
gsub(/\034/, "\x1b[7m^\\\x1b[27m", desc);
Expand Down Expand Up @@ -2885,40 +2919,51 @@ function ble/complete/mandb/.generate-cache {
g_desc = "";
}
sub(/^[[:space:]]*__ble_key__/, "", $0) {
flush_pair();
mode = "key";
}
sub(/^[[:space:]]*__ble_desc__/, "", $0) {
mode = "desc";
}
mode == "key" {
line = $0;
function process_key(line) {
gsub(/\x1b\[[ -?]*[@-~]/, "", line); # CSI seq
gsub(/\x1b[ -\/]*[0-~]/, "", line); # ESC seq
gsub(/.\x08/, "", line); # CHAR BS
gsub(/\x0E/, "", line); # SO
gsub(/\x0F/, "", line); # SI
gsub(/[\x00-\x1F]/, "", line); # 制御文字は結局全削除
gsub(/[\x00-\x1F]/, "", line); # Give up all the other control chars
gsub(/^[[:space:]]*|[[:space:]]*$/, "", line);
#gsub(/[[:space:]]+/, " ", line);
if (line == "") next;
if (line == "") return;
if (g_name != "") g_name = g_name " ";
g_name = g_name line;
}
mode == "desc" {
line = $0;
function process_desc(line) {
gsub(/^[[:space:]]*|[[:space:]]*$/, "", line);
gsub(/[[:space:]][[:space:]]+/, " ", line);
if (line == "") {
if (g_desc != "") mode = "";
next;
if (g_desc != "") return 0;
return 1;
}
if (g_desc != "") g_desc = g_desc " ";
g_desc = g_desc line;
return 1;
}
sub(/^[[:space:]]*__ble_key__/, "", $0) {
flush_pair();
mode = "key";
if (match($0, /__ble_desc__/) > 0) {
s_key = substr($0, 1, RSTART - 1);
s_desc = substr($0, RSTART + RLENGTH);
process_key(s_key);
mode = "desc";
if (!process_desc(s_desc)) mode = "";
next;
}
}
sub(/^[[:space:]]*__ble_desc__/, "", $0) {
mode = "desc";
}
mode == "key" { process_key($0); }
mode == "desc" { if (!process_desc($0)) mode = ""; }
END { flush_pair(); }
' | ble/bin/sort -k 1
}
Expand Down Expand Up @@ -3007,7 +3052,7 @@ function ble/complete/source:argument/.generate-user-defined-completion {
}

function ble/complete/source:argument/.contains-literal-option {
local word
local word ret
for word; do
ble/syntax:bash/simple-word/is-simple "$word" &&
ble/syntax:bash/simple-word/eval "$word" noglob &&
Expand Down Expand Up @@ -3037,10 +3082,14 @@ function ble/complete/source:argument/.generate-from-mandb {
while local ret; ! ble/complete/mandb/load-cache "$cmd"; do
alias_checked=$alias_checked$cmd' '
ble/util/expand-alias "$cmd"
ble/string#split-words ret "$ret"
[[ $alias_checked != *" $ret "* ]] || return 1
ble/complete/source:argument/.contains-literal-option "${ret[@]:1}" && return 1
cmd=$ret
local words; ble/string#split-words ret "$ret"; words=("${ret[@]}")

# 変数代入は読み飛ばし
local iword=0 rex='^[_a-zA-Z][_a-zA-Z0-9]*\+?='
while [[ ${words[iword]} =~ $rex ]]; do ((iword++)); done
[[ ${words[iword]} && $alias_checked != *" ${words[iword]} "* ]] || return 1
ble/complete/source:argument/.contains-literal-option "${words[@]:iword+1}" && return 1
cmd=${words[iret]}
done

local entry
Expand Down
9 changes: 5 additions & 4 deletions memo/ChangeLog.md
Expand Up @@ -6,6 +6,10 @@
## New features

- complete/mandb: support mandb in FreeBSD `#D1432` 6c54f79
- mandb: fix BS contamination used by nroff to represent bold (reported by rlnore) `#D1429` b5c875a
- mandb: fix an encoding prpblem of utf8 manuals `#D1446` 7a4a480
- mandb: improve extraction and cache for each locale `#D1480` 3588158
- mandb: fix an infinite loop by a leak variable (reported by rlanore, riblo) `#D1550` 0000000
- decode (`ble-decode-kbd`): support various specifications of key sequences `#D1439` 0f01cab
- edit: support new options `bleopt edit_line_type={logical,graphical}` (motivated by 3ximus) `#D1442` 40ae242
- complete: support new options `bleopt complete_limit{,_auto}` (contributed by timjrd) `#D1445` b13f114 5504bbc
Expand Down Expand Up @@ -72,13 +76,11 @@

- term: fix a bug that VTE based terminals are not recognized `#D1427` 7e16d9d
- complete: fix a problem that candidates are not updated after menu-filter (reported by 3ximus) `#D1428` 98fbc1c
- complete/mandb: fix BS contamination used by nroff to represent bold (reported by rlnore) `#D1429` b5c875a
- edit: work around the wrong job information of Bash in trap handlers (reported by 3ximus) `#D1435` `#D1436` bc4735e
- edit (command-help): work around the Bash bug that tempenv vanishes with `builtin eval` `#D1438` 8379d4a
- global: suppress missing locale errors (reported by 3ximus) `#D1440` 4d3c595
- edit (sword): fix definition of `sword` (shell words) `#D1441` f923388
- edit (`kill-forward-logical-line`): fix a bug not deleting newline at the end of the line `#D1443` 09cf7f1
- complete (mandb): fix an encoding prpblem of utf8 manuals `#D1446` 7a4a480
- util (`ble/util/msleep`): fix hang in Cygwin by swithing from `/dev/udp/0.0.0.0/80` to `/dev/zero` `#D1452` d4d718a
- util (`ble/util/msleep`): work around the bash-4.3 bug of `read -t` (reported by 3ximus) `#D1468` `#D1469` 4ca9b2e
- syntax: fix broken AST with `[[` keyword `#D1454` 69658ef
Expand All @@ -87,7 +89,6 @@
- global: fix declaration of associative arrays for `ble-reload` (reported by 0xC0ncord) `#D1471` 3cae6e4
- bind: work around broken `cmd_xmap` after switching the editing mode `#D1478` 8d354c1
- edit: clear graphic rendition on newlines and external commands `#D1479` 18bb2d5
- mandb: improve extraction and cache for each locale `#D1480` 3588158
- decode (rlfunc): work around incomplete bytes in keyseq (reported by onelittlehope) `#D1483` 3559658 beb0383 37363be
- main: fix a bug that unset `IFS` is not correctly restored `#D1489` 808f6f7
- edit: fix error messages on accessing undo records in emacs mode (reported by rux616) `#D1497` 61a57c0 e9be69e
Expand All @@ -108,7 +109,7 @@
- complete: work around `fzf` completion settings loaded automatically `#D1508` 4fc51ae
- complete: work around `bash-completion` bugs (reported by oc1024) `#D1533` 9d4ad56
- main: work around MSYS2 .inputrc (reported by n1kk) `#D1534` 9e786ae
- util (`modifyOtherKeys`): work around a quirk of Kitty (reported by NoahGorny) `#D1549` 0000000
- util (`modifyOtherKeys`): work around a quirk of Kitty (reported by NoahGorny) `#D1549` f599525

## Internal changes and fixes

Expand Down

0 comments on commit 0efcb65

Please sign in to comment.