1 |
commit: 7cdf692beb93c596b4bf4f5f7b5bb8bcc69a27ea |
2 |
Author: Fabian Groffen <grobian <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sun Dec 29 09:54:28 2019 +0000 |
4 |
Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org> |
5 |
CommitDate: Sun Dec 29 09:54:28 2019 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=7cdf692b |
7 |
|
8 |
q: improve applet searching somewhat, change warn app name |
9 |
|
10 |
- use a single loop iteration to match an applet |
11 |
- set full name of applet (qlop iso lop) in argv0 for warn() to match |
12 |
the running command |
13 |
- use name of aliased applet when invoked by an alias (belongs -> qfile) |
14 |
|
15 |
Bug: https://bugs.gentoo.org/701968 |
16 |
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org> |
17 |
|
18 |
applets.h | 1 - |
19 |
q.c | 37 +++++++++++++++++++++++++++---------- |
20 |
2 files changed, 27 insertions(+), 11 deletions(-) |
21 |
|
22 |
diff --git a/applets.h b/applets.h |
23 |
index ea4eece..4625fde 100644 |
24 |
--- a/applets.h |
25 |
+++ b/applets.h |
26 |
@@ -102,7 +102,6 @@ static const struct applet_t { |
27 |
/*"glsa"*/ |
28 |
{"hasuse", quse_main, NULL, NULL}, |
29 |
/*"list"*/ |
30 |
- {"size", qsize_main, NULL, NULL}, |
31 |
/*"stats"*/ |
32 |
/*"uses"*/ |
33 |
/*"which"*/ |
34 |
|
35 |
diff --git a/q.c b/q.c |
36 |
index 4a2fd62..6d7eced 100644 |
37 |
--- a/q.c |
38 |
+++ b/q.c |
39 |
@@ -48,24 +48,41 @@ APPLET lookup_applet(const char *applet) |
40 |
if (strlen(applet) < 1) |
41 |
return NULL; |
42 |
|
43 |
- for (i = 0; applets[i].name; ++i) { |
44 |
- if (strcmp(applets[i].name, applet) == 0) { |
45 |
+ if (applet[0] == 'q') |
46 |
+ applet++; |
47 |
+ |
48 |
+ /* simple case, e.g. something like "qlop" or "q lop" both being "lop" */ |
49 |
+ for (i = 0; applets[i].desc != NULL; i++) { |
50 |
+ if (strcmp(applets[i].name + 1, applet) == 0) { |
51 |
argv0 = applets[i].name; |
52 |
- if (i && applets[i].desc != NULL) |
53 |
- ++argv0; /* chop the leading 'q' */ |
54 |
return applets[i].func; |
55 |
} |
56 |
} |
57 |
|
58 |
- /* No applet found? Search by shortname then... */ |
59 |
- for (i = 1; applets[i].desc != NULL; ++i) { |
60 |
- if (strcmp(applets[i].name + 1, applet) == 0) { |
61 |
- argv0 = applets[i].name + 1; |
62 |
- return applets[i].func; |
63 |
+ /* this is possibly an alias like "belongs" |
64 |
+ * NOTE: we continue where the previous loop left, e.g. on the first |
65 |
+ * alias (desc == NULL) */ |
66 |
+ for ( ; applets[i].name != NULL; i++) { |
67 |
+ if (strcmp(applets[i].name, applet) == 0) { |
68 |
+ unsigned int j; |
69 |
+ |
70 |
+ /* lookup the real name for this alias */ |
71 |
+ for (j = 1; applets[j].desc != NULL; j++) { |
72 |
+ if (applets[j].func == applets[i].func) { |
73 |
+ argv0 = applets[j].name; |
74 |
+ return applets[j].func; |
75 |
+ } |
76 |
+ } |
77 |
+ |
78 |
+ /* this shouldn't happen and means our array from applets.h |
79 |
+ * is inconsistent */ |
80 |
+ warn("invalid applet alias '%s': target applet unknown", applet); |
81 |
+ return NULL; |
82 |
} |
83 |
} |
84 |
+ |
85 |
/* still nothing ? those bastards ... */ |
86 |
- warn("Unknown applet '%s'", applet); |
87 |
+ warn("unknown applet: %s", applet); |
88 |
return NULL; |
89 |
} |