File: D1405.extract-man.sh

package info (click to toggle)
ble.sh 0.4.0~git20250321.d4c812b-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,516 kB
  • sloc: sh: 71,367; awk: 1,316; cpp: 750; ansic: 186; javascript: 43; makefile: 35
file content (151 lines) | stat: -rwxr-xr-x 3,837 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash

function ble/complete/mandb/search-file/.check {
  local path=$1
  if [[ $path && -s $path ]]; then
    ret=$path
    return 0
  else
    return 1
  fi
}
function ble/complete/mandb/search-file {
  local command=$1

  # Try "man -w" first
  ble/complete/mandb/search-file/.check "$(man -w "$command" 2>/dev/null)" && return 0

  local manpath=${MANPATH:-/usr/share/man:/usr/local/share/man:/usr/local/man}
  ble/string#split manpath : "$manpath"
  local path
  for path in "${manpath[@]}"; do
    ble/complete/mandb/search-file/.check "$path/man1/$man.1.gz" && return 0
    ble/complete/mandb/search-file/.check "$path/man1/$man.1" && return 0
    ble/complete/mandb/search-file/.check "$path/man1/$man.8.gz" && return 0
    ble/complete/mandb/search-file/.check "$path/man1/$man.8" && return 0
  done
  return 1
}

ble/complete/mandb/search-file "${1:-grep}" || return 1
path=$ret

if [[ $ret == *.gz ]]; then
  gzip -cd "$path" #/usr/share/man/man1/grep.1.gz
else
  cat "$path"
fi |
  awk '
    BEGIN {
      g_key = "";
      g_desc = "";
      print ".TH __ble_ignore__ 1 __ble_ignore__ __ble_ignore__";
      print ".ll 9999"
    }
    function flush_topic() {
      if (g_key == "") return;
      print "__ble_key__";
      print ".TP";
      print g_key;
      print "";
      print "__ble_desc__";
      print "";
      print g_desc;
      print "";

      g_key = "";
      g_desc = "";
    }

    /^\.TP\y/ { flush_topic(); mode = "key"; next; }
    /^\.(SS|SH)\y/ { flush_topic(); next; }

    mode == "key" {
      g_key = $0;
      g_desc = "";
      mode = "desc";
      next;
    }
    mode == "desc" {
      if (g_desc != "") g_desc = g_desc "\n";
      g_desc = g_desc $0;
    }

    END { flush_topic(); }
  ' | groff -Tutf8 -man | awk '
    function process_pair(name, desc) {
      if (!(g_name ~ /^-/)) return;

      # FS (\034) は ble.sh で内部使用の為除外する。
      sep = "\x1b[1;91m:\x1b[m";
      #sep = "\034";
      if (g_name ~ /\034/) return;
      gsub(/\034/, "\x1b[7m^\\\x1b[27m", desc);

      n = split(name, names, /,[[:blank:]]*/);
      sub(/(\.  |; ).*/, ".", desc);
      for (i = 1; i <= n; i++) {
        name = names[i];
        insert_suffix = " ";
        menu_suffix = "";
        if (match(name, /[[ =]/)) {
          m = substr(name, RSTART, 1);
          if (m == "=") {
            insert_suffix = "=";
          } else if (m == "[") {
            insert_suffix = "";
          }
          menu_suffix = substr(name, RSTART);
          name = substr(name, 1, RSTART - 1);
        }
        printf("%s" sep "%s" sep "%s" sep "%s\n", name, menu_suffix, insert_suffix, desc);
      }
    }

    function flush_pair() {
      if (g_name == "") return;
      if (g_name ~ /^-/) {
        process_pair(g_name, g_desc);
        #print "\x1b[1;94m" g_name "\x1b[0m";
        #print g_desc;
        #print "";
      }
      g_name = "";
      g_desc = "";
    }

    sub(/^[[:blank:]]*__ble_key__/, "", $0) {
      flush_pair();
      mode = "key";
    }
    sub(/^[[:blank:]]*__ble_desc__/, "", $0) {
      mode = "desc";
    }

    mode == "key" {
      line = $0;
      gsub(/\x1b\[[ -?]*[@-~]/, "", line); # CSI seq
      gsub(/\x1b[ -/]*[0-~]/, "", line); # ESC seq
      gsub(/\x0E/, "", line);
      gsub(/\x0F/, "", line);
      gsub(/^[[:blank:]]*|[[:blank:]]*$/, "", line);
      #gsub(/[[:blank:]]+/, " ", line);
      if (line == "") next;
      if (g_name != "") g_name = g_name " ";
      g_name = g_name line;
    }

    mode == "desc" {
      line = $0;
      gsub(/^[[:blank:]]*|[[:blank:]]*$/, "", line);
      #gsub(/[[:blank:]]+/, " ", line);
      if (line == "") {
        if (g_desc != "") mode = "";
        next;
      }
      if (g_desc != "") g_desc = g_desc " ";
      g_desc = g_desc line;
    }

    END { flush_pair(); }
  ' | sort -k 1