From 7b0995ea20afed2632893f8528ce9d57772d5498 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Wed, 1 Apr 2015 20:01:07 -0400 Subject: as of 2013-09-19 --- comhan.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ direct.c | 55 ++++++++++++++++ main.c | 22 +++++++ mpx.h | 64 +++++++++++++++++++ sys_reqc.c | 30 +++++++++ 5 files changed, 384 insertions(+) create mode 100644 comhan.c create mode 100644 direct.c create mode 100644 main.c create mode 100644 mpx.h create mode 100644 sys_reqc.c diff --git a/comhan.c b/comhan.c new file mode 100644 index 0000000..556aab2 --- /dev/null +++ b/comhan.c @@ -0,0 +1,213 @@ +/* + * file: comhan.c + * + * This file is the command handler for the + * MPX operating system. + */ + +#include +#include +#include "mpx.h" + +#define BUF_SIZE 80 /* Size of the command line buffer. */ +#define VERSION 0 +#define DATE 1 +#define DIRECTORY 2 +#define STOP 3 +#define HELP 4 +#define PROMPT 5 +#define ALIAS 6 + + + +int length; /* Length of the command line. */ + +char prompt[20] = "mpx>"; +const char version[] = "MPX OS - Version 1.0\n"; +char date[] = "01/09/1991"; + +char *cmds[] = {"version", "date", "directory","stop", + "help", "prompt", "alias", NULL}; +char *aliases[] = {"", "", "", "", "", "", "", NULL}; + +/* + * comhan() This is the command handler for the MPX OS. + * It repeatedly prints a prompt, makes a system + * request to read from the console, and then + * carries out the command. + * + * Parameters: None. + * Return value: None. + */ + +void comhan() { + char *args[5]; + int argc; + int cmd_num; + char buffer[BUF_SIZE]; + + do { + printf("%s ",prompt); /* Print a prompt. */ + length = BUF_SIZE; /* Reset length of buffer. */ + sys_req(CON,READ,buffer,&length); /* Request CON input */ + + argc = set_args(buffer, args); + + switch (cmd_num = get_cmd(args[0])) { + case VERSION: cmd_version(); break; + case DATE: cmd_date(args); break; + case DIRECTORY: cmd_directory(); break; + case STOP: cmd_stop(); break; + case HELP: cmd_help(args); break; + case PROMPT: cmd_prompt(args); break; + case ALIAS: cmd_alias(args); break; + default: + printf("Can't recognize.\n"); + break; + } + } while (cmd_num != STOP); +} + +int get_cmd(char cmd[]){ + /* return the number associated with a command (use STOP, HELP etc) */ + + int i =0 ; //loop control + + if (cmd == NULL) { + return -1; + } + + strlwr(cmd); + + while (cmds[i] != NULL) { + if (strcmp(cmds[i], cmd)==0 || strcmp(aliases[i], cmd)==0){ + return i; + } + i++; + } + + //default - means it wasn't a valid command + return -1; +} + +int set_args(char buffer[], char *args[]) { + /* use string tok to set the contents of args from buffer + and return the number of args (will go into argc) */ + char separators[4] = " =/"; //Characters that separate tokens + int i = 0; //loop control + + args[i] = strtok(buffer, separators); //Get first token + while(args[i] != NULL){ + args[++i] = strtok(NULL, separators); //Get next tokens + } + + return i; +} +/** + * Print the version number. + */ +void cmd_version() { + printf("%s", version); +} + +/** + * Print or change the date + */ +void cmd_date(char *args[]){ + + int m, d, y; + + if (strcmp("", args[1])==0) { + printf("%s \n", date); + } else { + + m = atoi(args[1]); + d = atoi(args[2]); + y = atoi(args[3]); + + if (m > 0 && m < 13 && d > 0 && d < 32) { + sprintf(date, "%d/%d/%d", m, d, y); + printf("Date set to %s \n", date); + } else { + printf("Invalid date. \n"); + } + } +} +void cmd_directory(){ + int no_proc = directory(direct, DIR_SIZE); + int i; + + if (no_proc == 1) { + printf("You have 1 program \n"); + } else if (no_proc == 0) { + printf("You have no programs \n"); + } else { + printf("You have %d programs \n", no_proc); + } + + if (no_proc > 0) { + printf("Size Name \n"); + printf("===================================== \n"); + } + + for (i = 0; i < no_proc; i++) { + printf("%8d %s \n", direct[i].dirsiz, direct[i].dirnam); + } +} + +/** + * Print a goodbye message + */ +void cmd_stop(){ + printf("**COMHAN execution complete **\n"); +} + +/** + * Print information about the COMHAN commands. + * Will print all commands or just information specific to the + * argument if given. + */ +void cmd_help(char *args[]){ + char ver[] = "version Display version number\n"; + char hlp[] = "help Provide information about commands\n"; + char dir[] = "directory List .mpx files\n"; + char dat[] = "date [mm/dd/yyyy] Display or set the system date\n"; + char stp[] = "stop Terminate execution of COMHAN\n"; + char prmpt[] = "prompt string Change the prompt for commands\n"; + char als[] = "alias command=string Create an alias for a command\n"; + + switch(get_cmd(args[1])){ + case VERSION: printf(ver); break; + case DATE: printf(dat); break; + case DIRECTORY: printf(dir); break; + case STOP: printf(stp); break; + case HELP: printf(hlp); break; + case PROMPT: printf(prmpt); break; + case ALIAS: printf(als); break; + default: + printf("**\tCommand Summary\t**\n"); + printf("Name Use\n"); + printf("======================================\n"); + printf(ver); + printf(dat); + printf(dir); + printf(stp); + printf(hlp); + printf(prmpt); + printf(als); + } +} + +/** + * Change the prompt. + */ +void cmd_prompt(char *args[]){ + strcpy(prompt, args[1]); +} + +void cmd_alias(char *args[]){ + //get the number of the command to alias + int num = get_cmd(args[1]); + strcpy(aliases[num], args[2]); + num ++; +} diff --git a/direct.c b/direct.c new file mode 100644 index 0000000..b6181d5 --- /dev/null +++ b/direct.c @@ -0,0 +1,55 @@ +/* + * file: direct.c + * + * This file contains the function which reads the names and + * sizes of the MPX/OS process files from the disk, and stores + * them in the directory entry array. + */ + +#include /* Borland header file */ +#include /* Borland header file */ +#include /* Borland header file */ +#include /* Borland header file */ +#include "mpx.h" + +dir direct[MAXSIZE]; /* The array of directory entries. */ + +/* + * + * directory - This procedure performs a sequential read of the MPX + * directory, obtaining all .MPX files up to the size of the + * directory. These are the file that can be loaded by + * COMHAN. Note that the file specific details are saved in + * the dir structure, direct. + * + * Parameters: direct - the array of directory entries. + * dir_size - the capacity of the direct array. + * + * Return value: The number of files entries made to the + * direct array. + */ + + +int directory(dir *direct, int dir_size) +{ + int num_procs; /* Number of .mpx files found. */ + char filename[15]; /* Name of a file with .mpx extension. */ + int done; /* Flags when no more .mpx files can be found. */ + struct ffblk ffblk; + + num_procs = 0; /* number of .MPX file entries placed in directory */ + + done = findfirst ("*.MPX",&ffblk,0); + while (!done && num_procs < dir_size) { + strcpy (filename,ffblk.ff_name); + strcpy(direct->dirnam,filename); + direct->dirnam[strcspn(filename,".")] = '\0'; + direct->dirsiz = ffblk.ff_fsize; + ++num_procs; + direct++; + done = findnext(&ffblk); + } + + return(num_procs); +} + diff --git a/main.c b/main.c new file mode 100644 index 0000000..76e9168 --- /dev/null +++ b/main.c @@ -0,0 +1,22 @@ + +/* file: main.c + * + * This file contains the function main + * for the MPX Operating System - this is where + * the program's execution begins + */ + +#include +#include "mpx.h" + + +int main(void) { + printf("... booting MPX\n\n"); + + /* Put initialization code here */ + + comhan(); /* Execute the command handler */ + + return 0; +} + diff --git a/mpx.h b/mpx.h new file mode 100644 index 0000000..dfecbed --- /dev/null +++ b/mpx.h @@ -0,0 +1,64 @@ +/* + * file: mpx.h + * + * Header file for the MPX Operating System. + * + * This file contains constant, structure and function + * prototypes for the MPX Operating System + */ + +/* MPX System request numbers. */ + +#define EXIT_CODE 0 /* Process requesting termination. code. */ +#define CON 1 /* The console device - keyboard & monitor. */ +#define PRT 2 /* The printer device - LPT1. */ +#define COM 3 /* The serial port - COM1. */ + +/* MPX System request types. */ + +#define READ 0 /* Read from device. */ +#define WRITE 1 /* Write to device. */ +#define WAIT 2 /* Semaphore P operation for device. */ +#define SIGNAL 3 /* Semaphore V operation for device. */ + + +#define MAXSIZE 20 /* Size of the directory array. */ + +struct dirstruct { /* Data type for a directory entry. */ + char dirnam[9]; /* The name of a .mpx file. */ + int dirsiz; /* The size of the file (in bytes). */ +}; + +typedef struct dirstruct dir; /* Use dir as the data typer name. */ + +/* Function prototypes. */ + +/* main.c */ +int main(void); + +/* comhan.c */ +void comhan(void); /* The MPX/OS command handler. */ +int get_cmd(char args[]); +void cmd_version(void); +void cmd_date(char *[]); +void cmd_directory(void); +void cmd_stop(void); +void cmd_help(char *[]); +void cmd_prompt(char *[]); +void cmd_alias(char *[]); +void sys_req(int,int,char *,int *); /* MPX system request function. */ +int directory(dir *, int); /* Support function to load the */ + /* directory array. */ + +/* + * Global variable EXTERN directives. + * + * These extern declarations allow the variables to be + * accessed from any source code file which includes + * this header file. The memory space for the variables + * is declared in a *.c file. + */ +#define DIR_SIZE 20 + +extern dir direct[]; /* Array of directory entries - see direct.c */ +extern int directory(dir *direct, int dir_size); diff --git a/sys_reqc.c b/sys_reqc.c new file mode 100644 index 0000000..7856032 --- /dev/null +++ b/sys_reqc.c @@ -0,0 +1,30 @@ +/* + * file: sys_reqc.c + * + * This is the MPX system request function which + * uses the C gets function. + */ + + + +/* sys_req - implements the MPX system request function. + * + * Parameters: number - request number: a device or exit code. + * type - request type: read, write, wait, signal. + * s - address of read/write buffer. + * length - address of buffer length variable. + * + * Return value: None + * + * In this module, the only system request allowed is to + * read from the console (keyboard). This is done using + * the standard C function gets(). + */ + + +void sys_req(int number, int type, char *s, int *length) +{ + gets(s); /* Read a string from the keyboard. */ + *length = strlen(s); /* Get its length. */ +} + -- cgit v1.2.3