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

  1. /*******************************************************************************
  2.  *
  3.  *        G E T _ V I E W P O R T . C
  4.  *        ---------------------------
  5.  *
  6.  * Description:
  7.  *    Compiles a viewport statement.
  8.  *
  9.  * Included functions:
  10.  *    get_viewport    - Does the job
  11.  *
  12.  * Revision:
  13.  *    Ver    Date    By        Reason
  14.  *    ---    ----    --        ------
  15.  *    1.00    900619    Lars Berntzon    Created
  16.  *
  17.  ******************************************************************************/
  18.  
  19. #include "config.h"
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #ifdef STDLIB_H
  25. #include <stdlib.h>
  26. #endif
  27. #ifdef MALLOC_H
  28. #include <malloc.h>
  29. #endif
  30.  
  31. #include "token.h"
  32. #include "comp.h"
  33.  
  34. #define isnormal(ch) (isalnum(ch) || (ch) == '_')
  35.  
  36. static int found_pos();
  37. static int found_size();
  38.  
  39. static struct lookup_s lookup[] = {
  40.     "POS",  found_pos,
  41.     "SIZE", found_size
  42. };
  43.  
  44. struct viewport *get_viewport()
  45. {
  46.     char token[TOKENSIZE];
  47.     struct viewport *vp = NULL;
  48.     int i;
  49.     
  50.     if (GetTokNC(token) == NULL || !isnormal(token[0])) {
  51.         error("expected name of viewport");
  52.         return NULL;
  53.     }
  54.     
  55.     vp = memalloc(sizeof *vp);
  56.     link_name(&vp->link, token);
  57.     
  58.     if(GetTokNC(token) == NULL || strcmp(token, "{")) {
  59.         error("expected '{'");
  60.         unget_viewport(vp);
  61.         return NULL;
  62.     }
  63.     
  64.     while(GetTokNC(token) != NULL) {
  65.         if (strcmp(token, "}") == 0) break;
  66.         
  67.     for(i = 0; i < N_CMDS; i++) {
  68.         if(strequ(token, lookup[i].cmd) == 0) break;
  69.     }
  70.     if (i < N_CMDS) {
  71.         if ((*lookup[i].func)(vp) != OK) {
  72.             unget_viewport(vp);
  73.             return NULL;
  74.         }
  75.     }
  76.     else {
  77.         fprintf(stderr, "line %d: unknown token '%s'\n", line, token);
  78.     }
  79.     }
  80.     return vp;
  81. }
  82.  
  83. struct viewport *unget_viewport(struct viewport *vp)
  84. {
  85.     struct viewport *next;
  86.  
  87.     if (vp == NULL) return NULL;
  88.     
  89.     next = (struct viewport *)vp->link.next;
  90.     if (vp->link.name) free(vp->link.name);
  91.     free(vp);
  92.     return next;
  93. }
  94.  
  95. static int found_pos(vp)
  96.     struct viewport *vp;
  97. {
  98.     char token[TOKENSIZE];
  99.     
  100.     if (!GetTokNC(token) || !isdigit(token[0])) {
  101.         error("expected column");
  102.         return FAIL;
  103.     }
  104.     vp->pos.x = atoi(token);
  105.     
  106.     if(!GetTokNC(token) || strcmp(token, ",")) {
  107.         error("expected ','");
  108.         return FAIL;
  109.     }
  110.     if (!GetTokNC(token) || !isdigit(token[0])) {
  111.         error("expected row");
  112.         return FAIL;
  113.     }
  114.     vp->pos.y = atoi(token);
  115.     if (!GetTokNC(token) || strcmp(token, ";")) {
  116.         error("expected row");
  117.         return FAIL;
  118.     }
  119.     return OK;
  120. }
  121.  
  122. static int found_size(vp)
  123.     struct viewport *vp;
  124. {
  125.     char token[TOKENSIZE];
  126.     
  127.     if (!GetTokNC(token) || !isdigit(token[0])) {
  128.         error("expected column");
  129.         return FAIL;
  130.     }
  131.     vp->size.x = atoi(token);
  132.     
  133.     if(!GetTokNC(token) || strcmp(token, ",")) {
  134.         error("expected ','");
  135.         return FAIL;
  136.     }
  137.  
  138.     if (!GetTokNC(token) || !isdigit(token[0])) {
  139.         error("expected row");
  140.         return FAIL;
  141.     }
  142.     vp->size.y = atoi(token);
  143.     if (!GetTokNC(token) || strcmp(token, ";")) {
  144.         error("expected row");
  145.         return FAIL;
  146.     }
  147.     return OK;
  148. }
  149.  
  150.