EE445M RTOS
Taken at the University of Texas Spring 2015
system.c
Go to the documentation of this file.
1 /* -*- mode: c; c-basic-offset: 4; -*- */
2 #include "system.h"
3 
4 #include <stdbool.h>
5 #include <stdlib.h>
6 
7 #include "libut/utlist.h"
8 #include "libstd/nexus.h"
9 
12 
15 
18 
19 void system_init() {
20 
22  /* All commands begin in the unregistered state */
23  for(i=0; i<SYSTEM_MAX_COMMANDS; ++i) {
24  CDL_PREPEND(unregistered_commands, &SYSTEM_COMMANDS[i]);
25  }
26 }
27 
28 /* returns success (failure could be out of room) */
29 bool system_register_command(const char* command_name, int(*command)()) {
30 
31  /* Grab the first free command, move it from the unregisted to the
32  * registered pile, and populate it with this function's
33  * arguments */
35  CDL_DELETE(unregistered_commands, sys_command);
36  CDL_PREPEND(registered_commands, sys_command);
37 
38  sys_command->valid = true;
39  memset(sys_command->name, 0, SYSTEM_MAX_COMMANDS);
40  ustrcpy(sys_command->name, command_name);
41  sys_command->command = command;
42  return true;
43 }
44 
45 bool system_deregister_command(const char* command_name) {
46 
47  /* Grab the structure of the command to deregister and invaldiate
48  * the metadata. Move it from the registered to the unregistered
49  * pile. */
50  system_command* command = _system_command_from_name(command_name);
51  command->valid = false;
52  CDL_DELETE(registered_commands, command);
53  CDL_PREPEND(unregistered_commands, command);
54  return true;
55 }
56 
57 /* OPTIMIZE: inline */
58 system_command* _system_command_from_name(const char* command_name) {
59 
60  system_iterator i=0;
61  while(i<SYSTEM_MAX_COMMANDS &&
62  0 != strcmp(SYSTEM_COMMANDS[i].name, command_name)) {
63  ++i;
64  }
65  return &SYSTEM_COMMANDS[i];
66 }
67 
68 /* TODO: allow for argument passing */
69 exit_status_t system_exec(const char* command, const char** arguments) {
70 
71  system_command* sys_command = _system_command_from_name(command);
72  if (sys_command->valid) {
73  return sys_command->command(/*arguments*/);
74  } else {
75  /* TODO: determine what to do here */
76  postpone_death(); /* plan a */
77  return EXIT_FAILURE; /* plan b */
78  }
79 }
char name[16]
Definition: system.h:32
exit_status_t system_exec(const char *command, const char **arguments)
Definition: system.c:69
void ustrcpy(char *dest, const char *source)
Definition: nexus.c:69
system_command * _system_command_from_name(const char *command_name)
Definition: system.c:58
#define SYSTEM_MAX_COMMANDS
Definition: system.h:17
int strcmp(const char *s1, const char *s2)
Definition: nexus.c:35
#define CDL_DELETE(head, del)
Definition: utlist.h:705
#define postpone_death()
Definition: nexus.h:40
bool system_deregister_command(const char *command_name)
Definition: system.c:45
int(* command)()
Definition: system.h:33
bool valid
Definition: system.h:31
void * memset(void *b, int c, int len)
Definition: nexus.c:4
uint8_t exit_status_t
Definition: system.h:24
uint8_t system_iterator
Definition: system.h:21
void system_init()
Definition: system.c:19
system_command * unregistered_commands
Definition: system.c:17
system_command * registered_commands
Definition: system.c:14
#define NULL
Definition: defines.h:32
static system_command SYSTEM_COMMANDS[16]
Definition: system.c:11
bool system_register_command(const char *command_name, int(*command)())
Definition: system.c:29
#define CDL_PREPEND(head, add)
Definition: utlist.h:671