SimpleOS

LXR

Navigation



Site hébergé par : enix

The LXR Cross Referencer for SOS

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Article:1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 6.5 ] [ 7 ] [ 7.5 ] [ 8 ] [ 9 ] [ 9.5 ]

Diff markup

Differences between /extra/termslave.c (Article 9.5) and /extra/termslave.c (Article 9)


001 /* Unix pty slave program -- David Decotigny 2    001 /* Unix pty slave program -- David Decotigny 2005
002    License: GNU GPL version 2                     002    License: GNU GPL version 2
003    Most of it taken from the GNU C library doc    003    Most of it taken from the GNU C library doc examples */
004 #include <utmp.h>                                 004 #include <utmp.h>
005 #include <sys/types.h>                            005 #include <sys/types.h>
006 #include <sys/stat.h>                             006 #include <sys/stat.h>
007 #include <fcntl.h>                                007 #include <fcntl.h>
008 #include <unistd.h>                               008 #include <unistd.h>
009 #include <stdio.h>                                009 #include <stdio.h>
010 #include <stdlib.h>                               010 #include <stdlib.h>
011 #include <termios.h>                              011 #include <termios.h>
012                                                   012 
013 /**                                               013 /**
014  * @file termslave.c                              014  * @file termslave.c
015  *                                                015  *
016  * Linux pseudo-TTY slave program. To be used     016  * Linux pseudo-TTY slave program. To be used with "qemu -monitor pty"
017  * to access qemu's monitor from any linux ter    017  * to access qemu's monitor from any linux terminal. To use it, launch
018  * a "qemu -monitor pty" in one terminal, take    018  * a "qemu -monitor pty" in one terminal, take a look at the first
019  * line stating "char device redirected to /de    019  * line stating "char device redirected to /dev/pts/4" for example,
020  * and lauch the termslave program as "./terms    020  * and lauch the termslave program as "./termslave /dev/pts/4" in
021  * another terminal.                              021  * another terminal.
022  *                                                022  *
023  * To make this correctly work, one has to app    023  * To make this correctly work, one has to apply the
024  * patch-qemu-pty.diff patch to qemu < 0.8.0 (    024  * patch-qemu-pty.diff patch to qemu < 0.8.0 (qemu 0.8.0 and beyond
025  * already includes this patch).                  025  * already includes this patch).
026  *                                                026  *
027  * This program also works with the "-serial p    027  * This program also works with the "-serial pty" flag of qemu. It can
028  * be used to send commands to the SOS serial-    028  * be used to send commands to the SOS serial-line shell (article 9).
029  */                                               029  */
030                                                   030 
031                                                   031 
032 /* Use this variable to remember original term    032 /* Use this variable to remember original terminal attributes. */
033 struct termios saved_attributes;                  033 struct termios saved_attributes;
034                                                   034 
035 static void                                       035 static void
036 reset_input_mode (void)                           036 reset_input_mode (void)
037 {                                                 037 {
038   tcsetattr (STDIN_FILENO, TCSANOW, &saved_att    038   tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
039 }                                                 039 }
040                                                   040 
041 static void                                       041 static void
042 set_input_mode (void)                             042 set_input_mode (void)
043 {                                                 043 {
044   struct termios tattr;                           044   struct termios tattr;
045                                                   045   
046   /* Make sure stdin is a terminal. */            046   /* Make sure stdin is a terminal. */
047   if (!isatty (STDIN_FILENO))                     047   if (!isatty (STDIN_FILENO))
048     {                                             048     {
049       fprintf (stderr, "Not a terminal.\n");      049       fprintf (stderr, "Not a terminal.\n");
050       exit (EXIT_FAILURE);                        050       exit (EXIT_FAILURE);
051     }                                             051     }
052                                                   052   
053   /* Save the terminal attributes so we can re    053   /* Save the terminal attributes so we can restore them later. */
054   tcgetattr (STDIN_FILENO, &saved_attributes);    054   tcgetattr (STDIN_FILENO, &saved_attributes);
055   atexit (reset_input_mode);                      055   atexit (reset_input_mode);
056                                                   056   
057   /* Set the funny terminal modes. */             057   /* Set the funny terminal modes. */
058   tcgetattr (STDIN_FILENO, &tattr);               058   tcgetattr (STDIN_FILENO, &tattr);
059   tattr.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTR    059   tattr.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
060                        |INLCR|IGNCR|ICRNL|IXON    060                        |INLCR|IGNCR|ICRNL|IXON);
061   tattr.c_lflag &= ~(ICANON|ECHO);                061   tattr.c_lflag &= ~(ICANON|ECHO);
062   tattr.c_cflag |= CLOCAL;                        062   tattr.c_cflag |= CLOCAL;
063   tattr.c_cc[VMIN] = 1;                           063   tattr.c_cc[VMIN] = 1;
064   tattr.c_cc[VTIME] = 0;                          064   tattr.c_cc[VTIME] = 0;
065   tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);    065   tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
066 }                                                 066 }
067                                                   067 
068 int main (int argc, char *argv[])                 068 int main (int argc, char *argv[])
069 {                                                 069 {
070   int term;                                       070   int term;
071                                                   071 
072   if (argc < 2)                                   072   if (argc < 2)
073     {                                             073     {
074       fprintf(stderr, "Usage: %s /dev/pts/numb    074       fprintf(stderr, "Usage: %s /dev/pts/number\n", argv[0]);
075       return -1;                                  075       return -1;
076     }                                             076     }
077                                                   077 
078   term = open(argv[1], O_RDWR);                   078   term = open(argv[1], O_RDWR);
079   if (term < 0)                                   079   if (term < 0)
080     {                                             080     {
081       perror("open");                             081       perror("open");
082       return -1;                                  082       return -1;
083     }                                             083     }
084   if (! isatty(term))                             084   if (! isatty(term))
085     {                                             085     {
086       fprintf(stderr, "%s is not a valid termi    086       fprintf(stderr, "%s is not a valid terminal\n", argv[1]);
087       return -1;                                  087       return -1;
088     }                                             088     }
089                                                   089 
090   set_input_mode();                               090   set_input_mode();
091                                                   091 
092   while (1)                                       092   while (1)
093     {                                             093     {
094       fd_set cur_set;                             094       fd_set cur_set;
095       FD_ZERO(& cur_set);                         095       FD_ZERO(& cur_set);
096       FD_SET(STDIN_FILENO, & cur_set);            096       FD_SET(STDIN_FILENO, & cur_set);
097       FD_SET(term, & cur_set);                    097       FD_SET(term, & cur_set);
098       if (select(FD_SETSIZE, & cur_set, NULL,     098       if (select(FD_SETSIZE, & cur_set, NULL, NULL, NULL) < 1)
099         continue;                                 099         continue;
100                                                   100 
101       if (FD_ISSET(term, & cur_set))              101       if (FD_ISSET(term, & cur_set))
102         {                                         102         {
103           char buf[1024];                         103           char buf[1024];
104           int len = read(term, buf, sizeof(buf    104           int len = read(term, buf, sizeof(buf));
105                                                   105 
106           if (len >= 1)                           106           if (len >= 1)
107             write(STDOUT_FILENO, buf, len);       107             write(STDOUT_FILENO, buf, len);
108           else                                    108           else
109             {                                     109             {
110               fprintf(stderr, "Master exitted\    110               fprintf(stderr, "Master exitted\n");
111               break;                              111               break;
112             }                                     112             }
113         }                                         113         }
114                                                   114 
115       if (FD_ISSET(STDIN_FILENO, & cur_set))      115       if (FD_ISSET(STDIN_FILENO, & cur_set))
116         {                                         116         {
117           char c;                                 117           char c;
118           if (read(STDIN_FILENO, &c, 1) == 1)     118           if (read(STDIN_FILENO, &c, 1) == 1)
119             {                                     119             {
120               if (c == 0x4) /* ctrl-D */          120               if (c == 0x4) /* ctrl-D */
121                 break;                            121                 break;
122               write(term, &c, 1);                 122               write(term, &c, 1);
123             }                                     123             }
124           else                                    124           else
125             break;                                125             break;
126         }                                         126         }
127     }                                             127     }
128                                                   128   
129   return 0;                                       129   return 0;
130 }                                                 130 }
                                                      

source navigation ] diff markup ] identifier search ] general search ]