args.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * args.c
  3. * Routines for processing command-line arguments.
  4. *
  5. * Matej Pfajfar <mp292@cam.ac.uk>
  6. */
  7. /*
  8. * Changes :
  9. * $Log$
  10. * Revision 1.1 2002/06/26 22:45:50 arma
  11. * Initial revision
  12. *
  13. * Revision 1.3 2002/01/27 00:42:50 mp292
  14. * Reviewed according to Secure-Programs-HOWTO.
  15. *
  16. * Revision 1.2 2002/01/04 10:05:28 badbytes
  17. * Completed.
  18. *
  19. * Revision 1.1 2002/01/03 10:23:43 badbytes
  20. * Code based on that in op. Needs to be modified.
  21. */
  22. #include "or.h"
  23. /* prints help on using or */
  24. void print_usage()
  25. {
  26. char *program = "or";
  27. printf("\n%s - Onion Router.\nUsage : %s -f config [-l loglevel -h]\n-h : display this help\n-f config : config file\n-l loglevel : logging threshold; one of alert|crit|err|warning|notice|info|debug\n\n", program,program);
  28. }
  29. /* get command-line arguments */
  30. int getargs(int argc,char *argv[], char *args, char **conf_filename, int *loglevel)
  31. {
  32. char c; /* next option character */
  33. int gotf=0;
  34. if ((!args) || (!conf_filename) || (!loglevel))
  35. return -1;
  36. while ((c = getopt(argc,argv,args)) != -1)
  37. {
  38. switch(c)
  39. {
  40. case 'f': /* config file */
  41. *conf_filename = optarg;
  42. gotf=1;
  43. break;
  44. case 'h':
  45. print_usage(argv[0]);
  46. exit(0);
  47. case 'l':
  48. if (!strcmp(optarg,"emerg"))
  49. *loglevel = LOG_EMERG;
  50. else if (!strcmp(optarg,"alert"))
  51. *loglevel = LOG_ALERT;
  52. else if (!strcmp(optarg,"crit"))
  53. *loglevel = LOG_CRIT;
  54. else if (!strcmp(optarg,"err"))
  55. *loglevel = LOG_ERR;
  56. else if (!strcmp(optarg,"warning"))
  57. *loglevel = LOG_WARNING;
  58. else if (!strcmp(optarg,"notice"))
  59. *loglevel = LOG_NOTICE;
  60. else if (!strcmp(optarg,"info"))
  61. *loglevel = LOG_INFO;
  62. else if (!strcmp(optarg,"debug"))
  63. *loglevel = LOG_DEBUG;
  64. else
  65. {
  66. log(LOG_ERR,"Error : argument to -l must be one of alert|crit|err|warning|notice|info|debug.");
  67. print_usage(argv[0]);
  68. return -1;
  69. }
  70. break;
  71. case '?':
  72. if (isprint(c))
  73. log(LOG_ERR,"Missing argument or unknown option '-%c'. See help (-h).",optopt);
  74. else
  75. log(LOG_ERR,"Unknown option character 'x%x'. See help (-h).",optopt);
  76. print_usage(argv[0]);
  77. return -1;
  78. break;
  79. default:
  80. return -1;
  81. }
  82. }
  83. /* the -f option is mandatory */
  84. if (!gotf)
  85. {
  86. log(LOG_ERR,"You must specify a config file with the -f option. See help (-h).");
  87. return -1;
  88. }
  89. return 0;
  90. }