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.

cli.cpp 1.8 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. int cBaseApp::main() {
  21. int i, ct;
  22. char *p;
  23. bool switches = true;
  24. for(i=1; i<command_argc; i++) {
  25. if(switches && *command_args[i]=='-') { // switches!
  26. ct=0;
  27. if(command_args[i][1]=='-') { // long switches
  28. if(command_args[i][2]==0) { // --
  29. switches=false;
  30. continue;
  31. }
  32. p = command_args[i]+2;
  33. ct = do_switch(p);
  34. } else { // short switches
  35. for(p=command_args[i]+1; *p; p++) {
  36. if(ct) throw std::runtime_error(
  37. "Only the last switch in stacked short switches can require an argument"
  38. );
  39. ct = do_switch(p);
  40. }
  41. p--;
  42. }
  43. // switch arguments
  44. while(ct--) {
  45. if(++i>=command_argc) throw std::runtime_error(
  46. "Last switch requires more arguments"
  47. );
  48. do_switch_arg(p, command_args[i]);
  49. }
  50. } else // non-switch arguments
  51. do_arg(command_args[i]);
  52. }
  53. return ExitCode;
  54. }
  55. int cBaseApp::crash(const std::exception &e) {
  56. std::cerr << "Application crashed: " << e.what() << std::endl;
  57. return 216; // just a weird number hopefully not conflicting with anything else.
  58. }