| 170 |
static BOOL file_offsets = FALSE; |
static BOOL file_offsets = FALSE; |
| 171 |
static BOOL hyphenpending = FALSE; |
static BOOL hyphenpending = FALSE; |
| 172 |
static BOOL invert = FALSE; |
static BOOL invert = FALSE; |
| 173 |
|
static BOOL line_buffered = FALSE; |
| 174 |
static BOOL line_offsets = FALSE; |
static BOOL line_offsets = FALSE; |
| 175 |
static BOOL multiline = FALSE; |
static BOOL multiline = FALSE; |
| 176 |
static BOOL number = FALSE; |
static BOOL number = FALSE; |
| 207 |
#define N_NULL (-9) |
#define N_NULL (-9) |
| 208 |
#define N_LOFFSETS (-10) |
#define N_LOFFSETS (-10) |
| 209 |
#define N_FOFFSETS (-11) |
#define N_FOFFSETS (-11) |
| 210 |
|
#define N_LBUFFER (-12) |
| 211 |
|
|
| 212 |
static option_item optionlist[] = { |
static option_item optionlist[] = { |
| 213 |
{ OP_NODATA, N_NULL, NULL, "", " terminate options" }, |
{ OP_NODATA, N_NULL, NULL, "", " terminate options" }, |
| 230 |
{ OP_NODATA, 'l', NULL, "files-with-matches", "print only FILE names containing matches" }, |
{ OP_NODATA, 'l', NULL, "files-with-matches", "print only FILE names containing matches" }, |
| 231 |
{ OP_NODATA, 'L', NULL, "files-without-match","print only FILE names not containing matches" }, |
{ OP_NODATA, 'L', NULL, "files-without-match","print only FILE names not containing matches" }, |
| 232 |
{ OP_STRING, N_LABEL, &stdin_name, "label=name", "set name for standard input" }, |
{ OP_STRING, N_LABEL, &stdin_name, "label=name", "set name for standard input" }, |
| 233 |
|
{ OP_NODATA, N_LBUFFER, NULL, "line-buffered", "use line buffering" }, |
| 234 |
{ OP_NODATA, N_LOFFSETS, NULL, "line-offsets", "output line numbers and offsets, not text" }, |
{ OP_NODATA, N_LOFFSETS, NULL, "line-offsets", "output line numbers and offsets, not text" }, |
| 235 |
{ OP_STRING, N_LOCALE, &locale, "locale=locale", "use the named locale" }, |
{ OP_STRING, N_LOCALE, &locale, "locale=locale", "use the named locale" }, |
| 236 |
{ OP_NODATA, 'M', NULL, "multiline", "run in multiline mode" }, |
{ OP_NODATA, 'M', NULL, "multiline", "run in multiline mode" }, |
| 342 |
} |
} |
| 343 |
|
|
| 344 |
|
|
| 345 |
/************* Test stdout for being a terminal in Unix **********/ |
/************* Test for a terminal in Unix **********/ |
| 346 |
|
|
| 347 |
static BOOL |
static BOOL |
| 348 |
is_stdout_tty(void) |
is_stdout_tty(void) |
| 350 |
return isatty(fileno(stdout)); |
return isatty(fileno(stdout)); |
| 351 |
} |
} |
| 352 |
|
|
| 353 |
|
static BOOL |
| 354 |
|
is_file_tty(FILE *f) |
| 355 |
|
{ |
| 356 |
|
return isatty(fileno(f)); |
| 357 |
|
} |
| 358 |
|
|
| 359 |
|
|
| 360 |
/************* Directory scanning in Win32 ***********/ |
/************* Directory scanning in Win32 ***********/ |
| 361 |
|
|
| 468 |
} |
} |
| 469 |
|
|
| 470 |
|
|
| 471 |
/************* Test stdout for being a terminal in Win32 **********/ |
/************* Test for a terminal in Win32 **********/ |
| 472 |
|
|
| 473 |
/* I don't know how to do this; assume never */ |
/* I don't know how to do this; assume never */ |
| 474 |
|
|
| 478 |
return FALSE; |
return FALSE; |
| 479 |
} |
} |
| 480 |
|
|
| 481 |
|
static BOOL |
| 482 |
|
is_file_tty(FILE *f) |
| 483 |
|
{ |
| 484 |
|
return FALSE; |
| 485 |
|
} |
| 486 |
|
|
| 487 |
|
|
| 488 |
/************* Directory scanning when we can't do it ***********/ |
/************* Directory scanning when we can't do it ***********/ |
| 489 |
|
|
| 506 |
int isregfile(char *filename) { return 1; } |
int isregfile(char *filename) { return 1; } |
| 507 |
|
|
| 508 |
|
|
| 509 |
/************* Test stdout for being a terminal when we can't do it **********/ |
/************* Test for a terminal when we can't do it **********/ |
| 510 |
|
|
| 511 |
static BOOL |
static BOOL |
| 512 |
is_stdout_tty(void) |
is_stdout_tty(void) |
| 514 |
return FALSE; |
return FALSE; |
| 515 |
} |
} |
| 516 |
|
|
| 517 |
|
static BOOL |
| 518 |
|
is_file_tty(FILE *f) |
| 519 |
|
{ |
| 520 |
|
return FALSE; |
| 521 |
|
} |
| 522 |
|
|
| 523 |
#endif |
#endif |
| 524 |
|
|
| 547 |
|
|
| 548 |
|
|
| 549 |
/************************************************* |
/************************************************* |
| 550 |
|
* Read one line of input * |
| 551 |
|
*************************************************/ |
| 552 |
|
|
| 553 |
|
/* Normally, input is read using fread() into a large buffer, so many lines may |
| 554 |
|
be read at once. However, doing this for tty input means that no output appears |
| 555 |
|
until a lot of input has been typed. Instead, tty input is handled line by |
| 556 |
|
line. We cannot use fgets() for this, because it does not stop at a binary |
| 557 |
|
zero, and therefore there is no way of telling how many characters it has read, |
| 558 |
|
because there may be binary zeros embedded in the data. |
| 559 |
|
|
| 560 |
|
Arguments: |
| 561 |
|
buffer the buffer to read into |
| 562 |
|
length the maximum number of characters to read |
| 563 |
|
f the file |
| 564 |
|
|
| 565 |
|
Returns: the number of characters read, zero at end of file |
| 566 |
|
*/ |
| 567 |
|
|
| 568 |
|
static int |
| 569 |
|
read_one_line(char *buffer, int length, FILE *f) |
| 570 |
|
{ |
| 571 |
|
int c; |
| 572 |
|
int yield = 0; |
| 573 |
|
while ((c = fgetc(f)) != EOF) |
| 574 |
|
{ |
| 575 |
|
buffer[yield++] = c; |
| 576 |
|
if (c == '\n' || yield >= length) break; |
| 577 |
|
} |
| 578 |
|
return yield; |
| 579 |
|
} |
| 580 |
|
|
| 581 |
|
|
| 582 |
|
|
| 583 |
|
/************************************************* |
| 584 |
* Find end of line * |
* Find end of line * |
| 585 |
*************************************************/ |
*************************************************/ |
| 586 |
|
|
| 978 |
char *endptr; |
char *endptr; |
| 979 |
size_t bufflength; |
size_t bufflength; |
| 980 |
BOOL endhyphenpending = FALSE; |
BOOL endhyphenpending = FALSE; |
| 981 |
|
BOOL input_line_buffered = line_buffered; |
| 982 |
FILE *in = NULL; /* Ensure initialized */ |
FILE *in = NULL; /* Ensure initialized */ |
| 983 |
|
|
| 984 |
#ifdef SUPPORT_LIBZ |
#ifdef SUPPORT_LIBZ |
| 1016 |
|
|
| 1017 |
{ |
{ |
| 1018 |
in = (FILE *)handle; |
in = (FILE *)handle; |
| 1019 |
bufflength = fread(buffer, 1, 3*MBUFTHIRD, in); |
if (is_file_tty(in)) input_line_buffered = TRUE; |
| 1020 |
|
bufflength = input_line_buffered? |
| 1021 |
|
read_one_line(buffer, 3*MBUFTHIRD, in) : |
| 1022 |
|
fread(buffer, 1, 3*MBUFTHIRD, in); |
| 1023 |
} |
} |
| 1024 |
|
|
| 1025 |
endptr = buffer + bufflength; |
endptr = buffer + bufflength; |
| 1026 |
|
|
| 1027 |
/* Loop while the current pointer is not at the end of the file. For large |
/* Loop while the current pointer is not at the end of the file. For large |
| 1330 |
else FWRITE(ptr, 1, linelength + endlinelength, stdout); |
else FWRITE(ptr, 1, linelength + endlinelength, stdout); |
| 1331 |
} |
} |
| 1332 |
|
|
| 1333 |
/* End of doing what has to be done for a match */ |
/* End of doing what has to be done for a match. If --line-buffered was |
| 1334 |
|
given, flush the output. */ |
| 1335 |
|
|
| 1336 |
|
if (line_buffered) fflush(stdout); |
| 1337 |
rc = 0; /* Had some success */ |
rc = 0; /* Had some success */ |
| 1338 |
|
|
| 1339 |
/* Remember where the last match happened for after_context. We remember |
/* Remember where the last match happened for after_context. We remember |
| 1367 |
ptr += linelength + endlinelength; |
ptr += linelength + endlinelength; |
| 1368 |
filepos += linelength + endlinelength; |
filepos += linelength + endlinelength; |
| 1369 |
linenumber++; |
linenumber++; |
| 1370 |
|
|
| 1371 |
|
/* If input is line buffered, and the buffer is not yet full, read another |
| 1372 |
|
line and add it into the buffer. */ |
| 1373 |
|
|
| 1374 |
|
if (input_line_buffered && bufflength < sizeof(buffer)) |
| 1375 |
|
{ |
| 1376 |
|
int add = read_one_line(ptr, sizeof(buffer) - (ptr - buffer), in); |
| 1377 |
|
bufflength += add; |
| 1378 |
|
endptr += add; |
| 1379 |
|
} |
| 1380 |
|
|
| 1381 |
/* If we haven't yet reached the end of the file (the buffer is full), and |
/* If we haven't yet reached the end of the file (the buffer is full), and |
| 1382 |
the current point is in the top 1/3 of the buffer, slide the buffer down by |
the current point is in the top 1/3 of the buffer, slide the buffer down by |
| 1412 |
else |
else |
| 1413 |
#endif |
#endif |
| 1414 |
|
|
| 1415 |
bufflength = 2*MBUFTHIRD + fread(buffer + 2*MBUFTHIRD, 1, MBUFTHIRD, in); |
bufflength = 2*MBUFTHIRD + |
| 1416 |
|
(input_line_buffered? |
| 1417 |
|
read_one_line(buffer + 2*MBUFTHIRD, MBUFTHIRD, in) : |
| 1418 |
|
fread(buffer + 2*MBUFTHIRD, 1, MBUFTHIRD, in)); |
| 1419 |
endptr = buffer + bufflength; |
endptr = buffer + bufflength; |
| 1420 |
|
|
| 1421 |
/* Adjust any last match point */ |
/* Adjust any last match point */ |
| 1766 |
case N_FOFFSETS: file_offsets = TRUE; break; |
case N_FOFFSETS: file_offsets = TRUE; break; |
| 1767 |
case N_HELP: help(); exit(0); |
case N_HELP: help(); exit(0); |
| 1768 |
case N_LOFFSETS: line_offsets = number = TRUE; break; |
case N_LOFFSETS: line_offsets = number = TRUE; break; |
| 1769 |
|
case N_LBUFFER: line_buffered = TRUE; break; |
| 1770 |
case 'c': count_only = TRUE; break; |
case 'c': count_only = TRUE; break; |
| 1771 |
case 'F': process_options |= PO_FIXED_STRINGS; break; |
case 'F': process_options |= PO_FIXED_STRINGS; break; |
| 1772 |
case 'H': filenames = FN_FORCE; break; |
case 'H': filenames = FN_FORCE; break; |
| 2288 |
if (cs != NULL) colour_string = cs; |
if (cs != NULL) colour_string = cs; |
| 2289 |
} |
} |
| 2290 |
} |
} |
| 2291 |
|
|
| 2292 |
/* Interpret the newline type; the default settings are Unix-like. */ |
/* Interpret the newline type; the default settings are Unix-like. */ |
| 2293 |
|
|
| 2294 |
if (strcmp(newline, "cr") == 0 || strcmp(newline, "CR") == 0) |
if (strcmp(newline, "cr") == 0 || strcmp(newline, "CR") == 0) |