X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=gas%2Flisting.c;h=0192dd0f714ed933d7db9b468cd1946092f91b90;hb=6be47f0c48cbd58921a25e7493ca79a6f596dffe;hp=38518e40f17485f683c2dc31d06bd7153c108204;hpb=c71bb85d64f4ddcc7ae1c991e44ff3b48d2a5dc9;p=deliverable%2Fbinutils-gdb.git diff --git a/gas/listing.c b/gas/listing.c index 38518e40f1..0192dd0f71 100644 --- a/gas/listing.c +++ b/gas/listing.c @@ -1,7 +1,5 @@ /* listing.c - maintain assembly listings - Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, - 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010 - Free Software Foundation, Inc. + Copyright (C) 1991-2014 Free Software Foundation, Inc. This file is part of GAS, the GNU Assembler. @@ -255,13 +253,13 @@ listing_message (const char *name, const char *message) void listing_warning (const char *message) { - listing_message (_("Warning:"), message); + listing_message (_("Warning: "), message); } void listing_error (const char *message) { - listing_message (_("Error:"), message); + listing_message (_("Error: "), message); } static file_info_type * @@ -553,7 +551,8 @@ buffer_line (file_info_type *file, char *line, unsigned int size) /* This function rewinds the requested file back to the line requested, reads it in again into the buffer provided and then restores the file - back to its original location. */ + back to its original location. Returns the buffer pointer upon success + or an empty string if an error occurs. */ static char * rebuffer_line (file_info_type * file, @@ -562,13 +561,15 @@ rebuffer_line (file_info_type * file, unsigned int size) { unsigned int count = 0; - unsigned int current_line = 1; + unsigned int current_line; char * p = buffer; long pos; + long pos2; int c; + bfd_boolean found = FALSE; /* Sanity checks. */ - if (file == NULL || buffer == NULL || size == 0 || file->linenum <= linenum) + if (file == NULL || buffer == NULL || size <= 1 || file->linenum <= linenum) return ""; /* Check the cache and see if we last used this file. */ @@ -596,39 +597,72 @@ rebuffer_line (file_info_type * file, } /* Remember where we are in the current file. */ - pos = ftell (last_open_file); + pos2 = pos = ftell (last_open_file); + if (pos < 3) + return ""; + current_line = file->linenum; - /* Go back to the beginning. */ - fseek (last_open_file, 0, SEEK_SET); + /* Leave room for the nul at the end of the buffer. */ + size -= 1; + buffer[size] = 0; - /* Skip lines prior to the one we are interested in. */ - while (current_line < linenum) + /* Increment the current line count by one. + This is to allow for the fact that we are searching for the + start of a previous line, but we do this by detecting end-of-line + character(s) not start-of-line characters. */ + ++ current_line; + + while (pos2 > 0 && ! found) { - /* fgets only stops on newlines and has a size limit, - so we read one character at a time instead. */ - do + char * ptr; + + /* Move backwards through the file, looking for earlier lines. */ + pos2 = (long) size > pos2 ? 0 : pos2 - size; + fseek (last_open_file, pos2, SEEK_SET); + + /* Our caller has kindly provided us with a buffer, so we use it. */ + if (fread (buffer, 1, size, last_open_file) != size) { - c = fgetc (last_open_file); + as_warn (_("unable to rebuffer file: %s\n"), file->filename); + return ""; } - while (c != EOF && c != '\n' && c != '\r'); - ++ current_line; - - if (c == '\r' || c == '\n') + for (ptr = buffer + size; ptr >= buffer; -- ptr) { - int next = fgetc (last_open_file); + if (*ptr == '\n') + { + -- current_line; - /* If '\r' is followed by '\n', swallow that. Likewise, if '\n' - is followed by '\r', swallow that as well. */ - if ((c == '\r' && next != '\n') - || (c == '\n' && next != '\r')) - ungetc (next, last_open_file); + if (current_line == linenum) + { + /* We have found the start of the line we seek. */ + found = TRUE; + + /* FIXME: We could skip the read-in-the-line code + below if we know that we already have the whole + line in the buffer. */ + + /* Advance pos2 to the newline character we have just located. */ + pos2 += (ptr - buffer); + + /* Skip the newline and, if present, the carriage return. */ + if (ptr + 1 == buffer + size) + { + ++pos2; + if (fgetc (last_open_file) == '\r') + ++ pos2; + } + else + pos2 += (ptr[1] == '\r' ? 2 : 1); + + /* Move the file pointer to this location. */ + fseek (last_open_file, pos2, SEEK_SET); + break; + } + } } } - /* Leave room for the nul at the end of the buffer. */ - size -= 1; - /* Read in the line. */ c = fgetc (last_open_file);