fix: resize arrays as much as needed
This commit is contained in:
22
src/msh.c
22
src/msh.c
@@ -17,8 +17,9 @@
|
|||||||
#define HISTORY_FILE_NAME "/history.log"
|
#define HISTORY_FILE_NAME "/history.log"
|
||||||
#define LOCAL_SHARE_DIRNAME "/.local/share/msh"
|
#define LOCAL_SHARE_DIRNAME "/.local/share/msh"
|
||||||
|
|
||||||
#define DEFAULTLINECAPACITY 128
|
#define DEFAULTLINECAPACITY 256
|
||||||
#define DEFAULTTOKENSCAPACITY 32
|
#define DEFAULTTOKENSCAPACITY 64
|
||||||
|
#define DEFAULTHISTORYCAPACITY 128
|
||||||
|
|
||||||
const int LINESIZE = 512;
|
const int LINESIZE = 512;
|
||||||
|
|
||||||
@@ -267,8 +268,13 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
tokenworkcapacity = DEFAULTLINECAPACITY;
|
tokenworkcapacity = DEFAULTLINECAPACITY;
|
||||||
|
|
||||||
historycapacity = 16;
|
historycapacity = DEFAULTHISTORYCAPACITY;
|
||||||
history = calloc(historycapacity, sizeof(char*));
|
history = calloc(historycapacity, sizeof(char*));
|
||||||
|
if (history == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
memset(history, 0, historycapacity);
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
while(1) {
|
while(1) {
|
||||||
@@ -379,7 +385,7 @@ int main(int argc, char** argv) {
|
|||||||
linelen++;
|
linelen++;
|
||||||
|
|
||||||
// Are we out of capacity? Double line length
|
// Are we out of capacity? Double line length
|
||||||
if (linelen >= linecapacity - 1) {
|
while (linelen >= linecapacity - 1) {
|
||||||
linecapacity *= 2;
|
linecapacity *= 2;
|
||||||
line = realloc(line, linecapacity);
|
line = realloc(line, linecapacity);
|
||||||
if (line == NULL) {
|
if (line == NULL) {
|
||||||
@@ -409,7 +415,7 @@ print_prompt:
|
|||||||
fprintf(historyfile, "%s\n", line);
|
fprintf(historyfile, "%s\n", line);
|
||||||
fflush(historyfile);
|
fflush(historyfile);
|
||||||
|
|
||||||
if (tokenworkcapacity < linelen - 1) {
|
while (tokenworkcapacity < linelen - 1) {
|
||||||
tokenworkcapacity *= 2;
|
tokenworkcapacity *= 2;
|
||||||
tokenwork = realloc(tokenwork, tokenworkcapacity);
|
tokenwork = realloc(tokenwork, tokenworkcapacity);
|
||||||
}
|
}
|
||||||
@@ -422,8 +428,7 @@ print_prompt:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TEMPORARY
|
while (historyi >= historycapacity) {
|
||||||
if (historyi >= historycapacity) {
|
|
||||||
historycapacity *= 2;
|
historycapacity *= 2;
|
||||||
history = reallocarray(history, historycapacity, sizeof(char*));
|
history = reallocarray(history, historycapacity, sizeof(char*));
|
||||||
|
|
||||||
@@ -431,6 +436,7 @@ print_prompt:
|
|||||||
retval = 1;
|
retval = 1;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
memset(history + historyi, 0, (historycapacity - historyi) * sizeof(char*));
|
||||||
}
|
}
|
||||||
// change ownership of line to history
|
// change ownership of line to history
|
||||||
history[historyi++] = line;
|
history[historyi++] = line;
|
||||||
@@ -448,7 +454,7 @@ print_prompt:
|
|||||||
|
|
||||||
tokens[ntokens] = token;
|
tokens[ntokens] = token;
|
||||||
|
|
||||||
if (ntokens >= tokenscapacity - 1) {
|
while (ntokens >= tokenscapacity - 1) {
|
||||||
tokenscapacity *= 2;
|
tokenscapacity *= 2;
|
||||||
tokens = reallocarray(tokens, tokenscapacity, sizeof(char*));
|
tokens = reallocarray(tokens, tokenscapacity, sizeof(char*));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user