parsecmd 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. .TH PARSECMD 9
  2. .SH NAME
  3. parsecmd, cmderror, lookupcmd \- parse device commands
  4. .SH SYNOPSIS
  5. .ta \w'\fLCmdbuf* 'u
  6. .B
  7. Cmdbuf* parsecmd(char *a, int n)
  8. .PP
  9. .B
  10. void cmderror(Cmdbuf *cb, char *s)
  11. .PP
  12. .B
  13. Cmdtab* lookupcmd(Cmdbuf *cb, Cmdtab *ctab, int nctab)
  14. .SH DESCRIPTION
  15. .I Parsecmd
  16. is an interface to
  17. .I tokenize
  18. (see
  19. .IR getfields (2)),
  20. that safely parses a command, with blank-separated fields, as might be
  21. written to a device's
  22. .B ctl
  23. file.
  24. The buffer
  25. .I a
  26. and count
  27. .I n
  28. can be those passed to the driver's
  29. .I write
  30. function.
  31. .I Parsecmd
  32. converts the byte array (which might not be null-terminated) to a null-terminated string,
  33. trimming any trailing new line,
  34. before invoking
  35. .I tokenize
  36. to break the string into arguments, interpreting blank and tab as field separators
  37. when they are not quoted
  38. (in the style of
  39. .IR rc (1)).
  40. It returns a pointer to a dynamically-allocated
  41. .B Cmdbuf
  42. structure,
  43. which holds a copy of the string as
  44. modified by
  45. .IR parsefields ,
  46. and the resulting fields; it is defined as follows:
  47. .IP
  48. .EX
  49. .ta 6n +\w'char* 'u
  50. typedef
  51. struct Cmdbuf
  52. {
  53. char buf[128];
  54. char *f[16];
  55. int nf;
  56. } Cmdbuf;
  57. .EE
  58. .PP
  59. The array
  60. .B f
  61. holds the field pointers;
  62. .B nf
  63. gives the number of fields.
  64. .B Cmdbuf
  65. is allocated by
  66. .I smalloc
  67. (see
  68. .IR malloc (9)),
  69. and the caller is responsible for freeing it using
  70. .IR free .
  71. .I Cmderror
  72. prepends the given format with the original command,
  73. then calls
  74. .IR error (9).
  75. .PP
  76. Command strings may be turned into a (typically enumerated)
  77. integer with
  78. .IR lookupcmd .
  79. The catchall
  80. .L *
  81. matches any text. Unrecognized commands, or commands
  82. given an unacceptable number of arguments generate a
  83. call to
  84. .IR error .
  85. The definition is as follows
  86. .IP
  87. .EX
  88. .ta 6n +\w'char* 'u
  89. struct Cmdtab
  90. {
  91. int index;
  92. char *cmd;
  93. int narg;
  94. };
  95. .EE
  96. .PP
  97. The integer
  98. .B index
  99. is the number returned on command match.
  100. The string
  101. .B cmd
  102. is the command name, and
  103. .B narg
  104. is 0 (indicating a varadic function) or the
  105. number of arguments.
  106. .SH SOURCE
  107. .B /sys/src/9/port/parse.c
  108. .br
  109. .B /emu/port/dev.c