[BACK]Return to rpc_sample.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rpcgen

Annotation of src/usr.bin/rpcgen/rpc_sample.c, Revision 1.13

1.13    ! deraadt     1: /*     $OpenBSD: rpc_sample.c,v 1.12 2002/06/01 01:40:38 deraadt Exp $ */
1.1       deraadt     2: /*     $NetBSD: rpc_sample.c,v 1.2 1995/06/11 21:50:01 pk Exp $        */
                      3: /*
                      4:  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
                      5:  * unrestricted use provided that this legend is included on all tape
                      6:  * media and as a part of the software program in whole or part.  Users
                      7:  * may copy or modify Sun RPC without charge, but are not authorized
                      8:  * to license or distribute it to anyone else except as part of a product or
                      9:  * program developed by the user or with the express written consent of
                     10:  * Sun Microsystems, Inc.
                     11:  *
                     12:  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
                     13:  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
                     14:  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
                     15:  *
                     16:  * Sun RPC is provided with no support and without any obligation on the
                     17:  * part of Sun Microsystems, Inc. to assist in its use, correction,
                     18:  * modification or enhancement.
                     19:  *
                     20:  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
                     21:  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
                     22:  * OR ANY PART THEREOF.
                     23:  *
                     24:  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
                     25:  * or profits or other special, indirect and consequential damages, even if
                     26:  * Sun has been advised of the possibility of such damages.
                     27:  *
                     28:  * Sun Microsystems, Inc.
                     29:  * 2550 Garcia Avenue
                     30:  * Mountain View, California  94043
                     31:  */
                     32:
                     33: #ifndef lint
                     34: static char sccsid[] = "@(#)rpc_sample.c  1.1  90/08/30  (C) 1987 SMI";
                     35:
                     36: #endif
                     37:
                     38: /*
                     39:  * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
                     40:  */
                     41:
                     42: #include <sys/cdefs.h>
                     43: #include <stdio.h>
                     44: #include <string.h>
                     45: #include "rpc_parse.h"
                     46: #include "rpc_util.h"
                     47:
                     48: static char RQSTP[] = "rqstp";
                     49:
                     50: void printarglist();
1.11      millert    51: static void write_sample_client(char *, version_list *);
                     52: static void write_sample_server(definition *);
                     53: static void return_type(proc_list *);
1.1       deraadt    54:
                     55: void
                     56: write_sample_svc(def)
1.13    ! deraadt    57:        definition *def;
1.1       deraadt    58: {
                     59:
1.10      deraadt    60:        if (def->def_kind != DEF_PROGRAM)
                     61:                return;
1.1       deraadt    62:        write_sample_server(def);
                     63: }
                     64:
                     65:
                     66: int
                     67: write_sample_clnt(def)
1.13    ! deraadt    68:        definition *def;
1.1       deraadt    69: {
1.13    ! deraadt    70:        version_list *vp;
1.1       deraadt    71:        int count = 0;
                     72:
1.10      deraadt    73:        if (def->def_kind != DEF_PROGRAM)
                     74:                return(0);
1.1       deraadt    75:        /* generate sample code for each version */
                     76:        for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1.10      deraadt    77:                write_sample_client(def->def_name, vp);
                     78:                ++count;
1.1       deraadt    79:        }
1.10      deraadt    80:        return(count);
1.1       deraadt    81: }
                     82:
                     83:
1.10      deraadt    84: static void
                     85: write_sample_client(program_name, vp)
1.13    ! deraadt    86:        char *program_name;
        !            87:        version_list *vp;
1.1       deraadt    88: {
1.13    ! deraadt    89:        proc_list *proc;
        !            90:        int i;
        !            91:        decl_list *l;
        !            92:
        !            93:        fprintf(fout, "\n\nvoid\n");
        !            94:        pvname(program_name, vp->vers_num);
        !            95:        if (Cflag)
        !            96:                fprintf(fout,"(char *host)\n{\n");
1.1       deraadt    97:        else
1.13    ! deraadt    98:                fprintf(fout, "(host)\nchar *host;\n{\n");
        !            99:        fprintf(fout, "\tCLIENT *clnt;\n");
        !           100:
        !           101:        i = 0;
        !           102:        for (proc = vp->procs; proc != NULL; proc = proc->next) {
        !           103:                fprintf(fout, "\t");
        !           104:                ptype(proc->res_prefix, proc->res_type, 1);
        !           105:                fprintf(fout, " *result_%d;\n",++i);
        !           106:                /* print out declarations for arguments */
        !           107:                if (proc->arg_num < 2 && !newstyle) {
        !           108:                        fprintf(fout, "\t");
        !           109:                        if (!streq(proc->args.decls->decl.type, "void"))
        !           110:                                ptype(proc->args.decls->decl.prefix,
        !           111:                                    proc->args.decls->decl.type, 1);
        !           112:                        else
        !           113:                                fprintf(fout, "char *"); /* cannot have "void" type */
        !           114:                        fprintf(fout, " ");
        !           115:                        pvname(proc->proc_name, vp->vers_num);
        !           116:                        fprintf(fout, "_arg;\n");
        !           117:                } else if (!streq(proc->args.decls->decl.type, "void")) {
        !           118:                        for (l = proc->args.decls; l != NULL; l = l->next) {
        !           119:                                fprintf(fout, "\t");
        !           120:                                ptype(l->decl.prefix, l->decl.type, 1);
        !           121:                                fprintf(fout, " ");
        !           122:                                pvname(proc->proc_name, vp->vers_num);
        !           123:                                fprintf(fout, "_%s;\n", l->decl.name);
        !           124:                /*              pdeclaration(proc->args.argname, &l->decl, 1, ";\n");*/
        !           125:                        }
        !           126:                }
1.1       deraadt   127:        }
                    128:
1.13    ! deraadt   129:        /* generate creation of client handle */
        !           130:        fprintf(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
        !           131:            program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
        !           132:        fprintf(fout, "\tif (clnt == NULL) {\n");
        !           133:        fprintf(fout, "\t\tclnt_pcreateerror(host);\n");
        !           134:        fprintf(fout, "\t\texit(1);\n\t}\n");
        !           135:
        !           136:        /* generate calls to procedures */
        !           137:        i = 0;
        !           138:        for (proc = vp->procs; proc != NULL; proc = proc->next) {
        !           139:                fprintf(fout, "\tresult_%d = ",++i);
        !           140:                pvname(proc->proc_name, vp->vers_num);
        !           141:                if (proc->arg_num < 2 && !newstyle) {
        !           142:                        fprintf(fout, "(");
        !           143:                        if (streq(proc->args.decls->decl.type, "void"))
        !           144:                                fprintf(fout, "(void*)");
        !           145:                        fprintf(fout, "&");
        !           146:                        pvname(proc->proc_name, vp->vers_num);
        !           147:                        fprintf(fout, "_arg, clnt);\n");
        !           148:                } else if (streq(proc->args.decls->decl.type, "void")) {
        !           149:                        fprintf(fout, "(clnt);\n");
        !           150:                } else {
        !           151:                        fprintf(fout, "(");
        !           152:                        for (l = proc->args.decls;      l != NULL; l = l->next) {
        !           153:                                pvname(proc->proc_name, vp->vers_num);
        !           154:                                fprintf(fout, "_%s, ", l->decl.name);
        !           155:                        }
        !           156:                        fprintf(fout, "clnt);\n");
        !           157:                }
        !           158:                fprintf(fout, "\tif (result_%d == NULL) {\n", i);
        !           159:                fprintf(fout, "\t\tclnt_perror(clnt, \"call failed:\");\n");
        !           160:                fprintf(fout, "\t}\n");
1.1       deraadt   161:        }
                    162:
1.13    ! deraadt   163:        fprintf(fout, "\tclnt_destroy(clnt);\n");
        !           164:        fprintf(fout, "}\n");
1.1       deraadt   165: }
                    166:
1.10      deraadt   167: static void
1.1       deraadt   168: write_sample_server(def)
                    169:        definition *def;
                    170: {
                    171:        version_list *vp;
                    172:        proc_list *proc;
                    173:
                    174:        for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    175:                for (proc = vp->procs; proc != NULL; proc = proc->next) {
1.12      deraadt   176:                        fprintf(fout, "\n");
1.10      deraadt   177: /*                     if (Cflag)
1.12      deraadt   178:                                fprintf(fout, "extern \"C\"{\n");
1.1       deraadt   179: */
                    180:                        return_type(proc);
1.12      deraadt   181:                        fprintf(fout, "* \n");
1.3       deraadt   182:                        pvname_svc(proc->proc_name, vp->vers_num);
1.10      deraadt   183:                        printarglist(proc, RQSTP, "struct svc_req *");
1.1       deraadt   184:
1.12      deraadt   185:                        fprintf(fout, "{\n");
                    186:                        fprintf(fout, "\n\tstatic ");
1.10      deraadt   187:                        if (!streq(proc->res_type, "void"))
                    188:                                return_type(proc);
1.4       pvalchev  189:                        else
1.12      deraadt   190:                                fprintf(fout, "char*");  /* cannot have void type */
                    191:                        fprintf(fout, " result;\n");
                    192:                        fprintf(fout,
1.10      deraadt   193:                            "\n\t/*\n\t * insert server code here\n\t */\n\n");
                    194:                        if (!streq(proc->res_type, "void"))
1.12      deraadt   195:                                fprintf(fout, "\treturn(&result);\n}\n");
1.1       deraadt   196:                        else  /* cast back to void * */
1.12      deraadt   197:                                fprintf(fout, "\treturn((void*) &result);\n}\n");
1.10      deraadt   198: /*                     if (Cflag)
1.12      deraadt   199:                                fprintf(fout, "}\n");
1.1       deraadt   200: */
1.10      deraadt   201:                }
1.1       deraadt   202:        }
                    203: }
                    204:
1.10      deraadt   205: static void
1.1       deraadt   206: return_type(plist)
                    207:        proc_list *plist;
                    208: {
1.10      deraadt   209:        ptype(plist->res_prefix, plist->res_type, 1);
1.1       deraadt   210: }
                    211:
1.10      deraadt   212: void
1.1       deraadt   213: add_sample_msg()
                    214: {
1.12      deraadt   215:        fprintf(fout, "/*\n");
                    216:        fprintf(fout, " * This is sample code generated by rpcgen.\n");
                    217:        fprintf(fout, " * These are only templates and you can use them\n");
                    218:        fprintf(fout, " * as a guideline for developing your own functions.\n");
                    219:        fprintf(fout, " */\n\n");
1.1       deraadt   220: }
                    221:
                    222: void
                    223: write_sample_clnt_main()
                    224: {
1.10      deraadt   225:        list *l;
                    226:        definition *def;
                    227:        version_list *vp;
                    228:
1.12      deraadt   229:        fprintf(fout, "\n\n");
1.10      deraadt   230:        if (Cflag)
1.12      deraadt   231:                fprintf(fout,"main(int argc, char *argv[])\n{\n");
1.10      deraadt   232:        else
1.12      deraadt   233:                fprintf(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n");
1.1       deraadt   234:
1.12      deraadt   235:        fprintf(fout, "\tchar *host;");
                    236:        fprintf(fout, "\n\n\tif (argc < 2) {");
                    237:        fprintf(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n");
                    238:        fprintf(fout, "\t\texit(1);\n\t}");
                    239:        fprintf(fout, "\n\thost = argv[1];\n");
1.1       deraadt   240:
1.10      deraadt   241:        for (l = defined; l != NULL; l = l->next) {
1.1       deraadt   242:                def = l->val;
1.10      deraadt   243:                if (def->def_kind != DEF_PROGRAM)
1.1       deraadt   244:                        continue;
                    245:                for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1.12      deraadt   246:                        fprintf(fout, "\t");
1.1       deraadt   247:                        pvname(def->def_name, vp->vers_num);
1.12      deraadt   248:                        fprintf(fout, "(host);\n");
1.1       deraadt   249:                }
1.10      deraadt   250:        }
1.12      deraadt   251:        fprintf(fout, "}\n");
1.1       deraadt   252: }