This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.cc
72 lines (63 loc) · 1.74 KB
/
console.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "module.h"
#include "console.h"
static char buff[1024];
static char mod_prefix[512];
static char mod_path[1024];
const char help_items[] = "\
help print this list\n\
status show status\n\
cand show candidate modules avaliable for loading\n\
load load module\n\
unload unload module\n\
";
void console(void) {
char * mod_name;
{
char szTmp[64];
sprintf(szTmp, "/proc/%d/exe", getpid());
int bytes = readlink(szTmp, mod_prefix, sizeof(mod_prefix));
if (bytes < sizeof(mod_prefix))
bytes = sizeof(mod_prefix) - 1;
if (bytes < 0)
fprintf(stderr, "unable to get current work directory\n");
while (mod_prefix[--bytes] != '/');
bytes++;
strcpy(mod_prefix + bytes, "modules/");
strcpy(mod_path, mod_prefix);
mod_name = mod_path + strlen(mod_path);
}
fprintf(stderr, "module probing directory: %s\n", mod_prefix);
for (;;) {
// prompt
fprintf(stderr, "\n>>> ");
// read
int i = 0;
char c;
while ((c = getchar()) != '\n') {
buff[i] = c;
// prevent overflow
if (i < sizeof(buff)/sizeof(char)) i++;
}
buff[i] = '\0';
// parse & do
if (!strcmp(buff, "quit")) { break; }
if (!strcmp(buff, "help")) { fprintf(stderr, help_items); continue; }
if (!strcmp(buff, "status")) { mod_status(); continue; }
if (!strcmp(buff, "cand")) { mod_candidates(mod_prefix); continue; }
// split two params
i = 0;
while ((c = buff[i]) != '\0') {
if (c == ' ') { buff[i] = '\0'; i++; break; }
i++;
}
const char * name = buff + i;
if (!strcmp(buff, "load")) { strcpy(mod_name, name); mod_doLoad(mod_path); continue; }
if (!strcmp(buff, "unload")) { mod_doUnload(name); continue; }
// else bad command
fprintf(stderr, "Bad Command!\n");
}
return;
}