File: mkindex.awk

package info (click to toggle)
elk 3.99.7-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze
  • size: 5,068 kB
  • ctags: 3,303
  • sloc: ansic: 22,248; sh: 8,936; lisp: 6,208; makefile: 851; awk: 154; cpp: 92
file content (107 lines) | stat: -rwxr-xr-x 2,681 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#! /usr/bin/awk -f

# mkindex.awk: Copy named files or standard input (if no arguments are
# given) to standard output, replacing @[something] by troff macro calls.
# $Id: mkindex.awk 207 2003-09-27 12:05:59Z sam $
#
# These replacements are performed:
#
# @[.something]   -->  \n.Ix "something"\nsomething
# @[!something]   -->  \n.Id "something"\nsomething
#
# @[.some|thing]  -->  \n.Ix "thing, some"\nsome thing
# @[!some|thing]  -->  \n.Id "thing, some"\nsome thing
#
# @[.=something]  -->  \n.Ix "something"\n
# @[!=something]  -->  \n.Id "something"\n
#
#
#  1)  initial \n is omitted at the beginning of an output line
#
#  2)  initial \n is prefixed by \c if @[ follows "(" in input
#
#  3)  omit final \n if @[...] is at end of input line
#
#  4)  within @[...], \] is replaced by ]
#
#  5)  in the macro argument ("something"), all sequences of the form
#      `` or '' or \fX or \% are removed

{
    need_nl = 0;
    line = $0;

    while(line != "") {

        nxt = index(line, "@[")

        if(nxt == 0) {
            printf "%s", line;
            break;
        }

        if(nxt > 1) {
            if(substr(line, nxt - 1, 1) == "(") {
                need_c = 1;
            }
            need_nl = 1;
            printf "%s", substr(line, 1, nxt - 1);
            line = substr(line, nxt);
            continue;
        }

        tmp = substr(line, 1, 3);
        if(tmp == "@[.") {
            macro = "Ix";
        } else
        if(tmp == "@[.") {
            macro = "Id";
        } else {
            printf "error: invalid index %s\n", tmp > "/dev/stderr";
            exit 1;
        }

        end = match(line, "[^\\\\]]");

        if(end == 0) {
            printf "error: unfinished @[\n" > "/dev/stderr";
            exit 1;
        }

        inx = substr(line, 4, end - 3);
        gsub("\\\\]", "]", inx);
        arg = inx;
        gsub("(\\\\f.|''|``|\\\\%)", "", arg);

        if(arg == "") {
            printf "error: empty index\n" > "/dev/stderr";
            exit 1;
        }

        if(need_c) { printf "\\c"; }
        if(need_nl) { printf "\n"; }

        printf ".%s ", macro;

        line = substr(line, end + 2);

        if(sub("^=", "", arg)) {
            printf "\"%s\"", arg;
            if(line != "") {
                printf "\n";
                need_nl = 0;
                sub("^ ", "", line);
            }
        } else if (arg ~ /[|]/) {
            q = arg; sub("[^|]*[|]", "", q); sub("[|].*", "", arg);
            printf "\"%s, %s\"\n%s %s", q, arg, arg, q;
            need_nl = 1;
        } else {
            printf "\"%s\"\n%s", arg, inx;
            need_nl = 1;
        }
    }

    printf "\n";
}