KShell: argument splitting and tilde expansion

Oswald Buddenhagen ossi at kde.org
Sat Mar 1 19:45:43 GMT 2003


due to the incredible demand for such functionality (there seem to be
roughly as many (more or less broken) implementations out there as kde
apps), i've implemented a class with a few static functions that cover
some basic ((posix) shell)/bash functionality.

tildeExpand is self-explaining, i think.

splitArgs performs bash-like argument splitting. the following
quotings prevent word-splitting:
- backslash.
- single quotes. everything between them is taken literally.
- double quotes. the backslash can be used to escape the double quote
  (and itself).
- bash-like $'' quoting. read the bash man page for details.
note that backslashes preceeding non-special chars are treated literally.
splitArgs can optionally perform tilde expansion, too. note that this is
the only place where this is allowed, as
- before splitting you don't know where words begin
- after splitting the quotes are gone, so you don't know whether tilde
  expansion was intended by the user
possible todo:
- simple substitutions ($var, ${var})
- complex substitutions (${var:-foo})
- command substitutions ($(echo hallo), `echo hallo`)
- pipes, redirections ($(echo foo|clobber 2>/dev/null))
- command lists ($(echo foo; echo bar))
- complex commands (conditions, loops, grouping, assignments, functions)
- smoke weaker shit to get the feet back on the ground

i provided joinArgs just in case somebody needs it, even though i didn't
find a place where it would be needed, yet.

comments welcome.


Hi! I'm a .signature virus! Copy me into your ~/.signature, please!
Chaos, panic, and disorder - my work here is done.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: kshell.h
Type: text/x-chdr
Size: 1447 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20030301/c9033ae2/attachment.h>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: kshell.cpp
Type: text/x-c++src
Size: 8444 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20030301/c9033ae2/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: kshelltest.cpp
Type: text/x-c++src
Size: 1172 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20030301/c9033ae2/attachment-0001.cpp>
-------------- next part --------------
CFLAGS := -O2 -march=$(shell uname -m) -g -W -Wall
CXXFLAGS := $(CFLAGS) -fno-exceptions

QTDIR := /usr/local/kde

test: kshelltest

kshelltest: kshelltest.o kshell.o
	$(CXX) -o $@ $^ -L$(QTDIR)/lib -lqt-mt

kshelltest.o: kshelltest.cpp $(KSHELLDIR)/kshell.h
	$(CXX) $(CXXFLAGS) -o $@ -c $< -I$(QTDIR)/include -I$(KSHELLDIR)

kshell.o: $(KSHELLDIR)/kshell.cpp $(KSHELLDIR)/kshell.h
	$(CXX) $(CXXFLAGS) -o $@ -c $< -I$(QTDIR)/include -I$(KSHELLDIR)

	rm -f *.o *.s kshelltest

More information about the kde-core-devel mailing list