531
printf("%.*s\n", rest, bol);
532
}
533
534
+static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
535
+ char *bol, unsigned lno)
536
+{
537
+ unsigned cur = lno, from = 1;
538
+
539
+ if (opt->pre_context < lno)
540
+ from = lno - opt->pre_context;
541
+ if (from <= opt->last_shown)
542
+ from = opt->last_shown + 1;
543
+
544
+ /* Rewind. */
545
+ while (bol > buf && cur > from) {
546
+ bol--;
547
+ while (bol > buf && bol[-1] != '\n')
548
+ bol--;
549
+ cur--;
550
+ }
551
+
552
+ /* Back forward. */
553
+ while (cur < lno) {
554
+ char *eol = bol;
555
+
556
+ while (*eol != '\n')
557
+ eol++;
558
+ show_line(opt, bol, eol, name, cur, '-');
559
+ bol = eol + 1;
560
+ cur++;
561
+ }
562
+}
563
+
564
static int grep_buffer_1(struct grep_opt *opt, const char *name,
565
char *buf, unsigned long size, int collect_hits)
566
{
567
char *bol = buf;
568
unsigned long left = size;
569
unsigned lno = 1;
540
- struct pre_context_line {
541
- char *bol;
542
- char *eol;
543
- } *prev = NULL, *pcl;
570
unsigned last_hit = 0;
571
int binary_match_only = 0;
572
unsigned count = 0;
587
}
588
}
589
564
- if (opt->pre_context)
565
- prev = xcalloc(opt->pre_context, sizeof(*prev));
566
-
590
while (left) {
591
char *eol, ch;
592
int hit;
633
* the context which is nonsense, but the user
634
* deserves to get that ;-).
635
*/
613
- if (opt->pre_context) {
614
- unsigned from;
615
- if (opt->pre_context < lno)
616
- from = lno - opt->pre_context;
617
- else
618
- from = 1;
619
- if (from <= opt->last_shown)
620
- from = opt->last_shown + 1;
621
- while (from < lno) {
622
- pcl = &prev[lno-from-1];
623
- show_line(opt, pcl->bol, pcl->eol,
624
- name, from, '-');
625
- from++;
626
- }
627
- }
636
+ if (opt->pre_context)
637
+ show_pre_context(opt, name, buf, bol, lno);
638
if (!opt->count)
639
show_line(opt, bol, eol, name, lno, ':');
640
last_hit = lno;
646
*/
647
show_line(opt, bol, eol, name, lno, '-');
648
}
639
- if (opt->pre_context) {
640
- memmove(prev+1, prev,
641
- (opt->pre_context-1) * sizeof(*prev));
642
- prev->bol = bol;
643
- prev->eol = eol;
644
- }
649
650
next_line:
651
bol = eol + 1;
655
lno++;
656
}
657
654
- free(prev);
658
if (collect_hits)
659
return 0;
660