From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 7C012138334 for ; Sun, 29 Dec 2019 09:57:21 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 7D06EE0D77; Sun, 29 Dec 2019 09:57:18 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 53D13E0D77 for ; Sun, 29 Dec 2019 09:57:18 +0000 (UTC) Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 462EC34DCA6 for ; Sun, 29 Dec 2019 09:57:17 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 023663C for ; Sun, 29 Dec 2019 09:57:16 +0000 (UTC) From: "Fabian Groffen" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Fabian Groffen" Message-ID: <1577613268.7cdf692beb93c596b4bf4f5f7b5bb8bcc69a27ea.grobian@gentoo> Subject: [gentoo-commits] proj/portage-utils:master commit in: / X-VCS-Repository: proj/portage-utils X-VCS-Files: applets.h q.c X-VCS-Directories: / X-VCS-Committer: grobian X-VCS-Committer-Name: Fabian Groffen X-VCS-Revision: 7cdf692beb93c596b4bf4f5f7b5bb8bcc69a27ea X-VCS-Branch: master Date: Sun, 29 Dec 2019 09:57:16 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: e5a2e836-abf2-498e-9dc9-1dd8aa4bed3f X-Archives-Hash: 1b75cabc58213ed91ed934e97bbef57f commit: 7cdf692beb93c596b4bf4f5f7b5bb8bcc69a27ea Author: Fabian Groffen gentoo org> AuthorDate: Sun Dec 29 09:54:28 2019 +0000 Commit: Fabian Groffen gentoo 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 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; }