summaryrefslogtreecommitdiffstats
path: root/test/ruby/test_system.rb
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-10-17 13:17:35 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-10-17 13:17:35 +0000
commit196ca734e718f87624939b81183a828334209e67 (patch)
tree674c2c3c8f97834a83d4129708e51042d6467133 /test/ruby/test_system.rb
parent273fffbe3e5ae5e941c91b6b0f0d497b9197f15e (diff)
downloadruby-196ca734e718f87624939b81183a828334209e67.tar.gz
ruby-196ca734e718f87624939b81183a828334209e67.tar.xz
ruby-196ca734e718f87624939b81183a828334209e67.zip
* lib/set.rb: Reword and fix Overview.
* lib/set.rb: It is not necessary to require 'test/unit/ui/console/testrunner'. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@4796 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_system.rb')
0 files changed, 0 insertions, 0 deletions
36'>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
/* cfgtok.c - helper class to tokenize an input stream - which surprisingly
 * currently does not work with streams but with string. But that will
 * probably change over time ;) This class was originally written to support
 * the expression module but may evolve when (if) the expression module is
 * expanded (or aggregated) by a full-fledged ctoken based config parser.
 * Obviously, this class is used together with config files and not any other
 * parse function.
 *
 * Module begun 2008-02-19 by Rainer Gerhards
 *
 * This file is part of the rsyslog runtime library.
 *
 * The rsyslog runtime library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The rsyslog runtime library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the rsyslog runtime library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * A copy of the GPL can be found in the file "COPYING" in this distribution.
 * A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution.
 */

#include "config.h"
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include <assert.h>

#include "rsyslog.h"
#include "template.h"
#include "ctok.h"

/* static data */
DEFobjStaticHelpers
DEFobjCurrIf(ctok_token)
DEFobjCurrIf(var)


/* Standard-Constructor
 */
BEGINobjConstruct(ctok) /* be sure to specify the object type also in END macro! */
ENDobjConstruct(ctok)


/* ConstructionFinalizer
 * rgerhards, 2008-01-09
 */
rsRetVal ctokConstructFinalize(ctok_t __attribute__((unused)) *pThis)
{
	DEFiRet;
	RETiRet;
}


/* destructor for the ctok object */
BEGINobjDestruct(ctok) /* be sure to specify the object type also in END and CODESTART macros! */
CODESTARTobjDestruct(ctok)
	/* ... then free resources */
ENDobjDestruct(ctok)


/* unget character from input stream. At most one character can be ungotten.
 * This funtion is only permitted to be called after at least one character
 * has been read from the stream. Right now, we handle the situation simply by
 * moving the string "stream" pointer one position backwards. If we work with
 * real streams (some time), the strm object will handle the functionality
 * itself. -- rgerhards, 2008-02-19
 */
static rsRetVal
ctokUngetCharFromStream(ctok_t *pThis, uchar __attribute__((unused)) c)
{
	DEFiRet;

	ISOBJ_TYPE_assert(pThis, ctok);
	--pThis->pp;

	RETiRet;
}


/* get the next character from the input "stream" (currently just a in-memory
 * string...) -- rgerhards, 2008-02-19
 */
static rsRetVal 
ctokGetCharFromStream(ctok_t *pThis, uchar *pc)
{
	DEFiRet;

	ISOBJ_TYPE_assert(pThis, ctok);
	ASSERT(pc != NULL);

	/* end of string or begin of comment terminates the "stream" */
	if(*pThis->pp == '\0' || *pThis->pp == '#') {
		ABORT_FINALIZE(RS_RET_EOS);
	} else {
		*pc = *pThis->pp;
		++pThis->pp;
	}

finalize_it:
	RETiRet;
}


/* skip whitespace in the input "stream".
 * rgerhards, 2008-02-19
 */
static rsRetVal 
ctokSkipWhitespaceFromStream(ctok_t *pThis)
{
	DEFiRet;
	uchar c;

	ISOBJ_TYPE_assert(pThis, ctok);

	CHKiRet(ctokGetCharFromStream(pThis, &c));
	while(isspace(c)) {
		CHKiRet(ctokGetCharFromStream(pThis, &c));
	}

	/* we must unget the one non-whitespace we found */
	CHKiRet(ctokUngetCharFromStream(pThis, c));

dbgprintf("skipped whitepsace, stream now '%s'\n", pThis->pp);
finalize_it:
	RETiRet;
}


/* get the next word from the input "stream" (currently just a in-memory
 * string...). A word is anything from the current location until the
 * first non-alphanumeric character. If the word is longer
 * than the provided memory buffer, parsing terminates when buffer length
 * has been reached. A buffer of 128 bytes or more should always be by
 * far sufficient. -- rgerhards, 2008-02-19
 */
static rsRetVal 
ctokGetWordFromStream(ctok_t *pThis, uchar *pWordBuf, size_t lenWordBuf)
{
	DEFiRet;
	uchar c;

	ISOBJ_TYPE_assert(pThis, ctok);
	ASSERT(pWordBuf != NULL);
	ASSERT(lenWordBuf > 0);

	CHKiRet(ctokSkipWhitespaceFromStream(pThis));

	CHKiRet(ctokGetCharFromStream(pThis, &c));
	while((isalnum(c) || c == '_' || c == '-') && lenWordBuf > 1) {
		*pWordBuf++ = c;
		--lenWordBuf;
		CHKiRet(ctokGetCharFromStream(pThis, &c));
	}
	*pWordBuf = '\0'; /* there is always space for this - see while() */

	/* push back the char that we have read too much */
	CHKiRet(ctokUngetCharFromStream(pThis, c));

finalize_it:
	RETiRet;
}


/* read in a constant number
 * This is the "number" ABNF element
 * rgerhards, 2008-02-19
 */
static rsRetVal
ctokGetNumber(ctok_t *pThis, ctok_token_t *pToken)
{
	DEFiRet;
	number_t n; /* the parsed number */
	uchar c;
	int valC;
	int iBase;

	ISOBJ_TYPE_assert(pThis, ctok);
	ASSERT(pToken != NULL);

	pToken->tok = ctok_NUMBER;

	CHKiRet(ctokGetCharFromStream(pThis, &c));
	if(c == '0') { /* octal? */
		CHKiRet(ctokGetCharFromStream(pThis, &c));
		if(c == 'x') { /* nope, hex! */
			CHKiRet(ctokGetCharFromStream(pThis, &c));
			c = tolower(c);
			iBase = 16;
		} else {
			iBase = 8;
		}
	} else {
		iBase = 10;
	}
		
	n = 0;
	/* this loop is quite simple, a variable name is terminated by whitespace. */
	while(isdigit(c) || (c >= 'a' && c <= 'f')) {
		if(isdigit(c)) {
			valC = c - '0';
		} else {
			valC = c - 'a' + 10;
		}
		
		if(valC >= iBase) {
			if(iBase == 8) {
				ABORT_FINALIZE(RS_RET_INVALID_OCTAL_DIGIT);
			} else {
				ABORT_FINALIZE(RS_RET_INVALID_HEX_DIGIT);
			}
		}
		/* we now have the next value and know it is right */
		n = n * iBase + valC;
		CHKiRet(ctokGetCharFromStream(pThis, &c));
		c = tolower(c);
	}

	/* we need to unget the character that made the loop terminate */
	CHKiRet(ctokUngetCharFromStream(pThis, c));

	CHKiRet(var.SetNumber(pToken->pVar, n));

finalize_it:
	RETiRet;
}


/* read in a variable
 * This covers both msgvar and sysvar from the ABNF.
 * rgerhards, 2008-02-19
 */
static rsRetVal
ctokGetVar(ctok_t *pThis, ctok_token_t *pToken)
{
	DEFiRet;
	uchar c;
	cstr_t *pstrVal;

	ISOBJ_TYPE_assert(pThis, ctok);
	ASSERT(pToken != NULL);

	CHKiRet(ctokGetCharFromStream(pThis, &c));

	if(c == '$') { /* second dollar, we have a system variable */
		pToken->tok = ctok_SYSVAR;
		CHKiRet(ctokGetCharFromStream(pThis, &c)); /* "eat" it... */
	} else {
		pToken->tok = ctok_MSGVAR;
	}

	CHKiRet(rsCStrConstruct(&pstrVal));
	/* this loop is quite simple, a variable name is terminated by whitespace. */
	while(!isspace(c)) {
		CHKiRet(rsCStrAppendChar(pstrVal, tolower(c)));
		CHKiRet(ctokGetCharFromStream(pThis, &c));
	}
	CHKiRet(rsCStrFinish(pStrB));

	CHKiRet(var.SetString(pToken->pVar, pstrVal));
	pstrVal = NULL;

finalize_it:
	if(iRet != RS_RET_OK) {
		if(pstrVal != NULL) {
			rsCStrDestruct(&pstrVal);
		}
	}

	RETiRet;
}


/* read in a simple string (simpstr in ABNF)
 * rgerhards, 2008-02-19
 */
static rsRetVal
ctokGetSimpStr(ctok_t *pThis, ctok_token_t *pToken)
{
	DEFiRet;
	uchar c;
	int bInEsc = 0;
	cstr_t *pstrVal;

	ISOBJ_TYPE_assert(pThis, ctok);
	ASSERT(pToken != NULL);

	pToken->tok = ctok_SIMPSTR;

	CHKiRet(rsCStrConstruct(&pstrVal));
	CHKiRet(ctokGetCharFromStream(pThis, &c));
	/* while we are in escape mode (had a backslash), no sequence
	 * terminates the loop. If outside, it is terminated by a single quote.
	 */
	while(bInEsc || c != '\'') {
		if(bInEsc) {
			CHKiRet(rsCStrAppendChar(pstrVal, c));
			bInEsc = 0;
		} else {
			if(c == '\\') {
				bInEsc = 1;
			} else {
				CHKiRet(rsCStrAppendChar(pstrVal, c));
			}
		}
		CHKiRet(ctokGetCharFromStream(pThis, &c));
	}
	CHKiRet(rsCStrFinish(pStrB));

	CHKiRet(var.SetString(pToken->pVar, pstrVal));
	pstrVal = NULL;

finalize_it:
	if(iRet != RS_RET_OK) {
		if(pstrVal != NULL) {
			rsCStrDestruct(&pstrVal);
		}
	}

	RETiRet;
}


/* Unget a token. The token ungotten will be returned the next time
 * ctokGetToken() is called. Only one token can be ungotten at a time.
 * If a second token is ungotten, the first is lost. This is considered
 * a programming error.
 * rgerhards, 2008-02-20
 */
static rsRetVal
ctokUngetToken(ctok_t *pThis, ctok_token_t *pToken)
{
	DEFiRet;

	ISOBJ_TYPE_assert(pThis, ctok);
	ASSERT(pToken != NULL);
	ASSERT(pThis->pUngotToken == NULL);

	pThis->pUngotToken = pToken;

	RETiRet;
}


/* skip an inine comment (just like a C-comment) 
 * rgerhards, 2008-02-20
 */
static rsRetVal
ctokSkipInlineComment(ctok_t *pThis)
{
	DEFiRet;
	uchar c;
	int bHadAsterisk = 0;

	ISOBJ_TYPE_assert(pThis, ctok);

	CHKiRet(ctokGetCharFromStream(pThis, &c)); /* read a charater */
	while(!(bHadAsterisk && c == '/')) {
		bHadAsterisk = (c == '*') ? 1 : 0;
		CHKiRet(ctokGetCharFromStream(pThis, &c)); /* read next */
	}

finalize_it:
	RETiRet;
}



/* Get the *next* token from the input stream. This parses the next token and
 * ignores any whitespace in between. End of stream is communicated via iRet.
 * The returned token must either be destructed by the caller OR being passed
 * back to ctokUngetToken().
 * rgerhards, 2008-02-19
 */
static rsRetVal
ctokGetToken(ctok_t *pThis, ctok_token_t **ppToken)
{
	DEFiRet;
	ctok_token_t *pToken;
	uchar c;