2021-07-30 22:14:16 -04:00
|
|
|
#include "exec.h"
|
|
|
|
|
2021-08-09 13:52:52 -04:00
|
|
|
#ifdef LIBSCOMP_FREESTANDING
|
2021-07-30 22:14:16 -04:00
|
|
|
int strcmp(const char *s1, const char *s2) {
|
|
|
|
while (*s1 == *s2) {
|
|
|
|
if (!*s1)
|
|
|
|
break;
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *s1 - *s2;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2021-08-09 13:52:52 -04:00
|
|
|
struct libscomp_cmd *hg_bsearch(const char *name,
|
|
|
|
const struct libscomp_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;
|
|
|
|
}
|
|
|
|
|
2021-08-09 13:52:52 -04:00
|
|
|
int libscomp_exec(const struct libscomp_cmd_store *cmds,
|
|
|
|
struct libscomp_line *ln, void *ptr) {
|
|
|
|
struct libscomp_cmd *cmd;
|
2021-07-30 22:14:16 -04:00
|
|
|
|
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)
|
2021-08-09 13:52:52 -04:00
|
|
|
return LIBSCOMP_NOT_FOUND;
|
2021-07-31 20:36:22 -04:00
|
|
|
return cmd->exec(ln,ptr);
|
2021-07-30 22:14:16 -04:00
|
|
|
}
|