1 |
vapier 11/02/28 18:15:33 |
2 |
|
3 |
Modified: q.c |
4 |
Log: |
5 |
add a helper for easily running applets from other applets |
6 |
|
7 |
Revision Changes Path |
8 |
1.50 portage-utils/q.c |
9 |
|
10 |
file : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?rev=1.50&view=markup |
11 |
plain: http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?rev=1.50&content-type=text/plain |
12 |
diff : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?r1=1.49&r2=1.50 |
13 |
|
14 |
Index: q.c |
15 |
=================================================================== |
16 |
RCS file: /var/cvsroot/gentoo-projects/portage-utils/q.c,v |
17 |
retrieving revision 1.49 |
18 |
retrieving revision 1.50 |
19 |
diff -u -r1.49 -r1.50 |
20 |
--- q.c 21 Feb 2011 01:33:47 -0000 1.49 |
21 |
+++ q.c 28 Feb 2011 18:15:32 -0000 1.50 |
22 |
@@ -1,7 +1,7 @@ |
23 |
/* |
24 |
* Copyright 2005-2010 Gentoo Foundation |
25 |
* Distributed under the terms of the GNU General Public License v2 |
26 |
- * $Header: /var/cvsroot/gentoo-projects/portage-utils/q.c,v 1.49 2011/02/21 01:33:47 vapier Exp $ |
27 |
+ * $Header: /var/cvsroot/gentoo-projects/portage-utils/q.c,v 1.50 2011/02/28 18:15:32 vapier Exp $ |
28 |
* |
29 |
* Copyright 2005-2010 Ned Ludd - <solar@g.o> |
30 |
* Copyright 2005-2010 Mike Frysinger - <vapier@g.o> |
31 |
@@ -22,7 +22,7 @@ |
32 |
"Module path", |
33 |
COMMON_OPTS_HELP |
34 |
}; |
35 |
-static const char q_rcsid[] = "$Id: q.c,v 1.49 2011/02/21 01:33:47 vapier Exp $"; |
36 |
+static const char q_rcsid[] = "$Id: q.c,v 1.50 2011/02/28 18:15:32 vapier Exp $"; |
37 |
#define q_usage(ret) usage(ret, Q_FLAGS, q_long_opts, q_opts_help, lookup_applet_idx("q")) |
38 |
|
39 |
static APPLET lookup_applet(const char *applet) |
40 |
@@ -134,3 +134,43 @@ |
41 |
|
42 |
return (func)(argc - 1, ++argv); |
43 |
} |
44 |
+ |
45 |
+static int run_applet_l(const char *arg, ...) |
46 |
+{ |
47 |
+ int (*applet)(int, char **); |
48 |
+ va_list ap; |
49 |
+ int ret, optind_saved, argc; |
50 |
+ char **argv; |
51 |
+ const char *argv0_saved; |
52 |
+ |
53 |
+ optind_saved = optind; |
54 |
+ argv0_saved = argv0; |
55 |
+ |
56 |
+ applet = lookup_applet(arg); |
57 |
+ if (!applet) |
58 |
+ return -1; |
59 |
+ |
60 |
+ /* This doesn't NULL terminate argv, but you should be using argc */ |
61 |
+ va_start(ap, arg); |
62 |
+ argc = 0; |
63 |
+ argv = NULL; |
64 |
+ while (arg) { |
65 |
+ argv = xrealloc(argv, sizeof(*argv) * ++argc); |
66 |
+ argv[argc - 1] = xstrdup(arg); |
67 |
+ arg = va_arg(ap, const char *); |
68 |
+ } |
69 |
+ va_end(ap); |
70 |
+ |
71 |
+ optind = 0; |
72 |
+ argv0 = argv[0]; |
73 |
+ ret = applet(argc, argv); |
74 |
+ |
75 |
+ while (argc--) |
76 |
+ free(argv[argc]); |
77 |
+ free(argv); |
78 |
+ |
79 |
+ optind = optind_saved; |
80 |
+ argv0 = argv0_saved; |
81 |
+ |
82 |
+ return ret; |
83 |
+} |