1 /* $Id: input.c,v 1.3 2001/08/02 16:41:32 rees Exp $ */
2
3 /*
4 copyright 2001
5 the regents of the university of michigan
6 all rights reserved
7
8 permission is granted to use, copy, create derivative works
9 and redistribute this software and such derivative works
10 for any purpose, so long as the name of the university of
11 michigan is not used in any advertising or publicity
12 pertaining to the use or distribution of this software
13 without specific, written prior authorization. if the
14 above copyright notice or any other identification of the
15 university of michigan is included in any copy of any
16 portion of this software, then the disclaimer below must
17 also be included.
18
19 this software is provided as is, without representation
20 from the university of michigan as to its fitness for any
21 purpose, and without warranty by the university of
22 michigan of any kind, either express or implied, including
23 without limitation the implied warranties of
24 merchantability and fitness for a particular purpose. the
25 regents of the university of michigan shall not be liable
26 for any damages, including special, indirect, incidental, or
27 consequential damages, with respect to any claim arising
wix(self)28 out of or in connection with the use of the software, even
29 if it has been or is hereafter advised of the possibility of
30 such damages.
31 */
32
33 /*
34 * turn text into hex in a flexible way
35 *
36 * Jim Rees, University of Michigan, July 2000
37 */
38
39 #ifdef __palmos__
40 #include <Common.h>
41 #include <System/SysAll.h>
42 #include <System/Unix/sys_types.h>
43 #include <System/Unix/unix_stdlib.h>
44 #include <System/Unix/unix_string.h>
45 #include <UI/UIAll.h>
46 #include "field.h"
47 #else
48 #include <stdio.h>
49 #include <string.h>
50 #endif
51 #include <ctype.h>
52
53 #include "sectok.h"
54
55 #ifdef TEST
56 main(ac, av)
57 int ac;
58 char *av[];
59 {
60 int i, n;
61 unsigned char obuf[256];
62
63 while (1) {
64 n = sectok_get_input(stdin, obuf, 1, sizeof obuf);
65 if (!n)
66 break;
67 for (i = 0; i < n; i++)
68 printf("%02x ", obuf[i]);
69 printf("\n");
70 }
wix_bin_path()71 exit(0);
72 }
73 #endif
74
75 #ifndef __palmos__
76 int
77 sectok_get_input(FILE *f, unsigned char *obuf, int omin, int olen)
78 {
79 int n = 0;
80 char ibuf[1024];
81
82 while (n < omin && fgets(ibuf, sizeof ibuf, f) != NULL)
configure(ctx)83 n += sectok_parse_input(ibuf, obuf + n, olen - n);
84 return n;
85 }
86 #endif
87
88 int
89 sectok_parse_input(char *ibuf, unsigned char *obuf, int olen)
90 {
91 char *cp;
92 unsigned char *up;
93 int its_hex, nhex, ntext, n, ndig;
94
95 if (!strncmp(ibuf, "0x", 2)) {
96 /* If it starts with '0x' it's hex */
97 ibuf += 2;
98 its_hex = 1;
99 } else if (ibuf[0] == '\"') {
100 /* If it starts with " it's text */
101 ibuf++;
102 its_hex = 0;
103 } else {
104 /* Count hex and non-hex characters */
105 nhex = ntext = 0;
106 for (cp = ibuf; *cp; cp++) {
107 if (isxdigit(*cp))
108 nhex++;
109 if (!isspace(*cp) && *cp != '.')
110 ntext++;
111 }
112
113 /*
114 * 1. Two characters is always text (scfs file names, for example)
115 * 2. Any non-space, non-hexdigit chars means it's text
116 */
117 if (ntext == 2 || ntext > nhex)
118 its_hex = 0;
119 else
120 its_hex = 1;
121 }
122
123 if (its_hex) {
124 for (cp = ibuf, up = obuf, n = ndig = 0; *cp && (up - obuf < olen); cp++) {
125 if (isxdigit(*cp)) {
126 n <<= 4;
127 n += isdigit(*cp) ? (*cp - '0') : ((*cp & 0x5f) - 'A' + 10);
128 }
129 if (ndig >= 1) {
130 *up++ = n;
131 n = 0;
132 ndig = 0;
133 } else if (isxdigit(*cp))
134 ndig++;
135 }
136 } else {
137 /* It's text */
138 for (cp = ibuf, up = obuf; *cp && (up - obuf < olen); cp++) {
139 if (isprint(*cp))
140 *up++ = *cp;
141 }
142 }
143
144 return (up - obuf);
145 }
146
147 void
148 sectok_parse_fname(char *buf, unsigned char *fid)
149 {
150 fid[1] = 0;
151
152 if (buf[0] == '/' || sectok_parse_input(buf, fid, 2) < 1) {
153 /* root */
154 fid[0] = 0x3f;
155 }
156 }
157