1 #include "git-compat-util.h"
2 #include "line-range.h"
5 * Parse one item in the -L option
7 static const char *parse_loc(const char *spec
, nth_line_fn_t nth_line
,
8 void *data
, long lines
, long begin
, long *ret
)
17 /* Allow "-L <something>,+20" to mean starting at <something>
18 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
21 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
22 num
= strtol(spec
+ 1, &term
, 10);
23 if (term
!= spec
+ 1) {
27 *ret
= begin
+ num
- 2;
36 num
= strtol(spec
, &term
, 10);
44 /* it could be a regexp of form /.../ */
45 for (term
= (char *) spec
+ 1; *term
&& *term
!= '/'; term
++) {
52 /* try [spec+1 .. term-1] as regexp */
54 begin
--; /* input is in human terms */
55 line
= nth_line(data
, begin
);
57 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
58 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
59 const char *cp
= line
+ match
[0].rm_so
;
62 while (begin
++ < lines
) {
63 nline
= nth_line(data
, begin
);
64 if (line
<= cp
&& cp
< nline
)
75 regerror(reg_error
, ®exp
, errbuf
, 1024);
76 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
80 int parse_range_arg(const char *arg
, nth_line_fn_t nth_line_cb
,
81 void *cb_data
, long lines
, long *begin
, long *end
)
83 arg
= parse_loc(arg
, nth_line_cb
, cb_data
, lines
, 1, begin
);
86 arg
= parse_loc(arg
+ 1, nth_line_cb
, cb_data
, lines
, *begin
+ 1, end
);