1
0
mirror of https://github.com/UzixLS/picocom.git synced 2025-07-19 07:21:18 +03:00

Moved "generic" fd I/O functions from picocom.c to fdio.[ch]

This commit is contained in:
Nick Patavalis
2015-08-18 22:07:56 +03:00
parent 7b1c5f92e3
commit 7d7edf657f
4 changed files with 219 additions and 122 deletions

View File

@ -37,12 +37,13 @@ linenoise-1.0/linenoise.o : linenoise-1.0/linenoise.c linenoise-1.0/linenoise.h
#CPPFLAGS += -DNO_HELP
picocom : picocom.o term.o split.o
picocom : picocom.o term.o fdio.o split.o
# $(LD) $(LDFLAGS) -o $@ $+ $(LDLIBS)
picocom.o : picocom.c term.h
term.o : term.c term.h
split.o : split.c split.h
fdio.o : fdio.c fdio.h
doc : picocom.8 picocom.8.html picocom.8.ps
@ -60,7 +61,7 @@ picocom.8.ps : picocom.8
groff -mandoc -Tps $< > $@
clean:
rm -f picocom.o term.o split.o linenoise-1.0/linenoise.o
rm -f picocom.o term.o fdio.o split.o linenoise-1.0/linenoise.o
rm -f *~
rm -f \#*\#

167
fdio.c Normal file
View File

@ -0,0 +1,167 @@
/* vi: set sw=4 ts=4:
*
* fdio.c
*
* Functions for doing I/O on file descriptors.
*
* by Nick Patavalis (npat@efault.net)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
/**********************************************************************/
ssize_t
writen_ni(int fd, const void *buff, size_t n)
{
size_t nl;
ssize_t nw;
const char *p;
p = buff;
nl = n;
while (nl > 0) {
do {
nw = write(fd, p, nl);
} while ( nw < 0 && errno == EINTR );
if ( nw <= 0 ) break;
nl -= nw;
p += nw;
}
return n - nl;
}
int
fd_printf (int fd, const char *format, ...)
{
char buf[256];
va_list args;
int len;
va_start(args, format);
len = vsnprintf(buf, sizeof(buf), format, args);
buf[sizeof(buf) - 1] = '\0';
va_end(args);
return writen_ni(fd, buf, len);
}
/**********************************************************************/
#ifndef LINENOISE
static int
cput(int fd, char c)
{
return write(fd, &c, 1);
}
static int
cdel (int fd)
{
const char del[] = "\b \b";
return write(fd, del, sizeof(del) - 1);
}
static int
xput (int fd, unsigned char c)
{
const char hex[] = "0123456789abcdef";
char b[4];
b[0] = '\\'; b[1] = 'x'; b[2] = hex[c >> 4]; b[3] = hex[c & 0x0f];
return write(fd, b, sizeof(b) - 1);
}
static int
xdel (int fd)
{
const char del[] = "\b\b\b\b \b\b\b\b";
return write(fd, del, sizeof(del) - 1);
}
int
fd_readline (int fdi, int fdo, char *b, int bsz)
{
int r;
unsigned char c;
unsigned char *bp, *bpe;
bp = (unsigned char *)b;
bpe = (unsigned char *)b + bsz - 1;
while (1) {
r = read(fdi, &c, 1);
if ( r <= 0 ) { r = -1; goto out; }
switch (c) {
case '\b':
case '\x7f':
if ( bp > (unsigned char *)b ) {
bp--;
if ( isprint(*bp) )
cdel(fdo);
else
xdel(fdo);
} else {
cput(fdo, '\x07');
}
break;
case '\x03': /* CTRL-c */
r = -1;
errno = EINTR;
goto out;
case '\r':
*bp = '\0';
r = bp - (unsigned char *)b;
goto out;
default:
if ( bp < bpe ) {
*bp++ = c;
if ( isprint(c) )
cput(fdo, c);
else
xput(fdo, c);
} else {
cput(fdo, '\x07');
}
break;
}
}
out:
return r;
}
#endif /* of LINENOISE */
/**********************************************************************/
/*
* Local Variables:
* mode:c
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/

48
fdio.h Normal file
View File

@ -0,0 +1,48 @@
/* vi: set sw=4 ts=4:
*
* fdio.h
*
* Functions for doing I/O on file descriptors.
*
* by Nick Patavalis (npat@efault.net)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#ifndef FDIO_H
ssize_t writen_ni(int fd, const void *buff, size_t n);
int fd_printf (int fd, const char *format, ...);
#ifndef LINENOISE
int fd_readline (int fdi, int fdo, char *b, int bsz);
#endif
#endif /* of FDIO_H */
/**********************************************************************/
/*
* Local Variables:
* mode:c
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/

121
picocom.c
View File

@ -27,7 +27,6 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <stdarg.h>
@ -49,6 +48,7 @@
#define _GNU_SOURCE
#include <getopt.h>
#include "fdio.h"
#include "split.h"
#include "term.h"
#ifdef LINENOISE
@ -313,42 +313,6 @@ uucp_unlock(void)
/**********************************************************************/
ssize_t
writen_ni(int fd, const void *buff, size_t n)
{
size_t nl;
ssize_t nw;
const char *p;
p = buff;
nl = n;
while (nl > 0) {
do {
nw = write(fd, p, nl);
} while ( nw < 0 && errno == EINTR );
if ( nw <= 0 ) break;
nl -= nw;
p += nw;
}
return n - nl;
}
int
fd_printf (int fd, const char *format, ...)
{
char buf[256];
va_list args;
int len;
va_start(args, format);
len = vsnprintf(buf, sizeof(buf), format, args);
buf[sizeof(buf) - 1] = '\0';
va_end(args);
return writen_ni(fd, buf, len);
}
void
fatal (const char *format, ...)
{
@ -384,89 +348,6 @@ fatal (const char *format, ...)
#ifndef LINENOISE
int
cput(int fd, char c)
{
return write(fd, &c, 1);
}
int
cdel (int fd)
{
const char del[] = "\b \b";
return write(fd, del, sizeof(del) - 1);
}
int
xput (int fd, unsigned char c)
{
const char hex[] = "0123456789abcdef";
char b[4];
b[0] = '\\'; b[1] = 'x'; b[2] = hex[c >> 4]; b[3] = hex[c & 0x0f];
return write(fd, b, sizeof(b) - 1);
}
int
xdel (int fd)
{
const char del[] = "\b\b\b\b \b\b\b\b";
return write(fd, del, sizeof(del) - 1);
}
int
fd_readline (int fdi, int fdo, char *b, int bsz)
{
int r;
unsigned char c;
unsigned char *bp, *bpe;
bp = (unsigned char *)b;
bpe = (unsigned char *)b + bsz - 1;
while (1) {
r = read(fdi, &c, 1);
if ( r <= 0 ) { r = -1; goto out; }
switch (c) {
case '\b':
case '\x7f':
if ( bp > (unsigned char *)b ) {
bp--;
if ( isprint(*bp) )
cdel(fdo);
else
xdel(fdo);
} else {
cput(fdo, '\x07');
}
break;
case '\x03': /* CTRL-c */
r = -1;
errno = EINTR;
goto out;
case '\r':
*bp = '\0';
r = bp - (unsigned char *)b;
goto out;
default:
if ( bp < bpe ) {
*bp++ = c;
if ( isprint(c) )
cput(fdo, c);
else
xput(fdo, c);
} else {
cput(fdo, '\x07');
}
break;
}
}
out:
return r;
}
char *
read_filename (void)
{