Fixed combined getchar and scanf input to mimic command line.

This commit is contained in:
Xaver K 2020-04-09 13:23:56 +02:00
parent ecbea115c7
commit f8e99ec24c

View File

@ -12,31 +12,32 @@ char inputBuff[1<<10] = "";
void readIntoBuffer() {
long length = strlen(inputBuff);
char nextChar = 0;
while((nextChar = getchar()) != '\n'){
inputBuff[length++] = nextChar;
}
inputBuff[length++] = '\n';
inputBuff[length] = '\0';
while((nextChar = getchar()) != '\n' && nextChar != EOF){
inputBuff[length++] = nextChar;
}
inputBuff[length++] = '\n';
inputBuff[length] = '\0';
}
/* Define the functions to overload the old ones */
int scanf_wrap(const char *format, ...) {
/* unget chars in buffer */
int doRequest = 1;
char doRequest = 1;
char leadingNewline = 1;
long index = strlen(inputBuff) - 1;
for(; index >= 0; --index) {
ungetc(inputBuff[index], stdin);
/* if there already is a newline in buffer
we need no input request */
if(inputBuff[index] == '\n') {
doRequest = 0;
if(!leadingNewline){
doRequest = 0;
}
} else {
leadingNewline = 0;
}
}
/* Buffer will always be empty after scanf call
since there is no way to enter more than one
newline in the frontend */
inputBuff[0] = '\0';
// printf("doRequest: %d\n", doRequest);
if(doRequest) {
printf("<inputRequest>");
fflush(stdout);
@ -46,6 +47,9 @@ int scanf_wrap(const char *format, ...) {
int result = vscanf( format, arglist );
va_end( arglist );
/* Put the remaining input into the input buffer */
readIntoBuffer();
return result;
}