forked from quinot/taylor-uucp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uulog.c
496 lines (419 loc) · 11.8 KB
/
uulog.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* uulog.c
Display the UUCP log file.
Copyright (C) 1991, 1992, 1993, 1994, 1995, 2002 Ian Lance Taylor
This file is part of the Taylor UUCP package.
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.
The author of the program may be contacted at [email protected].
*/
#include "uucp.h"
#if USE_RCS_ID
const char uulog_rcsid[] = "$Id$";
#endif
#include <ctype.h>
#include <errno.h>
#include "getopt.h"
#include "uudefs.h"
#include "uuconf.h"
#include "system.h"
/* This is a pretty bad implementation of uulog, which I don't think
is a very useful program anyhow. It only takes a single -s and/or
-u switch. When using HAVE_HDB_LOGGING it requires a system. */
/* Local functions. */
static void ulusage P((void));
static void ulhelp P((void));
/* Long getopt options. */
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 (int argc, char **argv)
{
/* -D: display Debug file */
boolean fdebug = FALSE;
/* -f: keep displaying lines forever. */
boolean fforever = FALSE;
/* -n lines: number of lines to display. */
int cshow = 0;
/* -s: system name. */
const char *zsystem = NULL;
/* -S: display Stats file */
boolean fstats = FALSE;
/* -u: user name. */
const char *zuser = NULL;
/* -I: configuration file name. */
const char *zconfig = NULL;
#if HAVE_HDB_LOGGING
/* -x: display uuxqt log file. */
boolean fuuxqt = FALSE;
#endif
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;
zProgram = argv[0];
/* Look for a straight number argument, and convert it to -n before
passing the arguments to getopt. */
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':
/* Show debugging file. */
fdebug = TRUE;
break;
case 'f':
/* Keep displaying lines forever for a particular system. */
fforever = TRUE;
zsystem = optarg;
if (cshow == 0)
cshow = 10;
break;
case 'F':
/* Keep displaying lines forever. */
fforever = TRUE;
if (cshow == 0)
cshow = 10;
break;
case 'I':
/* Configuration file name. */
if (fsysdep_other_config (optarg))
zconfig = optarg;
break;
case 'n':
/* Number of lines to display. */
cshow = (int) strtol (optarg, (char **) NULL, 10);
break;
case 's':
/* System name. */
zsystem = optarg;
break;
case 'S':
/* Show statistics file. */
fstats = TRUE;
break;
case 'u':
/* User name. */
zuser = optarg;
break;
case 'x':
#if HAVE_HDB_LOGGING
/* Display uuxqt log file (HDB only). */
fuuxqt = TRUE;
#endif
break;
case 'X':
#if DEBUG > 1
/* Set debugging level. */
iDebug |= idebug_parse (optarg);
#endif
break;
case 'v':
/* Print version and exit. */
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);
/*NOTREACHED*/
case 2:
/* --follow. */
fforever = TRUE;
if (cshow == 0)
cshow = 10;
if (optarg != NULL)
zsystem = optarg;
break;
case 1:
/* --help. */
ulhelp ();
exit (EXIT_SUCCESS);
/*NOTREACHED*/
case 0:
/* Long option found and flag set. */
break;
default:
ulusage ();
/*NOTREACHED*/
}
}
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;
/* Canonicalize the system name. If we can't find the
system information, just use whatever we were given so
that people can check on systems that logged in
anonymously. */
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;
/* We need a system to find a HDB log file. */
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;
}
/* Read the log file and output the appropriate lines. */
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;
/* Skip any leading whitespace (not that there should be
any). */
znext = zline + strspn (zline, " \t");
if (! fstats)
{
#if ! HAVE_TAYLOR_LOGGING
/* The user name is the first field on the line. */
zluser = znext;
cluser = strcspn (znext, " \t");
#endif
/* Skip the first field. */
znext += strcspn (znext, " \t");
znext += strspn (znext, " \t");
/* The system is the second field on the line. */
zlsys = znext;
clsys = strcspn (znext, " \t");
/* Skip the second field. */
znext += clsys;
znext += strspn (znext, " \t");
#if HAVE_TAYLOR_LOGGING
/* The user is the third field on the line. */
zluser = znext;
cluser = strcspn (znext, " \t");
#endif
}
else
{
#if ! HAVE_HDB_LOGGING
/* The user name is the first field on the line, and the
system name is the second. */
zluser = znext;
cluser = strcspn (znext, " \t");
znext += cluser;
znext += strspn (znext, " \t");
zlsys = znext;
clsys = strcspn (znext, " \t");
#else
/* The first field is system!user. */
zlsys = znext;
clsys = strcspn (znext, "!");
znext += clsys + 1;
zluser = znext;
cluser = strcspn (znext, " \t");
#endif
}
/* See if we should print this line. */
if (zsystem != NULL
&& (csystem != clsys
|| strncmp (zsystem, zlsys, clsys) != 0))
continue;
if (zuser != NULL
&& (cuser != cluser
|| strncmp (zuser, zluser, cluser) != 0))
continue;
/* Output the line, or save it if we are outputting only a
particular number of lines. */
if (cshow <= 0)
printf ("%s", zline);
else
{
ubuffree ((pointer) pzshow[ishow]);
pzshow[ishow] = zbufcpy (zline);
ishow = (ishow + 1) % cshow;
}
}
/* Output the number of lines requested by the -n option. */
if (cshow > 0)
{
for (i = 0; i < cshow; i++)
{
if (pzshow[ishow] != NULL)
printf ("%s", pzshow[ishow]);
ishow = (ishow + 1) % cshow;
}
}
/* If -f was not specified, or an error occurred while reading
the file, get out. */
if (! fforever || ferror (e))
break;
clearerr (e);
cshow = 0;
/* Sleep 1 second before going around the loop again. */
usysdep_sleep (1);
}
(void) fclose (e);
ulog_close ();
usysdep_exit (TRUE);
/* Avoid errors about not returning a value. */
return 0;
}
/* Print a usage message and die. */
static void
ulusage (void)
{
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);
}
/* Print a help message. */
static void
ulhelp (void)
{
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 /* HAVE_TAYLOR_CONFIG */
printf (" -v,--version: Print version and exit\n");
printf (" --help: Print help and exit\n");
printf ("Report bugs to [email protected]\n");
}