-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.c
111 lines (93 loc) · 2.36 KB
/
menu.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "vma.h"
#include "menu.h"
void write_menu(arena_t *arena)
{
uint64_t address;
scanf("%ld", &address);
uint64_t size;
scanf("%ld", &size);
uint8_t leading_space;
scanf("%c", &leading_space);
int8_t *data = malloc(size + 1);
DIE(!data, "write_menu(): malloc() failed\n");
char ch;
for (uint64_t i = 0; i < size; i++) {
scanf("%c", &ch);
data[i] = ch;
}
data[size] = '\0';
write(arena, address, size, data);
free(data);
}
void mprotect_menu(arena_t *arena)
{
uint64_t address;
scanf("%ld", &address);
uint8_t leading_space;
scanf("%c", &leading_space);
int8_t *permission = malloc(L_MAX);
DIE(!permission, "menu(): malloc() failed\n");
fgets((char *)permission, L_MAX, stdin);
if (permission[strlen((char *)permission) - 1] == '\n')
permission[strlen((char *)permission) - 1] = '\0';
mprotect(arena, address, permission);
free(permission);
}
void invalid_menu(void)
{
printf("Invalid command. Please try again.\n");
char *rest_of_cmd = malloc(L_MAX);
DIE(!rest_of_cmd, "invalid_menu(): malloc() failed\n");
fgets(rest_of_cmd, L_MAX, stdin);
for (size_t i = 0; i < strlen(rest_of_cmd); i++)
if (rest_of_cmd[i] == '\n')
rest_of_cmd[i] = '\0';
char *p = strtok(rest_of_cmd, " ");
while (p) {
printf("Invalid command. Please try again.\n");
p = strtok(NULL, " ");
}
free(rest_of_cmd);
}
int menu(void)
{
arena_t *arena = NULL;
char *cmd = malloc(CMD_MAX);
DIE(!cmd, "menu(): malloc() failed\n");
while (scanf("%s", cmd)) {
if (strcmp(cmd, "ALLOC_ARENA") == 0) {
uint64_t size;
scanf("%ld", &size);
arena = alloc_arena(size);
} else if (strcmp(cmd, "DEALLOC_ARENA") == 0) {
dealloc_arena(arena);
free(cmd);
return 0;
} else if (strcmp(cmd, "ALLOC_BLOCK") == 0) {
uint64_t address;
scanf("%ld", &address);
uint64_t size;
scanf("%ld", &size);
alloc_block(arena, address, size);
} else if (strcmp(cmd, "FREE_BLOCK") == 0) {
uint64_t address;
scanf("%ld", &address);
free_block(arena, address);
} else if (strcmp(cmd, "READ") == 0) {
uint64_t address;
scanf("%ld", &address);
uint64_t size;
scanf("%ld", &size);
read(arena, address, size);
} else if (strcmp(cmd, "WRITE") == 0) {
write_menu(arena);
} else if (strcmp(cmd, "PMAP") == 0) {
pmap(arena);
} else if (strcmp(cmd, "MPROTECT") == 0) {
mprotect_menu(arena);
} else {
invalid_menu();
}
}
return 0;
}