% -*- mode: Noweb; noweb-code-mode: c-mode; c-indent-level: 4 -*-
\section{Scanning for uses of identifiers}
\subsection{Main program}
<<*>>=
static char rcsid[] = "$Id: finduses.nw,v 1.22 2008/10/06 01:03:05 nr Exp nr $";
static char rcsname[] = "$Name: v2_12 $";
static struct keepalive { char *s; struct keepalive *p; } keepalive[] =
  { {rcsid, keepalive}, {rcsname, keepalive} }; /* avoid warnings */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "errors.h"
#include "match.h"
#include "getline.h"
#include "recognize.h"
@
These choices of alphanumerics and symbols seem to work for most languages.
Making [[@]] alphanumeric helps {\LaTeX}, and making [[#]]
alphanumeric helps avoid false hits on C preprocessor directives like
[[#define]] and [[#include]].
<<*>>=
static Recognizer nwindex;
#define ALPHANUM "_'@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#"
#define SYMBOLS "!%^&*-+:=|~<>./?`"
/* note $ and \ both delimiters */
@ 

By default, find uses within quoted code ([[[[...]]]]).
<<*>>=
static int showquotes = 1;
<<typedefs>>
<<local prototypes>>
<<*>>=
int main(int argc, char **argv) {
    FILE *fp;
    int i;

    progname = argv[0];	
    for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1] != 0; i++)
        if (!strcmp(argv[i], "-noquote"))
            showquotes = 0;
        else
            errormsg(Error, "%s: unknown option %s\n", progname, argv[i]);
    nwindex = new_recognizer(ALPHANUM, SYMBOLS);
    if (i == argc) {
       <<add uses to stdin, grabbing defns from stdin>>
    } else {
       <<read identifiers to be defined from files named in [[argv]]>>
       stop_adding(nwindex);
       add_use_markers(stdin, stdout);
    }
    nowebexit(NULL);
    return errorlevel;          /* slay warning */
}
@
<<read identifiers to be defined from files named in [[argv]]>>=
for (; i < argc; i++)
    if ((fp=fopen(argv[i],"r"))==NULL)
        errormsg(Error, "%s: couldn't open file %s\n", progname, argv[i]);
    else {
        read_ids(fp);
        fclose(fp);
    }
<<local prototypes>>=
static void read_ids(FILE *in);
<<*>>=
static void read_ids(FILE *in) {
    char *line;
    while ((line = getline_nw(in)) != NULL) {
        if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
        add_ident(nwindex, line);
    }
}
@ 
<<add uses to stdin, grabbing defns from stdin>>=
{   FILE *tmp = tmpfile();
    char *line;
    if (tmp == NULL) <<complain about opening temp file and exit>>
    while ((line = getline_nw(stdin)) != NULL) {
        if (fputs(line, tmp) == EOF) <<complain about writing temp file and exit>>
        if (is_index(line, "defn")) {
            if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
            add_ident(nwindex, line+1+5+1+4+1);
        } else if (is_index(line, "localdefn")) {
            if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
            add_ident(nwindex, line+1+5+1+9+1);
        } else if (is_keyword(line, "fatal")) {
	    exit(1);
	}
    }
    rewind(tmp);
    stop_adding(nwindex);
    add_use_markers(tmp, stdout);
}
<<typedefs>>=
typedef struct line_and_outfile {
    char *line;
    FILE *out;
} LineOut;
<<local prototypes>>=
static void add_use_markers(FILE *in, FILE *out);
<<*>>=
static void add_use_markers(FILE *in, FILE *out) {
    char *line;
    int incode = 0;
    LineOut info; info.line = (char *)0; info.out = out;
    
    while ((line = getline_nw(in)) != NULL) {
        if (is_begin(line, "code") || (showquotes && is_keyword(line, "quote")))
            incode = 1;
        else if (is_end(line, "code") || is_keyword(line, "endquote"))
            incode = 0;
        if (is_keyword(line, "text") && incode) {
            info.line = line + 6; /* skip "@text " */
            search_for_ident(nwindex, line, write_index_use, &info);
            if (*info.line && *info.line != '\n') 
                fprintf(out, "@text %s", info.line);    /* has newline */
        } else
            fprintf(out, "%s", line);
    }       
}
@ 

We gradually cut out the uses, and the tail of the line is left in [[info.line]],
to be printed by the code above.
There's a tricky bug lurking here---if one identifier is a prefix of another,
but both are recognized (as with the C$++$ [[::]] separator), we have to avoid
writing them both out in full, because that would duplicate text unnecessarily.
As a result, we always emit the line in pieces.
The function [[emit_up_to(f, s, limit)]] emits the piece of the string [[s]] up to 
but not including [[limit]], if any.
It returns [[limit]] or [[s]], whichever is greater.
<<*>>=
static void write_index_use(void *closure, char *id, char *instance) {
  LineOut *info = (LineOut *) closure;
  info->line = emit_up_to(info->out, info->line, instance);
  fprintf(info->out, "@index use %s\n", id);
  info->line = emit_up_to(info->out, info->line, instance + strlen(id));
}
<<*>>=
static char *emit_up_to(FILE *f, char *s, char *limit) {
  if (s < limit) {
    char saved = *limit;
    *limit = 0;
    fprintf(f, "@text %s\n", s);
    *limit = saved;
    return limit;
  } else {
    return s;
  }
}
<<local prototypes>>=
static void write_index_use(void *closure, char *id, char *instance);
static char *emit_up_to(FILE *f, char *s, char *limit);
<<complain about opening temp file and exit>>=
errormsg(Fatal, "%s: couldn't open temporary file\n", progname);
<<complain about writing temp file and exit>>=
errormsg(Fatal, "%s: error writing temporary file\n", progname);
