66 lines
1.1 KiB
C
66 lines
1.1 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)), \
|
||
|
.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 \n", g_icmds[i].cmd, line);
|
||
|
if (strncmp(g_icmds[i].cmd, line, g_icmds[i].len) == 0)
|
||
|
return g_icmds[i].func(line);
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|