psh/src/icmd.c
2018-12-01 18:16:31 +01:00

69 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "icmd.h"
#include "common.h"
#define ICMDS_SIZE \
(sizeof(g_icmds) / sizeof(g_icmds[0]))
#define ICMD(name) \
{ \
.cmd = TOSTR(name), \
.len = sizeof(TOSTR(name))-1, \
.func = CONCAT(icmd_, name), \
}
struct icmd g_icmds[] =
{
ICMD(exit),
ICMD(cd),
ICMD(ls),
};
int
call_icmds(char *line)
{
size_t i;
for (i = 0; i < ICMDS_SIZE; i++)
{
printf("cmp: '%s' : '%s' with len %d \n", g_icmds[i].cmd, line, g_icmds[i].len);
if (strncmp(g_icmds[i].cmd, line, g_icmds[i].len) == 0)
return g_icmds[i].func(line);
}
/* todo: find better way to signal this? */
/* maybe return function pointer? */
return INT_MAX;
}
int
icmd_exit(char *line)
{
printf("exiting with reason: %s", line);
exit(0);
}
int
icmd_cd(char *line)
{
printf("I'm a cd! line: %s\n", line);
return 0;
}
int
icmd_ls(char *line)
{
printf("I'm a ls! line: %s\n", line);
return 1;
}