Skip to content

Commit

Permalink
indent -kr compl.c
Browse files Browse the repository at this point in the history
  • Loading branch information
stsp committed Mar 23, 2024
1 parent db990de commit fc1145a
Showing 1 changed file with 117 additions and 119 deletions.
236 changes: 117 additions & 119 deletions src/compl.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,164 +25,162 @@
#include "compl.h"

struct compl_s {
void *opaque;
const char *(*get_name)(int idx, void *arg);
int (*get_num)(void *arg);
void *opaque;
const char *(*get_name)(int idx, void *arg);
int (*get_num)(void *arg);
};

#define MAX_COMPLS 10
struct cmpl_s {
int num;
struct compl_s compls[MAX_COMPLS];
int num;
struct compl_s compls[MAX_COMPLS];
};

static int cmpstr(const char *s1, const char *s2)
{
int cnt = 0;
while (s1[cnt] && s2[cnt] && s1[cnt] == s2[cnt])
cnt++;
return cnt;
int cnt = 0;
while (s1[cnt] && s2[cnt] && s1[cnt] == s2[cnt])
cnt++;
return cnt;
}

static const char *get_cmd_name(int idx, void *arg)
{
struct built_in_cmd *cmd = arg;
return cmd[idx].cmd_name;
struct built_in_cmd *cmd = arg;
return cmd[idx].cmd_name;
}

static int get_cmd_num(void *arg)
{
return CMD_TABLE_COUNT;
return CMD_TABLE_COUNT;
}

static const char *get_fname(int idx, void *arg)
{
glob_t *gl = arg;
if (idx >= gl->gl_pathc)
return NULL;
return gl->gl_pathv[idx];
glob_t *gl = arg;
if (idx >= gl->gl_pathc)
return NULL;
return gl->gl_pathv[idx];
}

static int get_glb_num(void *arg)
{
glob_t *gl = arg;
return gl->gl_pathc;
glob_t *gl = arg;
return gl->gl_pathc;
}

static const char *get_compl_name(int idx, void *arg)
{
struct cmpl_s *cmpl = arg;
int i;

for (i = 0; i < cmpl->num; i++) {
struct compl_s *c = &cmpl->compls[i];
int num = c->get_num(c->opaque);
if (idx >= num) {
idx -= num;
continue;
struct cmpl_s *cmpl = arg;
int i;

for (i = 0; i < cmpl->num; i++) {
struct compl_s *c = &cmpl->compls[i];
int num = c->get_num(c->opaque);
if (idx >= num) {
idx -= num;
continue;
}
return c->get_name(idx, c->opaque);
}
return c->get_name(idx, c->opaque);
}
return NULL;
return NULL;
}

static int do_compl(const char *prefix, int print, int *r_len,
char *r_p, const char *(*get)(int idx, void *arg), void *arg,
int num)
{
int i, cnt = 0, idx = -1, len = strlen(prefix);
char suff[MAX_CMD_BUFLEN] = "";

for (i = 0; i < num; i++)
{
const char *c = get(i, arg);
if (strncmp(prefix, c, len) == 0)
{
const char *p = c + len;
int l = cmpstr(p, suff);

strcpy(suff, p);
if (cnt)
suff[l] = '\0';
cnt++;
idx = i;
if (print)
puts(c);
}
char *r_p, const char *(*get)(int idx, void *arg),
void *arg, int num)
{
int i, cnt = 0, idx = -1, len = strlen(prefix);
char suff[MAX_CMD_BUFLEN] = "";

for (i = 0; i < num; i++) {
const char *c = get(i, arg);
if (strncmp(prefix, c, len) == 0) {
const char *p = c + len;
int l = cmpstr(p, suff);

strcpy(suff, p);
if (cnt)
suff[l] = '\0';
cnt++;
idx = i;
if (print)
puts(c);
}
}
if (cnt == 0)
return -1;
*r_len = strlen(suff);
strcpy(r_p, get(idx, arg) + len);
if (cnt == 1)
return 1;
return 0;
}
if (cnt == 0)
return -1;
*r_len = strlen(suff);
strcpy(r_p, get(idx, arg) + len);
if (cnt == 1)
return 1;
return 0;
}

static int glb_add(struct cmpl_s *cmpl, glob_t *gl)
{
struct compl_s *c = &cmpl->compls[cmpl->num++];
struct compl_s *c = &cmpl->compls[cmpl->num++];

c->opaque = gl;
c->get_name = get_fname;
c->get_num = get_glb_num;
return gl->gl_pathc;
c->opaque = gl;
c->get_name = get_fname;
c->get_num = get_glb_num;
return gl->gl_pathc;
}

int compl_cmds(const char *prefix, int print, int *r_len, char *r_p)
{
char buf[MAXPATH];
struct cmpl_s cmpl = {};
glob_t gl_bat, gl_exe, gl_com;
int err, ret = -1, cnt = 0;

snprintf(buf, MAXPATH, "%s*.bat", prefix);
err = glob(buf, GLOB_ERR, NULL, &gl_bat);
if (err && err != GLOB_NOMATCH)
return -1;
if (!err)
cnt += glb_add(&cmpl, &gl_bat);
snprintf(buf, MAXPATH, "%s*.exe", prefix);
err = glob(buf, GLOB_ERR, NULL, &gl_exe);
if (err && err != GLOB_NOMATCH)
goto err1;
if (!err)
cnt += glb_add(&cmpl, &gl_exe);
snprintf(buf, MAXPATH, "%s*.com", prefix);
err = glob(buf, GLOB_ERR, NULL, &gl_com);
if (err && err != GLOB_NOMATCH)
goto err2;
if (!err)
cnt += glb_add(&cmpl, &gl_com);

cmpl.compls[cmpl.num].opaque = cmd_table;
cmpl.compls[cmpl.num].get_name = get_cmd_name;
cmpl.compls[cmpl.num].get_num = get_cmd_num;
cmpl.num++;

ret = do_compl(prefix, print, r_len, r_p, get_compl_name, &cmpl,
CMD_TABLE_COUNT + cnt);

globfree(&gl_com);
err2:
globfree(&gl_exe);
err1:
globfree(&gl_bat);

return ret;
}
{
char buf[MAXPATH];
struct cmpl_s cmpl = { };
glob_t gl_bat, gl_exe, gl_com;
int err, ret = -1, cnt = 0;

snprintf(buf, MAXPATH, "%s*.bat", prefix);
err = glob(buf, GLOB_ERR, NULL, &gl_bat);
if (err && err != GLOB_NOMATCH)
return -1;
if (!err)
cnt += glb_add(&cmpl, &gl_bat);
snprintf(buf, MAXPATH, "%s*.exe", prefix);
err = glob(buf, GLOB_ERR, NULL, &gl_exe);
if (err && err != GLOB_NOMATCH)
goto err1;
if (!err)
cnt += glb_add(&cmpl, &gl_exe);
snprintf(buf, MAXPATH, "%s*.com", prefix);
err = glob(buf, GLOB_ERR, NULL, &gl_com);
if (err && err != GLOB_NOMATCH)
goto err2;
if (!err)
cnt += glb_add(&cmpl, &gl_com);

cmpl.compls[cmpl.num].opaque = cmd_table;
cmpl.compls[cmpl.num].get_name = get_cmd_name;
cmpl.compls[cmpl.num].get_num = get_cmd_num;
cmpl.num++;

ret = do_compl(prefix, print, r_len, r_p, get_compl_name, &cmpl,
CMD_TABLE_COUNT + cnt);

globfree(&gl_com);
err2:
globfree(&gl_exe);
err1:
globfree(&gl_bat);

return ret;
}

int compl_fname(const char *prefix, int print, int *r_len, char *r_p)
{
char buf[MAXPATH];
glob_t gl;
int err, ret;

snprintf(buf, MAXPATH, "%s*", prefix);
err = glob(buf, GLOB_ERR, NULL, &gl);
if (err)
return -1;
ret = do_compl(prefix, print, r_len, r_p, get_fname, &gl, gl.gl_pathc);
globfree(&gl);
return ret;
}
{
char buf[MAXPATH];
glob_t gl;
int err, ret;

snprintf(buf, MAXPATH, "%s*", prefix);
err = glob(buf, GLOB_ERR, NULL, &gl);
if (err)
return -1;
ret = do_compl(prefix, print, r_len, r_p, get_fname, &gl, gl.gl_pathc);
globfree(&gl);
return ret;
}

0 comments on commit fc1145a

Please sign in to comment.