format a parameterized string, ala sprintf */
| 349 | |
| 350 | /* format a parameterized string, ala sprintf */ |
| 351 | char * |
| 352 | tparam(const char *ctl, /* parameter control string */ |
| 353 | char *buf, /* output buffer */ |
| 354 | int buflen, /* ought to have been `size_t'... */ |
| 355 | int row, int col, int row2, int col2) |
| 356 | { |
| 357 | int atmp, ac, av[5]; |
| 358 | char c, *r, *z, *bufend, numbuf[32]; |
| 359 | const char *fmt; |
| 360 | #ifndef NO_SPECIAL_CHARS_FIXUP |
| 361 | int bc = 0, up = 0; |
| 362 | #endif |
| 363 | |
| 364 | av[0] = row, av[1] = col, av[2] = row2, av[3] = col2, av[4] = 0; |
| 365 | ac = 0; |
| 366 | r = buf, bufend = r + buflen - 1; |
| 367 | while (*ctl) { |
| 368 | if ((*r = *ctl++) == '%') { |
| 369 | if (ac > 4) |
| 370 | ac = 4; |
| 371 | fmt = 0; |
| 372 | switch ((c = *ctl++)) { |
| 373 | case '%': |
| 374 | break; /* '%' already copied */ |
| 375 | case 'd': |
| 376 | fmt = "%d"; |
| 377 | break; |
| 378 | case '2': |
| 379 | fmt = "%02d"; |
| 380 | break; |
| 381 | case '3': |
| 382 | fmt = "%03d"; |
| 383 | break; |
| 384 | case '+': /*FALLTHRU*/ |
| 385 | case '.': |
| 386 | *r = (char) av[ac++]; |
| 387 | if (c == '+') |
| 388 | *r += *ctl++; |
| 389 | if (!*r) { |
| 390 | *r = (char) '\200'; |
| 391 | } else { |
| 392 | #ifndef NO_SPECIAL_CHARS_FIXUP |
| 393 | /* avoid terminal driver intervention for |
| 394 | various control characters, to prevent |
| 395 | LF from becoming CR+LF, for instance; only |
| 396 | makes sense if this is a cursor positioning |
| 397 | sequence, but we have no way to check that */ |
| 398 | while (strchr("\004\t\n\013\f\r", *r)) { |
| 399 | if (ac & 1) { /* row */ |
| 400 | if (!UP || !*UP) |
| 401 | break; /* can't fix */ |
| 402 | ++up; /* incr row now, later move up */ |
| 403 | } else { /* column */ |
| 404 | if (!BC || !*BC) |
| 405 | break; /* can't fix */ |
| 406 | ++bc; /* incr column, later backspace */ |
| 407 | } |
| 408 | (*r)++; |