Re: Can I echo something to my own command line?

From: laura fairhead (laura_fairhead_at_INVALID.com)
Date: 10/26/03


Date: Sat, 25 Oct 2003 22:10:04 +0000

In article <slrnbpjkbs.ds.i@jal.net>, Ian Gil wrote:
>I'd like to
> echo "ping " > [my own command line]
>in a script like so:
>
> #!/bin/bash
> ...[shell code here]
> echo "ping " > [my own command line]
>
>So that after running the above I end up with this command line:
>
> $ ping
> ^
> A space here saves me having to hit the space bar!
>
>so all I have to do is type the IP address and hit enter.
>
>Is such a thing possible?

Usually, no it isn't possible. To do this in the most general case
you want to be able to write to the terminal input buffer and calls
to do that aren't standard UNIX, ie; it will get very system specific
because there is no conventional interface for this functionality.
Another, less general, way would be to ask the shell to do it for
you - usually a modern shell puts the terminal into raw mode with
echo disabled while reading a line in order that the shell can take
over the line editting function and offer more features (like command line
history for example). It may be that a certain shell would allow you
to programmatically send characters to it's input buffer, although I'd
doubt it personally because the interface is still difficult to define
(how do the codes sent relate to keys pressed?) and also the problem
here you would get is that your program runs in a new process space
and there is no way without adding in new systems structures (like
devices) that that program can tell the parent to change it's environment,
if the commandline is part of the shell process it can't generally be
effected by a child shell unless there is some communication protocol
and this would end up gettng messy for limited use value.

The ideal would be if you could put a pseudo-terminal between the
real terminal and the program. The program (your shell in this case)
would communicate with the psuedo-terminal (thinking it was a real
terminal ) and generally all the psueodo-terminal does is to pass
input characters from the real terminal to the shell process
and pass outputcharacters from the shell process to the real terminal.
The psuedo-terminal would set the terminal into raw mode as the
shell does and then it would emulate the usual terminal line input
mode (with CTRL+H, CTRL+W etc.,), this is so that it could keep
everything in synchronisation when you want to start having your
program (the shell process group) send it a command to put key presses
into its buffer. Anyway, even psuedo-terminals which are themselves
widely used (xterm, screen, script, telnet,...) are not standardised
there is this huge morass of different ways of setting them up and
commanding them for the different systems so it's not much fun to
write a "quick" program to do that. Maybe someone has written a generic
psuedo-terminal program which you could use for this already and it's
out there somewhere I don't know you'd have to look around.

One idea to do this (but only in Linux)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Anyhow, I was thinking about how 'gpm' does htis in Linux and found
that it uses the console ioctl call TIOCLINUX. Unfortunately, although
this provides several functions the actual cut/paste is not done
by inserting keys directly into the buffer, it works by first specifying
an area of the screen that you want as the "selection" and then you
have a function to paste the "selection" buffer into the console.

So, you need the text on the screen and also this call only works for
the superuser (or owner of the console). In any case it is possible
to do something with this, the program can be "suid" and it can
save the screen, write the text, copy the text to the selection buffer,
paste the text, restore the screen, to get around the inconvient calls.

I wrote a quick piece of code to do all that and it will put the
first argument into the console input buffer , it is a quick and dirty
solution so I wouldn't use it for anything serious :)

byefornow
laura

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>

#include <linux/vt.h>
#include <sys/kd.h>
#include <termios.h>

int main(int argc,char **argv)
{
int s,fd,fd2;
char a[16384],b[16];
char *txt;

// text to insert into console is the first argument
if(argc != 2) { fprintf(stderr,"%s: usage: %s <text>\n",argv[0],argv[0]);
                exit(1); }
txt=argv[1];
for(s=0;s<16;++s) b[s]=0;

// save screen, cursor etc
fd2=open("/dev/vcsa0",O_RDONLY);
if(fd2<=0) return 1;
s=read(fd2,&a[0],16384);
close(fd2);

// print text at topleft
printf("\0337\033[1;1H%s\n",txt);
// select the text
b[0]=2; // select function
b[1]=1; b[3]=1; // start at (1,1)
b[5]=strlen(txt); b[7]=1; // end
b[9]=0;
fd=open("/dev/tty0",O_WRONLY);
if(fd<=0) return 1;
ioctl(fd,TIOCLINUX,&b[0]);
close(fd);
fd=open("/dev/tty0",O_WRONLY);
// and paste it to console
b[0]=3; // paste function
ioctl(fd,TIOCLINUX,&b[0]);
close(fd);

// restore screen display
printf("\0338");
fd2=open("/dev/vcsa0",O_WRONLY);
write(fd2,&a[0],s);
close(fd2);

return 0;
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

>
>
>Ian

-- 
echo alru_aafriehdab@ittnreen.tocm |sed 's/\(.\)\(.\)/\2\1/g'


Relevant Pages

  • Re: cmdEX.exe - augmenting the existing CMD.EXE
    ... > It is different from replacement shells, like Take Command or WinOne, ... Replacing the very limited editing features of the existing console ... Not wanting to engineer another shell ... > console app that uses the ReadConsole kernel32 API has this ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: cmdEX.exe - augmenting the existing CMD.EXE
    ... > It is different from replacement shells, like Take Command or WinOne, ... Replacing the very limited editing features of the existing console ... Not wanting to engineer another shell ... > console app that uses the ReadConsole kernel32 API has this ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: expect question
    ... watch the *output* from the spawned shell. ... building a terminal emulator is MUCH simpler than ... > the buffer for the bad command that would be executed on the remote device ... > modifying my buffer and outputting to the screen. ...
    (comp.lang.tcl)
  • Re: Filemanager - low mem, but reasonably comfortable?
    ... >> Console is slow for me when it comes to typing in file names. ... And all the keybindings for copying names from the panels to the command ... Meta-Return copies whatever file or dir is under the cursor to the commandline. ... > Shell, or Gentoo file manager. ...
    (comp.os.linux.misc)
  • Re: Ultra Newbie needs basic advice
    ... screen you will be using the shell in the console and are working from ... the command line. ... console mode. ... If you press ctrl-alt-Fx where x is 1 to 6 you will find ...
    (alt.os.linux)

Loading