home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / IOLIB.ICN < prev    next >
Text File  |  1991-07-13  |  18KB  |  542 lines

  1. ########################################################################
  2. #    
  3. #    Name:     iolib.icn
  4. #    
  5. #    Title:     Icon termlib-type tools for MS-DOS and UNIX
  6. #    
  7. #    Author:     Richard L. Goerwitz (with help from Norman Azadian)
  8. #
  9. #    Date:     June 3, 1991
  10. #
  11. #    Version: 1.9
  12. #
  13. #########################################################################
  14. #
  15. #  The following library represents a series of rough functional
  16. #  equivalents to the standard Unix low-level termcap routines.  It is
  17. #  not meant as an exact termlib clone.  Nor is it enhanced to take
  18. #  care of magic cookie terminals, terminals that use \D in their
  19. #  termcap entries, or archaic terminals that require padding.  This
  20. #  library is geared mainly for use with ANSI and VT-100 devices.
  21. #  Note that this file may, in most instances, be used in place of the
  22. #  older UNIX-only itlib.icn file.  It essentially replaces the DOS-
  23. #  only itlibdos routines.  For DOS users not familiar with the whole
  24. #  notion of generalized screen I/O, I've included extra documentation
  25. #  below.  Please read it.
  26. #
  27. #  The sole disadvantage of this over the old itlib routines is that
  28. #  iolib.icn cannot deal with archaic or arcane UNIX terminals and/or
  29. #  odd system file arrangements.  Note that because these routines
  30. #  ignore padding, they can (unlike itlib.icn) be run on the NeXT and
  31. #  other systems which fail to implement the -g option of the stty
  32. #  command.  Iolib.icn is also simpler and faster than itlib.icn.
  33. #
  34. #  I want to thank Norman Azadian for suggesting the whole idea of
  35. #  combining itlib.icn and itlibdos.icn into one distribution, for
  36. #  suggesting things like letting drive specifications appear in DOS
  37. #  TERMCAP environment variables, and for finding several bugs (e.g.
  38. #  the lack of support for %2 and %3 in cm).  Although he is loathe
  39. #  to accept this credit, I think he deserves it.
  40. #
  41. #########################################################################
  42. #
  43. #  Contents:
  44. #
  45. #  setname(term)
  46. #    Use only if you wish to initialize itermlib for a terminal
  47. #  other than what your current environment specifies.  "Term" is the
  48. #  name of the termcap entry to use.  Normally this initialization is
  49. #  done automatically, and need not concern the user.
  50. #
  51. #  getval(id)
  52. #    Works something like tgetnum, tgetflag, and tgetstr.  In the
  53. #  spirit of Icon, all three have been collapsed into one routine.
  54. #  Integer valued caps are returned as integers, strings as strings,
  55. #  and flags as records (if a flag is set, then type(flag) will return
  56. #  "true").  Absence of a given capability is signalled by procedure
  57. #  failure.
  58. #
  59. #  igoto(cm,destcol,destline) - NB:  default 1 offset (*not* zero)!
  60. #    Analogous to tgoto.  "Cm" is the cursor movement command for
  61. #  the current terminal, as obtained via getval("cm").  Igoto()
  62. #  returns a string which, when output via iputs, will cause the
  63. #  cursor to move to column "destcol" and line "destline."  Column and
  64. #  line are always calculated using a *one* offset.  This is far more
  65. #  Iconish than the normal zero offset used by tgoto.  If you want to
  66. #  go to the first square on your screen, then include in your program
  67. #  "iputs(igoto(getval("cm"),1,1))."
  68. #
  69. #  iputs(cp,affcnt)
  70. #    Equivalent to tputs.  "Cp" is a string obtained via getval(),
  71. #  or, in the case of "cm," via igoto(getval("cm"),x,y).  Affcnt is a
  72. #  count of affected lines.  It is completely irrelevant for most
  73. #  modern terminals, and is supplied here merely for the sake of
  74. #  backward compatibility with itlib, a UNIX-only version of these
  75. #  routines (one which handles padding on archaic terminals).
  76. #
  77. ##########################################################################
  78. #
  79. #  Notes for MS-DOS users:
  80. #
  81. #    There are two basic reasons for using the I/O routines
  82. #  contained in this package.  First, by using a set of generalized
  83. #  routines, your code will become much more readable.  Secondly, by
  84. #  using a high level interface, you can avoid the cardinal
  85. #  programming error of hard coding things like screen length and
  86. #  escape codes into your programs.
  87. #
  88. #    To use this collection of programs, you must do two things.
  89. #  First, you must add the line "device=ansi.sys" (or the name of some
  90. #  other driver, like zansi.sys, nansi.sys, or nnansi.sys [=new
  91. #  nansi.sys]) to your config.sys file.  Secondly, you must add two
  92. #  lines to your autoexec.bat file: 1) "set TERM=ansi-mono" and 2)
  93. #  "set TERMCAP=\location\termcap."  The purpose of setting the TERM
  94. #  variable is to tell this program what driver you are using.  If you
  95. #  have a color system, you could use "ansi-color" instead of
  96. #  "ansi-mono," although for compatibility with a broader range of
  97. #  users, it would perhaps be better to stick with mono.  The purpose
  98. #  of setting TERMCAP is to make it possible to determine where the
  99. #  termcap database file is located.  The termcap file (which should
  100. #  have been packed with this library as termcap.dos) is a short
  101. #  database of all the escape sequences used by the various terminal
  102. #  drivers.  Set TERMCAP so that it reflects the location of this file
  103. #  (which should be renamed as termcap, for the sake of consistency
  104. #  across UNIX and MS-DOS spectra).  If desired, you can also try
  105. #  using termcap2.dos.  Certain games work a lot better using this
  106. #  alternate file.  To try it out, rename it to termcap, and set
  107. #  the environment variable TERMCAP to its location.
  108. #
  109. #    Although the authors make no pretense of providing here a
  110. #  complete introduction to the format of the termcap database file,
  111. #  it will be useful, we believe, to explain a few basic facts about
  112. #  how to use this program in conjunction with it.  If, say, you want
  113. #  to clear the screen, add the line,
  114. #
  115. #    iputs(getval("cl"))
  116. #
  117. #  to your program.  The function iputs() outputs screen control
  118. #  sequences.  Getval retrieves a specific sequence from the termcap
  119. #  file.  The string "cl" is the symbol used in the termcap file to
  120. #  mark the code used to clear the screen.  By executing the
  121. #  expression "iputs(getval("cl"))," you are 1) looking up the "cl"
  122. #  (clear) code in the termcap database entry for your terminal, and
  123. #  the 2) outputting that sequence to the screen.
  124. #
  125. #    Some other useful termcap symbols are "ce" (clear to end of
  126. #  line), "ho" (go to the top left square on the screen), "so" (begin
  127. #  standout mode), and "se" (end standout mode).  To output a
  128. #  boldfaced string, str, to the screen, you would write -
  129. #
  130. #    iputs(getval("so"))
  131. #    writes(str)
  132. #    iputs(getval("se"))
  133. #
  134. #  You can also write "writes(getval("so") || str || getval("se")),
  135. #  but this would make reimplementation for UNIX terminals that
  136. #  require padding rather difficult.
  137. #
  138. #    It is also heartily to be recommended that MS-DOS programmers
  139. #  try not to assume that everyone will be using a 25-line screen.
  140. #  Most terminals are 24-line.  Some 43.  Some have variable window
  141. #  sizes.  If you want to put a status line on, say, the 2nd-to-last
  142. #  line of the screen, then determine what that line is by executing
  143. #  "getval("li")."  The termcap database holds not only string-valued
  144. #  sequences, but numeric ones as well.  The value of "li" tells you
  145. #  how many lines the terminal has (compare "co," which will tell you
  146. #  how many columns).  To go to the beginning of the second-to-last
  147. #  line on the screen, type in:
  148. #
  149. #    iputs(igoto(getval("cm"), 1, getval("li")-1))
  150. #
  151. #  The "cm" capability is a special capability, and needs to be output
  152. #  via igoto(cm,x,y), where cm is the sequence telling your computer
  153. #  to move the cursor to a specified spot, x is the column, and y is
  154. #  the row.  The expression "getval("li")-1" will return the number of
  155. #  the second-to-last line on your screen.
  156. #
  157. ##########################################################################
  158. #
  159. #  Requires: UNIX or MS-DOS, co-expressions
  160. #
  161. #  See also: itlib.icn, iscreen.icn
  162. #
  163. ##########################################################################
  164.  
  165.  
  166. global tc_table, isDOS
  167. record true()
  168.  
  169.  
  170. procedure check_features()
  171.  
  172.     initial {
  173.  
  174.     if find("UNIX",&features) then
  175.         isDOS := &null
  176.     else if find("MS-DOS", &features) then
  177.         isDOS := 1
  178.     else stop("check_features:  OS not (yet?) supported.")
  179.  
  180.     find("expressi",&features) |
  181.         er("check_features","co-expressions not implemented - &$#!",1)
  182.     }
  183.  
  184.     return
  185.  
  186. end
  187.  
  188.  
  189.  
  190. procedure setname(name)
  191.  
  192.     # Sets current terminal type to "name" and builds a new termcap
  193.     # capability database (residing in tc_table).  Fails if unable to
  194.     # find a termcap entry for terminal type "name."  If you want it
  195.     # to terminate with an error message under these circumstances,
  196.     # comment out "| fail" below, and uncomment the er() line.
  197.  
  198.     #tc_table is global
  199.     
  200.     check_features()
  201.  
  202.     tc_table := table()
  203.     tc_table := maketc_table(getentry(name)) | fail
  204.     # er("setname","no termcap entry found for "||name,3)
  205.     return "successfully reset for terminal " || name
  206.  
  207. end
  208.  
  209.  
  210.  
  211. procedure getname()
  212.  
  213.     # Getname() first checks to be sure we're running under DOS or
  214.     # UNIX, and, if so, tries to figure out what the current terminal
  215.     # type is, checking successively the value of the environment
  216.     # variable TERM, and then (under UNIX) the output of "tset -".
  217.     # Terminates with an error message if the terminal type cannot be
  218.     # ascertained.  DOS defaults to "mono."
  219.  
  220.     local term, tset_output
  221.  
  222.     check_features()
  223.  
  224.     if \isDOS then {
  225.         term := getenv("TERM") | "mono"
  226.     }
  227.     else {
  228.     if not (term := getenv("TERM")) then {
  229.         tset_output := open("/bin/tset -","pr") |
  230.         er("getname","can't find tset command",1)
  231.         term := !tset_output
  232.         close(tset_output)
  233.     }
  234.     }
  235.  
  236.     return \term |
  237.     er("getname","can't seem to determine your terminal type",1)
  238.  
  239. end
  240.  
  241.  
  242.  
  243. procedure er(func,msg,errnum)
  244.  
  245.     # short error processing utility
  246.     write(&errout,func,":  ",msg)
  247.     exit(errnum)
  248.  
  249. end
  250.  
  251.  
  252.  
  253. procedure getentry(name, termcap_string)
  254.     local entry
  255.  
  256.     # "Name" designates the current terminal type.  Getentry() scans
  257.     # the current environment for the variable TERMCAP.  If the
  258.     # TERMCAP string represents a termcap entry for a terminal of type
  259.     # "name," then getentry() returns the TERMCAP string.  Otherwise,
  260.     # getentry() will check to see if TERMCAP is a file name.  If so,
  261.     # getentry() will scan that file for an entry corresponding to
  262.     # "name."  If the TERMCAP string does not designate a filename,
  263.     # getentry() will scan the termcap file for the correct entry.
  264.     # Whatever the input file, if an entry for terminal "name" is
  265.     # found, getentry() returns that entry.  Otherwise, getentry()
  266.     # fails.
  267.  
  268.     local isFILE, f, getline, line, nm, ent1, ent2
  269.     static slash, termcap_names
  270.     initial {
  271.     if \isDOS then {
  272.         slash := "\\"
  273.         termcap_names := ["termcap","termcap.dos","termcap2.dos"]
  274.     }
  275.     else {
  276.         slash := "/"
  277.         termcap_names := ["/etc/termcap"]
  278.     }
  279.     }
  280.  
  281.  
  282.     # You can force getentry() to use a specific termcap file by cal-
  283.     # ling it with a second argument - the name of the termcap file
  284.     # to use instead of the regular one, or the one specified in the
  285.     # termcap environment variable.
  286.     /termcap_string := getenv("TERMCAP")
  287.  
  288.     if \isDOS then {
  289.     if \termcap_string then {
  290.         if termcap_string ? (
  291.          not ((tab(any(&letters)), match(":")) | match(slash)),
  292.          pos(1) | tab(find("|")+1), =name)
  293.         then return termcap_string
  294.         else isFILE := 1
  295.     }
  296.     }
  297.     else {
  298.     if \termcap_string then {
  299.         if termcap_string ? (
  300.             not match(slash), pos(1) | tab(find("|")+1), =name)
  301.         then return termcap_string
  302.         else isFILE := 1
  303.     }
  304.     }
  305.  
  306.     # The logic here probably isn't clear.  The idea is to try to use
  307.     # the termcap environment variable successively as 1) a termcap en-
  308.     # try and then 2) as a termcap file.  If neither works, 3) go to
  309.     # the /etc/termcap file.  The else clause here does 2 and, if ne-
  310.     # cessary, 3.  The "\termcap_string ? (not match..." expression
  311.     # handles 1.
  312.  
  313.     if \isFILE            # if find(slash, \termcap_string)
  314.     then f := open(termcap_string)
  315.     /f := open(!termcap_names) |
  316.     er("getentry","I can't access your termcap file.  Read iolib.icn.",1)
  317.     
  318.     getline := create read_file(f)
  319.     
  320.     while line := @getline do {
  321.     if line ? (pos(1) | tab(find("|")+1), =name, any(':|')) then {
  322.         entry := ""
  323.         while (\line | @getline) ? {
  324.         if entry ||:= 1(tab(find(":")+1), pos(0))
  325.         then {
  326.             close(f)
  327.             # if entry ends in tc= then add in the named tc entry
  328.             entry ?:= tab(find("tc=")) ||
  329.             # recursively fetch the new termcap entry
  330.             (move(3), getentry(tab(find(":"))) ?
  331.              # remove the name field from the new entry
  332.              (tab(find(":")+1), tab(0)))
  333.             return entry
  334.         }
  335.         else {
  336.             \line := &null # must precede the next line
  337.             entry ||:= trim(trim(tab(0),'\\'),':')
  338.         }
  339.         }
  340.     }
  341.     }
  342.  
  343.     close(f)
  344.     er("getentry","can't find and/or process your termcap entry",3)
  345.  
  346. end
  347.  
  348.  
  349.  
  350. procedure read_file(f)
  351.  
  352.     # Suspends all non #-initial lines in the file f.
  353.     # Removes leading tabs and spaces from lines before suspending
  354.     # them.
  355.  
  356.     local line
  357.  
  358.     \f | er("read_tcap_file","no valid termcap file found",3)
  359.     while line := read(f) do {
  360.     match("#",line) & next
  361.     line ?:= (tab(many('\t ')) | &null, tab(0))
  362.     suspend line
  363.     }
  364.  
  365.     fail
  366.  
  367. end
  368.  
  369.  
  370.  
  371. procedure maketc_table(entry)
  372.     local str
  373.  
  374.     # Maketc_table(s) (where s is a valid termcap entry for some
  375.     # terminal-type): Returns a table in which the keys are termcap
  376.     # capability designators, and the values are the entries in
  377.     # "entry" for those designators.
  378.  
  379.     local k, v
  380.  
  381.     /entry & er("maketc_table","no entry given",8)
  382.     if entry[-1] ~== ":" then entry ||:= ":"
  383.     
  384.     /tc_table := table()
  385.  
  386.     entry ? {
  387.  
  388.     tab(find(":")+1)    # tab past initial (name) field
  389.  
  390.     while tab((find(":")+1) \ 1) ? {
  391.         &subject == "" & next
  392.         if k := 1(move(2), ="=") then {
  393.         # Get rid of null padding information.  Iolib can't
  394.         # handle it (unlike itlib.icn).  Leave star in.  It
  395.         # indicates a real dinosaur terminal, and will later
  396.         # prompt an abort.
  397.         str := ="*" | ""; tab(many(&digits))
  398.         tc_table[k] := Decode(str || tab(find(":")))
  399.         }
  400.         else if k := 1(move(2), ="#")
  401.         then tc_table[k] := integer(tab(find(":")))
  402.         else if k := 1(tab(find(":")), pos(-1))
  403.         then tc_table[k] := true()
  404.         else er("maketc_table", "your termcap file has a bad entry",3)
  405.     }
  406.     }
  407.  
  408.     return tc_table
  409.  
  410. end
  411.  
  412.  
  413.  
  414. procedure getval(id)
  415.  
  416.     /tc_table := maketc_table(getentry(getname())) |
  417.     er("getval","can't make a table for your terminal",4)
  418.  
  419.     return \tc_table[id] | fail
  420.     # er("getval","the current terminal doesn't support "||id,7)
  421.  
  422. end
  423.  
  424.  
  425.  
  426. procedure Decode(s)
  427.     local new_s, chr, chr2
  428.  
  429.     # Does things like turn ^ plus a letter into a genuine control
  430.     # character.
  431.  
  432.     new_s := ""
  433.  
  434.     s ? {
  435.  
  436.     while new_s ||:= tab(upto('\\^')) do {
  437.         chr := move(1)
  438.         if chr == "\\" then {
  439.         new_s ||:= {
  440.             case chr2 := move(1) of {
  441.             "\\" : "\\"
  442.             "^"  : "^"
  443.             "E"  : "\e"
  444.             "b"  : "\b"
  445.             "f"  : "\f"
  446.             "n"  : "\n"
  447.             "r"  : "\r"
  448.             "t"  : "\t"
  449.             default : {
  450.                 if any(&digits,chr2) then {
  451.                 char(integer("8r"||chr2||move(2 to 0 by -1))) |
  452.                     er("Decode","bad termcap entry",3)
  453.                 }
  454.                else chr2
  455.             }
  456.             }
  457.         }
  458.         }
  459.         else new_s ||:= char(ord(map(move(1),&lcase,&ucase)) - 64)
  460.     }
  461.     new_s ||:= tab(0)
  462.     }
  463.  
  464.     return new_s
  465.  
  466. end
  467.  
  468.  
  469.  
  470. procedure igoto(cm,col,line)
  471.  
  472.     local colline, range, increment, padding, str, outstr, chr, x, y
  473.  
  474.     if col > (tc_table["co"]) | line > (tc_table["li"]) then {
  475.     colline := string(\col) || "," || string(\line) | string(\col|line)
  476.     range := "(" || tc_table["co"]-1 || "," || tc_table["li"]-1 || ")"
  477.     er("igoto",colline || " out of range " || (\range|""),9)
  478.     } 
  479.  
  480.     # Use the Iconish 1;1 upper left corner & not the C-ish 0 offsets
  481.     increment := -1
  482.     outstr := ""
  483.     
  484.     cm ? {
  485.     while outstr ||:= tab(find("%")) do {
  486.         tab(match("%"))
  487.         if padding := integer(tab(any('23')))
  488.         then chr := (="d" | "d")
  489.         else chr := move(1)
  490.         if case \chr of {
  491.         "." :  outstr ||:= char(line + increment)
  492.         "+" :  outstr ||:= char(line + ord(move(1)) + increment)
  493.         "d" :  {
  494.             str := string(line + increment)
  495.             outstr ||:= right(str, \padding, "0") | str
  496.         }
  497.         }
  498.         then line :=: col
  499.         else {
  500.         case chr of {
  501.             "n" :  line := ixor(line,96) & col := ixor(col,96)
  502.             "i" :  increment := 0
  503.             "r" :  line :=: col
  504.             "%" :  outstr ||:= "%"
  505.             "B" :  line := ior(ishift(line / 10, 4), line % 10)
  506.             ">" :  {
  507.             x := move(1); y := move(1)
  508.             line > ord(x) & line +:= ord(y)
  509.             &null
  510.             }
  511.         } | er("goto","bad termcap entry",5)
  512.         }
  513.     }
  514.     return outstr || tab(0)
  515.     }
  516.  
  517. end
  518.  
  519.  
  520.  
  521. procedure iputs(cp, affcnt)
  522.  
  523.     # Writes cp to the screen.  Use this instead of writes() for
  524.     # compatibility with itlib (a UNIX-only version which can handle
  525.     # albeit inelegantly) terminals that need padding.
  526.  
  527.     static num_chars
  528.     initial num_chars := &digits ++ '.'
  529.  
  530.     type(cp) == "string" |
  531.     er("iputs","you can't iputs() a non-string value!",10)
  532.  
  533.     cp ? {
  534.     if tab(many(num_chars)) & ="*" then
  535.         stop("iputs:  iolib can't use terminals that require padding.")
  536.     writes(tab(0))
  537.     }
  538.  
  539.     return
  540.  
  541. end
  542.