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

  1. /*******************************************************************************
  2.  *
  3.  *        G E T _ S T M T . C
  4.  *        -------------------
  5.  *
  6.  * Description:
  7.  *    Compiles a C-statement block into an allocated memory chain.
  8.  *
  9.  * Included functions:
  10.  *    get_stmt    - Does the job
  11.  *
  12.  * Revision:
  13.  *    Ver    Date    By        Reason
  14.  *    ---    ----    --        ------
  15.  *    1.00    900625    Lars Berntzon    Created
  16.  *
  17.  ******************************************************************************/
  18.  
  19. #include "config.h"
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24.  
  25. #ifdef STDLIB_H
  26. #include <stdlib.h>
  27. #endif
  28. #ifdef MALLOC_H
  29. #include <malloc.h>
  30. #endif
  31.  
  32. #include "token.h"
  33. #include "comp.h"
  34.  
  35. #define isnormal(ch) (isalnum(ch) || (ch) == '_')
  36.  
  37. struct stmt *get_stmt()
  38. {
  39.     char input[500];
  40.     char token[TOKENSIZE];
  41.     struct stmt *first = NULL, *sp = NULL, *tail = NULL;
  42.     int pos = 0;
  43.     int nesting = 1;
  44.     
  45.     if (GetTokNC(token) == NULL) {
  46.     error("premature end of file fot C-statement");
  47.     return NULL;
  48.     }
  49.     
  50.     if (strcmp(token, "{") != 0) {
  51.         error("expected '{' for C-statement");
  52.         return NULL;
  53.     }
  54.     
  55.     for(;;) {
  56.         if (GetTokNC(token) == NULL) {
  57.             error("premature end of file for C-statement");
  58.             return NULL;
  59.         }
  60.         
  61.     if (strcmp(token, "{") == 0) nesting++;
  62.     else if (strcmp(token, "}") == 0) nesting--;
  63.     
  64.     if (newline || nesting == 0) {
  65.         if (pos != 0) {
  66.             sp = memalloc(offsetof(struct stmt, txt[strlen(input) + 1]));
  67.             sp->line = line - newline;
  68.             strcpy(sp->txt, input);
  69.             sp->next = NULL;
  70.             if (first == NULL) {
  71.                 first = sp;
  72.             }
  73.             else {
  74.                 tail->next = sp;
  75.             }
  76.             tail = sp;
  77.         }
  78.         if (nesting == 0) break;
  79.         
  80.         pos = (nesting - 1) * 4; /* indentation */
  81.         memset(input, ' ', pos);
  82.         input[pos] = 0;
  83.     }
  84.     
  85.     if (pos != 0 && isnormal(token[0]) && isnormal(input[pos - 1])) {
  86.         strcpy(input + pos, " ");
  87.         pos += 1;
  88.     }
  89.     strcpy(input + pos, token);
  90.     pos += strlen(token);
  91.     }
  92.     return first;
  93. }
  94.  
  95. void unget_stmt(struct stmt *sp)
  96. {
  97.     struct stmt *next;
  98.     
  99.     for(; sp != NULL; sp = next) {
  100.         next = sp->next;
  101.         free(sp);
  102.     }
  103. }    
  104.     
  105.     
  106.