1 /* server-help.h - Helper functions for writing Assuan servers.
2 * Copyright (C) 2003, 2009, 2010 g10 Code GmbH
4 * This file is part of GnuPG.
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of either
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.
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.
19 * or both in parallel, as here.
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.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <https://www.gnu.org/licenses/>.
33 #include "server-help.h"
36 /* Skip over options in LINE.
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. */
44 skip_options (const char *line)
48 while (*line == '-' && line[1] == '-')
50 while (*line && !spacep (line))
59 /* Check whether the option NAME appears in LINE. */
61 has_option (const char *line, const char *name)
64 int n = strlen (name);
66 s = strstr (line, name);
67 if (s && s >= skip_options (line))
69 return (s && (s == line || spacep (s-1)) && (!s[n] || spacep (s+n)));
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
77 has_leading_option (const char *line, const char *name)
82 if (name[0] != '-' || name[1] != '-' || !name[2] || spacep (name+2))
85 while ( *line == '-' && line[1] == '-' )
88 while (*line && !spacep (line))
90 if (n == (line - s) && !strncmp (s, name, n))
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
106 has_option_name (const char *line, const char *name)
109 int n = strlen (name);
111 s = strstr (line, name);
112 return (s && (s == line || spacep (s-1))
113 && (!s[n] || spacep (s+n) || s[n] == '=')) ? (s+n) : NULL;
117 /* Return a pointer to the argument of the option with NAME. If such
118 an option is not given, NULL is returned. */
120 option_value (const char *line, const char *name)
123 int n = strlen (name);
125 s = strstr (line, name);
126 if (s && s >= skip_options (line))
128 if (s && (s == line || spacep (s-1))
129 && s[n] && (spacep (s+n) || s[n] == '='))
132 s += strspn (s, " ");
133 if (*s && !spacep(s))