/* * Print routing tables. */ void routepr(u_long rtree, u_long mtree, u_long af2idx, u_long rtbl_id_max, u_int tableid) { struct radix_node_head *rnh, head; int i, idxmax = 0; u_int rtidxmax; printf("Routing tables\n"); if (rtree == 0 || af2idx == 0) { printf("rt_tables: symbol not in namelist\n"); return; } kread((u_long)rtree, &rt_head, sizeof(rt_head)); kread((u_long)rtbl_id_max, &rtidxmax, sizeof(rtidxmax)); kread((long)af2idx, &af2rtafidx, sizeof(af2rtafidx)); for (i = 0; i <= AF_MAX; i++) { if (af2rtafidx[i] > idxmax) idxmax = af2rtafidx[i]; } if ((rnt = calloc(rtidxmax + 1, sizeof(struct radix_node_head **))) == NULL) err(1, NULL); kread((u_long)rt_head, rnt, (rtidxmax + 1) * sizeof(struct radix_node_head **)); if (tableid > rtidxmax || rnt[tableid] == NULL) { printf("Bad table %u\n", tableid); return; } kread((u_long)rnt[tableid], rt_tables, (idxmax + 1) * sizeof(rnh)); for (i = 0; i <= AF_MAX; i++) { if (i == AF_UNSPEC) { if (Aflag && (af == AF_UNSPEC || af == 0xff)) { kread(mtree, &rnh, sizeof(rnh)); kread((u_long)rnh, &head, sizeof(head)); printf("Netmasks:\n"); p_tree(head.rnh_treetop); } continue; } if (af2rtafidx[i] == 0) /* no table for this AF */ continue; if ((rnh = rt_tables[af2rtafidx[i]]) == NULL) continue; kread((u_long)rnh, &head, sizeof(head)); if (af == AF_UNSPEC || af == i) { pr_family(i); do_rtent = 1; pr_rthdr(i, Aflag); p_tree(head.rnh_treetop); } } }
static_fn void p_arg(const struct argnod *arg, int endchar, int opts) { int flag = -1; do { if (!arg->argnxt.ap) { flag = endchar; } else if (opts & PRE) { // case alternation lists in reverse order p_arg(arg->argnxt.ap, '|', opts); flag = endchar; } else if (opts) { flag = ' '; } const char *cp = arg->argval; if (*cp == 0 && (arg->argflag & ARG_EXP) && arg->argchn.ap) { int c = (arg->argflag & ARG_RAW) ? '>' : '<'; sfputc(outfile, c); sfputc(outfile, '('); p_tree((Shnode_t *)arg->argchn.ap, 0); sfputc(outfile, ')'); } else if (*cp == 0 && opts == POST && arg->argchn.ap) { // compound assignment struct fornod *fp = (struct fornod *)arg->argchn.ap; sfprintf(outfile, "%s=(\n", fp->fornam); sfnputc(outfile, '\t', ++level); p_tree(fp->fortre, 0); if (--level) sfnputc(outfile, '\t', level); sfputc(outfile, ')'); } else if ((arg->argflag & ARG_RAW) && (cp[1] || (*cp != '[' && *cp != ']'))) { cp = sh_fmtq(cp); } sfputr(outfile, cp, flag); if (flag == '\n') begin_line = 1; arg = arg->argnxt.ap; } while ((opts & POST) && arg); }
static void p_tree(struct radix_node *rn) { again: kget(rn, rnode); if (rnode.rn_bit < 0) { if (Aflag) printf("%-8.8lx ", (u_long)rn); if (rnode.rn_flags & RNF_ROOT) { if (Aflag) printf("(root node)%s", rnode.rn_dupedkey ? " =>\n" : "\n"); } else if (do_rtent) { kget(rn, rtentry); p_rtentry(&rtentry); if (Aflag) p_rtnode(); } else { p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key), NULL, 0, 44); putchar('\n'); } if ((rn = rnode.rn_dupedkey)) goto again; } else { if (Aflag && do_rtent) { printf("%-8.8lx ", (u_long)rn); p_rtnode(); } rn = rnode.rn_right; p_tree(rnode.rn_left); p_tree(rn); } }
static void p_tree(struct radix_node *rn) { again: kread((u_long)rn, &rnode, sizeof(rnode)); if (rnode.rn_b < 0) { if (Aflag) printf("%-16p ", hideroot ? 0 : rn); if (rnode.rn_flags & RNF_ROOT) { if (Aflag) printf("(root node)%s", rnode.rn_dupedkey ? " =>\n" : "\n"); } else if (do_rtent) { kread((u_long)rn, &rtentry, sizeof(rtentry)); p_krtentry(&rtentry); if (Aflag) p_rtnode(); } else { p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key), 0, 0, 44); putchar('\n'); } if ((rn = rnode.rn_dupedkey)) goto again; } else { if (Aflag && do_rtent) { printf("%-16p ", hideroot ? 0 : rn); p_rtnode(); } rn = rnode.rn_r; p_tree(rnode.rn_l); p_tree(rn); } }
void p_tree(struct node *t, int h) { int i; if(t == NULL) return; for(i = 0; i < h; i++) fprintf(stdout," "); fprintf(stdout,"%d\n", t->item); p_tree(t->left, h+1); p_tree(t->right, h+1); }
void print_tree(struct node *t) { if( t == NULL) { fprintf(stdout,"Null Tree\n"); return; } p_tree(t, 0); }
/* * Print routing tables. */ void show_route_statistic() { struct radix_node_head *rnh, *head; int i; printf("Routing tables\n"); if (Aflag == 0 && NewTree) ntreestuff(); else { //kget(rtree, rt_tables); for (i = 0; i <= AF_MAX; i++) { if ((rnh = rt_tables[i]) == 0) continue; //kget(rnh, head); head = rnh; if (i == AF_UNSPEC) { if (Aflag && af == 0) { printf("Netmasks:\n"); p_tree(head->rnh_treetop); } } else if (af == AF_UNSPEC || af == i) { size_cols(i, head->rnh_treetop); pr_family(i); do_rtent = 1; pr_rthdr(i); p_tree(head->rnh_treetop); } } } }
static_fn void p_switch(const struct regnod *reg) { if (level > 1) sfnputc(outfile, '\t', level - 1); p_arg(reg->regptr, ')', PRE); begin_line = 0; sfputc(outfile, '\t'); if (reg->regcom) p_tree(reg->regcom, 0); level++; if (reg->regflag) { p_keyword(";&", END); } else { p_keyword(";;", END); } if (reg->regnxt) p_switch(reg->regnxt); }
/* * Print routing tables. */ void routepr(u_long rtree) { struct radix_node_head *rnh, head; int i; printf("Routing tables\n"); if (Aflag == 0 && NewTree) ntreestuff(); else { if (rtree == 0) { printf("rt_tables: symbol not in namelist\n"); return; } kget(rtree, rt_tables); for (i = 0; i <= AF_MAX; i++) { if ((rnh = rt_tables[i]) == 0) continue; kget(rnh, head); if (i == AF_UNSPEC) { if (Aflag && af == 0) { printf("Netmasks:\n"); p_tree(head.rnh_treetop); } } else if (af == AF_UNSPEC || af == i) { size_cols(i, head.rnh_treetop); pr_family(i); do_rtent = 1; pr_rthdr(i); p_tree(head.rnh_treetop); } } } }
static void p_arg(register const struct argnod *arg,register int endchar,int opts) { register const char *cp; register int flag; do { if(!arg->argnxt.ap) flag = endchar; else if(opts&PRE) { /* case alternation lists in reverse order */ p_arg(arg->argnxt.ap,'|',opts); flag = endchar; } else if(opts) flag = ' '; cp = arg->argval; if(*cp==0 && opts==POST && arg->argchn.ap) { /* compound assignment */ struct fornod *fp=(struct fornod*)arg->argchn.ap; sfprintf(outfile,"%s=(\n",fp->fornam); sfnputc(outfile,'\t',++level); p_tree(fp->fortre,0); if(--level) sfnputc(outfile,'\t',level); sfputc(outfile,')'); } else if((arg->argflag&ARG_RAW) && (cp[1] || (*cp!='[' && *cp!=']'))) cp = sh_fmtq(cp); sfputr(outfile,cp,flag); if(flag=='\n') begin_line = 1; arg = arg->argnxt.ap; } while((opts&POST) && arg); return; }
/* * Print routing tables. */ void routepr(u_long rtree) { struct radix_node_head **rnhp, *rnh, head; size_t intsize; int fam, fibnum, numfibs; intsize = sizeof(int); if (sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1) fibnum = 0; if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1) numfibs = 1; rt_tables = calloc(numfibs * (AF_MAX+1), sizeof(struct radix_node_head *)); if (rt_tables == NULL) err(EX_OSERR, "memory allocation failed"); /* * Since kernel & userland use different timebase * (time_uptime vs time_second) and we are reading kernel memory * directly we should do rt_rmx.rmx_expire --> expire_time conversion. */ if (clock_gettime(CLOCK_UPTIME, &uptime) < 0) err(EX_OSERR, "clock_gettime() failed"); printf("Routing tables\n"); if (Aflag == 0 && NewTree) ntreestuff(); else { if (rtree == 0) { printf("rt_tables: symbol not in namelist\n"); return; } if (kread((u_long)(rtree), (char *)(rt_tables), (numfibs * (AF_MAX+1) * sizeof(struct radix_node_head *))) != 0) return; for (fam = 0; fam <= AF_MAX; fam++) { int tmpfib; switch (fam) { case AF_INET6: case AF_INET: tmpfib = fibnum; break; default: tmpfib = 0; } rnhp = (struct radix_node_head **)*rt_tables; /* Calculate the in-kernel address. */ rnhp += tmpfib * (AF_MAX+1) + fam; /* Read the in kernel rhn pointer. */ if (kget(rnhp, rnh) != 0) continue; if (rnh == NULL) continue; /* Read the rnh data. */ if (kget(rnh, head) != 0) continue; if (fam == AF_UNSPEC) { if (Aflag && af == 0) { printf("Netmasks:\n"); p_tree(head.rnh_treetop); } } else if (af == AF_UNSPEC || af == fam) { size_cols(fam, head.rnh_treetop); pr_family(fam); do_rtent = 1; pr_rthdr(fam); p_tree(head.rnh_treetop); } } } }
// Print script corresponding to shell tree <t>. static_fn void p_tree(const Shnode_t *t, int tflags) { char *cp = NULL; int save = end_line; int needbrace = (tflags & NEED_BRACE); int bracket = 0; tflags &= ~NEED_BRACE; if (tflags & NO_NEWLINE) { end_line = ' '; } else { end_line = '\n'; } int cmd_type = t->tre.tretyp & COMMSK; switch (cmd_type) { case TTIME: { if (t->tre.tretyp & COMSCAN) { p_keyword("!", BEGIN); } else { p_keyword("time", BEGIN); } if (t->par.partre) p_tree(t->par.partre, tflags); level--; break; } case TCOM: { if (begin_line && level > 0) sfnputc(outfile, '\t', level); begin_line = 0; p_comarg((struct comnod *)t); break; } case TSETIO: { if (t->tre.tretyp & FPCL) { tflags |= NEED_BRACE; } else { tflags = NO_NEWLINE | NEED_BRACE; } p_tree(t->fork.forktre, tflags); p_redirect(t->fork.forkio); break; } case TFORK: { if (needbrace) tflags |= NEED_BRACE; if (t->tre.tretyp & (FAMP | FCOOP)) { tflags = NEED_BRACE | NO_NEWLINE; end_line = ' '; } else if (t->fork.forkio) { tflags = NO_NEWLINE; } p_tree(t->fork.forktre, tflags); if (t->fork.forkio) p_redirect(t->fork.forkio); if (t->tre.tretyp & FCOOP) { sfputr(outfile, "|&", '\n'); begin_line = 1; } else if (t->tre.tretyp & FAMP) { sfputr(outfile, "&", '\n'); begin_line = 1; } break; } case TIF: { p_keyword("if", BEGIN); p_tree(t->if_.iftre, 0); p_keyword("then", MIDDLE); p_tree(t->if_.thtre, 0); if (t->if_.eltre) { p_keyword("else", MIDDLE); p_tree(t->if_.eltre, 0); } p_keyword("fi", END); break; } case TWH: { if (t->wh.whinc) { cp = "for"; } else if (t->tre.tretyp & COMSCAN) { cp = "until"; } else { cp = "while"; } p_keyword(cp, BEGIN); if (t->wh.whinc) { struct argnod *arg = (t->wh.whtre)->ar.arexpr; sfprintf(outfile, "(( %s; ", forinit); forinit = ""; sfputr(outfile, arg->argval, ';'); arg = (t->wh.whinc)->arexpr; sfprintf(outfile, " %s))\n", arg->argval); } else { p_tree(t->wh.whtre, 0); } t = t->wh.dotre; p_keyword("do", MIDDLE); p_tree(t, 0); p_keyword("done", END); break; } case TLST: { Shnode_t *tr = t->lst.lstrit; if (tr->tre.tretyp == TWH && tr->wh.whinc && t->lst.lstlef->tre.tretyp == TARITH) { // Arithmetic for statement. struct argnod *init = (t->lst.lstlef)->ar.arexpr; forinit = init->argval; p_tree(t->lst.lstrit, tflags); break; } if (needbrace) p_keyword("{", BEGIN); p_tree(t->lst.lstlef, 0); if (needbrace) tflags = 0; p_tree(t->lst.lstrit, tflags); if (needbrace) p_keyword("}", END); break; } case TAND: case TORF: case TFIL: { if (cmd_type == TAND) { cp = "&&"; } else if (cmd_type == TORF) { cp = "||"; } else { cp = "|"; } if (t->tre.tretyp & TTEST) { tflags |= NO_NEWLINE; if (!(tflags & NO_BRACKET)) { p_keyword("[[", BEGIN); tflags |= NO_BRACKET; bracket = 1; } } p_tree(t->lst.lstlef, NEED_BRACE | NO_NEWLINE | (tflags & NO_BRACKET)); if (tflags & FALTPIPE) { Shnode_t *tt = t->lst.lstrit; if (tt->tre.tretyp != TFIL || !(tt->lst.lstlef->tre.tretyp & FALTPIPE)) { sfputc(outfile, '\n'); return; } } sfputr(outfile, cp, here_doc ? '\n' : ' '); if (here_doc) { here_body(here_doc); here_doc = 0; } level++; p_tree(t->lst.lstrit, tflags | NEED_BRACE); if (bracket) p_keyword("]]", END); level--; break; } case TPAR: { p_keyword("(", BEGIN); p_tree(t->par.partre, 0); p_keyword(")", END); break; } case TARITH: { struct argnod *ap = t->ar.arexpr; if (begin_line && level) sfnputc(outfile, '\t', level); sfprintf(outfile, "(( %s ))%c", ap->argval, end_line); if (!(tflags & NO_NEWLINE)) begin_line = 1; break; } case TFOR: { cp = ((t->tre.tretyp & COMSCAN) ? "select" : "for"); p_keyword(cp, BEGIN); sfputr(outfile, t->for_.fornam, ' '); if (t->for_.forlst) { sfputr(outfile, "in", ' '); tflags = end_line; end_line = '\n'; p_comarg(t->for_.forlst); end_line = tflags; } else { sfputc(outfile, '\n'); } begin_line = 1; t = t->for_.fortre; p_keyword("do", MIDDLE); p_tree(t, 0); p_keyword("done", END); break; } case TSW: { p_keyword("case", BEGIN); p_arg(t->sw.swarg, ' ', 0); if (t->sw.swlst) { begin_line = 1; sfputr(outfile, "in", '\n'); tflags = end_line; end_line = '\n'; p_switch(t->sw.swlst); end_line = tflags; } p_keyword("esac", END); break; } case TFUN: { if (t->tre.tretyp & FPOSIX) { sfprintf(outfile, "%s", t->funct.functnam); p_keyword("()\n", BEGIN); } else { p_keyword("function", BEGIN); tflags = (t->funct.functargs ? ' ' : '\n'); sfputr(outfile, t->funct.functnam, tflags); if (t->funct.functargs) { tflags = end_line; end_line = '\n'; p_comarg(t->funct.functargs); end_line = tflags; } } begin_line = 1; p_keyword("{\n", MIDDLE); begin_line = 1; p_tree(t->funct.functtre, 0); p_keyword("}", END); break; } case TTST: { // new test compound command if (!(tflags & NO_BRACKET)) p_keyword("[[", BEGIN); if ((t->tre.tretyp & TPAREN) == TPAREN) { p_keyword("(", BEGIN); p_tree(t->lst.lstlef, NO_BRACKET | NO_NEWLINE); p_keyword(")", END); } else { int flags = (t->tre.tretyp) >> TSHIFT; if (t->tre.tretyp & TNEGATE) sfputr(outfile, "!", ' '); if (t->tre.tretyp & TUNARY) { un_op[1] = flags; sfputr(outfile, un_op, ' '); } else { cp = ((char *)(shtab_testops + (flags & 037) - 1)->sh_name); } p_arg(&(t->lst.lstlef->arg), ' ', 0); if (t->tre.tretyp & TBINARY) { assert(cp); sfputr(outfile, cp, ' '); p_arg(&(t->lst.lstrit->arg), ' ', 0); } } if (!(tflags & NO_BRACKET)) p_keyword("]]", END); } default: { break; } } while (begin_line && here_doc) { here_body(here_doc); here_doc = 0; } end_line = save; }
void sh_deparse(Sfio_t *out, const Shnode_t *t, int tflags) { outfile = out; p_tree(t, tflags); }