loader: remove shouting from ORB's variable name
[hvf.git] / cp / shell / cmd_helpers.c
blob0badca3b3f59b0d9f05f3efa8260dd0c66bc4777
1 /*
2 * (C) Copyright 2007-2010 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * This file is released under the GPLv2. See the COPYING file for more
5 * details.
6 */
8 static char* __extract_dec(char *str, u64 *val)
10 u64 res;
11 u64 tmp;
12 int len;
14 if (!str || !val)
15 return ERR_PTR(-EINVAL);
17 res = 0;
18 len = -1;
20 for (; str && *str != '\0'; str++, len++) {
21 if (*str >= '0' && *str <= '9')
22 tmp = *str - '0';
23 else if (*str == ' ' || *str == '\t')
24 break;
25 else
26 return ERR_PTR(-EINVAL);
28 res = (res * 10) + tmp;
31 if (len == -1)
32 return ERR_PTR(-EINVAL);
34 *val = res;
36 return str;
39 static char* __extract_hex(char *str, u64 *val)
41 u64 res;
42 u64 tmp;
43 int len;
45 if (!str || !val)
46 return ERR_PTR(-EINVAL);
48 res = 0;
49 len = -1;
51 for (; str && *str != '\0'; str++, len++) {
52 if (*str >= '0' && *str <= '9')
53 tmp = *str - '0';
54 else if (*str >= 'A' && *str <= 'F')
55 tmp = *str - 'A' + 10;
56 else if (*str >= 'a' && *str <= 'f')
57 tmp = *str - 'a' + 10;
58 else if (*str == ' ' || *str == '\t')
59 break;
60 else
61 return ERR_PTR(-EINVAL);
63 res = (res << 4) | tmp;
66 if (len == -1)
67 return ERR_PTR(-EINVAL);
69 *val = res;
71 return str;
74 static char* __consume_ws(char *str)
76 if (!str)
77 return str;
79 /* consume any extra whitespace */
80 while(*str == ' ' || *str == '\t')
81 str++;
83 return str;