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 
27 const char *
nvkm_stropt(const char * optstr,const char * opt,int * arglen)28 nvkm_stropt(const char *optstr, const char *opt, int *arglen)
29 {
30 	while (optstr && *optstr != '\0') {
31 		int len = strcspn(optstr, ",=");
32 		switch (optstr[len]) {
33 		case '=':
34 			if (!strncasecmpz(optstr, opt, len)) {
35 				optstr += len + 1;
36 				*arglen = strcspn(optstr, ",=");
37 				return *arglen ? optstr : NULL;
38 			}
39 			optstr++;
40 			break;
41 		case ',':
42 			optstr++;
43 			break;
44 		default:
45 			break;
46 		}
47 		optstr += len;
48 	}
49 
50 	return NULL;
51 }
52 
53 bool
nvkm_boolopt(const char * optstr,const char * opt,bool value)54 nvkm_boolopt(const char *optstr, const char *opt, bool value)
55 {
56 	int arglen;
57 
58 	optstr = nvkm_stropt(optstr, opt, &arglen);
59 	if (optstr) {
60 		if (!strncasecmpz(optstr, "0", arglen) ||
61 		    !strncasecmpz(optstr, "no", arglen) ||
62 		    !strncasecmpz(optstr, "off", arglen) ||
63 		    !strncasecmpz(optstr, "false", arglen))
64 			value = false;
65 		else
66 		if (!strncasecmpz(optstr, "1", arglen) ||
67 		    !strncasecmpz(optstr, "yes", arglen) ||
68 		    !strncasecmpz(optstr, "on", arglen) ||
69 		    !strncasecmpz(optstr, "true", arglen))
70 			value = true;
71 	}
72 
73 	return value;
74 }
75 
76 int
nvkm_dbgopt(const char * optstr,const char * sub)77 nvkm_dbgopt(const char *optstr, const char *sub)
78 {
79 	int mode = 1, level = CONFIG_NOUVEAU_DEBUG_DEFAULT;
80 
81 	while (optstr) {
82 		int len = strcspn(optstr, ",=");
83 		switch (optstr[len]) {
84 		case '=':
85 			if (strncasecmpz(optstr, sub, len))
86 				mode = 0;
87 			optstr++;
88 			break;
89 		default:
90 			if (mode) {
91 				if (!strncasecmpz(optstr, "fatal", len))
92 					level = NV_DBG_FATAL;
93 				else if (!strncasecmpz(optstr, "error", len))
94 					level = NV_DBG_ERROR;
95 				else if (!strncasecmpz(optstr, "warn", len))
96 					level = NV_DBG_WARN;
97 				else if (!strncasecmpz(optstr, "info", len))
98 					level = NV_DBG_INFO_NORMAL;
99 				else if (!strncasecmpz(optstr, "debug", len))
100 					level = NV_DBG_DEBUG;
101 				else if (!strncasecmpz(optstr, "trace", len))
102 					level = NV_DBG_TRACE;
103 				else if (!strncasecmpz(optstr, "paranoia", len))
104 					level = NV_DBG_PARANOIA;
105 				else if (!strncasecmpz(optstr, "spam", len))
106 					level = NV_DBG_SPAM;
107 			}
108 
109 			if (optstr[len] != '\0') {
110 				optstr++;
111 				mode = 1;
112 				break;
113 			}
114 
115 			return level;
116 		}
117 		optstr += len;
118 	}
119 
120 	return level;
121 }
122