home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / itab.icn < prev    next >
Text File  |  2000-07-29  |  3KB  |  106 lines

  1. ############################################################################
  2. #
  3. #    File:     itab.icn
  4. #
  5. #    Subject:  Program to entab an Icon program
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     August 30, 1990
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  itab -- Entab an Icon program, leaving quoted strings alone.
  18. #
  19. #    itab [options] [source-program...]
  20. #
  21. #    options:
  22. #        -i    Input tab spacing (default 8)
  23. #        -o    Outut tab spacing (default 8)
  24. #
  25. #  Observes Icon Programming Language conventions for escapes and
  26. #  continuations in string constants.  If no source-program names are
  27. #  given, standard input is "itabbed" to standard output.
  28. #
  29. ############################################################################
  30. #
  31. #  Links: options, io
  32. #
  33. ############################################################################
  34.  
  35. link options
  36. link io
  37.  
  38. global mapchars,intabs,outtabs
  39.  
  40. procedure main(arg)
  41.  
  42.    local opt, fn, f, outfn, outf, f1, f2, buf
  43.  
  44.    opt := options(arg,"i+o+")
  45.    intabs := (\opt["i"] | 8) + 1
  46.    outtabs := (\opt["o"] | 8) + 1
  47.    if *arg = 0 then itab(&input,&output)
  48.    else every fn := !arg do {
  49.       if not (fn[-4:0] == ".icn") then fn ||:= ".icn"
  50.       write(&errout,"Entabbing ",fn)
  51.       f := open(fn) | stop("Can't open input file ",fn)
  52.       outfn := fn || ".temp"
  53.       outf := open(outfn,"w") | stop("Can't open output file ",outfn)
  54.       itab(f,outf)
  55.       close(outf)
  56.       close(f)
  57.       fcopy(outfn,fn)
  58.       remove(outfn)
  59.       }
  60. end
  61.  
  62.  
  63. procedure itab(f,outf)
  64.    local line,c,nonwhite,comment,delim
  65.    line := ""
  66.    while c := readx(f) do {
  67.       if not any(' \t',c) then nonwhite := 1
  68.       case c of {
  69.      "\n": {
  70.         write(outf,map(entab(line,outtabs),\mapchars," \t") | line)
  71.         line := ""
  72.         nonwhite := comment := &null
  73.         }
  74.      "'" | "\"": {
  75.         if /comment then
  76.           (/delim := c) | (if c == delim then delim := &null)
  77.         line ||:= c
  78.         }
  79.      "\\": line ||:= c || if /comment then readx(f) else ""
  80.      "#": {
  81.         if /delim then comment := c
  82.         line ||:= c
  83.         }
  84.      default: {
  85.         line ||:= if /comment & \delim & \nonwhite & \mapchars then
  86.           map(c," \t",mapchars) else c
  87.         }
  88.      }
  89.       }
  90.    return
  91. end
  92.  
  93.  
  94. procedure readx(f)
  95.    static buf,printchars
  96.    initial {
  97.       buf := ""
  98.       printchars := &cset[33:128]
  99.       }
  100.    if *buf = 0 then {
  101.       buf := detab(read(f),intabs) || "\n" | fail
  102.       mapchars := (printchars -- buf)[1+:2] | &null
  103.       }
  104.    return 1(.buf[1],buf[1] := "")
  105. end
  106.