On Wed, Aug 12, 2009 at 10:07:16AM +0200, Jim Meyering
wrote:> Hi Rich,
>
> I've changed part of generator.ml to look like this:
>
> (match snd style with
> | [] -> ()
> | args ->
> pr " memset (&args, 0, sizeof args);\n";
> pr "\n";
> pr " if (!xdr_guestfs_%s_args (xdr_in, &args))
{\n" name;
> pr " reply_with_error (\"%%s: daemon failed to
decode procedure arguments\", \"%s\");\n" name;
> pr " return;\n";
> pr " }\n";
> List.iter (
> function
> | Pathname n ->
> pr " char *%s = args.%s;\n" n n;
> pr " ABS_PATH (%s, goto done);\n" n;
> | Device n ->
> pr " char *%s = args.%s;\n" n n;
> pr " RESOLVE_DEVICE (%s, goto done);" n;
> | Dev_or_Path n ->
> pr " char *%s = args.%s;\n" n n;
> pr " REQUIRE_ROOT_OR_RESOLVE_DEVICE (%s, goto
done);" n;
> | String n -> pr " char *%s = args.%s;\n" n n
> | OptString n -> pr " %s = args.%s ? *args.%s :
NULL;\n" n n n
> | StringList n ->
> pr " %s = realloc (args.%s.%s_val,\n" n n n;
> pr " sizeof (char *) *
(args.%s.%s_len+1));\n" n n;
> pr " if (%s == NULL) {\n" n;
> pr " reply_with_perror
(\"realloc\");\n";
> pr " goto done;\n";
> pr " }\n";
> pr " %s[args.%s.%s_len] = NULL;\n" n n n;
> pr " args.%s.%s_val = %s;\n" n n n;
> | Bool n -> pr " %s = args.%s;\n" n n
> | Int n -> pr " %s = args.%s;\n" n n
> | FileIn _ | FileOut _ -> ()
> ) args;
> pr "\n"
> );
>
> Note the 4-x repeated statement:
>
> pr " char *%s = args.%s;\n" n n;
>
> What's the best way to avoid that?
Just created a nested function:
let pr_args n pr " char *%s = args.%s;\n" n n
in
(* ... *)
pr_args n
You can create functions anywhere, at any level of nesting, even
inside expressions, and nested functions are common and a Good Thing.
Rich.
--
Richard Jones, Emerging Technologies, Red Hat http://et.redhat.com/~rjones
New in Fedora 11: Fedora Windows cross-compiler. Compile Windows
programs, test, and build Windows installers. Over 70 libraries supprt'd
http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw