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

  1. #line 4 "finduses.nw"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "errors.h"
  7. #include "match.h"
  8. #include "getline.h"
  9. #include "recognize.h"
  10. #line 18 "finduses.nw"
  11. static Recognizer nwindex;
  12. #define ALPHANUM "_'@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#"
  13. #define SYMBOLS "!%^&*-+:=|~<>./?`"
  14. /* note $ and \ both delimiters */
  15. #line 26 "finduses.nw"
  16. static int showquotes = 1;
  17. #line 90 "finduses.nw"
  18. typedef struct line_and_outfile {
  19.     char *line;
  20.     FILE *out;
  21. } LineOut;
  22. #line 61 "finduses.nw"
  23. static void read_ids(FILE *in);
  24. #line 95 "finduses.nw"
  25. static void add_use_markers(FILE *in, FILE *out);
  26. #line 148 "finduses.nw"
  27. static void write_index_use(void *closure, char *id, char *instance);
  28. static char *emit_up_to(FILE *f, char *s, char *limit);
  29. #line 30 "finduses.nw"
  30. #ifdef macintosh
  31. #include <QuickDraw.h>
  32. #endif
  33.  
  34. main(int argc, char **argv) {
  35.     FILE *fp;
  36.     char *myself = *argv;
  37.     int i;
  38.  
  39. #ifdef macintosh
  40.     InitGraf(&qd.thePort);
  41. #endif
  42.  
  43.     for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1] != 0; i++)
  44.         if (!strcmp(argv[i], "-noquote"))
  45.             showquotes = 0;
  46.         else
  47.             errormsg(Error, "%s: unknown option -%c\n", myself, argv[i][1]);
  48.     nwindex = new_recognizer(ALPHANUM, SYMBOLS);
  49.     if (i == argc) {
  50.        
  51. #line 72 "finduses.nw"
  52. {   FILE *tmp = tmpfile();
  53.     char *line;
  54.     if (tmp == NULL) 
  55. #line 151 "finduses.nw"
  56. errormsg(Fatal, "%s: couldn't open temporary file\n");
  57. #line 75 "finduses.nw"
  58.     while ((line = getline(stdin)) != NULL) {
  59.         if (fputs(line, tmp) == EOF) 
  60. #line 153 "finduses.nw"
  61. errormsg(Fatal, "%s: error writing temporary file\n");
  62. #line 77 "finduses.nw"
  63.         if (is_index(line, "defn")) {
  64.             if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
  65.             add_ident(nwindex, line+1+5+1+4+1);
  66.         } else if (is_index(line, "localdefn")) {
  67.             if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
  68.             add_ident(nwindex, line+1+5+1+9+1);
  69.         }
  70.     }
  71.     rewind(tmp);
  72.     stop_adding(nwindex);
  73.     add_use_markers(tmp, stdout);
  74. }
  75. #line 43 "finduses.nw"
  76.     } else {
  77.        
  78. #line 53 "finduses.nw"
  79. for (; i < argc; i++)
  80.     if ((fp=fopen(argv[i],"r"))==NULL)
  81.         errormsg(Error, "%s: couldn't open file %s\n", myself, argv[i]);
  82.     else {
  83.         read_ids(fp);
  84.         fclose(fp);
  85.     }
  86. #line 45 "finduses.nw"
  87.        stop_adding(nwindex);
  88.        add_use_markers(stdin, stdout);
  89.     }
  90.     exit(errorlevel);
  91.     return errorlevel;          /* slay warning */
  92. }
  93. #line 63 "finduses.nw"
  94. static void read_ids(FILE *in) {
  95.     char *line;
  96.     while ((line = getline(in)) != NULL) {
  97.         if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
  98.         add_ident(nwindex, line);
  99.     }
  100. }
  101. #line 97 "finduses.nw"
  102. static void add_use_markers(FILE *in, FILE *out) {
  103.     char *line;
  104.     int incode = 0;
  105.     LineOut info; info.line = (char *)0; info.out = out;
  106.     
  107.     while ((line = getline(in)) != NULL) {
  108.         if (is_begin(line, "code") || showquotes && is_keyword(line, "quote"))
  109.             incode = 1;
  110.         else if (is_end(line, "code") || is_keyword(line, "endquote"))
  111.             incode = 0;
  112.         if (is_keyword(line, "text") && incode) {
  113.             info.line = line + 6; /* skip "@text " */
  114.             search_for_ident(nwindex, line, write_index_use, &info);
  115.             if (*info.line && *info.line != '\n') 
  116.                 fprintf(out, "@text %s", info.line);    /* has newline */
  117.         } else
  118.             fprintf(out, "%s", line);
  119.     }       
  120. }
  121. #line 128 "finduses.nw"
  122. static void write_index_use(void *closure, char *id, char *instance) {
  123.   LineOut *info = (LineOut *) closure;
  124.   info->line = emit_up_to(info->out, info->line, instance);
  125.   fprintf(info->out, "@index use %s\n", id);
  126.   info->line = emit_up_to(info->out, info->line, instance + strlen(id));
  127. }
  128. #line 136 "finduses.nw"
  129. static char *emit_up_to(FILE *f, char *s, char *limit) {
  130.   if (s < limit) {
  131.     char saved = *limit;
  132.     *limit = 0;
  133.     fprintf(f, "@text %s\n", s);
  134.     *limit = saved;
  135.     return limit;
  136.   } else {
  137.     return s;
  138.   }
  139. }
  140.