Main Page | Class List | File List | Class Members | File Members

interface.c

Go to the documentation of this file.
00001 #include "standard.h"
00002 #include "interface.h"
00003 #include <sys/types.h>
00004 #include <sys/time.h>
00005 
00006 /*#define DEBUG*/
00007 #define STRLEN 256
00008 
00009 static char line[STRLEN];
00010 static char lastline[STRLEN];
00011 static char cmd[STRLEN];
00012 static char arg[STRLEN];
00013 static int breakpoint = 0;
00014 static int stop = 0;
00015 static enum { Prompt, Run, Cont, Next, Quit } mode;
00016 
00017 extern int Quit_Mux;
00018 extern int debug;
00019 
00020 static void prompt(void)
00021 {
00022   printf("mux_sim:%d [%s]> ", get_clock(), lastline);
00023   fflush(stdout);
00024 }
00025 
00026 static void get_input(void)
00027 {
00028   fgets(line, STRLEN, stdin);
00029   if (strlen(line)) {
00030     line[strlen(line)-1]='\0';
00031   }
00032 #ifdef DEBUG
00033   printf("line=\"%s\"\n", line);
00034 #endif  
00035 }
00036 
00037 static int white(char c)
00038 {
00039   return ((c == ' ') || (c == '\t'));
00040 }
00041 
00042 static void parse_input(void)
00043 {
00044   int i=0,j=0,k=0;
00045 
00046   if ((line[0]=='\0') && (lastline[0]!='\0'))
00047     strcpy(line, lastline);
00048   else
00049     strcpy(lastline, line);
00050   
00051   cmd[0]='\0';
00052   arg[0]='\0';
00053 
00054   if (strlen(line)) {
00055     while (white(line[i]))
00056       i++;
00057     while ((!white(line[i])) && (line[i]!='\0'))
00058       cmd[j++]=line[i++];
00059     cmd[j]='\0';
00060     while (white(line[i]))
00061       i++;
00062     while (line[i]!='\0')
00063       arg[k++]=line[i++];
00064     arg[k]='\0';
00065 #ifdef DEBUG
00066     printf("cmd=\"%s\", arg=\"%s\"\n", cmd, arg);
00067 #endif
00068   }
00069 }
00070 
00071 static int equals(char *s1, char *s2)
00072 {
00073   return (strcmp(s1,s2)==0);
00074 }
00075 
00076 
00077 
00078 static void show_vars()
00079 {
00080   printf("Variables:\n");
00081   printf("\tdebug = %d\n", debug);
00082 }
00083 
00084 
00085 static char *rank(int x)
00086 {
00087   if (x >= 250)
00088     return (" Master MUXer");
00089   if (x >= 200)
00090     return (" MUX Engineer");
00091   if (x >= 150)
00092     return ("n ITU LBC Expert");
00093   if (x >= 100)
00094     return ("n ITU Technical Writer");  
00095   return(" Novice Adventurer");  
00096 }
00097 
00098 
00099 static void show_score(void)
00100 {
00101   static int first = 1;
00102   int x;
00103   if (first) {
00104     time_t tloc;
00105     srand((int) time(&tloc));
00106     first=0;
00107   }
00108   x = rand() % 256;
00109   printf("Your score is %d points, which ranks you as a%s\n", x, rank(x));
00110 }
00111 
00112 static void print_commands(void)
00113 {
00114   printf("\nAvailable Commands:\n\n");
00115   printf("b <x>   = break when clock = x\n");
00116   printf("c       = continue until breakpoint\n");
00117   printf("d <l>   = set debug = l\n");
00118   printf("n <i>   = continue for i clock cycles\n");
00119   printf("r       = run continuously\n");
00120   printf("q       = quit\n");
00121   printf("\n");
00122 }
00123 
00124 
00125 static void do_cmd(void)
00126 {
00127   char s[STRLEN];
00128   if (equals(cmd, "score")) {
00129     show_score();
00130   } 
00131   else if (equals(cmd, "xyzzy")) {
00132     if (debug)
00133       printf("Poof!\n");
00134     else
00135       printf("Nothing happens.\n"); 
00136   } 
00137   else if (equals(cmd, "clear")) {
00138     clrscr();
00139   } 
00140   else if (equals(cmd, "?")||equals(cmd, "h")||equals(cmd, "help")) {
00141     print_commands();
00142   } 
00143   else if (equals(cmd, "q")) {
00144     Quit_Mux = 1;
00145     mode = Quit;
00146   } 
00147   else if (equals(cmd, "quit")) {
00148     Quit_Mux = 1;
00149     mode = Quit;
00150   } 
00151   else if (equals(cmd, "set")) {
00152     show_vars();
00153   }
00154   else if (equals(cmd, "c")) {
00155     mode = Cont;
00156   } 
00157   else if (equals(cmd, "d")) {
00158     debug = atoi(arg);
00159     printf("debug set to %d\n", debug);
00160   }
00161   else if (equals(cmd, "b")) {
00162     if (arg[0]!='\0') {
00163       breakpoint = atoi(arg);
00164       printf("New breakpoint at clock=%d\n", breakpoint);
00165     } else {
00166       printf("Breakpoint not changed.\n");
00167     }
00168   } 
00169   else if (equals(cmd, "n")) {
00170     if (stop=atoi(arg))
00171       stop += get_clock();
00172     else
00173       stop += get_clock() + 1;
00174     mode = Next;
00175   }
00176 }
00177 
00178 
00179 void interface(void)
00180 {
00181   switch (mode) {
00182     case Prompt: break;
00183     case Run:    break;
00184     case Cont:   
00185                  if (get_clock() == breakpoint)
00186                    mode = Prompt;
00187                  break;
00188     case Next:
00189                  if (get_clock() == stop)
00190                    mode = Prompt;
00191                  break;
00192     case Quit: 
00193                  break;
00194     default:
00195                 error("interface","invalid mode");
00196   }
00197 
00198   while (mode == Prompt) {
00199     prompt();
00200     get_input();
00201     parse_input();
00202     do_cmd();
00203   }
00204 }

Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1