usage.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <u.h>
  2. #include <libc.h>
  3. void
  4. main(void)
  5. {
  6. Fmt fmt;
  7. char buf[512];
  8. char *argv0, *args, *flags, *p, *p0;
  9. int single;
  10. Rune r;
  11. argv0 = getenv("0");
  12. if((p = strrchr(argv0, '/')) != nil)
  13. argv0 = p+1;
  14. flags = getenv("flagfmt");
  15. args = getenv("args");
  16. if(argv0 == nil){
  17. fprint(2, "aux/usage: $0 not set\n");
  18. exits("$0");
  19. }
  20. if(flags == nil)
  21. flags = "";
  22. if(args == nil)
  23. args = "";
  24. fmtfdinit(&fmt, 2, buf, sizeof buf);
  25. fmtprint(&fmt, "usage: %s", argv0);
  26. if(flags[0]){
  27. single = 0;
  28. for(p=flags; *p; ){
  29. p += chartorune(&r, p);
  30. if(*p == ',' || *p == 0){
  31. if(!single){
  32. fmtprint(&fmt, " [-");
  33. single = 1;
  34. }
  35. fmtprint(&fmt, "%C", r);
  36. if(*p == ',')
  37. p++;
  38. continue;
  39. }
  40. while(*p == ' ')
  41. p++;
  42. if(single){
  43. fmtprint(&fmt, "]");
  44. single = 0;
  45. }
  46. p0 = p;
  47. p = strchr(p0, ',');
  48. if(p == nil)
  49. p = "";
  50. else
  51. *p++ = 0;
  52. fmtprint(&fmt, " [-%C %s]", r, p0);
  53. }
  54. if(single)
  55. fmtprint(&fmt, "]");
  56. }
  57. if(args)
  58. fmtprint(&fmt, " %s", args);
  59. fmtprint(&fmt, "\n");
  60. fmtfdflush(&fmt);
  61. exits("usage");
  62. }