The first phase of your semester project will be to write a command line interpr
ID: 3889538 • Letter: T
Question
The first phase of your semester project will be to write a command line interpreter for the computer to be described in a later assignment. This command line interpreter will function as a simple operating system for the rest of the project. This must be written in the C programming language (NOT C++) and must run in the LINUX environment without any added compiler options The interpreter should do the following: The command interpreter should output an appropriate welcome message and a prompt and wait for the user to type a command line. The command interpreter will process the command and call the functiorn which carries out the command. Commands will consist of single word commands followed by zero or more parameters. If a command requires a parameter and it is missing, the interpreter should tell the user that a parameter is required and then output the prompt and wait for a command. The same holds if a parameter is provided that is not required. For now, each command (except Help, Directory, and Exit) should be a stub which simply states that it has been reached. These commands will be prog later phases. NOTE: Your program MUST check for the presence/absence of parameters! 1. rammed in You may only use In later phases you may use 2. For this phase, do not use any precompiled routines to break up the line into pieces. That is, do not use strtok (or any of its variants). loops and comparisons. these. If in doubt, don't use it! 3. The command interpreter should not be menu driven, and should recognize the following commands (only the commands indicated by * need to be implemented, but all should be recognized) load filename: will call the load function to load the specified file. execute ; will call the computer simulation program to execute the program that was previously loaded in memory debug will allow you to execute in debug mode. dump start end: will call the dump function, passing the values of start and end. *help; will print out a list of available commands. assemble filename; will assemble an SIC assembly language program into a load module and store it in "directory: will list the files stored in the current directory. Do this by system("ls"); *exit: will exit from the simulator. Start and end will be hexadecimal values level, a message to type help should be displayed. a file If an unrecognized command is typed at the command You will need the directive ttinclude . NOTE: The * is not part of the command!!!Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LOAD 1
#define EXECUTE 2
#define DEBUG 3
#define DUMP 4
#define HELP 5
#define ASSEMBLE 6
#define DIRECTORY 7
#define EXIT 8
#define INVALIDKEY 0
typedef struct { char *key; int val; } sym_table_struct;
static sym_table_struct lookuptable[] = {
{ "load", LOAD }, { "execute", EXECUTE },
{ "debug", DEBUG }, { "dump", DUMP },
{ "help", HELP }, { "assemble", ASSEMBLE },
{ "directory", DIRECTORY }, { "exit", EXIT }
};
#define NKEYS (sizeof(lookuptable)/sizeof(sym_table_struct))
int key_from_string(char *key)
{
int i;
sym_table_struct *sym = lookuptable;
for (i=0; i < NKEYS; i++) {
if (strcmp(sym->key, key) == 0)
return sym->val;
sym++;
}
return INVALIDKEY;
}
void load(char* filename)
{
printf("Loading file: %s ", filename);
}
void execute()
{
printf("Executing ");
}
void debug()
{
printf("Debugging ");
}
void dump(int start, int end)
{
printf("Dumping start: %x end: %x ", start, end);
}
void help()
{
printf("Lis of commands: ");
printf("load <filename> ");
printf("execute ");
printf("debug ");
printf("dump start end ");
printf("help ");
printf("assemble <filename> ");
printf("directory ");
printf("exit ");
}
void assemble(char* filename)
{
printf("Assembling %s ", filename);
}
void directory()
{
system("ls");
}
void call_exit()
{
printf("Exit. ");
}
int main(int argc, char const *argv[])
{
printf("Ciao! ");
printf("Welcome to tiny interpreter! ");
printf("Type help to get list of commands. ");
char quit = 0;
char command[100], buf [3][100];
int i, j, k;
while(!quit)
{
fgets(command, 100, stdin);
j = 0;
k = 0;
for (i = 0; command[i]!='' || j>2; ++i)
{
//to split
if(command[i] == ' ')
{
buf[j][k] = '';
if(k>0) j++;
k=0;
}
else buf[j][k++] = command[i];
}
int start, end;
printf("key to string %d ", key_from_string(buf[0]));
switch (key_from_string(buf[0])) {
case LOAD:
//run load
load(buf[1]);
break;
case EXECUTE:
//run execute
execute();
break;
case DEBUG:
//run debug
debug();
break;
case DUMP:
//run dump
start = atoi(buf[1]);
end = atoi(buf[2]);
break;
case HELP:
//run help
help();
break;
case ASSEMBLE:
//run assemble
assemble(buf[1]);
break;
case DIRECTORY:
//run directory
directory();
break;
case EXIT:
//exit
call_exit();
quit = 1;
return 0;
break;
case INVALIDKEY:
help();
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.