#include "uucp.h"
#if USE_RCS_ID
const char uulog_rcsid[] = "$Id: uulog.c,v 1.29 2002/03/05 19:10:42 ian Rel $";
#endif
#include <ctype.h>
#include <errno.h>
#include "getopt.h"
#include "uudefs.h"
#include "uuconf.h"
#include "system.h"
static void ulusage P((void));
static void ulhelp P((void));
static const struct option asLlongopts[] =
{
{ "debuglog", no_argument, NULL, 'D' },
{ "follow", optional_argument, NULL, 2 },
{ "lines", required_argument, NULL, 'n' },
{ "system", required_argument, NULL, 's' },
{ "statslog", no_argument, NULL, 'S' },
{ "user", required_argument, NULL, 'u' },
{ "uuxqtlog", no_argument, NULL, 'x' },
{ "config", required_argument, NULL, 'I' },
{ "debug", required_argument, NULL, 'X' },
{ "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 1 },
{ NULL, 0, NULL, 0 }
};
int
main (argc, argv)
int argc;
char **argv;
{
boolean fdebug = FALSE;
boolean fforever = FALSE;
int cshow = 0;
const char *zsystem = NULL;
boolean fstats = FALSE;
const char *zuser = NULL;
const char *zconfig = NULL;
boolean fuuxqt = FALSE;
int i;
int iopt;
pointer puuconf;
int iuuconf;
const char *zlogfile;
const char *zstatsfile;
const char *zdebugfile;
const char *zfile;
FILE *e;
char **pzshow = NULL;
int ishow = 0;
size_t csystem = 0;
size_t cuser = 0;
char *zline;
size_t cline;
if (argc < 1)
{
zProgram = "uulog";
ulusage ();
}
zProgram = argv[0];
for (i = 0; i < argc; i++)
{
if (argv[i][0] == '-' && isdigit (argv[i][1]))
{
size_t clen;
char *znew;
clen = strlen (argv[i]);
znew = zbufalc (clen + 2);
znew[0] = '-';
znew[1] = 'n';
memcpy (znew + 2, argv[i] + 1, clen);
argv[i] = znew;
}
}
while ((iopt = getopt_long (argc, argv, "Df:FI:n:s:Su:vxX:", asLlongopts,
(int *) NULL)) != EOF)
{
switch (iopt)
{
case 'D':
fdebug = TRUE;
break;
case 'f':
fforever = TRUE;
zsystem = optarg;
if (cshow == 0)
cshow = 10;
break;
case 'F':
fforever = TRUE;
if (cshow == 0)
cshow = 10;
break;
case 'I':
if (fsysdep_other_config (optarg))
zconfig = optarg;
break;
case 'n':
cshow = (int) strtol (optarg, (char **) NULL, 10);
break;
case 's':
zsystem = optarg;
break;
case 'S':
fstats = TRUE;
break;
case 'u':
zuser = optarg;
break;
case 'x':
fuuxqt = TRUE;
break;
case 'X':
#if DEBUG > 1
iDebug |= idebug_parse (optarg);
#endif
break;
case 'v':
printf ("uulog (Taylor UUCP) %s\n", VERSION);
printf ("Copyright (C) 1991, 92, 93, 94, 1995, 2002 Ian Lance Taylor\n");
printf ("This program is free software; you may redistribute it under the terms of\n");
printf ("the GNU General Public LIcense. This program has ABSOLUTELY NO WARRANTY.\n");
exit (EXIT_SUCCESS);
case 2:
fforever = TRUE;
if (cshow == 0)
cshow = 10;
if (optarg != NULL)
zsystem = optarg;
break;
case 1:
ulhelp ();
exit (EXIT_SUCCESS);
case 0:
break;
default:
ulusage ();
}
}
if (optind != argc || (fstats && fdebug))
ulusage ();
iuuconf = uuconf_init (&puuconf, (const char *) NULL, zconfig);
if (iuuconf != UUCONF_SUCCESS)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
#if DEBUG > 1
{
const char *zdebug;
iuuconf = uuconf_debuglevel (puuconf, &zdebug);
if (iuuconf != UUCONF_SUCCESS)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
if (zdebug != NULL)
iDebug |= idebug_parse (zdebug);
}
#endif
iuuconf = uuconf_logfile (puuconf, &zlogfile);
if (iuuconf != UUCONF_SUCCESS)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
iuuconf = uuconf_statsfile (puuconf, &zstatsfile);
if (iuuconf != UUCONF_SUCCESS)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
iuuconf = uuconf_debugfile (puuconf, &zdebugfile);
if (iuuconf != UUCONF_SUCCESS)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
usysdep_initialize (puuconf, INIT_NOCHDIR);
if (zsystem != NULL)
{
#if HAVE_HDB_LOGGING
if (strcmp (zsystem, "ANY") != 0)
#endif
{
struct uuconf_system ssys;
iuuconf = uuconf_system_info (puuconf, zsystem, &ssys);
if (iuuconf == UUCONF_SUCCESS)
{
zsystem = zbufcpy (ssys.uuconf_zname);
(void) uuconf_system_free (puuconf, &ssys);
}
}
}
if (fstats)
zfile = zstatsfile;
else if (fdebug)
zfile = zdebugfile;
else
{
#if ! HAVE_HDB_LOGGING
zfile = zlogfile;
#else
const char *zprogram;
char *zalc;
if (zsystem == NULL)
ulog (LOG_FATAL,
"system name (-s argument) required for HDB format log files");
if (fuuxqt)
zprogram = "uuxqt";
else
zprogram = "uucico";
zalc = zbufalc (strlen (zlogfile)
+ strlen (zprogram)
+ strlen (zsystem)
+ 1);
sprintf (zalc, zlogfile, zprogram, zsystem);
zfile = zalc;
if (! fsysdep_file_exists (zfile))
ulog (LOG_FATAL, "no log file available for system %s", zsystem);
if (strcmp (zsystem, "ANY") == 0)
zsystem = NULL;
#endif
}
e = fopen (zfile, "r");
if (e == NULL)
{
ulog (LOG_ERROR, "fopen (%s): %s", zfile, strerror (errno));
usysdep_exit (FALSE);
}
if (cshow > 0)
{
pzshow = (char **) xmalloc (cshow * sizeof (char *));
for (ishow = 0; ishow < cshow; ishow++)
pzshow[ishow] = NULL;
ishow = 0;
}
if (zsystem != NULL)
csystem = strlen (zsystem);
if (zuser != NULL)
cuser = strlen (zuser);
zline = NULL;
cline = 0;
while (TRUE)
{
while (getline (&zline, &cline, e) > 0)
{
char *zluser, *zlsys, *znext;
size_t cluser, clsys;
znext = zline + strspn (zline, " \t");
if (! fstats)
{
#if ! HAVE_TAYLOR_LOGGING
zluser = znext;
cluser = strcspn (znext, " \t");
#endif
znext += strcspn (znext, " \t");
znext += strspn (znext, " \t");
zlsys = znext;
clsys = strcspn (znext, " \t");
znext += clsys;
znext += strspn (znext, " \t");
#if HAVE_TAYLOR_LOGGING
zluser = znext;
cluser = strcspn (znext, " \t");
#endif
}
else
{
#if ! HAVE_HDB_LOGGING
zluser = znext;
cluser = strcspn (znext, " \t");
znext += cluser;
znext += strspn (znext, " \t");
zlsys = znext;
clsys = strcspn (znext, " \t");
#else
zlsys = znext;
clsys = strcspn (znext, "!");
znext += clsys + 1;
zluser = znext;
cluser = strcspn (znext, " \t");
#endif
}
if (zsystem != NULL
&& (csystem != clsys
|| strncmp (zsystem, zlsys, clsys) != 0))
continue;
if (zuser != NULL
&& (cuser != cluser
|| strncmp (zuser, zluser, cluser) != 0))
continue;
if (cshow <= 0)
printf ("%s", zline);
else
{
ubuffree ((pointer) pzshow[ishow]);
pzshow[ishow] = zbufcpy (zline);
ishow = (ishow + 1) % cshow;
}
}
if (cshow > 0)
{
for (i = 0; i < cshow; i++)
{
if (pzshow[ishow] != NULL)
printf ("%s", pzshow[ishow]);
ishow = (ishow + 1) % cshow;
}
}
if (! fforever || ferror (e))
break;
clearerr (e);
cshow = 0;
usysdep_sleep (1);
}
(void) fclose (e);
ulog_close ();
usysdep_exit (TRUE);
return 0;
}
static void
ulusage ()
{
fprintf (stderr,
"Usage: %s [-n #] [-sf system] [-u user] [-xDSF] [-I file] [-X debug]\n",
zProgram);
fprintf (stderr, "Use %s --help for help\n", zProgram);
exit (EXIT_FAILURE);
}
static void
ulhelp ()
{
printf ("Taylor UUCP %s, copyright (C) 1991, 92, 93, 94, 1995, 2002 Ian Lance Taylor\n",
VERSION);
#if HAVE_HDB_LOGGING
printf ("Usage: %s [-n #] [-sf system] [-u user] [-xDS] [-I file] [-X debug]\n",
zProgram);
#else
printf ("Usage: %s [-n #] [-sf system] [-u user] [-DSF] [-I file] [-X debug]\n",
zProgram);
#endif
printf (" -n,--lines: show given number of lines from end of log\n");
printf (" -s,--system: print entries for named system\n");
printf (" -f system,--follow=system: follow entries for named system\n");
printf (" -u,--user user: print entries for named user\n");
#if HAVE_HDB_LOGGING
printf (" -x,--uuxqt: print uuxqt log rather than uucico log\n");
#else
printf (" -F,--follow: follow entries for any system\n");
#endif
printf (" -S,--statslog: show statistics file\n");
printf (" -D,--debuglog: show debugging file\n");
printf (" -X,--debug debug: Set debugging level\n");
#if HAVE_TAYLOR_CONFIG
printf (" -I,--config file: Set configuration file to use\n");
#endif
printf (" -v,--version: Print version and exit\n");
printf (" --help: Print help and exit\n");
printf ("Report bugs to taylor-uucp@gnu.org\n");
}