terminal_alloc(): * Maintain a string pool for termcap strings */
| 325 | * Maintain a string pool for termcap strings |
| 326 | */ |
| 327 | private void |
| 328 | terminal_alloc(EditLine *el, const struct termcapstr *t, const char *cap) |
| 329 | { |
| 330 | char termbuf[TC_BUFSIZE]; |
| 331 | size_t tlen, clen; |
| 332 | char **tlist = el->el_terminal.t_str; |
| 333 | char **tmp, **str = &tlist[t - tstr]; |
| 334 | |
| 335 | (void) memset(termbuf, 0, sizeof(termbuf)); |
| 336 | if (cap == NULL || *cap == '\0') { |
| 337 | *str = NULL; |
| 338 | return; |
| 339 | } else |
| 340 | clen = strlen(cap); |
| 341 | |
| 342 | tlen = *str == NULL ? 0 : strlen(*str); |
| 343 | |
| 344 | /* |
| 345 | * New string is shorter; no need to allocate space |
| 346 | */ |
| 347 | if (clen <= tlen) { |
| 348 | if (*str) |
| 349 | (void) strcpy(*str, cap); /* XXX strcpy is safe */ |
| 350 | return; |
| 351 | } |
| 352 | /* |
| 353 | * New string is longer; see if we have enough space to append |
| 354 | */ |
| 355 | if (el->el_terminal.t_loc + 3 < TC_BUFSIZE) { |
| 356 | /* XXX strcpy is safe */ |
| 357 | (void) strcpy(*str = &el->el_terminal.t_buf[ |
| 358 | el->el_terminal.t_loc], cap); |
| 359 | el->el_terminal.t_loc += clen + 1; /* one for \0 */ |
| 360 | return; |
| 361 | } |
| 362 | /* |
| 363 | * Compact our buffer; no need to check compaction, cause we know it |
| 364 | * fits... |
| 365 | */ |
| 366 | tlen = 0; |
| 367 | for (tmp = tlist; tmp < &tlist[T_str]; tmp++) |
| 368 | if (*tmp != NULL && *tmp != '\0' && *tmp != *str) { |
| 369 | char *ptr; |
| 370 | |
| 371 | for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++) |
| 372 | continue; |
| 373 | termbuf[tlen++] = '\0'; |
| 374 | } |
| 375 | memcpy(el->el_terminal.t_buf, termbuf, TC_BUFSIZE); |
| 376 | el->el_terminal.t_loc = tlen; |
| 377 | if (el->el_terminal.t_loc + 3 >= TC_BUFSIZE) { |
| 378 | (void) fprintf(el->el_errfile, |
| 379 | "Out of termcap string space.\n"); |
| 380 | return; |
| 381 | } |
| 382 | /* XXX strcpy is safe */ |
| 383 | (void) strcpy(*str = &el->el_terminal.t_buf[el->el_terminal.t_loc], |
| 384 | cap); |
no outgoing calls
no test coverage detected