In the program below, I'm getting exit code 11. What I'm trying to do is to add a command "RUN filename". This command will take filename.txt and parse it to execute the commands in the file. So I intend to use fgets().
Also, in order to differentiate commands from the shell and from the file, I used a global boolean variable fromfile
.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// maximum number of registers
#define RMAX 4
// maximum length of a string
#define SMAX 80
// maximum number of memory locations
#define MMAX 16
// registers
int r[RMAX];
// memory
int m[MMAX];
// loop variable
int i;
// cmd input string
char cmd_input[SMAX];
// cmd portion of input (1st word)
char cmd[SMAX];
// argument portion of input (2nd word)
char arg[SMAX];
int iarg; // interger version of argument
// number words found in input
int wordsFound;
// display mode
char display_mode[4] = "DEC"; // same as {'D','E','C','\0'}
bool done = false;
FILE* fp = NULL; // file pointer for basic input/output
FILE* runfp = NULL; // file pointer for run file command
bool fromfile = false;
int temp; // temporary integer
void printRegister(int reg) {
// prints value of r[reg] in correct display mode
printf("[%s] ", display_mode);
if (strcmp(display_mode, "DEC") == 0) {
printf("r[%d]: ", reg);
printf("%d\n", r[reg]);
} else if (strcmp(display_mode, "HEX") == 0) {
printf("r[0x%x]: ", reg);
printf("0x%x\n", r[reg]);
} else {
printf("INVALID MODE\n");
}
}
int readIntFromString(char* source){
// reads value of integer from source string in correct display mode
int intsFound;
int result;
if (strcmp(display_mode, "DEC") == 0) {
intsFound = sscanf(source, "%d", &result);
if (intsFound != 1) {
printf("INVALID NUMBER - used 0 instead\n");
result = 0;
}
} else if (strcmp(display_mode, "HEX") == 0) {
intsFound = sscanf(source, "%x", &result);
if (intsFound != 1) {
printf("INVALID NUMBER - used 0x0 instead\n");
result = 0;
}
} else {
printf("INVALID MODE - used 0 instead\n");
result = 0;
}
return(result);
}
void stringUpper(char string[]){
// upper case the argument string
int i = 0;
while ((string[i] != '\0') && (i < SMAX)) {
if (string[i] >= 'a' && string[i] <= 'z') {
string[i] = string[i] - 32;
}
i++;
}
}
void clearAllRegisters(){
int i;
for (i=0; i<RMAX; i++) {
r[i] = 0;
}
}
void clearAllMemory(){
int i;
for (i=0; i<MMAX; i++) {
m[i] = 0;
}
}
// ============= MAIN PROGRAM ==============
int main () {
// initialize registers to 0
clearAllRegisters();
//initialize memories to 0
clearAllMemory();
printf("\nWelcome to the command shell...now with memory!\n");
while (!done) {
// GET INPUT
// show current status and value of r[0]
printRegister(0);
if(fromfile == true){
if(feof(runfp) == 0){
printf("RUN> %s", cmd_input);
fgets(cmd_input, SMAX, runfp);
} else{
fromfile = false;
fclose(runfp);
printf("=====================");
printf("RUN - end reading from file");
printf("=====================");
}
}
else{
//prompt for input and read input string from shell
printf("> ");
fgets(cmd_input, SMAX, stdin);
}
// break the input string into cmd arg and junk
wordsFound = sscanf(cmd_input, "%s %s %*s", cmd, arg);
// uppercase cmd to standard format
stringUpper(cmd);
// handle error inputs
if (wordsFound <= 0) {
// no command found, substitute NOP
strcpy(cmd, "NOP");
} else if (wordsFound <= 1) {
// no argument found, substitute empty string
strcpy(arg, "");
}
// PROCESS INPUT
if(strcmp(cmd, "RUN") == 0) {
printf("=====================\n");
printf("RUN - start to read from file: %s\n", arg);
printf("=====================\n");
runfp = fopen(arg, "r");
fromfile = true;
} else if ((strcmp(cmd, "EXIT") == 0) || (strcmp(cmd, "QUIT") == 0)) {
// exit from shell
printf("EXIT encountered.\n");
done = true;
} else if (strcmp(cmd, "NOP") == 0) {
// do nothing
printf("NOP\n");
} else if (strcmp(cmd, "ECHO") == 0) {
// echo arg
printf("%s\n", arg);
} else if (strcmp(cmd, "MODE") == 0) {
// update display mode
// standardize argument
stringUpper(arg);
if (strcmp(arg, "DEC") == 0) {
strcpy(display_mode, "DEC");
} else if (strcmp(arg, "HEX") == 0) {
strcpy(display_mode, "HEX");
} else {
printf("INVALID DISPLAY MODE: %s\n", arg);
}
} else {
// Error on Input
printf(">> Unknown Command |%s|%s|\n", cmd, arg);
}
}
printf("\nEnd Program.\n\n");
return(0);
}