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