astyle --recursive --indent=spaces=2 --style=kr --indent-col1-comments --break-blocks --convert-tabs --max-code-length=200

This is done for better merging branch/LG_PRE_RELEASE_0.3 into trunk (same process is applied on this branch)

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@7073 818b1a75-f10b-46b9-bf7c-635c3b92a50f
This commit is contained in:
Lionel Gauthier
2015-04-09 09:29:12 +00:00
parent f0a8a0b3b5
commit 059a0a3fdc
2044 changed files with 1182666 additions and 1145123 deletions
+426 -366
View File
@@ -58,423 +58,483 @@ void link_slave (int fd);
int main(int argc, char *argv[])
{
int result;
extern char *optarg;
extern int optind;
int maxfd = -1;
char devbuf[512];
int devbytes;
int remoteaddrlen;
int c;
int waitlogged = 0;
int maxConnects = 1;
int writeonly = 0;
register int i;
int result;
extern char *optarg;
extern int optind;
int maxfd = -1;
char devbuf[512];
int devbytes;
int remoteaddrlen;
int c;
int waitlogged = 0;
int maxConnects = 1;
int writeonly = 0;
register int i;
while ( (c=getopt(argc,argv,"dl:m:p:r:s:wx:")) != EOF )
switch (c) {
case 'd':
isdaemon = 1;
break;
case 'l':
linkname = optarg;
break;
case 'x':
debug = atoi(optarg);
break;
case 'm':
maxConnects = atoi(optarg);
break;
case 'p':
server_port = atoi(optarg);
break;
case 'r':
machinename = optarg;
break;
case 's':
sttyparms = optarg;
break;
case 'w':
writeonly = 1;
break;
case '?':
usage(argv[0]);
exit(1);
}
while ( (c=getopt(argc,argv,"dl:m:p:r:s:wx:")) != EOF )
switch (c) {
case 'd':
isdaemon = 1;
break;
sdevname = argv[optind];
remotefd = (int *) malloc (maxConnects * sizeof(int));
case 'l':
linkname = optarg;
break;
// struct group *getgrgid(gid_t gid);
case 'x':
debug = atoi(optarg);
break;
printf("sdevname=%s,server_port=%d,stty=%s\n",sdevname,server_port,sttyparms);
case 'm':
maxConnects = atoi(optarg);
break;
openlog("remserial", LOG_PID, LOG_USER);
case 'p':
server_port = atoi(optarg);
break;
if (writeonly)
devfd = open(sdevname,O_WRONLY);
else
devfd = open(sdevname,O_RDWR);
case 'r':
machinename = optarg;
break;
if ( devfd == -1 ) {
syslog(LOG_ERR, "Open of %s failed: %m",sdevname);
printf("Open of %s failed: %m",sdevname);
exit(1);
case 's':
sttyparms = optarg;
break;
case 'w':
writeonly = 1;
break;
case '?':
usage(argv[0]);
exit(1);
}
if (linkname)
link_slave(devfd);
sdevname = argv[optind];
remotefd = (int *) malloc (maxConnects * sizeof(int));
if ( sttyparms ) {
set_tty(devfd,sttyparms);
// struct group *getgrgid(gid_t gid);
printf("sdevname=%s,server_port=%d,stty=%s\n",sdevname,server_port,sttyparms);
openlog("remserial", LOG_PID, LOG_USER);
if (writeonly)
devfd = open(sdevname,O_WRONLY);
else
devfd = open(sdevname,O_RDWR);
if ( devfd == -1 ) {
syslog(LOG_ERR, "Open of %s failed: %m",sdevname);
printf("Open of %s failed: %m",sdevname);
exit(1);
}
if (linkname)
link_slave(devfd);
if ( sttyparms ) {
set_tty(devfd,sttyparms);
}
signal(SIGINT,sighandler);
signal(SIGHUP,sighandler);
signal(SIGTERM,sighandler);
if ( machinename ) {
//-----------------------------------------------------------------
// We are the client, Find the IP address for the remote machine
//-----------------------------------------------------------------
remotehost = gethostbyname(machinename);
if ( !remotehost ) {
syslog(LOG_ERR, "Couldn't determine address of %s", machinename );
exit(1);
}
signal(SIGINT,sighandler);
signal(SIGHUP,sighandler);
signal(SIGTERM,sighandler);
/* Copy it into the addr structure */
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = 0;
if ( machinename ) {
//-----------------------------------------------------------------
// We are the client, Find the IP address for the remote machine
//-----------------------------------------------------------------
//memcpy(&(addr.sin_addr),remotehost->h_addr_list[0], sizeof(struct in_addr));
//addr.sin_port = htons(server_port);
addr.sin_port = htons(0);
remotehost = gethostbyname(machinename);
if ( !remotehost ) {
syslog(LOG_ERR, "Couldn't determine address of %s", machinename );
exit(1);
}
remotefd[curConnects] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
/* Copy it into the addr structure */
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = 0;
//memcpy(&(addr.sin_addr),remotehost->h_addr_list[0], sizeof(struct in_addr));
//addr.sin_port = htons(server_port);
addr.sin_port = htons(0);
remotefd[curConnects] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( remotefd[curConnects] == -1 ) {
syslog(LOG_ERR, "Can't open socket: %m");
exit(1);
}
/* Set up to listen on the given port */
if( bind( remotefd[curConnects], (struct sockaddr*)(&addr), sizeof(struct sockaddr_in)) < 0 ) {
syslog(LOG_ERR, "Couldn't bind port %d, aborting: %m",client_port );
exit(1);
}
addr.sin_family = AF_INET;
memcpy(&(addr.sin_addr),remotehost->h_addr_list[0], sizeof(struct in_addr));
addr.sin_port = htons(server_port);
if (connect(remotefd[curConnects], (struct sockaddr*)(&addr), sizeof(struct sockaddr_in)) < 0 ) {
syslog(LOG_ERR, "Couldn't connect client socket to server socket, aborting: %m");
exit(1);
}
if ( debug>1 )
syslog(LOG_NOTICE,"Connected to remote UDP socket");
curConnects += 1;
} else {
//-------------------------------
// We are the server
//-------------------------------
/* Open the initial socket for communications */
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( sockfd == -1 ) {
syslog(LOG_ERR, "Can't open socket: %m");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = 0;
addr.sin_port = htons(server_port);
/* Set up to listen on the given port */
if( bind( sockfd, (struct sockaddr*)(&addr),
sizeof(struct sockaddr_in)) < 0 ) {
syslog(LOG_ERR, "Couldn't bind port %d, aborting: %m",server_port );
exit(1);
}
if ( debug>1 )
syslog(LOG_NOTICE,"Bound port");
if ( remotefd[curConnects] == -1 ) {
syslog(LOG_ERR, "Can't open socket: %m");
exit(1);
}
if ( isdaemon ) {
setsid();
close(0);
close(1);
close(2);
/* Set up to listen on the given port */
if( bind( remotefd[curConnects], (struct sockaddr*)(&addr), sizeof(struct sockaddr_in)) < 0 ) {
syslog(LOG_ERR, "Couldn't bind port %d, aborting: %m",client_port );
exit(1);
}
/* Set up the files/sockets for the select() call */
if ( sockfd != -1 ) {
FD_SET(sockfd,&fdsread);
if ( sockfd >= maxfd )
maxfd = sockfd + 1;
addr.sin_family = AF_INET;
memcpy(&(addr.sin_addr),remotehost->h_addr_list[0], sizeof(struct in_addr));
addr.sin_port = htons(server_port);
if (connect(remotefd[curConnects], (struct sockaddr*)(&addr), sizeof(struct sockaddr_in)) < 0 ) {
syslog(LOG_ERR, "Couldn't connect client socket to server socket, aborting: %m");
exit(1);
}
for (i=0 ; i<curConnects ; i++) {
FD_SET(remotefd[i],&fdsread);
if ( remotefd[i] >= maxfd )
maxfd = remotefd[i] + 1;
if ( debug>1 )
syslog(LOG_NOTICE,"Connected to remote UDP socket");
curConnects += 1;
} else {
//-------------------------------
// We are the server
//-------------------------------
/* Open the initial socket for communications */
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( sockfd == -1 ) {
syslog(LOG_ERR, "Can't open socket: %m");
exit(1);
}
if (!writeonly) {
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = 0;
addr.sin_port = htons(server_port);
/* Set up to listen on the given port */
if( bind( sockfd, (struct sockaddr*)(&addr),
sizeof(struct sockaddr_in)) < 0 ) {
syslog(LOG_ERR, "Couldn't bind port %d, aborting: %m",server_port );
exit(1);
}
if ( debug>1 )
syslog(LOG_NOTICE,"Bound port");
}
if ( isdaemon ) {
setsid();
close(0);
close(1);
close(2);
}
/* Set up the files/sockets for the select() call */
if ( sockfd != -1 ) {
FD_SET(sockfd,&fdsread);
if ( sockfd >= maxfd )
maxfd = sockfd + 1;
}
for (i=0 ; i<curConnects ; i++) {
FD_SET(remotefd[i],&fdsread);
if ( remotefd[i] >= maxfd )
maxfd = remotefd[i] + 1;
}
if (!writeonly) {
FD_SET(devfd,&fdsread);
if ( devfd >= maxfd )
maxfd = devfd + 1;
}
while (1) {
/* Wait for data from the listening socket, the device
or the remote connection */
fdsreaduse = fdsread;
if ( select(maxfd,&fdsreaduse,NULL,NULL,NULL) == -1 )
break;
/* Activity on the controlling socket, only on server */
if ( !machinename && FD_ISSET(sockfd,&fdsreaduse) ) {
remoteaddrlen = sizeof(struct sockaddr_in);
devbytes = recvfrom(sockfd, devbuf, 512, 0, (struct sockaddr *) &remoteaddr, &remoteaddrlen);
//if ( debug>1 && devbytes>0 )
if (debug>1) {
syslog(LOG_INFO,"Remote: %d bytes",devbytes);
}
printf("Socket Remote read: %d bytes\n",devbytes);
if ( devbytes == 0 ) {
} else if ( devfd != -1 ) {
if (connect(sockfd, (struct sockaddr*)(&remoteaddr), remoteaddrlen) < 0 ) {
syslog(LOG_ERR, "Couldn't connect server socket to client socket, aborting: %m");
exit(1);
}
// socket read/write
//remotefd[curConnects] = sockfd;
/* Write the data to the device */
result = write(devfd,devbuf,devbytes);
if (result < 0) {
printf("write(%d,devbuf,%d):%s", devfd, devbytes,strerror(result));
} else {
printf("Write to device returned %d", result);
}
fflush(NULL);
}
}
/* Data to read from the device */
if ( FD_ISSET(devfd,&fdsreaduse) ) {
devbytes = read(devfd,devbuf,512);
//if ( debug>1 && devbytes>0 )
if (debug>1) {
syslog(LOG_INFO,"Device: %d bytes",devbytes);
}
printf("Device: %d bytes", devbytes);
if ( devbytes <= 0 ) {
if ( debug>0 ) {
syslog(LOG_INFO,"%s closed",sdevname);
printf("%s closed",sdevname);
}
close(devfd);
FD_CLR(devfd,&fdsread);
while (1) {
devfd = open(sdevname,O_RDWR);
if ( devfd != -1 )
break;
syslog(LOG_ERR, "Open of %s failed: %m", sdevname);
if ( errno != EIO )
exit(1);
sleep(1);
}
if ( debug>0 )
syslog(LOG_INFO,"%s re-opened",sdevname);
if ( sttyparms )
set_tty(devfd,sttyparms);
if (linkname)
link_slave(devfd);
FD_SET(devfd,&fdsread);
if ( devfd >= maxfd )
maxfd = devfd + 1;
maxfd = devfd + 1;
} else {
for (i=0 ; i<curConnects ; i++) {
result = write(remotefd[i],devbuf,devbytes);
if (result < 0) {
printf("write(remotefd[%d]=%d,devbuf,%d):%s", i, remotefd[i], devbytes, strerror(result));
} else {
printf("Write to remotefd[%d]=%d returned %d", i, remotefd[i], result);
}
fflush(NULL);
}
if ( !machinename) {
result = write(sockfd, devbuf,devbytes);
if (result < 0) {
printf("write(sockfd=%d,devbuf,%d):%s", sockfd, devbytes, strerror(result));
} else {
printf("Write to sockfd=%d returned %d", sockfd, result);
}
fflush(NULL);
}
}
}
while (1) {
/* Wait for data from the listening socket, the device
or the remote connection */
fdsreaduse = fdsread;
if ( select(maxfd,&fdsreaduse,NULL,NULL,NULL) == -1 )
break;
/* Activity on the controlling socket, only on server */
if ( !machinename && FD_ISSET(sockfd,&fdsreaduse) ) {
remoteaddrlen = sizeof(struct sockaddr_in);
devbytes = recvfrom(sockfd, devbuf, 512, 0, (struct sockaddr *) &remoteaddr, &remoteaddrlen);
//if ( debug>1 && devbytes>0 )
if (debug>1) {
syslog(LOG_INFO,"Remote: %d bytes",devbytes);
}
printf("Socket Remote read: %d bytes\n",devbytes);
if ( devbytes == 0 ) {
} else if ( devfd != -1 ) {
if (connect(sockfd, (struct sockaddr*)(&remoteaddr), remoteaddrlen) < 0 ) {
syslog(LOG_ERR, "Couldn't connect server socket to client socket, aborting: %m");
exit(1);
}
// socket read/write
//remotefd[curConnects] = sockfd;
/* Write the data to the device */
result = write(devfd,devbuf,devbytes);
if (result < 0) {
printf("write(%d,devbuf,%d):%s", devfd, devbytes,strerror(result));
} else {
printf("Write to device returned %d", result);
}
fflush(NULL);
}
}
/* Data to read from the device */
if ( FD_ISSET(devfd,&fdsreaduse) ) {
devbytes = read(devfd,devbuf,512);
//if ( debug>1 && devbytes>0 )
if (debug>1) {
syslog(LOG_INFO,"Device: %d bytes",devbytes);
}
printf("Device: %d bytes", devbytes);
if ( devbytes <= 0 ) {
if ( debug>0 ) {
syslog(LOG_INFO,"%s closed",sdevname);
printf("%s closed",sdevname);
}
close(devfd);
FD_CLR(devfd,&fdsread);
while (1) {
devfd = open(sdevname,O_RDWR);
if ( devfd != -1 )
break;
syslog(LOG_ERR, "Open of %s failed: %m", sdevname);
if ( errno != EIO )
exit(1);
sleep(1);
}
if ( debug>0 )
syslog(LOG_INFO,"%s re-opened",sdevname);
if ( sttyparms )
set_tty(devfd,sttyparms);
if (linkname)
link_slave(devfd);
FD_SET(devfd,&fdsread);
if ( devfd >= maxfd )
maxfd = devfd + 1;
} else {
for (i=0 ; i<curConnects ; i++) {
result = write(remotefd[i],devbuf,devbytes);
if (result < 0) {
printf("write(remotefd[%d]=%d,devbuf,%d):%s", i, remotefd[i], devbytes, strerror(result));
} else {
printf("Write to remotefd[%d]=%d returned %d", i, remotefd[i], result);
}
fflush(NULL);
}
if ( !machinename) {
result = write(sockfd, devbuf,devbytes);
if (result < 0) {
printf("write(sockfd=%d,devbuf,%d):%s", sockfd, devbytes, strerror(result));
} else {
printf("Write to sockfd=%d returned %d", sockfd, result);
}
fflush(NULL);
}
}
}
/* Data to read from the remote system */
for (i=0 ; i<curConnects ; i++)
if (FD_ISSET(remotefd[i],&fdsreaduse) ) {
devbytes = read(remotefd[i],devbuf,512);
//if ( debug>1 && devbytes>0 )
if (debug>1) {
syslog(LOG_INFO,"Remote: %d bytes",devbytes);
}
printf("Remote: %d bytes",devbytes);
if ( devbytes == 0 ) {
/*register int j;
syslog(LOG_NOTICE,"Connection closed");
printf("Connection closed");
close(remotefd[i]);
FD_CLR(remotefd[i],&fdsread);
curConnects--;
for (j=i ; j<curConnects ; j++)
remotefd[j] = remotefd[j+1];
if ( machinename ) {
// Wait for the server again
remotefd[curConnects++] = connect_to(&addr);
FD_SET(remotefd[curConnects-1],&fdsread);
if ( remotefd[curConnects-1] >= maxfd )
maxfd = remotefd[curConnects-1] + 1;
}*/
}
else if ( devfd != -1 ) {
/* Write the data to the device */
result = write(devfd,devbuf,devbytes);
if (result < 0) {
printf("write(devfd=%d,devbuf,%d):%s", devfd, devbytes, strerror(result));
} else {
printf("Write to devfd=%d returned %d", devfd, result);
}
fflush(NULL);
}
}
}
close(sockfd);
/* Data to read from the remote system */
for (i=0 ; i<curConnects ; i++)
close(remotefd[i]);
printf("End of program, normal exit\n");
if (FD_ISSET(remotefd[i],&fdsreaduse) ) {
devbytes = read(remotefd[i],devbuf,512);
//if ( debug>1 && devbytes>0 )
if (debug>1) {
syslog(LOG_INFO,"Remote: %d bytes",devbytes);
}
printf("Remote: %d bytes",devbytes);
if ( devbytes == 0 ) {
/*register int j;
syslog(LOG_NOTICE,"Connection closed");
printf("Connection closed");
close(remotefd[i]);
FD_CLR(remotefd[i],&fdsread);
curConnects--;
for (j=i ; j<curConnects ; j++)
remotefd[j] = remotefd[j+1];
if ( machinename ) {
// Wait for the server again
remotefd[curConnects++] = connect_to(&addr);
FD_SET(remotefd[curConnects-1],&fdsread);
if ( remotefd[curConnects-1] >= maxfd )
maxfd = remotefd[curConnects-1] + 1;
}*/
} else if ( devfd != -1 ) {
/* Write the data to the device */
result = write(devfd,devbuf,devbytes);
if (result < 0) {
printf("write(devfd=%d,devbuf,%d):%s", devfd, devbytes, strerror(result));
} else {
printf("Write to devfd=%d returned %d", devfd, result);
}
fflush(NULL);
}
}
}
close(sockfd);
for (i=0 ; i<curConnects ; i++)
close(remotefd[i]);
printf("End of program, normal exit\n");
}
void sighandler(int sig)
{
int i;
int i;
if ( sockfd != -1 )
close(sockfd);
for (i=0 ; i<curConnects ; i++)
close(remotefd[i]);
if ( devfd != -1 )
close(devfd);
if (linkname)
unlink(linkname);
syslog(LOG_ERR,"Terminating on signal %d",sig);
exit(0);
if ( sockfd != -1 )
close(sockfd);
for (i=0 ; i<curConnects ; i++)
close(remotefd[i]);
if ( devfd != -1 )
close(devfd);
if (linkname)
unlink(linkname);
syslog(LOG_ERR,"Terminating on signal %d",sig);
exit(0);
}
void link_slave(int fd)
{
char *slavename;
int status = grantpt(devfd);
if (status != -1)
status = unlockpt(devfd);
if (status != -1) {
slavename = ptsname(devfd);
if (slavename) {
// Safety first
unlink(linkname);
status = symlink(slavename, linkname);
}
else
status = -1;
}
if (status == -1) {
syslog(LOG_ERR, "Cannot create link for pseudo-tty: %m");
printf("Cannot create link for pseudo-tty: %m");
exit(1);
}
char *slavename;
int status = grantpt(devfd);
if (status != -1)
status = unlockpt(devfd);
if (status != -1) {
slavename = ptsname(devfd);
if (slavename) {
// Safety first
unlink(linkname);
status = symlink(slavename, linkname);
} else
status = -1;
}
if (status == -1) {
syslog(LOG_ERR, "Cannot create link for pseudo-tty: %m");
printf("Cannot create link for pseudo-tty: %m");
exit(1);
}
}
int
connect_to(struct sockaddr_in *addr)
{
int waitlogged = 0;
int stat;
extern int errno;
int sockfd;
int waitlogged = 0;
int stat;
extern int errno;
int sockfd;
if ( debug>0 ) {
unsigned long ip = ntohl(addr->sin_addr.s_addr);
syslog(LOG_NOTICE, "Trying to connect to %d.%d.%d.%d",
(int)(ip>>24)&0xff,
(int)(ip>>16)&0xff,
(int)(ip>>8)&0xff,
(int)(ip>>0)&0xff);
if ( debug>0 ) {
unsigned long ip = ntohl(addr->sin_addr.s_addr);
syslog(LOG_NOTICE, "Trying to connect to %d.%d.%d.%d",
(int)(ip>>24)&0xff,
(int)(ip>>16)&0xff,
(int)(ip>>8)&0xff,
(int)(ip>>0)&0xff);
}
while (1) {
/* Open the socket for communications */
sockfd = socket(AF_INET, SOCK_STREAM, 6);
if ( sockfd == -1 ) {
syslog(LOG_ERR, "Can't open socket: %m");
exit(1);
}
while (1) {
/* Open the socket for communications */
sockfd = socket(AF_INET, SOCK_STREAM, 6);
if ( sockfd == -1 ) {
syslog(LOG_ERR, "Can't open socket: %m");
exit(1);
}
/* Try to connect to the remote server,
if it fails, keep trying */
/* Try to connect to the remote server,
if it fails, keep trying */
stat = connect(sockfd, (struct sockaddr*)addr,
sizeof(struct sockaddr_in));
stat = connect(sockfd, (struct sockaddr*)addr,
sizeof(struct sockaddr_in));
if ( debug>1 )
if (stat == -1)
syslog(LOG_NOTICE, "Connect status %d, errno %d: %m\n", stat,errno);
else
syslog(LOG_NOTICE, "Connect status %d\n", stat);
if ( debug>1 )
if (stat == -1)
syslog(LOG_NOTICE, "Connect status %d, errno %d: %m\n", stat,errno);
else
syslog(LOG_NOTICE, "Connect status %d\n", stat);
if ( stat == 0 )
break;
/* Write a message to syslog once */
if ( ! waitlogged ) {
syslog(LOG_NOTICE, "Waiting for server on %s port %d: %m", machinename,server_port );
waitlogged = 1;
}
close(sockfd);
sleep(10);
if ( stat == 0 )
break;
/* Write a message to syslog once */
if ( ! waitlogged ) {
syslog(LOG_NOTICE, "Waiting for server on %s port %d: %m", machinename,server_port );
waitlogged = 1;
}
if ( waitlogged || debug>0 )
syslog(LOG_NOTICE, "Connected to server %s port %d", machinename,server_port );
return sockfd;
close(sockfd);
sleep(10);
}
if ( waitlogged || debug>0 )
syslog(LOG_NOTICE, "Connected to server %s port %d", machinename,server_port );
return sockfd;
}
void usage(char *progname) {
printf("Remserial version 1.3. Usage:\n");
printf("remserial [-r machinename] [-p netport] [-s \"stty params\"] [-m maxconnect] device\n\n");
void usage(char *progname)
{
printf("Remserial version 1.3. Usage:\n");
printf("remserial [-r machinename] [-p netport] [-s \"stty params\"] [-m maxconnect] device\n\n");
printf("-r machinename The remote machine name to connect to. If not\n");
printf(" specified, then this is the server side.\n");
printf("-p netport Specifiy IP port# (default 23000)\n");
printf("-s \"stty params\" If serial port, specify stty parameters, see man stty\n");
printf("-m max-connections Maximum number of simultaneous client connections to allow\n");
printf("-d Run as a daemon program\n");
printf("-x debuglevel Set debug level, 0 is default, 1,2 give more info\n");
printf("-l linkname If the device name is a pseudo-tty, create a link to the slave\n");
printf("-w Only write to the device, no reading\n");
printf("device I/O device, either serial port or pseudo-tty master\n");
printf("-r machinename The remote machine name to connect to. If not\n");
printf(" specified, then this is the server side.\n");
printf("-p netport Specifiy IP port# (default 23000)\n");
printf("-s \"stty params\" If serial port, specify stty parameters, see man stty\n");
printf("-m max-connections Maximum number of simultaneous client connections to allow\n");
printf("-d Run as a daemon program\n");
printf("-x debuglevel Set debug level, 0 is default, 1,2 give more info\n");
printf("-l linkname If the device name is a pseudo-tty, create a link to the slave\n");
printf("-w Only write to the device, no reading\n");
printf("device I/O device, either serial port or pseudo-tty master\n");
}
+196 -177
View File
@@ -22,228 +22,247 @@
#include <unistd.h>
#include <string.h>
#define CFLG 0
#define IFLG 1
#define OFLG 2
#define LFLG 3
#define RFLG 4
#define BFLG 5
#define CFLG 0
#define IFLG 1
#define OFLG 2
#define LFLG 3
#define RFLG 4
#define BFLG 5
extern int errno;
static struct sttyset {
char *name;
int which;
int mask;
int value;
char *name;
int which;
int mask;
int value;
} sttynames[] = {
{ "0", BFLG, 0, B0 },
{ "50", BFLG, 0, B50 },
{ "75", BFLG, 0, B75 },
{ "110", BFLG, 0, B110 },
{ "134", BFLG, 0, B134 },
{ "150", BFLG, 0, B150 },
{ "200", BFLG, 0, B200 },
{ "300", BFLG, 0, B300 },
{ "600", BFLG, 0, B600 },
{ "1200", BFLG, 0, B1200 },
{ "1800", BFLG, 0, B1800 },
{ "2400", BFLG, 0, B2400 },
{ "4800", BFLG, 0, B4800 },
{ "9600", BFLG, 0, B9600 },
{ "19200", BFLG, 0, B19200 },
{ "38400", BFLG, 0, B38400 },
{ "0", BFLG, 0, B0 },
{ "50", BFLG, 0, B50 },
{ "75", BFLG, 0, B75 },
{ "110", BFLG, 0, B110 },
{ "134", BFLG, 0, B134 },
{ "150", BFLG, 0, B150 },
{ "200", BFLG, 0, B200 },
{ "300", BFLG, 0, B300 },
{ "600", BFLG, 0, B600 },
{ "1200", BFLG, 0, B1200 },
{ "1800", BFLG, 0, B1800 },
{ "2400", BFLG, 0, B2400 },
{ "4800", BFLG, 0, B4800 },
{ "9600", BFLG, 0, B9600 },
{ "19200", BFLG, 0, B19200 },
{ "38400", BFLG, 0, B38400 },
#ifdef B57600
{ "57600", BFLG, 0, B57600 },
{ "57600", BFLG, 0, B57600 },
#endif
#ifdef B115200
{ "115200", BFLG, 0, B115200 },
{ "115200", BFLG, 0, B115200 },
#endif
#ifdef B230400
{ "230400", BFLG, 0, B230400 },
{ "230400", BFLG, 0, B230400 },
#endif
{ "cs7", CFLG, CSIZE, CS7 },
{ "cs8", CFLG, CSIZE, CS8 },
{ "cstopb", CFLG, CSTOPB, CSTOPB },
{ "cread", CFLG, CREAD, CREAD },
{ "parenb", CFLG, PARENB, PARENB },
{ "parodd", CFLG, PARODD, PARODD },
{ "hubcl", CFLG, HUPCL, HUPCL },
{ "clocal", CFLG, CLOCAL, CLOCAL },
{ "cs7", CFLG, CSIZE, CS7 },
{ "cs8", CFLG, CSIZE, CS8 },
{ "cstopb", CFLG, CSTOPB, CSTOPB },
{ "cread", CFLG, CREAD, CREAD },
{ "parenb", CFLG, PARENB, PARENB },
{ "parodd", CFLG, PARODD, PARODD },
{ "hubcl", CFLG, HUPCL, HUPCL },
{ "clocal", CFLG, CLOCAL, CLOCAL },
#ifdef CRTSCTS
{ "crtscts", CFLG, CRTSCTS, CRTSCTS },
{ "crtscts", CFLG, CRTSCTS, CRTSCTS },
#endif
#ifdef ORTSFL
{ "ortsfl", CFLG, ORTSFL, ORTSFL },
{ "ortsfl", CFLG, ORTSFL, ORTSFL },
#endif
#ifdef CTSFLOW
{ "ctsflow", CFLG, CTSFLOW, CTSFLOW },
{ "ctsflow", CFLG, CTSFLOW, CTSFLOW },
#endif
#ifdef RTSFLOW
{ "rtsflow", CFLG, RTSFLOW, RTSFLOW },
{ "rtsflow", CFLG, RTSFLOW, RTSFLOW },
#endif
{ "ignbrk", IFLG, IGNBRK, IGNBRK },
{ "brkint", IFLG, BRKINT, BRKINT },
{ "ignpar", IFLG, IGNPAR, IGNPAR },
{ "parmrk", IFLG, PARMRK, PARMRK },
{ "inpck", IFLG, INPCK, INPCK },
{ "istrip", IFLG, ISTRIP, ISTRIP },
{ "inlcr", IFLG, INLCR, INLCR },
{ "igncr", IFLG, IGNCR, IGNCR },
{ "icrnl", IFLG, ICRNL, ICRNL },
#ifdef IUCLC // Missing on OSX, FreeBSD
{ "iuclc", IFLG, IUCLC, IUCLC },
{ "ignbrk", IFLG, IGNBRK, IGNBRK },
{ "brkint", IFLG, BRKINT, BRKINT },
{ "ignpar", IFLG, IGNPAR, IGNPAR },
{ "parmrk", IFLG, PARMRK, PARMRK },
{ "inpck", IFLG, INPCK, INPCK },
{ "istrip", IFLG, ISTRIP, ISTRIP },
{ "inlcr", IFLG, INLCR, INLCR },
{ "igncr", IFLG, IGNCR, IGNCR },
{ "icrnl", IFLG, ICRNL, ICRNL },
#ifdef IUCLC // Missing on OSX, FreeBSD
{ "iuclc", IFLG, IUCLC, IUCLC },
#endif
{ "ixon", IFLG, IXON, IXON },
{ "ixany", IFLG, IXANY, IXANY },
{ "ixoff", IFLG, IXOFF, IXOFF },
{ "ixon", IFLG, IXON, IXON },
{ "ixany", IFLG, IXANY, IXANY },
{ "ixoff", IFLG, IXOFF, IXOFF },
#ifdef IMAXBEL
{ "imaxbel", IFLG, IMAXBEL, IMAXBEL },
{ "imaxbel", IFLG, IMAXBEL, IMAXBEL },
#endif
{ "opost", OFLG, OPOST, OPOST },
#ifdef ILCUC // Missing on OSX, FreeBSD
{ "olcuc", OFLG, OLCUC, OLCUC },
{ "opost", OFLG, OPOST, OPOST },
#ifdef ILCUC // Missing on OSX, FreeBSD
{ "olcuc", OFLG, OLCUC, OLCUC },
#endif
{ "onlcr", OFLG, ONLCR, ONLCR },
{ "ocrnl", OFLG, OCRNL, OCRNL },
{ "onocr", OFLG, ONOCR, ONOCR },
{ "onlret", OFLG, ONLRET, ONLRET },
{ "ofil", OFLG, OFILL, OFILL },
{ "ofdel", OFLG, OFDEL, OFDEL },
{ "nl0", OFLG, NLDLY, NL0 },
{ "nl1", OFLG, NLDLY, NL1 },
{ "cr0", OFLG, CRDLY, CR0 },
{ "cr1", OFLG, CRDLY, CR1 },
{ "cr2", OFLG, CRDLY, CR2 },
{ "cr3", OFLG, CRDLY, CR3 },
{ "tab0", OFLG, TABDLY, TAB0 },
{ "tab1", OFLG, TABDLY, TAB1 },
{ "tab2", OFLG, TABDLY, TAB2 },
{ "tab3", OFLG, TABDLY, TAB3 },
{ "bs0", OFLG, BSDLY, BS0 },
{ "bs1", OFLG, BSDLY, BS1 },
{ "vt0", OFLG, VTDLY, VT0 },
{ "vt1", OFLG, VTDLY, VT1 },
{ "ff0", OFLG, FFDLY, FF0 },
{ "ff1", OFLG, FFDLY, FF1 },
{ "isig", LFLG, ISIG, ISIG },
{ "icanon", LFLG, ICANON, ICANON },
#ifdef XCASE // Missing on OSX, FreeBSD
{ "xcase", LFLG, XCASE, XCASE },
{ "onlcr", OFLG, ONLCR, ONLCR },
{ "ocrnl", OFLG, OCRNL, OCRNL },
{ "onocr", OFLG, ONOCR, ONOCR },
{ "onlret", OFLG, ONLRET, ONLRET },
{ "ofil", OFLG, OFILL, OFILL },
{ "ofdel", OFLG, OFDEL, OFDEL },
{ "nl0", OFLG, NLDLY, NL0 },
{ "nl1", OFLG, NLDLY, NL1 },
{ "cr0", OFLG, CRDLY, CR0 },
{ "cr1", OFLG, CRDLY, CR1 },
{ "cr2", OFLG, CRDLY, CR2 },
{ "cr3", OFLG, CRDLY, CR3 },
{ "tab0", OFLG, TABDLY, TAB0 },
{ "tab1", OFLG, TABDLY, TAB1 },
{ "tab2", OFLG, TABDLY, TAB2 },
{ "tab3", OFLG, TABDLY, TAB3 },
{ "bs0", OFLG, BSDLY, BS0 },
{ "bs1", OFLG, BSDLY, BS1 },
{ "vt0", OFLG, VTDLY, VT0 },
{ "vt1", OFLG, VTDLY, VT1 },
{ "ff0", OFLG, FFDLY, FF0 },
{ "ff1", OFLG, FFDLY, FF1 },
{ "isig", LFLG, ISIG, ISIG },
{ "icanon", LFLG, ICANON, ICANON },
#ifdef XCASE // Missing on OSX, FreeBSD
{ "xcase", LFLG, XCASE, XCASE },
#endif
{ "echo", LFLG, ECHO, ECHO },
{ "echoe", LFLG, ECHOE, ECHOE },
{ "echok", LFLG, ECHOK, ECHOK },
{ "echonl", LFLG, ECHONL, ECHONL },
{ "noflsh", LFLG, NOFLSH, NOFLSH },
{ "tostop", LFLG, TOSTOP, TOSTOP },
{ "echo", LFLG, ECHO, ECHO },
{ "echoe", LFLG, ECHOE, ECHOE },
{ "echok", LFLG, ECHOK, ECHOK },
{ "echonl", LFLG, ECHONL, ECHONL },
{ "noflsh", LFLG, NOFLSH, NOFLSH },
{ "tostop", LFLG, TOSTOP, TOSTOP },
#ifdef ECHOCTL
{ "echoctl", LFLG, ECHOCTL, ECHOCTL },
{ "echoctl", LFLG, ECHOCTL, ECHOCTL },
#endif
#ifdef ECHOPRT
{ "echoprt", LFLG, ECHOPRT, ECHOPRT },
{ "echoprt", LFLG, ECHOPRT, ECHOPRT },
#endif
#ifdef ECHOKE
{ "echoke", LFLG, ECHOKE, ECHOKE },
{ "echoke", LFLG, ECHOKE, ECHOKE },
#endif
#ifdef FLUSHO
{ "flusho", LFLG, FLUSHO, FLUSHO },
{ "flusho", LFLG, FLUSHO, FLUSHO },
#endif
#ifdef PENDIN
{ "pendin", LFLG, PENDIN, PENDIN },
{ "pendin", LFLG, PENDIN, PENDIN },
#endif
{ "iexten", LFLG, IEXTEN, IEXTEN },
{ "iexten", LFLG, IEXTEN, IEXTEN },
#ifdef TOSTOP
{ "tostop", LFLG, TOSTOP, TOSTOP },
{ "tostop", LFLG, TOSTOP, TOSTOP },
#endif
{ "raw", RFLG, 0, 0 },
{ NULL, 0, 0, 0 } };
{ "raw", RFLG, 0, 0 },
{ NULL, 0, 0, 0 }
};
static void
set_this_tty(struct termios *term,struct sttyset *p,int turnon)
{
/*
pdebug(5,"set_this_tty: setting %s on? %d\n",p->name,turnon);
*/
switch ( p->which ) {
case CFLG:
term->c_cflag &= ~(p->mask);
if ( turnon )
term->c_cflag |= p->value;
break;
case IFLG:
term->c_iflag &= ~(p->mask);
if ( turnon )
term->c_iflag |= p->value;
break;
case OFLG:
term->c_oflag &= ~(p->mask);
if ( turnon )
term->c_oflag |= p->value;
break;
case LFLG:
term->c_lflag &= ~(p->mask);
if ( turnon )
term->c_lflag |= p->value;
break;
case RFLG:
term->c_iflag = 0;
term->c_oflag = 0;
term->c_lflag = 0;
term->c_cc[VMIN] = 1;
term->c_cc[VTIME] = 0;
break;
case BFLG:
cfsetispeed(term, p->value);
cfsetospeed(term, p->value);
break;
}
/*
pdebug(5,"set_this_tty: setting %s on? %d\n",p->name,turnon);
*/
switch ( p->which ) {
case CFLG:
term->c_cflag &= ~(p->mask);
if ( turnon )
term->c_cflag |= p->value;
break;
case IFLG:
term->c_iflag &= ~(p->mask);
if ( turnon )
term->c_iflag |= p->value;
break;
case OFLG:
term->c_oflag &= ~(p->mask);
if ( turnon )
term->c_oflag |= p->value;
break;
case LFLG:
term->c_lflag &= ~(p->mask);
if ( turnon )
term->c_lflag |= p->value;
break;
case RFLG:
term->c_iflag = 0;
term->c_oflag = 0;
term->c_lflag = 0;
term->c_cc[VMIN] = 1;
term->c_cc[VTIME] = 0;
break;
case BFLG:
cfsetispeed(term, p->value);
cfsetospeed(term, p->value);
break;
}
}
int
set_tty(int fd,char *settings)
{
register char *p;
register char *s;
struct termios term;
register int i;
int mode;
register char *p;
register char *s;
struct termios term;
register int i;
int mode;
/*
pdebug(4,"set_tty: fd %d settings %s\n",fd,settings);
*/
if ( tcgetattr(fd,&term) == -1 ) {
/*
pdebug(4,"set_tty: cannot get settings for fd %d, error %d\n",
fd,errno);
*/
return -1;
}
/*
pdebug(4,"set_tty: fd %d settings %s\n",fd,settings);
*/
if ( tcgetattr(fd,&term) == -1 ) {
/*
pdebug(4,"set_tty: cannot get settings for fd %d, error %d\n",
fd,errno);
*/
return -1;
}
s = strdup(settings);
p = strtok(s," \t\n");
while (p) {
mode = 1;
if ( *p == '-' ) {
mode = 0;
p++;
}
for ( i=0 ; sttynames[i].name ; i++ ) {
if ( !strcmp(p,sttynames[i].name) ) {
set_this_tty(&term,&sttynames[i],mode);
break;
}
}
p = strtok(NULL," \t\n");
}
free(s);
if ( tcsetattr(fd,TCSANOW,&term) == -1 ) {
/*
pdebug(4,"set_tty: cannot get settings for fd %d error %d\n",
fd,errno);
*/
return -1;
}
else
return 0;
s = strdup(settings);
p = strtok(s," \t\n");
while (p) {
mode = 1;
if ( *p == '-' ) {
mode = 0;
p++;
}
for ( i=0 ; sttynames[i].name ; i++ ) {
if ( !strcmp(p,sttynames[i].name) ) {
set_this_tty(&term,&sttynames[i],mode);
break;
}
}
p = strtok(NULL," \t\n");
}
free(s);
if ( tcsetattr(fd,TCSANOW,&term) == -1 ) {
/*
pdebug(4,"set_tty: cannot get settings for fd %d error %d\n",
fd,errno);
*/
return -1;
} else
return 0;
}