0001
0002 #include <stdio.h>
0003 #include <stdlib.h>
0004 #include <string.h>
0005 #include <linux/string.h>
0006 #include <termios.h>
0007 #include <sys/ioctl.h>
0008 #include <sys/types.h>
0009 #include <sys/stat.h>
0010 #include <unistd.h>
0011 #include <dirent.h>
0012 #include "subcmd-util.h"
0013 #include "help.h"
0014 #include "exec-cmd.h"
0015
0016 void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
0017 {
0018 struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
0019
0020 ent->len = len;
0021 memcpy(ent->name, name, len);
0022 ent->name[len] = 0;
0023
0024 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
0025 cmds->names[cmds->cnt++] = ent;
0026 }
0027
0028 void clean_cmdnames(struct cmdnames *cmds)
0029 {
0030 unsigned int i;
0031
0032 for (i = 0; i < cmds->cnt; ++i)
0033 zfree(&cmds->names[i]);
0034 zfree(&cmds->names);
0035 cmds->cnt = 0;
0036 cmds->alloc = 0;
0037 }
0038
0039 int cmdname_compare(const void *a_, const void *b_)
0040 {
0041 struct cmdname *a = *(struct cmdname **)a_;
0042 struct cmdname *b = *(struct cmdname **)b_;
0043 return strcmp(a->name, b->name);
0044 }
0045
0046 void uniq(struct cmdnames *cmds)
0047 {
0048 unsigned int i, j;
0049
0050 if (!cmds->cnt)
0051 return;
0052
0053 for (i = j = 1; i < cmds->cnt; i++)
0054 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
0055 cmds->names[j++] = cmds->names[i];
0056
0057 cmds->cnt = j;
0058 }
0059
0060 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
0061 {
0062 size_t ci, cj, ei;
0063 int cmp;
0064
0065 ci = cj = ei = 0;
0066 while (ci < cmds->cnt && ei < excludes->cnt) {
0067 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
0068 if (cmp < 0) {
0069 cmds->names[cj++] = cmds->names[ci++];
0070 } else if (cmp == 0) {
0071 ci++;
0072 ei++;
0073 } else if (cmp > 0) {
0074 ei++;
0075 }
0076 }
0077
0078 while (ci < cmds->cnt)
0079 cmds->names[cj++] = cmds->names[ci++];
0080
0081 cmds->cnt = cj;
0082 }
0083
0084 static void get_term_dimensions(struct winsize *ws)
0085 {
0086 char *s = getenv("LINES");
0087
0088 if (s != NULL) {
0089 ws->ws_row = atoi(s);
0090 s = getenv("COLUMNS");
0091 if (s != NULL) {
0092 ws->ws_col = atoi(s);
0093 if (ws->ws_row && ws->ws_col)
0094 return;
0095 }
0096 }
0097 #ifdef TIOCGWINSZ
0098 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
0099 ws->ws_row && ws->ws_col)
0100 return;
0101 #endif
0102 ws->ws_row = 25;
0103 ws->ws_col = 80;
0104 }
0105
0106 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
0107 {
0108 int cols = 1, rows;
0109 int space = longest + 1;
0110 struct winsize win;
0111 int max_cols;
0112 int i, j;
0113
0114 get_term_dimensions(&win);
0115 max_cols = win.ws_col - 1;
0116
0117 if (space < max_cols)
0118 cols = max_cols / space;
0119 rows = (cmds->cnt + cols - 1) / cols;
0120
0121 for (i = 0; i < rows; i++) {
0122 printf(" ");
0123
0124 for (j = 0; j < cols; j++) {
0125 unsigned int n = j * rows + i;
0126 unsigned int size = space;
0127
0128 if (n >= cmds->cnt)
0129 break;
0130 if (j == cols-1 || n + rows >= cmds->cnt)
0131 size = 1;
0132 printf("%-*s", size, cmds->names[n]->name);
0133 }
0134 putchar('\n');
0135 }
0136 }
0137
0138 static int is_executable(const char *name)
0139 {
0140 struct stat st;
0141
0142 if (stat(name, &st) ||
0143 !S_ISREG(st.st_mode))
0144 return 0;
0145
0146 return st.st_mode & S_IXUSR;
0147 }
0148
0149 static int has_extension(const char *filename, const char *ext)
0150 {
0151 size_t len = strlen(filename);
0152 size_t extlen = strlen(ext);
0153
0154 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
0155 }
0156
0157 static void list_commands_in_dir(struct cmdnames *cmds,
0158 const char *path,
0159 const char *prefix)
0160 {
0161 int prefix_len;
0162 DIR *dir = opendir(path);
0163 struct dirent *de;
0164 char *buf = NULL;
0165
0166 if (!dir)
0167 return;
0168 if (!prefix)
0169 prefix = "perf-";
0170 prefix_len = strlen(prefix);
0171
0172 astrcatf(&buf, "%s/", path);
0173
0174 while ((de = readdir(dir)) != NULL) {
0175 int entlen;
0176
0177 if (!strstarts(de->d_name, prefix))
0178 continue;
0179
0180 astrcat(&buf, de->d_name);
0181 if (!is_executable(buf))
0182 continue;
0183
0184 entlen = strlen(de->d_name) - prefix_len;
0185 if (has_extension(de->d_name, ".exe"))
0186 entlen -= 4;
0187
0188 add_cmdname(cmds, de->d_name + prefix_len, entlen);
0189 }
0190 closedir(dir);
0191 free(buf);
0192 }
0193
0194 void load_command_list(const char *prefix,
0195 struct cmdnames *main_cmds,
0196 struct cmdnames *other_cmds)
0197 {
0198 const char *env_path = getenv("PATH");
0199 char *exec_path = get_argv_exec_path();
0200
0201 if (exec_path) {
0202 list_commands_in_dir(main_cmds, exec_path, prefix);
0203 qsort(main_cmds->names, main_cmds->cnt,
0204 sizeof(*main_cmds->names), cmdname_compare);
0205 uniq(main_cmds);
0206 }
0207
0208 if (env_path) {
0209 char *paths, *path, *colon;
0210 path = paths = strdup(env_path);
0211 while (1) {
0212 if ((colon = strchr(path, ':')))
0213 *colon = 0;
0214 if (!exec_path || strcmp(path, exec_path))
0215 list_commands_in_dir(other_cmds, path, prefix);
0216
0217 if (!colon)
0218 break;
0219 path = colon + 1;
0220 }
0221 free(paths);
0222
0223 qsort(other_cmds->names, other_cmds->cnt,
0224 sizeof(*other_cmds->names), cmdname_compare);
0225 uniq(other_cmds);
0226 }
0227 free(exec_path);
0228 exclude_cmds(other_cmds, main_cmds);
0229 }
0230
0231 void list_commands(const char *title, struct cmdnames *main_cmds,
0232 struct cmdnames *other_cmds)
0233 {
0234 unsigned int i, longest = 0;
0235
0236 for (i = 0; i < main_cmds->cnt; i++)
0237 if (longest < main_cmds->names[i]->len)
0238 longest = main_cmds->names[i]->len;
0239 for (i = 0; i < other_cmds->cnt; i++)
0240 if (longest < other_cmds->names[i]->len)
0241 longest = other_cmds->names[i]->len;
0242
0243 if (main_cmds->cnt) {
0244 char *exec_path = get_argv_exec_path();
0245 printf("available %s in '%s'\n", title, exec_path);
0246 printf("----------------");
0247 mput_char('-', strlen(title) + strlen(exec_path));
0248 putchar('\n');
0249 pretty_print_string_list(main_cmds, longest);
0250 putchar('\n');
0251 free(exec_path);
0252 }
0253
0254 if (other_cmds->cnt) {
0255 printf("%s available from elsewhere on your $PATH\n", title);
0256 printf("---------------------------------------");
0257 mput_char('-', strlen(title));
0258 putchar('\n');
0259 pretty_print_string_list(other_cmds, longest);
0260 putchar('\n');
0261 }
0262 }
0263
0264 int is_in_cmdlist(struct cmdnames *c, const char *s)
0265 {
0266 unsigned int i;
0267
0268 for (i = 0; i < c->cnt; i++)
0269 if (!strcmp(s, c->names[i]->name))
0270 return 1;
0271 return 0;
0272 }