home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / cforms / part01 / src / get_event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-18  |  4.2 KB  |  215 lines

  1. /*******************************************************************************
  2.  *
  3.  *        G E T _ E V E N T . C
  4.  *        ---------------------
  5.  *
  6.  * Description:
  7.  *    Compiles an event statement.
  8.  *
  9.  * Included functions:
  10.  *    get_event    - Does the job
  11.  *
  12.  * Revision:
  13.  *    Ver    Date    By        Reason
  14.  *    ---    ----    --        ------
  15.  *    1.00    900625    Lars Berntzon    Created
  16.  *
  17.  ******************************************************************************/
  18.  
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include <string.h>
  22. #include <assert.h>
  23.  
  24. #include "token.h"
  25. #include "comp.h"
  26.  
  27. static int get_event_stmt(struct stmt **stmt);
  28.  
  29. static int found_key();
  30. static int found_refresh();
  31. static int found_draw();
  32. static int found_entry();
  33. static int found_exit();
  34. static int found_next();
  35. static int found_previous();
  36.  
  37. static struct keys_s keys[] = {
  38. #include "keys.h"
  39. };
  40.  
  41. static struct lookup_s lookup[] = {
  42.     "KEY",      found_key,
  43.     "REFRESH",  found_refresh,
  44.     "DRAW",     found_draw,
  45.     "ENTRY",    found_entry,
  46.     "EXIT",     found_exit,
  47.     "NEXT",     found_next,
  48.     "PREVIOUS", found_previous
  49. };
  50.  
  51. struct event *get_event()
  52. {
  53.     char token[TOKENSIZE];
  54.     struct event *ep = NULL;
  55.     int i;
  56.  
  57.     if (GetTokNC(token) == NULL) {
  58.         error("expected type of event");
  59.         return NULL;
  60.     }
  61.     
  62.     for (i = 0; i < N_CMDS; i++) {
  63.         if (strequ(lookup[i].cmd, token) == 0) break;
  64.     }
  65.     
  66.     if (i >= N_CMDS) {
  67.         error("illegal type of event");
  68.         return NULL;
  69.     }
  70.     
  71.     ep = memalloc(sizeof *ep);
  72.     
  73.     if ((*lookup[i].func)(ep) != OK) {
  74.         unget_event(ep);
  75.         return NULL;
  76.     }
  77.     
  78.     return ep;
  79. }
  80.  
  81. struct event *unget_event(struct event *ep)
  82. {
  83.     struct event *next;
  84.     
  85.     if (ep == NULL) return NULL;
  86.  
  87.     next = (struct event *)ep->link.next;
  88.     
  89.     if (ep->link.name) free(ep->link.name);
  90.     free(ep);
  91.  
  92.     return next;
  93. }
  94.  
  95. static int found_key(ep)
  96.     struct event *ep;
  97. {
  98.     char token[TOKENSIZE];
  99.     int i;
  100.     
  101.     if (GetTokNC(token) == NULL) {
  102.         error("expected name of key for event");
  103.         return FAIL;
  104.     }
  105.     
  106.     for(i = 0; i < N_KEYS; i++) {
  107.         if(strequ(token, keys[i].key) == 0) break;
  108.     }
  109.  
  110.     if (i >= N_KEYS) {
  111.         error("unknown key");
  112.         return FAIL;
  113.     }
  114.     
  115.     if (ep->link.name == NULL) {
  116.         sprintf(token, "key_%s", keys[i].key);
  117.         link_name(&ep->link, token);
  118.     }
  119.     
  120.     ep->type = "EVENT_KEY";
  121.     ep->code = keys[i].code;
  122.     if (get_event_stmt(&ep->stmt) != OK) return FAIL;
  123.     
  124.     return OK;
  125. }
  126.  
  127. static found_refresh(ep)
  128.     struct event *ep;
  129. {
  130.     link_name(&ep->link, "REFRESH");
  131.     ep->type = "EVENT_REFRESH";
  132.     ep->code = "0";
  133.     if (get_event_stmt(&ep->stmt) != OK) return FAIL;
  134.     return OK;
  135. }
  136.  
  137. static found_draw(ep)
  138.     struct event *ep;
  139. {
  140.     link_name(&ep->link, "DRAW");
  141.     ep->type = "EVENT_DRAW";
  142.     ep->code = "0";
  143.     if (get_event_stmt(&ep->stmt) != OK) return FAIL;
  144.     return OK;
  145. }
  146.  
  147. static found_next(ep)
  148.     struct event *ep;
  149. {
  150.     link_name(&ep->link, "NEXT");
  151.     ep->type = "EVENT_NEXT";
  152.     ep->code = "0";
  153.     if (get_event_stmt(&ep->stmt) != OK) return FAIL;
  154.     return OK;
  155. }
  156.  
  157. static found_previous(ep)
  158.     struct event *ep;
  159. {
  160.     link_name(&ep->link, "PREVIOUS");
  161.     ep->type = "EVENT_PREVIOUS";
  162.     ep->code = "0";
  163.     if (get_event_stmt(&ep->stmt) != OK) return FAIL;
  164.     return OK;
  165. }
  166.  
  167. static found_entry(ep)
  168.     struct event *ep;
  169. {
  170.     link_name(&ep->link, "ENTRY");
  171.     ep->type = "EVENT_ENTRY";
  172.     ep->code = "0";
  173.     if (get_event_stmt(&ep->stmt) != OK) return FAIL;
  174.     return OK;
  175. }
  176.  
  177. static found_exit(ep)
  178.     struct event *ep;
  179. {
  180.     link_name(&ep->link, "EXIT");
  181.     ep->type = "EVENT_EXIT";
  182.     ep->code = "0";
  183.     if (get_event_stmt(&ep->stmt) != OK) return FAIL;
  184.     return OK;
  185. }
  186.  
  187. static int get_event_stmt(struct stmt **stmt)
  188. {
  189.     char token[TOKENSIZE];
  190.     
  191.     assert(stmt);
  192.     
  193.     if (GetTokNC(token) == NULL) {
  194.         error("expected either '{' or 'forget'");
  195.         return FAIL;
  196.     }
  197.     
  198.     if (strcmp(token, "{") == 0) {
  199.         UnGetTok("{");
  200.         if (*stmt = get_stmt()) return OK;
  201.         return FAIL;
  202.     }
  203.     else if (strequ(token, "forget") == 0) {
  204.         *stmt = NULL;
  205.         if (GetTokNC(token) == NULL ||strequ(token, ";")) {
  206.             error("expected ';'");
  207.             return FAIL;
  208.         }
  209.         return OK;
  210.     }
  211.     error("expected either '{' or 'forget'");
  212.     return FAIL;
  213. }
  214.  
  215.