skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

subgetopt.c (1296B)


      1 /* ISC license. */
      2 
      3 #undef SUBGETOPT_SHORT
      4 #include <skalibs/sgetopt.h>
      5 
      6 int subgetopt_r (int argc, char const *const *argv, char const *opts, subgetopt *o)
      7 {
      8   o->arg = 0 ;
      9   if ((o->ind >= argc) || !argv[o->ind]) return -1 ;
     10   if (o->pos && !argv[o->ind][o->pos])
     11   {
     12     o->ind++ ;
     13     o->pos = 0 ;
     14     if ((o->ind >= argc) || !argv[o->ind]) return -1 ;
     15   }
     16 
     17   if (!o->pos)
     18   {
     19     char c ;
     20     if (argv[o->ind][0] != '-') return -1 ;
     21     o->pos++ ;
     22     c = argv[o->ind][1] ;
     23     if (c == '-')
     24     {
     25       if (argv[o->ind][2]) return o->problem = '-', '?' ;
     26       o->ind++ ;
     27       o->pos = 0 ;
     28       return -1 ;
     29     }
     30     if (!c || c == '-') return o->pos = 0, -1 ;
     31   }
     32   {
     33     char c = argv[o->ind][o->pos++] ;
     34     char const *s = opts ;
     35     char retnoarg = (*s == ':') ? (s++, ':') : '?' ;
     36     while (*s)
     37     {
     38       if (c == *s)
     39       {
     40         if (s[1] == ':')
     41         {
     42           o->arg = argv[o->ind++] + o->pos ;
     43           o->pos = 0 ;
     44           if (!*o->arg)
     45           {
     46             o->arg = argv[o->ind] ;
     47             if ((o->ind >= argc) || !o->arg)
     48             {
     49               o->problem = c ;
     50               return retnoarg ;
     51             }
     52 	    o->ind++ ;
     53           }
     54         }
     55         return c ;
     56       }
     57       if (*++s == ':') s++ ;
     58     }
     59     o->problem = c ;
     60   }
     61   return '?' ;
     62 }