LinuxQuestions.org
Review your favorite Linux distribution.
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 02-08-2007, 12:50 PM   #1
inlogger
LQ Newbie
 
Registered: Feb 2007
Distribution: Gentoo
Posts: 5

Rep: Reputation: 0
reading and sending to terminal


Hi all,

I am writing a program in C. The program needs to be able to read typed commands from a terminal and also sends commands to that terminal. How can I make a C program that does these things?

It's actually for a File manager I'm writing and I imagine being able to have an xterm open and my file manager open and whenever I change directory in one program, it changes in the other.

I'm new to C and to Linux but I should understand any answers I get (with research if necessary ).

Thanks,
inlogger
 
Old 02-08-2007, 01:23 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
I presume you're reading and writing in the xterm window.

For writing, there's usually nothing better than printf().

For reading, there are two stages: getting the data into a character array, and parsting those characters into whatever variables you want.

For getting the data into a character array, use fgets(character pointer, character count, stdin).

Do not use gets; if the person types more than the amount you've reserved for your buffer, it'll blithely overwrite whatever's next in memory. THIS IS A COMMON ERROR.

fgets() lets you set a limit on how much is input. The final input character is followed by a NUL character (0x00). If the previous character is '\n' (0x0A), you know that the whole typed line fit into the buffer; if not, then the user typed too many characters. The rest must be absorbed by another fgets().

Before reading into a buffer, you need to actually allocate that buffer. You can do it statically, thus:

Code:
char input_buffer[160];
... or you can allocate it dynamically, thus:

Code:
#include <stdio.h>
#include <stdlib.h>

.
.
.

char *input_buffer;

.
.
.

input_buffer=malloc(160);

if(input_buffer==NULL)
{
  fprintf(stderr,
          "allocation of keyboard input buffer failed\n"
         );

  exit(1);
}
The former will probably be ok for small amounts of memory; 160 bytes qualifies as small. For large amounts, use the latter method, although it's quite ok to use the latter method also for small amounts.

Once you've used fgets() to read in the data, you might wish to replace that '\n' at the end with a NUL character, just to keep things less confusing while coding and debugging. It isn't necessary, but I've made it a habit.

Then use sscanf() to parse the data, unless you want to parse the data "by hand". Resist the notion of using scanf() or fscanf() instead of fgets() followed by sscanf(); I've seen programs become difficult to debug if they're presented with invalid data and they're using scanf() or fscanf().

Hope this helps.

Last edited by wjevans_7d1@yahoo.co; 02-08-2007 at 01:24 PM.
 
Old 02-09-2007, 01:55 AM   #3
inlogger
LQ Newbie
 
Registered: Feb 2007
Distribution: Gentoo
Posts: 5

Original Poster
Rep: Reputation: 0
I don't mean just printing to an xterm and prompting the user for input. I want to send and eavesdrop on commands that the terminal processes.

I what I mean to say is: If my program sends the command "cd /etc" to the xterm terminal, it will change its directory to /etc.

I also want it so that if user typed "cd /etc" into the terminal, my program would know about it.
 
Old 02-09-2007, 08:23 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Ok, here's what you can do.

Imagine that you have a special I/O device that allowed your program to act like a human user. Whenever your program outputs to this device, it acts as though you, the human, had typed on the keyboard. Whatever the output to the screen would be, your program could read from the device.

None of this actually goes through a keyboard or a screen; this device acts as though it were a keyboard/screen combination that your program (rather than you) uses.

Then your program could read from the actual keyboard (standard input), send that to this special device after monitoring and maybe modifying it, reading the output from that special device, and sending it to the screen (standard output) after monitoring and maybe modifying it.

There is such a special device. It's called a pty (pseudoteletype).

For an intro, go here:

http://en.wikipedia.org/wiki/Pseudo_terminal

For more info, go here:

http://www.die.net/doc/linux/man/man7/pty.7.html

I have a program which uses a pty to automate picking up mail from one of my ISP's. Through March of 2007, you'll be able to download it from here:

http://www.panix.com/~wje/wiwohefi/getmale.c

Use it as a sample for using pty's.

Hope this helps.
 
Old 02-09-2007, 12:23 PM   #5
inlogger
LQ Newbie
 
Registered: Feb 2007
Distribution: Gentoo
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks

Great information there.

I know how I'll do my program now.

I must say it took me a while to get my head around the concept and how to use it for what I wanted. Playing with the program "script" helped out. It is a good example of putting a program between the user and the terminal when you run the command
Code:
script > `tty`
.

And thankyou again for putting up your program.

inlogger
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Reading from stdin till eof stuck when sending multiple commands fredyk_ Linux - Software 1 04-25-2006 03:23 AM
reading characters from terminal driver kshkid Programming 2 01-17-2006 09:02 AM
Sending Data Bytes to a terminal jonty_11 Programming 8 03-28-2005 08:55 PM
Reading Arabic in Linux terminal gamor Linux - Newbie 1 03-11-2005 12:16 AM
Digiboard accel4r sending modem commands to terminal lansman Linux - Newbie 0 04-06-2003 10:58 AM

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

All times are GMT -5. The time now is 06:06 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