Header And Logo

PostgreSQL
| The world's most advanced open source database.

uri-regress.c

Go to the documentation of this file.
00001 /*
00002  * uri-regress.c
00003  *      A test program for libpq URI format
00004  *
00005  * This is a helper for libpq conninfo regression testing.  It takes a single
00006  * conninfo string as a parameter, parses it using PQconninfoParse, and then
00007  * prints out the values from the parsed PQconninfoOption struct that differ
00008  * from the defaults (obtained from PQconndefaults).
00009  *
00010  * Portions Copyright (c) 2012-2013, PostgreSQL Global Development Group
00011  *
00012  * IDENTIFICATION
00013  *      src/interfaces/libpq/test/uri-regress.c
00014  */
00015 
00016 #include "postgres_fe.h"
00017 
00018 #include "libpq-fe.h"
00019 
00020 int
00021 main(int argc, char *argv[])
00022 {
00023     PQconninfoOption *opts;
00024     PQconninfoOption *defs;
00025     PQconninfoOption *opt;
00026     PQconninfoOption *def;
00027     char       *errmsg = NULL;
00028     bool        local = true;
00029 
00030     if (argc != 2)
00031         return 1;
00032 
00033     opts = PQconninfoParse(argv[1], &errmsg);
00034     if (opts == NULL)
00035     {
00036         fprintf(stderr, "uri-regress: %s\n", errmsg);
00037         return 1;
00038     }
00039 
00040     defs = PQconndefaults();
00041     if (defs == NULL)
00042     {
00043         fprintf(stderr, "uri-regress: cannot fetch default options\n");
00044         return 1;
00045     }
00046 
00047     /*
00048      * Loop on the options, and print the value of each if not the default.
00049      *
00050      * XXX this coding assumes that PQconninfoOption structs always have the
00051      * keywords in the same order.
00052      */
00053     for (opt = opts, def = defs; opt->keyword; ++opt, ++def)
00054     {
00055         if (opt->val != NULL)
00056         {
00057             if (def->val == NULL || strcmp(opt->val, def->val) != 0)
00058                 printf("%s='%s' ", opt->keyword, opt->val);
00059 
00060             /*
00061              * Try to detect if this is a Unix-domain socket or inet.  This is
00062              * a bit grotty but it's the same thing that libpq itself does.
00063              *
00064              * Note that we directly test for '/' instead of using
00065              * is_absolute_path, as that would be considerably more messy.
00066              * This would fail on Windows, but that platform doesn't have
00067              * Unix-domain sockets anyway.
00068              */
00069             if (*opt->val &&
00070                 (strcmp(opt->keyword, "hostaddr") == 0 ||
00071                  (strcmp(opt->keyword, "host") == 0 && *opt->val != '/')))
00072             {
00073                 local = false;
00074             }
00075         }
00076     }
00077 
00078     if (local)
00079         printf("(local)\n");
00080     else
00081         printf("(inet)\n");
00082 
00083     return 0;
00084 }