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
|
/*
* nsfError.c --
*
* Error reporting functions for the Next Scripting Framework.
*
* Copyright (C) 1999-2018 Gustaf Neumann (a, b)
* Copyright (C) 1999-2007 Uwe Zdun (a, b)
* Copyright (C) 2011-2016 Stefan Sobernig (b)
*
* (a) University of Essen
* Specification of Software Systems
* Altendorferstrasse 97-101
* D-45143 Essen, Germany
*
* (b) Vienna University of Economics and Business
* Institute of Information Systems and New Media
* A-1020, Welthandelsplatz 1
* Vienna, Austria
*
* This work is licensed under the MIT License http://www.opensource.org/licenses/MIT
*
* Copyright:
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#include "nsfInt.h"
/* function prototypes */
Tcl_Obj *NsfParamDefsSyntax(
Tcl_Interp *interp, Nsf_Param const *paramsPtr,
NsfObject *contextObject, const char *pattern
) nonnull(1) nonnull(2) returns_nonnull;
/*
*----------------------------------------------------------------------
*
* NsfDStringVPrintf --
*
* Appends a formatted value to a Tcl_DString. This function
* continues until having allocated sufficient memory.
*
* Note: The current implementation assumes C99 compliant
* implementations of vs*printf() for all runtimes other than
* MSVC. For MSVC, the pre-C99 vs*printf() implementations are
* explicitly set by Tcl internals (see tclInt.h). For
* MinGW/MinGW-w64, __USE_MINGW_ANSI_STDIO must be set (see
* nsfInt.h).
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
NsfDStringVPrintf(Tcl_DString *dsPtr, const char *fmt, va_list argPtr) {
int result, avail, offset;
va_list argPtrCopy;
bool failure;
/*
* Tcl_DStringLength returns the current length *without* the
* terminating character (NTC).
*/
offset = Tcl_DStringLength(dsPtr);
#if defined(_MSC_VER)
/*
* Pre-C99: currently free storage, excluding NTC
*/
avail = dsPtr->spaceAvl - offset - 1;
#else
/*
* C99: currently free storage, including NTC
*/
avail = dsPtr->spaceAvl - offset;
#endif
/*
* 1) Copy va_list so that the caller's copy is untouched.
* 2) Run vsnprintf() eagerly.
*/
va_copy(argPtrCopy, argPtr);
result = vsnprintf(dsPtr->string + offset, (size_t)avail, fmt, argPtrCopy);
va_end(argPtrCopy);
#if defined(_MSC_VER)
/*
* vs*printf() in pre-C99 runtimes (MSVC up to VS13, VS15 and newer
* in backward-compat mode) return -1 upon overflowing the buffer.
*
* Note: Tcl via tclInt.h precludes the use of pre-C99 mode even in
* VS15 and newer (vsnprintf points to backwards compatible, pre-C99
* _vsnprintf).
*/
failure = (result == -1);
#else
/*
* vs*printf() in C99 compliant runtimes (GCC, CLANG, MSVC in VS15
* and newer, MinGW/MinGW-w64 with __USE_MINGW_ANSI_STDIO) returns
* the number of chars to be written if the buffer would be
* sufficiently large (excluding NTC, the terminating null
* character). A return value of -1 signals an encoding error.
*/
assert(result > -1); /* no encoding error */
failure = (result >= (int)avail);
#endif
if (likely(! failure)) {
/*
* vsnprintf() copied all content, adjust the Tcl_DString length.
*/
Tcl_DStringSetLength(dsPtr, offset + result);
} else {
int addedStringLength;
/*
* vsnprintf() could not copy all content, content was truncated.
* Determine the required length (for MSVC), adjust the Tcl_DString
* size, and copy again.
*/
#if defined(_MSC_VER)
va_copy(argPtrCopy, argPtr);
addedStringLength = _vscprintf(fmt, argPtrCopy);
va_end(argPtrCopy);
#else
addedStringLength = result;
#endif
Tcl_DStringSetLength(dsPtr, offset + addedStringLength);
#if defined(_MSC_VER)
/*
* Pre-C99: currently free storage, excluding NTC
*/
avail = dsPtr->spaceAvl - offset - 1;
#else
/*
* C99: currently free storage, including NTC
*/
avail = dsPtr->spaceAvl - offset;
#endif
va_copy(argPtrCopy, argPtr);
result = vsnprintf(dsPtr->string + offset, (size_t)avail, fmt, argPtrCopy);
va_end(argPtrCopy);
#if defined(_MSC_VER)
failure = (result == -1);
#else
failure = (result == -1 || result >= avail);
#endif
#if defined(NDEBUG)
if (unlikely(failure)) {
Tcl_Panic("writing string-formatting output to a dynamic Tcl string failed");
}
#endif
assert(! failure);
}
}
/*
*----------------------------------------------------------------------
* Nsf_DStringPrintf --
*
* Append a sequence of values using a format string.
*
* Results:
* Pointer to the current string value.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
Nsf_DStringPrintf(Tcl_DString *dsPtr, const char *fmt, ...)
{
va_list ap;
nonnull_assert(dsPtr != NULL);
nonnull_assert(fmt != NULL);
va_start(ap, fmt);
NsfDStringVPrintf(dsPtr, fmt, ap);
va_end(ap);
}
/*
*----------------------------------------------------------------------
*
* NsfDStringArgv --
*
* Appends argument vector to an initialized Tcl_DString.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
NsfDStringArgv(
Tcl_DString *dsPtr,
int objc, Tcl_Obj *const objv[]
) {
nonnull_assert(dsPtr != NULL);
nonnull_assert(objv != NULL);
if (objc > 0) {
int i;
Tcl_DStringAppendElement(dsPtr, NsfMethodName(objv[0]));
for (i = 1; i < objc; i++) {
Tcl_DStringAppendElement(dsPtr, ObjStr(objv[i]));
}
}
}
/*
*----------------------------------------------------------------------
*
* NsfPrintError --
*
* Produce a formatted error message with a printf-like semantics.
*
* Results:
* TCL_ERROR
*
* Side effects:
* Sets the result message.
*
*----------------------------------------------------------------------
*/
int
NsfPrintError(Tcl_Interp *interp, const char *fmt, ...) {
va_list ap;
Tcl_DString ds;
Tcl_DStringInit(&ds);
va_start(ap, fmt);
NsfDStringVPrintf(&ds, fmt, ap);
va_end(ap);
Tcl_SetObjResult(interp, Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)));
Tcl_DStringFree(&ds);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* NsfErrInProc --
*
* Produce a general error message when an error occurs in a
* scripted NSF method.
*
* Results:
* TCL_ERROR
*
* Side effects:
* Sets the result message.
*
*----------------------------------------------------------------------
*/
int
NsfErrInProc(
Tcl_Interp *interp,
Tcl_Obj *objName,
Tcl_Obj *clName,
const char *procName
) {
Tcl_DString errMsg;
const char *cName, *space;
Tcl_DStringInit(&errMsg);
Tcl_DStringAppend(&errMsg, "\n ", -1);
if (clName != NULL) {
cName = ObjStr(clName);
space = " ";
} else {
cName = "";
space = "";
}
Tcl_DStringAppend(&errMsg, ObjStr(objName), -1);
Tcl_DStringAppend(&errMsg, space, -1);
Tcl_DStringAppend(&errMsg, cName, -1);
Tcl_DStringAppend(&errMsg, "->", 2);
Tcl_DStringAppend(&errMsg, procName, -1);
Tcl_AddErrorInfo (interp, Tcl_DStringValue(&errMsg));
Tcl_DStringFree(&errMsg);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* NsfObjWrongArgs --
*
* Produce a general error message when a NSF method is called with
* an invalid argument list (wrong number of arguments).
*
* Results:
* TCL_ERROR
*
* Side effects:
* Sets the result message.
*
*----------------------------------------------------------------------
*/
int
NsfObjWrongArgs(
Tcl_Interp *interp,
const char *msg, Tcl_Obj *cmdNameObj,
Tcl_Obj *methodPathObj, const char *arglist
) {
bool need_space = NSF_FALSE;
Tcl_DString ds;
nonnull_assert(interp != NULL);
nonnull_assert(msg != NULL);
Tcl_DStringInit(&ds);
Nsf_DStringPrintf(&ds, "%s should be \"", msg);
if (cmdNameObj != NULL) {
Tcl_DStringAppend(&ds, ObjStr(cmdNameObj), -1);
need_space = NSF_TRUE;
}
if (methodPathObj != NULL) {
if (need_space) {
Tcl_DStringAppend(&ds, " ", 1);
}
INCR_REF_COUNT(methodPathObj);
Tcl_DStringAppend(&ds, ObjStr(methodPathObj), -1);
DECR_REF_COUNT(methodPathObj);
need_space = NSF_TRUE;
}
if (arglist != NULL) {
if (need_space) {
Tcl_DStringAppend(&ds, " ", 1);
}
Tcl_DStringAppend(&ds, arglist, -1);
}
Tcl_DStringAppend(&ds, "\"", 1);
Tcl_SetObjResult(interp, Tcl_NewStringObj(ds.string, ds.length));
Tcl_DStringFree(&ds);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* NsfArgumentError --
*
* Produce a wrong-number-of-arguments error based on a parameter
* definition.
*
* Results:
* TCL_ERROR
*
* Side effects:
* Sets the result message.
*
*----------------------------------------------------------------------
*/
int
NsfArgumentError(
Tcl_Interp *interp,
const char *errorMsg, Nsf_Param const *paramPtr,
Tcl_Obj *cmdNameObj, Tcl_Obj *methodPathObj
) {
Tcl_Obj *argStringObj = NsfParamDefsSyntax(interp, paramPtr, NULL, NULL);
nonnull_assert(interp != NULL);
nonnull_assert(errorMsg != NULL);
nonnull_assert(paramPtr != NULL);
NsfObjWrongArgs(interp, errorMsg, cmdNameObj, methodPathObj, ObjStr(argStringObj));
DECR_REF_COUNT2("paramDefsObj", argStringObj);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* NsfUnexpectedArgumentError --
*
* Produce an error message on an unexpected argument (most likely,
* too many arguments)
*
* Results:
* TCL_ERROR
*
* Side effects:
* Sets the result message.
*
*----------------------------------------------------------------------
*/
int
NsfUnexpectedArgumentError(Tcl_Interp *interp, const char *argumentString,
Nsf_Object *object, Nsf_Param const *paramPtr,
Tcl_Obj *methodPathObj) {
Tcl_DString ds, *dsPtr = &ds;
nonnull_assert(interp != NULL);
nonnull_assert(argumentString != NULL);
nonnull_assert(paramPtr != NULL);
nonnull_assert(methodPathObj != NULL);
DSTRING_INIT(dsPtr);
Nsf_DStringPrintf(dsPtr, "invalid argument '%s', maybe too many arguments;", argumentString);
NsfArgumentError(interp, Tcl_DStringValue(dsPtr), paramPtr, (object != NULL) ? object->cmdName : NULL,
methodPathObj);
DSTRING_FREE(dsPtr);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* NsfUnexpectedNonposArgumentError --
*
* Produce an error message on an invalid non-positional argument.
*
* Results:
* TCL_ERROR
*
* Side effects:
* Sets the result message.
*
*----------------------------------------------------------------------
*/
int
NsfUnexpectedNonposArgumentError(
Tcl_Interp *interp,
const char *argumentString,
Nsf_Object *object,
Nsf_Param const *currentParamPtr,
Nsf_Param const *paramPtr,
Tcl_Obj *methodPathObj
) {
Tcl_DString ds, *dsPtr = &ds;
const Nsf_Param *pPtr;
nonnull_assert(interp != NULL);
nonnull_assert(argumentString != NULL);
nonnull_assert(currentParamPtr != NULL);
nonnull_assert(paramPtr != NULL);
nonnull_assert(methodPathObj != NULL);
DSTRING_INIT(dsPtr);
Nsf_DStringPrintf(dsPtr, "invalid non-positional argument '%s', valid are: ", argumentString);
for (pPtr = currentParamPtr; (pPtr->name != NULL) && (*pPtr->name == '-'); pPtr ++) {
if (pPtr->flags & NSF_ARG_NOCONFIG) {
continue;
}
Tcl_DStringAppend(dsPtr, pPtr->name, -1);
Tcl_DStringAppend(dsPtr, ", ", -1);
}
Tcl_DStringSetLength(dsPtr, Tcl_DStringLength(dsPtr) - 2);
Tcl_DStringAppend(dsPtr, ";\n", 2);
NsfArgumentError(interp, Tcl_DStringValue(dsPtr), paramPtr, (object != NULL) ? object->cmdName : NULL,
methodPathObj);
DSTRING_FREE(dsPtr);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* NsfDispatchClientDataError --
*
* Produce an error message when a method was not dispatched on an
* object.
*
* Results:
* TCL_ERROR
*
* Side effects:
* Sets the result message.
*
*----------------------------------------------------------------------
*/
int
NsfDispatchClientDataError(
Tcl_Interp *interp, ClientData clientData,
const char *what, const char *methodName
) {
nonnull_assert(interp != NULL);
nonnull_assert(what != NULL);
nonnull_assert(methodName != NULL);
if (clientData != NULL) {
return NsfPrintError(interp, "method %s not dispatched on valid %s",
methodName, what);
} else {
return NsfNoCurrentObjectError(interp, methodName);
}
}
/*
*----------------------------------------------------------------------
*
* NsfNoCurrentObjectError --
*
* Produce an error message when a method/command was called
* outside the context of an object or a method. The passed in
* methodName is NULL when e.g. "self" is called outside of a NSF
* context.
*
* Results:
* TCL_ERROR
*
* Side effects:
* Sets the result message.
*
*----------------------------------------------------------------------
*/
int
NsfNoCurrentObjectError(Tcl_Interp *interp, const char *methodName) {
nonnull_assert(interp != NULL);
return NsfPrintError(interp, "no current object; %s called outside the context of a Next Scripting method",
(methodName != NULL) ? methodName : "command");
}
/*
*----------------------------------------------------------------------
*
* NsfObjErrType --
*
* Produce a general error message when a NSF method is called with
* an invalid value for some argument.
*
* Results:
* TCL_ERROR
*
* Side effects:
* Sets the result message.
*
*----------------------------------------------------------------------
*/
int
NsfObjErrType(
Tcl_Interp *interp,
const char *context,
Tcl_Obj *value,
const char *type,
Nsf_Param const *NsfObjErrType
) {
bool isNamed = (NsfObjErrType && (NsfObjErrType->flags & NSF_ARG_UNNAMED) == 0);
int returnValue = !isNamed && NsfObjErrType && (NsfObjErrType->flags & NSF_ARG_IS_RETURNVALUE);
int errMsgLen;
const char *prevErrMsg = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), &errMsgLen);
Tcl_DString ds;
Tcl_DStringInit(&ds);
if (errMsgLen > 0) {
Tcl_DStringAppend(&ds, prevErrMsg, errMsgLen);
Tcl_DStringAppend(&ds, " 2nd error: ", -1);
}
if (context != NULL) {
Tcl_DStringAppend(&ds, context, -1);
Tcl_DStringAppend(&ds, ": ", 2);
}
Nsf_DStringPrintf(&ds, "expected %s but got \"%s\"", type, ObjStr(value));
if (isNamed) {
Nsf_DStringPrintf(&ds, " for parameter \"%s\"", NsfObjErrType->name);
} else if (returnValue != 0) {
Tcl_DStringAppend(&ds, " as return value", -1);
}
Tcl_SetObjResult(interp, Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)));
Tcl_DStringFree(&ds);
return TCL_ERROR;
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 2
* fill-column: 72
* indent-tabs-mode: nil
* eval: (c-guess)
* End:
*/
|