[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.10

1.10    ! deraadt     1: /*     $OpenBSD: rpc_sample.c,v 1.8 2001/11/24 19:17:47 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.10    ! deraadt    51: static void write_sample_client __P((char *, version_list *));
        !            52: static void write_sample_server __P((definition *));
        !            53: static void return_type __P((proc_list *));
1.1       deraadt    54:
                     55: void
                     56: write_sample_svc(def)
                     57:      definition *def;
                     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)
                     68:      definition *def;
                     69: {
                     70:         version_list *vp;
                     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)
        !            86:      char *program_name;
1.1       deraadt    87:      version_list *vp;
                     88: {
                     89:   proc_list *proc;
                     90:   int i;
                     91:   decl_list *l;
                     92:
1.10    ! deraadt    93:   f_print(fout, "\n\nvoid\n");
        !            94:   pvname(program_name, vp->vers_num);
        !            95:   if (Cflag)
        !            96:     f_print(fout,"(char *host)\n{\n");
1.1       deraadt    97:   else
1.10    ! deraadt    98:     f_print(fout, "(host)\nchar *host;\n{\n");
1.1       deraadt    99:   f_print(fout, "\tCLIENT *clnt;\n");
                    100:
                    101:   i = 0;
                    102:   for (proc = vp->procs; proc != NULL; proc = proc->next) {
                    103:       f_print(fout, "\t");
                    104:       ptype(proc->res_prefix, proc->res_type, 1);
                    105:       f_print(fout, " *result_%d;\n",++i);
                    106:       /* print out declarations for arguments */
1.10    ! deraadt   107:       if (proc->arg_num < 2 && !newstyle) {
        !           108:        f_print(fout, "\t");
        !           109:        if (!streq(proc->args.decls->decl.type, "void"))
1.1       deraadt   110:          ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
                    111:        else
1.10    ! deraadt   112:          f_print(fout, "char *");  /* cannot have "void" type */
1.1       deraadt   113:        f_print(fout, " ");
1.10    ! deraadt   114:        pvname(proc->proc_name, vp->vers_num);
1.1       deraadt   115:        f_print(fout, "_arg;\n");
1.10    ! deraadt   116:       } else if (!streq(proc->args.decls->decl.type, "void")) {
1.1       deraadt   117:        for (l = proc->args.decls; l != NULL; l = l->next) {
1.10    ! deraadt   118:          f_print(fout, "\t");
1.1       deraadt   119:          ptype(l->decl.prefix, l->decl.type, 1);
1.10    ! deraadt   120:          f_print(fout, " ");
        !           121:          pvname(proc->proc_name, vp->vers_num);
        !           122:          f_print(fout, "_%s;\n", l->decl.name);
        !           123: /*       pdeclaration(proc->args.argname, &l->decl, 1, ";\n");*/
1.1       deraadt   124:        }
                    125:       }
                    126:     }
                    127:
                    128:   /* generate creation of client handle */
1.10    ! deraadt   129:   f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
1.1       deraadt   130:          program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
                    131:   f_print(fout, "\tif (clnt == NULL) {\n");
                    132:   f_print(fout, "\t\tclnt_pcreateerror(host);\n");
                    133:   f_print(fout, "\t\texit(1);\n\t}\n");
                    134:
                    135:   /* generate calls to procedures */
                    136:   i = 0;
                    137:   for (proc = vp->procs; proc != NULL; proc = proc->next) {
1.10    ! deraadt   138:       f_print(fout, "\tresult_%d = ",++i);
1.1       deraadt   139:       pvname(proc->proc_name, vp->vers_num);
                    140:       if (proc->arg_num < 2 && !newstyle) {
1.10    ! deraadt   141:        f_print(fout, "(");
        !           142:        if (streq(proc->args.decls->decl.type, "void"))  /* cast to void* */
1.1       deraadt   143:          f_print(fout, "(void*)");
1.10    ! deraadt   144:        f_print(fout, "&");
        !           145:        pvname(proc->proc_name, vp->vers_num);
1.1       deraadt   146:        f_print(fout, "_arg, clnt);\n");
1.10    ! deraadt   147:       } else if (streq(proc->args.decls->decl.type, "void")) {
1.1       deraadt   148:            f_print(fout, "(clnt);\n");
                    149:          }
                    150:       else {
                    151:        f_print(fout, "(");
                    152:        for (l = proc->args.decls;  l != NULL; l = l->next) {
1.10    ! deraadt   153:          pvname(proc->proc_name, vp->vers_num);
1.1       deraadt   154:          f_print(fout, "_%s, ", l->decl.name);
                    155:        }
                    156:        f_print(fout, "clnt);\n");
                    157:       }
                    158:       f_print(fout, "\tif (result_%d == NULL) {\n", i);
                    159:       f_print(fout, "\t\tclnt_perror(clnt, \"call failed:\");\n");
                    160:       f_print(fout, "\t}\n");
                    161:     }
                    162:
1.10    ! deraadt   163:   f_print(fout, "\tclnt_destroy(clnt);\n");
1.1       deraadt   164:   f_print(fout, "}\n");
                    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) {
                    176:                        f_print(fout, "\n");
1.10    ! deraadt   177: /*                     if (Cflag)
        !           178:                                f_print(fout, "extern \"C\"{\n");
1.1       deraadt   179: */
                    180:                        return_type(proc);
                    181:                        f_print(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:
                    185:                        f_print(fout, "{\n");
                    186:                        f_print(fout, "\n\tstatic ");
1.10    ! deraadt   187:                        if (!streq(proc->res_type, "void"))
        !           188:                                return_type(proc);
1.4       pvalchev  189:                        else
1.10    ! deraadt   190:                                f_print(fout, "char*");  /* cannot have void type */
1.7       miod      191:                        f_print(fout, " result;\n");
1.10    ! deraadt   192:                        f_print(fout,
        !           193:                            "\n\t/*\n\t * insert server code here\n\t */\n\n");
        !           194:                        if (!streq(proc->res_type, "void"))
        !           195:                                f_print(fout, "\treturn(&result);\n}\n");
1.1       deraadt   196:                        else  /* cast back to void * */
1.10    ! deraadt   197:                                f_print(fout, "\treturn((void*) &result);\n}\n");
        !           198: /*                     if (Cflag)
        !           199:                                f_print(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: {
                    215:        f_print(fout, "/*\n");
                    216:        f_print(fout, " * This is sample code generated by rpcgen.\n");
                    217:        f_print(fout, " * These are only templates and you can use them\n");
                    218:        f_print(fout, " * as a guideline for developing your own functions.\n");
                    219:        f_print(fout, " */\n\n");
                    220: }
                    221:
                    222: void
                    223: write_sample_clnt_main()
                    224: {
1.10    ! deraadt   225:        list *l;
        !           226:        definition *def;
        !           227:        version_list *vp;
        !           228:
        !           229:        f_print(fout, "\n\n");
        !           230:        if (Cflag)
        !           231:                f_print(fout,"main(int argc, char *argv[])\n{\n");
        !           232:        else
        !           233:                f_print(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n");
1.1       deraadt   234:
1.10    ! deraadt   235:        f_print(fout, "\tchar *host;");
        !           236:        f_print(fout, "\n\n\tif (argc < 2) {");
        !           237:        f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n");
        !           238:        f_print(fout, "\t\texit(1);\n\t}");
        !           239:        f_print(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.10    ! deraadt   246:                        f_print(fout, "\t");
1.1       deraadt   247:                        pvname(def->def_name, vp->vers_num);
1.10    ! deraadt   248:                        f_print(fout, "(host);\n");
1.1       deraadt   249:                }
1.10    ! deraadt   250:        }
        !           251:        f_print(fout, "}\n");
1.1       deraadt   252: }