Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
B.  RPC Protocol and Language Specification RPC Language Specification RPCL Enumerations  Previous   Contents   Next 
   
 

RPCL Constants

You can use XDR symbolic constants wherever an integer constant is used. A typical use might be in array size specifications:

   const-definition:
   const const-ident = integer 

The following example defines a constant, DOZEN, as equal to 12.

const DOZEN = 12; --> #define DOZEN 12 

RPCL Type Definitions

XDR typedefs have the same syntax as C typedefs.

typedef-definition:
      typedef declaration 

This example defines an fname_type used for declaring file-name strings that have a maximum length of 255 characters.

typedef string fname_type<255>; --> typedef char *fname_type;

RPCL Declarations

XDR has four kinds of declarations. These declarations must be a part of a struct or a typedef. They cannot stand alone.

declaration:
      simple-declaration
 		fixed-array-declaration
 		variable-array-declaration
 		pointer-declaration 

RPCL Simple Declarations

Simple declarations are just like simple C declarations.

simple-declaration:
   type-ident variable-ident 

Example:

   colortype color; --> colortype color;

RPCL Fixed-Length Array Declarations

Fixed-length array declarations are just like C array declarations.

fixed-array-declaration:
      type-ident variable-ident [value] 

Example:

colortype palette[8]; --> colortype palette[8];

Many programmers confuse variable declarations with type declarations. Note that rpcgen does not support variable declarations. The following example is a program that does not compile.

int data[10];
program P {
   version V {
      int PROC(data) = 1;
 	} = 1;
} = 0x200000;

The previous example does not compile because of the variable declaration:

int data[10]

Instead use:

typedef int data[10];

or

struct data {int dummy [10]};

RPCL Variable-Length Array Declarations

Variable-length array declarations have no explicit syntax in C. The XDR language does have a syntax, using angle brackets:

variable-array-declaration:
      type-ident variable-ident <value>
      type-ident variable-ident < > 

The maximum size is specified between the angle brackets. You can omit the size, indicating that the array can be of any size.

int heights<12>; /* at most 12 items */
int widths<>; /* any number of items */

Because variable-length arrays have no explicit syntax in C, these declarations are compiled into struct declarations. An example is the heights declaration compiled into the following struct.

struct {
   u_int heights_len;               /* # of items in array */
 	int *heights_val;                /* pointer to array */
} heights;

The number of items in the array is stored in the _len component and the pointer to the array is stored in the _val component. The first part of each component name is the same as the name of the declared XDR variable, heights.

RPCL Pointer Declarations

Pointer declarations are made in XDR exactly as they are in C. Address pointers are not really sent over the network. Instead, XDR pointers are useful for sending recursive data types such as lists and trees. The type is called optional-data, not pointer, in XDR language.

pointer-declaration:
 	type-ident *variable-ident 

Example:

listitem *next; --> listitem *next;

RPCL Structures

An RPC/XDR struct is declared almost exactly like its C counterpart. It looks like the following.

struct-definition:
 	struct struct-ident "{"
      declaration-list
 	"}" 
declaration-list:
 	declaration ";"
 	declaration ";" declaration-list

The following XDR structure is an example of a 2-D coordinate and the C structure that it compiles into.

struct coord {                 struct coord {
 	int x;            -->           int x;
 	int y;                          int y;
};                             };
                               typedef struct coord coord;

The output is identical to the input, except for the added typedef at the end of the output. This typedef enables you to use coord instead of struct coord when declaring items.

RPCL Unions

XDR unions are discriminated unions, and do not look like C unions. They are more similar to Pascal variant records.

union-definition:
	"union" union-ident "switch" "("simple declaration")" "{"
 		case-list
 	"}" 
case-list:
 	"case" value ":" declaration ";"
 	"case" value ":" declaration ";" case-list
 	"default" ":" declaration ";" 

The following example is of a type returned as the result of a "read data" operation: if no error occurs, return a block of data. Otherwise, don't return anything.

union read_result switch (int errno) {
 	case 0:
 		opaque data[1024];
 	default:
 		void;
 	};

This union compiles into the following:

struct read_result {
 	int errno;
 	union {
 		char data[1024];
 	} read_result_u;
};
typedef struct read_result read_result;

Notice that the union component of the output struct has the same name as the type name, except for the trailing _u.

 
 
 
  Previous   Contents   Next