tools/lib/subcmd/help.c

Source file repositories/reference/linux-study-clean/tools/lib/subcmd/help.c

File Facts

System
Linux kernel
Corpus path
tools/lib/subcmd/help.c
Extension
.c
Size
6291 bytes
Lines
302
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (cmds->names[i]) {
			if (i == j)
				j++;
			else
				cmds->names[j++] = cmds->names[i];
		}
	}
	cmds->cnt = j;
	while (j < i)
		cmds->names[j++] = NULL;
}

void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
{
	size_t ci, cj, ei;
	int cmp;

	if (!excludes->cnt)
		return;

	ci = cj = ei = 0;
	while (ci < cmds->cnt && ei < excludes->cnt) {
		cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
		if (cmp < 0) {
			if (ci == cj) {
				ci++;
				cj++;
			} else {
				cmds->names[cj++] = cmds->names[ci];
				cmds->names[ci++] = NULL;
			}
		} else if (cmp == 0) {
			zfree(&cmds->names[ci]);
			ci++;
			ei++;
		} else if (cmp > 0) {
			ei++;
		}
	}
	while (ci < cmds->cnt) {
		if (ci != cj) {
			cmds->names[cj] = cmds->names[ci];
			cmds->names[ci] = NULL;
		}
		ci++;
		cj++;
	}
	for (ci = cj; ci < cmds->cnt; ci++)
		assert(cmds->names[ci] == NULL);
	cmds->cnt = cj;
}

static void get_term_dimensions(struct winsize *ws)
{
	char *s = getenv("LINES");

	if (s != NULL) {
		ws->ws_row = atoi(s);
		s = getenv("COLUMNS");
		if (s != NULL) {
			ws->ws_col = atoi(s);
			if (ws->ws_row && ws->ws_col)
				return;
		}
	}
#ifdef TIOCGWINSZ
	if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
	    ws->ws_row && ws->ws_col)
		return;
#endif
	ws->ws_row = 25;
	ws->ws_col = 80;
}

static void pretty_print_string_list(struct cmdnames *cmds, int longest)
{
	int cols = 1, rows;
	int space = longest + 1; /* min 1 SP between words */
	struct winsize win;
	int max_cols;
	int i, j;

	get_term_dimensions(&win);
	max_cols = win.ws_col - 1; /* don't print *on* the edge */

	if (space < max_cols)
		cols = max_cols / space;
	rows = (cmds->cnt + cols - 1) / cols;

	for (i = 0; i < rows; i++) {

Annotation

Implementation Notes