Thread: Tokenizing Command Line Arguments

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    1

    Question Tokenizing Command Line Arguments

    Hi,

    I am working on a tokenizer for command line arguments, to eventually work in a mini shell program. I cannot use strtok, I must get the arguments by hand. I have to ignore tokens that are in quotation marks.

    For example:
    cd ls "test"

    token 0 - cd
    token 1 - is
    token 2 - "test"

    So I was thinking that if I had a pair of starting quotes, then I would advance the i counter until i hit the pair of ending quotes, then put that info into the token. Here is what I have that is definitely not working. Any ideas on what direction I am going, or not going in? Any guidance would be appreciated. Thanks.

    Code:
    struct token* tokenizer(char* __commandline,int* __argumentcounter)
    {
      /*the following data structure should hold all the tokens*/
      struct token* tokenlist = malloc(sizeof(struct token));
    
    		int i = 0;
    		int j = 0;
    		/*struct token* head = tokenlist;*/
    		struct token tokstruct;
    		struct token *tok;
    		tok = &tokstruct;
    		while (__commandline[i] != '\n')
    		{
    			if(__commandline[i] == '\"')
          
    			{
    				i++;
    				if(__commandline[i] == '\"')
    				{
    					tok*[j] = __commandline[i];
    					j++;
    				}
       
    				else if(__commandline[i] == ' ')
    				{
    					i++;
    				}
    			}
    		}

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    If you want to ignore whatever is in quotes, I think something like this should work:
    Code:
    while (__commandline[i] != '\n')
    {
        if(__commandline[i] == '\"')
        {
            do
            {
                i++;
            } while(__commandline[i] != '\"') 
        }
        i++;
    }
    [edit] Actually if a \n char is in the inner loop you will need to break out of both loops as well [/edit]
    Last edited by mike_g; 09-23-2007 at 06:04 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, why are you using __commandline and __argumentcounter for your variable names instead of just commandline and argumentcounter respectively? I note that according to the standard "all identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look up "finite state machine".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM