Skip to content

Commit 555ba98

Browse files
committed
make yacli_cmd_dump_path interative
1 parent d6d73b4 commit 555ba98

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

yacli.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// $Id: yacli.c,v 4.13 2026/04/30 06:59:53 bbonev Exp $
1+
// $Id: yacli.c,v 4.14 2026/04/30 22:21:00 bbonev Exp $
22
//
33
// Copyright © 2015-2020 Boian Bonev (bbonev@ipacct.com) {{{
44
//
@@ -216,7 +216,7 @@ inline void yacli_set_showtermsize(yacli *cli,int v) { // {{{
216216
cli->showtsize=!!v;
217217
} // }}}
218218

219-
static char myver[]="\0Yet another command line interface library (https://github.com/bbonev/yacli) $Revision: 4.13 $\n\n"; // {{{
219+
static char myver[]="\0Yet another command line interface library (https://github.com/bbonev/yacli) $Revision: 4.14 $\n\n"; // {{{
220220
// }}}
221221

222222
inline const char *yacli_ver(void) { // {{{
@@ -1853,12 +1853,29 @@ static void yacli_dyn_upd(yacli *cli,cmnode *n,const char *prefix) { // {{{
18531853
cli->listcb(cli,pnode,atoi(n->cmd+1));
18541854
} // }}}
18551855

1856+
static inline void yacli_cmd_dump_path(yacli *cli,cmnode *n) { // {{{ print the root→n path (space-separated), no trailing space; iterative to avoid recursing up the parent chain
1857+
int depth=0,i;
1858+
cmnode *p;
1859+
1860+
for (p=n;p;p=p->parent) // count depth
1861+
depth++;
1862+
while (depth>0) {
1863+
depth--;
1864+
p=n;
1865+
for (i=0;i<depth;i++) // re-walk to the i-th ancestor; O(depth^2) but depth is tiny in practice and avoids any recursion or auxiliary storage
1866+
p=p->parent;
1867+
yacli_print(cli,"%s",p->cmd[0]=='^'?p->help:p->cmd);
1868+
if (depth>0)
1869+
yacli_print(cli," ");
1870+
}
1871+
} // }}}
1872+
18561873
static inline int yacli_cmd_dump_node(yacli *cli,cmnode *n) { // {{{
18571874
if (!cli)
18581875
return 1; // stop any processing on NULL cli
18591876

18601877
if (!n)
1861-
return 0; // this is recursion end case and should not interrupt processing
1878+
return 0; // recursion end case (still hit by yacli_cmd_dump_r); should not interrupt processing
18621879
if (n->cmd[0]=='@') {
18631880
cmnode *p;
18641881
int first=1;
@@ -1872,17 +1889,15 @@ static inline int yacli_cmd_dump_node(yacli *cli,cmnode *n) { // {{{
18721889
yacli_print(cli,"\n");
18731890
first=0;
18741891
if (n->parent) {
1875-
if (yacli_cmd_dump_node(cli,n->parent))
1876-
return 1;
1892+
yacli_cmd_dump_path(cli,n->parent);
18771893
yacli_print(cli," ");
18781894
}
18791895
yacli_print(cli,"%s",p->cmd);
18801896
p=p->next;
18811897
}
18821898
} else {
18831899
if (n->parent) {
1884-
if (yacli_cmd_dump_node(cli,n->parent))
1885-
return 1;
1900+
yacli_cmd_dump_path(cli,n->parent);
18861901
yacli_print(cli," ");
18871902
}
18881903
yacli_print(cli,"%s",n->cmd[0]=='^'?n->help:n->cmd);

0 commit comments

Comments
 (0)