1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
|
/****************************************************************
* *
* Copyright 2001, 2014 Fidelity Information Services, Inc *
* *
* This source code contains the intellectual property *
* of its copyright holder(s), and is made available *
* under a license. If you do not know the terms of *
* the license, please stop and do not read further. *
* *
****************************************************************/
/*
* -----------------------------------------------------
* Lexical analyzer routines for command line interpreter
* -----------------------------------------------------
*/
#include "mdef.h"
#include "gtm_ctype.h"
#include <errno.h>
#include "gtm_stdio.h"
#include "gtm_string.h"
#ifdef UNICODE_SUPPORTED
#include "gtm_icu_api.h"
#include "gtm_utf8.h"
#endif
#include "cli.h"
#include "eintr_wrappers.h"
#include "min_max.h"
GBLDEF char cli_token_buf[MAX_LINE + 1]; /* Token buffer */
GBLREF int cmd_cnt;
GBLREF char **cmd_arg;
GBLDEF boolean_t gtm_cli_interpret_string = TRUE;
GBLDEF IN_PARMS *cli_lex_in_ptr;
#ifdef UNICODE_SUPPORTED
GBLREF boolean_t gtm_utf8_mode;
#define CLI_GET_CHAR(PTR, BUFEND, CHAR) (gtm_utf8_mode ? UTF8_MBTOWC(PTR, BUFEND, CHAR) : (CHAR = (wint_t)*(PTR), (PTR) + 1))
#define CLI_PUT_CHAR(PTR, CHAR) (gtm_utf8_mode ? UTF8_WCTOMB(CHAR, PTR) : (*(PTR) = CHAR, (PTR) + 1))
#define CLI_ISSPACE(CHAR) (gtm_utf8_mode ? U_ISSPACE(CHAR) : ISSPACE_ASCII((int)CHAR))
#else
#define CLI_GET_CHAR(PTR, BUFEND, CHAR) (CHAR = (int)*(PTR), (PTR) + 1)
#define CLI_PUT_CHAR(PTR, CHAR) (*(PTR) = CHAR, (PTR) + 1)
#define CLI_ISSPACE(CHAR) ISSPACE_ASCII(CHAR)
#endif
static int tok_string_extract(void)
{
int token_len;
boolean_t have_quote, first_quote;
uchar_ptr_t in_sp, out_sp, in_next, last_in_next,
bufend; /* really one past last byte of buffer */
# ifdef UNICODE_SUPPORTED
wint_t ch;
# else
int ch;
# endif
assert(cli_lex_in_ptr);
in_sp = (uchar_ptr_t)cli_lex_in_ptr->tp;
bufend = (uchar_ptr_t)&cli_lex_in_ptr->in_str[0] + cli_lex_in_ptr->buflen;
out_sp = (uchar_ptr_t)cli_token_buf;
token_len = 0;
have_quote = FALSE;
in_next = CLI_GET_CHAR(in_sp, bufend, ch);
for ( ; ;)
{
/* '-' is not a token separator */
while (ch && !CLI_ISSPACE(ch))
{
last_in_next = in_next;
if (ch == '"')
{
if (!have_quote)
{
if (!gtm_cli_interpret_string)
{
out_sp = CLI_PUT_CHAR(out_sp, ch);
token_len++;
}
have_quote = TRUE;
in_next = CLI_GET_CHAR(in_next, bufend, ch);
} else
{
if (!gtm_cli_interpret_string)
{
out_sp = CLI_PUT_CHAR(out_sp, ch);
token_len++;
}
in_next = CLI_GET_CHAR(in_next, bufend, ch);
if (ch == '"')
{ /* double quote, one goes in string, still have quote */
out_sp = CLI_PUT_CHAR(out_sp, ch);
in_next = CLI_GET_CHAR(in_next, bufend, ch);
token_len++;
} else
have_quote = FALSE;
}
} else
{
out_sp = CLI_PUT_CHAR(out_sp, ch);
in_next = CLI_GET_CHAR(in_next, bufend, ch);
token_len++;
}
}
if (ch == '\0')
{
in_sp = last_in_next; /* Points to start of null char so scan ends next call */
break;
}
if (have_quote)
{
out_sp = CLI_PUT_CHAR(out_sp, ch);
in_next = CLI_GET_CHAR(in_next, bufend, ch);
token_len++;
continue;
}
in_sp = in_next;
break;
}
ch = 0;
out_sp = CLI_PUT_CHAR(out_sp, ch);
cli_lex_in_ptr->tp = (char *)in_sp;
return (token_len);
}
/*
* -------------------------
* Inintialize lexer
* -------------------------
*/
#ifdef __osf__
/* N.B. If the process is started by mumps, argv passed in from main (in gtm.c) is almost straight from the operating system.
* if the process is started externally (call-ins), argc and argv are 0 and NULL respectively */
#pragma pointer_size (save)
#pragma pointer_size (long)
#endif
void cli_lex_setup (int argc, char **argv)
{
int parmlen, parmindx;
char **parmptr;
# ifdef __osf__
# pragma pointer_size (restore)
# endif
# ifdef KEEP_zOS_EBCDIC
__argvtoascii_a(argc, argv);
# endif
cmd_cnt = argc;
cmd_arg = (char **)argv;
/* Quickly run through the parameters to get a ballpark on the
size of the string needed to store them.
*/
for (parmindx = 1, parmptr = argv, parmlen = 0; parmindx <= argc; parmptr++, parmindx++)
parmlen += STRLEN(*parmptr) + 1;
parmlen = parmlen + PARM_OVHD; /* Extraneous extras, etc. */
parmlen = (parmlen > MAX_LINE ? MAX_LINE : parmlen) + 1;
/* call-ins may repeatedly initialize cli_lex_setup for every invocation of gtm_init() */
if (!cli_lex_in_ptr || parmlen > cli_lex_in_ptr->buflen)
{ /* We have the cure for a missing or unusable buffer */
if (cli_lex_in_ptr)
free(cli_lex_in_ptr);
cli_lex_in_ptr = (IN_PARMS *)malloc(SIZEOF(IN_PARMS) + parmlen);
cli_lex_in_ptr->buflen = parmlen;
}
cli_lex_in_ptr->argc = argc;
cli_lex_in_ptr->argv = argv;
cli_lex_in_ptr->in_str[0] = '\0';
cli_lex_in_ptr->tp = NULL;
}
void cli_str_setup(int addrlen, char *addr)
{
int alloclen;
assert(cli_lex_in_ptr);
alloclen = (addrlen > MAX_LINE ? MAX_LINE : addrlen) + 1;
if (!cli_lex_in_ptr || alloclen > cli_lex_in_ptr->buflen)
{ /* We have the cure for a missing or unusable buffer */
if (cli_lex_in_ptr)
free(cli_lex_in_ptr);
cli_lex_in_ptr = (IN_PARMS *)malloc(SIZEOF(IN_PARMS) + alloclen);
cli_lex_in_ptr->buflen = alloclen;
}
cli_lex_in_ptr->argv = NULL;
cli_lex_in_ptr->argc = 0;
cli_lex_in_ptr->tp = cli_lex_in_ptr->in_str;
addrlen = MIN(addrlen, alloclen - 1);
memcpy(cli_lex_in_ptr->in_str, addr, addrlen);
(cli_lex_in_ptr->in_str)[addrlen] = '\0';
}
/*
* ---------------------------------------------------------------
* Convert string to upper case. Do it only for ascii characters.
* ---------------------------------------------------------------
*/
void cli_strupper(char *sp)
{
int c;
while (c = *sp)
*sp++ = TOUPPER(c);
}
/*
* -------------------------------------------------------
* Check if string is a Hex number
*
* Return:
* TRUE - identifier
* FALSE - otherwise
* -------------------------------------------------------
*/
int cli_is_hex(char *p)
{
if (('+' == *p) || ('-' == *p))
p++;
if (('0' == *p) && ('X' == TOUPPER(*(p + 1))))
{
p = p + 2;
}
while (*p && ISXDIGIT_ASCII(*p))
p++;
return ((*p) ? FALSE : TRUE);
}
/*
* -------------------------------------------------------
* Check if token is a qualifier
*
* Return:
* TRUE - qualifier
* FALSE - otherwise
* -------------------------------------------------------
*/
int cli_is_qualif(char *p)
{
return (*p == '-');
}
/*
* -------------------------------------------------------
* Check if token is an assignment symbol
*
* Return:
* TRUE - assignment
* FALSE - otherwise
* -------------------------------------------------------
*/
int cli_is_assign(char *p)
{
return (*p == '=');
}
/* ----------------------------------------------
* Routine to skip white space while reading.
* Called when a parameter has to be read.
* The tok_string_extract () doesnt remove
* starting spaces while reading a string.
* To make use of that while reading a parameter
* this has to be called first.
* ----------------------------------------------
*/
void skip_white_space(void)
{
uchar_ptr_t in_sp;
# ifdef UNICODE_SUPPORTED
wint_t ch;
uchar_ptr_t next_sp, bufend;
# endif
assert(cli_lex_in_ptr);
in_sp = (uchar_ptr_t)cli_lex_in_ptr->tp;
# ifdef UNICODE_SUPPORTED
if (gtm_utf8_mode)
{
bufend = (uchar_ptr_t)(cli_lex_in_ptr->in_str + cli_lex_in_ptr->buflen);
for ( ; ; )
{
next_sp = UTF8_MBTOWC(in_sp, bufend, ch);
if (!U_ISSPACE(ch))
break;
in_sp = next_sp;
}
}
else
#endif
while(ISSPACE_ASCII((int)*in_sp))
in_sp++;
cli_lex_in_ptr->tp = (char *)in_sp;
}
/*
* --------------------------------------------
* Extract one token from a string.
* Token is anything between the separator characters
* or separator character itself, if it is '-' or '='.
*
* Return:
* token Length
* --------------------------------------------
*/
static int tok_extract (void)
{
int token_len;
uchar_ptr_t in_sp, in_next, out_sp, bufend;
# ifdef UNICODE_SUPPORTED
wint_t ch;
# else
int ch;
# endif
assert(cli_lex_in_ptr);
skip_white_space(); /* Skip leading blanks */
in_sp = (uchar_ptr_t)cli_lex_in_ptr->tp;
bufend = (uchar_ptr_t)&cli_lex_in_ptr->in_str[0] + cli_lex_in_ptr->buflen;
out_sp = (uchar_ptr_t)cli_token_buf;
token_len = 0;
in_next = CLI_GET_CHAR(in_sp, bufend, ch);
if ('-' == ch || '=' == ch)
{
out_sp = CLI_PUT_CHAR(out_sp, ch);
in_sp = in_next; /* advance one character */
token_len = 1;
} else if (ch) /* only if something there */
{
/* smw if quotable, need to unicode isspace (BYPASSOK) */
/* '-' is not a token separator */
while(ch && !CLI_ISSPACE(ch)
&& ch != '=')
{
out_sp = CLI_PUT_CHAR(out_sp, ch);
in_sp = in_next;
in_next = CLI_GET_CHAR(in_next, bufend, ch);
token_len++;
}
}
ch = 0;
out_sp = CLI_PUT_CHAR(out_sp, ch);
cli_lex_in_ptr->tp = (char *)in_sp;
return(token_len);
}
static void cli_lex_in_expand(int in_len)
{
IN_PARMS *new_cli_lex_in_ptr;
new_cli_lex_in_ptr = (IN_PARMS *)malloc(SIZEOF(IN_PARMS) + in_len);
new_cli_lex_in_ptr->argc = cli_lex_in_ptr->argc;
new_cli_lex_in_ptr->argv = cli_lex_in_ptr->argv;
new_cli_lex_in_ptr->buflen = in_len; /* in_str[1] accounts for null */
free(cli_lex_in_ptr);
cli_lex_in_ptr = new_cli_lex_in_ptr;
}
char *cli_fgets(char *buffer, int buffersize, FILE *fp, boolean_t cli_lex_str)
{
size_t in_len;
char cli_fgets_buffer[MAX_LINE], *destbuffer, *retptr;
# ifdef UNICODE_SUPPORTED
int mbc_len, u16_off, destsize;
int32_t mbc_dest_len;
UErrorCode errorcode;
UChar *uc_fgets_ret;
UChar32 uc32_cp;
UChar cli_fgets_Ubuffer[MAX_LINE];
UFILE *u_fp;
# endif
# ifdef UNICODE_SUPPORTED
if (gtm_utf8_mode)
{
cli_fgets_Ubuffer[0] = 0;
if (!cli_lex_str)
assert(MAX_LINE >= buffersize);
u_fp = u_finit(fp, NULL, UTF8_NAME);
if (NULL != u_fp)
{
do
{ /* no f_ferror */
uc_fgets_ret = u_fgets(cli_fgets_Ubuffer,
(int32_t)(SIZEOF(cli_fgets_Ubuffer) / SIZEOF(UChar)) - 1, u_fp);
} while (NULL == uc_fgets_ret && !u_feof(u_fp) && ferror(fp) && EINTR == errno);
if (NULL == uc_fgets_ret)
{
if (cli_lex_str)
cli_lex_in_ptr->tp = NULL;
u_fclose(u_fp);
return NULL;
}
in_len = u_strlen(cli_fgets_Ubuffer);
in_len = trim_U16_line_term(cli_fgets_Ubuffer, (int)in_len);
for (u16_off = 0, mbc_len = 0; u16_off < in_len; )
{
U16_NEXT(cli_fgets_Ubuffer, u16_off, in_len, uc32_cp);
mbc_len += U8_LENGTH(uc32_cp);
if (!cli_lex_str && mbc_len >= buffersize)
{ /* can't expand */
mbc_len = buffersize - 1;
cli_fgets_Ubuffer[u16_off] = 0;
U16_BACK_1(cli_fgets_Ubuffer, 0, u16_off);
in_len = u16_off + 1; /* offset to length */
break;
}
}
if (cli_lex_str)
{
if (mbc_len > cli_lex_in_ptr->buflen)
cli_lex_in_expand(mbc_len); /* for terminating null */
destsize = cli_lex_in_ptr->buflen + 1;
destbuffer = cli_lex_in_ptr->in_str;
} else
{ /* very unlikely parm is larger than MAX_LINE even i UTF-8 */
if (mbc_len >= buffersize)
destsize = buffersize - 1; /* for null */
else
destsize = buffersize;
destbuffer = buffer;
}
errorcode = U_ZERO_ERROR;
u_strToUTF8(destbuffer, destsize, &mbc_dest_len, cli_fgets_Ubuffer, (int4)in_len + 1, &errorcode);
if (U_FAILURE(errorcode))
if (U_BUFFER_OVERFLOW_ERROR == errorcode)
{ /* truncate so null terminated */
destbuffer[destsize - 1] = 0;
retptr = destbuffer;
} else
retptr = NULL;
else
retptr = destbuffer; /* Repoint to new home */
if (cli_lex_str)
cli_lex_in_ptr->tp = retptr;
u_fclose(u_fp);
} else if (cli_lex_str)
cli_lex_in_ptr->tp = NULL;
} else
{
# endif
cli_fgets_buffer[0] = '\0';
FGETS_FILE(cli_fgets_buffer, SIZEOF(cli_fgets_buffer), fp, retptr);
if (NULL != retptr)
{
in_len = strlen(cli_fgets_buffer);
if (cli_lex_str)
{
if (cli_lex_in_ptr->buflen < in_len)
cli_lex_in_expand((int)in_len);
destbuffer = cli_lex_in_ptr->in_str;
} else
{
assert(SIZEOF(cli_fgets_buffer) >= buffersize);
destbuffer = buffer;
}
retptr = destbuffer; /* return proper buffer */
if ('\n' == cli_fgets_buffer[in_len - 1])
cli_fgets_buffer[in_len - 1] = '\0'; /* replace NL */
memcpy(destbuffer, cli_fgets_buffer, in_len);
if (cli_lex_str)
cli_lex_in_ptr->tp = destbuffer;
} else if (cli_lex_str)
cli_lex_in_ptr->tp = NULL;
# ifdef UNICODE_SUPPORTED
}
# endif
return retptr;
}
/*
* -------------------------------------------------------
* Get token
*
* Return:
* Token Length
*
* Side effects:
* set eof to <> 0 for EOF condition.
* -------------------------------------------------------
*/
int cli_gettoken (int *eof)
{
int arg_no, token_len, in_len;
char *from, *to;
IN_PARMS *new_cli_lex_in_ptr;
char *tmp_tp;
assert(cli_lex_in_ptr);
/* Reading from program argument list */
if (cli_lex_in_ptr->argc > 1 && cli_lex_in_ptr->tp == 0)
{
cli_lex_in_ptr->tp = cli_lex_in_ptr->in_str;
arg_no = 1;
/* convert arguments into array */
while(arg_no < cli_lex_in_ptr->argc)
{
if (arg_no > 1)
strcat(cli_lex_in_ptr->in_str, " ");
if (strlen(cli_lex_in_ptr->in_str)
+ strlen(cli_lex_in_ptr->argv[arg_no]) > MAX_LINE)
break;
strcat(cli_lex_in_ptr->in_str, cli_lex_in_ptr->argv[arg_no++]);
}
}
if (NULL == cli_lex_in_ptr->tp || strlen(cli_lex_in_ptr->tp) < 1)
{
cli_token_buf[0] = '\0';
/* cli_fgets can malloc/free cli_lex_in_ptr. Passing in TRUE as last parameter will do the set
* to cli_lex_in_ptr->tp within cli_fgets() after any malloc/free, thus avoiding the problem of
* writing to freed memory if the set were done here.
*/
cli_fgets(cli_lex_in_ptr->in_str, MAX_LINE, stdin, TRUE);
if (NULL != cli_lex_in_ptr->tp)
*eof = 0;
else
{
*eof = EOF;
return (0);
}
}
token_len = tok_extract();
*eof = (cli_lex_in_ptr->argc > 1 && token_len == 0);
return token_len;
}
/*
* --------------------------------------------
* Copy next token to the token buffer.
* Do not advance the token pointer.
*
* Return:
* Token Length
*
* Side effects:
* set eof to <> 0 for EOF condition.
* -------------------------------------------------------
*/
int cli_look_next_token(int *eof)
{
int tok_len;
char *old_tp;
assert(cli_lex_in_ptr);
if (((char *) NULL == cli_lex_in_ptr->tp) || (!strlen(cli_lex_in_ptr->tp)))
return(0);
old_tp = cli_lex_in_ptr->tp;
tok_len = cli_gettoken(eof);
cli_lex_in_ptr->tp = old_tp;
return(tok_len);
}
int cli_look_next_string_token(int *eof)
{
int tok_len;
char *old_tp;
assert(cli_lex_in_ptr);
if (!strlen(cli_lex_in_ptr->tp))
return(0);
old_tp = cli_lex_in_ptr->tp;
tok_len = cli_get_string_token(eof);
cli_lex_in_ptr->tp = old_tp;
return(tok_len);
}
int cli_get_string_token(int *eof)
{
int arg_no, token_len, in_len;
char *from, *to;
IN_PARMS *new_cli_lex_in_ptr;
assert(cli_lex_in_ptr);
/* Reading from program argument list */
if (cli_lex_in_ptr->argc > 1 && cli_lex_in_ptr->tp == 0)
{
cli_lex_in_ptr->tp = cli_lex_in_ptr->in_str;
arg_no = 1;
/* convert arguments into array */
while(arg_no < cli_lex_in_ptr->argc)
{
if (arg_no > 1)
strcat(cli_lex_in_ptr->in_str, " ");
if (strlen(cli_lex_in_ptr->in_str) + strlen(cli_lex_in_ptr->argv[arg_no]) > MAX_LINE)
break;
if (cli_has_space(cli_lex_in_ptr->argv[arg_no]))
{
from = cli_lex_in_ptr->argv[arg_no++];
to = cli_lex_in_ptr->in_str + strlen(cli_lex_in_ptr->in_str) - 1;
*to++ = '\"';
while(*from != '\0')
{
if ('\"' == *from)
*to++ = *from;
*to++ = *from++;
}
*to++ = '\"';
*to = '\0';
} else
strcat(cli_lex_in_ptr->in_str, cli_lex_in_ptr->argv[arg_no++]);
}
}
if (NULL == cli_lex_in_ptr->tp || strlen(cli_lex_in_ptr->tp) < 1)
{
cli_token_buf[0] = '\0';
/* cli_fgets can malloc/free cli_lex_in_ptr. Passing in TRUE as last parameter will do the set
* to cli_lex_in_ptr->tp within cli_fgets() after any malloc/free, thus avoiding the problem of
* writing to freed memory if the set were done here.
*/
cli_fgets(cli_lex_in_ptr->in_str, MAX_LINE, stdin, TRUE);
if (NULL != cli_lex_in_ptr->tp)
*eof = 0;
else
{
*eof = EOF;
return (0);
}
}
token_len = tok_string_extract();
*eof = (cli_lex_in_ptr->argc > 1 && token_len == 0);
return token_len;
}
/*
* -------------------------------------------------------
* Check if string has space in it
*
* Return:
* TRUE - identifier
* FALSE - otherwise
* -------------------------------------------------------
*/
int cli_has_space(char *p)
{
# ifdef UNICODE_SUPPORTED
uchar_ptr_t local_p, next_p, bufend;
wint_t ch;
if (gtm_utf8_mode)
{
local_p = (uchar_ptr_t)p;
bufend = local_p + strlen(p);
while (local_p)
{
next_p = UTF8_MBTOWC(local_p, bufend, ch);
if (!ch || U_ISSPACE(ch))
break;
local_p = next_p;
}
p = (char *)local_p;
}
else
# endif
while (*p && !ISSPACE_ASCII(*p))
p++;
return ((*p) ? (TRUE) : (FALSE));
}
|