Unix Programming - Parsing program args

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2005 > Parsing program args





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Parsing program args
Robert M. Gary

2005-01-11, 5:55 pm

I have a program that will exec another. The args for the exec'd program are
provided to me in a string (including the actual command itself). I'm trying
to think of the best way to parse this so I can call execvp. I could simply
parse spaces or strtok but I often have spaces in args that are quoted. I
certainly could come up with an algorithm to do this, but I'm wondering if
the parser that shell uses is available to me.

-Robert


Thomas Maier-Komor

2005-01-11, 5:55 pm

Robert M. Gary wrote:
> I have a program that will exec another. The args for the exec'd program are
> provided to me in a string (including the actual command itself). I'm trying
> to think of the best way to parse this so I can call execvp. I could simply
> parse spaces or strtok but I often have spaces in args that are quoted. I
> certainly could come up with an algorithm to do this, but I'm wondering if
> the parser that shell uses is available to me.
>
> -Robert
>
>


you are speaking of a C program? why not just call system?
D. Rock

2005-01-11, 5:55 pm

In comp.unix.solaris Robert M. Gary <foobar@foobar.com> wrote:
> I have a program that will exec another. The args for the exec'd program are
> provided to me in a string (including the actual command itself). I'm trying
> to think of the best way to parse this so I can call execvp. I could simply
> parse spaces or strtok but I often have spaces in args that are quoted. I
> certainly could come up with an algorithm to do this, but I'm wondering if
> the parser that shell uses is available to me.


If you can trust the input you usually let the shell do the dirty work.

See the manpage for system(3C)

--
Daniel
Darren Dunham

2005-01-11, 5:55 pm

In comp.unix.solaris Robert M. Gary <foobar@foobar.com> wrote:
> I have a program that will exec another. The args for the exec'd program are
> provided to me in a string (including the actual command itself). I'm trying
> to think of the best way to parse this so I can call execvp. I could simply
> parse spaces or strtok but I often have spaces in args that are quoted. I
> certainly could come up with an algorithm to do this, but I'm wondering if
> the parser that shell uses is available to me.


It is if you call the shell. Just exec the shell and pass the string to
it with -c. That's how many programs handle it.

truss -a -f this and see how PERL does it...

perl -e 'exec "ls -l . < /dev/null"'

--
Darren Dunham ddunham@taos.com
Senior Technical Consultant TAOS http://www.taos.com/
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
Robert M. Gary

2005-01-11, 5:55 pm

I have processing to do between the fork and the exec in order to create a
slave terminal session.



"Thomas Maier-Komor" <maierkom@lpr.e-technik.no-spam.tu-muenchen.de> wrote
in message news:cs1al2$bk5$1@wsc10.lrz-muenchen.de...
> Robert M. Gary wrote:
are[vbcol=seagreen]
trying[vbcol=seagreen]
simply[vbcol=seagreen]
I[vbcol=seagreen]
if[vbcol=seagreen]
>
> you are speaking of a C program? why not just call system?



Heny Townsend

2005-01-11, 8:50 pm

Robert M. Gary wrote:
> I have a program that will exec another. The args for the exec'd program are
> provided to me in a string (including the actual command itself). I'm trying
> to think of the best way to parse this so I can call execvp. I could simply
> parse spaces or strtok but I often have spaces in args that are quoted. I
> certainly could come up with an algorithm to do this, but I'm wondering if
> the parser that shell uses is available to me.


I had the same problem and was able to find a function to do the parsing
on the net. It's called poptParseArgvString() and is part of the 'popt'
library. The tricky thing is that popt is hard to find by itself. It's
packaged along side of RPM, so go looking for RPM downloads and you
should find popt in the same ftp dir or thereabouts.

It was no problem to extract and build poptParseArgvString separately.

--
Henry Townsend
Ralf Fassel

2005-01-12, 7:48 am

* "Robert M. Gary" <foobar@foobar.com>
| I have processing to do between the fork and the exec in order to
| create a slave terminal session.

execl("/bin/sh", "sh", "-c", your_cmd_string, NULL);

R'
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com