getflags.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. void
  12. usage(void)
  13. {
  14. print("status=usage\n");
  15. exits(0);
  16. }
  17. char*
  18. findarg(char *flags, Rune r)
  19. {
  20. char *p;
  21. Rune rr;
  22. for(p=flags; p!=(char*)1; p=strchr(p, ',')+1){
  23. chartorune(&rr, p);
  24. if(rr == r)
  25. return p;
  26. }
  27. return nil;
  28. }
  29. int
  30. countargs(char *p)
  31. {
  32. int n;
  33. n = 1;
  34. while(*p == ' ')
  35. p++;
  36. for(; *p && *p != ','; p++)
  37. if(*p == ' ' && *(p-1) != ' ')
  38. n++;
  39. return n;
  40. }
  41. void
  42. main(int argc, char *argv[])
  43. {
  44. char *flags, *p, buf[512];
  45. int i, n;
  46. Fmt fmt;
  47. doquote = needsrcquote;
  48. quotefmtinstall();
  49. argv0 = argv[0]; /* for sysfatal */
  50. flags = getenv("flagfmt");
  51. if(flags == nil){
  52. fprint(2, "$flagfmt not set\n");
  53. print("exit 'missing flagfmt'");
  54. exits(0);
  55. }
  56. fmtfdinit(&fmt, 1, buf, sizeof buf);
  57. for(p=flags; p!=(char*)1; p=strchr(p, ',')+1)
  58. fmtprint(&fmt, "flag%.1s=()\n", p);
  59. ARGBEGIN{
  60. default:
  61. if((p = findarg(flags, ARGC())) == nil)
  62. usage();
  63. p += runelen(ARGC());
  64. if(*p == ',' || *p == 0){
  65. fmtprint(&fmt, "flag%C=1\n", ARGC());
  66. break;
  67. }
  68. n = countargs(p);
  69. fmtprint(&fmt, "flag%C=(", ARGC());
  70. for(i=0; i<n; i++)
  71. fmtprint(&fmt, "%s%q", i ? " " : "", EARGF(usage()));
  72. fmtprint(&fmt, ")\n");
  73. }ARGEND
  74. fmtprint(&fmt, "*=(");
  75. for(i=0; i<argc; i++)
  76. fmtprint(&fmt, "%s%q", i ? " " : "", argv[i]);
  77. fmtprint(&fmt, ")\n");
  78. fmtprint(&fmt, "status=''\n");
  79. fmtfdflush(&fmt);
  80. exits(0);
  81. }