Topic
  • 7 replies
  • Latest Post - ‏2012-09-11T19:56:59Z by SystemAdmin
HajoEhlers
HajoEhlers
253 Posts

Pinned topic Passing a parameter to a policy or use a default value.

‏2012-09-11T13:22:55Z |
I am aware of the -M option of the mmapply policy but still i am lost in passing a variable to a policy file and still be able to maintain a default value.
In the following example the default value shall for the Variable MAXAGE should be 9999, in case "-M MAXAGE=100" is used, MAXAGE should be 100

Example:


.... define(MAXAGE,9999)   RULE EXTERNAL LIST 
'listfiles' EXEC 
''   RULE 
'xl' LIST 
'listfiles' DIRECTORIES_PLUS SHOW    (  PATH_NAME  ) WHERE   (  DAYS(CURRENT_TIMESTAMP) - DAYS(MODIFICATION_TIME) < MAXAGE    ) ...


A -M MAXAGE=100 will overwrite ALL occurrence of MAXAGE thus making the define not valid and the policy will fail. In case i remove the "define MAXAGE .." i am losing the default value.

I also tried to use the function GetEnv('MAXAGE') but my SQL skill are not good enough to make the next example working:


... CASE when LENGTH(GetEnv(
'MAXAGE')) is NULL THEN MAXAGE=9999 ELSE MAXAGE=GetEnv(
'MAXAGE') ...


Any help is appreciated
Hajo
Updated on 2012-09-11T19:56:59Z at 2012-09-11T19:56:59Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    2092 Posts

    Re: Passing a parameter to a policy or use a default value. - use m4 !

    ‏2012-09-11T13:53:53Z  
    Use m4 ifdef .... Under mmapplypolicy the quote characters are [ and ] and the command line option is -M
    but you can practice/deom/fiddle directly with the m4 command:
    
    # *m4 -D foo=3* *foo* 3 *ifdef(`foo
    ',foo,99)* 3 *ifdef(`goo
    ',goo,98)* 98 *define(`goovalue
    ',ifdef(`goo',goo,98))* *goovalue* 98 *goo* goo
    

    See your m4 documentation (info m4)!
  • SystemAdmin
    SystemAdmin
    2092 Posts

    Re: Passing a parameter to a policy or use a default value. - use m4 !

    ‏2012-09-11T13:56:16Z  
    Use m4 ifdef .... Under mmapplypolicy the quote characters are [ and ] and the command line option is -M
    but you can practice/deom/fiddle directly with the m4 command:
    <pre class="jive-pre"> # *m4 -D foo=3* *foo* 3 *ifdef(`foo ',foo,99)* 3 *ifdef(`goo ',goo,98)* 98 *define(`goovalue ',ifdef(`goo',goo,98))* *goovalue* 98 *goo* goo </pre>
    See your m4 documentation (info m4)!
    Oops the formatting didn't come out quite right.

    I indicated what I typed in with \*s

    
    *What I typed* What I got back.
    
  • HajoEhlers
    HajoEhlers
    253 Posts

    Re: Passing a parameter to a policy or use a default value. - use m4 !

    ‏2012-09-11T15:04:34Z  
    Use m4 ifdef .... Under mmapplypolicy the quote characters are [ and ] and the command line option is -M
    but you can practice/deom/fiddle directly with the m4 command:
    <pre class="jive-pre"> # *m4 -D foo=3* *foo* 3 *ifdef(`foo ',foo,99)* 3 *ifdef(`goo ',goo,98)* 98 *define(`goovalue ',ifdef(`goo',goo,98))* *goovalue* 98 *goo* goo </pre>
    See your m4 documentation (info m4)!
    > info m4

    i am reading the documentation but it does not mean that i understand it in a way that i could solve my question.
    If so i would not ask.

    So for me it is not possible via a m4 macro to get what i am looking for since the -D option overwrites any "definition" which matches.

    I will check if i get further with the GetEnv('EnvironmentVariable') option since there a if/then logic should be possible based on SQL statements.

    thanks
    Hajo
  • SystemAdmin
    SystemAdmin
    2092 Posts

    Re: Passing a parameter to a policy or use a default value. - use m4 !

    ‏2012-09-11T16:47:28Z  
    > info m4

    i am reading the documentation but it does not mean that i understand it in a way that i could solve my question.
    If so i would not ask.

    So for me it is not possible via a m4 macro to get what i am looking for since the -D option overwrites any "definition" which matches.

    I will check if i get further with the GetEnv('EnvironmentVariable') option since there a if/then logic should be possible based on SQL statements.

    thanks
    Hajo
    I thought I answered your question. Really.
    I figured you could transpose goo and the brackets, no?
    How's this:
    
    ifdef([MAXAGE],,define([MAXAGE],9999))
    
  • SystemAdmin
    SystemAdmin
    2092 Posts

    OOPS - Passing a parameter to a policy or use a default value. - use m4 !

    ‏2012-09-11T16:57:04Z  
    I thought I answered your question. Really.
    I figured you could transpose goo and the brackets, no?
    How's this:
    <pre class="jive-pre"> ifdef([MAXAGE],,define([MAXAGE],9999)) </pre>
    OOPS quotes are very important, protect the define with quotes.
    I believe this is correct:
    
    ifdef([MAXAGE],,[define([MAXAGE],9999)])
    
  • HajoEhlers
    HajoEhlers
    253 Posts

    Re: OOPS - Passing a parameter to a policy or use a default value. - use m4 !

    ‏2012-09-11T18:27:56Z  
    OOPS quotes are very important, protect the define with quotes.
    I believe this is correct:
    <pre class="jive-pre"> ifdef([MAXAGE],,[define([MAXAGE],9999)]) </pre>
    i used the verbose option +m4 -dV .... + to see what happens on the following m4 script

    
    ifdef(MAXAGE,,define(MAXAGE,9999)) incr(MAXAGE)
    

    • Without the -D MAXAGE=1 , MAXAGE will be defined as 9999 since MAXAGE was not defined beforehand and the result will be 10000

    • With the -D MAXAGE=1 , MAXAGE is already DEFINED as 1 and the ifdef will look like
      
      define(`1
      ', `9999')
      
      but we do not care and the result is 2

    • If we would just issue m4 with -D MAXAGE , MAXAGE is already defined but empty and the result would be 1

    thx a lot
    Hajo

    P.S. I would never get this logic just by reading the man page or online docu.
  • SystemAdmin
    SystemAdmin
    2092 Posts

    Re: OOPS - Passing a parameter to a policy or use a default value. - use m4 !

    ‏2012-09-11T19:56:59Z  
    i used the verbose option +m4 -dV .... + to see what happens on the following m4 script

    <pre class="jive-pre"> ifdef(MAXAGE,,define(MAXAGE,9999)) incr(MAXAGE) </pre>
    • Without the -D MAXAGE=1 , MAXAGE will be defined as 9999 since MAXAGE was not defined beforehand and the result will be 10000

    • With the -D MAXAGE=1 , MAXAGE is already DEFINED as 1 and the ifdef will look like <pre class="jive-pre"> define(`1 ', `9999') </pre> but we do not care and the result is 2

    • If we would just issue m4 with -D MAXAGE , MAXAGE is already defined but empty and the result would be 1

    thx a lot
    Hajo

    P.S. I would never get this logic just by reading the man page or online docu.
    m4 is the only macro processor you'll ever need! ;-)

    It's based on Strachey's GPM, which I first learned about in 1972 or 1973.
    But made more practical for everyday use...

    Unlike most programming languages the basic syntax and semantics are extremely simple.
    (But to write useful macro/programs you must learn a few of the built-in macro functions.)

    If you want or need to know more peruse:

    http://en.wikipedia.org/wiki/Christopher_Strachey
    http://comjnl.oxfordjournals.org/content/8/3/225.full.pdf
    http://en.wikipedia.org/wiki/M4_(computer_language)

    You probably could learn it by carefully scrutinizing the m4 man and info pages and playing around with it...
    BUT the gnu doc at http://www.gnu.org/software/m4/manual

    is probably a better place to start.