diff --git a/src/tools/rc/compile.cpp b/src/tools/rc/compile.cpp index c1c69c3ccb..86993024aa 100644 --- a/src/tools/rc/compile.cpp +++ b/src/tools/rc/compile.cpp @@ -79,11 +79,18 @@ class AddIncludeDir { AddIncludeDir::AddIncludeDir(const char *file) - : - fPath(file) { - if (fPath.GetParent(&fPath) == B_OK) - rdef_add_include_dir(fPath.Path(), false); + // ignore the special stdin file + if (!strcmp(file, "-")) + return; + + if (fPath.SetTo(file) != B_OK + || fPath.GetParent(&fPath) != B_OK) { + fPath.Unset(); + return; + } + + rdef_add_include_dir(fPath.Path(), false); } @@ -187,7 +194,12 @@ compile_file(char *file) { strcpy(lexfile, file); - yyin = fopen(lexfile, "r"); + // "-" means reading from stdin + if (strcmp(file, "-")) + yyin = fopen(lexfile, "r"); + else + yyin = stdin; + if (yyin == NULL) { strcpy(rdef_err_file, lexfile); rdef_err = RDEF_FILE_NOT_FOUND; diff --git a/src/tools/rc/lexer.l b/src/tools/rc/lexer.l index a17f79507e..e2b25f8df5 100644 --- a/src/tools/rc/lexer.l +++ b/src/tools/rc/lexer.l @@ -50,8 +50,7 @@ static void addbuf(uint8); // appends byte to lexbuf // When we encounter an #include directive, we push the current // buffer, filename, and line number on the include stack, so we // can resume lexing that file when we're done with the include. -struct include_t -{ +struct include_t { YY_BUFFER_STATE buffer; char* filename; int lineno; @@ -188,24 +187,22 @@ $\" BEGIN(RAWDATA); resetbuf(); %% //------------------------------------------------------------------------------ -void resetbuf() +void +resetbuf() { lexptr = lexbuf; lexcnt = 0; } -//------------------------------------------------------------------------------ -void addbuf(uint8 b) +void +addbuf(uint8 b) { - if (lexcnt == lexsize) - { + if (lexcnt == lexsize) { lexsize += LEX_BUF_SIZE; lexbuf = (uint8*) realloc(lexbuf, lexsize); if (lexbuf == NULL) - { abort_compile(B_NO_MEMORY, "out of memory"); - } lexptr = lexbuf + lexcnt; } @@ -214,18 +211,16 @@ void addbuf(uint8 b) ++lexcnt; } -//------------------------------------------------------------------------------ -void open_include() +void +open_include() { yytext[yyleng - 1] = '\0'; // remove trailing " quote char tmpname[B_PATH_NAME_LENGTH]; - if (open_file_from_include_dir(yytext, tmpname)) - { + if (open_file_from_include_dir(yytext, tmpname)) { yyin = fopen(tmpname, "r"); - if (yyin != NULL) - { + if (yyin != NULL) { include_t incl; incl.buffer = YY_CURRENT_BUFFER; incl.lineno = yylineno; @@ -240,13 +235,13 @@ void open_include() return; } } - + abort_compile(RDEF_COMPILE_ERR, "cannot open include %s", yytext); } -//------------------------------------------------------------------------------ -void close_include() +void +close_include() { fclose(yyin); yy_delete_buffer(YY_CURRENT_BUFFER); @@ -261,34 +256,31 @@ void close_include() free(incl.filename); } -//------------------------------------------------------------------------------ -void init_lexer() +void +init_lexer() { lexsize = LEX_BUF_SIZE; lexbuf = (uint8*) malloc(lexsize); if (lexbuf == NULL) - { abort_compile(B_NO_MEMORY, "out of memory"); - } yyrestart(yyin); // necessary for multiple input files yylineno = 1; } -//------------------------------------------------------------------------------ -void clean_up_lexer() +void +clean_up_lexer() { - while (!include_stack.empty()) - { + while (!include_stack.empty()) { close_include(); } - fclose(yyin); + if (stdin != yyin) + fclose(yyin); yy_delete_buffer(YY_CURRENT_BUFFER); free(lexbuf); } -//------------------------------------------------------------------------------ diff --git a/src/tools/rc/rc.cpp b/src/tools/rc/rc.cpp index f64f1c9b0a..30fcef35d6 100644 --- a/src/tools/rc/rc.cpp +++ b/src/tools/rc/rc.cpp @@ -210,6 +210,9 @@ parse_options(int argc, char *argv[]) } else if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0) { version(); + } else if (!strcmp(argv[i], "-")) { + // stdin input file + break; } else if (argv[i][0] == '-') { error("unknown option %s", argv[i]); argv[i] = NULL;