xref: /plan9/sys/src/libsunrpc/fmt.c (revision 34e0422554c8e8bef66509534d2c44f4660bf678)
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include <sunrpc.h>
5 
6 /*
7  * print formatters
8  */
9 int
sunRpcFmt(Fmt * f)10 sunRpcFmt(Fmt *f)
11 {
12 	SunRpc *rpc;
13 
14 	rpc = va_arg(f->args, SunRpc*);
15 	sunRpcPrint(f, rpc);
16 	return 0;
17 }
18 
19 static SunProg **fmtProg;
20 static int nfmtProg;
21 static RWLock fmtLock;
22 
23 void
sunFmtInstall(SunProg * p)24 sunFmtInstall(SunProg *p)
25 {
26 	int i;
27 
28 	wlock(&fmtLock);
29 	for(i=0; i<nfmtProg; i++){
30 		if(fmtProg[i] == p){
31 			wunlock(&fmtLock);
32 			return;
33 		}
34 	}
35 	if(nfmtProg%16 == 0)
36 		fmtProg = erealloc(fmtProg, sizeof(fmtProg[0])*(nfmtProg+16));
37 	fmtProg[nfmtProg++] = p;
38 	wunlock(&fmtLock);
39 }
40 
41 int
sunCallFmt(Fmt * f)42 sunCallFmt(Fmt *f)
43 {
44 	int i;
45 	void (*fmt)(Fmt*, SunCall*);
46 	SunCall *c;
47 	SunProg *p;
48 
49 	c = va_arg(f->args, SunCall*);
50 	rlock(&fmtLock);
51 	for(i=0; i<nfmtProg; i++){
52 		p = fmtProg[i];
53 		if(p->prog == c->rpc.prog && p->vers == c->rpc.vers){
54 			runlock(&fmtLock);
55 			if(c->type < 0 || c->type >= p->nproc || (fmt=p->proc[c->type].fmt) == nil)
56 				return fmtprint(f, "unknown proc %c%d", "TR"[c->type&1], c->type>>1);
57 			(*fmt)(f, c);
58 			return 0;
59 		}
60 	}
61 	runlock(&fmtLock);
62 	fmtprint(f, "<sunrpc %d %d %c%d>", c->rpc.prog, c->rpc.vers, "TR"[c->type&1], c->type>>1);
63 	return 0;
64 }
65