arg.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <assert.h>
  18. #include <stdbool.h>
  19. #include <stddef.h>
  20. #include <string.h>
  21. #include <kern/arg.h>
  22. #include <kern/init.h>
  23. #include <kern/log.h>
  24. #include <kern/macros.h>
  25. #include <kern/panic.h>
  26. /*
  27. * Internally, all space characters are turned into null bytes so that
  28. * values returned to callers directly point in the command line string,
  29. * with no need for dynamic allocation.
  30. */
  31. static char arg_cmdline[ARG_CMDLINE_MAX_SIZE] __initdata;
  32. static const char *arg_cmdline_end __initdata;
  33. void __init
  34. arg_set_cmdline(const char *cmdline)
  35. {
  36. strlcpy(arg_cmdline, cmdline, sizeof(arg_cmdline));
  37. }
  38. static int __init
  39. arg_setup(void)
  40. {
  41. size_t i, length;
  42. length = strlen(arg_cmdline);
  43. for (i = 0; i < length; i++) {
  44. if (arg_cmdline[i] == ' ') {
  45. arg_cmdline[i] = '\0';
  46. }
  47. }
  48. arg_cmdline_end = arg_cmdline + length;
  49. return 0;
  50. }
  51. INIT_OP_DEFINE(arg_setup);
  52. void __init
  53. arg_log_info(void)
  54. {
  55. char cmdline[sizeof(arg_cmdline)];
  56. size_t i;
  57. for (i = 0; &arg_cmdline[i] < arg_cmdline_end; i++) {
  58. cmdline[i] = (arg_cmdline[i] == '\0') ? ' ' : arg_cmdline[i];
  59. }
  60. cmdline[i] = '\0';
  61. log_info("arg: %s", cmdline);
  62. }
  63. static const char * __init
  64. arg_walk(const char *s)
  65. {
  66. if (s == NULL) {
  67. s = arg_cmdline;
  68. } else {
  69. for (;;) {
  70. if (s >= arg_cmdline_end) {
  71. return NULL;
  72. }
  73. if (*s == '\0') {
  74. break;
  75. }
  76. s++;
  77. }
  78. }
  79. for (;;) {
  80. if (s >= arg_cmdline_end) {
  81. return NULL;
  82. }
  83. if (*s != '\0') {
  84. return s;
  85. }
  86. s++;
  87. }
  88. }
  89. static const char * __init
  90. arg_find_name_end(const char *arg)
  91. {
  92. const char *end;
  93. end = strchr(arg, '=');
  94. if (end == NULL) {
  95. end = arg + strlen(arg);
  96. }
  97. return end;
  98. }
  99. static const char * __init
  100. arg_find(const char *name)
  101. {
  102. const char *arg, *arg_name_end;
  103. size_t arg_name_length, name_length;
  104. name_length = strlen(name);
  105. assert(name_length != 0);
  106. for (arg = arg_walk(NULL); arg != NULL; arg = arg_walk(arg)) {
  107. arg_name_end = arg_find_name_end(arg);
  108. arg_name_length = arg_name_end - arg;
  109. if ((arg_name_length == name_length)
  110. && (memcmp(arg, name, name_length) == 0)) {
  111. return arg;
  112. }
  113. }
  114. return NULL;
  115. }
  116. bool __init
  117. arg_present(const char *name)
  118. {
  119. return (arg_find(name) != NULL);
  120. }
  121. const char * __init
  122. arg_value(const char *name)
  123. {
  124. const char *arg, *value;
  125. arg = arg_find(name);
  126. if (arg == NULL) {
  127. return NULL;
  128. }
  129. value = strchr(arg, '=');
  130. if (value == NULL) {
  131. return "";
  132. }
  133. return value + 1;
  134. }