Skip to content

Commit

Permalink
Implement -stdin.
Browse files Browse the repository at this point in the history
  • Loading branch information
H. S. Teoh committed Jun 9, 2017
1 parent ebd995a commit 85d13f5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/ddmd/globals.d
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ struct Param
bool enforcePropertySyntax;
bool betterC; // be a "better C" compiler; no dependency on D runtime
bool addMain; // add a default main() function
bool readStdin; // read source code from standard input
bool allInst; // generate code for all template instantiations
bool check10378; // check for issues transitioning to 10738
bool bug10378; // use pre- https://issues.dlang.org/show_bug.cgi?id=10378 search strategy
Expand Down Expand Up @@ -221,6 +222,7 @@ struct Global
const(char)* copyright;
const(char)* written;
const(char)* main_d; // dummy filename for dummy main()
const(char)* stdin_d; // dummy filename for reading from stdin
Array!(const(char)*)* path; // Array of char*'s which form the import lookup path
Array!(const(char)*)* filePath; // Array of char*'s which form the file import lookup path

Expand Down Expand Up @@ -339,6 +341,7 @@ struct Global
compiler.vendor = "Digital Mars D";
stdmsg = stdout;
main_d = "__main.d";
stdin_d = "__stdin.d";
errorLimit = 20;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ddmd/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ struct Global
const char *copyright;
const char *written;
const char *main_d; // dummy filename for dummy main()
const char *stdin_d; // dummy filename for reading from stdin
Array<const char *> *path; // Array of char*'s which form the import lookup path
Array<const char *> *filePath; // Array of char*'s which form the file import lookup path

Expand Down
70 changes: 69 additions & 1 deletion src/ddmd/mars.d
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,52 @@ extern (C++) void genCmain(Scope* sc)
rootHasMain = sc._module;
}

/**
* Copies data from a FILE* into a newly-allocated buffer.
*
* The buffer is always null-terminated.
*/
ubyte[] copyFile(FILE* fp)
{
enum bufIncrement = 4096;
size_t pos = 0;
size_t sz = bufIncrement;

ubyte* buf = cast(ubyte*).malloc(sz + 1); // +1 for sentinel
if (buf is null)
{
error(Loc(), "out of memory");
fatal();
}

assert(sz > pos);
size_t rlen;
pos += rlen = fread(buf + pos, 1, sz - pos, fp);
while (!ferror(fp) && !feof(fp))
{
if (pos == sz)
{
sz += bufIncrement;
buf = cast(ubyte*).realloc(buf, sz + 1);
if (buf is null)
{
error(Loc(), "out of memory");
fatal();
}
}
assert(sz > pos);
pos += rlen = fread(buf + pos, 1, sz - pos, fp);
}
if (ferror(fp))
{
error(Loc(), "read error");
fatal();
}

assert(pos < sz+1);
buf[pos] = '\0';
return buf[0 .. pos+1];
}

/**
* DMD's real entry point
Expand Down Expand Up @@ -434,6 +480,8 @@ private int tryMain(size_t argc, const(char)** argv)
}
else if (strcmp(p + 1, "shared") == 0)
global.params.dll = true;
else if (strcmp(p + 1, "stdin") == 0)
global.params.readStdin = true;
else if (strcmp(p + 1, "dylib") == 0)
{
static if (TARGET_OSX)
Expand Down Expand Up @@ -1081,7 +1129,7 @@ Language changes listed by -transition=id:
{
fatal();
}
if (files.dim == 0)
if (files.dim == 0 && !global.params.readStdin)
{
usage();
return EXIT_FAILURE;
Expand Down Expand Up @@ -1210,6 +1258,10 @@ Language changes listed by -transition=id:
{
files.push(cast(char*)global.main_d); // a dummy name, we never actually look up this file
}
if (global.params.readStdin)
{
files.push(cast(char*)global.stdin_d); // a dummy name, we don't actually look it up
}
// Create Modules
Modules modules;
modules.reserve(files.dim);
Expand Down Expand Up @@ -1343,6 +1395,22 @@ Language changes listed by -transition=id:
}
}
}
if (global.params.readStdin)
{
for (size_t i = 0; 1; i++)
{
assert(i != modules.dim);
Module m = modules[i];
if (strcmp(m.srcfile.name.str, global.stdin_d) == 0)
{
/* Slow stdio-based read, because File.read doesn't work with
* stdin. */
auto buf = copyFile(stdin);
m.srcfile.setbuffer(buf.ptr, buf.length);
break;
}
}
}
enum ASYNCREAD = false;
static if (ASYNCREAD)
{
Expand Down

0 comments on commit 85d13f5

Please sign in to comment.