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

  1. #line 8 "mnt.nw"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include <ctype.h>
  7. #include "modules.h"
  8. #include "modtrees.h"
  9. #include "notangle.h"
  10. #include "errors.h"
  11. #include "columns.h"
  12. #include "strsave.h"
  13.  
  14. #line 47 "mnt.nw"
  15. void add_uses_to_usecounts(Module mp);
  16. void emit_if_unused_and_conforming(Module mp);
  17. #line 79 "mnt.nw"
  18. static void emitfile(char *modname);
  19. #line 174 "mnt.nw"
  20. #ifdef TEMPNAM
  21. extern char *tempnam (const char *dir, const char *pfx);        /* temp file in dir */
  22. #else
  23. #define tempnam(DIR,PFX) (strsave(tmpnam(NULL)))
  24. #endif
  25.  
  26. #line 22 "mnt.nw"
  27. #define Clocformat "#line %L \"%F\"%N"
  28. static char *locformat = Clocformat;
  29. #ifdef macintosh
  30. #include <QuickDraw.h>
  31. #endif
  32.  
  33. main(int argc, char **argv) {
  34.     int i;
  35.  
  36. #ifdef macintosh
  37.     InitGraf(&qd.thePort);
  38. #endif
  39.  
  40.     tabsize = 0;  /* default for nt is not to use tabs */
  41.  
  42.     
  43. #line 41 "mnt.nw"
  44. read_defs(stdin);
  45. apply_each_module(remove_final_newline);
  46. #line 31 "mnt.nw"
  47.     for (i=1; i<argc; i++) 
  48.       switch (*argv[i]) {
  49.         case '-': 
  50. #line 146 "mnt.nw"
  51.     switch (*++argv[i]) {
  52.         case 'a':
  53.             if (strcmp(argv[i], "all"))
  54.                 errormsg(Warning, "Ignoring unknown option -%s", argv[i]);
  55.             else {
  56. #line 44 "mnt.nw"
  57. apply_each_module(add_uses_to_usecounts);
  58. apply_each_module(emit_if_unused_and_conforming);
  59. #line 150 "mnt.nw"
  60.                                                     }
  61.             break;
  62.         case 't': /* set tab size or turn off */
  63.             if (isdigit(argv[i][1]))
  64.                 tabsize = atoi(argv[i]+1);
  65.             else if (argv[i][1]==0)
  66.                 tabsize = 0;            /* no tabs */
  67.             else 
  68.                 errormsg(Error, "%s: ill-formed option %s\n", argv[0], argv[i]);
  69.             break;          
  70.         case 'L': /* have a #line number format */
  71.             locformat = argv[i] + 1;
  72.             if (!*locformat) locformat = Clocformat;
  73.             break;
  74.         default:
  75.             errormsg(Warning, "Ignoring unknown option -%s", argv[i]);
  76.      }
  77. #line 33 "mnt.nw"
  78.                                                                    break;
  79.         default:  emitfile(argv[i]);                               break;
  80.       }
  81.     exit(errorlevel);
  82.     return errorlevel;          /* slay warning */
  83. }
  84. #line 50 "mnt.nw"
  85. void add_uses_to_usecounts(Module mp) {
  86.     Module used;
  87.     struct modpart *p;
  88.     for (p=mp->head; p!=NULL; p=p->next)
  89.         if (p->ptype == MODULE) {
  90.             used = lookup(p->contents);
  91.             if (used != NULL)
  92.                 used->usecount++;
  93.         }
  94. }
  95. #line 66 "mnt.nw"
  96. void emit_if_unused_and_conforming(Module mp) {
  97.     char *index;
  98.     if (mp->usecount == 0 && strpbrk(mp->name, " \n\t\v\r\f") == NULL)
  99.         if (index = strpbrk(mp->name, "[](){}!$&<>*?;|^`'\\\""),
  100.             index == NULL || index[0] == '*' && index[1] == 0)
  101.             emitfile(mp->name);
  102.         else 
  103.             errormsg(Error, "<<%s>> cannot be an output chunk; "
  104.                             "it contains a metacharacter", mp->name);
  105. }
  106. #line 81 "mnt.nw"
  107. static void emitfile(char *modname) { 
  108.   Module root = lookup(modname);
  109.   char *tempname = tempnam(".", 0);
  110.   FILE *fp;
  111.   char *lfmt, *filename;
  112.   
  113. #line 102 "mnt.nw"
  114. { int n = strlen(modname) - 1;
  115.   if (n >= 0 && modname[n] == '*') {
  116.     lfmt = locformat;
  117.     filename = strsave(modname);
  118.     filename[n] = 0;
  119.   } else {
  120.     lfmt = "";
  121.     filename = modname;
  122.   }
  123. }
  124. #line 87 "mnt.nw"
  125.   
  126. #line 141 "mnt.nw"
  127. if (root == NULL) {
  128.   errormsg(Error, "Chunk <<%s>> is undefined", filename);
  129.   return;
  130. }
  131. #line 88 "mnt.nw"
  132.   fp = fopen(tempname, "w");
  133.   if (fp == NULL) errormsg(Fatal, "Can't open temporary file %s", tempname);
  134.   
  135. #line 113 "mnt.nw"
  136. resetloc();
  137. (void) expand(root, 0, 0, 0, lfmt, fp);
  138. putc('\n', fp);
  139. fclose(fp);
  140. #line 91 "mnt.nw"
  141.   
  142. #line 119 "mnt.nw"
  143. { FILE *dest, *tmp;
  144.   dest = fopen(filename, "r");
  145.   if (dest != NULL) {
  146.     int x, y;
  147.     tmp = fopen(tempname, "r");
  148.     assert(tmp);
  149.     do { 
  150.       x = getc(tmp);
  151.       y = getc(dest);
  152.     } while (x == y && x != EOF);
  153.     fclose(tmp);
  154.     fclose(dest);
  155.     if (x == y) {
  156.       remove(tempname);
  157.       return;
  158.     }
  159.   }
  160. }
  161. #line 92 "mnt.nw"
  162.   remove(filename);
  163.   if (rename(tempname, filename) != 0) { /* different file systems? (may have to copy) */
  164.     FILE *fp = fopen(filename, "w");
  165.     if (fp == NULL) {remove(tempname); 
  166. #line 138 "mnt.nw"
  167. errormsg(Error, "Can't open output file %s", filename);
  168. return;
  169. #line 95 "mnt.nw"
  170.                                                                                      }
  171.     
  172. #line 113 "mnt.nw"
  173. resetloc();
  174. (void) expand(root, 0, 0, 0, lfmt, fp);
  175. putc('\n', fp);
  176. fclose(fp);
  177. #line 97 "mnt.nw"
  178.     remove(tempname);
  179.   }
  180. }
  181.