summaryrefslogtreecommitdiff
path: root/web/noweb/contrib/norman/scopehack.icn
blob: d837a2ff61b954525c9046c7daba263e7f5a3532 (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
# scopehack; a replacement for totex but splitting output into multiple files

global totex

procedure main(args)
  totex := "PATH=/usr/local/noweb/lib:/usr/lib/noweb:$PATH totex"  
  every totex ||:= " '" || !args || "'"

  lines := []
  file 

  while line := read() do {
    line ? 
      if ="@fatal" then exit(1)
      else if ="@file " then
        if =\file & pos(0) then # no change
          &null
        else {
          flush(file, lines)
          file := tab(0)
	}
    put(lines, line)
  }
  flush(file, lines)
end

procedure flush(file, lines)
  if /file & *lines > 0 then
    stop("First line is not @file")
  else if *lines = 0 then
    return
  else {
    outfile := suffex(file) || ".tex"
    p := open(totex || " > " || outfile, "wp") | stop ("cannot run ", totex)
    while write(p, get(lines))
    close(p)
    return
  }
end

procedure suffex(s)
  return reverse (reverse(s) ? {tab(upto('.')) & ="."; tab(0)})
end