[tools/peerindex] Add support for -o option

This commit is contained in:
Lorenzo Cogotti 2021-08-03 23:56:05 +02:00
parent 49e4ee3bfa
commit 0d6093a74b
3 changed files with 51 additions and 3 deletions

View File

@ -42,6 +42,14 @@ section below for usage examples.
.SH OPTIONS .SH OPTIONS
The following options are supported: The following options are supported:
.IP "\fB\-o \fI<file>\fP" 10
Write output to
.BR file .
Instead of using standard output,
.IR @UTILITY@
shall output PEER INDEX TABLE information to the specified file. If option
occurs multiple times, last specified file is used.
.IP "\fB\-r or \-\-only\-refs\fP" 10 .IP "\fB\-r or \-\-only\-refs\fP" 10
By default By default
.IR @UTILITY@ .IR @UTILITY@
@ -67,7 +75,6 @@ may cause some peers inside PEER INDEX TABLES to be discarded (See the
.IR OPTIONS .IR OPTIONS
section for details). section for details).
Each peer is formatted as the following `|' separated fields: Each peer is formatted as the following `|' separated fields:
.RS 4 .RS 4
.nf .nf
@ -136,7 +143,9 @@ supported compression formats. If the file extension is not recognized,
or there is no extension, then it is assumed to be uncompressed. or there is no extension, then it is assumed to be uncompressed.
.SH STDOUT .SH STDOUT
The standard output is used to print a human readable text representation of Unless redirected explicitly via
.IR OPTIONS ,
the standard output is used to print a human readable text representation of
PEER INDEX TABLE contents, nothing else shall be written to the standard output. PEER INDEX TABLE contents, nothing else shall be written to the standard output.
.IR @UTILITY@ .IR @UTILITY@
may detect and treat as error whenever the standard output is a regular file, may detect and treat as error whenever the standard output is a regular file,

View File

@ -32,6 +32,7 @@
typedef enum { typedef enum {
ONLY_REFS_FLAG, ONLY_REFS_FLAG,
OUTPUT_FLAG,
NUM_FLAGS NUM_FLAGS
} PeerindexOpt; } PeerindexOpt;
@ -40,6 +41,9 @@ static Optflag options[] = {
[ONLY_REFS_FLAG] = { [ONLY_REFS_FLAG] = {
'r', "only-refs", NULL, "Only dump peers referenced by RIBs", ARG_NONE 'r', "only-refs", NULL, "Only dump peers referenced by RIBs", ARG_NONE
}, },
[OUTPUT_FLAG] = {
'o', NULL, "file", "Write output to file", ARG_REQ
},
[NUM_FLAGS] = { '\0' } [NUM_FLAGS] = { '\0' }
}; };
@ -64,6 +68,21 @@ static void Peerindex_SetupCommandLine(char *argv0)
"Any diagnostic message is logged to stderr."; "Any diagnostic message is logged to stderr.";
} }
static void Peerindex_Fatal(const char *fmt, ...)
{
va_list va;
Sys_Print(STDERR, com_progName);
Sys_Print(STDERR, ": ERROR: ");
va_start(va, fmt);
Sys_VPrintf(STDERR, fmt, va);
va_end(va);
Sys_Print(STDERR, "\n");
exit(EXIT_FAILURE);
}
static void Peerindex_Warning(const char *fmt, ...) static void Peerindex_Warning(const char *fmt, ...)
{ {
va_list va; va_list va;
@ -180,6 +199,20 @@ static void Peerindex_ApplyProgramOptions(void)
S.peerIndexClearVal = 0; S.peerIndexClearVal = 0;
else else
S.peerIndexClearVal = 0xff; // so we always print the full table S.peerIndexClearVal = 0xff; // so we always print the full table
if (options[OUTPUT_FLAG].flagged) {
const char *filename = options[OUTPUT_FLAG].optarg;
Fildes fd = Sys_Fopen(filename, FM_WRITE, /*hints=*/0);
if (fd == FILDES_BAD)
Peerindex_Fatal("Can't open output file \"%s\"", filename);
S.outf = STM_FILDES(fd);
S.outfOps = Stm_FildesOps;
} else {
S.outf = STM_CONHN(STDOUT);
S.outfOps = Stm_ConOps;
}
} }
static void Peerindex_Init(void) static void Peerindex_Init(void)
@ -267,7 +300,7 @@ static void Peerindex_FlushPeerIndexTable(void)
Uint16 idx = 0; Uint16 idx = 0;
Bufio_Init(&sb, STM_CONHN(STDOUT), Stm_ConOps); Bufio_Init(&sb, S.outf, S.outfOps);
Bgp_StartMrtPeersv2(&it, &S.peerIndex); Bgp_StartMrtPeersv2(&it, &S.peerIndex);
while ((peer = Bgp_NextMrtPeerv2(&it)) != NULL) { while ((peer = Bgp_NextMrtPeerv2(&it)) != NULL) {
@ -389,5 +422,7 @@ int main(int argc, char **argv)
while (i < argc) while (i < argc)
Peerindex_ProcessMrtDump(argv[i++]); Peerindex_ProcessMrtDump(argv[i++]);
if (S.outfOps->Close) S.outfOps->Close(S.outf);
return (S.nerrors > 0) ? EXIT_FAILURE : EXIT_SUCCESS; return (S.nerrors > 0) ? EXIT_FAILURE : EXIT_SUCCESS;
} }

View File

@ -31,6 +31,10 @@ FORCE_INLINE Boolean ISPEERINDEXREF(const PeerRefsTab tab, Uint16 idx)
} }
typedef struct { typedef struct {
// Output stream
void *outf;
const StmOps *outfOps;
// MRT input file stream // MRT input file stream
const char *filename; const char *filename;
void *inf; void *inf;