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

  1. #line 66 "markmain.nw"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "errors.h"
  7. #include "markup.h"
  8. #include "getline.h"
  9. #include "columns.h"
  10.  
  11. #line 34 "markmain.nw"
  12. typedef enum state {Code=1, Docs=2, CodeIndex=3} State;
  13. typedef enum mark {Begin=1, End=2} Mark;
  14. typedef enum index {Defn=1, Use=2, Newline=3} Index;
  15.  
  16. static char *states[]  = { "bad state", "code", "docs", "code" };
  17. static char *marks[]   = { "bad mark", "begin", "end" };
  18. static char *indices[] = { "bad index", "defn", "use", "nl" };
  19. static char low_at_sign = '@';
  20.  
  21. static void print_state(FILE *out, Mark be, State state, int count) {
  22.     fprintf(out, "%c%s %s %d\n", low_at_sign, marks[be], states[state], count);
  23. }
  24.  
  25. static void print_index(FILE *out, Index idx, char *arg) {
  26.     if (arg)
  27.         fprintf(out, "%cindex %s %s\n", low_at_sign, indices[idx], arg);
  28.     else
  29.         fprintf(out, "%cindex %s\n",    low_at_sign, indices[idx]);
  30. }
  31.  
  32. static void print_pair(FILE *out, char *name, char *value) {
  33.     if (value) {
  34.         int last=strlen(value)-1;
  35.         if (last>=0 && value[last]=='\n')
  36.             fprintf(out, "%c%s %s%cnl\n", low_at_sign, name, value, low_at_sign);
  37.         else
  38.             fprintf(out, "%c%s %s\n", low_at_sign, name, value);
  39.     } else
  40.         fprintf(out, "%c%s\n", low_at_sign, name);
  41. }
  42.  
  43. #line 77 "markmain.nw"
  44. void markup (FILE* in, FILE *out, char *filename) {
  45.     State state = Docs;         /* what we are reading */
  46.     int quoting = 0;            /* currently quoting code? */
  47.     int count = 0;              /* number of current chunk, from 0 */
  48.     int missing_newline;        /* was last line missing a trailing \n? */
  49.     int lineno = 0;             /* number of lines read */
  50.     int last_open_quote;        /* line number of last unmatched open quote */
  51.  
  52.     char *line;                 /* part of line up to mark (or end) */
  53.     char *mark;                 /* potentially marked part of line */
  54. #define MAX_MODNAME 255
  55.     char modname[MAX_MODNAME+1] = ""; /* name of module currently being read, 
  56.                                          [[""]] if no module is being read */ 
  57.  
  58.     
  59. #line 118 "markmain.nw"
  60. print_pair(out, "file", filename);
  61. print_state(out, Begin, state, count);
  62. while ((line = getline_expand(in)) != NULL) {
  63.     lineno++;
  64.     
  65. #line 161 "markmain.nw"
  66. missing_newline = line[strlen(line)-1] != '\n';
  67. #line 123 "markmain.nw"
  68.     if (starts_code(line, filename, lineno)) {
  69.         
  70. #line 216 "markmain.nw"
  71. if (quoting) {
  72.     errorat(filename, last_open_quote, Warning, "open quote `[[' never closed");
  73.     quoting = 0;
  74. }
  75. #line 125 "markmain.nw"
  76.         print_state(out, End, state, count);
  77.         count++;
  78.         state = Code;
  79.         print_state(out, Begin, state, count);
  80.         getmodname(modname,MAX_MODNAME,line);
  81.         print_pair(out,"defn",modname);
  82.         print_pair(out,"nl",0);     /* could be implicit but this is better */
  83.     } else if (is_def(line)) {
  84.         
  85. #line 202 "markmain.nw"
  86. line = remove_def_marker(line);
  87. while (*line && isspace(*line)) line++;
  88. while (*line) {
  89.   char tmp;
  90.   char *s = line+1;
  91.   while (*s && !isspace(*s)) s++;
  92.   tmp = *s; *s = 0;
  93.   print_index(out, Defn, line);
  94.   *s = tmp;
  95.   while (isspace(*s)) s++;
  96.   line = s;
  97. }
  98. print_index(out, Newline, 0);
  99. #line 134 "markmain.nw"
  100.         if (state == Code)
  101.             state = CodeIndex;
  102.     } else {
  103.         if (starts_doc(line) || state == CodeIndex) {
  104.             
  105. #line 216 "markmain.nw"
  106. if (quoting) {
  107.     errorat(filename, last_open_quote, Warning, "open quote `[[' never closed");
  108.     quoting = 0;
  109. }
  110. #line 139 "markmain.nw"
  111.             print_state(out, End, state, count);
  112.             count++;
  113.             state = Docs;       /* always reading docs after a stop */
  114.             print_state(out, Begin, state, count);
  115.             if (starts_doc(line))
  116.                 line = first_doc_line(line);
  117.         }
  118.         switch(state) {
  119.             case Code: 
  120. #line 168 "markmain.nw"
  121. while ((mark = mod_start(line,1)) != NULL) { 
  122.     if (*line) print_pair(out, "text",unescape(line)); 
  123.                                    /* emit string before module */
  124.     if ((line=mod_end(mark,1))==NULL) { /* no matching >>; treat << as text */
  125.         line = mark - 2;        /* back up; point to << */
  126.         *line = '<';            /* restore missing < (was '\0') */
  127.         break;
  128.     } else {  
  129.         print_pair(out, "use",mark);       /* emit the module */
  130.     }
  131. }
  132. if (*line) print_pair(out, "text",unescape(line)); 
  133.                                   /* contribute the trailing string */
  134. #line 147 "markmain.nw"
  135.                                                break;
  136.             case Docs: 
  137. #line 184 "markmain.nw"
  138. for (; (mark = (quoting ? quote_end : quote_start)(line,1)) != NULL; 
  139.        line = mark) {
  140.     if (!quoting) {
  141. #line 197 "markmain.nw"
  142. if (mod_start(line,0) != NULL)
  143.     errorat(filename, lineno, Warning, 
  144.             "Warning: chunk name in documentation (or missing = in definition)");
  145. #line 186 "markmain.nw"
  146.                                                              }
  147.     if (*line) print_pair(out, "text", unescape(line));
  148.     print_pair(out, quoting ? "endquote" : "quote", 0);
  149.     if (quoting = !quoting) last_open_quote = lineno;
  150. }
  151. if (!quoting) {
  152. #line 197 "markmain.nw"
  153. if (mod_start(line,0) != NULL)
  154.     errorat(filename, lineno, Warning, 
  155.             "Warning: chunk name in documentation (or missing = in definition)");
  156. #line 191 "markmain.nw"
  157.                                                          }
  158. if (*line) print_pair(out, "text", unescape(line)); 
  159. #line 148 "markmain.nw"
  160.                                                         break;
  161.             default: impossible("bad state");
  162.         }    
  163.     }
  164. }
  165. #line 163 "markmain.nw"
  166. if (missing_newline) print_pair(out, "nl",0);
  167. #line 154 "markmain.nw"
  168. print_state(out, End, state, count);
  169. #line 92 "markmain.nw"
  170. }
  171. #line 225 "markmain.nw"
  172. #ifdef macintosh
  173. #include <QuickDraw.h>
  174. #endif
  175.  
  176. main(int argc, char **argv) {
  177.     FILE *fp;
  178.     char *myself=*argv;
  179.     int i;
  180.  
  181. #ifdef macintosh
  182.     InitGraf(&qd.thePort);
  183. #endif
  184.  
  185.     for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1] != 0; i++)
  186.         switch(argv[i][1]) {
  187.             case 't': 
  188. #line 256 "markmain.nw"
  189. if (isdigit(argv[i][2]))
  190.     tabsize = atoi(argv[i]+2);
  191. else if (argv[i][2]==0)
  192.     tabsize = 0;                /* no tabs */
  193. else 
  194.     errormsg(Error, "%s: ill-formed option %s\n", myself, argv[i]);
  195. #line 233 "markmain.nw"
  196.                                         break;
  197.             default : errormsg(Error, "%s: unknown option -%c\n", myself, argv[i][1]);
  198.                       break;
  199.         }
  200.     if (i < argc)
  201.         for (; i < argc; i++) {
  202.             if (!strcmp(argv[i], "-")) {
  203.                 markup(stdin,stdout,"");
  204.             } else 
  205.                 if ((fp=fopen(argv[i],"r"))==NULL)
  206.                     errormsg(Error, "%s: couldn't open file %s\n", myself, argv[i]);
  207.                 else {
  208.                     markup(fp,stdout,argv[i]);
  209.                     fclose(fp);
  210.                 }
  211.         }
  212.     else
  213.         markup(stdin,stdout,"");
  214.     exit(errorlevel);
  215.     return errorlevel;          /* slay warning */
  216. }
  217.