# class KMacroExpanderBase

###

Module | kdecore
Namespace |
Class | KMacroExpanderBase
Inherits |

\class KMacroExpanderBase kmacroexpander.h <KMacroExpanderBase>

Abstract base class for the worker classes behind the KMacroExpander namespace and the KCharMacroExpander and KWordMacroExpander classes.

Author Oswald Buddenhagen <ossi@kde.org>

### methods

- KMacroExpanderBase (c)

  returns ctor
  KDE 4.1.0 and later only

  Argument | Type | Default
  --- | --- | ---
  c | QChar | QLatin1Char('%')

  Constructor.

  c - escape char indicating start of macros, or QChar.null for none

- KMacroExpanderBase (a0)

  returns ctor
  before KDE 4.1.0 only

  Argument | Type | Default
  --- | --- | ---
  a0 | QChar | QLatin1Char('%')

- escapeChar ( )

  returns QChar

  Obtain the macro escape character. Returns escape char indicating start of macros, or QChar.null if none

- expandEscapedMacro (str, pos, ret)

  returns int

  Argument | Type | Default
  --- | --- | ---
  str | QString |
  pos | int |
  ret | QStringList |

  This function is called every time the escape char is found if it is not QChar.null. It should determine whether the string starting at pos witin str is a valid macro and return the substitution value for it if so.

  str - the input string

  pos - the offset within str. Note that this is the position of the occurrence of the escape char

  ret - return value: the string to substitute for the macro Returns If greater than zero, the number of chars at pos in str to substitute with ret (i.e., a valid macro was found). If less than zero, subtract this value from pos (to skip a macro, i.e., substitute it with itself). If zero, scanning continues as if no escape char was encountered at all.

- expandMacros (str)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  str | QString |

  Perform safe macro expansion (substitution) on a string.

  str - the string in which macros are expanded in-place

- expandMacrosShellQuote (str)

  returns bool, int (pos)

  Argument | Type | Default
  --- | --- | ---
  str | QString |

  Perform safe macro expansion (substitution) on a string for use in shell commands.

  ### *NIX notes

  Explicitly supported shell constructs: \ '' "" $'' $"" {} () $(()) ${} $() ``

  Implicitly supported shell constructs: (())

  Unsupported shell constructs that will cause problems: Shortened "`case $v in pat)`" syntax. Use "`case $v in (pat)`" instead.

  The rest of the shell (incl. bash) syntax is simply ignored, as it is not expected to cause problems.

  Note that bash contains a bug which makes macro expansion within double quoted substitutions (`"${VAR:-%macro}"`) inherently insecure.

  For security reasons, never put expandos in command line arguments that are shell commands by themselves - "`sh -c 'foo \%f'`" is taboo. "`file=\%f sh -c 'foo "$file"'`" is OK.

  ### Windows notes

  All quoting syntax supported by KShell is supported here as well. Additionally, command grouping via parentheses is recognized - note however, that the parser is much stricter about unquoted parentheses than cmd itself. The rest of the cmd syntax is simply ignored, as it is not expected to cause problems - do not use commands that embed other commands, though - "`for /f ...`" is taboo.

  str - the string in which macros are expanded in-place

  pos - the position inside the string at which parsing/substitution should start, and upon exit where processing stopped Returns false if the string could not be parsed and therefore no safe substitution was possible. Note that macros will have been processed up to the point where the error occurred. An unmatched closing paren or brace outside any shell construct is not an error (unlike in the function below), but still prematurely terminates processing.

- expandMacrosShellQuote (str)

  returns bool
  Not implemented

  Argument | Type | Default
  --- | --- | ---
  str | QString |

  Same as above, but always starts at position 0, and unmatched closing parens and braces are treated as errors.

- expandPlainMacro (str, pos, ret)

  returns int

  Argument | Type | Default
  --- | --- | ---
  str | QString |
  pos | int |
  ret | QStringList |

  This function is called for every single char within the string if the escape char is QChar.null. It should determine whether the string starting at pos within str is a valid macro and return the substitution value for it if so.

  str - the input string

  pos - the offset within str

  ret - return value: the string to substitute for the macro Returns If greater than zero, the number of chars at pos in str to substitute with ret (i.e., a valid macro was found). If less than zero, subtract this value from pos (to skip a macro, i.e., substitute it with itself). If zero, no macro starts at pos.

- setEscapeChar (c)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  c | QChar |

  Set the macro escape character.

  c - escape char indicating start of macros, or QChar.null if none