zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: Zsh Users <zsh-users@zsh.org>
Subject: protection racket
Date: Mon, 05 Feb 2018 13:20:58 -0800	[thread overview]
Message-ID: <3c2b2846-0315-453a-e009-7a935f18dd8d@eastlink.ca> (raw)

All:

So, my command wrappers have the ability to save to history the actual 
system commands that they execute. That involves taking some command 
string and saving it to a variable, but without any expansions or 
removals of quotes and with everything being taken literally except that 
variables will be expanded.   It seems that there's no way to do this 
except by protecting various things with backslashes, and it's easy to 
understand that, eg. the pipe symbol must be backslashed so as to be 
taken as a literal character. I'm used to this, but I've tried doing it 
with a command string that involved a few 'sed's and things got out of 
control. This was the command:

     apt-cache show "$1" | egrep --color=always "^|$1" | sed -r 
'/^Package: / s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' | sed 
's/^\(Package: .*\)$/\x1b[33;1m\1\x1b[0m/'

... it just colorizes apt-cache output with two colors, (yellow being 
applied to the "Package: ..." line) and if that color is applied to the 
line if it is already colored by egrep, you first hafta remove the 
existing codes.  More trouble than it's worth, but an interesting 
exercise anyway.  However, if I want to save that entire line for 
re-execution (which happens in '_execute' function), by trial and error 
I found that I had to backslash protect characters that never seemed to 
need it before:

     _execute apt-cache show  \"$1\" \| egrep --color=always \"^\|$1\" 
\| sed -r \'/^Package: / 
s/\\x1b\\[\([0-9]\{1,2\}(\;[0-9]\{1,2\})?\)\?[m\|K]//g\' \| sed -r 
\'s/(Package: .*)/\\x1b\[33\;1m\\1\\x1b\[0m/\'

... It seems crazy.  I know I need to protect:  |  '  "  \   ... and 
maybe one or two other characters not relevant here, but I've never 
needed to protect:  ;  (  ) { }  ... so why do I need to backslash them 
here?  Interestingly, one pair of parenthesis need protection but 
another did not.  Very strangely, if I didn't get it exactly right, it's 
not just that the command would recall improperly, but the parser itself 
complained before the string is either saved to it's variable or 
executed.  Sed seemed to voice it's displeasure at the point of saving 
the string, not at the point of trying to execute it, so it almost seems 
as it it's sed that's being troublesome, not zsh per se.   I'm betting 
that this is one of those things where I'm suffering from some deep 
misunderstanding.  It seems weird that single quoted strings sent to sed 
would not be entirely protected already, thus further backslashes would 
seen redundant.  So far, only sed has given me this level of grief.  
Might there be some simplifications?



             reply	other threads:[~2018-02-05 21:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-05 21:20 Ray Andrews [this message]
2018-02-06 17:13 ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3c2b2846-0315-453a-e009-7a935f18dd8d@eastlink.ca \
    --to=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).