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