2018-12-01 18:10:17 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
2018-12-01 18:16:31 +01:00
|
|
|
|
2018-12-01 18:10:17 +01:00
|
|
|
#include "icmd.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define ICMDS_SIZE \
|
|
|
|
(sizeof(g_icmds) / sizeof(g_icmds[0]))
|
|
|
|
|
|
|
|
#define ICMD(name) \
|
|
|
|
{ \
|
|
|
|
.cmd = TOSTR(name), \
|
2018-12-01 18:16:31 +01:00
|
|
|
.len = sizeof(TOSTR(name))-1, \
|
2018-12-01 18:10:17 +01:00
|
|
|
.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++)
|
|
|
|
{
|
2018-12-01 18:16:31 +01:00
|
|
|
printf("cmp: '%s' : '%s' with len %d \n", g_icmds[i].cmd, line, g_icmds[i].len);
|
2018-12-01 18:10:17 +01:00
|
|
|
if (strncmp(g_icmds[i].cmd, line, g_icmds[i].len) == 0)
|
|
|
|
return g_icmds[i].func(line);
|
|
|
|
}
|
|
|
|
|
2018-12-01 18:16:31 +01:00
|
|
|
/* todo: find better way to signal this? */
|
|
|
|
/* maybe return function pointer? */
|
2018-12-01 18:10:17 +01:00
|
|
|
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;
|
|
|
|
}
|