@@ -867,7 +867,7 @@ void free_grep_patterns(struct grep_opt *opt)
867
867
free_pattern_expr (opt -> pattern_expression );
868
868
}
869
869
870
- static char * end_of_line (char * cp , unsigned long * left )
870
+ static const char * end_of_line (const char * cp , unsigned long * left )
871
871
{
872
872
unsigned long l = * left ;
873
873
while (l && * cp != '\n' ) {
@@ -908,7 +908,8 @@ static void show_name(struct grep_opt *opt, const char *name)
908
908
opt -> output (opt , opt -> null_following_name ? "\0" : "\n" , 1 );
909
909
}
910
910
911
- static int patmatch (struct grep_pat * p , char * line , char * eol ,
911
+ static int patmatch (struct grep_pat * p ,
912
+ const char * line , const char * eol ,
912
913
regmatch_t * match , int eflags )
913
914
{
914
915
int hit ;
@@ -922,20 +923,16 @@ static int patmatch(struct grep_pat *p, char *line, char *eol,
922
923
return hit ;
923
924
}
924
925
925
- static int strip_timestamp (char * bol , char * * eol_p )
926
+ static void strip_timestamp (const char * bol , const char * * eol_p )
926
927
{
927
- char * eol = * eol_p ;
928
- int ch ;
928
+ const char * eol = * eol_p ;
929
929
930
930
while (bol < -- eol ) {
931
931
if (* eol != '>' )
932
932
continue ;
933
933
* eol_p = ++ eol ;
934
- ch = * eol ;
935
- * eol = '\0' ;
936
- return ch ;
934
+ break ;
937
935
}
938
- return 0 ;
939
936
}
940
937
941
938
static struct {
@@ -947,12 +944,12 @@ static struct {
947
944
{ "reflog " , 7 },
948
945
};
949
946
950
- static int match_one_pattern (struct grep_pat * p , char * bol , char * eol ,
947
+ static int match_one_pattern (struct grep_pat * p ,
948
+ const char * bol , const char * eol ,
951
949
enum grep_context ctx ,
952
950
regmatch_t * pmatch , int eflags )
953
951
{
954
952
int hit = 0 ;
955
- int saved_ch = 0 ;
956
953
const char * start = bol ;
957
954
958
955
if ((p -> token != GREP_PATTERN ) &&
@@ -971,7 +968,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
971
968
switch (p -> field ) {
972
969
case GREP_HEADER_AUTHOR :
973
970
case GREP_HEADER_COMMITTER :
974
- saved_ch = strip_timestamp (bol , & eol );
971
+ strip_timestamp (bol , & eol );
975
972
break ;
976
973
default :
977
974
break ;
@@ -1021,17 +1018,16 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
1021
1018
goto again ;
1022
1019
}
1023
1020
}
1024
- if (p -> token == GREP_PATTERN_HEAD && saved_ch )
1025
- * eol = saved_ch ;
1026
1021
if (hit ) {
1027
1022
pmatch [0 ].rm_so += bol - start ;
1028
1023
pmatch [0 ].rm_eo += bol - start ;
1029
1024
}
1030
1025
return hit ;
1031
1026
}
1032
1027
1033
- static int match_expr_eval (struct grep_opt * opt , struct grep_expr * x , char * bol ,
1034
- char * eol , enum grep_context ctx , ssize_t * col ,
1028
+ static int match_expr_eval (struct grep_opt * opt , struct grep_expr * x ,
1029
+ const char * bol , const char * eol ,
1030
+ enum grep_context ctx , ssize_t * col ,
1035
1031
ssize_t * icol , int collect_hits )
1036
1032
{
1037
1033
int h = 0 ;
@@ -1098,15 +1094,17 @@ static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, char *bol,
1098
1094
return h ;
1099
1095
}
1100
1096
1101
- static int match_expr (struct grep_opt * opt , char * bol , char * eol ,
1097
+ static int match_expr (struct grep_opt * opt ,
1098
+ const char * bol , const char * eol ,
1102
1099
enum grep_context ctx , ssize_t * col ,
1103
1100
ssize_t * icol , int collect_hits )
1104
1101
{
1105
1102
struct grep_expr * x = opt -> pattern_expression ;
1106
1103
return match_expr_eval (opt , x , bol , eol , ctx , col , icol , collect_hits );
1107
1104
}
1108
1105
1109
- static int match_line (struct grep_opt * opt , char * bol , char * eol ,
1106
+ static int match_line (struct grep_opt * opt ,
1107
+ const char * bol , const char * eol ,
1110
1108
ssize_t * col , ssize_t * icol ,
1111
1109
enum grep_context ctx , int collect_hits )
1112
1110
{
@@ -1138,7 +1136,8 @@ static int match_line(struct grep_opt *opt, char *bol, char *eol,
1138
1136
return hit ;
1139
1137
}
1140
1138
1141
- static int match_next_pattern (struct grep_pat * p , char * bol , char * eol ,
1139
+ static int match_next_pattern (struct grep_pat * p ,
1140
+ const char * bol , const char * eol ,
1142
1141
enum grep_context ctx ,
1143
1142
regmatch_t * pmatch , int eflags )
1144
1143
{
@@ -1159,7 +1158,8 @@ static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1159
1158
return 1 ;
1160
1159
}
1161
1160
1162
- static int next_match (struct grep_opt * opt , char * bol , char * eol ,
1161
+ static int next_match (struct grep_opt * opt ,
1162
+ const char * bol , const char * eol ,
1163
1163
enum grep_context ctx , regmatch_t * pmatch , int eflags )
1164
1164
{
1165
1165
struct grep_pat * p ;
@@ -1215,7 +1215,8 @@ static void show_line_header(struct grep_opt *opt, const char *name,
1215
1215
}
1216
1216
}
1217
1217
1218
- static void show_line (struct grep_opt * opt , char * bol , char * eol ,
1218
+ static void show_line (struct grep_opt * opt ,
1219
+ const char * bol , const char * eol ,
1219
1220
const char * name , unsigned lno , ssize_t cno , char sign )
1220
1221
{
1221
1222
int rest = eol - bol ;
@@ -1246,7 +1247,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1246
1247
if (opt -> color || opt -> only_matching ) {
1247
1248
regmatch_t match ;
1248
1249
enum grep_context ctx = GREP_CONTEXT_BODY ;
1249
- int ch = * eol ;
1250
1250
int eflags = 0 ;
1251
1251
1252
1252
if (opt -> color ) {
@@ -1261,7 +1261,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1261
1261
else if (sign == '=' )
1262
1262
line_color = opt -> colors [GREP_COLOR_FUNCTION ];
1263
1263
}
1264
- * eol = '\0' ;
1265
1264
while (next_match (opt , bol , eol , ctx , & match , eflags )) {
1266
1265
if (match .rm_so == match .rm_eo )
1267
1266
break ;
@@ -1279,7 +1278,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1279
1278
rest -= match .rm_eo ;
1280
1279
eflags = REG_NOTBOL ;
1281
1280
}
1282
- * eol = ch ;
1283
1281
}
1284
1282
if (!opt -> only_matching ) {
1285
1283
output_color (opt , bol , rest , line_color );
@@ -1307,7 +1305,8 @@ static inline void grep_attr_unlock(void)
1307
1305
pthread_mutex_unlock (& grep_attr_mutex );
1308
1306
}
1309
1307
1310
- static int match_funcname (struct grep_opt * opt , struct grep_source * gs , char * bol , char * eol )
1308
+ static int match_funcname (struct grep_opt * opt , struct grep_source * gs ,
1309
+ const char * bol , const char * eol )
1311
1310
{
1312
1311
xdemitconf_t * xecfg = opt -> priv ;
1313
1312
if (xecfg && !xecfg -> find_func ) {
@@ -1334,10 +1333,10 @@ static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bo
1334
1333
}
1335
1334
1336
1335
static void show_funcname_line (struct grep_opt * opt , struct grep_source * gs ,
1337
- char * bol , unsigned lno )
1336
+ const char * bol , unsigned lno )
1338
1337
{
1339
1338
while (bol > gs -> buf ) {
1340
- char * eol = -- bol ;
1339
+ const char * eol = -- bol ;
1341
1340
1342
1341
while (bol > gs -> buf && bol [-1 ] != '\n' )
1343
1342
bol -- ;
@@ -1356,7 +1355,7 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1356
1355
static int is_empty_line (const char * bol , const char * eol );
1357
1356
1358
1357
static void show_pre_context (struct grep_opt * opt , struct grep_source * gs ,
1359
- char * bol , char * end , unsigned lno )
1358
+ const char * bol , const char * end , unsigned lno )
1360
1359
{
1361
1360
unsigned cur = lno , from = 1 , funcname_lno = 0 , orig_from ;
1362
1361
int funcname_needed = !!opt -> funcname , comment_needed = 0 ;
@@ -1376,8 +1375,8 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1376
1375
1377
1376
/* Rewind. */
1378
1377
while (bol > gs -> buf && cur > from ) {
1379
- char * next_bol = bol ;
1380
- char * eol = -- bol ;
1378
+ const char * next_bol = bol ;
1379
+ const char * eol = -- bol ;
1381
1380
1382
1381
while (bol > gs -> buf && bol [-1 ] != '\n' )
1383
1382
bol -- ;
@@ -1408,7 +1407,7 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1408
1407
1409
1408
/* Back forward. */
1410
1409
while (cur < lno ) {
1411
- char * eol = bol , sign = (cur == funcname_lno ) ? '=' : '-' ;
1410
+ const char * eol = bol , sign = (cur == funcname_lno ) ? '=' : '-' ;
1412
1411
1413
1412
while (* eol != '\n' )
1414
1413
eol ++ ;
@@ -1436,12 +1435,12 @@ static int should_lookahead(struct grep_opt *opt)
1436
1435
static int look_ahead (struct grep_opt * opt ,
1437
1436
unsigned long * left_p ,
1438
1437
unsigned * lno_p ,
1439
- char * * bol_p )
1438
+ const char * * bol_p )
1440
1439
{
1441
1440
unsigned lno = * lno_p ;
1442
- char * bol = * bol_p ;
1441
+ const char * bol = * bol_p ;
1443
1442
struct grep_pat * p ;
1444
- char * sp , * last_bol ;
1443
+ const char * sp , * last_bol ;
1445
1444
regoff_t earliest = -1 ;
1446
1445
1447
1446
for (p = opt -> pattern_list ; p ; p = p -> next ) {
@@ -1543,8 +1542,8 @@ static int is_empty_line(const char *bol, const char *eol)
1543
1542
1544
1543
static int grep_source_1 (struct grep_opt * opt , struct grep_source * gs , int collect_hits )
1545
1544
{
1546
- char * bol ;
1547
- char * peek_bol = NULL ;
1545
+ const char * bol ;
1546
+ const char * peek_bol = NULL ;
1548
1547
unsigned long left ;
1549
1548
unsigned lno = 1 ;
1550
1549
unsigned last_hit = 0 ;
@@ -1626,7 +1625,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1626
1625
bol = gs -> buf ;
1627
1626
left = gs -> size ;
1628
1627
while (left ) {
1629
- char * eol , ch ;
1628
+ const char * eol ;
1630
1629
int hit ;
1631
1630
ssize_t cno ;
1632
1631
ssize_t col = -1 , icol = -1 ;
@@ -1647,14 +1646,11 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1647
1646
&& look_ahead (opt , & left , & lno , & bol ))
1648
1647
break ;
1649
1648
eol = end_of_line (bol , & left );
1650
- ch = * eol ;
1651
- * eol = 0 ;
1652
1649
1653
1650
if ((ctx == GREP_CONTEXT_HEAD ) && (eol == bol ))
1654
1651
ctx = GREP_CONTEXT_BODY ;
1655
1652
1656
1653
hit = match_line (opt , bol , eol , & col , & icol , ctx , collect_hits );
1657
- * eol = ch ;
1658
1654
1659
1655
if (collect_hits )
1660
1656
goto next_line ;
@@ -1713,7 +1709,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1713
1709
}
1714
1710
if (show_function && (!peek_bol || peek_bol < bol )) {
1715
1711
unsigned long peek_left = left ;
1716
- char * peek_eol = eol ;
1712
+ const char * peek_eol = eol ;
1717
1713
1718
1714
/*
1719
1715
* Trailing empty lines are not interesting.
@@ -1825,7 +1821,8 @@ int grep_source(struct grep_opt *opt, struct grep_source *gs)
1825
1821
return grep_source_1 (opt , gs , 0 );
1826
1822
}
1827
1823
1828
- static void grep_source_init_buf (struct grep_source * gs , char * buf ,
1824
+ static void grep_source_init_buf (struct grep_source * gs ,
1825
+ const char * buf ,
1829
1826
unsigned long size )
1830
1827
{
1831
1828
gs -> type = GREP_SOURCE_BUF ;
@@ -1837,7 +1834,7 @@ static void grep_source_init_buf(struct grep_source *gs, char *buf,
1837
1834
gs -> identifier = NULL ;
1838
1835
}
1839
1836
1840
- int grep_buffer (struct grep_opt * opt , char * buf , unsigned long size )
1837
+ int grep_buffer (struct grep_opt * opt , const char * buf , unsigned long size )
1841
1838
{
1842
1839
struct grep_source gs ;
1843
1840
int r ;
@@ -1889,7 +1886,9 @@ void grep_source_clear_data(struct grep_source *gs)
1889
1886
switch (gs -> type ) {
1890
1887
case GREP_SOURCE_FILE :
1891
1888
case GREP_SOURCE_OID :
1892
- FREE_AND_NULL (gs -> buf );
1889
+ /* these types own the buffer */
1890
+ free ((char * )gs -> buf );
1891
+ gs -> buf = NULL ;
1893
1892
gs -> size = 0 ;
1894
1893
break ;
1895
1894
case GREP_SOURCE_BUF :
0 commit comments