harmonize coding style

This commit is contained in:
marschap
2006-04-08 16:10:19 +00:00
parent 51dc6dcdfc
commit a1d741745a
+221 -268
View File
@@ -22,12 +22,12 @@
#undef PAUSE_AFTER_STRINGS #undef PAUSE_AFTER_STRINGS
/* Protocol values */ /* Protocol values */
unsigned char GLKCommand = 0xfe ; unsigned char GLKCommand = 0xfe;
unsigned char GLKConfirm = 0x01 ; unsigned char GLKConfirm = 0x01;
unsigned char GLKDeny = 0x08 ; unsigned char GLKDeny = 0x08;
unsigned char GLKBufferFull = 0xfe ; unsigned char GLKBufferFull = 0xfe;
unsigned char GLKBufferEmpty = 0xff ; unsigned char GLKBufferEmpty = 0xff;
/* glkopen /* glkopen
@@ -35,80 +35,76 @@ unsigned char GLKBufferEmpty = 0xff ;
* Open and configure a serial port for communication with * Open and configure a serial port for communication with
* a Matrix Orbital module (GLK or otherwise) * a Matrix Orbital module (GLK or otherwise)
*/ */
GLKDisplay * GLKDisplay *glkopen(char *name, tcflag_t speed)
glkopen(
char * name,
tcflag_t speed
)
{ {
int fd ; int fd;
struct termios new ; struct termios new;
GLKDisplay * retval ; GLKDisplay *retval;
if( name == NULL || speed == 0 ) { if (name == NULL || speed == 0) {
/* Invalid arguments */ /* Invalid arguments */
errno = EINVAL ; errno = EINVAL;
return( NULL ); return(NULL);
}; }
fd = open( name, O_RDWR | O_NOCTTY ); fd = open(name, O_RDWR | O_NOCTTY);
if( fd < 0 ) { if (fd < 0) {
return( NULL ); return(NULL);
}; }
/* Get current settings */ /* Get current settings */
if( tcgetattr( fd, &new ) < 0 ) { if (tcgetattr(fd, &new) < 0) {
int errsave = errno ; int errsave = errno;
close( fd ); close(fd);
errno = errsave ; errno = errsave;
return( NULL ); return(NULL);
}; }
retval = malloc( sizeof( GLKDisplay ) ); retval = malloc(sizeof(GLKDisplay));
if( retval == NULL ) { if (retval == NULL) {
errno = ENOMEM ; errno = ENOMEM;
return( NULL ); return(NULL);
}; }
retval->fd = fd ; retval->fd = fd;
retval->saved = new ; retval->saved = new;
retval->ungetin = retval->ungetout = 0 ; retval->ungetin = retval->ungetout = 0;
retval->timeout = GLK_TIMEOUT ; retval->timeout = GLK_TIMEOUT;
retval->flow = GLKFLOW_OK ; retval->flow = GLKFLOW_OK;
/* Make new settings */ /* Make new settings */
/* We use RAW mode */ /* We use RAW mode */
#ifdef HAVE_CFMAKERAW #ifdef HAVE_CFMAKERAW
/* The easy way */ /* The easy way */
cfmakeraw( &new ); cfmakeraw(&new);
#else #else
/* The hard way */ /* The hard way */
new.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP new.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON ); | INLCR | IGNCR | ICRNL | IXON);
new.c_oflag &= ~OPOST; new.c_oflag &= ~OPOST;
new.c_lflag &= ~( ECHO | ECHONL | ICANON | ISIG | IEXTEN ); new.c_lflag &= ~( ECHO | ECHONL | ICANON | ISIG | IEXTEN);
new.c_cflag &= ~( CSIZE | PARENB | CRTSCTS ); new.c_cflag &= ~( CSIZE | PARENB | CRTSCTS);
new.c_cflag |= CS8 | CREAD | CLOCAL ; new.c_cflag |= CS8 | CREAD | CLOCAL;
#endif #endif
/* Set default MIN and TIMEOUT values */ /* Set default MIN and TIMEOUT values */
new.c_cc[VMIN] = 0 ; new.c_cc[VMIN] = 0;
new.c_cc[VTIME] = GLK_TIMEOUT ; new.c_cc[VTIME] = GLK_TIMEOUT;
/* Set the out and in speeds */ /* Set the out and in speeds */
cfsetospeed( &new, speed ); cfsetospeed(&new, speed);
cfsetispeed( &new, B0 ); /* Input equals output speed */ cfsetispeed(&new, B0); /* Input equals output speed */
/* Configure the port */ /* Configure the port */
tcflush( fd, TCIOFLUSH ); tcflush(fd, TCIOFLUSH);
if( tcsetattr( fd, TCSANOW, &new ) < 0 ) { if (tcsetattr(fd, TCSANOW, &new) < 0) {
int errsave = errno ; int errsave = errno;
glkclose( retval ); glkclose(retval);
errno = errsave ; errno = errsave;
return( NULL ); return(NULL);
}; }
return( retval ); return(retval);
} }
/* glktimeout /* glktimeout
@@ -117,30 +113,27 @@ GLKDisplay *
* a read will block (glkget) waiting on data. * a read will block (glkget) waiting on data.
*/ */
int int
glktimeout( glktimeout(GLKDisplay *fd, int timeout)
GLKDisplay * fd,
int timeout
)
{ {
struct termios t ; struct termios t;
if( timeout < 0 || timeout > 255 ) { if (timeout < 0 || timeout > 255) {
errno = EINVAL ; /* Invalid argument */ errno = EINVAL; /* Invalid argument */
return( 1 ); /* Failure */ return(1); /* Failure */
}; }
if( tcgetattr( fd->fd, &t ) < 0 ) { if (tcgetattr(fd->fd, &t) < 0) {
return( 1 ); return(1);
}; }
fd->timeout = timeout ; fd->timeout = timeout;
t.c_cc[VTIME] = timeout ; t.c_cc[VTIME] = timeout;
if( tcsetattr( fd->fd, TCSANOW, &t ) < 0 ) { if (tcsetattr(fd->fd, TCSANOW, &t) < 0) {
return( 1 ); return(1);
}; }
return( 0 ); /* Success */ return(0); /* Success */
} }
/* glkflow /* glkflow
@@ -151,51 +144,47 @@ int
*/ */
#define GLKBUF (96) #define GLKBUF (96)
int int
glkflow( glkflow(GLKDisplay *fd, int full, int empty)
GLKDisplay * fd,
int full,
int empty
)
{ {
struct termios t ; struct termios t;
if( full > GLKBUF -1 if ((full > GLKBUF -1)
|| empty > GLKBUF -1 || (empty > GLKBUF -1)
|| full + empty > GLKBUF -1 || (full + empty > GLKBUF -1)) {
) { errno = EINVAL;
errno = EINVAL ; return(1);
return( 1 ); }
};
if( tcgetattr( fd->fd, &t ) < 0 ) { if (tcgetattr(fd->fd, &t) < 0) {
return( 1 ); /* Failure */ return(1); /* Failure */
}; }
if( full < 0 || empty < 0 ) { if (full < 0 || empty < 0) {
/* Turn it off */ /* Turn it off */
glkputl( fd, GLKCommand, 0x3b, EOF ); glkputl(fd, GLKCommand, 0x3b, EOF);
t.c_iflag &= ~(IXON | IXANY | IXOFF) ; t.c_iflag &= ~(IXON | IXANY | IXOFF);
t.c_cc[VSTART] = GLKBufferEmpty ; t.c_cc[VSTART] = GLKBufferEmpty;
t.c_cc[VSTOP] = GLKBufferFull ; t.c_cc[VSTOP] = GLKBufferFull;
fd->flow = GLKFLOW_DISABLE ; fd->flow = GLKFLOW_DISABLE;
} else { }
else {
/* Turn it on */ /* Turn it on */
glkputl( fd, GLKCommand, 0x3a, full, empty, EOF ); glkputl(fd, GLKCommand, 0x3a, full, empty, EOF);
/* Control what we send */ /* Control what we send */
t.c_iflag |= IXON ; t.c_iflag |= IXON;
/* Only start on VSTART (IXANY), * /* Only start on VSTART (IXANY), *
* Don't control what we receive (IXOFF) */ * Don't control what we receive (IXOFF) */
t.c_iflag &= ~(IXANY | IXOFF) ; t.c_iflag &= ~(IXANY | IXOFF);
t.c_cc[VSTART] = GLKBufferEmpty ; t.c_cc[VSTART] = GLKBufferEmpty;
t.c_cc[VSTOP] = GLKBufferFull ; t.c_cc[VSTOP] = GLKBufferFull;
fd->flow = GLKFLOW_OK ; fd->flow = GLKFLOW_OK;
}; }
if( tcsetattr( fd->fd, TCSANOW, &t ) < 0 ) { if (tcsetattr(fd->fd, TCSANOW, &t) < 0) {
return( 1 ); return(1);
}; }
return( 0 ); /* Success */ return(0); /* Success */
} }
/* glkclose /* glkclose
@@ -203,29 +192,27 @@ int
* Close a serial port and restore settings if provided. * Close a serial port and restore settings if provided.
*/ */
int int
glkclose( glkclose(GLKDisplay *fd)
GLKDisplay * fd
)
{ {
int retval = 0 ; int retval = 0;
if( fd->fd < 0 ) { if (fd->fd < 0) {
/* Probably already closed */ /* Probably already closed */
return( 0 ); /* Success? */ return(0); /* Success? */
}; }
/* Trash any unread data */ /* Trash any unread data */
tcflush( fd->fd, TCIFLUSH ); tcflush(fd->fd, TCIFLUSH);
/* Restore settings */ /* Restore settings */
tcsetattr( fd->fd, TCSANOW, &fd->saved ); tcsetattr(fd->fd, TCSANOW, &fd->saved);
retval = close( fd->fd ); retval = close(fd->fd);
fd->fd = -1 ; fd->fd = -1;
free( fd ); free(fd);
return( retval ); return(retval);
} }
@@ -234,22 +221,19 @@ int
* Send a character to the GLK * Send a character to the GLK
*/ */
int INLINE int INLINE
glkput( glkput(GLKDisplay *fd, int c)
GLKDisplay * fd,
int c
)
{ {
unsigned char val ; unsigned char val;
ssize_t retval ; ssize_t retval;
/* Output the byte */ /* Output the byte */
val = c ; val = c;
retval = write( fd->fd, &val, 1 ); retval = write(fd->fd, &val, 1);
/* retval <= 0 => FAILURE => 1 /* retval <= 0 => FAILURE => 1
* retval == 1 => SUCCESS => 0 * retval == 1 => SUCCESS => 0
*/ */
return( retval <= 0 ); return(retval <= 0);
} }
/* glkunget /* glkunget
@@ -262,20 +246,17 @@ int INLINE
* flow control signals * flow control signals
*/ */
int int
glkunget( glkunget(GLKDisplay *fd, int c)
GLKDisplay * fd,
int c
)
{ {
/* Is buffer already full? */ /* Is buffer already full? */
if( ((fd->ungetin + 1) & (~UNGETBUFSIZE)) == fd->ungetout ) { if (((fd->ungetin + 1) & (~UNGETBUFSIZE)) == fd->ungetout) {
return( 1 ); /* Failure */ return(1); /* Failure */
}; }
fd->ungetbuf[fd->ungetin] = c ; fd->ungetbuf[fd->ungetin] = c;
fd->ungetin = (fd->ungetin + 1) & (~UNGETBUFSIZE) ; fd->ungetin = (fd->ungetin + 1) & (~UNGETBUFSIZE);
return( 0 ); return(0);
} }
/* glkget /* glkget
@@ -283,19 +264,17 @@ int
* Read a character from the GLK * Read a character from the GLK
*/ */
int INLINE int INLINE
glkget( glkget(GLKDisplay *fd)
GLKDisplay * fd
)
{ {
unsigned char c ; unsigned char c;
ssize_t retval ; ssize_t retval;
retval = read( fd->fd, &c, 1 ); retval = read(fd->fd, &c, 1);
if( retval <= 0 ) { if (retval <= 0) {
return( -1 ); return(-1);
} else { } else {
return( (int) c ); return((int) c);
}; }
} }
/* glkpoll /* glkpoll
@@ -304,24 +283,21 @@ int INLINE
* GLK. * GLK.
*/ */
int int
glkpoll( glkpoll(GLKDisplay *fd, int timeout)
GLKDisplay * fd,
int timeout
)
{ {
struct pollfd fds ; struct pollfd fds;
int retval ; int retval;
fds.fd = fd->fd ; fds.fd = fd->fd;
fds.events = POLLIN ; fds.events = POLLIN;
fds.revents = 0 ; fds.revents = 0;
retval = poll( &fds, 1, timeout ); retval = poll(&fds, 1, timeout);
if( retval < 0 ) { if (retval < 0) {
/* Some error. Ignore it, and return "no data" */ /* Some error. Ignore it, and return "no data" */
retval = 0 ; retval = 0;
}; }
return( retval ); return(retval);
} }
/* glkgetc /* glkgetc
@@ -332,41 +308,38 @@ int
* GLKBufferFull and GLKBufferEmpty whereas glkgetc wil NOT. * GLKBufferFull and GLKBufferEmpty whereas glkgetc wil NOT.
*/ */
int int
glkgetc( glkgetc(GLKDisplay *fd)
GLKDisplay * fd
)
{ {
int c ; int c;
/* Are there characters in the unget buffer? */ /* Are there characters in the unget buffer? */
if( fd->ungetin == fd->ungetout ) { if (fd->ungetin == fd->ungetout) {
/* Nope, so read one */ /* Nope, so read one */
for( ; ; ) { /* loop until we get a non-flow control value */ for ( ; ; ) { /* loop until we get a non-flow control value */
c = glkget( fd ); c = glkget(fd);
if( fd->flow != GLKFLOW_DISABLE ) { if (fd->flow != GLKFLOW_DISABLE) {
if( c == GLKBufferFull ) { if (c == GLKBufferFull) {
fd->flow = GLKFLOW_STOPPED ; fd->flow = GLKFLOW_STOPPED;
continue ; continue;
} else if( c == GLKBufferEmpty ) { } else if (c == GLKBufferEmpty) {
fd->flow = GLKFLOW_OK ; fd->flow = GLKFLOW_OK;
continue ; continue;
}; }
}; }
/* Must be what we're looking for */ /* Must be what we're looking for */
break ; break;
}; }
}
} else { else {
/* Get one of the "ungotten" char's */ /* Get one of the "ungotten" char's */
c = fd->ungetbuf[fd->ungetout] ; c = fd->ungetbuf[fd->ungetout];
fd->ungetout = (fd->ungetout + 1) & (~UNGETBUFSIZE) ; fd->ungetout = (fd->ungetout + 1) & (~UNGETBUFSIZE);
}; }
return( c ); return(c);
} }
/* glkput_confirm /* glkput_confirm
@@ -375,30 +348,27 @@ int
* confirmation of echo * confirmation of echo
*/ */
int int
glkput_confirm( glkput_confirm(GLKDisplay *fd, int c)
GLKDisplay * fd,
int c
)
{ {
int echo ; int echo;
/* Output the byte */ /* Output the byte */
if( glkput( fd, c ) ) { if (glkput(fd, c)) {
return( 1 ); /* Failure */ return(1); /* Failure */
} }
/* Look for the echo */ /* Look for the echo */
echo = glkget( fd ); echo = glkget(fd);
if( echo < 0 ) { if (echo < 0) {
return( 1 ); /* Failure */ return(1); /* Failure */
}; }
if( echo == c ) { if (echo == c) {
glkput( fd, GLKConfirm ); glkput(fd, GLKConfirm);
return( 0 ); /* Success */ return(0); /* Success */
} else { } else {
glkput( fd, GLKDeny ); glkput(fd, GLKDeny);
return( 1 ); return(1);
} }
} }
@@ -408,21 +378,17 @@ int
* confirmation. * confirmation.
*/ */
int int
glkputa_confirm( glkputa_confirm(GLKDisplay *fd, int len, unsigned char *str)
GLKDisplay * fd,
int len,
unsigned char * str
)
{ {
int i ; int i;
int retval ; int retval;
retval = 0 ; /* Assume Success */ retval = 0; /* Assume Success */
for( i = len ; !retval && i ; ++str, --i ) { for (i = len; !retval && i; ++str, --i) {
retval = glkput_confirm( fd, *str ); retval = glkput_confirm(fd, *str);
}; }
return( retval ); return(retval);
} }
/* glkput_echo /* glkput_echo
@@ -430,28 +396,25 @@ int
* Send a character to the GLK on fd with echo expected * Send a character to the GLK on fd with echo expected
*/ */
int int
glkput_echo( glkput_echo(GLKDisplay *fd, int c)
GLKDisplay * fd,
int c
)
{ {
int echo ; int echo;
/* Output the byte */ /* Output the byte */
if( glkput( fd, c ) ) { if (glkput(fd, c)) {
return( 1 ); /* Failure */ return(1); /* Failure */
} }
/* Look for the echo */ /* Look for the echo */
echo = glkget( fd ); echo = glkget(fd);
if( echo < 0 ) { if (echo < 0) {
return( 1 ); /* Failure */ return(1); /* Failure */
}; }
if( echo == c ) { if (echo == c) {
return( 0 ); /* Success */ return(0); /* Success */
} else { } else {
return( 1 ); /* Failure */ return(1); /* Failure */
} }
} }
@@ -461,25 +424,22 @@ int
* terminated by an EOF valued argument. * terminated by an EOF valued argument.
*/ */
int int
glkputl( glkputl(GLKDisplay *fd, ...)
GLKDisplay * fd,
...
)
{ {
va_list ap ; va_list ap;
int value ; int value;
int retval ; int retval;
va_start( ap, fd ); va_start(ap, fd);
retval = 0 ; /* Success */ retval = 0; /* Success */
value = va_arg( ap, int ); value = va_arg(ap, int);
while( !retval && value != EOF ) { while (!retval && value != EOF) {
retval = glkput( fd, value ); retval = glkput(fd, value);
value = va_arg( ap, int ); value = va_arg(ap, int);
}; }
return( retval ); return(retval);
} }
/* glkputs /* glkputs
@@ -488,20 +448,17 @@ int
* GLK. * GLK.
*/ */
int int
glkputs( glkputs(GLKDisplay *fd, char *str)
GLKDisplay * fd,
char * str
)
{ {
register char * p = str ; register char *p = str;
int retval ; int retval;
retval = 0 ; /* Success */ retval = 0; /* Success */
for( p = str ; !retval && *p ; ++p ) { for (p = str; !retval && *p; ++p) {
retval = glkput( fd, *p ); retval = glkput(fd, *p);
}; }
return( retval ); return(retval);
} }
/* glkputa /* glkputa
@@ -509,20 +466,16 @@ int
* Send an array of characters. * Send an array of characters.
*/ */
int int
glkputa( glkputa(GLKDisplay *fd, int len, unsigned char *str)
GLKDisplay * fd,
int len,
unsigned char * str
)
{ {
register unsigned char * p = str ; register unsigned char *p = str;
int i ; int i;
int retval ; int retval;
retval = 0 ; /* Assume Success */ retval = 0; /* Assume Success */
for( i = len ; !retval && i ; ++p, --i ) { for (i = len; !retval && i; ++p, --i) {
retval = glkput( fd, *p ); retval = glkput(fd, *p);
}; }
return( retval ); return(retval);
} }