Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <termios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "subcmd-util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "help.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "exec-cmd.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	ent->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	memcpy(ent->name, name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	ent->name[len] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	cmds->names[cmds->cnt++] = ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) void clean_cmdnames(struct cmdnames *cmds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	for (i = 0; i < cmds->cnt; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		zfree(&cmds->names[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	zfree(&cmds->names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	cmds->cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	cmds->alloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) int cmdname_compare(const void *a_, const void *b_)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct cmdname *a = *(struct cmdname **)a_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct cmdname *b = *(struct cmdname **)b_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	return strcmp(a->name, b->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) void uniq(struct cmdnames *cmds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	unsigned int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (!cmds->cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	for (i = j = 1; i < cmds->cnt; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			cmds->names[j++] = cmds->names[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	cmds->cnt = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	size_t ci, cj, ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	int cmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	ci = cj = ei = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	while (ci < cmds->cnt && ei < excludes->cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		if (cmp < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			cmds->names[cj++] = cmds->names[ci++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		} else if (cmp == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			ci++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			ei++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		} else if (cmp > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			ei++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	while (ci < cmds->cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		cmds->names[cj++] = cmds->names[ci++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	cmds->cnt = cj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static void get_term_dimensions(struct winsize *ws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	char *s = getenv("LINES");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (s != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		ws->ws_row = atoi(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		s = getenv("COLUMNS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		if (s != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			ws->ws_col = atoi(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			if (ws->ws_row && ws->ws_col)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #ifdef TIOCGWINSZ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	    ws->ws_row && ws->ws_col)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	ws->ws_row = 25;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	ws->ws_col = 80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static void pretty_print_string_list(struct cmdnames *cmds, int longest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	int cols = 1, rows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	int space = longest + 1; /* min 1 SP between words */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct winsize win;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int max_cols;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	get_term_dimensions(&win);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	max_cols = win.ws_col - 1; /* don't print *on* the edge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (space < max_cols)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		cols = max_cols / space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	rows = (cmds->cnt + cols - 1) / cols;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	for (i = 0; i < rows; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		printf("  ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		for (j = 0; j < cols; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			unsigned int n = j * rows + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			unsigned int size = space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			if (n >= cmds->cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			if (j == cols-1 || n + rows >= cmds->cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				size = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			printf("%-*s", size, cmds->names[n]->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		putchar('\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int is_executable(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (stat(name, &st) || /* stat, not lstat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	    !S_ISREG(st.st_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return st.st_mode & S_IXUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int has_extension(const char *filename, const char *ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	size_t len = strlen(filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	size_t extlen = strlen(ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void list_commands_in_dir(struct cmdnames *cmds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 					 const char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 					 const char *prefix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	int prefix_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	DIR *dir = opendir(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	struct dirent *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	char *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (!dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (!prefix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		prefix = "perf-";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	prefix_len = strlen(prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	astrcatf(&buf, "%s/", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	while ((de = readdir(dir)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		int entlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		if (!strstarts(de->d_name, prefix))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		astrcat(&buf, de->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		if (!is_executable(buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		entlen = strlen(de->d_name) - prefix_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		if (has_extension(de->d_name, ".exe"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			entlen -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		add_cmdname(cmds, de->d_name + prefix_len, entlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	closedir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) void load_command_list(const char *prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		struct cmdnames *main_cmds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		struct cmdnames *other_cmds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	const char *env_path = getenv("PATH");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	char *exec_path = get_argv_exec_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (exec_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		list_commands_in_dir(main_cmds, exec_path, prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		qsort(main_cmds->names, main_cmds->cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		      sizeof(*main_cmds->names), cmdname_compare);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		uniq(main_cmds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (env_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		char *paths, *path, *colon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		path = paths = strdup(env_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			if ((colon = strchr(path, ':')))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 				*colon = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			if (!exec_path || strcmp(path, exec_path))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				list_commands_in_dir(other_cmds, path, prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			if (!colon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			path = colon + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		free(paths);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		qsort(other_cmds->names, other_cmds->cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		      sizeof(*other_cmds->names), cmdname_compare);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		uniq(other_cmds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	free(exec_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	exclude_cmds(other_cmds, main_cmds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) void list_commands(const char *title, struct cmdnames *main_cmds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		   struct cmdnames *other_cmds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	unsigned int i, longest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	for (i = 0; i < main_cmds->cnt; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		if (longest < main_cmds->names[i]->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			longest = main_cmds->names[i]->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	for (i = 0; i < other_cmds->cnt; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		if (longest < other_cmds->names[i]->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			longest = other_cmds->names[i]->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (main_cmds->cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		char *exec_path = get_argv_exec_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		printf("available %s in '%s'\n", title, exec_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		printf("----------------");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		mput_char('-', strlen(title) + strlen(exec_path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		putchar('\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		pretty_print_string_list(main_cmds, longest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		putchar('\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		free(exec_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (other_cmds->cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		printf("%s available from elsewhere on your $PATH\n", title);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		printf("---------------------------------------");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		mput_char('-', strlen(title));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		putchar('\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		pretty_print_string_list(other_cmds, longest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		putchar('\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) int is_in_cmdlist(struct cmdnames *c, const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	for (i = 0; i < c->cnt; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		if (!strcmp(s, c->names[i]->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }