procmail
[Top] [All Lists]

Re: regular expression language in procmail

1997-03-10 15:23:19
dattier(_at_)wwa(_dot_)com (David W. Tamkin) writes:
Mark McCreary wrote,

| So far so good.  However, I am still stumped on how truncate the -on
| portion of the address.  I cannot find any substring or truncation commands
| in procmail.

Well, at least not from the right.  Sometimes they can be faked, but taking
"-on" off the rightward end is tricky when the part you want to keep might
contain hyphens, o's, or n's.

| Or do I have access to shell commands ?  Does the $ command
| to evaluate conditions according to sh give me some leverage ?

Yes, you can use command substitution with backticks in a condition line that
has the $ modifier, but you don't need the $ modifier in an assignment:

  :0
  * MATCH ?? -on^^
  { savemetas=$SHELLMETAS # the asterisk below is not a globbing pattern
    baselist=`expr "$MATCH" : '\(.*\)-on'`
    SHELLMETAS=$savemetas }
  :0E
  { baselist=$MATCH }

I can't think of a good way to do it without an outside program.  There is
something in my mind, but it is not easily portable, and it requires one of
the more recent procmail versions that will let you reset MATCH in the middle
of a recipe for later conditions:

The diffs I have show that it was fixed sometime between 3.10 and 3.11pre3.


  grosslist=$MATCH
  :0
  * grosslist ?? -on^^
  * MATCH ?? ^^\/.*[^n]
  * MATCH ?? ^^\/.*[^o]
  * MATCH ?? ^^\/.*[^-]
  { baselist=$MATCH }
  :0E
  { baselist=$grosslist }

This will indeed work, though I agree that it is gross.  For those who
require elegance, you can also solve the problem using a recursive
INCLUDERC.  Put the following in your .procmailrc:


# ^TO_ is a procmail 3.11'ism.  Use just ^TO if you don't have 3.11
:0
* ^TO_[a-z]+-on@
{
    STRING = $MATCH
    TERM = -on@
    INCLUDERC = $HOME/Mail/rm_term.rc
    baselist = $STRING
}



and the following in $HOME/Mail/rm_term.rc (or wherever you put it):


# Remove a terminating string $TERM from $STRING, returning it in
# STRING.  $TERM *must* start with a '-', as that's what this
# routine splits up STRING on.  "found" is a temporary variable that
# must be empty on entry.  The script will clear it on exit, so things
# should be fine as long as you don't use it yourself.
 
# Append the next 'word' in STRING to $found
:0
* STRING ?? $ ^^$\found\/${found+-}[^-]*
{ found = "$found$MATCH" }
:0 E
{
    # This cannot happen unless found was set on entry!
    LOG = "*** WARNING ***
variable 'found' was set to $found on entry to $_
one of the uses must be renamed for rm_term.rc to work!
"
    # Help me, help me!
    :0:
    $DEFAULT
}
 
# Are we done?
:0
* ! STRING ?? $ ^^$\found$TERM^^
{
    # Nope
    INCLUDERC = $_
}
:0 E
{
    # Return the match, and clear our temporary.
    STRING = $found
    found
}



Ta da!

Philip Guenther

<Prev in Thread] Current Thread [Next in Thread>