LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-22-2007, 07:17 PM   #1
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
console interaction in C?


So, I know that one can use the system(); command in C to run an external command. My question is, is there any way to interact with that program from within your C code?

I'm trying to piece together an extremely lightweight but relatively full-featured set of apps for an ancient machine, and I'd love a media player with a nicer playlist-type interface similar to rhythmbox, but in gtk+2 only and very memory concious. I'm trying to write it myself and I'm wondering if there's anyway to leverage MPlayer's existing interactive console controls or if I'll have to delve into the MPlayer (or perhaps MPlayerXP) code.
 
Old 01-22-2007, 10:04 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You can open a pipe to the program, and send data to it that way (the program reading it from stdin). This is suitable for programs which read commands from stdin, e.g. bc.
 
Old 01-22-2007, 10:07 PM   #3
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
Got it, I suppose my inexperience at programming is showing, as I have no clue how to open a pipe, but I suppose now is a good time to rtfm! It's good for me :-)

Thanks!
 
Old 01-22-2007, 10:10 PM   #4
nnivanov
LQ Newbie
 
Registered: Nov 2006
Location: Russia, Velikiy Novgorod
Distribution: Slackware 12
Posts: 18

Rep: Reputation: 0
It's really easy:

man 2 pipe
man 1 mkfifo
 
Old 01-22-2007, 11:30 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
popen is probably the call you want to read up on. popen will start a process and depending on the type you select, you can read from the pipe, or write to it. Note that you cannot do both reading and writing. If you need to both write to and read from a process, you can do it using pty. Here's some example code snippets: http://www.codase.com/search/call?name=openpty
 
Old 01-23-2007, 10:42 AM   #6
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
It would appear that pty's are exactly what I'm looking for, but I'm a bit unsure of the manpage for openpty(). It makes constant reference to int values master and slave? The way I'm understanding it, I give it master, and a pointer to slave, and it changes the contents of slave. But then how do I go from having master and slave to having read/write access to a stream?

Quote:
int openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp);
The openpty() function finds an available pseudo-tty and returns file
descriptors for the master and slave in amaster and aslave. If name is
non-null, the filename of the slave is returned in name. If termp is
non-null, the terminal parameters of the slave will be set to the values
in termp. If winp is non-null, the window size of the slave will be set
to the values in winp.
by "file descriptor" do they mean that amaster is an integer representation of a null-terminaled string containing the filesystem location, like /dev/ptyp2 for example?
 
Old 01-23-2007, 10:22 PM   #7
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
I'm having a remarkably hard time finding any documentation on openpty() or forkpty(). The little documentation I have found, just the man page, is what leads me to believe that I want forkpty(), but can anyone point me to some more documentation? I can't figure out how to use these. Thanks!
 
Old 01-23-2007, 10:55 PM   #8
shevegen
Member
 
Registered: May 2004
Distribution: Slackware / GoboLinux / LFS / VoidLinux
Posts: 145

Rep: Reputation: 26
You can do a lot of stuff, my favourite is still attaching a library
like paco on sourceforge does - but actually, I dont think its
using system()
 
Old 01-24-2007, 11:17 AM   #9
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
if openpty() and/or forkpty() isn't well documented, is there any other way? I do need two way communication.
 
  


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
Automated Interaction rubadub Programming 1 11-12-2006 06:51 AM
CSS element interaction thew00t Programming 4 01-24-2006 10:23 AM
python os interaction help JoeUser11 Programming 6 06-22-2005 11:46 PM
Interaction in shell script sudhasmyle Programming 1 12-08-2004 07:27 AM
No desktop interaction Iriel Linux - Newbie 1 11-11-2003 10:05 AM

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

All times are GMT -5. The time now is 04:49 PM.

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