2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: exksh_prpar.c /main/4 1995/11/01 15:54:12 rswiston $ */
25 /* Copyright (c) 1991, 1992 UNIX System Laboratories, Inc. */
26 /* All Rights Reserved */
28 /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF */
29 /* UNIX System Laboratories, Inc. */
30 /* The copyright notice above does not evidence any */
31 /* actual or intended publication of such source code. */
36 #include <sys/types.h>
42 #include "exksh_prpar.h"
48 static int xk_uppercase(
58 struct symlist Val_list;
60 unsigned int Pr_format = PRSYMBOLIC|PRMIXED|PRNAMES;
64 * Takes a pointer to a pointer to a character buffer, and skips any
65 * whitespace, as defined by isspace(section 3). Increments the
66 * buf parameter to the first non-whitespace character.
67 * Returns SUCCESS, there are no known ways for it to fail.
74 while (isspace(**buf))
85 while (isspace(**buf)) {
89 if ((*buf)[0] == '\\')
96 * Takes a pointer to a character buffer and a string. Sees if
97 * the str is present as the first part of the buffer (minus any
98 * whitespace), and if so increments the buffer past the string.
99 * If not, returns FAIL without incrementing buffer (except perhaps
100 * by eating leading whitespace).
108 RIF(xk_skipwhite(buf));
109 if (strncmp(*buf, str, strlen(str)) == 0) {
118 * Takes a pointer to a char buffer, and a string. Returns
119 * TRUE if the string appears immediately (after skipping whitespace).
120 * or FALSE otherwise.
128 RIF(xk_skipwhite(buf));
129 if (strncmp(*buf, str, strlen(str)) == 0)
139 unsigned long *old_v )
142 register int i, printed = 0;
148 v = *((unsigned char *) old_v);
151 v = *((unsigned short *) old_v);
154 v = *((unsigned int *) old_v);
160 if ((Pr_format & PRSYMBOLIC) && ((Val_list.syms != NULL) || ((sym = fsymbolic(tbl)) != NULL))) {
161 if (Val_list.syms != NULL)
165 *buf += lsprintf(*buf, "0");
168 for (i = 0; i < sym->nsyms; i++) {
169 if (sym->syms[i].addr & v) {
170 if (Pr_format & PRMIXED_SYMBOLIC) {
171 if (Pr_format & PRDECIMAL)
172 *buf += lsprintf(*buf, "%s%s(%d)", printed ? "|" : "", sym->syms[i].str, sym->syms[i].addr);
174 *buf += lsprintf(*buf, "%s%s(0x%x)", printed ? "|" : "", sym->syms[i].str, sym->syms[i].addr);
177 *buf += lsprintf(*buf, "%s%s", printed ? "|" : "", sym->syms[i].str);
178 v &= ~(sym->syms[i].addr);
183 if (Pr_format & PRMIXED_SYMBOLIC) {
184 if (Pr_format & PRDECIMAL)
185 *buf += lsprintf(*buf, "%sNOSYMBOLIC(%d)", printed ? "|" : "", v);
187 *buf += lsprintf(*buf, "%sNOSYMBOLIC(0x%x)", printed ? "|" : "", v);
190 if (Pr_format & PRDECIMAL)
191 *buf += lsprintf(*buf, "%s%d", printed ? "|" : "", v);
193 *buf += lsprintf(*buf, "%s0x%x", printed ? "|" : "", v);
199 for (i = 0; i < sym->nsyms; i++) {
200 if (sym->syms[i].addr == v) {
201 if (Pr_format & PRMIXED_SYMBOLIC) {
202 if (Pr_format & PRDECIMAL)
203 *buf += lsprintf(*buf, "%s(%d)", sym->syms[i].str, v);
205 *buf += lsprintf(*buf, "%s(0x%x)", sym->syms[i].str, v);
208 *buf += lsprintf(*buf, "%s", sym->syms[i].str);
214 if (Pr_format & PRHEX)
215 *buf += lsprintf(*buf, "0x%x", v);
216 else if (Pr_format & PRDECIMAL)
217 *buf += lsprintf(*buf, "%d", v);
219 *buf += lsprintf(*buf, "%d(0x%x)", v, v);
227 struct envsymbols *env )
229 register int ret, base;
234 strncpy(nbuf, *buf, sizeof(nbuf)-1);
235 if (strchr(nbuf, '|') == NULL) {
236 for (p = nbuf; *p && *p != ' ' && *p != ',' && *p != ']'
237 && *p != '{' && *p != '}' && *p != '/' && *p != '@'
238 && *p != ':' && *p != '.' && *p != 13 && *p != 10 && *p != 11
239 && *p != 12 && *p != 9; p++)
244 for (p = nbuf; *p && *p != ','
245 && *p != '{' && *p != '}' && *p != '/' && *p != '@'
246 && *p != ':' && *p != '.' ; p++)
256 if ((p = strchr(nbuf, '"')) != NULL) {
259 if ((p = strchr(nbuf, '+')) != NULL) {
267 xk_par_int(&qq, &v1, env);
268 xk_par_int(&p, &v2, env);
273 if ((p = strchr(&nbuf[1], '-')) != NULL) {
281 xk_par_int(&qq, &v1, env);
282 xk_par_int(&p, &v2, env);
287 for (p = strtok(nbuf, " |\t\n"); p; p = strtok(NULL, " |\t\n")) {
288 for (pp = p; *pp && *pp != ' ' && *pp != ','
289 && *pp != '{' && *pp != '}' && *pp != '/' && *pp != '@'
290 && *pp != ':' && *pp != '.' && *pp != 13 &&
291 *pp != 11 && *pp != 12 && *pp != 9; pp++)
294 if ((pp = strchr(p, '#')) != NULL) {
295 base = strtol(p, &p, 10);
305 if (isdigit(*p) || *p == '-') {
306 *v |= strtoul(p, (char **)NULL, base);
311 /* knock out commentary parenthesized things */
312 if ((q = strchr(p, '(' /*)*/ )) != NULL)
314 /* Search through available names for partial match */
315 if (!fdef(p, &val)) {
332 return(xk_prin_charstr(buf, (unsigned char *)str,
333 str ? strlen(str) : 0));
345 *buf += lsprintf(*buf, "NULL");
347 *buf += lsprintf(*buf, "\"");
348 for (i = 0; i < len; i++) {
350 *buf += lsprintf(*buf, "\\\"");
351 } else if (isprint(str[i])) {
352 *buf += lsprintf(*buf, "%c", str[i]);
356 *buf += lsprintf(*buf, "\\n");
359 *buf += lsprintf(*buf, "\\t");
362 *buf += lsprintf(*buf, "\\b");
365 *buf += lsprintf(*buf, "\\v");
368 *buf += lsprintf(*buf, "\\f");
371 *buf += lsprintf(*buf, "\\r");
374 *buf += lsprintf(*buf, "\\00");
377 *buf += lsprintf(*buf, "\\%x", (unsigned int)str[i]);
382 *buf += lsprintf(*buf, "\"");
397 *buf += lsprintf(*buf, "NULL");
399 *buf += lsprintf(*buf, "%s", "0x");
400 for (i = 0; i < len; i++) {
403 *buf += lsprintf(*buf, "%x", tempc);
406 *buf += lsprintf(*buf, "0%x", tempc);
412 #define MALSIZ 16 /* initial size of string to malloc */
420 return(xk_par_charstr(buf, &str, len));
423 #define CHAR_QUOTED 0
424 #define CHAR_HEXSTR 1
434 RIF(xk_par_charstr(buf, str, &temp));
448 int didmalloc = FALSE, getmode;
449 char cbuf[3]; /* conversion buffer for hex strings */
453 RIF(xk_skipwhite(buf));
454 if (xk_parpeek(buf, "NULL")) {
455 RIF(xk_parexpect(buf, "NULL"));
460 /* this is pure internal feature, no error setting */
470 for (p = &filename[0];
471 **buf != ',' && **buf != /* { */ '}' && **buf != ' ' &&
472 **buf != '\t' && p < &filename[sizeof(filename)];
476 if ((fp = fopen(filename, "r")) == NULL) {
477 errmsg=strdup(GETMESSAGE(7,1,
478 "Unable to open the file '%s'; verify that it exists and is readable"));
479 lsprintf(gbuf, errmsg, filename);
480 fprintf(stderr, gbuf);
485 if (getsize(fileno(fp), &size) == FAIL) {
486 errmsg=strdup(GETMESSAGE(7,2,
487 "Unable to access the file '%s'; verify you have permission to access it"));
488 lsprintf(gbuf, errmsg, filename);
489 fprintf(stderr, gbuf);
494 if ((*str = malloc(*len)) == NULL) {
499 while (fgets(gbuf, sizeof(gbuf), fp) != NULL) {
502 /* eat any leading 0x */
503 if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
506 for ( ; *p && *p != '\n'; ) {
515 if (!isxdigit(p[1])) {
522 str[0][i++] = (char)strtol(cbuf, (char **)NULL, 16);
530 } else if (!ispunct(**buf)) {
531 getmode = CHAR_HEXSTR;
532 if ((*buf)[0] == '0' && ((*buf)[1] == 'x' || (*buf)[1] == 'X'))
536 getmode = CHAR_QUOTED;
539 if ((*str = malloc(MALSIZ)) == NULL) {
546 while ((*buf)[0] != '\0' && ((getmode == CHAR_QUOTED && (*buf)[0] != delim) ||
547 (getmode == CHAR_HEXSTR && (isxdigit((*buf)[0]))) ||
548 (getmode == CHAR_HEXSTR && (isspace((*buf)[0]))))) {
549 /* NOTE: must always leave 1 additional byte for a null
550 * termination, because could be called by xk_par_nts!
553 if (didmalloc == FALSE) {
556 if ((*str = realloc(*str, *len + MALSIZ)) == NULL) {
562 if (getmode == CHAR_QUOTED) {
563 if ((*buf)[0] == '\\') {
591 str[0][i++] = (char)strtol(*buf, buf, 8);
597 str[0][i++] = *(*buf)++;
600 str[0][i++] = *(*buf)++;
602 if (!isxdigit((*buf)[1])) {
608 str[0][i++] = (char)strtol(cbuf, (char **)NULL, 16);
613 if (getmode == CHAR_QUOTED)
614 (*buf)++; /* eat the trailing quote */
616 * NOTE: We leave a malloced buffer the same size rather
617 * than realloc()'ing it to be the exact size in order
618 * to save time and avoid malloc arena fragmentation
624 /* Case Ignoring String Functions. */
645 for (i=0; i < len && s1[i] != '\0' && s2[i] != '\0'; i++)
646 if ((diff = tolower(s1[i]) - tolower(s2[i])) != 0)
648 return(i == len ? 0 : s1[i] - s2[i]);
658 if (fstat(fd, &stat) == FAIL)
660 *psize = stat.st_size;