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

  1. /*******************************************************************************
  2.  *
  3.  *        G E T _ P I C T U R E . C
  4.  *        -------------------------
  5.  *
  6.  * Description:
  7.  *    Compiles a picture statement.
  8.  *
  9.  * Included functions:
  10.  *    get_picture    - Does the job
  11.  *
  12.  * Revision:
  13.  *    Ver    Date    By        Reason
  14.  *    ---    ----    --        ------
  15.  *    1.00    900621    Lars Berntzon    Created
  16.  *
  17.  ******************************************************************************/
  18.  
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include <string.h>
  22.  
  23. #include "token.h"
  24. #include "comp.h"
  25.  
  26. #define isnormal(ch) (isalnum(ch) || (ch) == '_')
  27.  
  28. static int found_field();
  29. static int found_event();
  30. static int found_literal();
  31.  
  32. static struct lookup_s lookup[] = {
  33.     "FIELD",   found_field,
  34.     "LITERAL", found_literal,
  35.     "EVENT",   found_event
  36. };
  37.  
  38. struct picture *get_picture()
  39. {
  40.     char token[TOKENSIZE];
  41.     struct link *lp;
  42.     struct picture *pp = NULL;
  43.     int i;
  44.     
  45.     if(GetTokNC(token) == NULL || !isnormal(token[0])) {
  46.         error("expected nam of picture");
  47.         return NULL;
  48.     }
  49.     
  50.     pp = memalloc(sizeof *pp);
  51.     link_name(&pp->link, token);
  52.     
  53.     /* Check that viewport is specified */
  54.     if (GetTokNC(token) == NULL || strequ(token, "viewport")) {
  55.         error("expected viewport");
  56.         unget_picture(pp);
  57.         return NULL;
  58.     }
  59.     
  60.     if (GetTokNC(token) == NULL || !isnormal(token[0])) {
  61.         error("expected name of viewport");
  62.         unget_picture(pp);
  63.         return NULL;
  64.     }
  65.     
  66.     if ((lp = find_name((struct link *)list.viewport, token)) == NULL) {
  67.         error("unknown viewport");
  68.         unget_picture(pp);
  69.         return NULL;
  70.     }
  71.     
  72.     pp->viewport = (struct viewport *)lp;
  73.     
  74.     if (GetTokNC(token) == NULL || strcmp(token, "{")) {
  75.         error("expected '{'");
  76.         unget_picture(pp);
  77.         return NULL;
  78.     }
  79.     
  80.     while(GetTokNC(token) != NULL) {
  81.         if (strcmp(token, "}") == 0) break;
  82.         
  83.     for(i = 0; i < N_CMDS; i++) {
  84.         if (strequ(token, lookup[i].cmd) == 0) break;
  85.     }
  86.     if (i < N_CMDS) {
  87.         if ((*lookup[i].func)(pp) != OK) {
  88.             unget_picture(pp);
  89.             return NULL;
  90.         }
  91.     }
  92.     else {
  93.         fprintf(stderr, "line %d: unknown token '%s' for picture\n", 
  94.                 line, token);
  95.     }
  96.     }
  97.     return pp;
  98. }
  99.  
  100. struct picture *unget_picture(struct picture *pp)
  101. {
  102.     struct picture *next;
  103.     struct field *fp;
  104.     struct literal *lp;
  105.     struct event *ep;
  106.     
  107.     if (pp == NULL) return NULL;
  108.     
  109.     next = (struct picture *)pp->link.next;
  110.     
  111.     for(fp = pp->field; fp != NULL; fp = unget_field(fp))
  112.     ;
  113.     
  114.     for(lp = pp->literal; lp != NULL; lp = unget_literal(lp))
  115.     ;
  116.  
  117.     for(ep = pp->event; ep != NULL; ep = unget_event(ep))
  118.     ;
  119.  
  120.     if (pp->link.name) free(pp->link.name);
  121.  
  122.     free(pp);
  123.  
  124.     return next;
  125. }
  126.  
  127. static int found_field(pp)
  128.     struct picture *pp;
  129. {
  130.     struct field *fp;
  131.     
  132.     if ((fp = get_field()) == NULL) return FAIL;
  133.  
  134.     link((struct link **)&pp->field, &fp->link, TYPE_FIELD);
  135.  
  136.     return OK;
  137. }
  138.  
  139. static int found_literal(pp)
  140.     struct picture *pp;
  141. {
  142.     struct literal *lp;
  143.     
  144.     if ((lp = get_literal()) == NULL) return FAIL;
  145.     
  146.     link((struct link **)&pp->literal, &lp->link, TYPE_LITERAL);
  147.  
  148.     return OK;
  149. }
  150.  
  151. static int found_event(pp)
  152.     struct picture *pp;
  153. {
  154.     struct event *ep;
  155.     
  156.     if ((ep = get_event()) == NULL) return FAIL;
  157.     
  158.     link((struct link **)&pp->event, &ep->link, TYPE_EVENT);
  159.  
  160.     return OK;
  161. }
  162.  
  163.