Bug 1163891 - Read lines of input in xpcshell/jsshell while properly handling EINTR failures and retrying in response. r=glandium

--HG--
extra : rebase_source : a821bfabfac56e7d9d82dc6b37475d6bae3c0ebc
This commit is contained in:
Jeff Walden 2015-07-02 21:19:22 -07:00
parent c1484ba42b
commit 9f7e9fa682
2 changed files with 31 additions and 14 deletions

View File

@ -259,8 +259,6 @@ extern JS_EXPORT_API(void) add_history(char* line);
static char*
GetLine(FILE* file, const char * prompt)
{
size_t size;
char* buffer;
#ifdef EDITLINE
/*
* Use readline only if file is stdin, because there's no way to specify
@ -283,17 +281,29 @@ GetLine(FILE* file, const char * prompt)
return linep;
}
#endif
size_t len = 0;
if (*prompt != '\0') {
fprintf(gOutFile, "%s", prompt);
fflush(gOutFile);
}
size = 80;
buffer = (char*) malloc(size);
size_t size = 80;
char* buffer = static_cast<char*>(malloc(size));
if (!buffer)
return nullptr;
char* current = buffer;
while (fgets(current, size - len, file)) {
while (true) {
while (true) {
if (fgets(current, size - len, file))
break;
if (errno != EINTR) {
free(buffer);
return nullptr;
}
}
len += strlen(current);
char* t = buffer + len - 1;
if (*t == '\n') {
@ -301,9 +311,10 @@ GetLine(FILE* file, const char * prompt)
*t = '\0';
return buffer;
}
if (len + 1 == size) {
size = size * 2;
char* tmp = (char*) js_realloc(buffer, size);
char* tmp = static_cast<char*>(realloc(buffer, size));
if (!tmp) {
free(buffer);
return nullptr;
@ -312,6 +323,7 @@ GetLine(FILE* file, const char * prompt)
}
current = buffer + len;
}
if (len && !ferror(file))
return buffer;
free(buffer);

View File

@ -193,16 +193,21 @@ GetLocationProperty(JSContext* cx, unsigned argc, Value* vp)
}
static bool
GetLine(JSContext* cx, char* bufp, FILE* file, const char* prompt) {
{
char line[4096] = { '\0' };
fputs(prompt, gOutFile);
fflush(gOutFile);
if ((!fgets(line, sizeof line, file) && errno != EINTR) || feof(file))
GetLine(JSContext* cx, char* bufp, FILE* file, const char* prompt)
{
fputs(prompt, gOutFile);
fflush(gOutFile);
char line[4096] = { '\0' };
while (true) {
if (fgets(line, sizeof line, file)) {
strcpy(bufp, line);
return true;
}
if (errno != EINTR) {
return false;
strcpy(bufp, line);
}
}
return true;
}
static bool