~vcs-imports/gawk/master

« back to all changes in this revision

Viewing changes to pc/awklib/igawk.awk

  • Committer: Arnold D. Robbins
  • Date: 2010-07-16 09:41:09 UTC
  • Revision ID: git-v1:8c042f99cc7465c86351d21331a129111b75345d
Tags: gawk-3.0.0
Move to gawk-3.0.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# igawk.awk
 
2
# process @include directives
 
3
 
 
4
function pathto(file,    i, t, junk)
 
5
{
 
6
    if (index(file, "/") != 0)
 
7
        return file
 
8
 
 
9
    for (i = 1; i <= ndirs; i++) {
 
10
        t = (pathlist[i] "/" file)
 
11
        if ((getline junk < t) > 0) {
 
12
            # found it
 
13
            close(t)
 
14
            return t
 
15
        }
 
16
    }
 
17
    return ""
 
18
}
 
19
BEGIN {
 
20
    path = ENVIRON["AWKPATH"]
 
21
    ndirs = split(path, pathlist, ";")
 
22
    for (i = 1; i <= ndirs; i++) {
 
23
        if (pathlist[i] == "")
 
24
            pathlist[i] = "."
 
25
    }
 
26
    stackptr = 0
 
27
    input[stackptr] = ARGV[1] # ARGV[1] is first file
 
28
 
 
29
    for (; stackptr >= 0; stackptr--) {
 
30
        while ((getline < input[stackptr]) > 0) {
 
31
            if (tolower($1) != "@include") {
 
32
                print
 
33
                continue
 
34
            }
 
35
            fpath = pathto($2)
 
36
            if (fpath == "") {
 
37
                printf("igawk:%s:%d: cannot find %s\n", \
 
38
                    input[stackptr], FNR, $2) > "/dev/stderr"
 
39
                continue
 
40
            }
 
41
            if (! (fpath in processed)) {
 
42
                processed[fpath] = input[stackptr]
 
43
                input[++stackptr] = fpath
 
44
            } else
 
45
                print $2, "included in", input[stackptr], \
 
46
                    "already included in", \
 
47
                    processed[fpath] > "/dev/stderr"
 
48
        }
 
49
        close(input[stackptr])
 
50
    }
 
51
}