[OpenAFS-devel] [Patch] Additional capabilities for rxgen

Felix Frank Felix.Frank@Desy.de
Tue, 12 May 2009 08:40:34 +0200


This is a multi-part message in MIME format.
--------------020503000907090800070309
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

The attached patch allows rxgen to do two new things:

(1) Generate a TranslateOpcode function in *.xdr.c
     It is generated in a fashion similar to Execute_Request in *.ss.c
     and produces RPC names as strings from their numeric codes.

(2) The new -t option generates a plain opcode/name table for the given
     interface.

--------------020503000907090800070309
Content-Type: text/x-patch;
 name="translate_rpc.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="translate_rpc.patch"

Index: src/rxgen/rpc_parse.c
===================================================================
--- src/rxgen/rpc_parse.c	(revision 646)
+++ src/rxgen/rpc_parse.c	(revision 655)
@@ -924,7 +924,7 @@
 	if (Sflag || cflag)
 	    ss_Proc_CodeGeneration(defp);
     }
-    if (Sflag)
+    if (Sflag || (cflag && xflag && !proc_split_flag))
 	STOREVAL(&proc_defined[PackageIndex], defp);
 }
 
@@ -2019,6 +2019,9 @@
     list *listp;
     definition *defp;
 
+    if ( cflag )	/* not needed for opcode translation */
+	return;
+
     f_print(fout, "\n");
     for (listp = proc_defined[PackageIndex]; listp != NULL;
 	 listp = listp->next) {
@@ -2038,26 +2041,37 @@
 
     if ((listp = proc_defined[PackageIndex])) {
 	defp = (definition *) listp->val;
-	if (defp->pc.proc_serverstub) {
-	    f_print(fout, "\nstatic afs_int32 (*StubProcsArray%d[])() = {%s",
-		    PackageIndex, defp->pc.proc_serverstub);
-	} else {
-	    f_print(fout,
-		    "\nstatic afs_int32 (*StubProcsArray%d[])(struct rx_call *z_call, XDR *z_xdrs) = {_%s%s%s",
-		    PackageIndex, prefix, defp->pc.proc_prefix,
-		    ((definition *) listp->val)->pc.proc_name);
-	    defp = (definition *) listp->val;
+	if ( cflag )  {	/* generate translator functions */
+	    f_print(fout, "\nstatic char *opnames%d[] = {\"%s%s\"", 
+			PackageIndex, defp->pc.proc_prefix, defp->pc.proc_name);
 	}
+	else {		/* generate Execute_Request */
+	    if (defp->pc.proc_serverstub) {
+		f_print(fout,"\nstatic afs_int32 (*StubProcsArray%d[])() = {%s",
+			PackageIndex, defp->pc.proc_serverstub);
+	    } else {
+		f_print(fout,
+			"\nstatic afs_int32 (*StubProcsArray%d[])(struct rx_call *z_call, XDR *z_xdrs) = {_%s%s%s",
+			PackageIndex, prefix, defp->pc.proc_prefix,
+			((definition *) listp->val)->pc.proc_name);
+		defp = (definition *) listp->val;
+	    }
+	}
 	listp = listp->next;
     }
     for (; listp != NULL; listp = listp->next) {
 	defp = (definition *) listp->val;
-	if (defp->pc.proc_serverstub) {
-	    f_print(fout, ",%s", defp->pc.proc_serverstub);
-	} else {
-	    f_print(fout, ", _%s%s%s", prefix, defp->pc.proc_prefix,
-		    defp->pc.proc_name);
+	if ( cflag ) {	/* translator */
+	    f_print(fout, ", \"%s%s\"",defp->pc.proc_prefix,defp->pc.proc_name);
 	}
+	else {		/* Execute_Request */
+	    if (defp->pc.proc_serverstub) {
+		f_print(fout, ",%s", defp->pc.proc_serverstub);
+	    } else {
+		f_print(fout, ", _%s%s%s", prefix, defp->pc.proc_prefix,
+			defp->pc.proc_name);
+	    }
+	}
     }
     f_print(fout, "};\n\n");
 }
@@ -2066,6 +2080,16 @@
 static void
 er_ProcMainBody_setup(void)
 {
+    if ( cflag ) {
+	f_print(fout, "char *%sTranslateOpCode(int op)\n{\n",
+		PackagePrefix[PackageIndex]);
+	f_print(fout, "\tif (op < %sLOWEST_OPCODE || op > %sHIGHEST_OPCODE)\n"
+			"\t\treturn NULL;\n",
+		PackagePrefix[PackageIndex], PackagePrefix[PackageIndex]);
+	f_print(fout, "\treturn opnames%d[op - %sLOWEST_OPCODE];\n}\n",
+		PackageIndex, PackagePrefix[PackageIndex]);
+	return;
+    }
     f_print(fout, "int %s%sExecuteRequest(register struct rx_call *z_call)\n",
 	    prefix, PackagePrefix[PackageIndex]);
     f_print(fout, "{\n\tint op;\n");
@@ -2087,17 +2111,23 @@
 static void
 er_HeadofOldStyleProc_setup(void)
 {
-    f_print(fout,
-	    "\nint %s%sExecuteRequest (register struct rx_call *z_call)\n",
-	    prefix,
+    if ( cflag ) {
+	f_print(fout, "char *%sTranslateOpCode(int op)\n{\n",
 	    (combinepackages ? MasterPrefix : PackagePrefix[PackageIndex]));
-    f_print(fout, "{\n");
-    f_print(fout, "\tint op;\n");
-    f_print(fout, "\tXDR z_xdrs;\n");
-    f_print(fout, "\t" "afs_int32 z_result;\n\n");
-    f_print(fout, "\txdrrx_create(&z_xdrs, z_call, XDR_DECODE);\n");
-    f_print(fout, "\tz_result = RXGEN_DECODE;\n");
-    f_print(fout, "\tif (!xdr_int(&z_xdrs, &op)) goto fail;\n");
+    }
+    else {
+	f_print(fout,
+		"\nint %s%sExecuteRequest (register struct rx_call *z_call)\n",
+		prefix,
+		(combinepackages ? MasterPrefix : PackagePrefix[PackageIndex]));
+	f_print(fout, "{\n");
+	f_print(fout, "\tint op;\n");
+	f_print(fout, "\tXDR z_xdrs;\n");
+	f_print(fout, "\t" "afs_int32 z_result;\n\n");
+	f_print(fout, "\txdrrx_create(&z_xdrs, z_call, XDR_DECODE);\n");
+	f_print(fout, "\tz_result = RXGEN_DECODE;\n");
+	f_print(fout, "\tif (!xdr_int(&z_xdrs, &op)) goto fail;\n");
+    }
     f_print(fout, "\tswitch (op) {\n");
 }
 
@@ -2130,6 +2160,11 @@
     } else {
 	f_print(fout, "\t\tcase %s:\n", defp->pc.proc_opcodename);
     }
+    if ( cflag ) {
+	f_print(fout, "\t\t\treturn \"%s%s\";\n", 
+		defp->pc.proc_prefix, defp->pc.proc_name);
+	return;
+    }
     if (defp->pc.proc_serverstub) {
 	f_print(fout, "\t\t\tz_result = %s(z_call, &z_xdrs);\n",
 		defp->pc.proc_serverstub);
@@ -2145,13 +2180,16 @@
 er_TailofOldStyleProc_setup(void)
 {
     f_print(fout, "\t\tdefault:\n");
+    if ( cflag ) {
+	f_print(fout, "\t\t\treturn NULL;\n\t}\n}\n");
+	return;
+    }
     f_print(fout, "\t\t\tz_result = RXGEN_OPCODE;\n");
     f_print(fout, "\t\t\tbreak;\n\t}\n");
     f_print(fout, "fail:\n");
     f_print(fout, "\treturn z_result;\n}\n");
 }
 
-
 void
 h_opcode_stats(void)
 {
Index: src/rxgen/rpc_scan.c
===================================================================
--- src/rxgen/rpc_scan.c	(revision 646)
+++ src/rxgen/rpc_scan.c	(revision 655)
@@ -57,6 +57,7 @@
 int pushed = 0;			/* is a token pushed */
 token lasttok;			/* last token, if pushed */
 int scan_print = 1;
+int put_directives = 1;		/* set to 0 to suppress printdirective()s */
 
 /* static prototypes */
 static void findstrconst(char **str, char **val);
@@ -470,6 +471,8 @@
 void
 printdirective(char *line)
 {
+    if ( !put_directives )
+	return;
     f_print(fout, "%s", line + 1);
 }
 
Index: src/rxgen/rpc_main.c
===================================================================
--- src/rxgen/rpc_main.c	(revision 646)
+++ src/rxgen/rpc_main.c	(revision 655)
@@ -75,6 +75,7 @@
     int xflag;
     int yflag;
     int uflag;
+    int tflag;
     char *infile;
     char *outfile;
 };
@@ -123,6 +124,8 @@
 };
 #endif
 
+extern int put_directives;
+
 #include "AFS_component_version_number.c"
 
 /* static prototypes */
@@ -142,6 +145,7 @@
 		     int append);
 static void S_output(char *infile, char *define, int extend, char *outfile,
 		     int append);
+static void t_output(char *infile, int extend, char *outfile, int append);
 static char *uppercase(char *str);
 
 int
@@ -174,7 +178,7 @@
     if (!parseargs(argc, argv, &cmd)) {
 	f_print(stderr, "usage: %s infile\n", cmdname);
 	f_print(stderr,
-		"       %s [-c | -h | -l | -m | -C | -S | -r | -k | -R | -p | -d | -z | -u] [-Pprefix] [-Idir] [-o outfile] [infile]\n",
+		"       %s [-c | -h | -l | -m | -C | -S | -r | -k | -R | -p | -d | -z | -u | -t] [-Pprefix] [-Idir] [-o outfile] [infile]\n",
 		cmdname);
 	f_print(stderr, "       %s [-s udp|tcp]* [-o outfile] [infile]\n",
 		cmdname);
@@ -199,6 +203,9 @@
     } else if (cmd.Sflag) {
 	OutFileFlag = NULL;
 	S_output(cmd.infile, "-DRPC_SERVER", !EXTEND, cmd.outfile, 1);
+    } else if (cmd.tflag) {
+	OutFileFlag = NULL;
+	t_output(cmd.infile, !EXTEND, cmd.outfile, 1);
     } else {
 	if (OutFileFlag && (strrchr(OutFile, '.') == NULL))
 	    strcat(OutFile, ".");
@@ -413,9 +420,9 @@
 		    f_print(fout, ",\n\t\"%s\"", function_list[j][i]);
 		}
 	    }
-
 	    f_print(fout, "\n};\n");
 	}
+	er_Proc_CodeGeneration();
     }
 
     if (extend && tell == ftell(fout)) {
@@ -757,6 +764,47 @@
     Sflag = 0;
 }
 
+/*
+ * create a simple lookup table from interface definition
+ */
+static void
+t_output(char *infile, int extend, char *outfile, int append)
+{
+    char *include;
+    char *outfilename;
+    char fullname[1024];
+    definition *def;
+    long tell;
+    char *currfile = (OutFileFlag ? OutFile : infile);
+
+    put_directives = 0;
+
+    open_input(infile, "");
+    memset(fullname, 0, sizeof(fullname));
+    if (append) {
+	strcpy(fullname, prefix);
+	strcat(fullname, infile);
+    } else
+	strcpy(fullname, infile);
+    outfilename = extend ? extendfile(fullname, outfile) : outfile;
+    open_output(infile, outfilename);
+
+    tell = ftell(fout);
+    fflush(fout);
+    while (def = get_definition()) {
+	if ( def->def_kind != DEF_PROC )
+	    continue;
+	fflush(fout);
+	fprintf(fout, "%d\t%s\n", def->pc.proc_opcodenum, def->pc.proc_name);
+    }
+
+    if (extend && tell == ftell(fout)) {
+	(void)unlink(outfilename);
+    }
+
+    put_directives = 1;
+}
+
 static char *
 uppercase(char *str)
 {
@@ -812,6 +860,7 @@
 		case 'k':
 		case 'p':
 		case 'd':
+		case 't':
 		case 'u':
 		case 'x':
 		case 'y':
@@ -873,6 +922,7 @@
     cmd->uflag = uflag = flag['u'];
     cmd->kflag = kflag = flag['k'];
     cmd->pflag = flag['p'];
+    cmd->tflag = flag['t'];
     cmd->dflag = debug = flag['d'];
     zflag = flag['z'];
     if (cmd->pflag)

--------------020503000907090800070309--