aboutsummaryrefslogtreecommitdiff
path: root/comhan.c
blob: 556aab22518d32531ec903d51b6e76c2ee66178d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 *   file:  comhan.c
 *
 *   This file is the command handler for the
 *   MPX operating system.
 */

#include <stdio.h>
#include <string.h>
#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 ++;
}