Extend KMacroExpander for shell-expansion?

Andreas Pakulat apaku at gmx.de
Fri Jul 18 19:54:11 BST 2008

On 12.07.08 15:13:47, Oswald Buddenhagen wrote:
> On Sat, Jul 12, 2008 at 01:17:51AM +0200, Andreas Pakulat wrote:
> > FOOBAR=kde
> > Now I'm thinking this might be of interest for other people too and as
> > there's already expanding code in KMacroExpander I'm wondering wether I
> > should put a expandShellVariables() function into that namespace too. Or
> > should it rather go into KShell??
> > 
> the right place would be KShell for hopefully obvious reasons.


> simple unix-style variable expansion can be implemented rather trivially
> with KMacroExpander::expandMacros( str, var2valueMap, QLatin1Char('$') )
> (var2valueMap being pre-populated with the environment) or (more
> efficiently) by subclassing KWordMacroExpander.
> to make subsequent expansions possible, one could use
> expandMacrosShellQuote().

I have to admit that after looking through the API and implementation
I'm not quite sure how to properly implement something like this:


where I of course want SOMEVAR=BAR as result. The problem I'm having
here is that the list of variables I have as input is not ordered like
above. So when I do expand $BAZ via expandMacro the first time I get
$FOO. So I need to expand again. So the obvious way would be to re-run
the expandMacro until the expanded String doesn't change anymore.
However that bears the risk of getting infinite loops unless I'm wrong:


In a shell the above case works because the shell immediately resolves
FOO to the empty string. But in my case I just have the list of these
two variables and need to do the expansion. Am I just missing something
or is there simply no way to do this?

> note that KMacroExpander is unfit to implement the totally braindead
> semantics of dos-style variable expansion - and i suppose we want to use
> platform-native expansions?

If possible I guess thats the "easiest way out"...

> i think one should introduce a VariableExpand flag to KShell::Options
> and pass that as an additional option to KShell::splitArgs().
> for the non-splitting variant, one should generalize the tildeExpand()
> function into an expand() function that takes an option field like
> splitArgs().

I think I understand. Will have a look and come up with a patch when I
find the time.


You are deeply attached to your friends and acquaintances.

More information about the kde-core-devel mailing list