option.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include <core/option.h>
  25. #include <core/debug.h>
  26. const char *
  27. nvkm_stropt(const char *optstr, const char *opt, int *arglen)
  28. {
  29. while (optstr && *optstr != '\0') {
  30. int len = strcspn(optstr, ",=");
  31. switch (optstr[len]) {
  32. case '=':
  33. if (!strncasecmpz(optstr, opt, len)) {
  34. optstr += len + 1;
  35. *arglen = strcspn(optstr, ",=");
  36. return *arglen ? optstr : NULL;
  37. }
  38. optstr++;
  39. break;
  40. case ',':
  41. optstr++;
  42. break;
  43. default:
  44. break;
  45. }
  46. optstr += len;
  47. }
  48. return NULL;
  49. }
  50. bool
  51. nvkm_boolopt(const char *optstr, const char *opt, bool value)
  52. {
  53. int arglen;
  54. optstr = nvkm_stropt(optstr, opt, &arglen);
  55. if (optstr) {
  56. if (!strncasecmpz(optstr, "0", arglen) ||
  57. !strncasecmpz(optstr, "no", arglen) ||
  58. !strncasecmpz(optstr, "off", arglen) ||
  59. !strncasecmpz(optstr, "false", arglen))
  60. value = false;
  61. else
  62. if (!strncasecmpz(optstr, "1", arglen) ||
  63. !strncasecmpz(optstr, "yes", arglen) ||
  64. !strncasecmpz(optstr, "on", arglen) ||
  65. !strncasecmpz(optstr, "true", arglen))
  66. value = true;
  67. }
  68. return value;
  69. }
  70. long
  71. nvkm_longopt(const char *optstr, const char *opt, long value)
  72. {
  73. long result = value;
  74. int arglen;
  75. char *s;
  76. optstr = nvkm_stropt(optstr, opt, &arglen);
  77. if (optstr && (s = kstrndup(optstr, arglen, GFP_KERNEL))) {
  78. int ret = kstrtol(s, 0, &value);
  79. if (ret == 0)
  80. result = value;
  81. kfree(s);
  82. }
  83. return result;
  84. }
  85. int
  86. nvkm_dbgopt(const char *optstr, const char *sub)
  87. {
  88. int mode = 1, level = CONFIG_NOUVEAU_DEBUG_DEFAULT;
  89. while (optstr) {
  90. int len = strcspn(optstr, ",=");
  91. switch (optstr[len]) {
  92. case '=':
  93. if (strncasecmpz(optstr, sub, len))
  94. mode = 0;
  95. optstr++;
  96. break;
  97. default:
  98. if (mode) {
  99. if (!strncasecmpz(optstr, "fatal", len))
  100. level = NV_DBG_FATAL;
  101. else if (!strncasecmpz(optstr, "error", len))
  102. level = NV_DBG_ERROR;
  103. else if (!strncasecmpz(optstr, "warn", len))
  104. level = NV_DBG_WARN;
  105. else if (!strncasecmpz(optstr, "info", len))
  106. level = NV_DBG_INFO;
  107. else if (!strncasecmpz(optstr, "debug", len))
  108. level = NV_DBG_DEBUG;
  109. else if (!strncasecmpz(optstr, "trace", len))
  110. level = NV_DBG_TRACE;
  111. else if (!strncasecmpz(optstr, "paranoia", len))
  112. level = NV_DBG_PARANOIA;
  113. else if (!strncasecmpz(optstr, "spam", len))
  114. level = NV_DBG_SPAM;
  115. }
  116. if (optstr[len] != '\0') {
  117. optstr++;
  118. mode = 1;
  119. break;
  120. }
  121. return level;
  122. }
  123. optstr += len;
  124. }
  125. return level;
  126. }