1 /* $NetBSD: lobject.c,v 1.14 2023/06/08 21:12:08 nikita Exp $ */ 2 3 /* 4 ** Id: lobject.c 5 ** Some generic functions over Lua objects 6 ** See Copyright Notice in lua.h 7 */ 8 9 #define lobject_c 10 #define LUA_CORE 11 12 #include "lprefix.h" 13 14 15 #ifndef _KERNEL 16 #include <locale.h> 17 #include <math.h> 18 #include <stdarg.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <string.h> 22 #endif /* _KERNEL */ 23 24 #include "lua.h" 25 26 #include "lctype.h" 27 #include "ldebug.h" 28 #include "ldo.h" 29 #include "lmem.h" 30 #include "lobject.h" 31 #include "lstate.h" 32 #include "lstring.h" 33 #include "lvm.h" 34 35 36 /* 37 ** Computes ceil(log2(x)) 38 */ 39 int luaO_ceillog2 (unsigned int x) { 40 static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */ 41 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 42 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 43 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 44 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 45 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 46 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 47 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 48 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 49 }; 50 int l = 0; 51 x--; 52 while (x >= 256) { l += 8; x >>= 8; } 53 return l + log_2[x]; 54 } 55 56 57 static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, 58 lua_Integer v2) { 59 switch (op) { 60 case LUA_OPADD: return intop(+, v1, v2); 61 case LUA_OPSUB:return intop(-, v1, v2); 62 case LUA_OPMUL:return intop(*, v1, v2); 63 case LUA_OPMOD: return luaV_mod(L, v1, v2); 64 case LUA_OPIDIV: return luaV_idiv(L, v1, v2); 65 case LUA_OPBAND: return intop(&, v1, v2); 66 case LUA_OPBOR: return intop(|, v1, v2); 67 case LUA_OPBXOR: return intop(^, v1, v2); 68 case LUA_OPSHL: return luaV_shiftl(v1, v2); 69 case LUA_OPSHR: return luaV_shiftr(v1, v2); 70 case LUA_OPUNM: return intop(-, 0, v1); 71 case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); 72 default: lua_assert(0); return 0; 73 } 74 } 75 76 77 #ifndef _KERNEL 78 static lua_Number numarith (lua_State *L, int op, lua_Number v1, 79 lua_Number v2) { 80 switch (op) { 81 case LUA_OPADD: return luai_numadd(L, v1, v2); 82 case LUA_OPSUB: return luai_numsub(L, v1, v2); 83 case LUA_OPMUL: return luai_nummul(L, v1, v2); 84 case LUA_OPDIV: return luai_numdiv(L, v1, v2); 85 case LUA_OPPOW: return luai_numpow(L, v1, v2); 86 case LUA_OPIDIV: return luai_numidiv(L, v1, v2); 87 case LUA_OPUNM: return luai_numunm(L, v1); 88 case LUA_OPMOD: return luaV_modf(L, v1, v2); 89 default: lua_assert(0); return 0; 90 } 91 } 92 #endif /* _KERNEL */ 93 94 95 int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, 96 TValue *res) { 97 switch (op) { 98 case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: 99 case LUA_OPSHL: case LUA_OPSHR: 100 case LUA_OPBNOT: { /* operate only on integers */ 101 lua_Integer i1; lua_Integer i2; 102 if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) { 103 setivalue(res, intarith(L, op, i1, i2)); 104 return 1; 105 } 106 else return 0; /* fail */ 107 } 108 #ifndef _KERNEL 109 case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ 110 lua_Number n1; lua_Number n2; 111 if (tonumberns(p1, n1) && tonumberns(p2, n2)) { 112 setfltvalue(res, numarith(L, op, n1, n2)); 113 return 1; 114 } 115 else return 0; /* fail */ 116 } 117 #endif /* _KERNEL */ 118 default: { /* other operations */ 119 #ifndef _KERNEL 120 lua_Number n1; lua_Number n2; 121 if (ttisinteger(p1) && ttisinteger(p2)) { 122 setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); 123 return 1; 124 } 125 else if (tonumberns(p1, n1) && tonumberns(p2, n2)) { 126 setfltvalue(res, numarith(L, op, n1, n2)); 127 return 1; 128 } 129 #else /* _KERNEL */ 130 lua_Integer i1; lua_Integer i2; 131 if (tointeger(p1, &i1) && tointeger(p2, &i2)) { 132 setivalue(res, intarith(L, op, i1, i2)); 133 return 1; 134 } 135 #endif /* _KERNEL */ 136 else return 0; /* fail */ 137 } 138 } 139 } 140 141 142 void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, 143 StkId res) { 144 if (!luaO_rawarith(L, op, p1, p2, s2v(res))) { 145 /* could not perform raw operation; try metamethod */ 146 luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); 147 } 148 } 149 150 151 int luaO_hexavalue (int c) { 152 if (lisdigit(c)) return c - '0'; 153 else return (ltolower(c) - 'a') + 10; 154 } 155 156 157 static int isneg (const char **s) { 158 if (**s == '-') { (*s)++; return 1; } 159 else if (**s == '+') (*s)++; 160 return 0; 161 } 162 163 164 165 #ifndef _KERNEL 166 /* 167 ** {================================================================== 168 ** Lua's implementation for 'lua_strx2number' 169 ** =================================================================== 170 */ 171 172 #if !defined(lua_strx2number) 173 174 /* maximum number of significant digits to read (to avoid overflows 175 even with single floats) */ 176 #define MAXSIGDIG 30 177 178 /* 179 ** convert a hexadecimal numeric string to a number, following 180 ** C99 specification for 'strtod' 181 */ 182 static lua_Number lua_strx2number (const char *s, char **endptr) { 183 int dot = lua_getlocaledecpoint(); 184 lua_Number r = l_mathop(0.0); /* result (accumulator) */ 185 int sigdig = 0; /* number of significant digits */ 186 int nosigdig = 0; /* number of non-significant digits */ 187 int e = 0; /* exponent correction */ 188 int neg; /* 1 if number is negative */ 189 int hasdot = 0; /* true after seen a dot */ 190 *endptr = cast_charp(s); /* nothing is valid yet */ 191 while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ 192 neg = isneg(&s); /* check sign */ 193 if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ 194 return l_mathop(0.0); /* invalid format (no '0x') */ 195 for (s += 2; ; s++) { /* skip '0x' and read numeral */ 196 if (*s == dot) { 197 if (hasdot) break; /* second dot? stop loop */ 198 else hasdot = 1; 199 } 200 else if (lisxdigit(cast_uchar(*s))) { 201 if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ 202 nosigdig++; 203 else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ 204 r = (r * l_mathop(16.0)) + luaO_hexavalue(*s); 205 else e++; /* too many digits; ignore, but still count for exponent */ 206 if (hasdot) e--; /* decimal digit? correct exponent */ 207 } 208 else break; /* neither a dot nor a digit */ 209 } 210 if (nosigdig + sigdig == 0) /* no digits? */ 211 return l_mathop(0.0); /* invalid format */ 212 *endptr = cast_charp(s); /* valid up to here */ 213 e *= 4; /* each digit multiplies/divides value by 2^4 */ 214 if (*s == 'p' || *s == 'P') { /* exponent part? */ 215 int exp1 = 0; /* exponent value */ 216 int neg1; /* exponent sign */ 217 s++; /* skip 'p' */ 218 neg1 = isneg(&s); /* sign */ 219 if (!lisdigit(cast_uchar(*s))) 220 return l_mathop(0.0); /* invalid; must have at least one digit */ 221 while (lisdigit(cast_uchar(*s))) /* read exponent */ 222 exp1 = exp1 * 10 + *(s++) - '0'; 223 if (neg1) exp1 = -exp1; 224 e += exp1; 225 *endptr = cast_charp(s); /* valid up to here */ 226 } 227 if (neg) r = -r; 228 return l_mathop(ldexp)(r, e); 229 } 230 231 #endif 232 /* }====================================================== */ 233 234 235 /* maximum length of a numeral to be converted to a number */ 236 #if !defined (L_MAXLENNUM) 237 #define L_MAXLENNUM 200 238 #endif 239 240 /* 241 ** Convert string 's' to a Lua number (put in 'result'). Return NULL on 242 ** fail or the address of the ending '\0' on success. ('mode' == 'x') 243 ** means a hexadecimal numeral. 244 */ 245 static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { 246 char *endptr; 247 *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */ 248 : lua_str2number(s, &endptr); 249 if (endptr == s) return NULL; /* nothing recognized? */ 250 while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */ 251 return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */ 252 } 253 254 255 /* 256 ** Convert string 's' to a Lua number (put in 'result') handling the 257 ** current locale. 258 ** This function accepts both the current locale or a dot as the radix 259 ** mark. If the conversion fails, it may mean number has a dot but 260 ** locale accepts something else. In that case, the code copies 's' 261 ** to a buffer (because 's' is read-only), changes the dot to the 262 ** current locale radix mark, and tries to convert again. 263 ** The variable 'mode' checks for special characters in the string: 264 ** - 'n' means 'inf' or 'nan' (which should be rejected) 265 ** - 'x' means a hexadecimal numeral 266 ** - '.' just optimizes the search for the common case (no special chars) 267 */ 268 static const char *l_str2d (const char *s, lua_Number *result) { 269 const char *endptr; 270 const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */ 271 int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; 272 if (mode == 'n') /* reject 'inf' and 'nan' */ 273 return NULL; 274 endptr = l_str2dloc(s, result, mode); /* try to convert */ 275 if (endptr == NULL) { /* failed? may be a different locale */ 276 char buff[L_MAXLENNUM + 1]; 277 const char *pdot = strchr(s, '.'); 278 if (pdot == NULL || strlen(s) > L_MAXLENNUM) 279 return NULL; /* string too long or no dot; fail */ 280 strcpy(buff, s); /* copy string to buffer */ 281 buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */ 282 endptr = l_str2dloc(buff, result, mode); /* try again */ 283 if (endptr != NULL) 284 endptr = s + (endptr - buff); /* make relative to 's' */ 285 } 286 return endptr; 287 } 288 #endif /* _KERNEL */ 289 290 291 #define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10) 292 #define MAXLASTD cast_int(LUA_MAXINTEGER % 10) 293 294 static const char *l_str2int (const char *s, lua_Integer *result) { 295 lua_Unsigned a = 0; 296 int empty = 1; 297 int neg; 298 while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ 299 neg = isneg(&s); 300 if (s[0] == '0' && 301 (s[1] == 'x' || s[1] == 'X')) { /* hex? */ 302 s += 2; /* skip '0x' */ 303 for (; lisxdigit(cast_uchar(*s)); s++) { 304 a = a * 16 + luaO_hexavalue(*s); 305 empty = 0; 306 } 307 } 308 else { /* decimal */ 309 for (; lisdigit(cast_uchar(*s)); s++) { 310 int d = *s - '0'; 311 if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ 312 return NULL; /* do not accept it (as integer) */ 313 a = a * 10 + d; 314 empty = 0; 315 } 316 } 317 while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ 318 if (empty || *s != '\0') return NULL; /* something wrong in the numeral */ 319 else { 320 *result = l_castU2S((neg) ? 0u - a : a); 321 return s; 322 } 323 } 324 325 326 size_t luaO_str2num (const char *s, TValue *o) { 327 #ifndef _KERNEL 328 lua_Integer i; lua_Number n; 329 #else /* _KERNEL */ 330 lua_Integer i; 331 #endif /* _KERNEL */ 332 const char *e; 333 if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */ 334 setivalue(o, i); 335 } 336 #ifndef _KERNEL 337 else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */ 338 setfltvalue(o, n); 339 } 340 #endif /* _KERNEL */ 341 else 342 return 0; /* conversion failed */ 343 return (e - s) + 1; /* success; return string size */ 344 } 345 346 347 int luaO_utf8esc (char *buff, unsigned long x) { 348 int n = 1; /* number of bytes put in buffer (backwards) */ 349 lua_assert(x <= 0x7FFFFFFFu); 350 if (x < 0x80) /* ascii? */ 351 buff[UTF8BUFFSZ - 1] = cast_char(x); 352 else { /* need continuation bytes */ 353 unsigned int mfb = 0x3f; /* maximum that fits in first byte */ 354 do { /* add continuation bytes */ 355 buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f)); 356 x >>= 6; /* remove added bits */ 357 mfb >>= 1; /* now there is one less bit available in first byte */ 358 } while (x > mfb); /* still needs continuation byte? */ 359 buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x); /* add first byte */ 360 } 361 return n; 362 } 363 364 365 /* 366 ** Maximum length of the conversion of a number to a string. Must be 367 ** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT. 368 ** (For a long long int, this is 19 digits plus a sign and a final '\0', 369 ** adding to 21. For a long double, it can go to a sign, 33 digits, 370 ** the dot, an exponent letter, an exponent sign, 5 exponent digits, 371 ** and a final '\0', adding to 43.) 372 */ 373 #define MAXNUMBER2STR 44 374 375 376 /* 377 ** Convert a number object to a string, adding it to a buffer 378 */ 379 static int tostringbuff (TValue *obj, char *buff) { 380 int len; 381 lua_assert(ttisnumber(obj)); 382 #ifndef _KERNEL 383 if (ttisinteger(obj)) 384 len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj)); 385 else { 386 len = lua_number2str(buff, MAXNUMBER2STR, fltvalue(obj)); 387 if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ 388 buff[len++] = lua_getlocaledecpoint(); 389 buff[len++] = '0'; /* adds '.0' to result */ 390 } 391 } 392 #else /* _KERNEL */ 393 lua_assert(ttisinteger(obj)); 394 len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj)); 395 #endif /* _KERNEL */ 396 return len; 397 } 398 399 400 /* 401 ** Convert a number object to a Lua string, replacing the value at 'obj' 402 */ 403 void luaO_tostring (lua_State *L, TValue *obj) { 404 char buff[MAXNUMBER2STR]; 405 int len = tostringbuff(obj, buff); 406 setsvalue(L, obj, luaS_newlstr(L, buff, len)); 407 } 408 409 410 411 412 /* 413 ** {================================================================== 414 ** 'luaO_pushvfstring' 415 ** =================================================================== 416 */ 417 418 /* 419 ** Size for buffer space used by 'luaO_pushvfstring'. It should be 420 ** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages, 421 ** so that 'luaG_addinfo' can work directly on the buffer. 422 */ 423 #define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95) 424 425 /* buffer used by 'luaO_pushvfstring' */ 426 typedef struct BuffFS { 427 lua_State *L; 428 int pushed; /* true if there is a part of the result on the stack */ 429 int blen; /* length of partial string in 'space' */ 430 char space[BUFVFS]; /* holds last part of the result */ 431 } BuffFS; 432 433 434 /* 435 ** Push given string to the stack, as part of the result, and 436 ** join it to previous partial result if there is one. 437 ** It may call 'luaV_concat' while using one slot from EXTRA_STACK. 438 ** This call cannot invoke metamethods, as both operands must be 439 ** strings. It can, however, raise an error if the result is too 440 ** long. In that case, 'luaV_concat' frees the extra slot before 441 ** raising the error. 442 */ 443 static void pushstr (BuffFS *buff, const char *str, size_t lstr) { 444 lua_State *L = buff->L; 445 setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr)); 446 L->top.p++; /* may use one slot from EXTRA_STACK */ 447 if (!buff->pushed) /* no previous string on the stack? */ 448 buff->pushed = 1; /* now there is one */ 449 else /* join previous string with new one */ 450 luaV_concat(L, 2); 451 } 452 453 454 /* 455 ** empty the buffer space into the stack 456 */ 457 static void clearbuff (BuffFS *buff) { 458 pushstr(buff, buff->space, buff->blen); /* push buffer contents */ 459 buff->blen = 0; /* space now is empty */ 460 } 461 462 463 /* 464 ** Get a space of size 'sz' in the buffer. If buffer has not enough 465 ** space, empty it. 'sz' must fit in an empty buffer. 466 */ 467 static char *getbuff (BuffFS *buff, int sz) { 468 lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS); 469 if (sz > BUFVFS - buff->blen) /* not enough space? */ 470 clearbuff(buff); 471 return buff->space + buff->blen; 472 } 473 474 475 #define addsize(b,sz) ((b)->blen += (sz)) 476 477 478 /* 479 ** Add 'str' to the buffer. If string is larger than the buffer space, 480 ** push the string directly to the stack. 481 */ 482 static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { 483 if (slen <= BUFVFS) { /* does string fit into buffer? */ 484 char *bf = getbuff(buff, cast_int(slen)); 485 memcpy(bf, str, slen); /* add string to buffer */ 486 addsize(buff, cast_int(slen)); 487 } 488 else { /* string larger than buffer */ 489 clearbuff(buff); /* string comes after buffer's content */ 490 pushstr(buff, str, slen); /* push string */ 491 } 492 } 493 494 495 /* 496 ** Add a numeral to the buffer. 497 */ 498 static void addnum2buff (BuffFS *buff, TValue *num) { 499 char *numbuff = getbuff(buff, MAXNUMBER2STR); 500 int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ 501 addsize(buff, len); 502 } 503 504 505 /* 506 ** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%' 507 conventional formats, plus Lua-specific '%I' and '%U' 508 */ 509 const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { 510 BuffFS buff; /* holds last part of the result */ 511 const char *e; /* points to next '%' */ 512 buff.pushed = buff.blen = 0; 513 buff.L = L; 514 while ((e = strchr(fmt, '%')) != NULL) { 515 addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */ 516 switch (*(e + 1)) { /* conversion specifier */ 517 case 's': { /* zero-terminated string */ 518 const char *s = va_arg(argp, char *); 519 if (s == NULL) s = "(null)"; 520 addstr2buff(&buff, s, strlen(s)); 521 break; 522 } 523 case 'c': { /* an 'int' as a character */ 524 char c = cast_uchar(va_arg(argp, int)); 525 addstr2buff(&buff, &c, sizeof(char)); 526 break; 527 } 528 case 'd': { /* an 'int' */ 529 TValue num; 530 setivalue(&num, va_arg(argp, int)); 531 addnum2buff(&buff, &num); 532 break; 533 } 534 case 'I': { /* a 'lua_Integer' */ 535 TValue num; 536 setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt))); 537 addnum2buff(&buff, &num); 538 break; 539 } 540 #ifndef _KERNEL 541 case 'f': { /* a 'lua_Number' */ 542 TValue num; 543 setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber))); 544 addnum2buff(&buff, &num); 545 break; 546 } 547 #endif /* _KERNEL */ 548 case 'p': { /* a pointer */ 549 const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */ 550 char *bf = getbuff(&buff, sz); 551 void *p = va_arg(argp, void *); 552 int len = lua_pointer2str(bf, sz, p); 553 addsize(&buff, len); 554 break; 555 } 556 case 'U': { /* a 'long' as a UTF-8 sequence */ 557 char bf[UTF8BUFFSZ]; 558 int len = luaO_utf8esc(bf, va_arg(argp, long)); 559 addstr2buff(&buff, bf + UTF8BUFFSZ - len, len); 560 break; 561 } 562 case '%': { 563 addstr2buff(&buff, "%", 1); 564 break; 565 } 566 default: { 567 luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'", 568 *(e + 1)); 569 } 570 } 571 fmt = e + 2; /* skip '%' and the specifier */ 572 } 573 addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ 574 clearbuff(&buff); /* empty buffer into the stack */ 575 lua_assert(buff.pushed == 1); 576 return svalue(s2v(L->top.p - 1)); 577 } 578 579 580 const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { 581 const char *msg; 582 va_list argp; 583 va_start(argp, fmt); 584 msg = luaO_pushvfstring(L, fmt, argp); 585 va_end(argp); 586 return msg; 587 } 588 589 /* }================================================================== */ 590 591 592 #define RETS "..." 593 #define PRE "[string \"" 594 #define POS "\"]" 595 596 #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) 597 598 void luaO_chunkid (char *out, const char *source, size_t srclen) { 599 size_t bufflen = LUA_IDSIZE; /* free space in buffer */ 600 if (*source == '=') { /* 'literal' source */ 601 if (srclen <= bufflen) /* small enough? */ 602 memcpy(out, source + 1, srclen * sizeof(char)); 603 else { /* truncate it */ 604 addstr(out, source + 1, bufflen - 1); 605 *out = '\0'; 606 } 607 } 608 else if (*source == '@') { /* file name */ 609 if (srclen <= bufflen) /* small enough? */ 610 memcpy(out, source + 1, srclen * sizeof(char)); 611 else { /* add '...' before rest of name */ 612 addstr(out, RETS, LL(RETS)); 613 bufflen -= LL(RETS); 614 memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char)); 615 } 616 } 617 else { /* string; format as [string "source"] */ 618 const char *nl = strchr(source, '\n'); /* find first new line (if any) */ 619 addstr(out, PRE, LL(PRE)); /* add prefix */ 620 bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ 621 if (srclen < bufflen && nl == NULL) { /* small one-line source? */ 622 addstr(out, source, srclen); /* keep it */ 623 } 624 else { 625 if (nl != NULL) srclen = nl - source; /* stop at first newline */ 626 if (srclen > bufflen) srclen = bufflen; 627 addstr(out, source, srclen); 628 addstr(out, RETS, LL(RETS)); 629 } 630 memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); 631 } 632 } 633 634