1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
#include <ctype.h> /* ANSI C */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <limits.h>
#include <config.h> /* For NLS */
#ifdef ENABLE_NLS
#include <libintl.h>
#include <glib.h>
#endif
#include "efaxmsg.h"
// there can be certain circumstances where PIPE_BUF is not
// defined in <limits.h>. If so, just define the minimum
// required by POSIX
#ifndef PIPE_BUF
#define PIPE_BUF 512
#endif
#define MAXTSTAMP 80 /* maximum length of a time stamp */
#if PIPE_BUF>8192
#define MAXMSGBUF 4096 /* maximum status/error message bytes held */
#else
#define MAXMSGBUF PIPE_BUF/2
#endif
#define NLOG 2
char *verb[NLOG] = { "ewin", "" } ;
char *argv0 = "" ;
int nxtoptind = 1 ; /* for communication with nextopt() */
char *nxtoptarg ;
int use_utf8 = 0 ;
int line_buffered = 0 ;
/* For systems without strerror(3) */
#ifdef NO_STRERROR
extern int sys_nerr;
extern char *sys_errlist[];
extern char *strerror( int i )
{
return ( i >= 0 && i < sys_nerr ) ? sys_errlist[i] : "Unknown Error" ;
}
#endif
/* Provide dummy gettext() function if there is no internationalisation support */
#ifndef ENABLE_NLS
static const char *gettext ( const char *text )
{
return text;
}
#endif
/* Print time stamp. */
time_t tstamp ( time_t last, FILE *f )
{
time_t now ;
char tbuf [ MAXTSTAMP ] ;
#ifdef ENABLE_NLS
gsize written = 0 ;
char *message ;
#endif
now = time ( 0 ) ;
strftime ( tbuf, MAXTSTAMP, ( now - last > 600 ) ? "%c" : "%H:%M:%S",
localtime( &now ) ) ;
#ifdef ENABLE_NLS
if ( use_utf8 ) {
if ( g_utf8_validate ( tbuf, -1, NULL ) ) {
fputs ( tbuf, f ) ;
} else {
message = g_locale_to_utf8 ( tbuf, -1, NULL, &written, NULL ) ;
if ( message ) {
fputs ( message, f ) ;
g_free ( message ) ;
}
}
}
else fputs ( tbuf, f ) ;
#else
fputs ( tbuf, f ) ;
#endif
return now ;
}
/* Return string corresponding to character c. */
char *cname ( uchar c )
{
#define CNAMEFMT "<0x%02x>"
#define CNAMELEN 6+1
static char *cnametab [ 256 ] = { /* character names */
"<NUL>","<SOH>","<STX>","<ETX>", "<EOT>","<ENQ>","<ACK>","<BEL>",
"<BS>", "<HT>", "<LF>", "<VT>", "<FF>", "<CR>", "<SO>", "<SI>",
"<DLE>","<XON>","<DC2>","<XOFF>","<DC4>","<NAK>","<SYN>","<ETB>",
"<CAN>","<EM>", "<SUB>","<ESC>", "<FS>", "<GS>", "<RS>", "<US>" } ;
static char names[ (127-32)*2 + 129*(CNAMELEN) ] ;
char *p=names ;
static int i=0 ;
if ( ! i ) {
for ( i=32 ; i<256 ; i++ ) {
cnametab [ i ] = p ;
sprintf ( p, i<127 ? "%c" : CNAMEFMT, i ) ;
p += strlen ( p ) + 1 ;
}
}
return cnametab [ c ] ;
}
/* Print a message with a variable number of printf()-type
arguments if the first character appears in the global
verb[ose] string. Other leading characters and digits do
additional actions: + allows the message to be continued on
the same line, '-' buffers the message instead of printing it,
E, and W expand into strings, S prints the error message for
the most recent system error, a digit sets the return value, a
space ends prefix but isn't printed. Returns 0 if no prefix
digit. */
enum msgflags { E=0x01, W=0x02, S=0x04, NOFLSH=0x08, NOLF=0x10 } ;
int msg ( char *fmt, ... )
{
static int init=0 ;
static FILE *logfile [ NLOG ] ;
static char msgbuf [ NLOG ] [ MAXMSGBUF ] ;
static time_t logtime [ NLOG ] = { 0, 0 } ;
static int atcol1 [ NLOG ] = { 1, 1 } ;
int err=0, i, flags=0 ;
char *p;
va_list ap ;
#ifdef ENABLE_NLS
gsize written = 0 ;
char *message ;
#endif
if ( ! init ) {
logfile[0] = stderr ;
logfile[1] = stdout ;
for ( i=0 ; i<NLOG ; i++ ) {
/* have stderr line buffered so that shared error reporting
mechanisms, such as that used by efax-gtk, do not cause
partial writes of a UTF-8 character (in the unlikely event
that the buffer for stderr maintained by this program would
otherwise fill up before being flushed) - we need UTF-8
validation checking at the read end anyway, but with line
buffering of error messages we can have more than one process
per pipe */
if ( !i && setvbuf ( logfile[i], msgbuf[i], _IOLBF, MAXMSGBUF ) )
setvbuf ( logfile[i], msgbuf[i], _IOFBF, MAXMSGBUF ) ; /* fallback */
/* stdout is busy, so unless the -n option is used, applications
monitoring stdout must be able to handle partial UTF-8
character writes from efax when the buffer flushes on filling
up (which also entails having one pipe dedicated to efax as
well as validation checking at the read end) - adopt full
buffering for stdout unless -n option used for terminal
output */
if ( i && ( !line_buffered || setvbuf ( logfile[i], msgbuf[i], _IOLBF, MAXMSGBUF ) ) )
setvbuf ( logfile[i], msgbuf[i], _IOFBF, MAXMSGBUF ) ;
}
cname ( 0 ) ;
init = 1 ;
}
for ( i=0 ; i<NLOG ; i++ ) {
va_start ( ap, fmt ) ;
for ( p=fmt ; *p ; p++ ) {
switch ( *p ) {
case ' ': p++ ; goto print ;
case 'E': flags |= E ; break ;
case 'W': flags |= W ; break ;
case 'S': flags |= S ; break ;
case '+': flags |= NOLF ; break ;
case '-': flags |= NOFLSH ; break ;
default:
if ( isdigit ( (unsigned char) *p ) ) {
err = *p - '0' ;
} else if ( ! isupper ( (unsigned char) *p ) ) {
goto print ;
}
}
}
print:
if ( strchr ( verb[i], tolower ( (unsigned char) *fmt ) ) ) {
if ( atcol1[i] ) {
fprintf ( logfile[i], "%s: ", argv0 ) ;
logtime[i] = tstamp ( logtime[i], logfile[i] ) ;
fputs ( ( flags & E ) ? gettext ( " Error: " ) :
( flags & W ) ? gettext ( " Warning: " ) :
" ",
logfile[i] ) ;
}
vfprintf( logfile[i], p, ap ) ;
if ( flags & S ) {
#ifdef ENABLE_NLS
message = strerror ( errno ) ;
if ( use_utf8 ) {
if ( g_utf8_validate ( message, -1, NULL ) ) {
fprintf ( logfile[i], " %s", message ) ;
} else {
message = g_locale_to_utf8 ( message, -1, NULL, &written, NULL ) ;
if ( message ) {
fprintf ( logfile[i], " %s", message ) ;
g_free ( message ) ;
}
}
}
else fprintf ( logfile[i], " %s", message ) ;
#else
fprintf ( logfile[i], " %s", strerror ( errno ) ) ;
#endif
}
if ( ! ( flags & NOLF ) ) fputs ( "\n", logfile[i] ) ;
atcol1[i] = flags & NOLF ? 0 : 1 ;
if ( ! ( flags & NOFLSH ) ) fflush ( logfile[i] ) ;
}
va_end ( ap ) ;
}
return err ;
}
/* Simple (one option per argument) version of getopt(3). */
int nextopt( int argc, char **argv, char *args )
{
char *a, *p ;
if ( nxtoptind >= argc || *(a = argv[nxtoptind]) != '-' ) return -1 ;
nxtoptind++ ;
if ( ! *(a+1) || ( ( p = strchr ( args, *(a+1) ) ) == 0 ) )
return msg ( "Eunknown option (%s)", a ), '?' ;
if ( *(p+1) != ':' ) nxtoptarg = 0 ;
else
if ( *(a+2) ) nxtoptarg = a+2 ;
else
if ( nxtoptind >= argc ) return msg ( "Eno argument for (%s)", a ), '?' ;
else nxtoptarg = argv [ nxtoptind++ ] ;
return *(a+1) ;
}
|