summaryrefslogtreecommitdiff
path: root/ipl/progs/itab.icn
blob: c81a38b5b9daa06a7078e6a1529d44d5719691a0 (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
############################################################################
#
#	File:     itab.icn
#
#	Subject:  Program to entab an Icon program
#
#	Author:   Robert J. Alexander
#
#	Date:     August 30, 1990
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  itab -- Entab an Icon program, leaving quoted strings alone.
#
#	itab [options] [source-program...]
#
#	options:
#		-i	Input tab spacing (default 8)
#		-o	Outut tab spacing (default 8)
#
#  Observes Icon Programming Language conventions for escapes and
#  continuations in string constants.  If no source-program names are
#  given, standard input is "itabbed" to standard output.
#
############################################################################
#
#  Links: options, io
#
############################################################################

link options
link io

global mapchars,intabs,outtabs

procedure main(arg)

   local opt, fn, f, outfn, outf, f1, f2, buf

   opt := options(arg,"i+o+")
   intabs := (\opt["i"] | 8) + 1
   outtabs := (\opt["o"] | 8) + 1
   if *arg = 0 then itab(&input,&output)
   else every fn := !arg do {
      if not (fn[-4:0] == ".icn") then fn ||:= ".icn"
      write(&errout,"Entabbing ",fn)
      f := open(fn) | stop("Can't open input file ",fn)
      outfn := fn || ".temp"
      outf := open(outfn,"w") | stop("Can't open output file ",outfn)
      itab(f,outf)
      close(outf)
      close(f)
      fcopy(outfn,fn)
      remove(outfn)
      }
end


procedure itab(f,outf)
   local line,c,nonwhite,comment,delim
   line := ""
   while c := readx(f) do {
      if not any(' \t',c) then nonwhite := 1
      case c of {
	 "\n": {
	    write(outf,map(entab(line,outtabs),\mapchars," \t") | line)
	    line := ""
	    nonwhite := comment := &null
	    }
	 "'" | "\"": {
	    if /comment then
		  (/delim := c) | (if c == delim then delim := &null)
	    line ||:= c
	    }
	 "\\": line ||:= c || if /comment then readx(f) else ""
	 "#": {
	    if /delim then comment := c
	    line ||:= c
	    }
	 default: {
	    line ||:= if /comment & \delim & \nonwhite & \mapchars then
		  map(c," \t",mapchars) else c
	    }
	 }
      }
   return
end


procedure readx(f)
   static buf,printchars
   initial {
      buf := ""
      printchars := &cset[33:128]
      }
   if *buf = 0 then {
      buf := detab(read(f),intabs) || "\n" | fail
      mapchars := (printchars -- buf)[1+:2] | &null
      }
   return 1(.buf[1],buf[1] := "")
end