| 1376 | static int is_empty_line(const char *bol, const char *eol); |
| 1377 | |
| 1378 | static void show_pre_context(struct grep_opt *opt, struct grep_source *gs, |
| 1379 | const char *bol, const char *end, unsigned lno) |
| 1380 | { |
| 1381 | unsigned cur = lno, from = 1, funcname_lno = 0, orig_from; |
| 1382 | int funcname_needed = !!opt->funcname, comment_needed = 0; |
| 1383 | |
| 1384 | if (opt->pre_context < lno) |
| 1385 | from = lno - opt->pre_context; |
| 1386 | if (from <= opt->last_shown) |
| 1387 | from = opt->last_shown + 1; |
| 1388 | orig_from = from; |
| 1389 | if (opt->funcbody) { |
| 1390 | if (match_funcname(opt, gs, bol, end)) |
| 1391 | comment_needed = 1; |
| 1392 | else |
| 1393 | funcname_needed = 1; |
| 1394 | from = opt->last_shown + 1; |
| 1395 | } |
| 1396 | |
| 1397 | /* Rewind. */ |
| 1398 | while (bol > gs->buf && cur > from) { |
| 1399 | const char *next_bol = bol; |
| 1400 | const char *eol = --bol; |
| 1401 | |
| 1402 | while (bol > gs->buf && bol[-1] != '\n') |
| 1403 | bol--; |
| 1404 | cur--; |
| 1405 | if (comment_needed && (is_empty_line(bol, eol) || |
| 1406 | match_funcname(opt, gs, bol, eol))) { |
| 1407 | comment_needed = 0; |
| 1408 | from = orig_from; |
| 1409 | if (cur < from) { |
| 1410 | cur++; |
| 1411 | bol = next_bol; |
| 1412 | break; |
| 1413 | } |
| 1414 | } |
| 1415 | if (funcname_needed && match_funcname(opt, gs, bol, eol)) { |
| 1416 | funcname_lno = cur; |
| 1417 | funcname_needed = 0; |
| 1418 | if (opt->funcbody) |
| 1419 | comment_needed = 1; |
| 1420 | else |
| 1421 | from = orig_from; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | /* We need to look even further back to find a function signature. */ |
| 1426 | if (opt->funcname && funcname_needed) |
| 1427 | show_funcname_line(opt, gs, bol, cur); |
| 1428 | |
| 1429 | /* Back forward. */ |
| 1430 | while (cur < lno) { |
| 1431 | const char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-'; |
| 1432 | |
| 1433 | while (*eol != '\n') |
| 1434 | eol++; |
| 1435 | show_line(opt, bol, eol, gs->name, cur, 0, sign); |
no test coverage detected