LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2005, 03:35 PM   #1
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
C function for IRCD reply parsing


OK .. so basically I have a program wich connects to an IRC server etc. .. and I'm trying to parse the IRC server's replys .. so I built a "parse_irc()" function wich should basicly split the server's reply into tokens separated by "\n" and pass each of these tokens to a "parse_line()" function to do the rest of the work ..

the parse_line() function is rather borring at the moment and looks something like this:

void parse_line(char* str)
{
fprintf(stdout, "parsing line: %s\n", str);
fflush(stdout);
return;
}

the parse_irc() function looks like this:



Code:
void parse_irc()
{
	char *tok, *tokcpy, *auxbuf;
	size_t unparsed_len;
	unsigned char last_tok_is_unparsed = 0;
	unparsed_len = strlen(unparsed);
	if (unparsed_len > 0) {
		auxbuf = malloc(unparsed_len+strlen(buf)-1);
		if (auxbuf == NULL) {
			perror("malloc");
			exit(1);
		}
		strcpy(auxbuf, unparsed);
		strcat(auxbuf, buf);
	}
	else {
		auxbuf = malloc(strlen(buf));
		if (auxbuf == NULL) {
			perror("malloc");
			exit(1);
		}
		strcpy(auxbuf, buf);
	}

	if (auxbuf[strlen(auxbuf)-1] != 10)
		last_tok_is_unparsed = 1;
	tok = strtok(auxbuf, "\n");
	do {
		tokcpy = tok;
		tok = strtok(NULL, "\n");
		if (tok != NULL)
			parse_line(tokcpy);
	} while (tok != NULL);

	if (last_tok_is_unparsed == 1) {
		unparsed = malloc(sizeof(tokcpy));
		if (unparsed == NULL)
			exit(0);
		strcpy(unparsed, tokcpy);
	}
	else {
		parse_line(tokcpy);
		free(unparsed);
	}

	free(auxbuf);
	return;
}
.. sorry for the lack of comments .. basicly , the parse_irc function treats the global variable "buf" in wich I read the server's replys:

(in function main

Code:
	do {
		if (read(socket_d, buf, sizeof(buf)-1) == 0)
			return 1;
		parse_irc();
	} while (1);
the problem is .. if I call parse_irc from anywhere in main() (and I presume this would happen regardless of the function I'd be calling it from) , the program simply exits right away (exit code 0) .. if I , lets say , replace "parse_irc()" in main with "printf("%s", buf)", the program behaves normally and just prints the server's replys ..

oh .. and here are the definitions for "unparsed" and "buf" (global):

Code:
char nickname[10], buf[257], *unparsed;
does anyone know why this is happening ?
thanks!
 
Old 05-20-2005, 08:40 PM   #2
LostSheepOfThePorn
LQ Newbie
 
Registered: May 2005
Location: Berkeley, CA
Distribution: Linux
Posts: 8

Rep: Reputation: 0
Well, first of all, you aren't passing anything to parse_irc(). Second,
while *auxbuf is pointing to valid memory, char *tok and char *tokcpy aren't pointing anywhere. This causes strcpy() to fail. If you aren't planning on passing anyting to parse_irc(), then you should probably have the function return a pointer. Just my 2 cents on what I see when I scan the code.

Last edited by LostSheepOfThePorn; 05-20-2005 at 08:51 PM.
 
Old 05-20-2005, 11:04 PM   #3
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Re: C function for IRCD reply parsing

Quote:
Originally posted by introuble
Code:
	if (last_tok_is_unparsed == 1) {
		unparsed = malloc(sizeof(tokcpy));  // should it be unparsed = malloc(strlen(tokcpy)+1);  
		if (unparsed == NULL)
			exit(0);
		strcpy(unparsed, tokcpy);
	}
I think sizeof(tokcpy) return size of (char*) but length of tokcpy
 
Old 05-20-2005, 11:37 PM   #4
LostSheepOfThePorn
LQ Newbie
 
Registered: May 2005
Location: Berkeley, CA
Distribution: Linux
Posts: 8

Rep: Reputation: 0
Your right, the statement

unparsed = malloc(sizeof(tokcpy));

Returns the size of the pointer. If it is failing because of this, shouldn't the return code be zero? Wouldn't it just be easier to change exit(0) to exit(69)? This way you could tell if the program exited because either malloc() failed or because of something else. If we did that, then like

69 would mean malloc() failed. 64 to 113 means user define exit codes.
137(or something like that) would mean strcpy() failed
0 would retain the meaning the program exited normally.

Last edited by LostSheepOfThePorn; 05-20-2005 at 11:42 PM.
 
Old 05-21-2005, 02:09 AM   #5
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Original Poster
Rep: Reputation: 31
Quote:
Well, first of all, you aren't passing anything to parse_irc()
well I don't need to pass anything to parse_irc() as it's "treating" "buf" , wich is a global variable

Quote:
char *tok and char *tokcpy aren't pointing anywhere
... why do you say that ?

the only place where I use strcpy on one of tok/tokcpy is :

Code:
	if (last_tok_is_unparsed == 1) {
		unparsed = malloc(sizeof(tokcpy));
		if (unparsed == NULL)
			exit(0);
		strcpy(unparsed, tokcpy);
	}
and in this case , tokcpy will point to the last token of buf ..

ok .. unparsed = malloc(sizeof(tokcpy)); is definitly bad .. I changed it to unparsed = malloc(strlen(tokcpy));

the main function contains this :
Code:
        if (fork() != 0) {
                perror("fork");
                return 0;
        }
after the "if" .. I added "printf("reached");" .. it seems I get on my screen "fork: Success" but "reached" is not printed ...

btw, this is how I'm compiling my program:
$ gcc -posix -Wall -O2 -o iclient iclient.c

.. before I had changed "unparsed = malloc(sizeof(tokcpy));" to "unparsed = malloc(strlen(tokcpy));" , the program didn't even get to the fork instruction ..
 
Old 05-21-2005, 03:20 AM   #6
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Quote:
Originally posted by introuble
ok .. unparsed = malloc(sizeof(tokcpy)); is definitly bad .. I changed it to unparsed = malloc(strlen(tokcpy));
There are harmless to including 1 extra byte to store '\0'.

Quote:
Originally posted by introuble
the main function contains this :
Code:
        if (fork() != 0) {
                perror("fork");
                return 0;
        }
after the "if" .. I added "printf("reached");" .. it seems I get on my screen "fork: Success" but "reached" is not printed ...

btw, this is how I'm compiling my program:
$ gcc -posix -Wall -O2 -o iclient iclient.c

.. before I had changed "unparsed = malloc(sizeof(tokcpy));" to "unparsed = malloc(strlen(tokcpy));" , the program didn't even get to the fork instruction .. [/B]
fork return -1 on failure, not 0 => your child process terminate immediately after print "fork: Success". It should be:

Code:
  
status = fork();
if (status == -1) {
  perror("fork");
  return 0;
} else if (status == 0) {
  printf("I am child\n");
} else {
  printf("My sun is %d\n", status);
}
Hope it helps
Giang Hu
 
Old 05-21-2005, 04:13 AM   #7
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Original Poster
Rep: Reputation: 31
I know very well what the return values for fork() are .. the if does exactly what I want it to do : parent or error -> exit

the problem was in the mallocs .. it's fixed now
 
Old 05-21-2005, 08:56 AM   #8
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Quote:
Originally posted by introuble
I know very well what the return values for fork() are .. the if does exactly what I want it to do : parent or error -> exit

the problem was in the mallocs .. it's fixed now
/me confused )
 
Old 05-22-2005, 03:48 AM   #9
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Original Poster
Rep: Reputation: 31
*cough*

"go into background, kill the parent, if there was an error exit silently"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick Reply "post reply" button. IsaacKuo LQ Suggestions & Feedback 16 07-01-2018 02:52 PM
ircd + ssl BeNoMiS Linux - Security 1 01-19-2004 04:46 PM
post reply & submit reply buttons annehoog LQ Suggestions & Feedback 10 01-05-2004 06:43 PM
Evolution - reply - missing own reply emanuelgreisen Linux - Software 0 01-31-2003 04:40 AM
ircd sqn Linux - Networking 2 04-26-2002 04:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration