home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- *
- * G E T _ E V E N T . C
- * ---------------------
- *
- * Description:
- * Compiles an event statement.
- *
- * Included functions:
- * get_event - Does the job
- *
- * Revision:
- * Ver Date By Reason
- * --- ---- -- ------
- * 1.00 900625 Lars Berntzon Created
- *
- ******************************************************************************/
-
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <assert.h>
-
- #include "token.h"
- #include "comp.h"
-
- static int get_event_stmt(struct stmt **stmt);
-
- static int found_key();
- static int found_refresh();
- static int found_draw();
- static int found_entry();
- static int found_exit();
- static int found_next();
- static int found_previous();
-
- static struct keys_s keys[] = {
- #include "keys.h"
- };
-
- static struct lookup_s lookup[] = {
- "KEY", found_key,
- "REFRESH", found_refresh,
- "DRAW", found_draw,
- "ENTRY", found_entry,
- "EXIT", found_exit,
- "NEXT", found_next,
- "PREVIOUS", found_previous
- };
-
- struct event *get_event()
- {
- char token[TOKENSIZE];
- struct event *ep = NULL;
- int i;
-
- if (GetTokNC(token) == NULL) {
- error("expected type of event");
- return NULL;
- }
-
- for (i = 0; i < N_CMDS; i++) {
- if (strequ(lookup[i].cmd, token) == 0) break;
- }
-
- if (i >= N_CMDS) {
- error("illegal type of event");
- return NULL;
- }
-
- ep = memalloc(sizeof *ep);
-
- if ((*lookup[i].func)(ep) != OK) {
- unget_event(ep);
- return NULL;
- }
-
- return ep;
- }
-
- struct event *unget_event(struct event *ep)
- {
- struct event *next;
-
- if (ep == NULL) return NULL;
-
- next = (struct event *)ep->link.next;
-
- if (ep->link.name) free(ep->link.name);
- free(ep);
-
- return next;
- }
-
- static int found_key(ep)
- struct event *ep;
- {
- char token[TOKENSIZE];
- int i;
-
- if (GetTokNC(token) == NULL) {
- error("expected name of key for event");
- return FAIL;
- }
-
- for(i = 0; i < N_KEYS; i++) {
- if(strequ(token, keys[i].key) == 0) break;
- }
-
- if (i >= N_KEYS) {
- error("unknown key");
- return FAIL;
- }
-
- if (ep->link.name == NULL) {
- sprintf(token, "key_%s", keys[i].key);
- link_name(&ep->link, token);
- }
-
- ep->type = "EVENT_KEY";
- ep->code = keys[i].code;
- if (get_event_stmt(&ep->stmt) != OK) return FAIL;
-
- return OK;
- }
-
- static found_refresh(ep)
- struct event *ep;
- {
- link_name(&ep->link, "REFRESH");
- ep->type = "EVENT_REFRESH";
- ep->code = "0";
- if (get_event_stmt(&ep->stmt) != OK) return FAIL;
- return OK;
- }
-
- static found_draw(ep)
- struct event *ep;
- {
- link_name(&ep->link, "DRAW");
- ep->type = "EVENT_DRAW";
- ep->code = "0";
- if (get_event_stmt(&ep->stmt) != OK) return FAIL;
- return OK;
- }
-
- static found_next(ep)
- struct event *ep;
- {
- link_name(&ep->link, "NEXT");
- ep->type = "EVENT_NEXT";
- ep->code = "0";
- if (get_event_stmt(&ep->stmt) != OK) return FAIL;
- return OK;
- }
-
- static found_previous(ep)
- struct event *ep;
- {
- link_name(&ep->link, "PREVIOUS");
- ep->type = "EVENT_PREVIOUS";
- ep->code = "0";
- if (get_event_stmt(&ep->stmt) != OK) return FAIL;
- return OK;
- }
-
- static found_entry(ep)
- struct event *ep;
- {
- link_name(&ep->link, "ENTRY");
- ep->type = "EVENT_ENTRY";
- ep->code = "0";
- if (get_event_stmt(&ep->stmt) != OK) return FAIL;
- return OK;
- }
-
- static found_exit(ep)
- struct event *ep;
- {
- link_name(&ep->link, "EXIT");
- ep->type = "EVENT_EXIT";
- ep->code = "0";
- if (get_event_stmt(&ep->stmt) != OK) return FAIL;
- return OK;
- }
-
- static int get_event_stmt(struct stmt **stmt)
- {
- char token[TOKENSIZE];
-
- assert(stmt);
-
- if (GetTokNC(token) == NULL) {
- error("expected either '{' or 'forget'");
- return FAIL;
- }
-
- if (strcmp(token, "{") == 0) {
- UnGetTok("{");
- if (*stmt = get_stmt()) return OK;
- return FAIL;
- }
- else if (strequ(token, "forget") == 0) {
- *stmt = NULL;
- if (GetTokNC(token) == NULL ||strequ(token, ";")) {
- error("expected ';'");
- return FAIL;
- }
- return OK;
- }
- error("expected either '{' or 'forget'");
- return FAIL;
- }
-
-