The Poor Man's (or Woman's) Intrusion Detection System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

90 lines
2.1 KiB

  1. /*********************************************************************
  2. * C++ Base Application Object
  3. * Written by Jonathan A. Foster <jon@jfpossibilities.com
  4. * Started August 31st, 2020
  5. *********************************************************************/
  6. //#include <ostream>
  7. #include <iostream>
  8. #include <cstdlib>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include "cli.h"
  12. /*********************************************************************
  13. * cBaseApp
  14. *********************************************************************/
  15. cBaseApp *baseapp = 0;
  16. cBaseApp &cBaseApp::init(int argc, char **argv) {
  17. command_argc = argc;
  18. command_args = argv;
  19. }
  20. unsigned cBaseApp::do_switch(const char *arg) {
  21. throw CLIerror("Invalid switch '"+std::string(arg)+"'");
  22. }
  23. int cBaseApp::main() {
  24. int i, ct;
  25. char *p;
  26. bool switches = true;
  27. for(i=1; i<command_argc; i++) {
  28. if(switches && *command_args[i]=='-') { // switches!
  29. ct=0;
  30. if(command_args[i][1]=='-') { // long switches
  31. if(command_args[i][2]==0) { // --
  32. switches=false;
  33. continue;
  34. }
  35. p = command_args[i]+2;
  36. ct = do_switch(p);
  37. } else { // short switches
  38. for(p=command_args[i]+1; *p; p++) {
  39. if(ct) throw std::runtime_error(
  40. "Only the last switch in stacked short switches can require an argument"
  41. );
  42. ct = do_switch(p);
  43. }
  44. p--;
  45. }
  46. // switch arguments
  47. while(ct--) {
  48. if(++i>=command_argc) throw std::runtime_error(
  49. "Last switch requires more arguments"
  50. );
  51. do_switch_arg(p, command_args[i]);
  52. }
  53. } else // non-switch arguments
  54. do_arg(command_args[i]);
  55. }
  56. return ExitCode;
  57. }
  58. int cBaseApp::help() {
  59. std::cerr <<
  60. "Invalid command line arguments and the developer didn't provide any help."
  61. << std::endl;
  62. return ExitCode = 1;
  63. }
  64. int cBaseApp::crash(const std::exception &e) {
  65. std::cerr << "Application crashed: " << e.what() << std::endl;
  66. return 216; // just a weird number hopefully not conflicting with anything else.
  67. }