0

I want to make exec() calls with some amount of shell functionality. Given a string naming a shell, a string naming an executable, and a string representing a command line, how can I generate an array of strings that are the arguments that the shell would pass to the executable?

So on my machine, for the shell string "bash", the executable string "tr", and the command line string "tr 'hi there' goodbye", I'd like to generate the array {"tr", "hi there", "goodbye"}.

Many basic shell features, like the word splitting example above, aren't terribly difficult to emulate. Still, it would be preferable to directly determine how a shell on the current machine would behave.

I don't simply want to run a shell command. I want to retain control of the rest of the execution context, for example, file descriptors.

Specifically, I'm interested in if there is a simple way to apply brace expansion, tilde expansion, variable expansion, word splitting, filename expansion, and quote removal.

I'm not asking how to implement a shell. My question is if there's a system call on linux platforms to just do these things or any other way to programmatically access the command line parsing of a shell.

The goal is to create a program that will be given a string that specifies an executable to run and the arguments for that executable. The program will run the specific executable by fork and exec then communicate with it through stdin and stdout. This question is only asking about the handling of the string that specifies the executable and arguments. Is there a simple way to parse a command line string as a shell would?

To get shell command line parsing functionality, it is possible to feed shell scripts or "sh -c " style commands to the program. However, if there exists a system functionality to parse a command line and make the result viewable programmatically that would be preferable. Does that exist? If so, what is it?

The target OS is linux. The shell of primary interest is bash. Portability is preferable but not necessary.

9
  • 1
    Are you saying you want to emulate all the complex substitution that a typical shell does? That will basically involve writing most of a shell (or finding a library that does it for you). (Not my downvote, BTW...) Aug 31, 2014 at 8:40
  • I'm asking how I can avoid emulating a shell. I want to know if there's a way to determine how a shell would interpret a command line. Aug 31, 2014 at 8:48
  • Why do you want to exactly emulate a shell? Why not some better scripting language? Aug 31, 2014 at 8:50
  • @Praxeolitic: Ok, so you need to find a library/util of some kind that does the work for you. There's no way of knowing what foo $(find . -name "*bar*") $(groups) etc. etc. will evaluate to without actually evaluating it. (But I'm afraid I don't know of such a library off-hand.) Aug 31, 2014 at 8:51
  • 1
    I still don't understand the question and what really motivates it. I gave some references in my answer, but I downvoted the question because it is extremely unclear. What are your exact goals? What is your software trying to achieve? Please edit your question to improve it! Aug 31, 2014 at 9:10

1 Answer 1

3

Read first glob(7)

Recall that shells are expanding -i.e. globbing- arguments like *.txt before doing the exec(3) functions.

In your C code, you could use glob(3) and/or fnmatch(3) and even better wordexp(3). For file descriptors, you'll need open(2), dup(2), pipe(2) and many other syscalls(2).

To scan a file tree like find(1) does use nftw(3). To just read a directory, use opendir(3), readdir(3) etc...

At some point, you'll need to multiplex input (and/or output), so learn about poll(2)

Read also Advanced Linux Programming.

For lexing a string in constituent parts, you might consider strtok(3).

Actually, it looks like you want to embed some scripting interpreter in your program. So consider GNU guile or lua (and perhaps prefer some different syntax & scripting language than the one provided by the shell).

Alternately design your software as a library and code the necessary glue code to embed it inside an existing scripting interpreter (like Python, etc...) - or even (but I probably don't recommend that) as a plugin to some existing shell.

BTW, system(3) and popen(3) both fork a shell. You could build carefully their argument string. Beware of code injection so quote appropriately some arguments.

addenda

after your edit, you want wordexp. However, you should be careful, e.g. because some file names or directory names may contain spaces, and you need to parse these spaces as part of the name...

3
  • This is a very nice summary. wordexp is what I was after. Aug 31, 2014 at 9:36
  • But you still need to be careful, e.g. if you have spaces in some directory name. Something has to parse the space correctly... Aug 31, 2014 at 9:44
  • 2
    With gcc on a recent Ubuntu, wordexp() does globbing and appropriately handles filenames with spaces. It appropriately identifies them as a single token. Based on your comment, is that behavior not something I can rely on in general? It's not clear to me from the documentation. Aug 31, 2014 at 23:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.