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

  1. ############################################################################
  2. #
  3. #    Name:    shquote.icn
  4. #
  5. #    Title:    Quote words for UNIX-like shells
  6. #
  7. #    Author:    Robert J. Alexander
  8. #
  9. #    Date:    September 18, 1990
  10. #
  11. ############################################################################
  12. #
  13. #  The following procedures are useful for writing Icon programs that
  14. #  generate shell commands.  Certain characters cannot appear in the
  15. #  open in strings that are to be interpreted as "words" by command
  16. #  shells.  This family of procedures assists in quoting such strings so
  17. #  that they will be interpreted as single words.  Quoting characters
  18. #  are applied only if necessary -- if strings need no quoting they are
  19. #  returned unchanged.
  20. #
  21. #  shquote(s1,c1,s2) : s3 -- Produces a version of s1 which is properly
  22. #  quoted for a UNIX-type command shell. c1 is the cset of characters
  23. #  that must be quoted; s2 is the escape character. Appropriate defaults
  24. #  for omitted c1 and s2 are provided for the Bourne shell (sh).
  25. #
  26. #  cshquote(s1) : s2 -- Produces a version of s1 which is properly
  27. #  quoted for the c-shell (csh).
  28. #
  29. #  mpwquote(s1) : s2 -- Produces a version of s1 which is properly
  30. #  quoted for the Macintosh Programmer's Workshop shell (MPW Shell).
  31. #
  32. #  dequote(s1,s2) : s3 -- Produces the UNIX-style command line word s1
  33. #  with any quoting characters removed. s2 is the escape character
  34. #  required by the shell (s2 defaults the the usual UNIX escape
  35. #  character, the backslash "\\").
  36. #
  37. ############################################################################
  38.  
  39. procedure shquote(s,quotedChar,escapeChar)
  40.    /quotedChar := '\t\n\r $"#&\'()*;<>?\\^`|'
  41.    /escapeChar := "\\"
  42.    if not upto(quotedChar,s) then return s
  43.    s ? {
  44.       s := "'"
  45.       while s ||:= tab(find("'")) || "'" || escapeChar || "''" & move(1)
  46.       s ||:= tab(0) || "'"
  47.       }
  48.    return s 
  49. end
  50.  
  51. procedure cshquote(s)
  52.    return shquote(s,'\t\n $"#&\'()*;<>?[\\`|~')
  53. end
  54.  
  55. procedure mpwquote(s)
  56.    #
  57.    #  The following are Macintosh Option- characters that have special
  58.    #  meaning to the MPW Shell.  They are represented here as Icon
  59.    #  escape sequences rather than as themselves since some
  60.    #  ASCII-oriented mailers change characters that have their
  61.    #  high-order bits set.
  62.    #
  63.    #  \xa8    circled r
  64.    #  \xb3    >= (I/O redirection)
  65.    #  \xb6    lower case delta (escape character)
  66.    #  \xb7    upper case sigma
  67.    #  \xc5    lower case phi
  68.    #  \xc7    << (I/O redirection)
  69.    #  \xc8    >> (I/O redirection)
  70.    #  \xc9    ...
  71.    #
  72.    return shquote(s,
  73.        '\0\t\n\r "#&\'()*+/;<>?[\\]`{|}\xa8\xb3\xb6\xb7\xc5\xc7\xc8\xc9',
  74.        "\xb6")
  75. end
  76.  
  77. procedure dequote(s,escapeChar)
  78.    local quoteChars,c,d
  79.    /escapeChar := "\\"
  80.    quoteChars := '"\'' ++ escapeChar
  81.    s ? {
  82.       s := ""
  83.       while s ||:= tab(upto(quoteChars)) do {
  84.          c := move(1)
  85.          if c == escapeChar then s ||:= move(1)
  86.          else {
  87.         if \d then (s ||:= d ~== c) | (d := &null)
  88.         else d := c
  89.             }
  90.          }
  91.       return s || tab(0)
  92.       }
  93. end
  94.