summaryrefslogtreecommitdiff
path: root/web/noweb/src/awk/noindex
blob: ba530f5c5cc6feafb6cf1dfc12223f1449991f71 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/sh

if [ $# -ne 1 ]; then
  echo "Usage: `basename $0` file[.tex]" 1>&2
  exit 1
fi

nawk 'BEGIN { infile="'"$1"'"
              if (infile ~ /\.tex$/) {
                  infile = substr(infile, 1, length(infile)-4) ".aux"
              } else if (infile !~ /\.aux$/) {
                  infile = infile ".aux"
              }
              idx[0] = ""
              delete idx[0]
              gobble(infile)
              alphasort(idx)
              outname = substr(infile, 1, length(infile)-4) ".nwi"
              last = ""
              for (i = 0; i < count; i++) {
                out = stripcount(sorted[i])
                if (last != out) {
                  print out > outname
                  last = out
              #    <show sort key [[i]]>
                }
              } 
              exit
            }
function gobble(name, infile, rc, tag) {
  for (rc = (getline line < name); rc > 0; rc = (getline line < name)) {
    if (line ~ /^\\@input{[^}]*}$/)
        gobble(substr(line, 9, length(line)-9))
    else if (line ~ /^\\nwixadds{/) {
        count++
        tag = "000000" count
        tag = substr(tag, length(tag)-6+1)
        idx[count] = tag " " substr(line, 11)
    }
  }
  if (rc < 0) print "No file " name "."
  else close(name)
  return
}
function stripcount(s) {
  sub(/^[0-9]+/, "", s)
  sub(/ +/, "", s)
  return "\\nwixaddsx{" s
}
function alphasort(a, x, n) {
  n = 0
  for (x in a) 
    n = insertitem(a[x], n)
  finish_sorting(n)
  return n
}
function insertitem(x, n, i, tmp) {
  sorted[n] = x
  sortkeys[n] = sortkey(x)
  return n+1
}
function finish_sorting(n) {
  firstwork = nextwork = 0
  addquick(0, n)
  while(nextwork > firstwork)
    qsort()
}
function addquick(l, r) {
  workq[nextwork++] = l
  workq[nextwork++] = r
}
function qsort(l, r,    mid, i, last) {
  l = workq[firstwork]
  delete workq[firstwork]
  firstwork++
  r = workq[firstwork]
  delete workq[firstwork]
  firstwork++
  if (r - l < 10) 
    isort(l, r)
  else {
    mid = l + int((r - l) * rand())
    swap(l, mid)
    last = l
    for (i = l+1; i < r; i++)
       if (sortkeys[i] <  sortkeys[l] ||
           sortkeys[i] == sortkeys[l] && sorted[i] < sorted[l])
              swap(++last, i)
    swap(l, last)
    addquick(l, last)
    addquick(last+1, r)    
  }
}
function isort(l, r,    n) {
  for (n = l + 1; n < r; n++)
    for (i = n; i > l && (sortkeys[i] <  sortkeys[i-1] ||
                          sortkeys[i] == sortkeys[i-1] && sorted[i] < sorted[i-1]); i--)
      swap(i, i-1)
}
function swap(i, j, tmp) {
    tmp = sortkeys [i]; sortkeys [i] = sortkeys [j]; sortkeys [j] = tmp
    tmp = sorted[i]; sorted[i] = sorted[j]; sorted[j] = tmp
}
function sortkey(s, count) {
  match(s, /^[0-9]+/)
  count = substr(s, RSTART, RLENGTH)
  sub(/^[0-9]+ */, "", s)
  if (s ~ /c}/) {
      return firstkey(substr(s, 3)) "\n" count
  } else if (s ~ /i}/) {
      return firstkey(substr(s, 3)) "\n" count
  } else {
      print "sortkey handed non-chunk and non-index: " s
      exit 1
  }
}
function firstkey(s, r, openbrace) {
  if (s !~ /^{{/) {
     print "key \"" substr(s, 1, 6) "...\" is ill-formatted"
     exit 1
  }
  sub (/^{{/, "", s)
  gsub(/\\([a-zA-Z]+|.) */, "", s)      # kill control sequences
  openbrace = 1
  r = ""
  while (openbrace > 0)
    if (match(s, /^[^{}]*/) <= 0) 
      openbrace--
    else {
      r = r substr(s, RSTART, RLENGTH)
      c = substr(s, RSTART+RLENGTH, 1)
      s = substr(s, RSTART+RLENGTH+1)
      if (c == "}") openbrace--
      else openbrace++
      if (openbrace > 0) r = r c
    }
  return alphabet(r) "\n" r
}
function alphabet(s, r) {
  r = ""
  while (match(s, /[a-zA-Z \t]/) > 0) {
    s = substr(s, RSTART)
    c = substr(s, 1, 1)
    if (c == " " || c == "\t") {
      r = r " "
      sub(/^[ \t]+/, "", s)
    } else {
      match(s, /^[a-zA-Z]+/)
      r = r substr(s, RSTART, RLENGTH)
      s = substr(s, RSTART+RLENGTH)
    }
  }
  sub(/^ +/, "", r)
  return r
}' 1>&2