cmdline.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * This file is part of the Linux kernel, and is made available under
  3. * the terms of the GNU General Public License version 2.
  4. *
  5. * Misc librarized functions for cmdline poking.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/string.h>
  9. #include <linux/ctype.h>
  10. #include <asm/setup.h>
  11. static inline int myisspace(u8 c)
  12. {
  13. return c <= ' '; /* Close enough approximation */
  14. }
  15. /**
  16. * Find a boolean option (like quiet,noapic,nosmp....)
  17. *
  18. * @cmdline: the cmdline string
  19. * @option: option string to look for
  20. *
  21. * Returns the position of that @option (starts counting with 1)
  22. * or 0 on not found. @option will only be found if it is found
  23. * as an entire word in @cmdline. For instance, if @option="car"
  24. * then a cmdline which contains "cart" will not match.
  25. */
  26. int cmdline_find_option_bool(const char *cmdline, const char *option)
  27. {
  28. char c;
  29. int pos = 0, wstart = 0;
  30. const char *opptr = NULL;
  31. enum {
  32. st_wordstart = 0, /* Start of word/after whitespace */
  33. st_wordcmp, /* Comparing this word */
  34. st_wordskip, /* Miscompare, skip */
  35. } state = st_wordstart;
  36. if (!cmdline)
  37. return -1; /* No command line */
  38. if (!strlen(cmdline))
  39. return 0;
  40. /*
  41. * This 'pos' check ensures we do not overrun
  42. * a non-NULL-terminated 'cmdline'
  43. */
  44. while (pos < COMMAND_LINE_SIZE) {
  45. c = *(char *)cmdline++;
  46. pos++;
  47. switch (state) {
  48. case st_wordstart:
  49. if (!c)
  50. return 0;
  51. else if (myisspace(c))
  52. break;
  53. state = st_wordcmp;
  54. opptr = option;
  55. wstart = pos;
  56. /* fall through */
  57. case st_wordcmp:
  58. if (!*opptr) {
  59. /*
  60. * We matched all the way to the end of the
  61. * option we were looking for. If the
  62. * command-line has a space _or_ ends, then
  63. * we matched!
  64. */
  65. if (!c || myisspace(c))
  66. return wstart;
  67. else
  68. state = st_wordskip;
  69. } else if (!c) {
  70. /*
  71. * Hit the NULL terminator on the end of
  72. * cmdline.
  73. */
  74. return 0;
  75. } else if (c != *opptr++) {
  76. state = st_wordskip;
  77. }
  78. break;
  79. case st_wordskip:
  80. if (!c)
  81. return 0;
  82. else if (myisspace(c))
  83. state = st_wordstart;
  84. break;
  85. }
  86. }
  87. return 0; /* Buffer overrun */
  88. }
  89. /*
  90. * Find a non-boolean option (i.e. option=argument). In accordance with
  91. * standard Linux practice, if this option is repeated, this returns the
  92. * last instance on the command line.
  93. *
  94. * @cmdline: the cmdline string
  95. * @max_cmdline_size: the maximum size of cmdline
  96. * @option: option string to look for
  97. * @buffer: memory buffer to return the option argument
  98. * @bufsize: size of the supplied memory buffer
  99. *
  100. * Returns the length of the argument (regardless of if it was
  101. * truncated to fit in the buffer), or -1 on not found.
  102. */
  103. static int
  104. __cmdline_find_option(const char *cmdline, int max_cmdline_size,
  105. const char *option, char *buffer, int bufsize)
  106. {
  107. char c;
  108. int pos = 0, len = -1;
  109. const char *opptr = NULL;
  110. char *bufptr = buffer;
  111. enum {
  112. st_wordstart = 0, /* Start of word/after whitespace */
  113. st_wordcmp, /* Comparing this word */
  114. st_wordskip, /* Miscompare, skip */
  115. st_bufcpy, /* Copying this to buffer */
  116. } state = st_wordstart;
  117. if (!cmdline)
  118. return -1; /* No command line */
  119. /*
  120. * This 'pos' check ensures we do not overrun
  121. * a non-NULL-terminated 'cmdline'
  122. */
  123. while (pos++ < max_cmdline_size) {
  124. c = *(char *)cmdline++;
  125. if (!c)
  126. break;
  127. switch (state) {
  128. case st_wordstart:
  129. if (myisspace(c))
  130. break;
  131. state = st_wordcmp;
  132. opptr = option;
  133. /* fall through */
  134. case st_wordcmp:
  135. if ((c == '=') && !*opptr) {
  136. /*
  137. * We matched all the way to the end of the
  138. * option we were looking for, prepare to
  139. * copy the argument.
  140. */
  141. len = 0;
  142. bufptr = buffer;
  143. state = st_bufcpy;
  144. break;
  145. } else if (c == *opptr++) {
  146. /*
  147. * We are currently matching, so continue
  148. * to the next character on the cmdline.
  149. */
  150. break;
  151. }
  152. state = st_wordskip;
  153. /* fall through */
  154. case st_wordskip:
  155. if (myisspace(c))
  156. state = st_wordstart;
  157. break;
  158. case st_bufcpy:
  159. if (myisspace(c)) {
  160. state = st_wordstart;
  161. } else {
  162. /*
  163. * Increment len, but don't overrun the
  164. * supplied buffer and leave room for the
  165. * NULL terminator.
  166. */
  167. if (++len < bufsize)
  168. *bufptr++ = c;
  169. }
  170. break;
  171. }
  172. }
  173. if (bufsize)
  174. *bufptr = '\0';
  175. return len;
  176. }
  177. int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
  178. int bufsize)
  179. {
  180. return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option,
  181. buffer, bufsize);
  182. }