Add two new kdb environment access methods as kdb_setenv() and kdb_printenv() in order to abstract out environment access code from kdb command functions. Also, replace (char *)0 with NULL as an initializer for environment variables array.
Unified · Split
| @@ -140,42 +140,18 @@ static const int __nkdb_err = ARRAY_SIZE(kdbmsgs); · 139 unmodified lines | |||
| 140 | * KDB_ENVBUFSIZE if required). | 140 | * KDB_ENVBUFSIZE if required). |
| 141 | */ | 141 | */ |
| 142 | 142 | ||
| 143 | static char *__env[] = { | 143 | static char *__env[31] = { |
| 144 | #if defined(CONFIG_SMP) | 144 | #if defined(CONFIG_SMP) |
| 145 | "PROMPT=[%d]kdb> ", | 145 | "PROMPT=[%d]kdb> ", |
| 146 | #else | 146 | #else |
| 147 | "PROMPT=kdb> ", | 147 | "PROMPT=kdb> ", |
| 148 | #endif | 148 | #endif |
| 149 | "MOREPROMPT=more> ", | 149 | "MOREPROMPT=more> ", |
| 150 | "RADIX=16", | 150 | "RADIX=16", |
| 151 | "MDCOUNT=8", /* lines of md output */ | 151 | "MDCOUNT=8", /* lines of md output */ |
| 152 | KDB_PLATFORM_ENV, | 152 | KDB_PLATFORM_ENV, |
| 153 | "DTABCOUNT=30", | 153 | "DTABCOUNT=30", |
| 154 | "NOSECT=1", | 154 | "NOSECT=1", |
| 155 | (char *)0, | ||
| 156 | (char *)0, | ||
| 157 | (char *)0, | ||
| 158 | (char *)0, | ||
| 159 | (char *)0, | ||
| 160 | (char *)0, | ||
| 161 | (char *)0, | ||
| 162 | (char *)0, | ||
| 163 | (char *)0, | ||
| 164 | (char *)0, | ||
| 165 | (char *)0, | ||
| 166 | (char *)0, | ||
| 167 | (char *)0, | ||
| 168 | (char *)0, | ||
| 169 | (char *)0, | ||
| 170 | (char *)0, | ||
| 171 | (char *)0, | ||
| 172 | (char *)0, | ||
| 173 | (char *)0, | ||
| 174 | (char *)0, | ||
| 175 | (char *)0, | ||
| 176 | (char *)0, | ||
| 177 | (char *)0, | ||
| 178 | (char *)0, | ||
| 179 | }; | 155 | }; |
| 180 | 156 | ||
| 181 | static const int __nenv = ARRAY_SIZE(__env); | 157 | static const int __nenv = ARRAY_SIZE(__env); |
| @@ -317,6 +293,63 @@ int kdbgetintenv(const char *match, int *value) · 135 unmodified lines | |||
| 317 | return diag; | 293 | return diag; |
| 318 | } | 294 | } |
| 319 | 295 | ||
| 296 | /* | ||
| 297 | * kdb_setenv() - Alter an existing environment variable or create a new one. | ||
| 298 | * @var: Name of the variable | ||
| 299 | * @val: Value of the variable | ||
| 300 | * | ||
| 301 | * Return: Zero on success, a kdb diagnostic on failure. | ||
| 302 | */ | ||
| 303 | static int kdb_setenv(const char *var, const char *val) | ||
| 304 | { | ||
| 305 | int i; | ||
| 306 | char *ep; | ||
| 307 | size_t varlen, vallen; | ||
| 308 | |||
| 309 | varlen = strlen(var); | ||
| 310 | vallen = strlen(val); | ||
| 311 | ep = kdballocenv(varlen + vallen + 2); | ||
| 312 | if (ep == (char *)0) | ||
| 313 | return KDB_ENVBUFFULL; | ||
| 314 | |||
| 315 | sprintf(ep, "%s=%s", var, val); | ||
| 316 | |||
| 317 | for (i = 0; i < __nenv; i++) { | ||
| 318 | if (__env[i] | ||
| 319 | && ((strncmp(__env[i], var, varlen) == 0) | ||
| 320 | && ((__env[i][varlen] == '\0') | ||
| 321 | || (__env[i][varlen] == '=')))) { | ||
| 322 | __env[i] = ep; | ||
| 323 | return 0; | ||
| 324 | } | ||
| 325 | } | ||
| 326 | |||
| 327 | /* | ||
| 328 | * Wasn't existing variable. Fit into slot. | ||
| 329 | */ | ||
| 330 | for (i = 0; i < __nenv-1; i++) { | ||
| 331 | if (__env[i] == (char *)0) { | ||
| 332 | __env[i] = ep; | ||
| 333 | return 0; | ||
| 334 | } | ||
| 335 | } | ||
| 336 | |||
| 337 | return KDB_ENVFULL; | ||
| 338 | } | ||
| 339 | |||
| 340 | /* | ||
| 341 | * kdb_printenv() - Display the current environment variables. | ||
| 342 | */ | ||
| 343 | static void kdb_printenv(void) | ||
| 344 | { | ||
| 345 | int i; | ||
| 346 | |||
| 347 | for (i = 0; i < __nenv; i++) { | ||
| 348 | if (__env[i]) | ||
| 349 | kdb_printf("%s\n", __env[i]); | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 320 | /* | 353 | /* |
| 321 | * kdbgetularg - This function will convert a numeric string into an | 354 | * kdbgetularg - This function will convert a numeric string into an |
| 322 | * unsigned long value. | 355 | * unsigned long value. |
| @@ -374,10 +407,6 @@ int kdbgetu64arg(const char *arg, u64 *value) · 51 unmodified lines | |||
| 374 | */ | 407 | */ |
| 375 | int kdb_set(int argc, const char **argv) | 408 | int kdb_set(int argc, const char **argv) |
| 376 | { | 409 | { |
| 377 | int i; | ||
| 378 | char *ep; | ||
| 379 | size_t varlen, vallen; | ||
| 380 | |||
| 381 | /* | 410 | /* |
| 382 | * we can be invoked two ways: | 411 | * we can be invoked two ways: |
| 383 | * set var=value argv[1]="var", argv[2]="value" | 412 | * set var=value argv[1]="var", argv[2]="value" |
| @@ -422,37 +451,7 @@ int kdb_set(int argc, const char **argv) · 38 unmodified lines | |||
| 422 | * Tokenizer squashed the '=' sign. argv[1] is variable | 451 | * Tokenizer squashed the '=' sign. argv[1] is variable |
| 423 | * name, argv[2] = value. | 452 | * name, argv[2] = value. |
| 424 | */ | 453 | */ |
| 425 | varlen = strlen(argv[1]); | 454 | return kdb_setenv(argv[1], argv[2]); |
| 426 | vallen = strlen(argv[2]); | ||
| 427 | ep = kdballocenv(varlen + vallen + 2); | ||
| 428 | if (ep == (char *)0) | ||
| 429 | return KDB_ENVBUFFULL; | ||
| 430 | |||
| 431 | sprintf(ep, "%s=%s", argv[1], argv[2]); | ||
| 432 | |||
| 433 | ep[varlen+vallen+1] = '\0'; | ||
| 434 | |||
| 435 | for (i = 0; i < __nenv; i++) { | ||
| 436 | if (__env[i] | ||
| 437 | && ((strncmp(__env[i], argv[1], varlen) == 0) | ||
| 438 | && ((__env[i][varlen] == '\0') | ||
| 439 | || (__env[i][varlen] == '=')))) { | ||
| 440 | __env[i] = ep; | ||
| 441 | return 0; | ||
| 442 | } | ||
| 443 | } | ||
| 444 | |||
| 445 | /* | ||
| 446 | * Wasn't existing variable. Fit into slot. | ||
| 447 | */ | ||
| 448 | for (i = 0; i < __nenv-1; i++) { | ||
| 449 | if (__env[i] == (char *)0) { | ||
| 450 | __env[i] = ep; | ||
| 451 | return 0; | ||
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 | return KDB_ENVFULL; | ||
| 456 | } | 455 | } |
| 457 | 456 | ||
| 458 | static int kdb_check_regs(void) | 457 | static int kdb_check_regs(void) |
| @@ -2055,12 +2054,7 @@ static int kdb_lsmod(int argc, const char **argv) · 1596 unmodified lines | |||
| 2055 | 2054 | ||
| 2056 | static int kdb_env(int argc, const char **argv) | 2055 | static int kdb_env(int argc, const char **argv) |
| 2057 | { | 2056 | { |
| 2058 | int i; | 2057 | kdb_printenv(); |
| 2059 | |||
| 2060 | for (i = 0; i < __nenv; i++) { | ||
| 2061 | if (__env[i]) | ||
| 2062 | kdb_printf("%s\n", __env[i]); | ||
| 2063 | } | ||
| 2064 | 2058 | ||
| 2065 | if (KDB_DEBUG(MASK)) | 2059 | if (KDB_DEBUG(MASK)) |
| 2066 | kdb_printf("KDBDEBUG=0x%x\n", | 2060 | kdb_printf("KDBDEBUG=0x%x\n", |