home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / MPW / MPW noweb 2.7 / src / c / notangle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-30  |  5.3 KB  |  190 lines  |  [TEXT/MPS ]

  1. #line 18 "notangle.nw"
  2. #define MAX_MODNAME 255
  3. #line 21 "notangle.nw"
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include "strsave.h"
  9. #include "getline.h"
  10. #include "modules.h"
  11. #include "modtrees.h"
  12. #include "errors.h"
  13. #include "match.h"
  14. #include "notangle.h"
  15.  
  16. #line 165 "notangle.nw"
  17. static void warn_dots(char *modname);          /* warn about names ending in ... */
  18.  
  19. #line 185 "notangle.nw"
  20. void insist(char *line, char *keyword, char *msg);
  21.  
  22. #line 35 "notangle.nw"
  23. void notangle (FILE* in, FILE *out, char *rootname, char *locformat) {
  24.     Module root = NULL; /* ptr to root module */
  25.  
  26.     read_defs(in);              /* read all the definitions */
  27.  
  28.     apply_each_module(remove_final_newline);
  29.                                 /* pretty up the module texts */
  30.  
  31.     root = lookup(rootname);
  32.     
  33. #line 169 "notangle.nw"
  34. if (root==NULL) {
  35.     errormsg(Fatal, "The root module <<%s>> was not defined.", rootname);
  36.     return;
  37. }
  38. #line 45 "notangle.nw"
  39.     (void) expand(root,0,0,0,locformat,out);
  40.     putc('\n',out);                     /* make output end with newline */
  41. }
  42. #line 60 "notangle.nw"
  43. void read_defs(FILE *in) {
  44.     char modname[MAX_MODNAME+1] = ""; /* name of module currently being read, 
  45.                                          [[""]] if no module is being read */ 
  46.     Module modptr = NULL;       /* ptr to current module, or NULL */
  47.     char *line = NULL;          /* buffer for input */
  48.     Location loc;
  49.  
  50.     while ((line = getline(in)) != NULL) {
  51.         
  52. #line 104 "notangle.nw"
  53. if (is_keyword(line, "nl") || is_index(line, "nl")) {
  54.     loc.lineno++;
  55. #line 116 "notangle.nw"
  56. } else if (is_keyword(line,"file")) {
  57.     
  58. #line 129 "notangle.nw"
  59. { char temp[MAX_MODNAME+1];
  60.   if (strlen(line) >= MAX_MODNAME + strlen("@file "))
  61.     overflow("file name size");
  62.   strcpy(temp,line+strlen("@file "));
  63.   temp[strlen(temp)-1]='\0';
  64.   loc.filename = strsave(temp);
  65. }
  66. #line 118 "notangle.nw"
  67.     loc.lineno = 1;
  68. } else if (is_keyword(line, "line")) {
  69.     
  70. #line 137 "notangle.nw"
  71. { char temp[MAX_MODNAME+1];
  72.   if (strlen(line) >= MAX_MODNAME + strlen("@line "))
  73.     overflow("file name size");
  74.   strcpy(temp,line+strlen("@line "));
  75.   temp[strlen(temp)-1]='\0';
  76.   
  77. #line 146 "notangle.nw"
  78. { char *p;
  79.   for (p = temp; *p; p++)
  80.     if (!isdigit(*p)) 
  81.       errormsg(Error, "non-numeric line number in `@line %s'", temp);
  82. }
  83. #line 143 "notangle.nw"
  84.   loc.lineno = atoi(temp);
  85. }
  86. #line 121 "notangle.nw"
  87.     loc.lineno--;
  88. #line 107 "notangle.nw"
  89. if (!is_begin(line, "code"))
  90.     continue;
  91. #line 69 "notangle.nw"
  92.         
  93. #line 100 "notangle.nw"
  94. do { line = getline(in);
  95. } while (line != NULL && !is_keyword(line,"defn") && !is_keyword(line,"text"));
  96. #line 70 "notangle.nw"
  97.         insist(line,"defn","code chunk had no definition line");
  98.         
  99. #line 126 "notangle.nw"
  100. strcpy(modname,line+strlen("@defn "));
  101. modname[strlen(modname)-1]='\0';
  102. #line 72 "notangle.nw"
  103.         warn_dots(modname);       /* names ending in ... aren't like web */
  104.         modptr = insert(modname); /* find or add module in table */
  105.  
  106.         line = getline(in);
  107.         insist(line,"nl","definition line not followed by newline");
  108.         loc.lineno++;
  109.         line = getline(in);
  110.         while (line != NULL && !is_end(line,"code")) {
  111.             if (is_keyword(line,"nl")) {
  112.                 addnewline(modptr);
  113.                 loc.lineno++;
  114.             } else if (is_keyword(line,"text")) {
  115.                 addstring(modptr,line+1+4+1,loc);
  116.             } else if (is_keyword(line,"use")) {
  117.                 warn_dots(line+1+3+5);
  118.                 addmodule(modptr,line+1+3+1);
  119.             } else if (is_index(line, "nl")) {
  120.                 loc.lineno++;
  121.             
  122. #line 116 "notangle.nw"
  123. } else if (is_keyword(line,"file")) {
  124.     
  125. #line 129 "notangle.nw"
  126. { char temp[MAX_MODNAME+1];
  127.   if (strlen(line) >= MAX_MODNAME + strlen("@file "))
  128.     overflow("file name size");
  129.   strcpy(temp,line+strlen("@file "));
  130.   temp[strlen(temp)-1]='\0';
  131.   loc.filename = strsave(temp);
  132. }
  133. #line 118 "notangle.nw"
  134.     loc.lineno = 1;
  135. } else if (is_keyword(line, "line")) {
  136.     
  137. #line 137 "notangle.nw"
  138. { char temp[MAX_MODNAME+1];
  139.   if (strlen(line) >= MAX_MODNAME + strlen("@line "))
  140.     overflow("file name size");
  141.   strcpy(temp,line+strlen("@line "));
  142.   temp[strlen(temp)-1]='\0';
  143.   
  144. #line 146 "notangle.nw"
  145. { char *p;
  146.   for (p = temp; *p; p++)
  147.     if (!isdigit(*p)) 
  148.       errormsg(Error, "non-numeric line number in `@line %s'", temp);
  149. }
  150. #line 143 "notangle.nw"
  151.   loc.lineno = atoi(temp);
  152. }
  153. #line 121 "notangle.nw"
  154.     loc.lineno--;
  155. #line 91 "notangle.nw"
  156.             } else if (!is_keyword(line, "index"))
  157.                 
  158. #line 187 "notangle.nw"
  159. errorat(loc.filename, loc.lineno, Error, "botched code chunk `%s'", line);
  160. #line 93 "notangle.nw"
  161.             line = getline(in);
  162.         }
  163.         
  164. #line 181 "notangle.nw"
  165. if (line==NULL) {
  166.     impossible("End of file occurred in mid-module");
  167. }
  168. #line 96 "notangle.nw"
  169.     }
  170. }
  171. #line 157 "notangle.nw"
  172. static
  173. void warn_dots(char *modname) {
  174.   if (!strcmp(modname+strlen(modname)-3,"...")) 
  175.     errormsg(Warning, "Module name <<%s>> isn't completed as in web", 
  176.              modname);
  177. }
  178. #line 174 "notangle.nw"
  179. void insist(char *line, char *keyword, char *msg) {
  180.   
  181. #line 181 "notangle.nw"
  182. if (line==NULL) {
  183.     impossible("End of file occurred in mid-module");
  184. }
  185. #line 176 "notangle.nw"
  186.   if (!is_keyword(line,keyword))
  187.     impossible(msg);
  188. }
  189.