int Ufmt(Fmt *f) { int i; Dev *d; Usbdev *ud; char buf[1024]; char *s, *e; s = buf; e = buf+sizeof(buf); d = va_arg(f->args, Dev*); if(d == nil) return fmtprint(f, "<nildev>\n"); s = seprint(s, e, "%s", d->dir); ud = d->usb; if(ud == nil) return fmtprint(f, "%s %ld refs\n", buf, d->Ref.ref); s = seprint(s, e, " csp %s.%uld.%uld", classname(Class(ud->csp)), Subclass(ud->csp), Proto(ud->csp)); s = seprint(s, e, " vid %#ux did %#ux", ud->vid, ud->did); s = seprint(s, e, " refs %ld\n", d->Ref.ref); s = seprint(s, e, "\t%s %s %s\n", ud->vendor, ud->product, ud->serial); for(i = 0; i < Nconf; i++){ if(ud->conf[i] == nil) break; else s = seprintconf(s, e, ud, i); } return fmtprint(f, "%s", buf); }
static void coutiprint(ASTNode *n, int ind, int env) { Symbol *s; Nodes *r; if(n == nil){ fmtprint(fout, "/* nil */ ;\n"); return; } switch(n->t){ case ASTDECL: break; case ASTMODULE: s = getfreesym(&cglob, smprint("run%s", n->sym->name)); fmtprint(fout, "int\n%s(void)\n{\n\tint rc;\n\n\trc = 0;\n", s->name); cloc = emalloc(sizeof(SymTab)); getsym(cloc, 0, "rc"); for(r = n->nl; r != nil; r = r->next) coutiprint(r->n, ind + 1, env); fmtprint(fout, "\treturn rc;\n}\n"); break; case ASTASS: coutass(n->n1, n->n2, ind, env); break; default: error(n, "coutiprint: unknown %A", n->t); } }
static void _synccmd(char*) { int i, fd; char *s; Fmt f; if(!loggedin){ sendok(""); return; } fmtstrinit(&f); fmtprint(&f, "delete mbox"); for(i=0; i<nmsg; i++) if(msg[i].deleted) fmtprint(&f, " %d", msg[i].upasnum); s = fmtstrflush(&f); if(strcmp(s, "delete mbox") != 0){ /* must have something to delete */ if((fd = open("../ctl", OWRITE)) < 0){ senderr("open ctl to delete messages: %r"); return; } if(write(fd, s, strlen(s)) < 0){ senderr("error deleting messages: %r"); return; } } sendok(""); }
static int constfmt(Fmt *f) { Const *c; int rc; int s; c = va_arg(f->args, Const *); rc = 0; if(c == nil || c->n == nil || c->x == nil) return fmtstrcpy(f, "<nil>"); s = c->n->sign; if(s < 0){ c->n->sign = 1; rc += fmtrune(f, '-'); } if(c->sz != 0) rc += fmtprint(f, "%d", c->sz); if(!is0(c->x)){ rc += binput(f, c); }else if(c->base == 0 || c->base == 10) rc += fmtprint(f, c->sz != 0 ? "'d%.10B" : "%.10B", c->n); else if(c->base == 16) rc += fmtprint(f, "'h%.16B", c->n); else rc += fmtprint(f, "'b%.2B", c->n); c->n->sign = s; return rc; }
int exprfmt(Fmt *fmt) { DwarfExpr *e; if((e = va_arg(fmt->args, DwarfExpr*)) == nil) return fmtstrcpy(fmt, "<nil>"); switch(e->type){ case RuleUndef: return fmtstrcpy(fmt, "undef"); case RuleSame: return fmtstrcpy(fmt, "same"); case RuleCfaOffset: return fmtprint(fmt, "%ld(cfa)", e->offset); case RuleRegister: return fmtprint(fmt, "r%ld", e->reg); case RuleRegOff: return fmtprint(fmt, "%ld(r%ld)", e->offset, e->reg); case RuleLocation: return fmtprint(fmt, "l.%.*H", e->loc.len, e->loc.data); default: return fmtprint(fmt, "?%d", e->type); } }
// Fmt "%J": Node details. static int Jconv(Fmt *fp) { Node *n; char *s; int c; n = va_arg(fp->args, Node*); c = fp->flags&FmtShort; if(!c && n->ullman != 0) fmtprint(fp, " u(%d)", n->ullman); if(!c && n->addable != 0) fmtprint(fp, " a(%d)", n->addable); if(!c && n->vargen != 0) fmtprint(fp, " g(%d)", n->vargen); if(n->lineno != 0) fmtprint(fp, " l(%d)", n->lineno); if(!c && n->xoffset != BADWIDTH) fmtprint(fp, " x(%lld%+lld)", n->xoffset, n->stkdelta); if(n->class != 0) { s = ""; if(n->class & PHEAP) s = ",heap"; if((n->class & ~PHEAP) < nelem(classnames)) fmtprint(fp, " class(%s%s)", classnames[n->class&~PHEAP], s); else fmtprint(fp, " class(%d?%s)", n->class&~PHEAP, s); }
void nfsMount3RExportPrint(Fmt *fmt, NfsMount3RExport *x) { USED(x); fmtprint(fmt, "%s\n", "NfsMount3RExport"); fmtprint(fmt, "\n"); }
int Qconv(Fmt *fp) { Bits bits; int i, first; first = 1; bits = va_arg(fp->args, Bits); while(bany(&bits)) { i = bnum(bits); if(first) first = 0; else fmtprint(fp, " "); if(var[i].node == N || var[i].node->sym == S) fmtprint(fp, "$%d", i); else { fmtprint(fp, "%s", var[i].node->sym->name); if(var[i].offset != 0) fmtprint(fp, "%+lld", (vlong)var[i].offset); } bits.b[i/32] &= ~(1L << (i%32)); } return 0; }
void _threaddebug(uint32_t flag, char *fmt, ...) { char buf[128]; va_list arg; Fmt f; Proc *p; if((_threaddebuglevel&flag) == 0) return; fmtfdinit(&f, 2, buf, sizeof buf); p = _threadgetproc(); if(p==nil) fmtprint(&f, "noproc "); else if(p->thread) fmtprint(&f, "%d.%d ", p->pid, p->thread->id); else fmtprint(&f, "%d._ ", p->pid); va_start(arg, fmt); fmtvprint(&f, fmt, arg); va_end(arg); fmtprint(&f, "\n"); fmtfdflush(&f); }
int Iconv(Fmt *fp) { int i, n; uint32 *p; char *s; Fmt fmt; n = fp->prec; fp->prec = 0; if(!(fp->flags&FmtPrec) || n < 0) return fmtstrcpy(fp, "%I"); fp->flags &= ~FmtPrec; p = va_arg(fp->args, uint32*); // format into temporary buffer and // call fmtstrcpy to handle padding. fmtstrinit(&fmt); for(i=0; i<n/4; i++) { if(i > 0) fmtprint(&fmt, " "); fmtprint(&fmt, "%.8ux", *p++); } s = fmtstrflush(&fmt); fmtstrcpy(fp, s); free(s); return 0; }
int userinfo(Fmt *f) { int got, i; Namelist *nl; Userinfo ui; nl = nil; if((got = RAPuserenum2(Sess, &Ipc, &nl)) == -1) if((got = RAPuserenum(Sess, &Ipc, &nl)) == -1){ fmtprint(f, "RAPuserenum: %r\n"); return 0; } for(i = 0; i < got; i++){ fmtprint(f, "%-24q ", nl[i].name); if(RAPuserinfo(Sess, &Ipc, nl[i].name, &ui) != -1){ fmtprint(f, "%-48q %q", ui.fullname, ui.comment); free(ui.user); free(ui.comment); free(ui.fullname); free(ui.user_comment); } free(nl[i].name); fmtprint(f, "\n"); } free(nl); return 0; }
int groupinfo(Fmt *f) { int got1, got2, i, j; Namelist *grps, *usrs; grps = nil; if((got1 = RAPgroupenum(Sess, &Ipc, &grps)) == -1){ fmtprint(f, "RAPgroupenum: %r\n"); return 0; } for(i = 0; i < got1; i++){ fmtprint(f, "%q ", grps[i].name); usrs = nil; if((got2 = RAPgroupusers(Sess, &Ipc, grps[i].name, &usrs)) != -1){ for(j = 0; j < got2; j++){ fmtprint(f, "%q ", usrs[j].name); free(usrs[j].name); } free(usrs); } free(grps[i].name); fmtprint(f, "\n"); } free(grps); return 0; }
static void coutdecl(ASTNode *n, char *hier) { Nodes *r; if(n == nil) return; switch(n->t){ case ASTDECL: n->sym->csym[0] = hiersymb(hier, n->sym, nil); n->sym->csym[1] = hiersymb(hier, n->sym, "_nxt"); coutprintdecl(n->sym->csym[0], n->sym->type); fmtprint(fout, ";\n"); coutprintdecl(n->sym->csym[1], n->sym->type); fmtprint(fout, ";\n"); break; case ASTMODULE: hier = hiersymb(hier, n->sym, nil)->name; for(r = n->ports; r != nil; r = r->next) coutdecl(r->n, hier); for(r = n->nl; r != nil; r = r->next) coutdecl(r->n, hier); break; case ASTASS: return; default: error(n, "coutdecl: unknown %A", n->t); } }
void nfsMount3TMntPrint(Fmt *fmt, NfsMount3TMnt *x) { fmtprint(fmt, "%s\n", "NfsMount3TMnt"); fmtprint(fmt, "\t%s=", "path"); fmtprint(fmt, "\"%s\"", x->path); fmtprint(fmt, "\n"); }
void wwwauthenticate(HttpState *hs, char *line) { char cred[64], *user, *pass, *realm, *s, *spec, *name; Fmt fmt; UserPasswd *up; spec = nil; up = nil; cred[0] = 0; hs->autherror[0] = 0; if(cistrncmp(line, "basic ", 6) != 0){ werrstr("unknown auth: %s", line); goto error; } line += 6; if(cistrncmp(line, "realm=", 6) != 0){ werrstr("missing realm: %s", line); goto error; } line += 6; user = hs->c->url->user; pass = hs->c->url->passwd; if(user==nil || pass==nil){ realm = unquote(line, &line); fmtstrinit(&fmt); name = servername(hs->netaddr); fmtprint(&fmt, "proto=pass service=http server=%q realm=%q", name, realm); free(name); if(hs->c->url->user) fmtprint(&fmt, " user=%q", hs->c->url->user); spec = fmtstrflush(&fmt); if(spec == nil) goto error; if((up = auth_getuserpasswd(nil, "%s", spec)) == nil) goto error; user = up->user; pass = up->passwd; } if((s = smprint("%s:%s", user, pass)) == nil) goto error; free(up); enc64(cred, sizeof(cred), (uint8_t*)s, strlen(s)); memset(s, 0, strlen(s)); free(s); hs->credentials = smprint("Basic %s", cred); if(hs->credentials == nil) goto error; return; error: free(up); free(spec); snprint(hs->autherror, sizeof hs->autherror, "%r"); fprint(2, "%s: Authentication failed: %r\n", argv0); }
int winfmt(Fmt *f) { Window *w; w = va_arg(f->args, Window*); if (w < (void *)4096) return fmtprint(f, "BOGUS w!: %p", w); return fmtprint(f, "%p: %s", w, w->label); }
static void portRDumpPrint(Fmt *fmt, PortRDump *x) { int i; fmtprint(fmt, "PortRDump"); for(i=0; i<x->nmap; i++){ fmtprint(fmt, " "); portMapPrint(fmt, &x->map[i]); } }
int conninfo(Fmt *f) { int i; typedef struct { int val; char *name; } Tab; static Tab captab[] = { { 1, "raw-mode" }, { 2, "mpx-mode" }, { 4, "unicode" }, { 8, "large-files" }, { 0x10, "NT-smbs" }, { 0x20, "rpc-remote-APIs" }, { 0x40, "status32" }, { 0x80, "l2-oplocks" }, { 0x100, "lock-read" }, { 0x200, "NT-find" }, { 0x1000, "Dfs" }, { 0x2000, "info-passthru" }, { 0x4000, "large-readx" }, { 0x8000, "large-writex" }, { 0x800000, "Unix" }, { 0x20000000, "bulk-transfer" }, { 0x40000000, "compressed" }, { 0x80000000, "extended-security" }, }; static Tab sectab[] = { { 1, "user-auth" }, { 2, "challange-response" }, { 4, "signing-available" }, { 8, "signing-required" }, }; fmtprint(f, "%q %q %q %q %+ldsec %dmtu %s\n", Sess->auth->user, Sess->cname, Sess->auth->windom, Sess->remos, Sess->slip, Sess->mtu, Sess->isguest? "as guest": ""); fmtprint(f, "caps: "); for(i = 0; i < nelem(captab); i++) if(Sess->caps & captab[i].val) fmtprint(f, "%s ", captab[i].name); fmtprint(f, "\n"); fmtprint(f, "security: "); for(i = 0; i < nelem(sectab); i++) if(Sess->secmode & sectab[i].val) fmtprint(f, "%s ", sectab[i].name); fmtprint(f, "\n"); if(Sess->nbt) fmtprint(f, "transport: cifs over netbios\n"); else fmtprint(f, "transport: cifs\n"); return 0; }
int linefmt(Fmt *f) { Istack *is; for(is=istack; is && !is->b; is=is->next) ; if(is) return fmtprint(f, "%S:%d", is->name, is->lineno); else return fmtprint(f, "<no input>"); }
static int fmtP(Fmt* f) { uintmem pa; pa = va_arg(f->args, uintmem); if(f->flags & FmtSharp) return fmtprint(f, "%#16.16llx", pa); return fmtprint(f, "%llu", pa); }
int afmt(Fmt *f) { Area *a; a = va_arg(f->args, Area*); if(a == nil) return fmtstrcpy(f, "<nil>"); if(a->floating) return fmtstrcpy(f, "~"); if(a->screen > 0 || (f->flags & FmtSharp)) return fmtprint(f, "%d:%d", a->screen, area_idx(a)); return fmtprint(f, "%d", area_idx(a)); }
void cout(ASTNode *n) { Fmt f; char buf[4096]; fmtfdinit(&f, 1, buf, sizeof buf); fout = &f; fmtprint(fout, "#include <u.h>\n#include <libc.h>\n#include <mp.h>\n#include \"sim.h\"\n\n"); coutdecl(n, nil); fmtprint(fout, "\n"); coutiprint(n, 0, 0); fmtfdflush(&f); }
char* ctlstring(void) { int i; Fmt fmt; fmtstrinit(&fmt); fmtprint(&fmt, "inquiry %s\n", inquiry); fmtprint(&fmt, "geometry %lld %lld %lld %lld %lld\n", nsect, sectsize, c, h, s); for(i=0; i<nelem(tab); i++) if(tab[i].inuse) fmtprint(&fmt, "part %s %lld %lld\n", tab[i].name, tab[i].offset, tab[i].length); return fmtstrflush(&fmt); }
int vtscorefmt(Fmt *f) { uint8_t *v; int i; v = va_arg(f->args, uint8_t*); if(v == nil) fmtprint(f, "*"); else for(i = 0; i < VtScoreSize; i++) fmtprint(f, "%2.2x", v[i]); return 0; }
int shareinfo(Fmt *f) { int i, j, n; char *type; Shareinfo2 si2; Share *sp, *sip; if((n = RAPshareenum(Sess, &Ipc, &sip)) < 1){ fmtprint(f, "can't enumerate shares: %r\n"); return 0; } for(i = 0; i < n; i++){ fmtprint(f, "%-13q ", sip[i].name); sp = &sip[i]; for(j = 0; j < Nshares; j++) if(strcmp(Shares[j].name, sip[i].name) == 0){ sp = &Shares[j]; break; } if(j >= Nshares) sp->tid = Ipc.tid; if(RAPshareinfo(Sess, sp, sp->name, &si2) != -1){ switch(si2.type){ case STYPE_DISKTREE: type = "disk"; break; case STYPE_PRINTQ: type = "printq"; break; case STYPE_DEVICE: type = "device"; break; case STYPE_IPC: type = "ipc"; break; case STYPE_SPECIAL: type = "special"; break; case STYPE_TEMP: type = "temp"; break; default: type = "unknown"; break; } fmtprint(f, "%-8s %5d/%-5d %s", type, si2.activeusrs, si2.maxusrs, si2.comment); free(si2.name); free(si2.comment); free(si2.path); free(si2.passwd); } fmtprint(f, "\n"); } free(sip); return 0; }
/* %Z from gc, for quoting import paths */ int Zconv(Fmt *fp) { Rune r; char *s, *se; int n; s = va_arg(fp->args, char*); if(s == nil) return fmtstrcpy(fp, "<nil>"); se = s + strlen(s); // NOTE: Keep in sync with ../gc/go.c:/^Zconv. while(s < se) { n = chartorune(&r, s); s += n; switch(r) { case Runeerror: if(n == 1) { fmtprint(fp, "\\x%02x", (uchar)*(s-1)); break; } // fall through default: if(r < ' ') { fmtprint(fp, "\\x%02x", r); break; } fmtrune(fp, r); break; case '\t': fmtstrcpy(fp, "\\t"); break; case '\n': fmtstrcpy(fp, "\\n"); break; case '\"': case '\\': fmtrune(fp, '\\'); fmtrune(fp, r); break; case 0xFEFF: // BOM, basically disallowed in source code fmtstrcpy(fp, "\\uFEFF"); break; } } return 0; }
char* ctlstring(void) { Part *p; Fmt fmt; fmtstrinit(&fmt); fmtprint(&fmt, "inquiry %s\n", inquiry); fmtprint(&fmt, "geometry %lld %lld\n", nsect, sectsize); for (p = tab; p < tab + nelem(tab); p++) if (p->inuse) fmtprint(&fmt, "part %s %lld %lld\n", p->name, p->offset, p->offset + p->length); return fmtstrflush(&fmt); }
/* binding -- print a binding statement */ static void binding(Format *f, char *keyword, Tree *tree) { Tree *np; char *sep = ""; fmtprint(f, "%s(", keyword); for (np = tree->u[0].p; np != NULL; np = np->u[1].p) { Tree *binding; assert(np->kind == nList); binding = np->u[0].p; assert(binding != NULL); assert(binding->kind == nAssign); fmtprint(f, "%s%#T=%T", sep, binding->u[0].p, binding->u[1].p); sep = ";"; } fmtprint(f, ")"); }
void error(char *fmt, ...) { va_list arg; char buf[256]; Fmt f; fmtfdinit(&f, 2, buf, sizeof buf); fmtprint(&f, "%s: ", argv0); va_start(arg, fmt); fmtprint(&f, fmt, arg); va_end(arg); fmtprint(&f, "\n"); fmtfdflush(&f); threadexitsall(buf); }
void error(char *fmt, ...) { Fmt f; char buf[128]; va_list arg; fmtfdinit(&f, 2, buf, sizeof buf); fmtprint(&f, "%s: ", argv0); va_start(arg, fmt); fmtvprint(&f, fmt, arg); va_end(arg); fmtprint(&f, "\n"); fmtfdflush(&f); errs = "errors"; }