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) {
29 *ret
= begin
+ num
- 2;
38 num
= strtol(spec
, &term
, 10);
47 /* it could be a regexp of form /.../ */
48 for (term
= (char *) spec
+ 1; *term
&& *term
!= '/'; term
++) {
55 /* in the scan-only case we are not interested in the regex */
59 /* try [spec+1 .. term-1] as regexp */
61 begin
--; /* input is in human terms */
62 line
= nth_line(data
, begin
);
64 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
65 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
66 const char *cp
= line
+ match
[0].rm_so
;
69 while (begin
++ < lines
) {
70 nline
= nth_line(data
, begin
);
71 if (line
<= cp
&& cp
< nline
)
82 regerror(reg_error
, ®exp
, errbuf
, 1024);
83 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
87 int parse_range_arg(const char *arg
, nth_line_fn_t nth_line_cb
,
88 void *cb_data
, long lines
, long *begin
, long *end
)
90 arg
= parse_loc(arg
, nth_line_cb
, cb_data
, lines
, 1, begin
);
93 arg
= parse_loc(arg
+ 1, nth_line_cb
, cb_data
, lines
, *begin
+ 1, end
);
101 const char *skip_range_arg(const char *arg
)
103 arg
= parse_loc(arg
, NULL
, NULL
, 0, -1, NULL
);
106 arg
= parse_loc(arg
+1, NULL
, NULL
, 0, 0, NULL
);