arg.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copy me if you can.
  3. * by 20h
  4. */
  5. #ifndef ARG_H__
  6. #define ARG_H__
  7. extern char *argv0;
  8. /* use main(int argc, char *argv[]) */
  9. #define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
  10. argv[0] && argv[0][0] == '-'\
  11. && argv[0][1];\
  12. argc--, argv++) {\
  13. char argc_;\
  14. char **argv_;\
  15. int brk_;\
  16. if (argv[0][1] == '-' && argv[0][2] == '\0') {\
  17. argv++;\
  18. argc--;\
  19. break;\
  20. }\
  21. for (brk_ = 0, argv[0]++, argv_ = argv;\
  22. argv[0][0] && !brk_;\
  23. argv[0]++) {\
  24. if (argv_ != argv)\
  25. break;\
  26. argc_ = argv[0][0];\
  27. switch (argc_)
  28. /* Handles obsolete -NUM syntax */
  29. #define ARGNUM case '0':\
  30. case '1':\
  31. case '2':\
  32. case '3':\
  33. case '4':\
  34. case '5':\
  35. case '6':\
  36. case '7':\
  37. case '8':\
  38. case '9'
  39. #define ARGEND }\
  40. }
  41. #define ARGC() argc_
  42. #define ARGNUMF() (brk_ = 1, estrtonum(argv[0], 0, INT_MAX))
  43. #define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\
  44. ((x), abort(), (char *)0) :\
  45. (brk_ = 1, (argv[0][1] != '\0')?\
  46. (&argv[0][1]) :\
  47. (argc--, argv++, argv[0])))
  48. #define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\
  49. (char *)0 :\
  50. (brk_ = 1, (argv[0][1] != '\0')?\
  51. (&argv[0][1]) :\
  52. (argc--, argv++, argv[0])))
  53. #define LNGARG() &argv[0][0]
  54. #endif