[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] factorization would be nice



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?


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]