LCOV - code coverage report
Current view: top level - common - server-help.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 45 64.4 %
Date: 2016-09-12 12:29:17 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /* server-help.h - Helper functions for writing Assuan servers.
       2             :  *      Copyright (C) 2003, 2009, 2010 g10 Code GmbH
       3             :  *
       4             :  * This file is part of GnuPG.
       5             :  *
       6             :  * This file is free software; you can redistribute it and/or modify
       7             :  * it under the terms of either
       8             :  *
       9             :  *   - the GNU Lesser General Public License as published by the Free
      10             :  *     Software Foundation; either version 3 of the License, or (at
      11             :  *     your option) any later version.
      12             :  *
      13             :  * or
      14             :  *
      15             :  *   - the GNU General Public License as published by the Free
      16             :  *     Software Foundation; either version 2 of the License, or (at
      17             :  *     your option) any later version.
      18             :  *
      19             :  * or both in parallel, as here.
      20             :  *
      21             :  * This file is distributed in the hope that it will be useful,
      22             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      23             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      24             :  * GNU General Public License for more details.
      25             :  *
      26             :  * You should have received a copy of the GNU General Public License
      27             :  * along with this program; if not, see <http://www.gnu.org/licenses/>.
      28             :  */
      29             : 
      30             : #include <config.h>
      31             : #include <string.h>
      32             : 
      33             : #include "server-help.h"
      34             : #include "util.h"
      35             : 
      36             : /* Skip over options in LINE.
      37             : 
      38             :    Blanks after the options are also removed.  Options are indicated
      39             :    by two leading dashes followed by a string consisting of non-space
      40             :    characters.  The special option "--" indicates an explicit end of
      41             :    options; all what follows will not be considered an option.  The
      42             :    first no-option string also indicates the end of option parsing. */
      43             : char *
      44         544 : skip_options (const char *line)
      45             : {
      46        1088 :   while (spacep (line))
      47           0 :     line++;
      48        1160 :   while (*line == '-' && line[1] == '-')
      49             :     {
      50        1025 :       while (*line && !spacep (line))
      51         881 :         line++;
      52         156 :       while (spacep (line))
      53          12 :         line++;
      54             :     }
      55         544 :   return (char*) line;
      56             : }
      57             : 
      58             : 
      59             : /* Check whether the option NAME appears in LINE.  */
      60             : int
      61         407 : has_option (const char *line, const char *name)
      62             : {
      63             :   const char *s;
      64         407 :   int n = strlen (name);
      65             : 
      66         407 :   s = strstr (line, name);
      67         407 :   if (s && s >= skip_options (line))
      68           0 :     return 0;
      69         407 :   return (s && (s == line || spacep (s-1)) && (!s[n] || spacep (s+n)));
      70             : }
      71             : 
      72             : 
      73             : /* Same as has_option but only considers options at the begin of the
      74             :    line.  This is useful for commands which allow arbitrary strings on
      75             :    the line.  */
      76             : int
      77           0 : has_leading_option (const char *line, const char *name)
      78             : {
      79             :   const char *s;
      80             :   int n;
      81             : 
      82           0 :   if (name[0] != '-' || name[1] != '-' || !name[2] || spacep (name+2))
      83           0 :     return 0;
      84           0 :   n = strlen (name);
      85           0 :   while ( *line == '-' && line[1] == '-' )
      86             :     {
      87           0 :       s = line;
      88           0 :       while (*line && !spacep (line))
      89           0 :         line++;
      90           0 :       if (n == (line - s) && !strncmp (s, name, n))
      91           0 :         return 1;
      92           0 :       while (spacep (line))
      93           0 :         line++;
      94             :     }
      95           0 :   return 0;
      96             : }
      97             : 
      98             : 
      99             : /* Same as has_option but does only test for the name of the option
     100             :    and ignores an argument, i.e. with NAME being "--hash" it would
     101             :    return a pointer for "--hash" as well as for "--hash=foo".  If
     102             :    there is no such option NULL is returned.  The pointer returned
     103             :    points right behind the option name, this may be an equal sign, Nul
     104             :    or a space.  */
     105             : const char *
     106         137 : has_option_name (const char *line, const char *name)
     107             : {
     108             :   const char *s;
     109         137 :   int n = strlen (name);
     110             : 
     111         137 :   s = strstr (line, name);
     112         137 :   return (s && (s == line || spacep (s-1))
     113         137 :           && (!s[n] || spacep (s+n) || s[n] == '=')) ? (s+n) : NULL;
     114             : }
     115             : 
     116             : 
     117             : /* Return a pointer to the argument of the option with NAME.  If such
     118             :    an option is not given, NULL is returned. */
     119             : char *
     120           8 : option_value (const char *line, const char *name)
     121             : {
     122             :   char *s;
     123           8 :   int n = strlen (name);
     124             : 
     125           8 :   s = strstr (line, name);
     126           8 :   if (s && s >= skip_options (line))
     127           0 :     return NULL;
     128           8 :   if (s && (s == line || spacep (s-1))
     129           1 :       && s[n] && (spacep (s+n) || s[n] == '='))
     130             :     {
     131           1 :       s += n + 1;
     132           1 :       s += strspn (s, " ");
     133           1 :       if (*s && !spacep(s))
     134           1 :         return s;
     135             :     }
     136           7 :   return NULL;
     137             : }

Generated by: LCOV version 1.11