LCOV - code coverage report
Current view: top level - lib/dpkg - options.c (source / functions) Hit Total Coverage
Test: dpkg 1.21.11 C code coverage Lines: 83 163 50.9 %
Date: 2022-12-03 00:40:01 Functions: 7 11 63.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 64 162 39.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * libdpkg - Debian packaging suite library routines
       3                 :            :  * options.c - option parsing functions
       4                 :            :  *
       5                 :            :  * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
       6                 :            :  * Copyright © 2000,2002 Wichert Akkerman <wichert@deephackmode.org>
       7                 :            :  * Copyright © 2008-2015 Guillem Jover <guillem@debian.org>
       8                 :            :  *
       9                 :            :  * This is free software; you can redistribute it and/or modify
      10                 :            :  * it under the terms of the GNU General Public License as published by
      11                 :            :  * the Free Software Foundation; either version 2 of the License, or
      12                 :            :  * (at your option) any later version.
      13                 :            :  *
      14                 :            :  * This is distributed in the hope that it will be useful,
      15                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17                 :            :  * GNU General Public License for more details.
      18                 :            :  *
      19                 :            :  * You should have received a copy of the GNU General Public License
      20                 :            :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21                 :            :  */
      22                 :            : 
      23                 :            : #include <config.h>
      24                 :            : #include <compat.h>
      25                 :            : 
      26                 :            : #include <errno.h>
      27                 :            : #include <limits.h>
      28                 :            : #include <string.h>
      29                 :            : #include <dirent.h>
      30                 :            : #include <stdarg.h>
      31                 :            : #include <stdlib.h>
      32                 :            : 
      33                 :            : #include <dpkg/i18n.h>
      34                 :            : #include <dpkg/c-ctype.h>
      35                 :            : #include <dpkg/dpkg.h>
      36                 :            : #include <dpkg/string.h>
      37                 :            : #include <dpkg/options.h>
      38                 :            : 
      39                 :            : static const char *printforhelp;
      40                 :            : 
      41                 :            : void
      42                 :         22 : badusage(const char *fmt, ...)
      43                 :            : {
      44                 :         22 :   char *buf = NULL;
      45                 :            :   va_list args;
      46                 :            : 
      47                 :         22 :   va_start(args, fmt);
      48                 :         22 :   m_vasprintf(&buf, fmt, args);
      49                 :         22 :   va_end(args);
      50                 :            : 
      51                 :         22 :   ohshit("%s\n\n%s", buf, gettext(printforhelp));
      52                 :            : }
      53                 :            : 
      54                 :            : static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(3)
      55                 :          0 : config_error(const char *file_name, int line_num, const char *fmt, ...)
      56                 :            : {
      57                 :          0 :   char *buf = NULL;
      58                 :            :   va_list args;
      59                 :            : 
      60                 :          0 :   va_start(args, fmt);
      61                 :          0 :   m_vasprintf(&buf, fmt, args);
      62                 :          0 :   va_end(args);
      63                 :            : 
      64                 :          0 :   ohshit(_("configuration error: %s:%d: %s"), file_name, line_num, buf);
      65                 :            : }
      66                 :            : 
      67                 :            : static void
      68                 :         96 : dpkg_options_load_file(const char *fn, const struct cmdinfo *cmdinfos)
      69                 :            : {
      70                 :            :   FILE *file;
      71                 :         96 :   int line_num = 0;
      72                 :            :   char linebuf[MAX_CONFIG_LINE];
      73                 :            : 
      74                 :         96 :   file= fopen(fn, "r");
      75         [ +  - ]:         96 :   if (!file) {
      76         [ +  - ]:         96 :     if (errno==ENOENT)
      77                 :         96 :       return;
      78                 :          0 :     warning(_("failed to open configuration file '%.255s' for reading: %s"),
      79                 :          0 :             fn, strerror(errno));
      80                 :          0 :     return;
      81                 :            :   }
      82                 :            : 
      83         [ #  # ]:          0 :   while (fgets(linebuf, sizeof(linebuf), file)) {
      84                 :            :     char *opt;
      85                 :            :     const struct cmdinfo *cip;
      86                 :            : 
      87                 :          0 :     line_num++;
      88                 :            : 
      89                 :          0 :     str_rtrim_spaces(linebuf, linebuf + strlen(linebuf));
      90                 :            : 
      91   [ #  #  #  # ]:          0 :     if ((linebuf[0] == '#') || (linebuf[0] == '\0'))
      92                 :          0 :       continue;
      93   [ #  #  #  # ]:          0 :     for (opt = linebuf; c_isalnum(*opt) || *opt == '-'; opt++) ;
      94         [ #  # ]:          0 :     if (*opt == '\0')
      95                 :          0 :       opt=NULL;
      96                 :            :     else {
      97                 :          0 :       *opt++ = '\0';
      98         [ #  # ]:          0 :       if (*opt=='=') opt++;
      99         [ #  # ]:          0 :       while (c_isspace(*opt))
     100                 :          0 :         opt++;
     101                 :            : 
     102                 :          0 :       opt = str_strip_quotes(opt);
     103         [ #  # ]:          0 :       if (opt == NULL)
     104                 :          0 :         config_error(fn, line_num, _("unbalanced quotes in '%s'"), linebuf);
     105                 :            :     }
     106                 :            : 
     107   [ #  #  #  # ]:          0 :     for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
     108                 :            :       int l;
     109                 :            : 
     110         [ #  # ]:          0 :       if (!cip->olong) continue;
     111         [ #  # ]:          0 :       if (strcmp(cip->olong, linebuf) == 0)
     112                 :          0 :         break;
     113                 :          0 :       l=strlen(cip->olong);
     114   [ #  #  #  #  :          0 :       if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
                   #  # ]
     115         [ #  # ]:          0 :           !opt && strncmp(linebuf, cip->olong, l) == 0) {
     116                 :          0 :         opt=linebuf+l+1;
     117                 :          0 :         break;
     118                 :            :       }
     119                 :            :     }
     120                 :            : 
     121         [ #  # ]:          0 :     if (!cip->olong)
     122                 :          0 :       config_error(fn, line_num, _("unknown option '%s'"), linebuf);
     123                 :            : 
     124         [ #  # ]:          0 :     if (cip->takesvalue) {
     125         [ #  # ]:          0 :       if (!opt)
     126                 :          0 :         config_error(fn, line_num, _("'%s' needs a value"), linebuf);
     127         [ #  # ]:          0 :       if (cip->call) cip->call(cip,opt);
     128                 :            :       else
     129                 :          0 :         *cip->sassignto = m_strdup(opt);
     130                 :            :     } else {
     131         [ #  # ]:          0 :       if (opt)
     132                 :          0 :         config_error(fn, line_num, _("'%s' does not take a value"), linebuf);
     133         [ #  # ]:          0 :       if (cip->call) cip->call(cip,NULL);
     134                 :            :       else
     135                 :          0 :         *cip->iassignto = cip->arg_int;
     136                 :            :     }
     137                 :            :   }
     138         [ #  # ]:          0 :   if (ferror(file))
     139                 :          0 :     ohshite(_("read error in configuration file '%.255s'"), fn);
     140         [ #  # ]:          0 :   if (fclose(file))
     141                 :          0 :     ohshite(_("error closing configuration file '%.255s'"), fn);
     142                 :            : }
     143                 :            : 
     144                 :            : static int
     145                 :          0 : valid_config_filename(const struct dirent *dent)
     146                 :            : {
     147                 :            :   const char *c;
     148                 :            : 
     149         [ #  # ]:          0 :   if (dent->d_name[0] == '.')
     150                 :          0 :     return 0;
     151                 :            : 
     152         [ #  # ]:          0 :   for (c = dent->d_name; *c; c++)
     153   [ #  #  #  #  :          0 :     if (!c_isalnum(*c) && *c != '_' && *c != '-')
                   #  # ]
     154                 :          0 :       return 0;
     155                 :            : 
     156         [ #  # ]:          0 :   if (*c == '\0')
     157                 :          0 :     return 1;
     158                 :            :   else
     159                 :          0 :     return 0;
     160                 :            : }
     161                 :            : 
     162                 :            : static void
     163                 :         48 : dpkg_options_load_dir(const char *prog, const struct cmdinfo *cmdinfos)
     164                 :            : {
     165                 :            :   char *dirname;
     166                 :            :   struct dirent **dlist;
     167                 :            :   int dlist_n, i;
     168                 :            : 
     169                 :         48 :   dirname = str_fmt("%s/%s.cfg.d", CONFIGDIR, prog);
     170                 :            : 
     171                 :         48 :   dlist_n = scandir(dirname, &dlist, valid_config_filename, alphasort);
     172         [ +  - ]:         48 :   if (dlist_n < 0) {
     173         [ +  - ]:         48 :     if (errno == ENOENT) {
     174                 :         48 :       free(dirname);
     175                 :         48 :       return;
     176                 :            :     } else
     177                 :          0 :       ohshite(_("error opening configuration directory '%s'"), dirname);
     178                 :            :   }
     179                 :            : 
     180         [ #  # ]:          0 :   for (i = 0; i < dlist_n; i++) {
     181                 :            :     char *filename;
     182                 :            : 
     183                 :          0 :     filename = str_fmt("%s/%s", dirname, dlist[i]->d_name);
     184                 :          0 :     dpkg_options_load_file(filename, cmdinfos);
     185                 :            : 
     186                 :          0 :     free(dlist[i]);
     187                 :          0 :     free(filename);
     188                 :            :   }
     189                 :            : 
     190                 :          0 :   free(dirname);
     191                 :          0 :   free(dlist);
     192                 :            : }
     193                 :            : 
     194                 :            : void
     195                 :         48 : dpkg_options_load(const char *prog, const struct cmdinfo *cmdinfos)
     196                 :            : {
     197                 :            :   char *home, *file;
     198                 :            : 
     199                 :         48 :   dpkg_options_load_dir(prog, cmdinfos);
     200                 :            : 
     201                 :         48 :   file = str_fmt("%s/%s.cfg", CONFIGDIR, prog);
     202                 :         48 :   dpkg_options_load_file(file, cmdinfos);
     203                 :         48 :   free(file);
     204                 :            : 
     205                 :         48 :   home = getenv("HOME");
     206         [ +  - ]:         48 :   if (home != NULL) {
     207                 :         48 :     file = str_fmt("%s/.%s.cfg", home, prog);
     208                 :         48 :     dpkg_options_load_file(file, cmdinfos);
     209                 :         48 :     free(file);
     210                 :            :   }
     211                 :         48 : }
     212                 :            : 
     213                 :            : void
     214                 :        371 : dpkg_options_parse(const char *const **argvp, const struct cmdinfo *cmdinfos,
     215                 :            :                    const char *help_str)
     216                 :            : {
     217                 :            :   const struct cmdinfo *cip;
     218                 :            :   const char *p, *value;
     219                 :            :   int l;
     220                 :            : 
     221         [ -  + ]:        371 :   if (**argvp == NULL)
     222                 :          0 :     badusage(_("missing program name in argv[0]"));
     223                 :            : 
     224                 :        371 :   printforhelp = help_str;
     225                 :            : 
     226                 :        371 :   ++(*argvp);
     227   [ +  +  +  +  :       1328 :   while ((p = **argvp) && p[0] == '-' && p[1] != '\0') {
                   +  - ]
     228                 :        967 :     ++(*argvp);
     229         [ +  + ]:        967 :     if (strcmp(p, "--") == 0)
     230                 :          1 :       break;
     231         [ +  + ]:        966 :     if (*++p == '-') {
     232                 :        897 :       ++p; value=NULL;
     233                 :        897 :       for (cip= cmdinfos;
     234   [ +  +  +  + ]:      13010 :            cip->olong || cip->oshort;
     235                 :      12113 :            cip++) {
     236         [ +  + ]:      13009 :         if (!cip->olong) continue;
     237         [ +  + ]:      12925 :         if (strcmp(p, cip->olong) == 0)
     238                 :        383 :           break;
     239                 :      12542 :         l= strlen(cip->olong);
     240   [ +  +  +  + ]:      13061 :         if (strncmp(p, cip->olong, l) == 0 &&
     241         [ -  + ]:       1032 :             (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
     242                 :            :       }
     243         [ +  + ]:        897 :       if (!cip->olong) badusage(_("unknown option --%s"),p);
     244         [ +  + ]:        896 :       if (cip->takesvalue) {
     245         [ +  + ]:        536 :         if (!value) {
     246                 :         23 :           value= *(*argvp)++;
     247         [ +  + ]:         23 :           if (!value) badusage(_("--%s option takes a value"),cip->olong);
     248                 :            :         }
     249         [ +  - ]:        533 :         if (cip->call) cip->call(cip,value);
     250                 :          0 :         else *cip->sassignto= value;
     251                 :            :       } else {
     252         [ -  + ]:        360 :         if (value) badusage(_("--%s option does not take a value"),cip->olong);
     253         [ +  + ]:        360 :         if (cip->call) cip->call(cip,NULL);
     254                 :            :         else
     255                 :         73 :           *cip->iassignto = cip->arg_int;
     256                 :            :       }
     257                 :            :     } else {
     258         [ +  + ]:        138 :       while (*p) {
     259   [ +  +  +  -  :        251 :         for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
                   +  + ]
     260         [ -  + ]:         69 :         if (!cip->oshort) badusage(_("unknown option -%c"),*p);
     261                 :         69 :         p++;
     262         [ +  + ]:         69 :         if (cip->takesvalue) {
     263         [ +  + ]:          5 :           if (!*p) {
     264                 :          2 :             value= *(*argvp)++;
     265         [ -  + ]:          2 :             if (!value) badusage(_("-%c option takes a value"),cip->oshort);
     266                 :            :           } else {
     267                 :          3 :             value= p; p="";
     268         [ -  + ]:          3 :             if (*value == '=') value++;
     269                 :            :           }
     270         [ +  + ]:          5 :           if (cip->call) cip->call(cip,value);
     271                 :          1 :           else *cip->sassignto= value;
     272                 :            :         } else {
     273         [ -  + ]:         64 :           if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
     274         [ +  - ]:         64 :           if (cip->call) cip->call(cip,NULL);
     275                 :            :           else
     276                 :          0 :             *cip->iassignto = cip->arg_int;
     277                 :            :         }
     278                 :            :       }
     279                 :            :     }
     280                 :            :   }
     281                 :        362 : }
     282                 :            : 
     283                 :            : long
     284                 :          0 : dpkg_options_parse_arg_int(const struct cmdinfo *cmd, const char *str)
     285                 :            : {
     286                 :            :   long value;
     287                 :            :   char *end;
     288                 :            : 
     289                 :          0 :   errno = 0;
     290                 :          0 :   value = strtol(str, &end, 0);
     291   [ #  #  #  #  :          0 :   if (str == end || *end || value < 0 || value > INT_MAX || errno != 0) {
          #  #  #  #  #  
                      # ]
     292         [ #  # ]:          0 :     if (cmd->olong)
     293                 :          0 :       badusage(_("invalid integer for --%s: '%.250s'"), cmd->olong, str);
     294                 :            :     else
     295                 :          0 :       badusage(_("invalid integer for -%c: '%.250s'"), cmd->oshort, str);
     296                 :            :   }
     297                 :            : 
     298                 :          0 :   return value;
     299                 :            : }
     300                 :            : 
     301                 :            : void
     302                 :          0 : setobsolete(const struct cmdinfo *cip, const char *value)
     303                 :            : {
     304                 :          0 :   warning(_("obsolete option '--%s'"), cip->olong);
     305                 :          0 : }
     306                 :            : 
     307                 :            : const struct cmdinfo *cipaction = NULL;
     308                 :            : 
     309                 :            : /* XXX: This function is a hack. */
     310                 :            : static inline int
     311                 :          2 : option_short(int c)
     312                 :            : {
     313         [ -  + ]:          2 :   return c ? c : '\b';
     314                 :            : }
     315                 :            : 
     316                 :            : void
     317                 :        365 : setaction(const struct cmdinfo *cip, const char *value)
     318                 :            : {
     319   [ +  +  +  - ]:        365 :   if (cipaction && cip)
     320                 :          1 :     badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
     321                 :          1 :              option_short(cip->oshort), cip->olong,
     322                 :          1 :              option_short(cipaction->oshort), cipaction->olong);
     323                 :        364 :   cipaction = cip;
     324   [ +  -  -  +  :        364 :   if (cip && cip->takesvalue == 2 && cip->sassignto)
                   -  - ]
     325                 :          0 :     *cipaction->sassignto = value;
     326                 :        364 : }

Generated by: LCOV version 1.16