home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / ITLIBDOS.ICN < prev    next >
Text File  |  1991-09-05  |  15KB  |  467 lines

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