[racket] Call racket with long code from racket

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Sep 8 17:41:20 EDT 2011

20 minutes ago, Jordan Schatz wrote:
> Niitsuma,
> > I am trying to call racket with long code from racket
> You might have the wrong idea... calling another racket process to
> evaluate some code is probably doing things the hard way.

Perhaps a more appropriate tool is the sandbox -- in addition to
protection, it can be used to have code that is more isolate.  (For
example, see `call-with-trusted-sandbox-configuration'.)

> But to answer your question, you can embed " and other special chars
> in a string by escaping them with \
> [...]

In general, doing this with shell syntax can be dangerous for your
hair.  It is *much* better to use functions like `system*' that accept
the arguments directly, not relying on any shell quoting rules.

If you really want to know why...

The characters that need to be quoted can be different from
shell to shell, which means that backslash-quoting is not too
reliable.  A more common solution is to use single quotes, which are
"almost completely" literal, so you don't need backslashes ... except
that because backslashes are plain characters, this:

  echo 'foo\'bar'

will not print "foo'bar".  To get a single quote into the string, the
common idiom is hairy -- you need to use a ' to stop the previous
quote and another to start a new one that goes to the end:

  echo 'foo''bar'

And now all you need is to add a quote in the middle...  But adding a
quote character is tricky since it is used as a quotation notation
too.  The solution is to double-quote the quote: "'".  So the whole
thing becomes:

  echo 'foo'"'"'bar'

which is finally the right thing.  Obviously in this case you could
have just used echo "foo'bar", which makes this be more popular with
generated code that needs to be quoted.  For extra credit, write shell
code that does that quoting...  In theory it's easy: you just need to
replace all 's by '"'"'s:

  sed -e s/'/'"'"'/g

but in practice, you now need to quote all *those* quotes.  For the
sake of the hair of anyone who reads it, here's the solution:

  sed -e 's/'"'"'/'"'"'"'"'"'"'"'"'/g'

which you can verify by using *it* on the above:

  $ sed -e 's/'"'"'/'"'"'"'"'"'"'"'"'/g'
  sed -e s/'/'"'"'/g                         # <--- input
  sed -e s/'"'"'/'"'"'"'"'"'"'"'"'/g         # <--- result

Now the only thing that's missing is for that `sed' rule to add the
quotes around the argument, but I'll stop here.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.