4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 stoa - convert string to address
34 If a string begins in \o or \O, the following address is octal
35 " " " " " \x or \X, the following address is hex
36 Otherwise, a string is considered text. Text may be quoted
37 with double quotes and the C-like escapes \n, \b, \t, \v, \r, and \nnn
38 (nnn = octal char) are recognized.
39 A \ followed by a newline causes the newline
40 to vanish. A \ followed by any other char causes any "magic" of
41 any other char to disappear.
43 Other escape sequences recognized are:
44 \!cmd args [ \! || EOL ]
45 which is replaced by the raw output of the execution of cmd.
46 This may only be used in a string.
48 \$cmd args [ \$ || EOL ]
49 which is replaced by the output of the execution of cmd and
52 A NULL is returned on any error(s).
61 #define toupper(c) (islower(c) ? _toupper(c) : (c))
62 #define todigit(c) ((int)((c) - '0')) /* char to digit */
63 #define toxdigit(c) ((isdigit(c))?todigit(c):(toupper(c)-(int)'A'+10))
64 #define isodigit(c) (isdigit(c) && ((c) != '9') && ((c) != '8'))
65 #define itoac(i) (((i) > 9) ? ((char)((i)-10) + 'A'):((char)(i) + '0'))
66 #define MASK(n) ((1 << (n)) - 1)
68 #define MAXRLEVEL 10 /* maximum recursion level */
73 char scanbuf
[SBUFSIZE
];
75 int rec
= 0; /* Recursion level */
82 stoa(str
, addr
) /* Return 0 for success, -1 for error */
86 char *xfer(), *prescan();
88 int myadr
; /* was netbuf struct allocated here ? */
89 int quote
; /* quoted string ? */
95 while (*str
&& isspace(*str
)) /* leading whites are OK */
98 str
= prescan(str
); /* Do all \$ ... \$ */
100 if (!str
|| !*str
) return NULL
; /* Nothing to convert */
103 if ((addr
= (struct netbuf
*)malloc(sizeof(struct netbuf
))) == NULL
)
111 /* Now process the address */
120 addr
->len
= dobase(++str
, sbuf
, HEX
);
123 case 'o': /* octal */
125 addr
->len
= dobase(++str
, sbuf
, OCT
);
128 case '\0': /* No address given!, length is 0 */
129 addr
->len
= dostring(str
, sbuf
, 0);
132 default: /* \ is handled by dostring */
133 addr
->len
= dostring(--str
, sbuf
, quote
);
138 if (*str
== '"') { /* quoted string */
142 addr
->len
= dostring(str
, sbuf
, quote
);
145 if (addr
->len
== 0) { /* Error in conversion */
150 if ((addr
->buf
= xfer(addr
->buf
, sbuf
, addr
->len
, addr
->maxlen
)) == NULL
)
158 dostring: Copy string at s to buf translating
159 escaped characters and shell escapes.
160 return length of string.
164 dostring(s
, buf
, quote
) /* read in a raw address */
175 if (len
>= SBUFSIZE
) {
176 fprintf(stderr
, "dostring: string too long\n");
182 case '!': /* raw shell escape */
183 if (rout
= xcmd(s
+1, '!', &s
, &l
)) {
184 if (len
+ l
< SBUFSIZE
)
185 memcpy(buf
+len
, rout
, l
);
191 case '\n': /* ignore newline */
195 case 'b': /* backspace */
196 buf
[len
++] = '\b'; s
++;
199 case 'n': /* newline */
200 buf
[len
++] = '\n'; s
++;
203 case 'r': /* return */
204 buf
[len
++] = '\r'; s
++;
207 case 't': /* horiz. tab */
208 buf
[len
++] = '\t'; s
++;
211 case 'v': /* vert. tab */
212 buf
[len
++] = '\v'; s
++;
219 for(oc
=ch
=0; (*s
>= '0' && *s
<= '7') && oc
++ < 3; ++s
)
220 ch
= (ch
<< 3) | (*s
- '0');
224 case 0: /* end of string -- terminate */
227 default: /* take the character blindly */
231 else if ((quote
&& (*s
== '"')) || (!quote
&& isspace(*s
)))
237 return (len
>= SBUFSIZE
) ? 0 : len
;
242 dobase : converts a hex or octal ASCII string
243 to a binary address. Only HEX or OCT may be used
245 return length of binary string (in bytes), 0 if error.
246 The binary result is placed at buf.
250 dobase(s
, buf
, type
) /* read in an address */
251 char *s
, *buf
; /* source ASCII, result binary string */
255 int bp
= SBUFSIZE
- 1;
259 for (end
= s
; *end
&& ((type
== OCT
) ? isodigit(*end
) :
260 isxdigit(*end
)); ++end
) ;
262 /* any non-white, non-digits cause address to be rejected,
263 other fields are ignored */
265 if ((*s
== 0) || (end
== s
) || (!isspace(*end
) && *end
)) {
266 fprintf(stderr
, "dobase: Illegal trailer on address string\n");
274 while (bp
> 0 && end
>= s
) {
275 buf
[bp
] |= toxdigit(*end
) << shift
;
278 buf
[--bp
] = (todigit(*end
) >> (8 - shift
))
281 if ((shift
= (shift
+ 3) % 8) == 0)
285 if ((shift
= (shift
) ? 0 : 4) == 0)
290 fprintf(stderr
, "stoa: dobase: number to long\n");
294 /* need to catch end case to avoid extra 0's in front */
297 memcp(buf
, &buf
[bp
], (SBUFSIZE
- bp
));
298 return (SBUFSIZE
- bp
);
306 atos(str, addr, type)
308 convert address to ASCII form with address in hex, octal,
310 return pointer to buffer (NULL on failure).
315 atos(str
, addr
, type
)
321 int mystr
= 0; /* was str allocated here ? */
322 unsigned x_atos(), o_atos();
331 if ((str
= malloc(SBUFSIZE
)) == NULL
)
343 return xfer(str
, sbuf
, o_atos(sbuf
+2, addr
->buf
, addr
->len
) + 2,
344 mystr
? SBUFSIZE
: 0);
351 return xfer(str
, sbuf
, x_atos(sbuf
+2, addr
->buf
, addr
->len
) + 2,
352 mystr
? SBUFSIZE
: 0);
355 base
= xfer(str
, addr
->buf
,
356 addr
->len
+ 1, mystr
? SBUFSIZE
: 0);
358 base
[addr
->len
] = '\0'; /* terminate*/
369 return the number of bytes occupied by string + NULL*/
372 x_atos : convert an address string a, length s
385 *s
++ = itoac(((*a
>> 4) & MASK (4)));
386 *s
++ = itoac((*a
& MASK(4)));
395 o_atos : convert an address a, length l
396 to octal ASCII in s */
413 /* take care of partial bits and set shift factor for next 3 */
416 *s
++ = itoac((*a
>>(i
+5)) & MASK(3-i
));
421 *s
++ = itoac((*a
>> shift
) & MASK(3));
426 shift
+= (shift
< 3) ? 5 : -3;
429 i
= (*a
& MASK(shift
-5)) << (8-shift
);
430 i
|= (*++a
>> shift
) & MASK(8-shift
);
442 memcp(d
, s
, n
) /* safe memcpy for overlapping regions */
451 /* transfer block to a given destination or allocate one of the
453 if max = 0 : ignore max
457 xfer(dest
, src
, len
, max
)
461 if (max
&& dest
&& max
< len
) { /* No room */
462 fprintf(stderr
, "xfer: destination not long enough\n");
466 if ((dest
= (char *)malloc(len
)) == NULL
) {
467 fprintf(stderr
, "xfer: malloc failed\n");
471 memcpy(dest
, src
, (int)len
);
476 prescan: scan through string s, expanding all \$...\$
478 Return pointer to string of expanded text.
488 if (!s
|| !*s
|| !scan(s
))
496 scan: scan through string s, expanding all \$...\$.
498 Return 0 if anything failed, else 1.
508 int esc
= 0; /* Keep lookout for \\$ */
511 if (!esc
&& (*s
== '\\' && *(s
+1) == '$')) {
512 if (rec
++ == MAXRLEVEL
) {
513 fprintf(stderr
, "scan: Recursion \
514 level past %d on shell escape\n", rec
);
517 if ((cmd
= xcmd(s
+2, '$', &s
, &len
)) != NULL
) {
527 else if (sbp
== SBUFSIZE
) {
528 fprintf(stderr
, "Overflow on shell esc expansion\n");
531 else if (sbp
< SBUFSIZE
)
532 esc
= ((scanbuf
[sbp
++] = *s
++) == '\\');
539 xcmd : extract command line for shell escape and execute it
540 return pointer to output of command
545 char *s
; /* input string */
546 char ec
; /* escape char ( $ or ! ) */
547 char **ps
; /* address of input string pointer */
548 int *len
; /* Number of bytes of output from command */
553 FILE *pfp
; /* pipe for process */
554 char *cmd
; /* command buffer */
555 char *cmdp
; /* pointer along cmd */
556 char *ocmd
; /* output of command buffer */
557 int esc
= 0; /* escaped escape shell */
561 if ((cmd
= cmdp
= (char *)malloc(SBUFSIZE
)) == NULL
) {
562 fprintf(stderr
, "xcmd: malloc failed\n");
566 if ((ocmd
= (char *)malloc(SBUFSIZE
)) == NULL
) {
567 fprintf(stderr
, "xcmd: malloc failed\n");
572 if (!esc
&& *s
== '\\' && *(s
+1) == ec
) {
577 esc
= (*cmdp
++ = *s
++) == '\\';
582 if ((pfp
= popen(cmd
, "r")) == NULL
)
583 fprintf(stderr
, "xcmd: popen failed\n");
584 while (fread(&ocmd
[*len
], 1, 1, pfp
))
585 if ((*len
+= 1) >= SBUFSIZE
) {
586 fprintf(stderr
, "xcmd: command output too long\n");