tweaks: improve several comments, and rewrap two lines

master
Benno Schulenberg 2020-06-18 17:10:05 +02:00
parent d8249917c3
commit 26d7a7494e
1 changed files with 18 additions and 22 deletions

View File

@ -2362,8 +2362,7 @@ bool is_dir(const char *path)
return retval; return retval;
} }
/* We consider the first buf_len characters of buf for ~username tab /* Try to complete the given fragment in 'buf' to a username. */
* completion. */
char **username_tab_completion(const char *buf, size_t *num_matches, char **username_tab_completion(const char *buf, size_t *num_matches,
size_t buf_len) size_t buf_len)
{ {
@ -2371,17 +2370,16 @@ char **username_tab_completion(const char *buf, size_t *num_matches,
#ifdef HAVE_PWD_H #ifdef HAVE_PWD_H
const struct passwd *userdata; const struct passwd *userdata;
/* Iterate through the entries in the passwd file, and add the /* Iterate through the entries in the passwd file, and
* home directory of each username that matches to the list. */ * add each fitting username to the list of matches. */
while ((userdata = getpwent()) != NULL) { while ((userdata = getpwent()) != NULL) {
if (strncmp(userdata->pw_name, buf + 1, buf_len - 1) == 0) { if (strncmp(userdata->pw_name, buf + 1, buf_len - 1) == 0) {
#ifdef ENABLE_OPERATINGDIR #ifdef ENABLE_OPERATINGDIR
/* Skip directories that are beyond the allowed area. */ /* Skip directories that are outside of the allowed area. */
if (outside_of_confinement(userdata->pw_dir, TRUE)) if (outside_of_confinement(userdata->pw_dir, TRUE))
continue; continue;
#endif #endif
matches = (char **)nrealloc(matches, (*num_matches + 1) * matches = (char **)nrealloc(matches, (*num_matches + 1) * sizeof(char *));
sizeof(char *));
matches[*num_matches] = charalloc(strlen(userdata->pw_name) + 2); matches[*num_matches] = charalloc(strlen(userdata->pw_name) + 2);
sprintf(matches[*num_matches], "~%s", userdata->pw_name); sprintf(matches[*num_matches], "~%s", userdata->pw_name);
++(*num_matches); ++(*num_matches);
@ -2394,8 +2392,8 @@ char **username_tab_completion(const char *buf, size_t *num_matches,
return matches; return matches;
} }
/* The next two functions, cwd_tab_completion() and input_tab(), were adapted /* The next two functions were adapted from busybox 0.46 (cmdedit.c).
* from busybox 0.46 (cmdedit.c). Here is the tweaked notice from that file: * Here is the tweaked notice from that file:
* *
* Termios command-line History and Editing, originally intended for NetBSD. * Termios command-line History and Editing, originally intended for NetBSD.
* Copyright (C) 1999, 2000 * Copyright (C) 1999, 2000
@ -2408,8 +2406,7 @@ char **username_tab_completion(const char *buf, size_t *num_matches,
* This code is 'as is' with no warranty. * This code is 'as is' with no warranty.
* This code may safely be consumed by a BSD or GPL license. */ * This code may safely be consumed by a BSD or GPL license. */
/* We consider the first buf_len characters of buf for filename tab /* Try to complete the given fragment in 'buf' to a filename. */
* completion. */
char **cwd_tab_completion(const char *buf, bool allow_files, char **cwd_tab_completion(const char *buf, bool allow_files,
size_t *num_matches, size_t buf_len) size_t *num_matches, size_t buf_len)
{ {
@ -2456,7 +2453,7 @@ char **cwd_tab_completion(const char *buf, bool allow_files,
filenamelen = strlen(filename); filenamelen = strlen(filename);
/* Iterate through the filenames in the directory, /* Iterate through the filenames in the directory,
* and add the ones that match to the list. */ * and add each fitting one to the list of matches. */
while ((nextdir = readdir(dir)) != NULL) { while ((nextdir = readdir(dir)) != NULL) {
bool skip_match = FALSE; bool skip_match = FALSE;
@ -2477,8 +2474,7 @@ char **cwd_tab_completion(const char *buf, bool allow_files,
if (skip_match) if (skip_match)
continue; continue;
matches = (char **)nrealloc(matches, (*num_matches + 1) * matches = (char **)nrealloc(matches, (*num_matches + 1) * sizeof(char *));
sizeof(char *));
matches[*num_matches] = copy_of(nextdir->d_name); matches[*num_matches] = copy_of(nextdir->d_name);
++(*num_matches); ++(*num_matches);
} }
@ -2491,9 +2487,8 @@ char **cwd_tab_completion(const char *buf, bool allow_files,
return matches; return matches;
} }
/* Do tab completion. place refers to how much the status-bar cursor /* Do tab completion. 'place' is the position of the status-bar cursor, and
* position should be advanced. refresh_func is the function we will * 'refresh_func' is the function to be called to refresh the edit window. */
* call to refresh the edit window. */
char *input_tab(char *buf, bool allow_files, size_t *place, char *input_tab(char *buf, bool allow_files, size_t *place,
bool *lastwastab, void (*refresh_func)(void), bool *listed) bool *lastwastab, void (*refresh_func)(void), bool *listed)
{ {
@ -2508,12 +2503,13 @@ char *input_tab(char *buf, bool allow_files, size_t *place,
return buf; return buf;
} }
/* If the word starts with `~' and there is no slash in the word, /* If the fragment starts with a tilde and contains no slash,
* then try completing this word as a username. */ * then try completing it as a username. */
if (buf[0] == '~' && strchr(buf, '/') == NULL) if (buf[0] == '~' && strchr(buf, '/') == NULL)
matches = username_tab_completion(buf, &num_matches, *place); matches = username_tab_completion(buf, &num_matches, *place);
/* If nothing matched yet, match against filenames in current directory. */ /* If there are no matches yet, try matching against filenames
* in the current directory. */
if (matches == NULL) if (matches == NULL)
matches = cwd_tab_completion(buf, allow_files, &num_matches, *place); matches = cwd_tab_completion(buf, allow_files, &num_matches, *place);