How to parse a string like C program parse the command line string?

L

linzhenhua1205

I want to parse a string like C program parse the command line into
argc & argv[][].
I hope don't use the array the allocate a fix memory first, and don't
use the memory allocate function like malloc.
who can give me some ideas?
The following is my program, but it has some problem. I hope someone
would correct it.
////////////////////////////
//Test_ConvertArg.c
////////////////////////////
#include <stdio.h>
#include <string.h>

int ConvertArg(char* Str, char* Argv[])
{
int Argc; // Count the argument).
int Count = 0;
int i = 0;
char* StrPtr;
char* TmpStrPtr;
StrPtr = Str;

for(; *StrPtr == ' ';) // ignore the whitespace before the
command!
{
++StrPtr;
}

TmpStrPtr = StrPtr;

for (Argc = 0; (*StrPtr) != '\n'; StrPtr++, Count++)
{

if(*StrPtr == ' ')
{
// if exist multi whitespace together,
//argc just count once,and don't continually change argv!
if( *(StrPtr+1) == ' ')
{
Count--;
continue;
}
}

Count--;
// the following setences have problems.
memcpy(Argv[Argc],TmpStrPtr,Count);

#if 0
for(i=0; i <= Count; i++)
{
Argv[Argc] = *TmpStrPtr;
TmpStrPtr++;
}
#endif


TmpStrPtr = StrPtr;
i = 0;
Argc++;
}
}

int main(int argc, char* argv[])
{
char** argv1;
char* str = "";

char** str1 = "test";
char* str2 = " ";
memcpy(str2, str1[0], 4);
printf("%s\n", str2);
// test ConvertStr()
str = " a asdf ";
argv1 = "";
printf("argc = %d, argv0 = \n",ConvertArg(str, argv1));
str = " a asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s, argv2 =
%s\n",ConvertArg(str, argv1), argv1[0], argv1[1], argv1[3]);
return 1;
}
 
D

Developper

I want to parse a string like C program parse the command line into
argc & argv[][].
I hope don't use the array the allocate a fix memory first, and don't
use the memory allocate function like malloc.
who can give me some ideas?
The following is my program, but it has some problem. I hope someone
would correct it.
////////////////////////////
//Test_ConvertArg.c
////////////////////////////
#include <stdio.h>
#include <string.h>

int ConvertArg(char* Str, char* Argv[])
{
int Argc; // Count the argument).
int Count = 0;
int i = 0;
char* StrPtr;
char* TmpStrPtr;
StrPtr = Str;

for(; *StrPtr == ' ';) // ignore the whitespace before the
command!
{
++StrPtr;
}

TmpStrPtr = StrPtr;

for (Argc = 0; (*StrPtr) != '\n'; StrPtr++, Count++)
{

if(*StrPtr == ' ')
{
// if exist multi whitespace together,
//argc just count once,and don't continually change argv!
if( *(StrPtr+1) == ' ')
{
Count--;
continue;
}
}

Count--;
// the following setences have problems.
memcpy(Argv[Argc],TmpStrPtr,Count);

#if 0
for(i=0; i <= Count; i++)
{
Argv[Argc] = *TmpStrPtr;
TmpStrPtr++;
}
#endif


TmpStrPtr = StrPtr;
i = 0;
Argc++;
}
}

int main(int argc, char* argv[])
{
char** argv1;
char* str = "";

char** str1 = "test";
char* str2 = " ";
memcpy(str2, str1[0], 4);
printf("%s\n", str2);
// test ConvertStr()
str = " a asdf ";
argv1 = "";
printf("argc = %d, argv0 = \n",ConvertArg(str, argv1));
str = " a asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s, argv2 =
%s\n",ConvertArg(str, argv1), argv1[0], argv1[1], argv1[3]);
return 1;
}


use the getopt function

Johan
 
L

linzhenhua1205

but my program is not want to run under the systemV Unix or something
like it.
That is not getopt system call
 
L

linzhenhua1205

I rewrite the Convert function like following:
//////////////////////


///////////////////////////////////////////////////////////////////////////////
///////
//PURPOSE : This function converts the Str into [int argc] & [char*
argv[]],
// which are the same as the argument of the C main function.
//
//ARGUMENT : Str contains the command , switch and archive file.
//RETURN : the number in of the argument.
//INFO : 1.skip leading white space and table space!
// 2.if occur the '\0' break;
// 3.Put the remain string into the Argv[Argc], be remember Argv[]
// is a string Pointer. if you file '\0' in string it means that
// you fill the Argv[][].
// 4.check the ' ' '\t' '\n'
// 5.fill the string by '\n'
// 6.change the string pointer to the next argument.
//TestStatus: UNDO
///////////////////////////////////////////////////////////////////////////////
///////
int ConvertArg(char* Str)
{
int Argc; // Count the argument).
char* Argv[MAXARGC]; // to hold the parse result.
char *StrPtr, *CurrentPtr;
StrPtr = Str;

for(Argc = 0;Argc < MAXARGC;Argc++) // init the Argv.
{
Argv[Argc] = NULLCHAR;
}


for(Argc = 0;Argc < MAXARGC && (*StrPtr) != '\0';)
{
// Skip leading white space and table space!
while(*StrPtr == ' ' || *StrPtr == '\t')
{
StrPtr++;
}

// When that is only space in the string this instance will happen!
if((*StrPtr) == '\0') // break if occur the char '\0'
{
break;
}

Argv[Argc++] = StrPtr; // Beginning of token.

// Find space or tab. If not present then we've already found the
last token.
for (CurrentPtr = StrPtr; *CurrentPtr; CurrentPtr++)
{
if (*CurrentPtr == ' ' || *CurrentPtr == '\t')
{
break;
}
}

if (*CurrentPtr != '\0')
{
*CurrentPtr++ = '\0';
}
StrPtr = CurrentPtr;
}

// empty command line
if (Argc < 1)
{
Argc = 1;
Argv[0] = "";
}

return Argc;
}
 
M

Mark McIntyre

but my program is not want to run under the systemV Unix or something
like it.
That is not getopt system call

the source for getopt() is available in the public domain. Try the gnu archives.
 
C

CBFalconer

Mark said:
the source for getopt() is available in the public domain. Try the
gnu archives.

You mean "The source for some version of something sometimes known
as getopt is ...,". There is no standard for this. For example,
the following is viable:

char *getopt(void) {return "opt";}

illustrating why the content of this group is constrained. :)
 
D

Daniel Vallstrom

CBFalconer said:
You mean "The source for some version of something sometimes known
as getopt is ...,". There is no standard for this. For example,
the following is viable:

char *getopt(void) {return "opt";}

Almost certainly not. getopt is covered by POSIX.


Daniel Vallstrom
 
M

Mac

the source for getopt() is available in the public domain. Try the gnu archives.

I would be very surprised if GNU put any of their stuff into the public
domain. AFAIK, GNU (or the FSF) maintains copyright ownership of all their
software and licenses it according to the GPL or LGPL.

--Mac
 
M

Mark McIntyre

You mean "The source for some version

indeed. I was merely indicating that
a) the wheel doesn't need reinvented
b) getopt is not restricted to SysV.
illustrating why the content of this group is constrained. :)

Well, that was rather why I redirected out of it.... :)
 
M

Mark McIntyre

I would be very surprised if GNU put any of their stuff into the public
domain. AFAIK, GNU (or the FSF) maintains copyright ownership of all their
software and licenses it according to the GPL or LGPL.

A rose.
 
C

CBFalconer

Daniel said:
Almost certainly not. getopt is covered by POSIX.

I see no reference to POSIX in the C standard. I believe getopt is
also firmly in the users namespace. This is c.l.c, and is
completely independant of POSIX.

Whether or not it is wise to write such a function named as above
is another matter entirely.
 
K

Keith Thompson

I want to parse a string like C program parse the command line into
argc & argv[][].
I hope don't use the array the allocate a fix memory first, and don't
use the memory allocate function like malloc.
who can give me some ideas?
The following is my program, but it has some problem. I hope someone
would correct it.
[snip]

I haven't studied your program in detail, but it looks like you're
trying to parse a single string into blank-delimited arguments; for
example, "foo bar" would yield the two strings "foo" and "bar".

You should be aware that typical C programs don't do this; rather,
it's done for them before they're executed. A C main program receives
a list of arguments in argc and argv. In some cases, whatever entity
invoked the program may have generated the list by parsing a single
string, splitting on blanks; in other cases, the arguments are
generated as a list, and never appear as a single string.

As far as C is concerned, blanks are not special; they're just like
any other character that can appear in the argument list.

<OT>
And if you look at getopt, you'll probably find that it works with
arguments already split into individual arguments.
</OT>

<OT>
Unix shells typically convert a single string into a command name and
a list of arguments, but the way they do so is more complex than just
splitting on blanks; see the documentation for their handling of
backslash and quotation characters.
</OT>

That's not to say that what you're trying to do isn't potentially
useful, but you probably need to nail down the problem definition.
 
D

Daniel Vallstrom

CBFalconer said:
I see no reference to POSIX in the C standard. I believe getopt is
also firmly in the users namespace. This is c.l.c, and is
completely independant of POSIX.

While there are references to POSIX in the C standard, that's beside
the point. The point is that there is a ubiquitous definition of
getopt.

Whether or not it is wise to write such a function named as above
is another matter entirely.

No, the question is if there is a standard for getopt.


Daniel Vallstrom
 
L

linzhenhua1205

Thank you.
Yet, I worked on Embedded System, That is just some simple Ansi C
function library.
And The command will come in the string format. so I should parse it.

I see that it have lots of thing should be done in the way C parses the
command,
but I just want to support some simple function.
so I look for help to see whether that is some thing more to be care.
Then I can decide which function I should be supported.
 
C

CBFalconer

Daniel said:
CBFalconer wrote:
.... snip ...


No, the question is if there is a standard for getopt.

That can be answered directly. In c.l.c, there is not any such
standard. In other environments, there may be, but that is of no
interest here.
 
K

Keith Thompson

Daniel Vallstrom said:
CBFalconer wrote: [...]
I see no reference to POSIX in the C standard. I believe getopt is
also firmly in the users namespace. This is c.l.c, and is
completely independant of POSIX.

While there are references to POSIX in the C standard, that's beside
the point. The point is that there is a ubiquitous definition of
getopt.

The word POSIX appears exactly once in the C99 standard, specifically
in the bibliograpy:

ISO/IEC 9945-2:1993, Information technology -- Portable Operating
System Interface (POSIX) -- Part 2: Shell and Utilities.
No, the question is if there is a standard for getopt.

There is a standard for getopt, but it's off-topic here.

On the other hand, when choosing identifiers in a C program, it's
probably worthwhile to be aware of identifiers used in widespread
auxiliary standard. Writing your own function called "getopt" could
lead to problems if your program is eventually going to depend on
POSIX.

(Name collisions are a problem that C doesn't handle particularly
well; IMHO something like C++ namespaces would be a good addition to
the language.)
 
N

Nick Keighley

I want to parse a string like C program parse the command line into
argc & argv[][].

I'm not entirly clear what you are trying to do. Do you want to break
up a string into substrings in a similar fashion to the way C handles
its command line arguments?

so this string "now is the winter of our discontent" would be broken
up into separate strings :-

"now", "is", "the", "winter", "of", "our", "discontent"

It might be worth taking a look at the standard function strtok(). It
has a few problems (may not handle "empty" arguments the way you want,
uses static data and modifies the argument string).

I hope don't use the array the allocate a fix memory first,

you don't want to alloocate the memory in advance?

and don't use the memory allocate function like malloc.

nor use malloc()?

strtok() doesn't do either of these things (just don't pass it
a string literal...).


who can give me some ideas?
The following is my program, but it has some problem. I hope someone
would correct it.

I can't be bothered to corect all of it (not least because I don't
know what it's supposed to be doing), but I tried compiling it.

int main(int argc, char* argv[])
{
char** argv1;
char* str = "";

char** str1 = "test";

do you really want to do this? You are initialising a char* with a
char**.
A bad idea I think.

char* str2 = " ";
memcpy(str2, str1[0], 4);

str2 has room for one character (plus a string delimiter) and you've
copied 4...

printf("%s\n", str2);
// test ConvertStr()
str = " a asdf ";
argv1 = "";

you are assigning a char* to a char**

printf("argc = %d, argv0 = \n",ConvertArg(str, argv1));
str = " a asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s, argv2 =
%s\n",ConvertArg(str, argv1), argv1[0], argv1[1], argv1[3]);
return 1;
}

I think you are still a bit confused about C strings and pointers.
Re-read
the appropriate parts of your C book. K&R is pretty good on this, do
the
exercises. If you get stuck ask again.


Happy Programming!

--
Nick Keighley

"High Integrity Software: The SPARK Approach to Safety and Security"
Customers interested in this title may also be interested in:
"Windows XP Home"
(Amazon)
 
D

Daniel Vallstrom

Keith said:
There is a standard for getopt, but it's off-topic here.

Let's review the subthread:

OP asked a question about command line options.

Someone replied "Have a look at getopt" or something to that
effect.

CBF replied that getopt isn't well-defined. Specifically he wrote:
'You mean "The source for some version of something sometimes
known as getopt is ...,". There is no standard for this.'

I then said that 'getopt' is indeed well-defined.

Don't get hung up on the fact that getopt isn't defined by the C
standard. CBF could (conceptually) just as well have complained about
the word 'look' instead of 'getopt'. Then, after getting a reply that
'look' is well-defined, it's wrong to say "yeah, but 'look' is not
defined by the C standard".


Daniel Vallstrom
 
J

Joona I Palaste

Let's review the subthread:
OP asked a question about command line options.
Someone replied "Have a look at getopt" or something to that
effect.
CBF replied that getopt isn't well-defined. Specifically he wrote:
'You mean "The source for some version of something sometimes
known as getopt is ...,". There is no standard for this.'
I then said that 'getopt' is indeed well-defined.
Don't get hung up on the fact that getopt isn't defined by the C
standard. CBF could (conceptually) just as well have complained about
the word 'look' instead of 'getopt'. Then, after getting a reply that
'look' is well-defined, it's wrong to say "yeah, but 'look' is not
defined by the C standard".

That's an entirely different thing. The word "look" here refers to the
common verb "look", but "getopt" refers to a C function called "getopt".
We are very strict about C functions, and damn proud of it. However,
with regard to common words such as verbs, we have the same everyday
rules as anyone else.
 
D

Daniel Vallstrom

Joona said:
Daniel Vallstrom <[email protected]> scribbled the following:







That's an entirely different thing. The word "look" here refers to the
common verb "look", but "getopt" refers to a C function called "getopt".
We are very strict about C functions, and damn proud of it. However,
with regard to common words such as verbs, we have the same everyday
rules as anyone else.

It wasn't a different thing (as evident from the quotes in the thread
review above) but now you have made it into something else.

Are you seriously suggesting that you aren't allowed to refer someone
to getopt when she asks for it? As a matter of fact it is alright to
discuss C tools to some extent in clc if there is no better place.
Further more, in this subthread there has been no discussion about
getopt, only references to it.

/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules!
--------/

In what way?


Daniel Vallstrom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top