-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipar_interactive.cpp
More file actions
100 lines (93 loc) · 2.18 KB
/
ipar_interactive.cpp
File metadata and controls
100 lines (93 loc) · 2.18 KB
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
// Program ipar_interactive
// ------------------------
// Reads IP address ranges from standard input.
// Performs the following, until EOF on input:
// * Prompt for input (> ).
// * Read in a line of input.
// * Write out an equivalent list of ranges in one of three formats, to
// standard output:
// * standard form.
// * intervals, with dashes.
// * individual 32-bit numbers, in hex format.
// The last format is useful for testing.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
using namespace std;
#include "ipar_iplist.h"
#include "ipar_common.h"
int main (int argc, char* argv[])
{
// Process arguments
IPAR::OutputStyle style;
switch (argc)
{
case 1:
style = IPAR::Scidr;
break;
case 2:
style = IPAR::o_style(argv[1]);
if (style == IPAR::Sunknown)
{
cerr << "Usage: ipar_interactive [-cidr|-dashes|-hex]" << endl;
cerr << "(no other arguments)" << endl;
return 1;
}
break;
default:
cerr << "Usage: ipar_interactive [-cidr|-dashes|-hex]" << endl;
cerr << "(no other arguments)" << endl;
return 1;
}
// Modify output format if necessary
if (style == IPAR::Shex) cout << hex << setfill('0');
string line;
cout << "> " << flush;
while (getline(cin, line))
{
// Process one line of input
string word;
istringstream sstr(line);
IPAR::List iplist;
while (sstr >> word)
{
// Ignore comments to end of line
if (word[0] == '#')
{
word = "";
break;
}
IPAR::Range iprange;
try {
iprange = IPAR::Range(word);
}
catch (const exception& ex) {
cerr << "ERROR: could not parse \"" << word << "\"" << endl;
break;
}
iplist.add (iprange);
}
// Report from one line of input
if (style == IPAR::Shex)
{
cout << hex << setfill('0');
for (auto iter = iplist.cbegin() ; iter != iplist.cend() ; ++iter)
{
uint32_t lower = iter->first;
while (lower <= iter->second)
{
cout << setw(8) << lower++ << endl;
}
}
}
else
{
iplist.print(cout, (style == IPAR::Sdashes));
}
cout << "> " << flush;
}
// Left over from last prompt
cout << endl;
return 0;
}