refactor(misc1): move user related code to os/users.c

This commit is contained in:
Björn Linse 2021-12-10 15:29:45 +01:00
parent 22d7dd2aec
commit 8b316b18d2
2 changed files with 60 additions and 63 deletions

View File

@ -60,8 +60,6 @@
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "misc1.c.generated.h" # include "misc1.c.generated.h"
#endif #endif
// All user names (for ~user completion as done by shell).
static garray_T ga_users = GA_EMPTY_INIT_VALUE;
/* /*
* get_leader_len() returns the length in bytes of the prefix of the given * get_leader_len() returns the length in bytes of the prefix of the given
@ -727,67 +725,6 @@ void vim_beep(unsigned val)
} }
} }
#if defined(EXITFREE)
void free_users(void)
{
ga_clear_strings(&ga_users);
}
#endif
/*
* Find all user names for user completion.
* Done only once and then cached.
*/
static void init_users(void)
{
static int lazy_init_done = FALSE;
if (lazy_init_done) {
return;
}
lazy_init_done = TRUE;
os_get_usernames(&ga_users);
}
/*
* Function given to ExpandGeneric() to obtain an user names.
*/
char_u *get_users(expand_T *xp, int idx)
{
init_users();
if (idx < ga_users.ga_len) {
return ((char_u **)ga_users.ga_data)[idx];
}
return NULL;
}
/*
* Check whether name matches a user name. Return:
* 0 if name does not match any user name.
* 1 if name partially matches the beginning of a user name.
* 2 is name fully matches a user name.
*/
int match_user(char_u *name)
{
int n = (int)STRLEN(name);
int result = 0;
init_users();
for (int i = 0; i < ga_users.ga_len; i++) {
if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) {
return 2; // full match
}
if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) {
result = 1; // partial match
}
}
return result;
}
/// os_call_shell() wrapper. Handles 'verbose', :profile, and v:shell_error. /// os_call_shell() wrapper. Handles 'verbose', :profile, and v:shell_error.
/// Invalidates cached tags. /// Invalidates cached tags.
/// ///

View File

@ -18,6 +18,9 @@
# include <lm.h> # include <lm.h>
#endif #endif
// All user names (for ~user completion as done by shell).
static garray_T ga_users = GA_EMPTY_INIT_VALUE;
// Add a user name to the list of users in garray_T *users. // Add a user name to the list of users in garray_T *users.
// Do nothing if user name is NULL or empty. // Do nothing if user name is NULL or empty.
static void add_user(garray_T *users, char *user, bool need_copy) static void add_user(garray_T *users, char *user, bool need_copy)
@ -157,3 +160,60 @@ char *os_get_user_directory(const char *name)
return NULL; return NULL;
} }
#if defined(EXITFREE)
void free_users(void)
{
ga_clear_strings(&ga_users);
}
#endif
/// Find all user names for user completion.
///
/// Done only once and then cached.
static void init_users(void)
{
static int lazy_init_done = false;
if (lazy_init_done) {
return;
}
lazy_init_done = true;
os_get_usernames(&ga_users);
}
/// Given to ExpandGeneric() to obtain an user names.
char_u *get_users(expand_T *xp, int idx)
{
init_users();
if (idx < ga_users.ga_len) {
return ((char_u **)ga_users.ga_data)[idx];
}
return NULL;
}
/// Check whether name matches a user name.
///
/// @return 0 if name does not match any user name.
/// 1 if name partially matches the beginning of a user name.
/// 2 is name fully matches a user name.
int match_user(char_u *name)
{
int n = (int)STRLEN(name);
int result = 0;
init_users();
for (int i = 0; i < ga_users.ga_len; i++) {
if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) {
return 2; // full match
}
if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) {
result = 1; // partial match
}
}
return result;
}