Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2012 Red Hat Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors: Ben Skeggs
0023  */
0024 #include <core/option.h>
0025 #include <core/debug.h>
0026 
0027 const char *
0028 nvkm_stropt(const char *optstr, const char *opt, int *arglen)
0029 {
0030     while (optstr && *optstr != '\0') {
0031         int len = strcspn(optstr, ",=");
0032         switch (optstr[len]) {
0033         case '=':
0034             if (!strncasecmpz(optstr, opt, len)) {
0035                 optstr += len + 1;
0036                 *arglen = strcspn(optstr, ",=");
0037                 return *arglen ? optstr : NULL;
0038             }
0039             optstr++;
0040             break;
0041         case ',':
0042             optstr++;
0043             break;
0044         default:
0045             break;
0046         }
0047         optstr += len;
0048     }
0049 
0050     return NULL;
0051 }
0052 
0053 bool
0054 nvkm_boolopt(const char *optstr, const char *opt, bool value)
0055 {
0056     int arglen;
0057 
0058     optstr = nvkm_stropt(optstr, opt, &arglen);
0059     if (optstr) {
0060         if (!strncasecmpz(optstr, "0", arglen) ||
0061             !strncasecmpz(optstr, "no", arglen) ||
0062             !strncasecmpz(optstr, "off", arglen) ||
0063             !strncasecmpz(optstr, "false", arglen))
0064             value = false;
0065         else
0066         if (!strncasecmpz(optstr, "1", arglen) ||
0067             !strncasecmpz(optstr, "yes", arglen) ||
0068             !strncasecmpz(optstr, "on", arglen) ||
0069             !strncasecmpz(optstr, "true", arglen))
0070             value = true;
0071     }
0072 
0073     return value;
0074 }
0075 
0076 long
0077 nvkm_longopt(const char *optstr, const char *opt, long value)
0078 {
0079     long result = value;
0080     int arglen;
0081     char *s;
0082 
0083     optstr = nvkm_stropt(optstr, opt, &arglen);
0084     if (optstr && (s = kstrndup(optstr, arglen, GFP_KERNEL))) {
0085         int ret = kstrtol(s, 0, &value);
0086         if (ret == 0)
0087             result = value;
0088         kfree(s);
0089     }
0090 
0091     return result;
0092 }
0093 
0094 int
0095 nvkm_dbgopt(const char *optstr, const char *sub)
0096 {
0097     int mode = 1, level = CONFIG_NOUVEAU_DEBUG_DEFAULT;
0098 
0099     while (optstr) {
0100         int len = strcspn(optstr, ",=");
0101         switch (optstr[len]) {
0102         case '=':
0103             if (strncasecmpz(optstr, sub, len))
0104                 mode = 0;
0105             optstr++;
0106             break;
0107         default:
0108             if (mode) {
0109                 if (!strncasecmpz(optstr, "fatal", len))
0110                     level = NV_DBG_FATAL;
0111                 else if (!strncasecmpz(optstr, "error", len))
0112                     level = NV_DBG_ERROR;
0113                 else if (!strncasecmpz(optstr, "warn", len))
0114                     level = NV_DBG_WARN;
0115                 else if (!strncasecmpz(optstr, "info", len))
0116                     level = NV_DBG_INFO;
0117                 else if (!strncasecmpz(optstr, "debug", len))
0118                     level = NV_DBG_DEBUG;
0119                 else if (!strncasecmpz(optstr, "trace", len))
0120                     level = NV_DBG_TRACE;
0121                 else if (!strncasecmpz(optstr, "paranoia", len))
0122                     level = NV_DBG_PARANOIA;
0123                 else if (!strncasecmpz(optstr, "spam", len))
0124                     level = NV_DBG_SPAM;
0125             }
0126 
0127             if (optstr[len] != '\0') {
0128                 optstr++;
0129                 mode = 1;
0130                 break;
0131             }
0132 
0133             return level;
0134         }
0135         optstr += len;
0136     }
0137 
0138     return level;
0139 }