home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 402_01 / cforms-2.2 / src / stmt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-20  |  3.1 KB  |  159 lines

  1. #include "config.h"
  2. #include "token.h"
  3. #include "comp.h"
  4.  
  5. #define istext(ch) (isalnum(ch) || (ch) == '_')
  6.  
  7. static void stmt_add_char(struct stmt **stmt, char ch);
  8.  
  9.  
  10. /******************************************************************
  11.  *        S T M T _ A D D
  12.  *        ---------------
  13.  * Description:
  14.  *    Add text to statement list.
  15.  *
  16.  * Arguments:
  17.  *    stmt    - Pointer to pointer of list.
  18.  *    str    - String to add.
  19.  *
  20.  * Return:
  21.  *    none
  22.  *
  23.  ******************************************************************/
  24. void
  25. stmt_add(struct stmt **stmt, char *str, char *space)
  26. {
  27.     if (str == NULL) return;
  28. #ifdef STANDALONE
  29.     fprintf(stderr, "stmt_add('%s', '%s')\n", str, space);
  30. #endif
  31.  
  32.     /*
  33.      * Find last but one statement.
  34.      */
  35.     for(; (*stmt) != NULL && (*stmt)->next != NULL; stmt = &(*stmt)->next) 
  36.     ;
  37.  
  38.     /*
  39.      * Append the text.
  40.      */
  41.  
  42.     while(*space)
  43.     {
  44.     stmt_add_char(stmt, *space++);
  45.     }
  46.  
  47.     while(*str)
  48.     {
  49.     stmt_add_char(stmt, *str++);
  50.     }
  51. }
  52. /******************************************************************
  53.  *        S T M T _ A D D _ C H A R
  54.  *        -------------------------
  55.  * Description:
  56.  *    Add one character to a statement. If statement is empty or
  57.  *    overfilled a new block is automatically allocated.
  58.  *
  59.  * Arguments:
  60.  *    stmt    - Pointer to list head.
  61.  *    ch    - Character to add.
  62.  *
  63.  * Return:
  64.  *    none
  65.  *
  66.  ******************************************************************/
  67. static void
  68. stmt_add_char(struct stmt **stmt, char ch)
  69. {
  70.     /*
  71.      * If this one is full, point to end of list.
  72.      */
  73.     if (((*stmt) != NULL) && (*stmt)->pos >= MAXPOS) {
  74.     stmt = &(*stmt)->next;
  75.     }
  76.  
  77.     /*
  78.      * If end of list (empty list), make entry.
  79.      */
  80.     if (*stmt == NULL) {
  81. #ifdef STANDALONE
  82.     (*stmt) = (struct stmt *)malloc(sizeof **stmt);
  83. #else
  84.     (*stmt) = memalloc(sizeof **stmt);
  85. #endif
  86.     (*stmt)->next = NULL;
  87.     (*stmt)->pos = 0;
  88.     (*stmt)->line = line;
  89.     }
  90.  
  91.     (*stmt)->txt[(*stmt)->pos++] = ch;
  92. }
  93.  
  94. /*******************************************************************
  95.  *              S T M T _ W R I T E
  96.  *              -------------------
  97.  *
  98.  *******************************************************************/
  99. void
  100. stmt_write(struct stmt *stmt, FILE *fp)
  101. {
  102.     char *p;
  103.     if (stmt == NULL) return;
  104.  
  105. #ifndef STANDALONE
  106.     fprintf(fp, "#line %d \"%s\"\n", stmt->line, filename);
  107. #endif
  108.  
  109.     /*
  110.      * Skip first new line.
  111.      */
  112.     if (stmt)
  113.     {
  114.     /*
  115.      * Check that line begins with a newline (possibly 
  116.      * leading blanks).
  117.      */
  118.     p = stmt->txt;
  119.     while(p < stmt->txt + stmt->pos && isspace(*p) && *p != '\n')
  120.     {
  121.         p++;
  122.     }
  123.  
  124.     /*
  125.      * If that is the case then remove them from output.
  126.      */
  127.     if (p < stmt->txt + stmt->pos && *p == '\n') {
  128.         p++;
  129.         stmt->pos -= p - stmt->txt;
  130.         fprintf(fp, "%.*s", stmt->pos, p);
  131.     }
  132.     else {
  133.         fprintf(fp, "%.*s", stmt->pos, stmt->txt);
  134.     }
  135.     stmt = stmt->next;
  136.     }
  137.  
  138.     for(; stmt != NULL; stmt = stmt->next) {
  139.     fprintf(fp, "%.*s", stmt->pos, stmt->txt);
  140.     }
  141. }
  142.  
  143. #ifdef STANDALONE
  144. main()
  145. {
  146.     char token[1000];
  147.     char space[1000];
  148.     struct stmt *stmt = NULL;
  149.     in = stdin;
  150.  
  151.     while(GetTok(token, space))
  152.     {
  153.     stmt_add(&stmt, token, space);
  154.     }
  155.  
  156.     stmt_write(stmt, stdout);
  157. }
  158. #endif
  159.