1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2010 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
23 #include <sys/types.h>
27 #include <glib/gprintf.h>
30 #include <gpg-error.h>
38 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__
, strerror(ENOMEM
));
57 ret
= g_vasprintf(&buf
, fmt
, ap
);
61 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(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 (!strcmp(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 gboolean
contains_whitespace(const gchar
*str
)
141 const gchar
*p
= str
;
145 len
= g_utf8_strlen(p
++, -1) -1;
148 c
= g_utf8_get_char(p
++);
150 if (g_unichar_isspace(c
))
157 gchar
*expand_homedir(gchar
*str
)
162 return g_strdup_printf("%s%s", g_get_home_dir(), p
);
164 return g_strdup(str
);
167 gchar
*strip_char(gchar
*str
, gchar c
)
171 for (p
= p2
= str
; *p
; p
++) {
182 gchar
*tohex(const guchar
*str
, gsize len
)
187 if (!str
|| len
<= 0)
190 p
= g_malloc0(len
*2+1);
195 for (i
= 0; i
< len
; i
++) {
198 g_sprintf(buf
, "%02x", str
[i
]);
205 gchar
**split_input_line(gchar
*str
, gchar
*delim
, gint n
)
210 return g_strsplit(str
, delim
, n
);
213 gpg_error_t
parse_options(gchar
**line
, struct argv_s
*args
[], gpointer data
)
218 for (; p
&& *p
; p
++) {
219 while (g_ascii_isspace(*p
))
225 if (*p
== '-' && *(p
+1) == '-') {
227 gchar opt
[255] = {0}, value
[255] = {0};
231 for (tp
= opt
, len
= 0; *p
; p
++, len
++) {
233 return GPG_ERR_LINE_TOO_LONG
;
235 if (*p
== ' ' || *p
== '=') {
236 gboolean inquote
= FALSE
;
243 for (tp
= value
, len
= 0; *p
; p
++, len
++) {
245 return GPG_ERR_LINE_TOO_LONG
;
251 else if (*p
== ' ' && !inquote
)
267 gboolean match
= FALSE
;
269 for (int i
= 0; args
[i
]; i
++) {
270 if (!g_strcmp0(args
[i
]->opt
, opt
)) {
271 if (args
[i
]->type
== OPT_NOARG
&& *value
)
272 return GPG_ERR_SYNTAX
;
273 else if (args
[i
]->type
== OPT_ARG
&& !*value
)
274 return GPG_ERR_SYNTAX
;
276 rc
= args
[i
]->func(data
, value
);
287 return GPG_ERR_UNKNOWN_OPTION
;