cpp-fixes-20080630
[openafs.git] / src / rxgen / rpc_clntout.c
1 /* @(#)rpc_clntout.c    1.2 87/11/24 3.9 RPCSRC */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
9  * 
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  * 
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  * 
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  * 
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  * 
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30
31 /*
32  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
33  * Copyright (C) 1987, Sun Microsytsems, Inc.
34  */
35 #include <afsconfig.h>
36 #include <afs/param.h>
37
38 RCSID
39     ("$Header$");
40
41 #include <stdio.h>
42 #include <string.h>
43 #include "rpc_scan.h"
44 #include "rpc_parse.h"
45 #include "rpc_util.h"
46
47 #define DEFAULT_TIMEOUT 25      /* in seconds */
48
49 /* static prototypes */
50 static void write_program(definition * def);
51 static char *ampr(char *type);
52 static void printbody(proc_list * proc);
53
54 void
55 write_stubs(void)
56 {
57     list *l;
58     definition *def;
59
60     f_print(fout, "\nstatic struct timeval TIMEOUT = { %d, 0 };\n",
61             DEFAULT_TIMEOUT);
62     for (l = defined; l != NULL; l = l->next) {
63         def = (definition *) l->val;
64         if (def->def_kind == DEF_PROGRAM) {
65             write_program(def);
66         }
67     }
68 }
69
70
71 static void
72 write_program(definition * def)
73 {
74     version_list *vp;
75     proc_list *proc;
76
77     for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
78         for (proc = vp->procs; proc != NULL; proc = proc->next) {
79             f_print(fout, "\n");
80             ptype(proc->res_prefix, proc->res_type, 1);
81             f_print(fout, "*\n");
82             pvname(proc->proc_name, vp->vers_num);
83             f_print(fout, "(argp, clnt)\n");
84             f_print(fout, "\t");
85             ptype(proc->arg_prefix, proc->arg_type, 1);
86             f_print(fout, "*argp;\n");
87             f_print(fout, "\tCLIENT *clnt;\n");
88             f_print(fout, "{\n");
89             printbody(proc);
90             f_print(fout, "}\n\n");
91         }
92     }
93 }
94
95 static char *
96 ampr(char *type)
97 {
98     if (isvectordef(type, REL_ALIAS)) {
99         return ("");
100     } else {
101         return ("&");
102     }
103 }
104
105 static void
106 printbody(proc_list * proc)
107 {
108     f_print(fout, "\tstatic ");
109     if (streq(proc->res_type, "void")) {
110         f_print(fout, "char ");
111     } else {
112         ptype(proc->res_prefix, proc->res_type, 0);
113     }
114     f_print(fout, "res;\n");
115     f_print(fout, "\n");
116     f_print(fout, "\tmemset(%sres, 0, sizeof(res));\n", ampr(proc->res_type));
117     f_print(fout,
118             "\tif (clnt_call(clnt, %s, xdr_%s, argp, xdr_%s, %sres, TIMEOUT) != RPC_SUCCESS) {\n",
119             proc->proc_name, stringfix(proc->arg_type),
120             stringfix(proc->res_type), ampr(proc->res_type));
121     f_print(fout, "\t\treturn (NULL);\n");
122     f_print(fout, "\t}\n");
123     if (streq(proc->res_type, "void")) {
124         f_print(fout, "\treturn ((void *)%sres);\n", ampr(proc->res_type));
125     } else {
126         f_print(fout, "\treturn (%sres);\n", ampr(proc->res_type));
127     }
128 }