delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/05/12:35:46

From: tomw AT tsys DOT demon DOT co DOT uk (Tom Wheeley)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Command Parser
Date: Sat, 05 Apr 97 09:17:11 GMT
Organization: Adventures and Diving
Message-ID: <860231831snz@tsys.demon.co.uk>
References: <01bc409f$afbb59c0$97c3b8cd AT scully>
Reply-To: tw104 AT york DOT ac DOT uk
Lines: 42
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <01bc409f$afbb59c0$97c3b8cd AT scully>
           scully AT ix DOT netcom DOT com "Cephaler" writes:

> what I need is a simple parser, one that would work with gets(input) (input

*Never* use gets()!  Always use fgets(stdin)  gets can quite easily overrun
your buffer and destroy your computer.

> = char *input) or scanf("%s",input) and divide each "chunk" of characters
> into seperate strings and put them all into one big array... kind of
> like....
> 
> Prog output: Enter a command: _
> User Input: /showfilestats math.c
> 
> then, after that, the program parses the user input and puts it into char
> *UserCommand[10] like this:
> 
> UserCommand[0] = /showfilestats
> UserCommand[1] = math.c

  char buf[BUFSIZE];
  char *p;
  char *argv[];
  int argc=1;

  fgets(buf, BUFSIZE, stdin);
  argv=malloc(sizeof (char *));
  argv[0]=buf;
  for(p=buf;*p;p++) {
    if (*p==' ') {
      *p='\0';
      argv=realloc(argv, ++argc * sizeof (char *));
      argv[argc-1]=p+1;
    }
  }

It's *very* simplistic...

-- 
:sb)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019