home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_12 / 8n12122a < prev    next >
Text File  |  1990-10-30  |  2KB  |  84 lines

  1.  
  2. /**** LISTING 3 *******/
  3.  
  4. main()
  5. {
  6.     int get_event();
  7.     ARG arg;
  8.  
  9.     init(&arg);
  10.  
  11.     /* the event loop */
  12.     while (1)
  13.     {
  14.         driver(get_event(), &arg);
  15.     }
  16. }
  17.  
  18. /* Initialize the ARG structure */
  19. int init(arg)
  20.     ARG *arg;
  21. {
  22.     arg->cur_state = 1;
  23.     arg->chan = 1;
  24. }
  25.  
  26. /* Get an event - here it is from stdio */
  27. int get_event()
  28. {
  29.     int ev;
  30.     printf("Event: ");
  31.     if (scanf("%d", &ev) != 1)
  32.     {
  33.         printf("\nCompleted\n");
  34.         exit(0);
  35.     }
  36.     return (ev);
  37. }
  38.  
  39. /* send the event through the state machine */
  40. int driver(ev, arg)
  41.     int ev;
  42.     ARG *arg;
  43. {
  44.     register int curr = arg->cur_state;
  45.     register int i,j;
  46.     int (*func) ();
  47.  
  48.     /* find the state */
  49.  
  50.     for (i = 0; (curr != s_table[i].state || s_table[i].state == END); i++);
  51.  
  52.     if (s_table[i].state == END)
  53.     {
  54.         printf("Invalid State: %d\n",curr);
  55.         return (-1);
  56.     }
  57.  
  58.     /* find the event for this state */
  59.  
  60.     for (; (s_table[i].event != ev && s_table[i].state == curr); i++);
  61.  
  62.     if (s_table[i].state != curr)
  63.     {
  64.         /* uncomment printf if warning desired */
  65.         /* printf("Invalid event: %d\n", ev);  */
  66.         return (-2);
  67.     }
  68.  
  69.     /* set the next state */
  70.  
  71.     arg->cur_state = s_table[i].n_state;
  72.  
  73.     /* execute the function list */
  74.  
  75.     for (j = 0; j < MAX_FUNCS; j++)
  76.     {
  77.         if ((func = *(s_table[i].flist[j])) != 0)
  78.             (*func) (arg);
  79.     }
  80.  
  81.     return (0);
  82. }
  83.  
  84.