rxgen: Don't use size_t in struct rx_opaque with XDR
[openafs.git] / src / rxgen / rpc_cout.c
index 66599be..a15b627 100644 (file)
@@ -6,47 +6,37 @@
  * may copy or modify Sun RPC without charge, but are not authorized
  * to license or distribute it to anyone else except as part of a product or
  * program developed by the user.
- * 
+ *
  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- * 
+ *
  * Sun RPC is provided with no support and without any obligation on the
  * part of Sun Microsystems, Inc. to assist in its use, correction,
  * modification or enhancement.
- * 
+ *
  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  * OR ANY PART THEREOF.
- * 
+ *
  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  * or profits or other special, indirect and consequential damages, even if
  * Sun has been advised of the possibility of such damages.
- * 
+ *
  * Sun Microsystems, Inc.
  * 2550 Garcia Avenue
  * Mountain View, California  94043
  */
 
 /*
- * rpc_cout.c, XDR routine outputter for the RPC protocol compiler 
- * Copyright (C) 1987, Sun Microsystems, Inc. 
+ * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
+ * Copyright (C) 1987, Sun Microsystems, Inc.
  */
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
-#include <stdio.h>
-#include <stdlib.h>
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
 #include "rpc_scan.h"
 #include "rpc_parse.h"
 #include "rpc_util.h"
@@ -58,6 +48,7 @@ static void print_header(definition * def);
 static void print_trailer(void);
 static void print_ifopen(int indent, char *name);
 static void print_ifarg(char *arg);
+static void print_ifarg_with_cast(int ptr_to, char *type, char *arg);
 static void print_ifsizeof(char *prefix, char *type);
 static void print_ifclose(int indent);
 static void space(void);
@@ -76,12 +67,12 @@ static void print_rxifsizeof(char *prefix, char *type);
 
 
 /*
- * Emit the C-routine for the given definition 
+ * Emit the C-routine for the given definition
  */
 void
 emit(definition * def)
 {
-    if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
+    if (def->def_kind == DEF_CONST) {
        return;
     }
     print_header(def);
@@ -98,6 +89,8 @@ emit(definition * def)
     case DEF_TYPEDEF:
        emit_typedef(def);
        break;
+    default:
+       break;
     }
     print_trailer();
 }
@@ -105,7 +98,7 @@ emit(definition * def)
 static int
 findtype(definition * def, char *type)
 {
-    if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
+    if (def->def_kind == DEF_CONST) {
        return (0);
     } else {
        return (streq(def->def_name, type));
@@ -166,6 +159,16 @@ print_ifarg(char *arg)
 
 
 static void
+print_ifarg_with_cast(int ptr_to, char *type, char *arg)
+{
+    if (streq(type, "bool")) {
+       f_print(fout, ptr_to ? ", (bool_t *) %s" : ", (bool_t) %s", arg);
+    } else {
+       f_print(fout, ptr_to ? ", (%s *) %s" : ", (%s) %s", type, arg);
+    }
+}
+
+static void
 print_ifsizeof(char *prefix, char *type)
 {
     if (streq(type, "bool")) {
@@ -196,16 +199,52 @@ space(void)
 }
 
 static void
+print_ifarg_val(char *objname, char *name)
+{
+    if (*objname == '&') {
+       if (brief_flag) {
+           f_print(fout, "%s.val", objname);
+       } else {
+           f_print(fout, "%s.%s_val", objname, name);
+       }
+    } else {
+       if (brief_flag) {
+           f_print(fout, "&%s->val", objname);
+       } else {
+           f_print(fout, "&%s->%s_val", objname, name);
+       }
+    }
+}
+
+static void
+print_ifarg_len(char *objname, char *name)
+{
+    if (*objname == '&') {
+       if (brief_flag) {
+           f_print(fout, "(u_int *)%s.len", objname);
+       } else {
+           f_print(fout, "(u_int *)%s.%s_len", objname, name);
+       }
+    } else {
+       if (brief_flag) {
+           f_print(fout, "(u_int *)&%s->len", objname);
+       } else {
+           f_print(fout, "(u_int *)&%s->%s_len", objname, name);
+       }
+    }
+}
+
+static void
 print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
             char *objname, char *name)
 {
     char *alt = NULL;
+    char *altcast = NULL;
 
     switch (rel) {
     case REL_POINTER:
        print_ifopen(indent, "pointer");
-       print_ifarg("(char **)");
-       f_print(fout, "%s", objname);
+       print_ifarg_with_cast(1, "char *", objname);
        print_ifsizeof(prefix, type);
        break;
     case REL_VECTOR:
@@ -213,14 +252,18 @@ print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
            alt = "string";
        } else if (streq(type, "opaque")) {
            alt = "opaque";
+            altcast = "caddr_t";
        }
        if (alt) {
            print_ifopen(indent, alt);
-           print_ifarg(objname);
+            if (altcast) {
+                print_ifarg_with_cast(0, altcast, objname);
+            } else {
+                print_ifarg(objname);
+            }
        } else {
            print_ifopen(indent, "vector");
-           print_ifarg("(char *)");
-           f_print(fout, "%s", objname);
+           print_ifarg_with_cast(1, "char", objname);
        }
        print_ifarg(amax);
        if (!alt) {
@@ -232,6 +275,14 @@ print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
            alt = "string";
        } else if (streq(type, "opaque")) {
            alt = "bytes";
+           tabify(fout, indent);
+           f_print(fout, "{\n");
+           ++indent;
+           tabify(fout, indent);
+           f_print(fout, "u_int __len = (u_int) ");
+           f_print(fout, "*(");
+           print_ifarg_len(objname, name);
+           f_print(fout, ");\n");
        }
        if (streq(type, "string")) {
            print_ifopen(indent, alt);
@@ -239,16 +290,17 @@ print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
        } else {
            if (alt) {
                print_ifopen(indent, alt);
+                print_ifarg("(char **)");
            } else {
                print_ifopen(indent, "array");
+                print_ifarg("(caddr_t *)");
            }
-           print_ifarg("(char **)");
-           if (*objname == '&') {
-               f_print(fout, "%s.%s_val, (u_int *)%s.%s_len", objname, name,
-                       objname, name);
+           print_ifarg_val(objname, name);
+           f_print(fout, ", ");
+           if (streq(type, "opaque")) {
+               f_print(fout, "&__len");
            } else {
-               f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len", objname,
-                       name, objname, name);
+               print_ifarg_len(objname, name);
            }
        }
        print_ifarg(amax);
@@ -262,6 +314,17 @@ print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
        break;
     }
     print_ifclose(indent);
+    if (rel == REL_ARRAY && streq(type, "opaque")) {
+       tabify(fout, indent);
+       f_print(fout, "*(");
+       print_ifarg_len(objname, name);
+       f_print(fout, ")");
+       f_print(fout, " = __len;\n");
+       --indent;
+       tabify(fout, indent);
+       f_print(fout, "}\n");
+    }
+
 }
 
 
@@ -282,6 +345,7 @@ emit_union(definition * def)
     declaration *cs;
     char *object;
     char *format = "&objp->%s_u.%s";
+    char *briefformat = "&objp->u.%s";
 
     print_stat(&def->def.un.enum_decl);
     f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
@@ -292,7 +356,12 @@ emit_union(definition * def)
            object =
                alloc(strlen(def->def_name) + strlen(format) +
                      strlen(cs->name) + 1);
-           s_print(object, format, def->def_name, cs->name);
+
+           if (brief_flag)
+               s_print(object, briefformat, cs->name);
+           else
+               s_print(object, format, def->def_name, cs->name);
+
            print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
                         object, cs->name);
            free(object);
@@ -380,10 +449,19 @@ print_hout(declaration * dec)
        switch (dec->rel) {
        case REL_ARRAY:
            f_print(fout, "struct %s {\n", dec->name);
-           f_print(fout, "\tu_int %s_len;\n", dec->name);
-           f_print(fout, "\t%s%s *%s_val;\n", prefix, dec->type, dec->name);
+           if (brief_flag) {
+               f_print(fout, "\tu_int %s_len;\n", dec->name);
+               f_print(fout, "\t%s%s *%s_val;\n", prefix,
+                       dec->type, dec->name);
+           } else {
+               f_print(fout, "\tu_int %s_len;\n", dec->name);
+               f_print(fout, "\t%s%s *%s_val;\n", prefix,
+                       dec->type, dec->name);
+           }
            f_print(fout, "} %s", dec->name);
            break;
+       default:
+           break;
        }
        f_print(fout, ";\n");
        f_print(fout, "bool_t xdr_%s(XDR *xdrs, %s *objp);\n", dec->name,
@@ -491,16 +569,15 @@ print_param(declaration * dec)
        } else if (streq(type, "opaque")) {
            alt = "opaque";
        }
-       if (alt) {
+       if (alt)
            print_rxifopen(alt);
-           print_rxifarg(amp, objname, 0);
-       } else {
+       else
            print_rxifopen("vector");
-           print_rxifarg(amp, "(char *)", 0);
-           sprintf(temp, "%s", objname);
-           strcat(Proc_list->code, temp);
-           strcat(Proc_list->scode, temp);
-       }
+
+       print_rxifarg(amp, "(char *)", 0);
+       sprintf(temp, "%s", objname);
+       strcat(Proc_list->code, temp);
+       strcat(Proc_list->scode, temp);
        print_rxifarg("", amax, 1);
        if (!alt) {
            print_rxifsizeof(prefix, type);
@@ -519,7 +596,7 @@ print_param(declaration * dec)
                Proc_list->pl.param_flag |= OUT_STRING;
                print_rxifarg("", objname, 0);
            } else
-               print_rxifarg("&", objname, 0);
+               print_rxifarg("(char **) &", objname, 0);
 /*                     print_rxifarg(amp, objname, 0); */
            print_rxifarg("", amax, 1);
            if (!alt) {