commit:     7cdf692beb93c596b4bf4f5f7b5bb8bcc69a27ea
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sun Dec 29 09:54:28 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sun Dec 29 09:54:28 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=7cdf692b

q: improve applet searching somewhat, change warn app name

- use a single loop iteration to match an applet
- set full name of applet (qlop iso lop) in argv0 for warn() to match
  the running command
- use name of aliased applet when invoked by an alias (belongs -> qfile)

Bug: https://bugs.gentoo.org/701968
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 applets.h |  1 -
 q.c       | 37 +++++++++++++++++++++++++++----------
 2 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/applets.h b/applets.h
index ea4eece..4625fde 100644
--- a/applets.h
+++ b/applets.h
@@ -102,7 +102,6 @@ static const struct applet_t {
        /*"glsa"*/
        {"hasuse",    quse_main,      NULL, NULL},
        /*"list"*/
-       {"size",      qsize_main,     NULL, NULL},
        /*"stats"*/
        /*"uses"*/
        /*"which"*/

diff --git a/q.c b/q.c
index 4a2fd62..6d7eced 100644
--- a/q.c
+++ b/q.c
@@ -48,24 +48,41 @@ APPLET lookup_applet(const char *applet)
        if (strlen(applet) < 1)
                return NULL;
 
-       for (i = 0; applets[i].name; ++i) {
-               if (strcmp(applets[i].name, applet) == 0) {
+       if (applet[0] == 'q')
+               applet++;
+
+       /* simple case, e.g. something like "qlop" or "q lop" both being "lop"  
*/
+       for (i = 0; applets[i].desc != NULL; i++) {
+               if (strcmp(applets[i].name + 1, applet) == 0) {
                        argv0 = applets[i].name;
-                       if (i && applets[i].desc != NULL)
-                               ++argv0; /* chop the leading 'q' */
                        return applets[i].func;
                }
        }
 
-       /* No applet found? Search by shortname then... */
-       for (i = 1; applets[i].desc != NULL; ++i) {
-               if (strcmp(applets[i].name + 1, applet) == 0) {
-                       argv0 = applets[i].name + 1;
-                       return applets[i].func;
+       /* this is possibly an alias like "belongs"
+        * NOTE: we continue where the previous loop left, e.g. on the first
+        * alias (desc == NULL) */
+       for ( ; applets[i].name != NULL; i++) {
+               if (strcmp(applets[i].name, applet) == 0) {
+                       unsigned int j;
+
+                       /* lookup the real name for this alias */
+                       for (j = 1; applets[j].desc != NULL; j++) {
+                               if (applets[j].func == applets[i].func) {
+                                       argv0 = applets[j].name;
+                                       return applets[j].func;
+                               }
+                       }
+
+                       /* this shouldn't happen and means our array from 
applets.h
+                        * is inconsistent */
+                       warn("invalid applet alias '%s': target applet 
unknown", applet);
+                       return NULL;
                }
        }
+
        /* still nothing ?  those bastards ... */
-       warn("Unknown applet '%s'", applet);
+       warn("unknown applet: %s", applet);
        return NULL;
 }
 

Reply via email to