libscomp/exec.c

48 lines
916 B
C
Raw Normal View History

2021-07-30 22:14:16 -04:00
#include "exec.h"
#ifdef LIBSCOMM_FREESTANDING
int strcmp(const char *s1, const char *s2) {
while (*s1 == *s2) {
if (!*s1)
break;
s1++;
s2++;
}
return *s1 - *s2;
}
#else
# include <string.h>
#endif
struct libscomm_cmd *hg_bsearch(const char *name,
const struct libscomm_cmd_store *cmds) {
2021-07-30 22:14:16 -04:00
size_t lower = 0, upper = cmds->len-1;
size_t i;
int rel;
2021-07-30 23:57:22 -04:00
while (upper >= lower) {
i = (upper - lower)/2 + lower;
2021-07-30 22:14:16 -04:00
rel = strcmp(name, cmds->arr[i].name);
/* x > cmds[i] */
if (rel > 0)
lower = i + 1;
/* x < cmds[i] */
else if (rel < 0)
upper = i - 1;
else
return &cmds->arr[i];
}
return NULL;
}
int libscomm_exec(const struct libscomm_cmd_store *cmds,
struct libscomm_line *ln, void *ptr) {
2021-07-30 22:14:16 -04:00
struct libscomm_cmd *cmd;
2021-07-30 23:57:22 -04:00
cmd = hg_bsearch(ln->buf[ln->name], cmds);
2021-07-30 22:14:16 -04:00
if (!cmd)
return LIBSCOMM_NOT_FOUND;
return cmd->exec(ln,ptr);
2021-07-30 22:14:16 -04:00
}