Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Something like Perl's eval on C? (Solved)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
jho
Apprentice
Apprentice


Joined: 24 May 2007
Posts: 153
Location: Jyväskylä, Finland

PostPosted: Wed Feb 27, 2008 5:52 pm    Post subject: Something like Perl's eval on C? (Solved) Reply with quote

Hello,

I have an IRC bot I wrote in Perl. One of it's features is eval, so I can for example do something like this from IRC (just an example):
Code:
19:46:54 @Cene >> -eval my @q = `df -h`; print $::net "PRIVMSG $::channel :$_\n" for(@q);
19:46:55 @catnip >> Filesystem            Size  Used Avail Use% Mounted on
19:46:55 @catnip >> /dev/sda3              19G   12G  5.7G  68% /
19:46:55 @catnip >> udev                   10M  164K  9.9M   2% /dev
19:46:55 @catnip >> /dev/sda4              92G   60G   27G  70% /home
19:46:55 @catnip >> shm                   252M     0  252M   0% /dev/shm
19:46:56 @catnip >> /dev/sda1              38M   12M   25M  31% /boot


The command basically picks up anything after the -eval and does eval "$_";

Is there something like Perl's eval available for C? Something that could evaluate the command from a variable (or more accurately, char* on C's case).

I tried googling, but didn't find anything useful.


Last edited by jho on Wed Feb 27, 2008 8:24 pm; edited 1 time in total
Back to top
View user's profile Send private message
timeBandit
Bodhisattva
Bodhisattva


Joined: 31 Dec 2004
Posts: 2674
Location: here, there or in transit

PostPosted: Wed Feb 27, 2008 6:09 pm    Post subject: Reply with quote

Strictly speaking, no, because C is a compiled not an interpreted language. You could execute an arbitrary shell command using the system() function (and accept all the gaping security holes that implies), but you can't evaluate a C expression such as 'printf("Hello IRC bot\n");'.

Based on your example though, it seems like system() may be what you want. man 3 system for details.
_________________
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Back to top
View user's profile Send private message
jho
Apprentice
Apprentice


Joined: 24 May 2007
Posts: 153
Location: Jyväskylä, Finland

PostPosted: Wed Feb 27, 2008 6:24 pm    Post subject: Reply with quote

timeBandit wrote:
Strictly speaking, no, because C is a compiled not an interpreted language. You could execute an arbitrary shell command using the system() function (and accept all the gaping security holes that implies), but you can't evaluate a C expression such as 'printf("Hello IRC bot\n");'.

Based on your example though, it seems like system() may be what you want. man 3 system for details.
Yeah, I already know of system(), and it is only partly what I'm looking for.. I guess I'll have to settle for that anyways. Another usage for that would be to change some variables on the fly. I've already thought of other ways to do that though.

Is there any way to save the output of system() call to a variable? I thought about saving the output to some file and then open and go through it on the program, but is there some better way?
Back to top
View user's profile Send private message
LikeCrack
Tux's lil' helper
Tux's lil' helper


Joined: 10 Nov 2006
Posts: 92
Location: Hannover, Germany

PostPosted: Wed Feb 27, 2008 6:54 pm    Post subject: Reply with quote

man 3 popen

or you can remap fd 0, 1, 2 to a pipe and read/write it to communicate with the process
_________________
I'm the fool on the hill.
Back to top
View user's profile Send private message
timeBandit
Bodhisattva
Bodhisattva


Joined: 31 Dec 2004
Posts: 2674
Location: here, there or in transit

PostPosted: Wed Feb 27, 2008 7:06 pm    Post subject: Reply with quote

schene wrote:
Is there any way to save the output of system() call to a variable? I thought about saving the output to some file and then open and go through it on the program, but is there some better way?
Only variations on the same basic idea. You can play games with the dup() and dup2() functions (qq.v.) to connect directly with stdin/stdout of the child process and avoid a temporary file, but you still have to read, buffer and process the output. There's no standard function to abstract it into a string for you.
_________________
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Back to top
View user's profile Send private message
LikeCrack
Tux's lil' helper
Tux's lil' helper


Joined: 10 Nov 2006
Posts: 92
Location: Hannover, Germany

PostPosted: Wed Feb 27, 2008 7:58 pm    Post subject: Reply with quote

i found an old example for the whole system() thing :)

http://forgottenroots.de/~vik/npipe.c
_________________
I'm the fool on the hill.
Back to top
View user's profile Send private message
jho
Apprentice
Apprentice


Joined: 24 May 2007
Posts: 153
Location: Jyväskylä, Finland

PostPosted: Wed Feb 27, 2008 8:24 pm    Post subject: Reply with quote

Ok, thanks to the both of you. I think I can make it through the rest myself. ;)

Marking solved.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum