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 / procs / shquote.icn < prev    next >
Text File  |  2000-07-29  |  4KB  |  148 lines

  1. ############################################################################
  2. #
  3. #    File:     shquote.icn
  4. #
  5. #    Subject:  Procedures to quote word for UNIX-like shells
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     December 30, 1993
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  The following procedures are useful for writing Icon programs that
  18. #  generate shell commands.  Certain characters cannot appear in the
  19. #  open in strings that are to be interpreted as "words" by command
  20. #  shells.  This family of procedures assists in quoting such strings so
  21. #  that they will be interpreted as single words.  Quoting characters
  22. #  are applied only if necessary -- if strings need no quoting they are
  23. #  returned unchanged.
  24. #
  25. #  shquote(s1, s2, ..., sN) :  s -- Produces a string of words s1, s2,
  26. #  ..., sN that are properly separated and quoted for the Bourne Shell
  27. #  (sh).
  28. #
  29. #  cshquote(s1, s2, ..., sN) :  s -- Produces a string of words s1, s2, ..., sN 
  30. #  that are properly separated and quoted for the C-Shell (csh).
  31. #
  32. #  mpwquote(s1, s2, ..., sN) :  s -- Produces a string of words s1, s2,
  33. #  ..., sN that are properly separated and quoted for the Macintosh
  34. #  Programmer's Workshop shell (MPW Shell).
  35. #
  36. #  dequote(s1,s2) : s3 -- Produces the UNIX-style command line word s1
  37. #  with any quoting characters removed. s2 is the escape character
  38. #  required by the shell (s2 defaults the the usual UNIX escape
  39. #  character, the backslash "\\").
  40. #
  41. ############################################################################
  42.  
  43. procedure shquote(s[])
  44.    return shquote_words(s)
  45. end
  46.  
  47. procedure cshquote(s[])
  48.    s := shquote_words(s,'\t\n $"#&\'()*;<>?[\\`|~')
  49.    #
  50.    #  But backslashes before any bangs (!).
  51.    #
  52.    s ? {
  53.       s := ""
  54.       while s ||:= tab(find("!")) do {
  55.      s ||:= "\\" || move(1)
  56.      }
  57.       s ||:= tab(0)
  58.       }
  59.    return s
  60. end
  61.  
  62. procedure mpwquote(s[])
  63.    #
  64.    #  The following are Macintosh Option- characters that have special
  65.    #  meaning to the MPW Shell.  They are represented here as Icon
  66.    #  escape sequences rather than as themselves since some
  67.    #  ASCII-oriented mailers change characters that have their
  68.    #  high-order bits set.
  69.    #
  70.    #  \xa8  circled r
  71.    #  \xb3  >= (I/O redirection)
  72.    #  \xb6  lower case delta (escape character)
  73.    #  \xb7  upper case sigma
  74.    #  \xc5  lower case phi
  75.    #  \xc7  << (I/O redirection)
  76.    #  \xc8  >> (I/O redirection)
  77.    #  \xc9  ...
  78.    #
  79.    local result
  80.    result := ""
  81.    #
  82.    # If there is a "return" in the string, it must be replaced by an
  83.    # escape sequence outside of the single quotes.
  84.    #
  85.    shquote_words(s,
  86.      '\0\t\n\r "#&\'()*+/;<>?[\\]`{|}\xa8\xb3\xb6\xb7\xc5\xc7\xc8\xc9',
  87.      "\xb6") ? {
  88.       while result ||:= tab(find("\x0d")) do {
  89.      result ||:= "'\xb6n'"
  90.      move (1)
  91.      }
  92.       result ||:= tab(0)
  93.       }
  94.    return result
  95. end
  96.  
  97. procedure shquote_words(wordList,quotedChars,escapeString,sepString)
  98.    local s, result, sep
  99.    /quotedChars := '\t\n\r $"#&\'()*;<>?[\\^`|'
  100.    /escapeString := "\\"
  101.    /sepString := " "
  102.    result := sep := ""
  103.    every s := !wordList do {
  104.       if s == "" | upto(quotedChars,s) then {
  105.      s ? {
  106.         s := "'"
  107.         while s ||:= tab(find("'")) || "'" || escapeString || "''" & move(1)
  108.         s ||:= tab(0) || "'"
  109.         }
  110.      }
  111.       result ||:= sep || s
  112.       sep := sepString
  113.       }
  114.    return result 
  115. end
  116.  
  117. procedure dequote(s,escapeString,escapeProc)
  118.    local quoteChars,c,d
  119.    /escapeString := "\\"
  120.    /escapeProc := 1
  121.    quoteChars := '"\'' ++ escapeString[1]
  122.    s ? {
  123.       s := ""
  124.       while s ||:= tab(upto(quoteChars)) do {
  125.      if =escapeString then s ||:= (if d === "'" then escapeString else 
  126. escapeProc(move(1)))
  127.      else {
  128.         c := move(1)
  129.         (/d := c) | (s ||:= d ~== c) | (d := &null)
  130.         }
  131.      }
  132.       return s || tab(0)
  133.       }
  134. end
  135.  
  136. procedure mpwdequote(s)
  137.    return dequote(s,"\xb6",mpw_escape_proc)
  138. end
  139.  
  140. procedure mpw_escape_proc(ch)
  141.    return case ch of {
  142.       "n": "\n"
  143.       "t": "\t"
  144.       "f": "\f"
  145.       default: ch
  146.       }
  147. end
  148.