1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2011 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
27 #include <sys/types.h>
31 #include <glib/gprintf.h>
34 #include <gpg-error.h>
36 #include "pwmd_error.h"
38 extern void log_write(const gchar
*fmt
, ...);
40 gboolean
strv_printf(gchar
***array
, const gchar
*fmt
, ...)
45 gint len
= *array
? g_strv_length(*array
) : 0;
51 if ((a
= g_realloc(*array
, (len
+ 2) * sizeof(gchar
*))) == NULL
) {
52 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
57 ret
= g_vasprintf(&buf
, fmt
, ap
);
61 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
71 gchar
**strvcatv(gchar
**dst
, gchar
**src
)
87 for (p
= src
; *p
; p
++) {
90 pa
= g_realloc(d
, (i
+ 2) * sizeof(gchar
*));
111 gboolean
valid_filename(const gchar
*filename
)
115 if (!filename
|| !*filename
)
118 if (!g_strcmp0(filename
, "-"))
121 for (p
= filename
; *p
; p
++) {
122 if (*p
== '/' || isspace(*p
))
129 gchar
*print_fmt(gchar
*buf
, gsize len
, const char *fmt
, ...)
134 g_vsnprintf(buf
, len
, fmt
, ap
);
139 gchar
*expand_homedir(gchar
*str
)
144 return g_strdup_printf("%s%s", g_get_home_dir(), p
);
146 return g_strdup(str
);
149 gchar
**split_input_line(gchar
*str
, gchar
*delim
, gint n
)
154 return g_strsplit(str
, delim
, n
);
157 gpg_error_t
parse_options(gchar
**line
, struct argv_s
*args
[], gpointer data
)
162 for (; p
&& *p
; p
++) {
163 while (g_ascii_isspace(*p
))
169 if (*p
== '-' && *(p
+1) == '-') {
171 gchar opt
[255] = {0}, value
[255] = {0};
175 if (!*p
|| *p
== ' ') {
182 for (tp
= opt
, len
= 0; *p
; p
++, len
++) {
184 return GPG_ERR_LINE_TOO_LONG
;
186 if (*p
== ' ' || *p
== '=') {
187 gboolean inquote
= FALSE
;
194 for (tp
= value
, len
= 0; *p
; p
++, len
++) {
196 return GPG_ERR_LINE_TOO_LONG
;
202 else if (*p
== ' ' && !inquote
)
218 gboolean match
= FALSE
;
220 for (int i
= 0; args
[i
]; i
++) {
221 if (!g_strcmp0(args
[i
]->opt
, opt
)) {
222 if (args
[i
]->type
== OPTION_TYPE_NOARG
&& *value
)
223 return GPG_ERR_SYNTAX
;
224 else if (args
[i
]->type
== OPTION_TYPE_ARG
&& !*value
)
225 return GPG_ERR_SYNTAX
;
227 rc
= args
[i
]->func(data
, value
);
232 log_write2("OPTION='%s' VALUE='%s'", opt
, value
);
239 return GPG_ERR_UNKNOWN_OPTION
;