lobject.c 18 KB

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