dot

packages and services management
Log | Files | Refs | README

commit d75d8306b7bf9152beada9954f1a0addb1bf7078
parent f725f36fbc294a4fb587cb6fdd13ec9600988ab0
Author: Josuah Demangeon <mail@josuah.net>
Date:   Wed, 14 Mar 2018 23:33:31 +0100

I did it

Diffstat:
Mbin/.pstree.swp | 0
Mbin/pstree | 69+++++++++++++++++++++++++++++++++++++++++++++------------------------
2 files changed, 45 insertions(+), 24 deletions(-)

diff --git a/bin/.pstree.swp b/bin/.pstree.swp Binary files differ. diff --git a/bin/pstree b/bin/pstree @@ -2,20 +2,6 @@ # pstree implementation in awk -# find(1) walk the entire tree and then ({} + means call at the end) call -# ls -ld with all the result, which are then sorted. The result will be -# a list with all ls -l information and the the path name from the current -# directory (./). This pattern is used to delimit the find-style output, -# which is transformed into tree(1)-style output: -# -# drwxr-xr-x 2 josuah josuah 512 Feb 16 13:19 |- .ssh -# -rw-r--r-- 1 josuah josuah 870 Feb 9 02:24 | `- config -# drwxr-xr-x 2 josuah josuah 1536 Feb 18 21:24 |- bin -# -rwxr-xr-x 1 josuah josuah 1351 Feb 18 22:30 | |- lt -# -rwxr-xr-x 1 josuah josuah 565 Feb 17 19:53 | |- mfilter -# -rwxr-xr-x 1 josuah josuah 5430 Feb 17 19:51 | `- xdg-open -# -rwxr-xr-x 1 josuah josuah 468 Feb 17 19:55 ... - BEGIN { LINE = "| "; NODE = "|- "; @@ -23,25 +9,37 @@ BEGIN { VOID = " "; list(entries); - fill(entries, 1, 0); + NUM = 1; fill(entries, 1, 0); + tree(entries, NUM); + + for (i = 0; i < NUM; i++) { + printf("%6s ", entries[i"pid"]); + for (j = 0; entries[i":"j] != ""; j++) + printf("%s", entries[i":"j]); + printf("%s\n", entries[i"argv"]); + } } # Build a relational database in entries from the output of ps. function list(entries) { - cmd = "ps -axo pid,ppid,comm"; - - cmd | getline; + cmd = "ps -axo pid,ppid,comm 2>/dev/null"; + if (!(cmd | getline)) { + cmd = "ps -o pid,ppid,comm" + cmd | getline; + } for (num = 0; cmd | getline; num++) { pid = $1; ppid = $2 sub(" *[0-9]* *[0-9]* ", "", $0); entries[num] = pid; - entries[pid":args"] = $0; - entries[ppid":cpid"] = entries[ppid":cpid"] "," pid; + entries[pid"args"] = $0; + entries[ppid"cpid"] = entries[ppid"cpid"] "," pid; } close(cmd); + + return num - 1; } # Using the informations from the child pid in entries, build the absolute @@ -49,13 +47,36 @@ function list(entries) function fill(entries, pid, lvl) { - for (i = 0; i <= lvl; i++) - entries[pid":"i] = LINE; - while (sub("[^,]*,", "", entries[pid":cpid"])) { - cpid = entries[pid":cpid"]; + for (j = 0; j < lvl; j++) + entries[NUM":"j] = LINE; + entries[NUM":"lvl] = NODE; + entries[NUM"pid"] = pid; + entries[NUM"argv"] = entries[pid"args"]; + NUM++; + while (sub("[^,]*,", "", entries[pid"cpid"])) { + cpid = entries[pid"cpid"]; sub(",.*", "", cpid); fill(entries, cpid, lvl + 1); } } +# Transform entries into a tree with duplicated components replaced by +# a line for drawing a tree. +function tree(entries, num) +{ + for (j = 0; !stop; j++) { + stop = tail = 1; + for (i = num; i > 0; i--) { + if (entries[i":"j] == LINE && tail) { + entries[i":"j] = VOID; + stop = 0; + } else if (entries[i":"j] == NODE && tail) { + entries[i":"j] = TAIL; + tail = stop = 0; + } else if (!entries[i":"j]) { + tail = 1; + } + } + } +}