Building an argc/argv style structure from a string (char*)


 
Thread Tools Search this Thread
Top Forums Programming Building an argc/argv style structure from a string (char*)
# 8  
Old 12-16-2009
Hi Jim,
I actually just posted a response while you were posting that.
It's awaiting moderation because, I suppose, there is a link in it (to the source i was currently working on).

Anyway, thanks! I was looking for something like that in bash and csh source, but failed to find it! Just out of curiosity, where did you find that?

Comparing it to my parser, it appears i have re-invented a flat wheel.

-Ryan

---------- Post updated at 05:41 PM ---------- Previous update was at 05:40 PM ----------

Woops, I responded before looking through the code entirely.

Nice, you're using popen+set! This is very, very nice.

Thanks again,
-Ryan
# 9  
Old 12-17-2009
If you want to, you can use a specific shell rather than the system default shell. For example, here is how to use ksh93 via the libshell's sh_init()/sh_trap() mechanism.
Code:
#include <shell.h>
#include <stdio.h>
#include <nval.h>

int 
call_shell(int argc, char* argv[], char *str)
{
    Namval_t *np, *np_sub;
    char tmp[512];

    sprintf(tmp, "set - %s && for i in \"$@\";\n do\n aname+=(\"$i\")\ndone", str);

    Shell_t *shp = sh_init(argc, argv, 0);
    sh_trap(tmp, 0);

    np = nv_open("aname", shp->var_tree, 0);
    nv_putsub(np, NULL, ARRAY_SCAN);
    np_sub = np;

    do {
        // copy out the arguments to wherever here. 
        fprintf(stderr, "%d: subscript='%s' value='%s'\n", np_sub, nv_getsub(np_sub), nv_getval(np_sub));
    } while (np_sub && nv_nextsub(np_sub));

    nv_close(np);

    return(0);
}

int 
main(int argc, char *argv[])
{
    char string[] = "hello world 'my name is simon' foo\\ bar";

    call_shell(argc, argv, string);
}

When compiled and run, the output is
Code:
... value='hello'
... value='world'
... value='my name is simon'
... value='foo bar'

# 10  
Old 12-17-2009
I like fpMurphy's approach better, but shells support
Code:
set -  parm1 parm2 parm3

as a way to redefine the parameters for the current invocation. This just uses the shell's built in parser. You can change behavior of the parser by defining the IFS variable.
# 11  
Old 12-17-2009
Quote:
I like fpMurphy's approach better
Thanks Jim. However it is not for the fainthearted. You have to know the internal structures of whichever shell you choice to use.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Help using argc/argv in assignment

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: First, create a "hello world" program that prints "Hello World". But NOW, instead use argc to verify that a... (9 Replies)
Discussion started by: miniviking10
9 Replies

2. Programming

Building Block style programming Book

Hello to all, Here is my situation. Some time in the mid-80's I stumbled across a small white programming book - can't remember the name but it was unique in that it started right out giving instructions on creating building blocks in code as a foundation for a complete system. The book was... (2 Replies)
Discussion started by: jozefn
2 Replies

3. UNIX for Advanced & Expert Users

O argv, argv, wherefore art thou argv?

All of my machines (various open source derivatives on x86 and amd64) store argv above the stack (at a higher memory address). I am curious to learn if any systems store argv below the stack (at a lower memory address). I am particularly interested in proprietary Unices, such as Solaris, HP-UX,... (9 Replies)
Discussion started by: alister
9 Replies

4. Shell Programming and Scripting

argc/ argv in awk

Hi guys, i'm trying to solve this problem. I have to run something like cat file1.txt | awk -f script.awk 10 if i'm in the awk script, how can i take the parameter :10 ??:wall: i try something like : BEGIN{ var=argv } {..} END{..} but obviously is not correct... (5 Replies)
Discussion started by: heaven25
5 Replies

5. Shell Programming and Scripting

ARGV and ARGC in bash 3 and bash 3.2

Hi Folks, I've prepared a shell script that takes action based on arguments and number of arguments..sample code like: ARGV=("$@") ARGC=("$#") case ${ARGV} in abc) if ; then ...... else printf "\nInvalid number of arguments, please check the inputs and... (2 Replies)
Discussion started by: SBC
2 Replies

6. Programming

How to turn argv[1] into a string in C?

i have a function that's parameter is char *s and in the main function i am sending that function &(argv), but i dont think this is working, how can i fix this? can i cast it to be a string or something? is there a way i can create a new string thats exactly what argv is equal to... (6 Replies)
Discussion started by: omega666
6 Replies

7. Programming

help for argv argc

Hi C experts, I have the following code for adding command line option for a program int main (argc, argv) int argc; char *argv; { char *mem_type; //memory type char *name; //name of the memory int addr; //address bits int data; ... (5 Replies)
Discussion started by: return_user
5 Replies

8. Programming

dbx debugger + argv[argc]

Is it possible to use the dbx debugger with the CL options for the executable ? Say you have created a executable called myfunc which can take string arguments at run-time. You run it like this ./myfunc Hello World where Hello and World are the string arguments My question is whether... (1 Reply)
Discussion started by: JamesGoh
1 Replies

9. Programming

Using argv argc

I searched on the forums. No advises. I am using a previous source code. I changed the main function main(int argc, char **argv) in a function misc(int argc, char **argv). How do you use the argc and argv parameters? This is how I am calling the function : char param; strcat(param,"wgrib ");... (4 Replies)
Discussion started by: Akeson Chihiro
4 Replies
Login or Register to Ask a Question