mirror of
https://github.com/GnoConsortium/gno.git
synced 2024-06-13 22:29:26 +00:00
Makefile:
eliminate oldlog.c from the build. old_syslog() was only used by syslog2.asm, and inspection showed it to be likely buggy anyway. It was missing a field in the passed data structure, and using it would cause any long-running daemon to run out of memory if didn't crash first. For now I'm leaving oldlog.c where it is rather than doing a 'cvs rm'. syslog.c: Major changes... - The facility/priority has gone back to being of type int vice long; the original change was a red herring. - The current syslog implementation will now talk to Phil's original syslogd. - The busy wait in sendPort now forces a context switch. syslog2.asm: - I commented the sources while figuring out what Phil's syslogd was doing. I may was well check in those comments even though this file is not currently used. - fixed the save name and the 'mcopy' directive vis.c, unvis.c: - initial checkin
This commit is contained in:
parent
edee25c942
commit
273780523c
|
@ -1,7 +1,7 @@
|
||||||
#
|
#
|
||||||
# Makefile for libc/gen.
|
# Makefile for libc/gen.
|
||||||
#
|
#
|
||||||
# $Id: Makefile,v 1.11 1998/06/24 04:19:57 gdr-ftp Exp $
|
# $Id: Makefile,v 1.12 1999/01/04 05:10:36 gdr-ftp Exp $
|
||||||
#
|
#
|
||||||
|
|
||||||
LIB = c
|
LIB = c
|
||||||
|
@ -11,8 +11,8 @@ SRC_ASM = setjmp.asm
|
||||||
SRC_C = basename.c bmem.c compat.c dirent.c err.c fnmatch.c fts.c \
|
SRC_C = basename.c bmem.c compat.c dirent.c err.c fnmatch.c fts.c \
|
||||||
getcwd.c getgrent.c getlogin.c getpass.c getpwent.c \
|
getcwd.c getgrent.c getlogin.c getpass.c getpwent.c \
|
||||||
getttyent.c hostname.c \
|
getttyent.c hostname.c \
|
||||||
oldlog.c popen.c psignal.c pwcache.c scandir.c siglist.c sleep.c \
|
popen.c psignal.c pwcache.c scandir.c siglist.c sleep.c \
|
||||||
syslog.c tty.c uname.c utime.c
|
syslog.c tty.c uname.c unvis.c utime.c vis.c
|
||||||
|
|
||||||
SRCS = $(SRC_ASM) $(SRC_C)
|
SRCS = $(SRC_ASM) $(SRC_C)
|
||||||
|
|
||||||
|
|
|
@ -31,11 +31,16 @@
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef __ORCAC__
|
||||||
|
segment "libc_gen__";
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(LIBC_SCCS) && !defined(lint)
|
#if defined(LIBC_SCCS) && !defined(lint)
|
||||||
static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94";
|
static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94";
|
||||||
#endif /* LIBC_SCCS and not lint */
|
#endif /* LIBC_SCCS and not lint */
|
||||||
|
|
||||||
#define USE_PORTS /* use ports mech rather than TCP/IP */
|
#define USE_PORTS /* use ports mech rather than TCP/IP */
|
||||||
|
#define USE_VZERO /* use syslogd v0 protocol a la GNO 2.0.4 */
|
||||||
#define __SYSLOG_INTERNALS /* needed for the ports interface */
|
#define __SYSLOG_INTERNALS /* needed for the ports interface */
|
||||||
|
|
||||||
#ifndef USE_PORTS
|
#ifndef USE_PORTS
|
||||||
|
@ -59,7 +64,10 @@ static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94";
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#ifdef __GNO__
|
#ifdef __GNO__
|
||||||
#include <gno/gno.h> /* __prognameGS() */
|
#include <types.h>
|
||||||
|
#include <gno/gno.h>
|
||||||
|
#include <misctool.h>
|
||||||
|
#include <memory.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if __STDC__
|
#if __STDC__
|
||||||
|
@ -87,7 +95,7 @@ extern char *__progname; /* Program name, from crt0. */
|
||||||
#ifdef USE_PORTS
|
#ifdef USE_PORTS
|
||||||
#include <sys/ports.h>
|
#include <sys/ports.h>
|
||||||
static int openPort(void);
|
static int openPort(void);
|
||||||
static int sendPort(int port, const void *buf, int len, int insertTime);
|
static int sendPort(int port, int pri, const void *buf, int len);
|
||||||
static void closePort(int port);
|
static void closePort(int port);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -134,11 +142,13 @@ static ssize_t writehook(
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void
|
void
|
||||||
vsyslog(long pri, const char *fmt, va_list ap)
|
vsyslog(int pri, const char *fmt, va_list ap)
|
||||||
{
|
{
|
||||||
register int cnt;
|
register int cnt;
|
||||||
register char ch, *p, *t;
|
register char ch, *p, *t;
|
||||||
|
#ifndef USE_VZERO
|
||||||
time_t now;
|
time_t now;
|
||||||
|
#endif
|
||||||
int fd, saved_errno;
|
int fd, saved_errno;
|
||||||
char *stdp;
|
char *stdp;
|
||||||
STATIC char tbuf[2048], fmt_cpy[1024];
|
STATIC char tbuf[2048], fmt_cpy[1024];
|
||||||
|
@ -172,9 +182,16 @@ vsyslog(long pri, const char *fmt, va_list ap)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Build the message. */
|
/* Build the message. */
|
||||||
|
#ifndef USE_VZERO
|
||||||
|
/*
|
||||||
|
* If we're using Phil's syslogd, we don't need to prepend the
|
||||||
|
* <facpri> or date stamp. The facpri is provided separately and
|
||||||
|
* the date stamp is determined by the daemon.
|
||||||
|
*/
|
||||||
(void)time(&now);
|
(void)time(&now);
|
||||||
(void)fprintf(fp, "<%ld>", pri);
|
(void)fprintf(fp, "<%d>", pri);
|
||||||
(void)fprintf(fp, "%.15s ", ctime(&now) + 4);
|
(void)fprintf(fp, "%.15s ", ctime(&now) + 4);
|
||||||
|
#endif
|
||||||
if (LogStat & LOG_PERROR) {
|
if (LogStat & LOG_PERROR) {
|
||||||
/* Transfer to string buffer */
|
/* Transfer to string buffer */
|
||||||
(void)fflush(fp);
|
(void)fflush(fp);
|
||||||
|
@ -255,7 +272,7 @@ vsyslog(long pri, const char *fmt, va_list ap)
|
||||||
if (!connected)
|
if (!connected)
|
||||||
openlog(LogTag, LogStat | LOG_NDELAY, 0);
|
openlog(LogTag, LogStat | LOG_NDELAY, 0);
|
||||||
#ifdef USE_PORTS
|
#ifdef USE_PORTS
|
||||||
if (sendPort(LogFile, tbuf, cnt, 0) >= 0)
|
if (sendPort(LogFile, pri, tbuf, cnt) >= 0)
|
||||||
return;
|
return;
|
||||||
#else
|
#else
|
||||||
if (send(LogFile, tbuf, cnt, 0) >= 0)
|
if (send(LogFile, tbuf, cnt, 0) >= 0)
|
||||||
|
@ -342,10 +359,9 @@ vsyslog(long pri, const char *fmt, va_list ap)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
vsyslogmt(long pri, const char *fmt, va_list ap)
|
vsyslogmt(int pri, const char *fmt, va_list ap)
|
||||||
{
|
{
|
||||||
#define _SYSLOG_BUFFERLEN_MT 128
|
char mt_buffer[_SYSLOG_BUFFERLEN_MT]; /* defined in <sys/syslog.h> */
|
||||||
char mt_buffer[_SYSLOG_BUFFERLEN_MT];
|
|
||||||
char *mt_bufptr;
|
char *mt_bufptr;
|
||||||
int mt_bytesLeft;
|
int mt_bytesLeft;
|
||||||
int i;
|
int i;
|
||||||
|
@ -353,7 +369,9 @@ vsyslogmt(long pri, const char *fmt, va_list ap)
|
||||||
int local_LogFile;
|
int local_LogFile;
|
||||||
char *p;
|
char *p;
|
||||||
char *stdp;
|
char *stdp;
|
||||||
|
#ifndef USE_VZERO
|
||||||
time_t now;
|
time_t now;
|
||||||
|
#endif
|
||||||
int fd, saved_errno;
|
int fd, saved_errno;
|
||||||
#define MT_BYTES_USED (_SYSLOG_BUFFERLEN_MT - mt_bytesLeft)
|
#define MT_BYTES_USED (_SYSLOG_BUFFERLEN_MT - mt_bytesLeft)
|
||||||
|
|
||||||
|
@ -377,8 +395,14 @@ vsyslogmt(long pri, const char *fmt, va_list ap)
|
||||||
/* Build the message. */
|
/* Build the message. */
|
||||||
mt_bufptr = mt_buffer;
|
mt_bufptr = mt_buffer;
|
||||||
mt_bytesLeft = _SYSLOG_BUFFERLEN_MT; /* not off-by-one */
|
mt_bytesLeft = _SYSLOG_BUFFERLEN_MT; /* not off-by-one */
|
||||||
|
#ifndef USE_VZERO
|
||||||
|
/*
|
||||||
|
* If we're using Phil's syslogd, we don't need to prepend the
|
||||||
|
* <facpri> or date stamp. The facpri is provided separately and
|
||||||
|
* the date stamp is determined by the daemon.
|
||||||
|
*/
|
||||||
time(&now);
|
time(&now);
|
||||||
p = sprintmt(mt_bufptr, mt_bytesLeft, "<%ld>", pri);
|
p = sprintmt(mt_bufptr, mt_bytesLeft, "<%d>", pri);
|
||||||
mt_bytesLeft -= (p - mt_bufptr);
|
mt_bytesLeft -= (p - mt_bufptr);
|
||||||
mt_bufptr = p;
|
mt_bufptr = p;
|
||||||
#if 0
|
#if 0
|
||||||
|
@ -386,6 +410,7 @@ vsyslogmt(long pri, const char *fmt, va_list ap)
|
||||||
STRNCPY2BUF(ctime(&now)+4, ((mt_bytesLeft < 15) ? mt_bytesLeft : 15));
|
STRNCPY2BUF(ctime(&now)+4, ((mt_bytesLeft < 15) ? mt_bytesLeft : 15));
|
||||||
STRCPY2BUF(" ");
|
STRCPY2BUF(" ");
|
||||||
#endif
|
#endif
|
||||||
|
#endif /* USE_VZERO */
|
||||||
|
|
||||||
/* mark the beginning of the string for stderr, if necessary */
|
/* mark the beginning of the string for stderr, if necessary */
|
||||||
if (LogStat & LOG_PERROR) {
|
if (LogStat & LOG_PERROR) {
|
||||||
|
@ -463,7 +488,7 @@ vsyslogmt(long pri, const char *fmt, va_list ap)
|
||||||
if (local_LogFile == -1) { /* an openlog wasn't done? */
|
if (local_LogFile == -1) { /* an openlog wasn't done? */
|
||||||
local_LogFile = openPort();
|
local_LogFile = openPort();
|
||||||
}
|
}
|
||||||
if (sendPort(local_LogFile, mt_buffer, MT_BYTES_USED, 1) >= 0) {
|
if (sendPort(local_LogFile, pri, mt_buffer, MT_BYTES_USED) >= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#else /* not USE_PORTS -- no output if someone forgot to openlog() */
|
#else /* not USE_PORTS -- no output if someone forgot to openlog() */
|
||||||
|
@ -519,7 +544,7 @@ static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void
|
void
|
||||||
openlog(const char *ident, int logstat, long logfac)
|
openlog(const char *ident, int logstat, int logfac)
|
||||||
{
|
{
|
||||||
if (ident != NULL)
|
if (ident != NULL)
|
||||||
LogTag = ident;
|
LogTag = ident;
|
||||||
|
@ -570,8 +595,8 @@ closelog(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setlogmask -- set the log mask level */
|
/* setlogmask -- set the log mask level */
|
||||||
long
|
int
|
||||||
setlogmask(long pmask)
|
setlogmask(int pmask)
|
||||||
{
|
{
|
||||||
int omask;
|
int omask;
|
||||||
|
|
||||||
|
@ -589,76 +614,109 @@ openPort(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
sendPort(int port, const void *buf, int len, int insertTime) {
|
sendPort(int port, int pri, const void *buf, int len)
|
||||||
SyslogDataBuffer_t data;
|
{
|
||||||
long answer;
|
Handle datahand;
|
||||||
|
Word memID;
|
||||||
|
char *buffer, *strptr;
|
||||||
|
int saved_errno, saved_toolerr;
|
||||||
|
SyslogDataBuffer_t *header;
|
||||||
|
int *lenptr, *offptr, *magicptr, length;
|
||||||
|
|
||||||
if (port == -1) {
|
if (port == -1) {
|
||||||
/* not a valid port; silent error */
|
/* not a valid port; silent error */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
data.sdb_magic = _SYSLOG_MAGIC;
|
|
||||||
data.sdb_version = _SYSLOG_STRUCT_VERSION;
|
|
||||||
data.sdb_buflen = len+1;
|
|
||||||
data.sdb_msglen = len;
|
|
||||||
data.sdb_busywait = 1;
|
|
||||||
data.sdb_needtime = insertTime;
|
|
||||||
data.sdb_buffer = buf;
|
|
||||||
|
|
||||||
/* send the data */
|
|
||||||
psend (port, (long) &data);
|
|
||||||
|
|
||||||
#if 1
|
/*
|
||||||
/* Wait for a reply. We use a busy-wait here so that we can avoid
|
* We don't have to worry about this process exiting before the
|
||||||
* the following methods and their associated problems:
|
* syslogd has a chance to change the ownership of the allocated
|
||||||
|
* memory because we do a busy-wait before returning. See the end
|
||||||
|
* of this routine.
|
||||||
*
|
*
|
||||||
* procreceive: using it would preclude user code from using it
|
* In order to avoid the busy wait, we would have to allocate a new
|
||||||
* since we might throw away their messages and
|
* user id for every message and ensure that the temporary user id
|
||||||
* vice versa.
|
* was deleted after use. That is probably more expensive than just
|
||||||
* signals: We don't have a special signal for this purpose,
|
* doing the busy-wait.
|
||||||
* and it would be bad to either overload an
|
|
||||||
* already assigned signal, or to use up either
|
|
||||||
* SIGUSR1 or SIGUSR2.
|
|
||||||
* ports: if the user fails to set up a port queue in
|
|
||||||
* the parent process, then we have no way to get
|
|
||||||
* the message out. If they *do* set it up, then
|
|
||||||
* we could get *our* messages going to our
|
|
||||||
* siblings.
|
|
||||||
* ptys We can't use these without a parent/child
|
|
||||||
* relationship between syslogd and every process
|
|
||||||
* that calls syslog(3). Perhaps this is why
|
|
||||||
* Phil Vandry had initd/syslogd as part of the
|
|
||||||
* same executable, originally?
|
|
||||||
*
|
*
|
||||||
* As it turns out, a busy wait isn't _too_ bad because syslogd will
|
* We could probably increase concurrency, though, if in syslogd we
|
||||||
* release our busy wait as soon as it has copied our buffer. In
|
* queued up all incoming buffers after changing their ownership,
|
||||||
* addition, syslogd has a port queue that is only one slot deep,
|
* then writing out the buffers only after no more processes are
|
||||||
* so if another process got to syslogd before us, we'll be blocked
|
* attempting to send messages to syslogd. With such a mechanism
|
||||||
* on our send, anyway.
|
* in place, one could also optimize out a bunch of extraneous open(2)
|
||||||
*
|
* and close(2) calls on the log files.
|
||||||
* We could optimize this busy wait by somehow forcing the kernel
|
|
||||||
* to schedule us out. We don't want to use sleep(3), because its
|
|
||||||
* implementation relies on signals.
|
|
||||||
*/
|
*/
|
||||||
while (data.sdb_busywait);
|
memID = _ownerid;
|
||||||
|
|
||||||
#else /* 0 */
|
/*
|
||||||
/* wait for a reply */
|
* Allocate the memory. For v0 messages, We need:
|
||||||
while((answer = procreceive()) != _SYSLOG_MAGIC) {
|
* sizeof(SyslogDataBuffer_t) for version, prio, and numstrings
|
||||||
/* try to write a message to the console */
|
* sizeof(int) * 1 for the offset value
|
||||||
int fd;
|
* sizeof(int) * 1 for the "sizeof struct" value
|
||||||
char *s;
|
* sizeof(int) * 1 for the length word of the first str
|
||||||
#define BAD_MAGIC ": bad magic from syslogd\r"
|
* len for the characters in buf
|
||||||
if ((fd = open(_PATH_CONSOLE, O_WRONLY)) >= 0) {
|
* 1 for a terminating NULL byte at the end
|
||||||
s = __progname;
|
* of buf -- expected by Phil's syslogd
|
||||||
write(fd, s, strlen(s));
|
*/
|
||||||
write(fd, BAD_MAGIC, sizeof(BAD_MAGIC)-1);
|
datahand = NewHandle(sizeof(SyslogDataBuffer_t) + (3 * sizeof(int))
|
||||||
close(fd);
|
+ len +1, memID, 0x4000, NULL);
|
||||||
}
|
if (_toolErr) {
|
||||||
#undef BAD_MAGIC
|
saved_toolerr = _toolErr;
|
||||||
|
DeleteID(memID);
|
||||||
|
_toolErr = saved_toolerr;
|
||||||
|
errno = _mapErr(_toolErr);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
#endif /* 0 */
|
|
||||||
return len;
|
/*
|
||||||
|
* Initialize the data structure and copy the data. The handle
|
||||||
|
* is already locked.
|
||||||
|
*/
|
||||||
|
header = (SyslogDataBuffer_t *) *datahand;
|
||||||
|
header->sdb0_version = 0;
|
||||||
|
header->sdb0_prio = pri;
|
||||||
|
header->sdb0_numstrings = 1;
|
||||||
|
|
||||||
|
offptr = (int *) (((char *) header) + sizeof(SyslogDataBuffer_t));
|
||||||
|
magicptr = (int *) (((char *) offptr) + sizeof(int));
|
||||||
|
lenptr = (int *) (((char *) magicptr) + sizeof(int));
|
||||||
|
strptr = (((char *) lenptr) + sizeof(int));
|
||||||
|
*offptr = 0;
|
||||||
|
*magicptr = sizeof(SyslogDataBuffer_t) + 2 * sizeof(int);
|
||||||
|
*lenptr = len;
|
||||||
|
memcpy(strptr, buf, len);
|
||||||
|
strptr[len] = '\0'; /* Phil's syslogd expects a NULL byte */
|
||||||
|
|
||||||
|
HUnlock(datahand);
|
||||||
|
|
||||||
|
if (psend(port, (long) datahand) == -1) {
|
||||||
|
/* We failed. Dump the region to avoid a mem leak. */
|
||||||
|
saved_errno = errno;
|
||||||
|
saved_toolerr = _toolErr;
|
||||||
|
DisposeHandle(datahand);
|
||||||
|
_toolErr = saved_toolerr;
|
||||||
|
errno = saved_errno;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Busy wait until we no longer own the memory block. Use the
|
||||||
|
* COP 0x7F instruction to force a context switch and thus minimize
|
||||||
|
* wasted clock cycles. We don't want to do a sleep(2) here because
|
||||||
|
* we don't futz with the user's world of signals. This is cleaner
|
||||||
|
* anyway.
|
||||||
|
*
|
||||||
|
* We reuse (overload) 'magicptr' here so we don't have to allocate
|
||||||
|
* more stack space just for this test.
|
||||||
|
*/
|
||||||
|
magicptr = (int *) ((char *) datahand + 6); /* pointer to owner */
|
||||||
|
while (*magicptr == memID) {
|
||||||
|
asm {
|
||||||
|
cop 127
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -675,7 +733,7 @@ closePort(int port) {
|
||||||
|
|
||||||
void
|
void
|
||||||
#if __STDC__
|
#if __STDC__
|
||||||
syslog(long pri, const char *fmt, ...)
|
syslog(int pri, const char *fmt, ...)
|
||||||
#else
|
#else
|
||||||
syslog(pri, fmt, va_alist)
|
syslog(pri, fmt, va_alist)
|
||||||
int pri;
|
int pri;
|
||||||
|
@ -696,7 +754,7 @@ syslog(pri, fmt, va_alist)
|
||||||
|
|
||||||
#ifdef __GNO__
|
#ifdef __GNO__
|
||||||
void
|
void
|
||||||
syslogmt(long pri, const char *fmt, ...)
|
syslogmt(int pri, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
|
|
|
@ -12,13 +12,13 @@
|
||||||
*
|
*
|
||||||
* Phillip Vandry, August 1993
|
* Phillip Vandry, August 1993
|
||||||
*
|
*
|
||||||
* $Id: syslog2.asm,v 1.2 1998/06/24 04:19:59 gdr-ftp Exp $
|
* $Id: syslog2.asm,v 1.3 1999/01/04 05:10:36 gdr-ftp Exp $
|
||||||
*
|
*
|
||||||
* This file is formatted for tab stops at positions 10, 16, 41, 40, 57,
|
* This file is formatted for tab stops at positions 10, 16, 41, 40, 57,
|
||||||
* 65, and 73 (standard Orca/M systabs setting).
|
* 65, and 73 (standard Orca/M systabs setting).
|
||||||
*
|
*
|
||||||
keep syslog
|
keep syslog2
|
||||||
mcopy syslog.mac
|
mcopy syslog2.mac
|
||||||
case on
|
case on
|
||||||
|
|
||||||
dummy start ; ends up in .root
|
dummy start ; ends up in .root
|
||||||
|
@ -103,8 +103,8 @@ error equ 23
|
||||||
sta >memid
|
sta >memid
|
||||||
|
|
||||||
* Log only if bit clear in LogMask
|
* Log only if bit clear in LogMask
|
||||||
lda >errno
|
lda >errno ; save value of errno in lerrno for future
|
||||||
sta lerrno
|
sta lerrno ; reference
|
||||||
lda prio
|
lda prio
|
||||||
and #7
|
and #7
|
||||||
tax
|
tax
|
||||||
|
@ -115,7 +115,8 @@ lsrloop lsr a
|
||||||
* carry = apropriate bit
|
* carry = apropriate bit
|
||||||
bcc dolog
|
bcc dolog
|
||||||
|
|
||||||
* get rif of parameters by running through sprintf
|
* LogMask was such that we're not logging the message; get rid of parameters
|
||||||
|
* before returning by running through sprintf
|
||||||
jsr mksendhand
|
jsr mksendhand
|
||||||
pei valist+2
|
pei valist+2
|
||||||
pei valist
|
pei valist
|
||||||
|
@ -127,6 +128,8 @@ lsrloop lsr a
|
||||||
~DisposeHandle <sendhand
|
~DisposeHandle <sendhand
|
||||||
brl return
|
brl return
|
||||||
|
|
||||||
|
* We will be logging. Use the given prio if nonzero, else use the default
|
||||||
|
* facility specified in the openlog() call.
|
||||||
dolog anop
|
dolog anop
|
||||||
lda prio
|
lda prio
|
||||||
and #$3f8
|
and #$3f8
|
||||||
|
@ -135,20 +138,21 @@ dolog anop
|
||||||
ora >LogFacility
|
ora >LogFacility
|
||||||
sta prio
|
sta prio
|
||||||
gotone anop
|
gotone anop
|
||||||
jsr mksendhand
|
jsr mksendhand ; allocate mem, init sendhand and sendptr
|
||||||
jsr cpsendhand
|
jsr cpsendhand ; init version, prio, numstrings, and offset
|
||||||
|
; in mem block. Init locals cumlen and sendptr
|
||||||
|
|
||||||
lda >LogTag
|
lda >LogTag ; if the tag has not been initialized, skip
|
||||||
sta cptr
|
sta cptr ; to the 'notag' label
|
||||||
lda >LogTag+2
|
lda >LogTag+2
|
||||||
sta cptr+2
|
sta cptr+2
|
||||||
ora cptr
|
ora cptr
|
||||||
beq notag
|
beq notag
|
||||||
|
|
||||||
lda >TagLen
|
lda >TagLen ; set y reg to length of LogTag string, store
|
||||||
bne already
|
bne already ; the result in TagLen. If we've already
|
||||||
ldy #0
|
ldy #0 ; determined it, use the cached value.
|
||||||
short m
|
short m ; Leave the TagLen value in the accumulator
|
||||||
lppp lda [cptr],y
|
lppp lda [cptr],y
|
||||||
beq foundlen
|
beq foundlen
|
||||||
iny
|
iny
|
||||||
|
@ -157,15 +161,15 @@ foundlen long m
|
||||||
tya
|
tya
|
||||||
sta >TagLen
|
sta >TagLen
|
||||||
|
|
||||||
already sta [cumlen]
|
already sta [cumlen] ; store TagLen into length word of GS string
|
||||||
tay
|
tay
|
||||||
short m
|
short m
|
||||||
fincp2 dey
|
fincp2 dey ; copy the LogTag into the GS string
|
||||||
bmi fincp
|
bmi fincp
|
||||||
lda [cptr],y
|
lda [cptr],y
|
||||||
sta [sendptr],y
|
sta [sendptr],y
|
||||||
bra fincp2
|
bra fincp2
|
||||||
fincp long m
|
fincp long m ; make sendptr point to end of copied tag
|
||||||
lda [cumlen]
|
lda [cumlen]
|
||||||
clc
|
clc
|
||||||
adc sendptr
|
adc sendptr
|
||||||
|
@ -182,10 +186,10 @@ notag anop
|
||||||
pha
|
pha
|
||||||
ldx #$0903
|
ldx #$0903
|
||||||
jsl $e10008 ; getpid
|
jsl $e10008 ; getpid
|
||||||
pea fmt|-16
|
pea fmt|-16 ; if the LOG_PID flag is set, append a text
|
||||||
pea fmt
|
pea fmt ; representation of the pid to the GS string.
|
||||||
pei sendptr+2
|
pei sendptr+2 ; update the length word and make sendptr
|
||||||
pei sendptr
|
pei sendptr ; point to the end of the string.
|
||||||
jsl sprintf
|
jsl sprintf
|
||||||
pha
|
pha
|
||||||
clc
|
clc
|
||||||
|
@ -199,10 +203,10 @@ notag anop
|
||||||
adc #0
|
adc #0
|
||||||
sta sendptr+2
|
sta sendptr+2
|
||||||
|
|
||||||
nopid anop
|
nopid anop ; if we had a log tag, append a " :" to
|
||||||
lda >LogTag
|
lda >LogTag ; the GS string. Update the length word
|
||||||
ora >LogTag+2
|
ora >LogTag+2 ; and make sendptr point to the end of the
|
||||||
beq notagsecond
|
beq notagsecond ; string
|
||||||
lda [cumlen]
|
lda [cumlen]
|
||||||
inc a
|
inc a
|
||||||
inc a
|
inc a
|
||||||
|
@ -217,10 +221,10 @@ nopid anop
|
||||||
adc #0
|
adc #0
|
||||||
sta sendptr+2
|
sta sendptr+2
|
||||||
|
|
||||||
notagsecond anop
|
notagsecond anop ; If errno was nonzero, make error point
|
||||||
lda lerrno
|
lda lerrno ; to the appropriate error string, otherwise
|
||||||
bne isone
|
bne isone ; make it a pointer to the empty string ("\0").
|
||||||
lda #ptrtozero
|
lda #ptrtozero ; Set errlen to the length of the error string.
|
||||||
sta error
|
sta error
|
||||||
lda #^ptrtozero
|
lda #^ptrtozero
|
||||||
sta error+2
|
sta error+2
|
||||||
|
@ -234,7 +238,10 @@ isone pha
|
||||||
jsl strlen
|
jsl strlen
|
||||||
sta errlen
|
sta errlen
|
||||||
|
|
||||||
none ldx #2 ; = bytes needed in copy (one null+slop)
|
; determine what the size of the format
|
||||||
|
; string will be after expanding all "%m"
|
||||||
|
; specifiers
|
||||||
|
none ldx #2 ; = bytes needed in copy (one null+slop)
|
||||||
ldy #0
|
ldy #0
|
||||||
runthrough lda [format],y
|
runthrough lda [format],y
|
||||||
cmp #$6d25 ; '%m'
|
cmp #$6d25 ; '%m'
|
||||||
|
@ -254,7 +261,7 @@ account iny
|
||||||
endstring pha
|
endstring pha
|
||||||
pha
|
pha
|
||||||
pea 0
|
pea 0
|
||||||
phx ; length
|
phx ; length
|
||||||
stz chand
|
stz chand
|
||||||
stz chand+2
|
stz chand+2
|
||||||
lda format
|
lda format
|
||||||
|
@ -266,8 +273,8 @@ endstring pha
|
||||||
pea $4000
|
pea $4000
|
||||||
pha
|
pha
|
||||||
pha
|
pha
|
||||||
~NewHandle *,*,*,*
|
~NewHandle *,*,*,* ; allocate memory for copied format string,
|
||||||
pla
|
pla ; handle is chand, dereferenced handle is cptr
|
||||||
plx
|
plx
|
||||||
bcs impossible
|
bcs impossible
|
||||||
sta chand
|
sta chand
|
||||||
|
@ -278,6 +285,8 @@ endstring pha
|
||||||
lda [chand]
|
lda [chand]
|
||||||
sta cptr
|
sta cptr
|
||||||
|
|
||||||
|
; copy format string to cptr, substituting
|
||||||
|
; %m specifiers along the way
|
||||||
pea 0 ; offset in source
|
pea 0 ; offset in source
|
||||||
ldy #0 ; offset in dest
|
ldy #0 ; offset in dest
|
||||||
realtime tyx
|
realtime tyx
|
||||||
|
@ -312,14 +321,18 @@ anotherreal tyx
|
||||||
donerr txy
|
donerr txy
|
||||||
bra realtime
|
bra realtime
|
||||||
excited txy
|
excited txy
|
||||||
sta [cptr],y ; zero
|
sta [cptr],y ; null terminate cptr
|
||||||
|
|
||||||
impossible anop ; jump here if malloc() failed
|
impossible anop ; jump here if mem alloc for copying format
|
||||||
|
; string failed, in which case cptr points to
|
||||||
|
; the original format string.
|
||||||
|
; We also fall through to here if we have
|
||||||
|
; successfully copied the format string.
|
||||||
pei valist+2
|
pei valist+2
|
||||||
pei valist
|
pei valist
|
||||||
pei cptr+2
|
pei cptr+2 ; use vsprintf to print format and any args
|
||||||
pei cptr
|
pei cptr ; to the GS string. Update the length word
|
||||||
pei sendptr+2
|
pei sendptr+2 ; of the GS string.
|
||||||
pei sendptr
|
pei sendptr
|
||||||
jsl vsprintf
|
jsl vsprintf
|
||||||
clc
|
clc
|
||||||
|
@ -329,38 +342,38 @@ impossible anop ; jump here if malloc() failed
|
||||||
and #$20 ; PERROR
|
and #$20 ; PERROR
|
||||||
beq noper
|
beq noper
|
||||||
|
|
||||||
ldx cumlen
|
ldx cumlen ; Write the message to stderr if the necessary
|
||||||
ldy cumlen+2
|
ldy cumlen+2 ; bit was set.
|
||||||
lda #3
|
lda #3
|
||||||
jsl WriteGString ; echo on standard error
|
jsl WriteGString
|
||||||
ldx #nlonly
|
ldx #nlonly
|
||||||
ldy #^nlonly
|
ldy #^nlonly
|
||||||
lda #3
|
lda #3
|
||||||
jsl WriteGString
|
jsl WriteGString
|
||||||
|
|
||||||
noper lda chand
|
noper lda chand ; Release the mem from the copied format
|
||||||
ora chand+2
|
ora chand+2 ; string, if it was successfully allocated
|
||||||
beq nochand
|
beq nochand
|
||||||
~DisposeHandle <chand
|
~DisposeHandle <chand
|
||||||
|
|
||||||
nochand pei sendhand+2
|
nochand pei sendhand+2 ; see if syslogd is running
|
||||||
pei sendhand
|
pei sendhand
|
||||||
pea portname|-16
|
pea portname|-16
|
||||||
pea portname
|
pea portname
|
||||||
jsl pgetport
|
jsl pgetport
|
||||||
cmp #$ffff
|
cmp #$ffff ; if not, use the old_syslog routine to print
|
||||||
beq nosyslogd
|
beq nosyslogd ; the message
|
||||||
pha
|
pha
|
||||||
jsl psend
|
jsl psend ; send the message to syslogd
|
||||||
ldy #6
|
ldy #6
|
||||||
tryagain lda [sendhand],y
|
tryagain lda [sendhand],y
|
||||||
cmp >memid
|
cmp >memid ; busy wait until we no longer own the
|
||||||
bne return
|
bne return ; memory block. Use COP to force a context
|
||||||
cop $7f
|
cop $7f ; switch to minimize wasted clock cycles
|
||||||
bra tryagain
|
bra tryagain
|
||||||
|
|
||||||
return plb
|
return plb
|
||||||
lda argstart-3
|
lda argstart-3 ; va_end()
|
||||||
sta valist+1
|
sta valist+1
|
||||||
lda argstart-2
|
lda argstart-2
|
||||||
sta valist+2
|
sta valist+2
|
||||||
|
@ -373,6 +386,9 @@ return plb
|
||||||
nosyslogd jsl old_syslog
|
nosyslogd jsl old_syslog
|
||||||
bra return
|
bra return
|
||||||
|
|
||||||
|
* Allocate a memory region of 1024 bytes. Create it locked. Store the
|
||||||
|
* handle into 'sendhand'. Deref the handle and store the result into 'sendptr'
|
||||||
|
|
||||||
mksendhand pha
|
mksendhand pha
|
||||||
pha
|
pha
|
||||||
pea 0
|
pea 0
|
||||||
|
@ -387,7 +403,7 @@ mksendhand pha
|
||||||
stx sendhand
|
stx sendhand
|
||||||
plx
|
plx
|
||||||
stx sendhand+2
|
stx sendhand+2
|
||||||
bcs giveup
|
bcs giveup ; if alloc fails, sleep 1 second and try again
|
||||||
ldy #2
|
ldy #2
|
||||||
lda [sendhand],y
|
lda [sendhand],y
|
||||||
sta sendptr+2
|
sta sendptr+2
|
||||||
|
@ -398,6 +414,13 @@ giveup pea 1
|
||||||
jsl sleep
|
jsl sleep
|
||||||
bra mksendhand
|
bra mksendhand
|
||||||
|
|
||||||
|
* - Copy the five words of Xsyslog into the region pointed to by sendptr.
|
||||||
|
* This initializes the version, prio, numstrings, string1 offset, and
|
||||||
|
* sizeof(region) to 0, 0, 1, 0, and 10, respectively.
|
||||||
|
* - Reset the prio field to the appropriate value
|
||||||
|
* - Make cumlen point to the length word of the GS string
|
||||||
|
* - Make sendptr point to the start of the text field of the GS string.
|
||||||
|
|
||||||
cpsendhand ldy #(Xthis-Xsyslog-2) ; is even
|
cpsendhand ldy #(Xthis-Xsyslog-2) ; is even
|
||||||
phb
|
phb
|
||||||
phk
|
phk
|
||||||
|
@ -416,15 +439,15 @@ still lda Xsyslog,y
|
||||||
adc #(Xthis-Xsyslog)
|
adc #(Xthis-Xsyslog)
|
||||||
sta cumlen
|
sta cumlen
|
||||||
lda sendptr+2
|
lda sendptr+2
|
||||||
adc #0
|
adc #0 ; cumlen now holds a pointer to the length
|
||||||
sta cumlen+2
|
sta cumlen+2 ; word of the first GS string.
|
||||||
lda #0
|
lda #0
|
||||||
sta [cumlen]
|
sta [cumlen] ; zero the length word
|
||||||
lda cumlen
|
lda cumlen
|
||||||
clc
|
clc
|
||||||
adc #2
|
adc #2
|
||||||
sta sendptr
|
sta sendptr ; sendptr now points to the text field of
|
||||||
lda cumlen+2
|
lda cumlen+2 ; the GS string.
|
||||||
adc #0
|
adc #0
|
||||||
sta sendptr+2
|
sta sendptr+2
|
||||||
rts
|
rts
|
||||||
|
|
248
lib/libc/gen/unvis.c
Normal file
248
lib/libc/gen/unvis.c
Normal file
|
@ -0,0 +1,248 @@
|
||||||
|
/*-
|
||||||
|
* Copyright (c) 1989, 1993
|
||||||
|
* The Regents of the University of California. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. All advertising materials mentioning features or use of this software
|
||||||
|
* must display the following acknowledgement:
|
||||||
|
* This product includes software developed by the University of
|
||||||
|
* California, Berkeley and its contributors.
|
||||||
|
* 4. Neither the name of the University nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __ORCAC__
|
||||||
|
#pragma optimize -1
|
||||||
|
segment "libc_gen__";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(LIBC_SCCS) && !defined(lint)
|
||||||
|
static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93";
|
||||||
|
#endif /* LIBC_SCCS and not lint */
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <vis.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* decode driven by state machine
|
||||||
|
*/
|
||||||
|
#define S_GROUND 0 /* haven't seen escape char */
|
||||||
|
#define S_START 1 /* start decoding special sequence */
|
||||||
|
#define S_META 2 /* metachar started (M) */
|
||||||
|
#define S_META1 3 /* metachar more, regular char (-) */
|
||||||
|
#define S_CTRL 4 /* control char started (^) */
|
||||||
|
#define S_OCTAL2 5 /* octal digit 2 */
|
||||||
|
#define S_OCTAL3 6 /* octal digit 3 */
|
||||||
|
|
||||||
|
#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
|
||||||
|
|
||||||
|
/*
|
||||||
|
* unvis - decode characters previously encoded by vis
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
unvis(char *cp, int c, int *astate, int flag)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (flag & UNVIS_END) {
|
||||||
|
if (*astate == S_OCTAL2 || *astate == S_OCTAL3) {
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
}
|
||||||
|
return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (*astate) {
|
||||||
|
|
||||||
|
case S_GROUND:
|
||||||
|
*cp = 0;
|
||||||
|
if (c == '\\') {
|
||||||
|
*astate = S_START;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
*cp = c;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
|
||||||
|
case S_START:
|
||||||
|
switch(c) {
|
||||||
|
case '\\':
|
||||||
|
*cp = c;
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
case '0': case '1': case '2': case '3':
|
||||||
|
case '4': case '5': case '6': case '7':
|
||||||
|
*cp = (c - '0');
|
||||||
|
*astate = S_OCTAL2;
|
||||||
|
return (0);
|
||||||
|
case 'M':
|
||||||
|
*cp = 0200;
|
||||||
|
*astate = S_META;
|
||||||
|
return (0);
|
||||||
|
case '^':
|
||||||
|
*astate = S_CTRL;
|
||||||
|
return (0);
|
||||||
|
case 'n':
|
||||||
|
*cp = '\n';
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
case 'r':
|
||||||
|
*cp = '\r';
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
case 'b':
|
||||||
|
*cp = '\b';
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
case 'a':
|
||||||
|
*cp = '\007';
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
case 'v':
|
||||||
|
*cp = '\v';
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
case 't':
|
||||||
|
*cp = '\t';
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
case 'f':
|
||||||
|
*cp = '\f';
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
case 's':
|
||||||
|
*cp = ' ';
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
case 'E':
|
||||||
|
*cp = '\033';
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
case '\n':
|
||||||
|
/*
|
||||||
|
* hidden newline
|
||||||
|
*/
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_NOCHAR);
|
||||||
|
case '$':
|
||||||
|
/*
|
||||||
|
* hidden marker
|
||||||
|
*/
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_NOCHAR);
|
||||||
|
}
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_SYNBAD);
|
||||||
|
|
||||||
|
case S_META:
|
||||||
|
if (c == '-')
|
||||||
|
*astate = S_META1;
|
||||||
|
else if (c == '^')
|
||||||
|
*astate = S_CTRL;
|
||||||
|
else {
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_SYNBAD);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
|
||||||
|
case S_META1:
|
||||||
|
*astate = S_GROUND;
|
||||||
|
*cp |= c;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
|
||||||
|
case S_CTRL:
|
||||||
|
if (c == '?')
|
||||||
|
*cp |= 0177;
|
||||||
|
else
|
||||||
|
*cp |= c & 037;
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
|
||||||
|
case S_OCTAL2: /* second possible octal digit */
|
||||||
|
if (isoctal(c)) {
|
||||||
|
/*
|
||||||
|
* yes - and maybe a third
|
||||||
|
*/
|
||||||
|
*cp = (*cp << 3) + (c - '0');
|
||||||
|
*astate = S_OCTAL3;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* no - done with current sequence, push back passed char
|
||||||
|
*/
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_VALIDPUSH);
|
||||||
|
|
||||||
|
case S_OCTAL3: /* third possible octal digit */
|
||||||
|
*astate = S_GROUND;
|
||||||
|
if (isoctal(c)) {
|
||||||
|
*cp = (*cp << 3) + (c - '0');
|
||||||
|
return (UNVIS_VALID);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* we were done, push back passed char
|
||||||
|
*/
|
||||||
|
return (UNVIS_VALIDPUSH);
|
||||||
|
|
||||||
|
default:
|
||||||
|
/*
|
||||||
|
* decoder in unknown state - (probably uninitialized)
|
||||||
|
*/
|
||||||
|
*astate = S_GROUND;
|
||||||
|
return (UNVIS_SYNBAD);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* strunvis - decode src into dst
|
||||||
|
*
|
||||||
|
* Number of chars decoded into dst is returned, -1 on error.
|
||||||
|
* Dst is null terminated.
|
||||||
|
*/
|
||||||
|
|
||||||
|
int
|
||||||
|
strunvis(register char *dst, register const char *src)
|
||||||
|
{
|
||||||
|
register char c;
|
||||||
|
char *start = dst;
|
||||||
|
int state = 0;
|
||||||
|
|
||||||
|
while ( (c = *src++) ) {
|
||||||
|
again:
|
||||||
|
switch (unvis(dst, c, &state, 0)) {
|
||||||
|
case UNVIS_VALID:
|
||||||
|
dst++;
|
||||||
|
break;
|
||||||
|
case UNVIS_VALIDPUSH:
|
||||||
|
dst++;
|
||||||
|
goto again;
|
||||||
|
case 0:
|
||||||
|
case UNVIS_NOCHAR:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
|
||||||
|
dst++;
|
||||||
|
*dst = '\0';
|
||||||
|
return (dst - start);
|
||||||
|
}
|
183
lib/libc/gen/vis.c
Normal file
183
lib/libc/gen/vis.c
Normal file
|
@ -0,0 +1,183 @@
|
||||||
|
/*-
|
||||||
|
* Copyright (c) 1989, 1993
|
||||||
|
* The Regents of the University of California. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. All advertising materials mentioning features or use of this software
|
||||||
|
* must display the following acknowledgement:
|
||||||
|
* This product includes software developed by the University of
|
||||||
|
* California, Berkeley and its contributors.
|
||||||
|
* 4. Neither the name of the University nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __ORCAC__
|
||||||
|
#pragma optimize -1
|
||||||
|
segment "libc_gen__";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(LIBC_SCCS) && !defined(lint)
|
||||||
|
static char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 7/19/93";
|
||||||
|
#endif /* LIBC_SCCS and not lint */
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <vis.h>
|
||||||
|
|
||||||
|
#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
|
||||||
|
|
||||||
|
/*
|
||||||
|
* vis - visually encode characters
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
vis(register char *dst, int c, register int flag, int nextc)
|
||||||
|
{
|
||||||
|
c = (unsigned char)c;
|
||||||
|
if (isgraph(c) ||
|
||||||
|
((flag & VIS_SP) == 0 && c == ' ') ||
|
||||||
|
((flag & VIS_TAB) == 0 && c == '\t') ||
|
||||||
|
((flag & VIS_NL) == 0 && c == '\n') ||
|
||||||
|
((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) {
|
||||||
|
*dst++ = c;
|
||||||
|
if (c == '\\' && (flag & VIS_NOSLASH) == 0)
|
||||||
|
*dst++ = '\\';
|
||||||
|
*dst = '\0';
|
||||||
|
return (dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flag & VIS_CSTYLE) {
|
||||||
|
switch(c) {
|
||||||
|
case '\n':
|
||||||
|
*dst++ = '\\';
|
||||||
|
*dst++ = 'n';
|
||||||
|
goto done;
|
||||||
|
case '\r':
|
||||||
|
*dst++ = '\\';
|
||||||
|
*dst++ = 'r';
|
||||||
|
goto done;
|
||||||
|
case '\b':
|
||||||
|
*dst++ = '\\';
|
||||||
|
*dst++ = 'b';
|
||||||
|
goto done;
|
||||||
|
#if __STDC__
|
||||||
|
case '\a':
|
||||||
|
#else
|
||||||
|
case '\007':
|
||||||
|
#endif
|
||||||
|
*dst++ = '\\';
|
||||||
|
*dst++ = 'a';
|
||||||
|
goto done;
|
||||||
|
case '\v':
|
||||||
|
*dst++ = '\\';
|
||||||
|
*dst++ = 'v';
|
||||||
|
goto done;
|
||||||
|
case '\t':
|
||||||
|
*dst++ = '\\';
|
||||||
|
*dst++ = 't';
|
||||||
|
goto done;
|
||||||
|
case '\f':
|
||||||
|
*dst++ = '\\';
|
||||||
|
*dst++ = 'f';
|
||||||
|
goto done;
|
||||||
|
case ' ':
|
||||||
|
*dst++ = '\\';
|
||||||
|
*dst++ = 's';
|
||||||
|
goto done;
|
||||||
|
case '\0':
|
||||||
|
*dst++ = '\\';
|
||||||
|
*dst++ = '0';
|
||||||
|
if (isoctal(nextc)) {
|
||||||
|
*dst++ = '0';
|
||||||
|
*dst++ = '0';
|
||||||
|
}
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
|
||||||
|
*dst++ = '\\';
|
||||||
|
*dst++ = ((u_char)c >> 6 & 07) + '0';
|
||||||
|
*dst++ = ((u_char)c >> 3 & 07) + '0';
|
||||||
|
*dst++ = ((u_char)c & 07) + '0';
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
if ((flag & VIS_NOSLASH) == 0)
|
||||||
|
*dst++ = '\\';
|
||||||
|
if (c & 0200) {
|
||||||
|
c &= 0177;
|
||||||
|
*dst++ = 'M';
|
||||||
|
}
|
||||||
|
if (iscntrl(c)) {
|
||||||
|
*dst++ = '^';
|
||||||
|
if (c == 0177)
|
||||||
|
*dst++ = '?';
|
||||||
|
else
|
||||||
|
*dst++ = c + '@';
|
||||||
|
} else {
|
||||||
|
*dst++ = '-';
|
||||||
|
*dst++ = c;
|
||||||
|
}
|
||||||
|
done:
|
||||||
|
*dst = '\0';
|
||||||
|
return (dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* strvis, strvisx - visually encode characters from src into dst
|
||||||
|
*
|
||||||
|
* Dst must be 4 times the size of src to account for possible
|
||||||
|
* expansion. The length of dst, not including the trailing NULL,
|
||||||
|
* is returned.
|
||||||
|
*
|
||||||
|
* Strvisx encodes exactly len bytes from src into dst.
|
||||||
|
* This is useful for encoding a block of data.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
strvis(register char *dst, register const char *src, int flag)
|
||||||
|
{
|
||||||
|
register char c;
|
||||||
|
char *start;
|
||||||
|
|
||||||
|
for (start = dst; (c = *src); )
|
||||||
|
dst = vis(dst, c, flag, *++src);
|
||||||
|
*dst = '\0';
|
||||||
|
return (dst - start);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
strvisx(register char *dst, register const char *src,
|
||||||
|
register size_t len, int flag)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
char *start;
|
||||||
|
|
||||||
|
for (start = dst; len > 1; len--) {
|
||||||
|
c = *src;
|
||||||
|
dst = vis(dst, c, flag, *++src);
|
||||||
|
}
|
||||||
|
if (len)
|
||||||
|
dst = vis(dst, *src, flag, '\0');
|
||||||
|
*dst = '\0';
|
||||||
|
|
||||||
|
return (dst - start);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user