FreeCalypso > hg > freecalypso-tools
annotate target-utils/simagent/stringarg.c @ 988:5a6019ed7e72
pln-ppb-test: implement read-id
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 03 Dec 2023 00:04:18 +0000 |
parents | 0cffc53991f9 |
children |
rev | line source |
---|---|
776
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module contains the function that parses our hex string arguments. |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <ctype.h> |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include "types.h" |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 static |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 decode_hex_digit(c) |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 { |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 if (isdigit(c)) |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 return c - '0'; |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 else if (islower(c)) |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 return c - 'a' + 10; |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 else |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 return c - 'A' + 10; |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 } |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 decode_hex_string_arg(arg, buf, maxlen) |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 char *arg; |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 u8 *buf; |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 unsigned maxlen; |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 { |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 unsigned count; |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 for (count = 0; ; ) { |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 while (isspace(*arg)) |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 arg++; |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 if (!*arg) |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 break; |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) { |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 printf("ERROR: invalid hex string input\n"); |
777
0cffc53991f9
simagent: bugfix in hex string arg parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
776
diff
changeset
|
33 return(-1); |
776
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 } |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 if (count >= maxlen) { |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 printf("ERROR: hex string input is too long\n"); |
777
0cffc53991f9
simagent: bugfix in hex string arg parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
776
diff
changeset
|
37 return(-1); |
776
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 } |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 buf[count++] = (decode_hex_digit(arg[0]) << 4) | |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 decode_hex_digit(arg[1]); |
777
0cffc53991f9
simagent: bugfix in hex string arg parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
776
diff
changeset
|
41 arg += 2; |
776
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 } |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 return count; |
fac3176de18d
simagent: bare Tx implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 } |