home *** CD-ROM | disk | FTP | other *** search
- ############################################################################
- #
- # File: shquote.icn
- #
- # Subject: Procedures to quote word for UNIX-like shells
- #
- # Author: Robert J. Alexander
- #
- # Date: April 2, 1991
- #
- ###########################################################################
- #
- # The following procedures are useful for writing Icon programs that
- # generate shell commands. Certain characters cannot appear in the
- # open in strings that are to be interpreted as "words" by command
- # shells. This family of procedures assists in quoting such strings so
- # that they will be interpreted as single words. Quoting characters
- # are applied only if necessary -- if strings need no quoting they are
- # returned unchanged.
- #
- # shquote(s1,c1,s2) : s3 -- Produces a version of s1 which is properly
- # quoted for a UNIX-type command shell. c1 is the cset of characters
- # that must be quoted; s2 is the escape character. Appropriate defaults
- # for omitted c1 and s2 are provided for the Bourne shell (sh).
- #
- # cshquote(s1) : s2 -- Produces a version of s1 which is properly
- # quoted for the c-shell (csh).
- #
- # mpwquote(s1) : s2 -- Produces a version of s1 which is properly
- # quoted for the Macintosh Programmer's Workshop shell (MPW Shell).
- #
- # dequote(s1,s2) : s3 -- Produces the UNIX-style command line word s1
- # with any quoting characters removed. s2 is the escape character
- # required by the shell (s2 defaults the the usual UNIX escape
- # character, the backslash "\\").
- #
- ############################################################################
-
- procedure shquote(s,quotedChar,escapeChar)
- /quotedChar := '\t\n\r $"#&\'()*;<>?[\\^`|'
- /escapeChar := "\\"
- if s ~== "" & not upto(quotedChar,s) then return s
- s ? {
- s := "'"
- while s ||:= tab(find("'")) || "'" || escapeChar || "''" & move(1)
- s ||:= tab(0) || "'"
- }
- return s
- end
-
- procedure cshquote(s)
- s := shquote(s,'\t\n $"#&\'()*;<>?[\\`|~')
- #
- # But backslashes before any bangs (!).
- #
- s ? {
- s := ""
- while s ||:= tab(find("!")) do {
- s ||:= "\\" || move(1)
- }
- s ||:= tab(0)
- }
- return s
- end
-
- procedure mpwquote(s)
- #
- # The following are Macintosh Option- characters that have special
- # meaning to the MPW Shell. They are represented here as Icon
- # escape sequences rather than as themselves since some
- # ASCII-oriented mailers change characters that have their
- # high-order bits set.
- #
- # \xa8 circled r
- # \xb3 >= (I/O redirection)
- # \xb6 lower case delta (escape character)
- # \xb7 upper case sigma
- # \xc5 lower case phi
- # \xc7 << (I/O redirection)
- # \xc8 >> (I/O redirection)
- # \xc9 ...
- #
- return shquote(s,
- '\0\t\n\r "#&\'()*+/;<>?[\\]`{|}\xa8\xb3\xb6\xb7\xc5\xc7\xc8\xc9',
- "\xb6")
- end
-
- procedure dequote(s,escapeChar)
- local quoteChars,c,d
- /escapeChar := "\\"
- quoteChars := '"\'' ++ escapeChar
- s ? {
- s := ""
- while s ||:= tab(upto(quoteChars)) do {
- c := move(1)
- if c == escapeChar then s ||:= move(1)
- else {
- if \d then (s ||:= d ~== c) | (d := &null)
- else d := c
- }
- }
- return s || tab(0)
- }
- end
-
-