/[pcre]/code/trunk/pcregrep.c
ViewVC logotype

Diff of /code/trunk/pcregrep.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 534 by ph10, Tue Jun 1 13:42:06 2010 UTC revision 535 by ph10, Thu Jun 3 19:18:24 2010 UTC
# Line 104  enum { DEE_READ, DEE_SKIP }; Line 104  enum { DEE_READ, DEE_SKIP };
104    
105  enum { EL_LF, EL_CR, EL_CRLF, EL_ANY, EL_ANYCRLF };  enum { EL_LF, EL_CR, EL_CRLF, EL_ANY, EL_ANYCRLF };
106    
107  /* In newer versions of gcc, with FORTIFY_SOURCE set (the default in some  /* In newer versions of gcc, with FORTIFY_SOURCE set (the default in some
108  environments), a warning is issued if the value of fwrite() is ignored.  environments), a warning is issued if the value of fwrite() is ignored.
109  Unfortunately, casting to (void) does not suppress the warning. To get round  Unfortunately, casting to (void) does not suppress the warning. To get round
110  this, we use a macro that compiles a fudge. Oddly, this does not also seem to  this, we use a macro that compiles a fudge. Oddly, this does not also seem to
111  apply to fprintf(). */  apply to fprintf(). */
112    
113  #define FWRITE(a,b,c,d) if (fwrite(a,b,c,d)) {}  #define FWRITE(a,b,c,d) if (fwrite(a,b,c,d)) {}
# Line 550  return sys_errlist[n]; Line 550  return sys_errlist[n];
550  *            Read one line of input              *  *            Read one line of input              *
551  *************************************************/  *************************************************/
552    
553  /* Normally, input is read using fread() into a large buffer, so many lines may  /* 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  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  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  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,  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.  because there may be binary zeros embedded in the data.
559    
560  Arguments:  Arguments:
561    buffer     the buffer to read into    buffer     the buffer to read into
562    length     the maximum number of characters to read    length     the maximum number of characters to read
563    f          the file    f          the file
564    
565  Returns:     the number of characters read, zero at end of file  Returns:     the number of characters read, zero at end of file
566  */  */
567    
568  static int  static int
569  read_one_line(char *buffer, int length, FILE *f)  read_one_line(char *buffer, int length, FILE *f)
# Line 573  int yield = 0; Line 573  int yield = 0;
573  while ((c = fgetc(f)) != EOF)  while ((c = fgetc(f)) != EOF)
574    {    {
575    buffer[yield++] = c;    buffer[yield++] = c;
576    if (c == '\n' || yield >= length) break;    if (c == '\n' || yield >= length) break;
577    }    }
578  return yield;  return yield;
579  }  }
580    
581    
# Line 1017  else Line 1017  else
1017    {    {
1018    in = (FILE *)handle;    in = (FILE *)handle;
1019    if (is_file_tty(in)) input_line_buffered = TRUE;    if (is_file_tty(in)) input_line_buffered = TRUE;
1020    bufflength = input_line_buffered?    bufflength = input_line_buffered?
1021      read_one_line(buffer, 3*MBUFTHIRD, in) :      read_one_line(buffer, 3*MBUFTHIRD, in) :
1022      fread(buffer, 1, 3*MBUFTHIRD, in);      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
# Line 1321  while (ptr < endptr) Line 1321  while (ptr < endptr)
1321            FWRITE(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout);            FWRITE(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout);
1322            fprintf(stdout, "%c[00m", 0x1b);            fprintf(stdout, "%c[00m", 0x1b);
1323            }            }
1324          FWRITE(ptr + last_offset, 1,          FWRITE(ptr + last_offset, 1,
1325            (linelength + endlinelength) - last_offset, stdout);            (linelength + endlinelength) - last_offset, stdout);
1326          }          }
1327    
# Line 1367  while (ptr < endptr) Line 1367  while (ptr < endptr)
1367    ptr += linelength + endlinelength;    ptr += linelength + endlinelength;
1368    filepos += (int)(linelength + endlinelength);    filepos += (int)(linelength + endlinelength);
1369    linenumber++;    linenumber++;
1370    
1371    /* If input is line buffered, and the buffer is not yet full, read another    /* If input is line buffered, and the buffer is not yet full, read another
1372    line and add it into the buffer. */    line and add it into the buffer. */
1373    
1374    if (input_line_buffered && bufflength < sizeof(buffer))    if (input_line_buffered && bufflength < sizeof(buffer))
1375      {      {
1376      int add = read_one_line(ptr, sizeof(buffer) - (ptr - buffer), in);      int add = read_one_line(ptr, sizeof(buffer) - (ptr - buffer), in);
1377      bufflength += add;      bufflength += add;
1378      endptr += add;      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
# Line 1412  while (ptr < endptr) Line 1412  while (ptr < endptr)
1412      else      else
1413  #endif  #endif
1414    
1415      bufflength = 2*MBUFTHIRD +      bufflength = 2*MBUFTHIRD +
1416        (input_line_buffered?        (input_line_buffered?
1417         read_one_line(buffer + 2*MBUFTHIRD, MBUFTHIRD, in) :         read_one_line(buffer + 2*MBUFTHIRD, MBUFTHIRD, in) :
1418         fread(buffer + 2*MBUFTHIRD, 1, MBUFTHIRD, in));         fread(buffer + 2*MBUFTHIRD, 1, MBUFTHIRD, in));
1419      endptr = buffer + bufflength;      endptr = buffer + bufflength;
1420    
# Line 1766  switch(letter) Line 1766  switch(letter)
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;    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;
# Line 2029  for (i = 1; i < argc; i++) Line 2029  for (i = 1; i < argc; i++)
2029          else                 /* Special case xxx=data */          else                 /* Special case xxx=data */
2030            {            {
2031            int oplen = (int)(equals - op->long_name);            int oplen = (int)(equals - op->long_name);
2032            int arglen = (argequals == NULL)?            int arglen = (argequals == NULL)?
2033              (int)strlen(arg) : (int)(argequals - arg);              (int)strlen(arg) : (int)(argequals - arg);
2034            if (oplen == arglen && strncmp(arg, op->long_name, oplen) == 0)            if (oplen == arglen && strncmp(arg, op->long_name, oplen) == 0)
2035              {              {
# Line 2289  if (colour_option != NULL && strcmp(colo Line 2289  if (colour_option != NULL && strcmp(colo
2289      if (cs != NULL) colour_string = cs;      if (cs != NULL) colour_string = cs;
2290      }      }
2291    }    }
2292    
2293  /* Interpret the newline type; the default settings are Unix-like. */  /* Interpret the newline type; the default settings are Unix-like. */
2294    
2295  if (strcmp(newline, "cr") == 0 || strcmp(newline, "CR") == 0)  if (strcmp(newline, "cr") == 0 || strcmp(newline, "CR") == 0)

Legend:
Removed from v.534  
changed lines
  Added in v.535

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12