pgbench: accept unambiguous builtin prefixes for -b
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 3 Mar 2016 22:37:13 +0000 (19:37 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 3 Mar 2016 22:37:13 +0000 (19:37 -0300)
This makes it easier to use "-b se" instead of typing the full "-b
select-only".

Author: Fabien Coelho
Reviewed-by: Michaƫl Paquier
doc/src/sgml/ref/pgbench.sgml
src/bin/pgbench/pgbench.c

index f39f341a269f87b76300f1ef65cbe1a84b9861e3..cc80b3fc4f5aa07c5863cfdcfd1931ece2433ff5 100644 (file)
@@ -269,6 +269,7 @@ pgbench <optional> <replaceable>options</> </optional> <replaceable>dbname</>
         Add the specified builtin script to the list of executed scripts.
         Available builtin scripts are: <literal>tpcb-like</>,
         <literal>simple-update</> and <literal>select-only</>.
+        Unambiguous prefixes of builtin names are accepted.
         With special name <literal>list</>, show the list of builtin scripts
         and exit immediately.
        </para>
index 66cfdc9af8bbd25b20d760036f0e580f543d2894..8b0b17a74ce48cad82f7be0431eb1c9af5e05284 100644 (file)
@@ -2746,22 +2746,36 @@ listAvailableScripts(void)
        fprintf(stderr, "\n");
 }
 
+/* return builtin script "name" if unambiguous */
 static char *
 findBuiltin(const char *name, char **desc)
 {
-       int                     i;
+       int                     i,
+                               found = 0,
+                               len = strlen(name);
+       char       *commands = NULL;
 
        for (i = 0; i < N_BUILTIN; i++)
        {
-               if (strncmp(builtin_script[i].name, name,
-                                       strlen(builtin_script[i].name)) == 0)
+               if (strncmp(builtin_script[i].name, name, len) == 0)
                {
                        *desc = builtin_script[i].desc;
-                       return builtin_script[i].commands;
+                       commands = builtin_script[i].commands;
+                       found++;
                }
        }
 
-       fprintf(stderr, "no builtin script found for name \"%s\"\n", name);
+       /* ok, unambiguous result */
+       if (found == 1)
+               return commands;
+
+       /* error cases */
+       if (found == 0)
+               fprintf(stderr, "no builtin script found for name \"%s\"\n", name);
+       else    /* found > 1 */
+               fprintf(stderr,
+                               "ambiguous builtin name: %d builtin scripts found for prefix \"%s\"\n", found, name);
+
        listAvailableScripts();
        exit(1);
 }