libscomp/exec.c

46 lines
851 B
C

#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) {
size_t lower = 0, upper = cmds->len-1;
size_t i;
int rel;
while (upper >= lower) {
i = (upper - lower)/2 + lower;
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) {
struct libscomm_cmd *cmd;
cmd = hg_bsearch(ln->buf[ln->name], cmds);
if (!cmd)
return LIBSCOMM_NOT_FOUND;
return cmd->exec(ln);
}