From fb6042c5cd8a411cfcfcf6c1a1d466eb42a4ff33 Mon Sep 17 00:00:00 2001 From: John Denker Date: Sat, 14 Jul 2012 23:13:38 -0700 Subject: working toward a process-group signalling system --- tools/skrewt.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'tools/skrewt.c') diff --git a/tools/skrewt.c b/tools/skrewt.c index 8ae4db4..b81f0ea 100644 --- a/tools/skrewt.c +++ b/tools/skrewt.c @@ -21,7 +21,8 @@ void usage(const int sts){ "\n" " Typically used as a filter in a pipeline, along with spamc -E\n" " Options\n" -" -h print this msg (and exit immediately).\n" +" -help print this msg (and exit immediately).\n" +" -maxsize ii msg size in bytes; anything bigger will be rejected.\n" "\n" " Messages containing the string '-please-bounce-this-' will be rejected.\n" " Messages with no date will be rejected.\n" @@ -82,17 +83,53 @@ string toLower(const std::string& a){ return rslt; } - +//////////////// string ltrim(string foo){ size_t where = foo.find_first_not_of(" \t\r\n"); if (where == foo.npos) return foo; return foo.substr(where); } -int main(int argc, char** argv){ - if (argc > 1) { - if (argv[1] == string("-h")) usage(0); - usage(1); +//////////////// +// little utility to help with argument parsing: +// +int prefix(const string shorter, const string longer){ + return shorter == longer.substr(0, shorter.length()); +} + +//////////////////////////////////////////////////////////// +int main(int _argc, const char** _argv){ +//// pid_t pid = getpid(); +//// cout << pid << endl; +//// cout << getpgid(pid) << endl; + int argc(_argc); + const char **argv(_argv); + string progname(*argv); argv++; argc--; + + int maxsize(1000000); + + while (argc) { + string arg(*argv); argv++; argc--; + if (arg.substr(0,2) == "--") arg = arg.substr(1); + if (prefix(arg, "-help")) { + usage(0); + } + if (prefix(arg, "-maxsize")) { + if (!argc) { + cerr << "Option -maxsize requires an argument" << endl; + exit(1); + } + maxsize = atoi(*argv); argv++; argc--; + } + if (arg.substr(0,1) == "-") { + cerr << "Unrecognized option '" << arg << "'" << endl; + cerr << "For help, try: " << progname << " -help" << endl; + exit(1); + } else { + cerr << "Extraneous verbiage '" << arg << "'" << endl; + cerr << "For help, try: " << progname << " -help" << endl; + exit(1); + } } int inheads(1); @@ -101,7 +138,6 @@ int main(int argc, char** argv){ int textlines(0); int gotdate(0); int msgsize(0); - int msgmax(1000000); for (;;){ if (cin.eof()) break; if (cin.bad()) return 1; @@ -119,8 +155,8 @@ int main(int argc, char** argv){ string line; if (getline(cin, line).fail()) continue; msgsize += line.length()+1; - if (msgsize > msgmax) { - cerr << "skrewt rejection: bigger than " << msgmax << endl; + if (msgsize > maxsize) { + cerr << "skrewt rejection: bigger than " << maxsize << endl; exit(1); } header += "\n" + line; @@ -185,8 +221,8 @@ int main(int argc, char** argv){ string line; if (!getline(cin, line).fail()) { msgsize += line.length()+1; - if (msgsize > msgmax) { - cerr << "skrewt rejection: bigger than " << msgmax << endl; + if (msgsize > maxsize) { + cerr << "skrewt rejection: bigger than " << maxsize << endl; exit(1); } if (line == "--" + boundary) { -- cgit v1.2.3