clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name search.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/less/less/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/usr.bin/less/less/.. -D SYSDIR="/etc" -D HELPDIR="/usr/share/misc" -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/less/less/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.bin/less/less/../search.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | #include "charset.h" |
| 17 | #include "less.h" |
| 18 | #include "pattern.h" |
| 19 | #include "position.h" |
| 20 | |
| 21 | #define MINPOS(a, b) (((a) < (b)) ? (a) : (b)) |
| 22 | #define MAXPOS(a, b) (((a) > (b)) ? (a) : (b)) |
| 23 | |
| 24 | extern int how_search; |
| 25 | extern int caseless; |
| 26 | extern int linenums; |
| 27 | extern int sc_height; |
| 28 | extern int jump_sline; |
| 29 | extern int bs_mode; |
| 30 | extern int ctldisp; |
| 31 | extern int status_col; |
| 32 | extern void *const ml_search; |
| 33 | extern off_t start_attnpos; |
| 34 | extern off_t end_attnpos; |
| 35 | extern int utf_mode; |
| 36 | extern int screen_trashed; |
| 37 | extern int hilite_search; |
| 38 | extern int size_linebuf; |
| 39 | extern int squished; |
| 40 | extern int can_goto_line; |
| 41 | static int hide_hilite; |
| 42 | static off_t prep_startpos; |
| 43 | static off_t prep_endpos; |
| 44 | static int is_caseless; |
| 45 | static int is_ucase_pattern; |
| 46 | |
| 47 | struct hilite { |
| 48 | struct hilite *hl_next; |
| 49 | off_t hl_startpos; |
| 50 | off_t hl_endpos; |
| 51 | }; |
| 52 | static struct hilite hilite_anchor = { NULL, -1, -1 }; |
| 53 | static struct hilite filter_anchor = { NULL, -1, -1 }; |
| 54 | #define hl_first hl_next |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | struct pattern_info { |
| 61 | regex_t *compiled; |
| 62 | char *text; |
| 63 | int search_type; |
| 64 | }; |
| 65 | |
| 66 | #define info_compiled(info) ((info)->compiled) |
| 67 | |
| 68 | static struct pattern_info search_info; |
| 69 | static struct pattern_info filter_info; |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | static int |
| 75 | is_ucase(char *str) |
| 76 | { |
| 77 | wchar_t ch; |
| 78 | int len; |
| 79 | |
| 80 | for (; *str != '\0'; str += len) { |
| 81 | if ((len = mbtowc(&ch, str, MB_CUR_MAX)) == -1) { |
| 82 | mbtowc(NULL, NULL, MB_CUR_MAX); |
| 83 | len = 1; |
| 84 | } else if (iswupper(ch)) |
| 85 | return (1); |
| 86 | } |
| 87 | return (0); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | static int |
| 94 | set_pattern(struct pattern_info *info, char *pattern, int search_type) |
| 95 | { |
| 96 | if (pattern == NULL) |
| 97 | info->compiled = NULL; |
| 98 | else if (compile_pattern(pattern, search_type, &info->compiled) < 0) |
| 99 | return (-1); |
| 100 | |
| 101 | free(info->text); |
| 102 | info->text = NULL; |
| 103 | if (pattern != NULL) |
| 104 | info->text = estrdup(pattern); |
| 105 | info->search_type = search_type; |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | is_ucase_pattern = is_ucase(pattern); |
| 112 | if (is_ucase_pattern && caseless != OPT_ONPLUS) |
| 113 | is_caseless = 0; |
| 114 | else |
| 115 | is_caseless = caseless; |
| 116 | return (0); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | static void |
| 123 | clear_pattern(struct pattern_info *info) |
| 124 | { |
| 125 | free(info->text); |
| 126 | info->text = NULL; |
| 127 | uncompile_pattern(&info->compiled); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | static void |
| 134 | init_pattern(struct pattern_info *info) |
| 135 | { |
| 136 | info->compiled = NULL; |
| 137 | info->text = NULL; |
| 138 | info->search_type = 0; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | void |
| 145 | init_search(void) |
| 146 | { |
| 147 | init_pattern(&search_info); |
| 148 | init_pattern(&filter_info); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | static int |
| 155 | get_cvt_ops(void) |
| 156 | { |
| 157 | int ops = 0; |
| 158 | if (is_caseless || bs_mode == BS_SPECIAL) { |
| 159 | if (is_caseless) |
| 160 | ops |= CVT_TO_LC; |
| 161 | if (bs_mode == BS_SPECIAL) |
| 162 | ops |= CVT_BS; |
| 163 | if (bs_mode != BS_CONTROL) |
| 164 | ops |= CVT_CRLF; |
| 165 | } else if (bs_mode != BS_CONTROL) { |
| 166 | ops |= CVT_CRLF; |
| 167 | } |
| 168 | if (ctldisp == OPT_ONPLUS) |
| 169 | ops |= CVT_ANSI; |
| 170 | return (ops); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | static int |
| 177 | prev_pattern(struct pattern_info *info) |
| 178 | { |
| 179 | if ((info->search_type & SRCH_NO_REGEX) == 0) |
| 180 | return (info->compiled != NULL); |
| 181 | return (info->text != NULL); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | void |
| 190 | repaint_hilite(int on) |
| 191 | { |
| 192 | int slinenum; |
| 193 | off_t pos; |
| 194 | int save_hide_hilite; |
| 195 | |
| 196 | if (squished) |
| 197 | repaint(); |
| 198 | |
| 199 | save_hide_hilite = hide_hilite; |
| 200 | if (!on) { |
| 201 | if (hide_hilite) |
| 202 | return; |
| 203 | hide_hilite = 1; |
| 204 | } |
| 205 | |
| 206 | if (!can_goto_line) { |
| 207 | repaint(); |
| 208 | hide_hilite = save_hide_hilite; |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++) { |
| 213 | pos = position(slinenum); |
| 214 | if (pos == -1) |
| 215 | continue; |
| 216 | (void) forw_line(pos); |
| 217 | goto_line(slinenum); |
| 218 | put_line(); |
| 219 | } |
| 220 | lower_left(); |
| 221 | hide_hilite = save_hide_hilite; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | void |
| 228 | clear_attn(void) |
| 229 | { |
| 230 | int slinenum; |
| 231 | off_t old_start_attnpos; |
| 232 | off_t old_end_attnpos; |
| 233 | off_t pos; |
| 234 | off_t epos; |
| 235 | int moved = 0; |
| 236 | |
| 237 | if (start_attnpos == -1) |
| 238 | return; |
| 239 | old_start_attnpos = start_attnpos; |
| 240 | old_end_attnpos = end_attnpos; |
| 241 | start_attnpos = end_attnpos = -1; |
| 242 | |
| 243 | if (!can_goto_line) { |
| 244 | repaint(); |
| 245 | return; |
| 246 | } |
| 247 | if (squished) |
| 248 | repaint(); |
| 249 | |
| 250 | for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++) { |
| 251 | pos = position(slinenum); |
| 252 | if (pos == -1) |
| 253 | continue; |
| 254 | epos = position(slinenum+1); |
| 255 | if (pos < old_end_attnpos && |
| 256 | (epos == -1 || epos > old_start_attnpos)) { |
| 257 | (void) forw_line(pos); |
| 258 | goto_line(slinenum); |
| 259 | put_line(); |
| 260 | moved = 1; |
| 261 | } |
| 262 | } |
| 263 | if (moved) |
| 264 | lower_left(); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | void |
| 271 | undo_search(void) |
| 272 | { |
| 273 | if (!prev_pattern(&search_info)) { |
| 274 | error("No previous regular expression", NULL); |
| 275 | return; |
| 276 | } |
| 277 | hide_hilite = !hide_hilite; |
| 278 | repaint_hilite(1); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | static void |
| 285 | clr_hlist(struct hilite *anchor) |
| 286 | { |
| 287 | struct hilite *hl; |
| 288 | struct hilite *nexthl; |
| 289 | |
| 290 | for (hl = anchor->hl_first; hl != NULL; hl = nexthl) { |
| 291 | nexthl = hl->hl_next; |
| 292 | free(hl); |
| 293 | } |
| 294 | anchor->hl_first = NULL; |
| 295 | prep_startpos = prep_endpos = -1; |
| 296 | } |
| 297 | |
| 298 | void |
| 299 | clr_hilite(void) |
| 300 | { |
| 301 | clr_hlist(&hilite_anchor); |
| 302 | } |
| 303 | |
| 304 | static void |
| 305 | clr_filter(void) |
| 306 | { |
| 307 | clr_hlist(&filter_anchor); |
| 308 | } |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | static int |
| 314 | is_hilited_range(off_t pos, off_t epos) |
| 315 | { |
| 316 | struct hilite *hl; |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | for (hl = hilite_anchor.hl_first; hl != NULL; hl = hl->hl_next) { |
| 322 | if (hl->hl_endpos > pos && |
| 323 | (epos == -1 || epos > hl->hl_startpos)) |
| 324 | return (1); |
| 325 | } |
| 326 | return (0); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | int |
| 333 | is_filtered(off_t pos) |
| 334 | { |
| 335 | struct hilite *hl; |
| 336 | |
| 337 | if (ch_getflags() & CH_HELPFILE) |
| 338 | return (0); |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | for (hl = filter_anchor.hl_first; hl != NULL; hl = hl->hl_next) { |
| 345 | if (hl->hl_startpos == pos) |
| 346 | return (1); |
| 347 | } |
| 348 | return (0); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | int |
| 356 | is_hilited(off_t pos, off_t epos, int nohide, int *p_matches) |
| 357 | { |
| 358 | int match; |
| 359 | |
| 360 | if (p_matches != NULL) |
| 361 | *p_matches = 0; |
| 362 | |
| 363 | if (!status_col && |
| 364 | start_attnpos != -1 && |
| 365 | pos < end_attnpos && |
| 366 | (epos == -1 || epos > start_attnpos)) |
| 367 | |
| 368 | |
| 369 | |
| 370 | return (1); |
| 371 | |
| 372 | match = is_hilited_range(pos, epos); |
| 373 | if (!match) |
| 374 | return (0); |
| 375 | |
| 376 | if (p_matches != NULL) |
| 377 | |
| 378 | |
| 379 | |
| 380 | *p_matches = 1; |
| 381 | |
| 382 | if (hilite_search == 0) |
| 383 | |
| 384 | |
| 385 | |
| 386 | return (0); |
| 387 | |
| 388 | if (!nohide && hide_hilite) |
| 389 | |
| 390 | |
| 391 | |
| 392 | return (0); |
| 393 | |
| 394 | return (1); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | static void |
| 401 | add_hilite(struct hilite *anchor, struct hilite *hl) |
| 402 | { |
| 403 | struct hilite *ihl; |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | for (ihl = anchor; ihl->hl_next != NULL; ihl = ihl->hl_next) |
| 42 | | Assuming field 'hl_next' is equal to NULL | |
|
| 43 | | Loop condition is false. Execution continues on line 419 | |
|
| 410 | { |
| 411 | if (ihl->hl_next->hl_startpos > hl->hl_startpos) |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | if (ihl != anchor) |
| |
| 420 | hl->hl_startpos = MAXPOS(hl->hl_startpos, ihl->hl_endpos); |
| 421 | if (ihl->hl_next != NULL) |
| |
| 422 | hl->hl_endpos = MINPOS(hl->hl_endpos, |
| 423 | ihl->hl_next->hl_startpos); |
| 424 | if (hl->hl_startpos >= hl->hl_endpos) { |
| 46 | | Assuming field 'hl_startpos' is >= field 'hl_endpos' | |
|
| |
| 425 | |
| 426 | |
| 427 | |
| 428 | free(hl); |
| |
| 429 | return; |
| 430 | } |
| 431 | hl->hl_next = ihl->hl_next; |
| 432 | ihl->hl_next = hl; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | static void |
| 439 | create_hilites(off_t linepos, int start_index, int end_index, int *chpos) |
| 440 | { |
| 441 | struct hilite *hl; |
| 442 | int i; |
| 443 | |
| 444 | |
| 445 | hl = ecalloc(1, sizeof (struct hilite)); |
| 446 | hl->hl_startpos = linepos + chpos[start_index]; |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | for (i = start_index+1; i <= end_index; i++) { |
| 36 | | Assuming 'i' is <= 'end_index' | |
|
| 37 | | Loop condition is true. Entering loop body | |
|
| 51 | | Assuming 'i' is <= 'end_index' | |
|
| 52 | | Loop condition is true. Entering loop body | |
|
| 457 | if (chpos[i] != chpos[i-1] + 1 || i == end_index) { |
| 38 | | Assuming the condition is false | |
|
| 39 | | Assuming 'i' is equal to 'end_index' | |
|
| |
| 53 | | Assuming the condition is true | |
|
| 458 | hl->hl_endpos = linepos + chpos[i-1] + 1; |
| 54 | | Use of memory after it is freed |
|
| 459 | add_hilite(&hilite_anchor, hl); |
| |
| 49 | | Returning; memory was released via 2nd parameter | |
|
| 460 | |
| 461 | if (i < end_index) { |
| |
| 462 | hl = ecalloc(1, sizeof (struct hilite)); |
| 463 | hl->hl_startpos = linepos + chpos[i]; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | static void |
| 475 | hilite_line(off_t linepos, char *line, int line_len, int *chpos, |
| 476 | char *sp, char *ep) |
| 477 | { |
| 478 | char *searchp; |
| 479 | char *line_end = line + line_len; |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | searchp = line; |
| 492 | do { |
| 493 | if (sp == NULL || ep == NULL) |
| 32 | | Assuming 'sp' is not equal to NULL | |
|
| 33 | | Assuming 'ep' is not equal to NULL | |
|
| |
| 494 | return; |
| 495 | |
| 496 | create_hilites(linepos, (intptr_t)sp - (intptr_t)line, |
| 35 | | Calling 'create_hilites' | |
|
| 497 | (intptr_t)ep - (intptr_t)line, chpos); |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | if (ep > searchp) |
| 504 | searchp = ep; |
| 505 | else if (searchp != line_end) |
| 506 | searchp++; |
| 507 | else |
| 508 | break; |
| 509 | } while (match_pattern(info_compiled(&search_info), search_info.text, |
| 510 | searchp, (intptr_t)line_end - (intptr_t)searchp, &sp, &ep, 1, |
| 511 | search_info.search_type)); |
| 512 | } |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | void |
| 519 | chg_caseless(void) |
| 520 | { |
| 521 | if (!is_ucase_pattern) |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | is_caseless = caseless; |
| 527 | else |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | clear_pattern(&search_info); |
| 533 | } |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | static void |
| 539 | hilite_screen(void) |
| 540 | { |
| 541 | struct scrpos scrpos; |
| 542 | |
| 543 | get_scrpos(&scrpos); |
| 544 | if (scrpos.pos == -1) |
| 545 | return; |
| 546 | prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1); |
| 547 | repaint_hilite(1); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | void |
| 554 | chg_hilite(void) |
| 555 | { |
| 556 | |
| 557 | |
| 558 | |
| 559 | clr_hilite(); |
| 560 | hide_hilite = 0; |
| 561 | |
| 562 | if (hilite_search == OPT_ONPLUS) |
| 563 | |
| 564 | |
| 565 | |
| 566 | hilite_screen(); |
| 567 | } |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | static off_t |
| 573 | search_pos(int search_type) |
| 574 | { |
| 575 | off_t pos; |
| 576 | int linenum; |
| 577 | |
| 578 | if (empty_screen()) { |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | if (search_type & SRCH_FORW) { |
| 587 | pos = ch_zero(); |
| 588 | } else { |
| 589 | pos = ch_length(); |
| 590 | if (pos == -1) { |
| 591 | (void) ch_end_seek(); |
| 592 | pos = ch_length(); |
| 593 | } |
| 594 | } |
| 595 | linenum = 0; |
| 596 | } else { |
| 597 | int add_one = 0; |
| 598 | |
| 599 | if (how_search == OPT_ON) { |
| 600 | |
| 601 | |
| 602 | |
| 603 | if (search_type & SRCH_FORW) |
| 604 | linenum = BOTTOM_PLUS_ONE; |
| 605 | else |
| 606 | linenum = TOP; |
| 607 | } else if (how_search == OPT_ONPLUS && |
| 608 | !(search_type & SRCH_AFTER_TARGET)) { |
| 609 | |
| 610 | |
| 611 | |
| 612 | if (search_type & SRCH_FORW) |
| 613 | linenum = TOP; |
| 614 | else |
| 615 | linenum = BOTTOM_PLUS_ONE; |
| 616 | } else { |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | linenum = jump_sline; |
| 625 | if (search_type & SRCH_FORW) |
| 626 | add_one = 1; |
| 627 | } |
| 628 | linenum = adjsline(linenum); |
| 629 | pos = position(linenum); |
| 630 | if (add_one) |
| 631 | pos = forw_raw_line(pos, NULL, NULL); |
| 632 | } |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | if (search_type & SRCH_FORW) { |
| 638 | while (pos == -1) { |
| 639 | if (++linenum >= sc_height) |
| 640 | break; |
| 641 | pos = position(linenum); |
| 642 | } |
| 643 | } else { |
| 644 | while (pos == -1) { |
| 645 | if (--linenum < 0) |
| 646 | break; |
| 647 | pos = position(linenum); |
| 648 | } |
| 649 | } |
| 650 | return (pos); |
| 651 | } |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | static int |
| 657 | search_range(off_t pos, off_t endpos, int search_type, int matches, |
| 658 | int maxlines, off_t *plinepos, off_t *pendpos) |
| 659 | { |
| 660 | char *line; |
| 661 | char *cline; |
| 662 | int line_len; |
| 663 | off_t linenum; |
| 664 | char *sp, *ep; |
| 665 | int line_match; |
| 666 | int cvt_ops; |
| 667 | int cvt_len; |
| 668 | int *chpos; |
| 669 | off_t linepos, oldpos; |
| 670 | |
| 671 | linenum = find_linenum(pos); |
| 672 | oldpos = pos; |
| 673 | for (;;) { |
| 10 | | Loop condition is true. Entering loop body | |
|
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | if (abort_sigs()) { |
| 11 | | Assuming the condition is false | |
|
| |
| 680 | |
| 681 | |
| 682 | |
| 683 | return (-1); |
| 684 | } |
| 685 | |
| 686 | if ((endpos != -1 && pos >= endpos) || |
| |
| 687 | maxlines == 0) { |
| 688 | |
| 689 | |
| 690 | |
| 691 | if (pendpos != NULL) |
| 692 | *pendpos = pos; |
| 693 | return (matches); |
| 694 | } |
| 695 | if (maxlines > 0) |
| |
| 696 | maxlines--; |
| 697 | |
| 698 | if (search_type & SRCH_FORW) { |
| |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | linepos = pos; |
| 704 | pos = forw_raw_line(pos, &line, &line_len); |
| 705 | if (linenum != 0) |
| 706 | linenum++; |
| 707 | } else { |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | pos = back_raw_line(pos, &line, &line_len); |
| 713 | linepos = pos; |
| 714 | if (linenum != 0) |
| 16 | | Assuming 'linenum' is equal to 0 | |
|
| |
| 715 | linenum--; |
| 716 | } |
| 717 | |
| 718 | if (pos == -1) { |
| 18 | | Assuming the condition is false | |
|
| |
| 719 | |
| 720 | |
| 721 | |
| 722 | if (pendpos != NULL) |
| 723 | *pendpos = oldpos; |
| 724 | return (matches); |
| 725 | } |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | if (linenums && abs((int)(pos - oldpos)) > 2048) |
| 20 | | Assuming 'linenums' is 0 | |
|
| 736 | add_lnum(linenum, pos); |
| 737 | oldpos = pos; |
| 738 | |
| 739 | if (is_filtered(linepos)) |
| |
| 740 | continue; |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | cvt_ops = get_cvt_ops(); |
| 747 | cvt_len = cvt_length(line_len); |
| 748 | cline = ecalloc(1, cvt_len); |
| 749 | chpos = cvt_alloc_chpos(cvt_len); |
| 750 | cvt_text(cline, line, chpos, &line_len, cvt_ops); |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | if ((search_type & SRCH_FIND_ALL) && |
| 22 | | Assuming the condition is false | |
|
| 757 | prev_pattern(&filter_info)) { |
| 758 | int line_filter = |
| 759 | match_pattern(info_compiled(&filter_info), |
| 760 | filter_info.text, cline, line_len, &sp, &ep, 0, |
| 761 | filter_info.search_type); |
| 762 | if (line_filter) { |
| 763 | struct hilite *hl = |
| 764 | ecalloc(1, sizeof (struct hilite)); |
| 765 | hl->hl_startpos = linepos; |
| 766 | hl->hl_endpos = pos; |
| 767 | add_hilite(&filter_anchor, hl); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | if (prev_pattern(&search_info)) { |
| |
| 777 | line_match = match_pattern(info_compiled(&search_info), |
| 778 | search_info.text, cline, line_len, &sp, &ep, 0, |
| 779 | search_type); |
| 780 | if (line_match) { |
| 24 | | Assuming 'line_match' is not equal to 0 | |
|
| |
| 781 | |
| 782 | |
| 783 | |
| 784 | if (search_type & SRCH_FIND_ALL) { |
| |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | hilite_line(linepos, cline, line_len, |
| 793 | chpos, sp, ep); |
| 794 | } else if (--matches <= 0) { |
| 27 | | Assuming the condition is true | |
|
| |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | if (hilite_search == OPT_ON) { |
| 29 | | Assuming 'hilite_search' is equal to OPT_ON | |
|
| |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | clr_hilite(); |
| 806 | hilite_line(linepos, cline, |
| |
| 807 | line_len, chpos, sp, ep); |
| 808 | } |
| 809 | free(cline); |
| 810 | free(chpos); |
| 811 | if (plinepos != NULL) |
| 812 | *plinepos = linepos; |
| 813 | return (0); |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | free(cline); |
| 818 | free(chpos); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | static int |
| 826 | hist_pattern(int search_type) |
| 827 | { |
| 828 | char *pattern; |
| 829 | |
| 830 | set_mlist(ml_search, 0); |
| 831 | pattern = cmd_lastpattern(); |
| 832 | if (pattern == NULL) |
| 833 | return (0); |
| 834 | |
| 835 | if (set_pattern(&search_info, pattern, search_type) < 0) |
| 836 | return (0); |
| 837 | |
| 838 | if (hilite_search == OPT_ONPLUS && !hide_hilite) |
| 839 | hilite_screen(); |
| 840 | |
| 841 | return (1); |
| 842 | } |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | int |
| 854 | search(int search_type, char *pattern, int n) |
| 855 | { |
| 856 | off_t pos; |
| 857 | |
| 858 | if (pattern == NULL || *pattern == '\0') { |
| 1 | Assuming 'pattern' is not equal to NULL | |
|
| 2 | | Assuming the condition is false | |
|
| |
| 859 | |
| 860 | |
| 861 | |
| 862 | search_type |= SRCH_AFTER_TARGET; |
| 863 | if (!prev_pattern(&search_info) && !hist_pattern(search_type)) { |
| 864 | error("No previous regular expression", NULL); |
| 865 | return (-1); |
| 866 | } |
| 867 | if ((search_type & SRCH_NO_REGEX) != |
| 868 | (search_info.search_type & SRCH_NO_REGEX)) { |
| 869 | error("Please re-enter search pattern", NULL); |
| 870 | return (-1); |
| 871 | } |
| 872 | if (hilite_search == OPT_ON) { |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | repaint_hilite(0); |
| 878 | } |
| 879 | if (hilite_search == OPT_ONPLUS && hide_hilite) { |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | hide_hilite = 0; |
| 885 | hilite_screen(); |
| 886 | } |
| 887 | hide_hilite = 0; |
| 888 | } else { |
| 889 | |
| 890 | |
| 891 | |
| 892 | if (set_pattern(&search_info, pattern, search_type) < 0) |
| |
| 893 | return (-1); |
| 894 | if (hilite_search) { |
| 5 | | Assuming 'hilite_search' is 0 | |
|
| |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | repaint_hilite(0); |
| 900 | hide_hilite = 0; |
| 901 | clr_hilite(); |
| 902 | } |
| 903 | if (hilite_search == OPT_ONPLUS) { |
| |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | hilite_screen(); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | pos = search_pos(search_type); |
| 916 | if (pos == -1) { |
| |
| 917 | |
| 918 | |
| 919 | |
| 920 | if (search_type & SRCH_PAST_EOF) |
| 921 | return (n); |
| 922 | |
| 923 | error("Nothing to search", NULL); |
| 924 | return (-1); |
| 925 | } |
| 926 | |
| 927 | n = search_range(pos, -1, search_type, n, -1, &pos, NULL); |
| |
| 928 | if (n != 0) { |
| 929 | |
| 930 | |
| 931 | |
| 932 | if (hilite_search == OPT_ON && n > 0) |
| 933 | |
| 934 | |
| 935 | |
| 936 | repaint_hilite(1); |
| 937 | return (n); |
| 938 | } |
| 939 | |
| 940 | if (!(search_type & SRCH_NO_MOVE)) { |
| 941 | |
| 942 | |
| 943 | |
| 944 | jump_loc(pos, jump_sline); |
| 945 | } |
| 946 | |
| 947 | if (hilite_search == OPT_ON) |
| 948 | |
| 949 | |
| 950 | |
| 951 | repaint_hilite(1); |
| 952 | return (0); |
| 953 | } |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | void |
| 967 | prep_hilite(off_t spos, off_t epos, int maxlines) |
| 968 | { |
| 969 | off_t nprep_startpos = prep_startpos; |
| 970 | off_t nprep_endpos = prep_endpos; |
| 971 | off_t new_epos; |
| 972 | off_t max_epos; |
| 973 | int result; |
| 974 | int i; |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | #define SEARCH_MORE (3*size_linebuf) |
| 981 | |
| 982 | if (!prev_pattern(&search_info) && !is_filtering()) |
| 983 | return; |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | if (maxlines < 0) { |
| 990 | max_epos = -1; |
| 991 | } else { |
| 992 | max_epos = spos; |
| 993 | for (i = 0; i < maxlines; i++) |
| 994 | max_epos = forw_raw_line(max_epos, NULL, NULL); |
| 995 | } |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | if (prep_startpos == -1 || |
| 1004 | (epos != -1 && epos < prep_startpos) || |
| 1005 | spos > prep_endpos) { |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | clr_hilite(); |
| 1011 | clr_filter(); |
| 1012 | if (epos != -1) |
| 1013 | epos += SEARCH_MORE; |
| 1014 | nprep_startpos = spos; |
| 1015 | } else { |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | if (epos != -1) { |
| 1020 | if (epos > prep_endpos) { |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | epos += SEARCH_MORE; |
| 1027 | |
| 1028 | } else { |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | epos = prep_startpos; |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | if (spos < prep_startpos) { |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | if (spos < SEARCH_MORE) |
| 1045 | spos = 0; |
| 1046 | else |
| 1047 | spos -= SEARCH_MORE; |
| 1048 | nprep_startpos = spos; |
| 1049 | } else { |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | spos = prep_endpos; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | if (epos != -1 && max_epos != -1 && |
| 1059 | epos > max_epos) |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | epos = max_epos; |
| 1064 | |
| 1065 | if (epos == -1 || epos > spos) { |
| 1066 | int search_type = SRCH_FORW | SRCH_FIND_ALL; |
| 1067 | search_type |= (search_info.search_type & SRCH_NO_REGEX); |
| 1068 | result = search_range(spos, epos, search_type, 0, |
| 1069 | maxlines, NULL, &new_epos); |
| 1070 | if (result < 0) |
| 1071 | return; |
| 1072 | if (prep_endpos == -1 || new_epos > prep_endpos) |
| 1073 | nprep_endpos = new_epos; |
| 1074 | } |
| 1075 | prep_startpos = nprep_startpos; |
| 1076 | prep_endpos = nprep_endpos; |
| 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | void |
| 1083 | set_filter_pattern(char *pattern, int search_type) |
| 1084 | { |
| 1085 | clr_filter(); |
| 1086 | if (pattern == NULL || *pattern == '\0') |
| 1087 | clear_pattern(&filter_info); |
| 1088 | else |
| 1089 | (void) set_pattern(&filter_info, pattern, search_type); |
| 1090 | screen_trashed = 1; |
| 1091 | } |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | int |
| 1097 | is_filtering(void) |
| 1098 | { |
| 1099 | if (ch_getflags() & CH_HELPFILE) |
| 1100 | return (0); |
| 1101 | return (prev_pattern(&filter_info)); |
| 1102 | } |