LCOV - code coverage report
Current view: top level - src/fe_utils - option_utils.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 93.0 % 43 40
Test Date: 2026-07-16 06:15:47 Functions: 100.0 % 4 4
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 86.1 % 36 31

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * Command line option processing facilities for frontend code
       4                 :             :  *
       5                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       6                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       7                 :             :  *
       8                 :             :  * src/fe_utils/option_utils.c
       9                 :             :  *
      10                 :             :  *-------------------------------------------------------------------------
      11                 :             :  */
      12                 :             : 
      13                 :             : #include "postgres_fe.h"
      14                 :             : 
      15                 :             : #include "common/logging.h"
      16                 :             : #include "common/string.h"
      17                 :             : #include "fe_utils/option_utils.h"
      18                 :             : 
      19                 :             : /*
      20                 :             :  * Provide strictly harmonized handling of --help and --version
      21                 :             :  * options.
      22                 :             :  */
      23                 :             : void
      24                 :         354 : handle_help_version_opts(int argc, char *argv[],
      25                 :             :                          const char *fixed_progname, help_handler hlp)
      26                 :             : {
      27         [ +  + ]:         354 :     if (argc > 1)
      28                 :             :     {
      29   [ +  +  -  + ]:         350 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
      30                 :             :         {
      31                 :          11 :             hlp(get_progname(argv[0]));
      32                 :          11 :             exit(0);
      33                 :             :         }
      34   [ +  +  +  + ]:         339 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
      35                 :             :         {
      36                 :          30 :             printf("%s (PostgreSQL) " PG_VERSION "\n", fixed_progname);
      37                 :          30 :             exit(0);
      38                 :             :         }
      39                 :             :     }
      40                 :         313 : }
      41                 :             : 
      42                 :             : /*
      43                 :             :  * option_parse_int
      44                 :             :  *
      45                 :             :  * Parse integer value for an option.  If the parsing is successful, returns
      46                 :             :  * true and stores the result in *result if that's given; if parsing fails,
      47                 :             :  * returns false.
      48                 :             :  */
      49                 :             : bool
      50                 :         250 : option_parse_int(const char *optarg, const char *optname,
      51                 :             :                  int min_range, int max_range,
      52                 :             :                  int *result)
      53                 :             : {
      54                 :             :     char       *endptr;
      55                 :             :     int         val;
      56                 :             : 
      57                 :         250 :     errno = 0;
      58                 :         250 :     val = strtoint(optarg, &endptr, 10);
      59                 :             : 
      60                 :             :     /*
      61                 :             :      * Skip any trailing whitespace; if anything but whitespace remains before
      62                 :             :      * the terminating character, fail.
      63                 :             :      */
      64   [ +  +  +  + ]:         251 :     while (*endptr != '\0' && isspace((unsigned char) *endptr))
      65                 :           1 :         endptr++;
      66                 :             : 
      67         [ +  + ]:         250 :     if (*endptr != '\0')
      68                 :             :     {
      69                 :           6 :         pg_log_error("invalid value \"%s\" for option %s",
      70                 :             :                      optarg, optname);
      71                 :           6 :         return false;
      72                 :             :     }
      73                 :             : 
      74   [ +  -  +  +  :         244 :     if (errno == ERANGE || val < min_range || val > max_range)
                   -  + ]
      75                 :             :     {
      76                 :          11 :         pg_log_error("%s must be in range %d..%d",
      77                 :             :                      optname, min_range, max_range);
      78                 :          11 :         return false;
      79                 :             :     }
      80                 :             : 
      81         [ +  + ]:         233 :     if (result)
      82                 :         227 :         *result = val;
      83                 :         233 :     return true;
      84                 :             : }
      85                 :             : 
      86                 :             : /*
      87                 :             :  * Provide strictly harmonized handling of the --sync-method option.
      88                 :             :  */
      89                 :             : bool
      90                 :           1 : parse_sync_method(const char *optarg, DataDirSyncMethod *sync_method)
      91                 :             : {
      92         [ -  + ]:           1 :     if (strcmp(optarg, "fsync") == 0)
      93                 :           0 :         *sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
      94         [ +  - ]:           1 :     else if (strcmp(optarg, "syncfs") == 0)
      95                 :             :     {
      96                 :             : #ifdef HAVE_SYNCFS
      97                 :           1 :         *sync_method = DATA_DIR_SYNC_METHOD_SYNCFS;
      98                 :             : #else
      99                 :             :         pg_log_error("this build does not support sync method \"%s\"",
     100                 :             :                      "syncfs");
     101                 :             :         return false;
     102                 :             : #endif
     103                 :             :     }
     104                 :             :     else
     105                 :             :     {
     106                 :           0 :         pg_log_error("unrecognized sync method: %s", optarg);
     107                 :           0 :         return false;
     108                 :             :     }
     109                 :             : 
     110                 :           1 :     return true;
     111                 :             : }
     112                 :             : 
     113                 :             : /*
     114                 :             :  * Fail with appropriate error if 2 or more of the specified options are set.
     115                 :             :  * The first parameter is the number of arguments.  Following that is an
     116                 :             :  * arbitrary number of bool/string pairs.  The bool indicates whether the
     117                 :             :  * option is set, and the string is used for the error message.  Note that only
     118                 :             :  * the first pair of enabled options we find are reported.
     119                 :             :  *
     120                 :             :  * Don't call this directly.  Use the check_mut_excl_opts() macro in
     121                 :             :  * option_utils.h, which is the exact same except it doesn't take the first
     122                 :             :  * parameter (it discovers the number of arguments automagically).
     123                 :             :  */
     124                 :             : void
     125                 :        1787 : check_mut_excl_opts_internal(int n, ...)
     126                 :             : {
     127                 :        1787 :     char       *first = NULL;
     128                 :             :     va_list     args;
     129                 :             : 
     130                 :             :     Assert(n % 2 == 0);
     131                 :             : 
     132                 :        1787 :     va_start(args, n);
     133         [ +  + ]:        5803 :     for (int i = 0; i < n; i += 2)
     134                 :             :     {
     135                 :        4023 :         bool        set = va_arg(args, int);
     136                 :        4023 :         char       *opt = va_arg(args, char *);
     137                 :             : 
     138   [ +  +  +  + ]:        4023 :         if (set && first)
     139                 :           7 :             pg_fatal("options %s and %s cannot be used together",
     140                 :             :                      first, opt);
     141                 :             : 
     142         [ +  + ]:        4016 :         if (set)
     143                 :         308 :             first = opt;
     144                 :             :     }
     145                 :        1780 :     va_end(args);
     146                 :        1780 : }
        

Generated by: LCOV version 2.0-1