home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / rpc3.9 / part12 / rpcgen.ms < prev   
Encoding:
Text File  |  1988-02-27  |  27.9 KB  |  1,165 lines

  1. .\" @(#)rpcgen.ms    1.2 87/11/09 3.9 RPCSRC
  2. .de BT
  3. .if \\n%=1 .tl ''- % -''
  4. ..
  5. .ND
  6. .\" prevent excess underlining in nroff
  7. .if n .fp 2 R
  8. .OH '\fBrpcgen\fP Programming Guide''Page %'
  9. .EH 'Page %''\fBrpcgen\fP Programming Guide'
  10. .if \\n%=1 .bp
  11. .SH
  12. \&\fBrpcgen\fP Programming Guide
  13. .NH 0
  14. \&The \fBrpcgen\fP Protocol Compiler
  15. .IX rpcgen "" \fIrpcgen\fP "" PAGE MAJOR
  16. .LP
  17. .IX RPC "" "" \fIrpcgen\fP
  18. The details of programming applications to use Remote Procedure Calls 
  19. can be overwhelming.  Perhaps most daunting is the writing of the XDR 
  20. routines necessary to convert procedure arguments and results into 
  21. their network format and vice-versa.  
  22. .LP
  23. Fortunately, 
  24. .I rpcgen 
  25. exists to help programmers write RPC applications simply and directly.
  26. .I rpcgen 
  27. does most of the dirty work, allowing programmers to debug 
  28. the  main  features of their application, instead of requiring them to
  29. spend most of their time debugging their network interface code.
  30. .LP
  31. .I rpcgen 
  32. is a  compiler.  It accepts a remote program interface definition written
  33. in a language, called RPC Language, which is similar to C.  It produces a C
  34. language output which includes stub versions of the client routines, a
  35. server skeleton, XDR filter routines for both parameters and results, and a
  36. header file that contains common definitions. The client stubs interface
  37. with the RPC library and effectively hide the network from their callers.
  38. The server stub similarly hides the network from the server procedures that
  39. are to be invoked by remote clients.
  40. .I rpcgen 's
  41. output files can be compiled and linked in the usual way.  The developer
  42. writes server procedures\(emin any language that observes Sun calling
  43. conventions\(emand links them with the server skeleton produced by
  44. .I rpcgen 
  45. to get an executable server program.  To use a remote program, a programmer
  46. writes an ordinary main program that makes local procedure calls to the 
  47. client stubs produced by
  48. .I rpcgen .
  49. Linking this program with 
  50. .I rpcgen 's
  51. stubs creates an executable program.  (At present the main program must be 
  52. written in C).
  53. .I rpcgen 
  54. options can be used to suppress stub generation and to specify the transport
  55. to be used by the server stub.
  56. .LP
  57. Like all compilers, 
  58. .I rpcgen 
  59. reduces development time
  60. that would otherwise be spent coding and debugging low-level routines.
  61. All compilers, including 
  62. .I rpcgen ,
  63. do this at a small cost in efficiency
  64. and flexibility.  However,   many compilers allow  escape  hatches for
  65. programmers to  mix low-level code with  high-level code. 
  66. .I rpcgen 
  67. is no exception.  In speed-critical applications, hand-written routines 
  68. can be linked with the 
  69. .I rpcgen 
  70. output without any difficulty.  Also, one may proceed by using
  71. .I rpcgen 
  72. output as a starting point, and rewriting it as necessary.
  73. .NH 1
  74. \&Converting Local Procedures into Remote Procedures
  75. .IX rpcgen "local procedures" \fIrpcgen\fP
  76. .IX rpcgen "remote procedures" \fIrpcgen\fP
  77. .LP
  78. Assume an application that runs on a single machine, one which we want 
  79. to convert to run over the network.  Here we will demonstrate such a 
  80. conversion by way of a simple example\(ema program that prints a 
  81. message to the console:
  82. .ie t .DS
  83. .el .DS L
  84. .ft I
  85. /*
  86.  * printmsg.c: print a message on the console
  87.  */
  88. .ft CW
  89. #include <stdio.h>
  90.  
  91. main(argc, argv)
  92.     int argc;
  93.     char *argv[];
  94. {
  95.     char *message;
  96.  
  97.     if (argc < 2) {
  98.         fprintf(stderr, "usage: %s <message>\en", argv[0]);
  99.         exit(1);
  100.     }
  101.     message = argv[1];
  102.  
  103.     if (!printmessage(message)) {
  104.         fprintf(stderr, "%s: couldn't print your message\en",
  105.             argv[0]);
  106.         exit(1);
  107.     } 
  108.     printf("Message delivered!\n");
  109. }
  110. .ft I
  111. /*
  112.  * Print a message to the console.
  113.  * Return a boolean indicating whether the message was actually printed.
  114.  */
  115. .ft CW
  116. printmessage(msg)
  117.     char *msg;
  118. {
  119.     FILE *f;
  120.  
  121.     f = fopen("/dev/console", "w");
  122.     if (f == NULL) {
  123.         return (0);
  124.     }
  125.     fprintf(f, "%s\en", msg);
  126.     fclose(f);
  127.     return(1);
  128. }
  129. .DE
  130. .LP
  131. And then, of course:
  132. .ie t .DS
  133. .el .DS L
  134. .ft CW
  135. example%  \fBcc printmsg.c -o printmsg\fP
  136. example%  \fBprintmsg "Hello, there."\fP
  137. Message delivered!
  138. example%
  139. .DE
  140. .LP
  141. If  
  142. .I printmessage 
  143. was turned into  a remote procedure,
  144. then it could be  called from anywhere in   the network.  
  145. Ideally,  one would just  like to stick   a  keyword like  
  146. .I remote 
  147. in  front  of a
  148. procedure to turn it into a  remote procedure.  Unfortunately,
  149. we  have to live  within the  constraints of  the   C language, since 
  150. it existed   long before  RPC did.  But   even without language 
  151. support, it's not very difficult to make a procedure remote.
  152. .LP
  153. In  general, it's necessary to figure  out  what the types are for
  154. all procedure inputs and outputs.  In  this case,   we  have a 
  155. procedure
  156. .I printmessage 
  157. which takes a  string as input, and returns  an integer
  158. as output.  Knowing  this, we can write a  protocol specification in RPC
  159. language that  describes the remote  version of 
  160. .I printmessage .
  161. Here it is:
  162. .ie t .DS
  163. .el .DS L
  164. .ft I
  165. /*
  166.  * msg.x: Remote message printing protocol
  167.  */
  168. .ft CW
  169.  
  170. program MESSAGEPROG {
  171.     version MESSAGEVERS {
  172.         int PRINTMESSAGE(string) = 1;
  173.     } = 1;
  174. } = 99;
  175. .DE
  176. .LP
  177. Remote procedures are part of remote programs, so we actually declared
  178. an  entire  remote program  here  which contains  the single procedure
  179. .I PRINTMESSAGE .
  180. This procedure was declared to be  in version  1 of the
  181. remote program.  No null procedure (procedure 0) is necessary because
  182. .I rpcgen 
  183. generates it automatically.
  184. .LP
  185. Notice that everything is declared with all capital  letters.  This is
  186. not required, but is a good convention to follow.
  187. .LP
  188. Notice also that the argument type is "string" and not "char *".  This
  189. is because a "char *" in C is ambiguous. Programmers usually intend it
  190. to mean  a null-terminated string   of characters, but  it  could also
  191. represent a pointer to a single character or a  pointer to an array of
  192. characters.  In  RPC language,  a  null-terminated  string is 
  193. unambiguously called a "string". 
  194. .LP
  195. There are  just two more things to  write.  First, there is the remote
  196. procedure itself.  Here's the definition of a remote procedure
  197. to implement the
  198. .I PRINTMESSAGE
  199. procedure we declared above:
  200. .ie t .DS
  201. .el .DS L
  202. .vs 11
  203. .ft I
  204. /*
  205.  * msg_proc.c: implementation of the remote procedure "printmessage"
  206.  */
  207. .ft CW
  208.  
  209. #include <stdio.h>
  210. #include <rpc/rpc.h>    /* \fIalways needed\fP  */
  211. #include "msg.h"        /* \fIneed this too: msg.h will be generated by rpcgen\fP */
  212.  
  213. .ft I
  214. /*
  215.  * Remote verson of "printmessage"
  216.  */
  217. .ft CW
  218. int *
  219. printmessage_1(msg)
  220.     char **msg;
  221. {
  222.     static int result;  /* \fImust be static!\fP */
  223.     FILE *f;
  224.  
  225.     f = fopen("/dev/console", "w");
  226.     if (f == NULL) {
  227.         result = 0;
  228.         return (&result);
  229.     }
  230.     fprintf(f, "%s\en", *msg);
  231.     fclose(f);
  232.     result = 1;
  233.     return (&result);
  234. }
  235. .vs
  236. .DE
  237. .LP
  238. Notice here that the declaration of the remote procedure
  239. .I printmessage_1 
  240. differs from that of the local procedure
  241. .I printmessage 
  242. in three ways:
  243. .IP  1.
  244. It takes a pointer to a string instead of a string itself.  This
  245. is true of all  remote procedures:  they always take pointers to  their
  246. arguments rather than the arguments themselves.
  247. .IP  2.
  248. It returns a pointer to an  integer instead of  an integer itself. This is
  249. also generally true of remote procedures: they always return a pointer
  250. to their results.
  251. .IP  3.
  252. It has  an "_1" appended to  its name.   In  general, all  remote
  253. procedures called by 
  254. .I rpcgen 
  255. are named by  the following rule: the name in the program  definition  
  256. (here 
  257. .I PRINTMESSAGE )
  258. is converted   to all
  259. lower-case   letters, an underbar ("_")   is appended  to it, and
  260. finally the version number (here 1) is appended.
  261. .LP
  262. The last thing to do is declare the main client program that will call
  263. the remote procedure. Here it is:
  264. .ie t .DS
  265. .el .DS L
  266. .ft I
  267. /*
  268.  * rprintmsg.c: remote version of "printmsg.c"
  269.  */
  270. .ft CW
  271. #include <stdio.h>
  272. #include <rpc/rpc.h>     /* \fIalways needed\fP  */
  273. #include "msg.h"         /* \fIneed this too: msg.h will be generated by rpcgen\fP */
  274.  
  275. main(argc, argv)
  276.     int argc;
  277.     char *argv[];
  278. {
  279.     CLIENT *cl;
  280.     int *result;
  281.     char *server;
  282.     char *message;
  283.  
  284.     if (argc < 3) {
  285.         fprintf(stderr, "usage: %s host message\en", argv[0]);
  286.         exit(1);
  287.     }
  288.  
  289. .ft I
  290.     /*
  291.      * Save values of command line arguments 
  292.      */
  293. .ft CW
  294.     server = argv[1];
  295.     message = argv[2];
  296.  
  297. .ft I
  298.     /*
  299.      * Create client "handle" used for calling \fIMESSAGEPROG\fP on the
  300.      * server designated on the command line. We tell the RPC package
  301.      * to use the "tcp" protocol when contacting the server.
  302.      */
  303. .ft CW
  304.     cl = clnt_create(server, MESSAGEPROG, MESSAGEVERS, "tcp");
  305.     if (cl == NULL) {
  306. .ft I
  307.         /*
  308.          * Couldn't establish connection with server.
  309.          * Print error message and die.
  310.          */
  311. .ft CW
  312.         clnt_pcreateerror(server);
  313.         exit(1);
  314.     }
  315.     
  316. .ft I
  317.     /*
  318.      * Call the remote procedure "printmessage" on the server
  319.      */
  320. .ft CW
  321.     result = printmessage_1(&message, cl);
  322.     if (result == NULL) {
  323. .ft I
  324.         /*
  325.          * An error occurred while calling the server. 
  326.           * Print error message and die.
  327.          */
  328. .ft CW
  329.         clnt_perror(cl, server);
  330.         exit(1);
  331.     }
  332.  
  333. .ft I
  334.     /*
  335.      * Okay, we successfully called the remote procedure.
  336.      */
  337. .ft CW
  338.     if (*result == 0) {
  339. .ft I
  340.         /*
  341.          * Server was unable to print our message. 
  342.          * Print error message and die.
  343.          */
  344. .ft CW
  345.         fprintf(stderr, "%s: %s couldn't print your message\en", 
  346.             argv[0], server);    
  347.         exit(1);
  348.     } 
  349.  
  350. .ft I
  351.     /*
  352.      * The message got printed on the server's console
  353.      */
  354. .ft CW
  355.     printf("Message delivered to %s!\en", server);
  356. }
  357. .DE
  358. There are two things to note here:
  359. .IP  1.
  360. .IX "client handle"
  361. First a client "handle" is created  using the RPC library routine
  362. .I clnt_create .
  363. This client handle will be passed  to the stub routines
  364. which call the remote procedure.
  365. .IP  2.
  366. The remote procedure  
  367. .I printmessage_1 
  368. is called exactly  the same way as it is  declared in 
  369. .I msg_proc.c 
  370. except for the inserted client handle as the first argument.
  371. .LP
  372. Here's how to put all of the pieces together:
  373. .ie t .DS
  374. .el .DS L
  375. .ft CW
  376. example%  \fBrpcgen msg.x\fP
  377. example%  \fBcc rprintmsg.c msg_clnt.c -o rprintmsg\fP
  378. example%  \fBcc msg_proc.c msg_svc.c -o msg_server\fP
  379. .DE
  380. Two programs were compiled here: the client program 
  381. .I printmsg 
  382. and the server  program 
  383. .I msg_server .
  384. Before doing this  though,  
  385. .I rpcgen 
  386. was used to fill in the missing pieces.  
  387. .LP
  388. Here is what 
  389. .I rpcgen 
  390. did with the input file 
  391. .I msg.x :
  392. .IP  1.
  393. It created a header file called 
  394. .I msg.h 
  395. that contained \fI#define\fP's for
  396. .I MESSAGEPROG ,
  397. .I MESSAGEVERS 
  398. and    
  399. .I PRINTMESSAGE 
  400. for use in  the  other modules.
  401. .IP  2.
  402. It created client "stub" routines in the 
  403. .I msg_clnt.c 
  404. file.   In this case there is only one, the 
  405. .I printmessage_1 
  406. that was referred to from the
  407. .I printmsg 
  408. client program.  The name  of the output file for
  409. client stub routines is always formed in this way:  if the name of the
  410. input file is  
  411. .I FOO.x ,
  412. the   client  stubs   output file is    called
  413. .I FOO_clnt.c .
  414. .IP  3.
  415. It created  the  server   program which calls   
  416. .I printmessage_1 
  417. in
  418. .I msg_proc.c .
  419. This server program is named  
  420. .I msg_svc.c .
  421. The rule for naming the server output file is similar  to the 
  422. previous one:  for an input  file   called  
  423. .I FOO.x ,
  424. the   output   server   file is  named
  425. .I FOO_svc.c .
  426. .LP
  427. Now we're ready to have some fun.  First, copy the server to a
  428. remote machine and run it.  For this  example,  the
  429. machine is called "moon".  Server processes are run in the 
  430. background, because they never exit.
  431. .ie t .DS
  432. .el .DS L
  433. .ft CW
  434. moon% msg_server &           
  435. .DE
  436. Then on our local machine ("sun") we can print a message on "moon"s 
  437. console.
  438. .ie t .DS
  439. .el .DS L
  440. .ft CW
  441. sun% printmsg moon "Hello, moon."
  442. .DE
  443. The message will   get printed to  "moon"s  console. You  can print  a
  444. message on anybody's console (including your own) with this program if
  445. you are able to copy the server to their machine and run it.
  446. .NH 1
  447. \&Generating XDR Routines
  448. .IX RPC "generating XDR routines"
  449. .LP
  450. The previous example  only demonstrated  the  automatic generation of
  451. client  and server RPC  code. 
  452. .I rpcgen 
  453. may also  be used to generate XDR routines, that  is,  the routines
  454. necessary to  convert   local  data
  455. structures into network format and vice-versa.  This example presents
  456. a complete RPC service\(ema remote directory listing service, which uses
  457. .I rpcgen
  458. not  only  to generate stub routines, but also to  generate  the XDR
  459. routines.  Here is the protocol description file:
  460. .ie t .DS
  461. .el .DS L
  462. .ft I
  463. /*
  464.  * dir.x: Remote directory listing protocol
  465.  */
  466. .ft CW
  467. const MAXNAMELEN = 255;        /* \fImaximum length of a directory entry\fP */
  468.  
  469. typedef string nametype<MAXNAMELEN>;    /* \fIa directory entry\fP */
  470.  
  471. typedef struct namenode *namelist;        /* \fIa link in the listing\fP */
  472.  
  473. .ft I
  474. /*
  475.  * A node in the directory listing
  476.  */
  477. .ft CW
  478. struct namenode {
  479.     nametype name;        /* \fIname of directory entry\fP */
  480.     namelist next;        /* \fInext entry\fP */
  481. };
  482.  
  483. .ft I
  484. /*
  485.  * The result of a READDIR operation.
  486.  */
  487. .ft CW
  488. union readdir_res switch (int errno) {
  489. case 0:
  490.     namelist list;    /* \fIno error: return directory listing\fP */
  491. default:
  492.     void;        /* \fIerror occurred: nothing else to return\fP */
  493. };
  494.  
  495. .ft I
  496. /*
  497.  * The directory program definition
  498.  */
  499. .ft CW
  500. program DIRPROG {
  501.     version DIRVERS {
  502.         readdir_res
  503.         READDIR(nametype) = 1;
  504.     } = 1;
  505. } = 76;
  506. .DE
  507. Running 
  508. .I rpcgen 
  509. on 
  510. .I dir.x 
  511. creates four output files. Three are the same
  512. as before: header file, client stub routines  and server skeleton. The
  513. fourth are the XDR routines necessary for converting the data types we
  514. declared into XDR format and vice-versa. These are  output in the file
  515. .I dir_xdr.c .
  516. .LP
  517. Here is the implementation of the "READDIR" procedure:
  518. .ie t .DS
  519. .el .DS L
  520. .vs 11
  521. .ft I
  522. /*
  523.  * dir_proc.c: remote readdir implementation
  524.  */
  525. .ft CW
  526. #include <rpc/rpc.h>
  527. #include <sys/dir.h>
  528. #include "dir.h"
  529.  
  530. extern int errno;
  531. extern char *malloc();
  532. extern char *strdup();
  533.  
  534. readdir_res *
  535. readdir_1(dirname)
  536.     nametype *dirname;
  537. {
  538.     DIR *dirp;
  539.     struct direct *d;
  540.     namelist nl;
  541.     namelist *nlp;
  542.     static readdir_res res; /* \fImust be static\fP! */
  543.     
  544. .ft I
  545.     /*
  546.      * Open directory
  547.      */
  548. .ft CW
  549.     dirp = opendir(*dirname);
  550.     if (dirp == NULL) {
  551.         res.errno = errno;
  552.         return (&res);
  553.     }
  554.  
  555. .ft I
  556.     /*
  557.      * Free previous result
  558.      */
  559. .ft CW
  560.     xdr_free(xdr_readdir_res, &res);
  561.  
  562. .ft I
  563.     /*
  564.      * Collect directory entries
  565.      */
  566. .ft CW
  567.     nlp = &res.readdir_res_u.list;
  568.     while (d = readdir(dirp)) {
  569.         nl = *nlp = (namenode *) malloc(sizeof(namenode));
  570.         nl->name = strdup(d->d_name);
  571.         nlp = &nl->next;
  572.     }
  573.     *nlp = NULL;
  574.  
  575. .ft I
  576.     /*
  577.      * Return the result
  578.      */
  579. .ft CW
  580.     res.errno = 0;
  581.     closedir(dirp);
  582.     return (&res);
  583. }
  584. .vs
  585. .DE
  586. Finally, there is the client side program to call the server:
  587. .ie t .DS
  588. .el .DS L
  589. .ft I
  590. /*
  591.  * rls.c: Remote directory listing client
  592.  */
  593. .ft CW
  594. #include <stdio.h>
  595. #include <rpc/rpc.h>    /* \fIalways need this\fP */
  596. #include "dir.h"        /* \fIneed this too: will be generated by rpcgen\fP */
  597.  
  598. extern int errno;
  599.  
  600. main(argc, argv)
  601.     int argc;
  602.     char *argv[];
  603. {
  604.     CLIENT *cl;
  605.     char *server;
  606.     char *dir;
  607.     readdir_res *result;
  608.     namelist nl;
  609.     
  610.  
  611.     if (argc != 3) {
  612.         fprintf(stderr, "usage: %s host directory\en", argv[0]);
  613.         exit(1);
  614.     }
  615.  
  616. .ft I
  617.     /*
  618.      * Remember what our command line arguments refer to
  619.      */
  620. .ft CW
  621.     server = argv[1];
  622.     dir = argv[2];
  623.  
  624. .ft I
  625.     /*
  626.      * Create client "handle" used for calling \fIMESSAGEPROG\fP on the
  627.      * server designated on the command line. We tell the RPC package
  628.      * to use the "tcp" protocol when contacting the server.
  629.      */
  630. .ft CW
  631.     cl = clnt_create(server, DIRPROG, DIRVERS, "tcp");
  632.     if (cl == NULL) {
  633. .ft I
  634.         /*
  635.          * Couldn't establish connection with server.
  636.          * Print error message and die.
  637.          */
  638. .ft CW
  639.         clnt_pcreateerror(server);
  640.         exit(1);
  641.     }
  642.     
  643. .ft I
  644.     /*
  645.      * Call the remote procedure \fIreaddir\fP on the server
  646.      */
  647. .ft CW
  648.     result = readdir_1(&dir, cl);
  649.     if (result == NULL) {
  650. .ft I
  651.         /*
  652.          * An error occurred while calling the server. 
  653.           * Print error message and die.
  654.          */
  655. .ft CW
  656.         clnt_perror(cl, server);
  657.         exit(1);
  658.     }
  659.  
  660. .ft I
  661.     /*
  662.      * Okay, we successfully called the remote procedure.
  663.      */
  664. .ft CW
  665.     if (result->errno != 0) {
  666. .ft I
  667.         /*
  668.          * A remote system error occurred.
  669.          * Print error message and die.
  670.          */
  671. .ft CW
  672.         errno = result->errno;
  673.         perror(dir);
  674.         exit(1);
  675.     }
  676.  
  677. .ft I
  678.     /*
  679.      * Successfully got a directory listing.
  680.      * Print it out.
  681.      */
  682. .ft CW
  683.     for (nl = result->readdir_res_u.list; nl != NULL; 
  684.       nl = nl->next) {
  685.         printf("%s\n", nl->name);
  686.     }
  687. }
  688. .DE
  689. Compile everything, and run.
  690. .DS
  691. .ft CW
  692. sun%  \fBrpcgen dir.x\fP
  693. sun%  \fBcc rls.c dir_clnt.c dir_xdr.c -o rls\fP
  694. sun%  \fBcc dir_svc.c dir_proc.c dir_xdr.c -o dir_svc\fP
  695.  
  696. sun%  \fBdir_svc &\fP
  697.  
  698. moon%  \fBrls sun /usr/pub\fP
  699. \&.
  700. \&..
  701. ascii
  702. eqnchar
  703. greek
  704. kbd
  705. marg8
  706. tabclr
  707. tabs
  708. tabs4
  709. moon%
  710. .DE
  711. .LP
  712. .IX debugging rpcgen "" \fIrpcgen\fP
  713. A final note about 
  714. .I rpcgen :
  715. The client program and the server procedure can be tested together 
  716. as a single program by simply linking them with each other rather 
  717. than with the client and server stubs.  The procedure calls will be
  718. executed as ordinary local procedure calls and the program can be 
  719. debugged with a local debugger such as 
  720. .I dbx .
  721. When the program is working, the client program can be linked to 
  722. the client stub produced by 
  723. .I rpcgen 
  724. and the server procedures can be linked to the server stub produced
  725. by 
  726. .I rpcgen .
  727. .SH
  728. .I NOTE :
  729. \fIIf you do this, you may want to comment out calls to RPC library
  730. routines, and have client-side routines call server routines
  731. directly.\fP
  732. .LP
  733. .NH 1
  734. \&The C-Preprocessor
  735. .IX rpcgen "C-preprocessor" \fIrpcgen\fP
  736. .LP
  737. The C-preprocessor is  run on all input  files before they are
  738. compiled, so all the preprocessor  directives are legal within a  ".x"
  739. file. Four symbols may be defined, depending upon which output file is
  740. getting generated. The symbols are:
  741. .TS
  742. box tab (&);
  743. lfI lfI
  744. lfL l .
  745. Symbol&Usage
  746. _
  747. RPC_HDR&for header-file output
  748. RPC_XDR&for XDR routine output
  749. RPC_SVC&for server-skeleton output
  750. RPC_CLNT&for client stub output
  751. .TE
  752. .LP
  753. Also, 
  754. .I rpcgen 
  755. does  a little preprocessing   of its own. Any  line that
  756. begins  with  a percent sign is passed  directly into the output file,
  757. without any interpretation of the line.  Here is a simple example that
  758. demonstrates the preprocessing features.
  759. .ie t .DS
  760. .el .DS L
  761. .ft I
  762. /*
  763.  * time.x: Remote time protocol
  764.  */
  765. .ft CW
  766. program TIMEPROG {
  767.         version TIMEVERS {
  768.                 unsigned int TIMEGET(void) = 1;
  769.         } = 1;
  770. } = 44;
  771.  
  772. #ifdef RPC_SVC
  773. %int *
  774. %timeget_1()
  775. %{
  776. %        static int thetime;
  777. %
  778. %        thetime = time(0);
  779. %        return (&thetime);
  780. %}
  781. #endif
  782. .DE
  783. The '%' feature is not generally recommended, as there is no guarantee
  784. that the compiler will stick the output where you intended.
  785. .NH 1
  786. \&RPC Language
  787. .IX RPCL
  788. .IX rpcgen "RPC Language" \fIrpcgen\fP
  789. .LP
  790. RPC language is an extension of XDR  language.   The sole extension is
  791. the addition of the
  792. .I program 
  793. type.  For a complete description of the XDR language syntax, see the
  794. \fIeXternal Data Representation Standard: Protocol Specification\fP
  795. chapter.  For a description of the RPC extensions to the XDR language,
  796. see the
  797. \fIRemote Procedure Calls: Protocol Specification\fP
  798. chapter.
  799. .LP
  800. However, XDR language is so close to C that if you know C, you know most
  801. of it already.  We describe here  the syntax of the RPC language,
  802. showing a  few examples along the way.   We also show how  the various
  803. RPC and XDR type definitions get  compiled into C  type definitions in
  804. the output header file.
  805. .KS
  806. .NH 2
  807. \&Definitions
  808. .IX rpcgen definitions \fIrpcgen\fP
  809. .LP
  810. An RPC language file consists of a series of definitions.
  811. .DS L
  812. .ft CW
  813.     definition-list:
  814.         definition ";"
  815.         definition ";" definition-list
  816. .DE
  817. .KE
  818. It recognizes five types of definitions. 
  819. .DS L
  820. .ft CW
  821.     definition:
  822.         enum-definition
  823.         struct-definition
  824.         union-definition
  825.         typedef-definition
  826.         const-definition
  827.         program-definition
  828. .DE
  829. .NH 2
  830. \&Structures
  831. .IX rpcgen structures \fIrpcgen\fP
  832. .LP
  833. An XDR struct  is declared almost exactly like  its C counterpart.  It
  834. looks like the following:
  835. .DS L
  836. .ft CW
  837.     struct-definition:
  838.         "struct" struct-ident "{"
  839.             declaration-list
  840.         "}"
  841.  
  842.     declaration-list:
  843.         declaration ";"
  844.         declaration ";" declaration-list
  845. .DE
  846. As  an example, here is an  XDR structure to define  a two-dimensional
  847. coordinate, and the C structure  that it  gets compiled into  in the
  848. output header file.
  849. .DS
  850. .ft CW
  851.    struct coord {             struct coord {
  852.         int x;       -->           int x;
  853.         int y;                     int y;
  854.    };                         };
  855.                               typedef struct coord coord;
  856. .DE
  857. The output is identical to the  input, except  for the added
  858. .I typedef
  859. at the end of  the output. This allows  one to use "coord" instead  of
  860. "struct coord" when declaring items.
  861. .NH 2
  862. \&Unions
  863. .IX rpcgen unions \fIrpcgen\fP
  864. .LP
  865. DR unions are discriminated unions, and look quite different from C
  866. unions. They are more analogous to  Pascal variant records than they
  867. are to C unions.
  868. .DS L
  869. .ft CW
  870.     union-definition:
  871.         "union" union-ident "switch" "(" declaration ")" "{"
  872.             case-list
  873.         "}"
  874.  
  875.     case-list:
  876.         "case" value ":" declaration ";"
  877.         "default" ":" declaration ";"
  878.         "case" value ":" declaration ";" case-list
  879. .DE
  880. Here is an example of a type that might be returned as the result of a
  881. "read data" operation.  If there is no  error, return a block of data.
  882. Otherwise, don't return anything.
  883. .DS L
  884. .ft CW
  885.     union read_result switch (int errno) {
  886.     case 0:
  887.         opaque data[1024];
  888.     default:
  889.         void;
  890.     };
  891. .DE
  892. It gets compiled into the following:
  893. .DS L
  894. .ft CW
  895.     struct read_result {
  896.         int errno;
  897.         union {
  898.             char data[1024];
  899.         } read_result_u;
  900.     };
  901.     typedef struct read_result read_result;
  902. .DE
  903. Notice that the union component of the  output struct  has the name as
  904. the type name, except for the trailing "_u".
  905. .NH 2
  906. \&Enumerations
  907. .IX rpcgen enumerations \fIrpcgen\fP
  908. .LP
  909. DR enumerations have the same syntax as C enumerations.
  910. .DS L
  911. .ft CW
  912.     enum-definition:
  913.         "enum" enum-ident "{"
  914.             enum-value-list
  915.         "}"
  916.  
  917.     enum-value-list:
  918.         enum-value
  919.         enum-value "," enum-value-list
  920.  
  921.     enum-value:
  922.         enum-value-ident 
  923.         enum-value-ident "=" value
  924. .DE
  925. Here is a short example of  an XDR enum,  and the C enum that  it gets
  926. compiled into.
  927. .DS L
  928. .ft CW
  929.      enum colortype {      enum colortype {
  930.           RED = 0,              RED = 0,
  931.           GREEN = 1,   -->      GREEN = 1,
  932.           BLUE = 2              BLUE = 2,
  933.      };                    };
  934.                            typedef enum colortype colortype;
  935. .DE
  936. .NH 2
  937. \&Typedef
  938. .IX rpcgen typedef \fIrpcgen\fP
  939. .LP
  940. DR typedefs have the same syntax as C typedefs.
  941. .DS L
  942. .ft CW
  943.     typedef-definition:
  944.         "typedef" declaration
  945. .DE
  946. Here  is an example  that defines a  
  947. .I fname_type 
  948. used  for declaring
  949. file name strings that have a maximum length of 255 characters.
  950. .DS L
  951. .ft CW
  952.     typedef string fname_type<255>; --> typedef char *fname_type;
  953. .DE
  954. .NH 2
  955. \&Constants
  956. .IX rpcgen constants \fIrpcgen\fP
  957. .LP
  958. DR constants  symbolic constants  that may be  used wherever  a
  959. integer constant is used, for example, in array size specifications.
  960. .DS L
  961. .ft CW
  962.     const-definition:
  963.         "const" const-ident "=" integer
  964. .DE
  965. For example, the following defines a constant
  966. .I DOZEN 
  967. equal to 12.
  968. .DS L
  969. .ft CW
  970.     const DOZEN = 12;  -->  #define DOZEN 12
  971. .DE
  972. .NH 2
  973. \&Programs
  974. .IX rpcgen programs \fIrpcgen\fP
  975. .LP
  976. RPC programs are declared using the following syntax:
  977. .DS L
  978. .ft CW
  979.     program-definition:
  980.         "program" program-ident "{" 
  981.             version-list
  982.         "}" "=" value 
  983.  
  984.     version-list:
  985.         version ";"
  986.         version ";" version-list
  987.  
  988.     version:
  989.         "version" version-ident "{"
  990.             procedure-list 
  991.         "}" "=" value
  992.  
  993.     procedure-list:
  994.         procedure ";"
  995.         procedure ";" procedure-list
  996.  
  997.     procedure:
  998.         type-ident procedure-ident "(" type-ident ")" "=" value
  999. .DE
  1000. For example, here is the time protocol, revisited:
  1001. .ie t .DS
  1002. .el .DS L
  1003. .ft I
  1004. /*
  1005.  * time.x: Get or set the time. Time is represented as number of seconds
  1006.  * since 0:00, January 1, 1970.
  1007.  */
  1008. .ft CW
  1009. program TIMEPROG {
  1010.     version TIMEVERS {
  1011.         unsigned int TIMEGET(void) = 1;
  1012.         void TIMESET(unsigned) = 2;
  1013.     } = 1;
  1014. } = 44;        
  1015. .DE
  1016. This file compiles into #defines in the output header file:
  1017. .ie t .DS
  1018. .el .DS L
  1019. .ft CW
  1020. #define TIMEPROG 44
  1021. #define TIMEVERS 1
  1022. #define TIMEGET 1
  1023. #define TIMESET 2
  1024. .DE
  1025. .NH 2
  1026. \&Declarations
  1027. .IX rpcgen declarations \fIrpcgen\fP
  1028. .LP
  1029. In XDR, there are only four kinds of declarations.  
  1030. .DS L
  1031. .ft CW
  1032.     declaration:
  1033.         simple-declaration
  1034.         fixed-array-declaration
  1035.         variable-array-declaration
  1036.         pointer-declaration
  1037. .DE
  1038. \fB1) Simple declarations\fP are just like simple C declarations.
  1039. .DS L
  1040. .ft CW
  1041.     simple-declaration:
  1042.         type-ident variable-ident
  1043. .DE
  1044. Example:
  1045. .DS L
  1046. .ft CW
  1047.     colortype color;    --> colortype color;
  1048. .DE
  1049. \fB2) Fixed-length Array Declarations\fP are just like C array declarations:
  1050. .DS L
  1051. .ft CW
  1052.     fixed-array-declaration:
  1053.         type-ident variable-ident "[" value "]"
  1054. .DE
  1055. Example:
  1056. .DS L
  1057. .ft CW
  1058.     colortype palette[8];    --> colortype palette[8];
  1059. .DE
  1060. \fB3) Variable-Length Array Declarations\fP have no explicit syntax 
  1061. in C, so XDR invents its own using angle-brackets.
  1062. .DS L
  1063. .ft CW
  1064. variable-array-declaration:
  1065.     type-ident variable-ident "<" value ">"
  1066.     type-ident variable-ident "<" ">"
  1067. .DE
  1068. The maximum size is specified between the angle brackets. The size may
  1069. be omitted, indicating that the array may be of any size.
  1070. .DS L
  1071. .ft CW
  1072.     int heights<12>;    /* \fIat most 12 items\fP */
  1073.     int widths<>;       /* \fIany number of items\fP */
  1074. .DE
  1075. Since  variable-length  arrays have no  explicit  syntax in  C,  these
  1076. declarations are actually  compiled into "struct"s.  For example,  the
  1077. "heights" declaration gets compiled into the following struct:
  1078. .DS L
  1079. .ft CW
  1080.     struct {
  1081.         u_int heights_len;  /* \fI# of items in array\fP */
  1082.         int *heights_val;   /* \fIpointer to array\fP */
  1083.     } heights;
  1084. .DE
  1085. Note  that the number  of items in  the array is stored in  the "_len"
  1086. component  and the  pointer  to   the array is  stored  in  the "_val"
  1087. component. The first part of each of these component's names is the
  1088. same as the name of the declared XDR variable.
  1089. .LP
  1090. \fB4) Pointer Declarations\fP are made in 
  1091. DR  exactly as they  are  in C.  You  can't
  1092. really send pointers over the network,  but  you  can use XDR pointers
  1093. for sending recursive data types such as lists and trees.  The type is
  1094. actually called "optional-data", not "pointer", in XDR language.
  1095. .DS L
  1096. .ft CW
  1097.     pointer-declaration:
  1098.         type-ident "*" variable-ident
  1099. .DE
  1100. Example:
  1101. .DS L
  1102. .ft CW
  1103.     listitem *next;  -->  listitem *next;
  1104. .DE
  1105. .NH 2
  1106. \&Special Cases
  1107. .IX rpcgen "special cases" \fIrpcgen\fP
  1108. .LP
  1109. There are a few exceptions to the rules described above.
  1110. .LP
  1111. .B Booleans:
  1112. C has no built-in boolean type. However, the RPC library does  a
  1113. boolean type   called 
  1114. .I bool_t 
  1115. that   is either  
  1116. .I TRUE 
  1117. or  
  1118. .I FALSE .
  1119. Things declared as  type 
  1120. .I bool 
  1121. in  XDR language  are  compiled  into
  1122. .I bool_t 
  1123. in the output header file.
  1124. .LP
  1125. Example:
  1126. .DS L
  1127. .ft CW
  1128.     bool married;  -->  bool_t married;
  1129. .DE
  1130. .B Strings:
  1131. C has  no built-in string  type, but  instead uses the null-terminated
  1132. "char *" convention.  In XDR  language, strings are declared using the
  1133. "string" keyword,  and  compiled into "char  *"s in the  output header
  1134. file. The  maximum size contained  in the angle brackets specifies the
  1135. maximum number of characters allowed in the  strings (not counting the
  1136. .I NULL 
  1137. character). The maximum size may be left off, indicating a string
  1138. of arbitrary length.
  1139. .LP
  1140. Examples:
  1141. .DS L
  1142. .ft CW
  1143.     string name<32>;    -->  char *name;
  1144.     string longname<>;  -->  char *longname;
  1145. .DE
  1146. .B "Opaque  Data:"
  1147. Opaque data is used in RPC and XDR to describe untyped  data, that is,
  1148. just  sequences of arbitrary  bytes.  It may be  declared  either as a
  1149. fixed or variable length array.
  1150. .DS L
  1151. Examples:
  1152. .ft CW
  1153.     opaque diskblock[512];  -->  char diskblock[512];
  1154.  
  1155.     opaque filedata<1024>;  -->  struct {
  1156.                                     u_int filedata_len;
  1157.                                     char *filedata_val;
  1158.                                  } filedata;
  1159. .DE
  1160. .B Voids:
  1161. In a void declaration, the variable is  not named.  The declaration is
  1162. just "void" and nothing else.  Void declarations can only occur in two
  1163. places: union definitions and program definitions (as the  argument or
  1164. result of a remote procedure).
  1165.