-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellmemory.c
More file actions
185 lines (164 loc) · 4.68 KB
/
Copy pathshellmemory.c
File metadata and controls
185 lines (164 loc) · 4.68 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shell.h"
struct memory_struct {
char *var;
char *value;
};
// 0 - FRAME_STORE_SIZE: reserved for frames
// FRAME_STORE_SIZE - FRAME_STORE_SIZE + VAR_STORE_SIZE: reserved for variables
struct memory_struct shellmemory[FRAME_STORE_SIZE + VAR_STORE_SIZE];
int FRAME_VAR_BORDER = FRAME_STORE_SIZE;
// Helper functions
int match(char *model, char *var) {
int i, len = strlen(var), matchCount = 0;
for (i = 0; i < len; i++)
if (*(model + i) == *(var + i))
matchCount++;
return matchCount == len ? 1 : 0;
}
char *extract(char *model) {
char token = '='; // look for this to find value
char value[1000]; // stores the extract value
int i, j, len = strlen(model);
for (i = 0; i < len && *(model + i) != token; i++)
; // loop till we get there
// extract the value
for (i = i + 1, j = 0; i < len; i++, j++)
value[j] = *(model + i);
value[j] = '\0';
return strdup(value);
}
// Shell memory functions
void mem_init() {
for (int i = 0; i < FRAME_STORE_SIZE + VAR_STORE_SIZE; i++) {
shellmemory[i].var = "none";
shellmemory[i].value = "none";
}
}
/**
* @brief clears the memory slot at a given position
*
* @param pos mempory position
*/
void mem_free_at(int pos) {
shellmemory[pos].var = strdup("none"),
shellmemory[pos].value = strdup("none");
}
/**
* @brief allocate a frame in the physical memory
*
* @param pid
*
* @return int** NULL: unable to allocate a space; otherwise: successfully
* allocated a space, the starting position is n(0 <= n <= 899)
*/
int frame_alloc(const char *pid, int *index, int *valid_bit) {
int i, j, k;
for (i = 0; i < FRAME_VAR_BORDER; i++) {
// check if there is a contiguous space of enough size
for (j = i; j < i + 3 && j < FRAME_VAR_BORDER; j++) {
// if no, break
if (strcmp(shellmemory[j].var, "none") != 0) {
break;
}
}
// if yes, mark this contiguous space as "occupied"
if (j == i + 3) {
for (k = i; k < i + 3; k++) {
shellmemory[k].var = strdup(pid);
index[k - i] = k, valid_bit[k - i] = 0;
}
return 0;
}
}
return 1; // unable to find a contiguous space in the memory
}
/**
* @brief store a key-value pair at a given position
*
* @param pos the position in the memory
* @param pid the id of the process
* @param value_in the command
* @param valid_bit
*/
void mem_set_value_at(int pos, const char *pid, const char *value_in,
int *valid_bit) {
shellmemory[pos].var = strdup(pid);
shellmemory[pos].value = strdup(value_in);
(*valid_bit) = 1; // set valid bit to 1
}
/**
* @brief get the value stored at a given position in the memory
*
* @param pos the memory position
* @return char* the value stored in that position
*/
char *mem_get_value_at(int pos) { return strdup(shellmemory[pos].value); }
// Set key value pair
void mem_set_value(char *var_in, char *value_in) {
int i;
for (i = FRAME_VAR_BORDER; i < FRAME_STORE_SIZE + VAR_STORE_SIZE; i++) {
if (strcmp(shellmemory[i].var, var_in) == 0) {
shellmemory[i].value = strdup(value_in);
return;
}
}
// Value does not exist, need to find a free spot.
for (i = FRAME_VAR_BORDER; i < FRAME_STORE_SIZE + VAR_STORE_SIZE; i++) {
if (strcmp(shellmemory[i].var, "none") == 0) {
shellmemory[i].var = strdup(var_in);
shellmemory[i].value = strdup(value_in);
return;
}
}
}
// get value based on input key
char *mem_get_value(char *var_in, char caller) {
int i;
for (i = FRAME_VAR_BORDER; i < FRAME_STORE_SIZE + VAR_STORE_SIZE; i++) {
if (strcmp(shellmemory[i].var, var_in) == 0) {
return strdup(shellmemory[i].value);
}
}
if (caller == 'p') {
// 'p' stands for print
return "Variable does not exist";
} else {
return ""; // echo a newline if not found
}
}
/**
* @brief reset the memory zone allocated for the variables
*
*/
void reset_var_zone() {
for (int i = FRAME_VAR_BORDER; i < FRAME_STORE_SIZE + VAR_STORE_SIZE; i++) {
mem_free_at(i);
}
}
/**
* @brief clear a frame in memory
*
* @param start_pos
*/
void clear_frame(int start_pos) {
for (int i = start_pos; i < start_pos + 3; i++) {
mem_free_at(i);
}
}
void printShellMemory() {
int count_empty = 0;
for (int i = 0; i < FRAME_STORE_SIZE + VAR_STORE_SIZE; i++) {
if (strcmp(shellmemory[i].var, "none") == 0) {
count_empty++;
} else {
printf("\nline %d: key: %s\t\tvalue: %s\n", i, shellmemory[i].var,
shellmemory[i].value);
}
}
printf("\n\t%d lines in total, %d lines in use, %d lines free\n\n",
FRAME_STORE_SIZE + VAR_STORE_SIZE,
FRAME_STORE_SIZE + VAR_STORE_SIZE - count_empty, count_empty);
}