Dealing with fragmented getopt parameters

From: Jem Berkes (jem_at_users.pc9__org)
Date: 10/30/03

  • Next message: David Schwartz: "Re: send problems linux"
    Date: 30 Oct 2003 00:22:00 GMT
    
    

    I have a C program that uses getopt. I added an -o switch that takes a
    parameter, and it works as expected when invoked from a shell:
                myprogram -g -o "sendmail -f sender recipient"
    That is, after parsing, optarg refers to everything within quotes

    I want to make it possible to call my program from postfix's pipe command.
    That documentation states that the command to execute has no shell-style
    interpretation. Sure enough, when I invoke postfix pipe's
                argv=myprogram -g -o "sendmail -f $(sender) $(recipient)"
    I saw experimentally that postfix delimits on spaces, quotes remain:
            -g
            -o
            "sendmail
            -f
            $(sender)
            $(recipient)"

    How can I work around this? One idea I had was to, after getopt iteration,
    check to see if optarg starts with a quote. This would imply that whatever
    invoked the command did not group things between "" as a single parameter.
    So then I can simply merge all subsequent argv[n]'s together, separating
    them by spaces.

    Does this seem like a good idea? Note that if the shell does properly group
    things within "" then the opening quote will not appear within optarg, and
    no special processing is required.

    -- 
    Jem Berkes
    http://www.sysdesign.ca/
    

  • Next message: David Schwartz: "Re: send problems linux"

    Relevant Pages

    • Re: Dealing with non-grouped getopt parameters
      ... when I invoke postfix pipe's ... During the getopt iteration, it would encounter -o and at ... that point optarg would point to '"sendmail' (note the visible quotes) ...
      (comp.unix.programmer)
    • Re: Dealing with fragmented getopt parameters
      ... > I have a C program that uses getopt. ... That documentation states that the command to execute has no ... when I invoke postfix pipe's ... > I saw experimentally that postfix delimits on spaces, quotes remain: ...
      (comp.lang.c)
    • Re: Dealing with fragmented getopt parameters
      ... > I have a C program that uses getopt. ... That documentation states that the command to execute has no ... when I invoke postfix pipe's ... > I saw experimentally that postfix delimits on spaces, quotes remain: ...
      (comp.unix.programmer)
    • Re: how to write a program that takes arguments from commandline?
      ... I assume argc contains the number of ... programs are often invoked from a shell ... Shells *can* use spaces to delimit arguments, ... like for constructing the arguments for the programs you invoke. ...
      (comp.lang.c)
    • Looking for a extensible shell for debug purpose
      ... process using IPC. ... Now I want to add a thread to this program to start shell, ... extensible shell, to enter command, then invoke the corresponding ... like lua, can embedded into the program, and by running lua script to ...
      (comp.os.linux.development.apps)