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

  1. ############################################################################ 
  2. #    Name:     ansi.icn
  3. #    Title:     ANSI-based terminal control library
  4. #    Author:  Richard Goerwitz, based on an earlier version by
  5. #           Ralph E. Griswold
  6. #    Version: 1.1
  7. #
  8. #    Date:     June 28, 1991
  9. ############################################################################ 
  10. #     This package of procedures implements a subset of the ANSI terminal
  11. #  control sequences.  The names of the procedures are taken directly from
  12. #  the ANSI names.  If it is necessary to use these routines with non-ANSI
  13. #  devices, link in iolib.icn, and (optionally) iscreen.icn as well.  Use
  14. #  will be made of whatever routines are made available via either of these
  15. #  libraries.  Be careful of naming conflicts if you link in iscreen.icn.
  16. #  It contains procedures like "clear" and "boldface."
  17. #
  18. #     CUB(i)        Moves the cursor left i columns
  19. #     CUD(i)        Moves the cursor down i rows
  20. #     CUF(i)        Moves the cursor right i columns
  21. #     CUP(i,j)    Moves the cursor to row i, column j
  22. #     CUU(i)        Moves the cursor up i rows
  23. #     ED(i)        Erases screen: i = 0, cursor to end; i = 1,
  24. #               beginning to cursor; i = 2, all (default 2)
  25. #     EL(i)        Erases data in cursor row: i = 0, cursor to
  26. #               end; i = 1, beginning to cursor; i = 2, all
  27. #               (default 0)
  28. #     SGR(i)        Sets video attributes: 0 = off; 1 = bold; 4 =
  29. #               underscore; 5 = blink; 7 = reverse (default
  30. #               0)    
  31. #
  32. #     Note that not all so-called ANSI terminals support every ANSI
  33. #  screen control sequence - not even the limited subset included in
  34. #  this file.
  35. #
  36. #     If you plan on using these routines with non-ANSI magic-cookie
  37. #  terminals (e.g. a Wyse-50) then it is strongly recommended that you
  38. #  link in iolib or itlib *and* iscreen (not just iolib or itlib by
  39. #  itself).  The routines WILL WORK with most magic cookie terminals;
  40. #  they just don't always get all the modes displayed (because they
  41. #  are basically too busy erasing the cookies).
  42. #
  43. ############################################################################ 
  44. #
  45. #  Links: iolib or itlib, iscreen (all optional)
  46. #
  47. ############################################################################ 
  48. #
  49. #  For DOS, or any system using ANSI-conformant output devices, there
  50. #  is no need to link any routines in.
  51. #
  52. #  For Unix systems, you may choose to link in itlib or iolib, and (if
  53. #  desired) iscreen as well.  Some of these may be in the IPL.  You can
  54. #  get any that aren't from Richard Goerwitz (goer@sophist.uchicago.edu).
  55. #
  56. ############################################################################ 
  57.  
  58. procedure _isANSI()
  59.     static isANSI
  60.     initial {
  61.     if find("MS-DOS",&features) then {
  62.         isANSI := 1
  63.     } else {
  64.         if type(getname) == "procedure" then {
  65.         if find("ansi",map(getname())) | getname() == "li"
  66.         then isANSI := 1
  67.         else isANSI := &null
  68.         } else {
  69.         # We'll take a chance on the user knowing what he/she
  70.         # is doing.
  71.         isANSI := 1
  72.         # If you're not so confident, comment out the following
  73.         # line:
  74.         # stop("_isANSI:  you need to link itlib or iolib")
  75.         }
  76.     }
  77.     }
  78.     return \isANSI
  79. end
  80.  
  81. procedure CUD(i)
  82.     if _isANSI()
  83.     then writes("\^[[",i,"B")
  84.     else {
  85.     iputs(igoto(getval("DO"),i)) | {
  86.         every 1 to i do
  87.         iputs(getval("do")) | stop("CUD:  no do capability")
  88.     }
  89.     }
  90.     return
  91. end
  92.  
  93. procedure CUB(i)
  94.     if _isANSI()
  95.     then writes("\^[[",i,"D")
  96.     else {
  97.     iputs(igoto(getval("LE"),i)) | {
  98.         every 1 to i do
  99.         iputs(getval("le")) | stop("CUB:  no le capability")
  100.     }
  101.     }
  102.     return
  103. end
  104.  
  105. procedure CUF(i)
  106.     if _isANSI()
  107.     then writes("\^[[",i,"C")
  108.     else {
  109.     iputs(igoto(getval("RI"),i)) | {
  110.         every 1 to i do
  111.         iputs(getval("nd")) | stop("CUF:  no nd capability")
  112.     }
  113.     }
  114.     return
  115. end
  116.  
  117. procedure CUP(i,j)
  118.     if _isANSI()
  119.     then writes("\^[[",i,";",j,"H")
  120.     else iputs(igoto(getval("cm"), j, i)) | stop("CUP:  no cm capability")
  121.     return
  122. end
  123.  
  124. procedure CUU(i)
  125.     if _isANSI()
  126.     then writes("\^[[",i,"A")
  127.     else {
  128.     iputs(igoto(getval("UP"),i)) | {
  129.         every 1 to i do
  130.         iputs(getval("up")) | stop("CUU:  no up capability")
  131.     }
  132.     }
  133.     return
  134. end
  135.  
  136. procedure ED(i)
  137.     /i := 2
  138.     if _isANSI() then {
  139.     writes("\^[[",i,"J")
  140.     } else {
  141.     case i of {
  142.         0:  iputs(getval("cd")) | stop("ED:  no cd capability")
  143.         1:  stop("ED:  termcap doesn't specify capability")
  144.         2:  {
  145.         if type(emphasize) == "procedure" then clear()
  146.         else iputs(getval("cl")) | stop("ED:  no cl capability")
  147.         }
  148.         default:  stop("ED:  unknown clear code, ",i)
  149.     }
  150.     }
  151.    return
  152. end
  153.  
  154. procedure EL(i)
  155.     /i := 0
  156.     if _isANSI() then {
  157.     if i = 0
  158.     then writes("\^[[K")
  159.     else writes("\^[[",i,"K")
  160.     } else {
  161.     case i of {
  162.         0:  iputs(getval("ce")) | stop("EL:  no ce capability")
  163.         1:  stop("EL:  termcap doesn't specify capability")
  164.         2:  stop("EL:  try using CUP to go to col 1, then EL(0)")
  165.         default:  stop("EL:  unknown line clear code, ",i)
  166.     }
  167.     }
  168.    return
  169. end
  170.  
  171. procedure SGR(i)
  172.  
  173.     static isISCR
  174.     initial {
  175.     if type(emphasize) == "procedure"
  176.     then isISCR := 1
  177.     }
  178.  
  179.     /i := 0
  180.     if _isANSI() then {
  181.     writes("\^[[",i,"m")
  182.     } else {
  183.     case i of {
  184.         0: (\isISCR, normal()) | {
  185.         every iputs(getval("me"|"so"|"ue"))
  186.         }
  187.         1: (\isISCR, boldface()) | {
  188.         iputs(getval("md"|"so"|"us"))
  189.         }
  190.         4: (\isISCR, underline()) | {
  191.         iputs(getval("us"|"md"|"so"))
  192.         }
  193.         5: (\isISCR, blink()) | {
  194.         iputs(getval("mb"|"us"|"me"|"so"))
  195.         }
  196.         7: (\isISCR, emphasize()) | {
  197.         iputs(getval("so"|"me"|"ue"))
  198.         }
  199.         default:  stop("SGR:  unknown mode, ",i)
  200.     }
  201.     }
  202.    return
  203. end
  204.