Skip to content

Commit f4e01bf

Browse files
committed
tcc -run: support custom stdin with -rstdin FILE
When tcc -run is used with input file from stdin, then run_main's stdin (which is tcc's stdin) is already EOF after we've read it. If the program operates exclusively on stdin, then it cannot be used. This commit adds an option -rstdin FILE which sets run_main's stdin. Can also be used in such case to set run_main's stdin to the tty, using -rstdin /dev/tty (posix) or -rstdin con (windows).
1 parent 234e2dd commit f4e01bf

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

libtcc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,7 @@ enum {
15471547
TCC_OPTION_rdynamic,
15481548
TCC_OPTION_pthread,
15491549
TCC_OPTION_run,
1550+
TCC_OPTION_rstdin,
15501551
TCC_OPTION_w,
15511552
TCC_OPTION_E,
15521553
TCC_OPTION_M,
@@ -1637,6 +1638,7 @@ static const TCCOption tcc_options[] = {
16371638
{ "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
16381639
{ "pthread", TCC_OPTION_pthread, 0},
16391640
{ "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1641+
{ "rstdin", TCC_OPTION_rstdin, TCC_OPTION_HAS_ARG },
16401642
{ "rdynamic", TCC_OPTION_rdynamic, 0 },
16411643
{ "r", TCC_OPTION_r, 0 },
16421644
{ "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
@@ -2041,6 +2043,12 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv)
20412043
goto set_output_type;
20422044
#else
20432045
return tcc_error_noabort("-run is not available in a cross compiler");
2046+
#endif
2047+
#ifdef TCC_IS_NATIVE
2048+
case TCC_OPTION_rstdin:
2049+
/* custom stdin for run_main */
2050+
s->run_stdin = optarg;
2051+
break;
20442052
#endif
20452053
case TCC_OPTION_v:
20462054
do ++s->verbose; while (*optarg++ == 'v');

tcc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static const char help[] =
3535
"General options:\n"
3636
" -c compile only - generate an object file\n"
3737
" -o outfile set output filename\n"
38-
" -run run compiled source\n"
38+
" -run run compiled source [with custom stdin: -rstdin FILE]\n"
3939
" -fflag set or reset (with 'no-' prefix) 'flag' (see tcc -hh)\n"
4040
" -Wwarning set or reset (with 'no-' prefix) 'warning' (see tcc -hh)\n"
4141
" -w disable all warnings\n"

tcc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,7 @@ struct TCCState {
976976
const char *run_main; /* entry for tcc_run() */
977977
void *run_ptr; /* runtime_memory */
978978
unsigned run_size; /* size of runtime_memory */
979+
const char *run_stdin; /* custom stdin file for run_main */
979980
#ifdef _WIN64
980981
void *run_function_table; /* unwind data */
981982
#endif

tccrun.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,17 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
233233
prog_main = (void*)get_sym_addr(s1, s1->run_main, 1, 1);
234234
if ((addr_t)-1 == (addr_t)prog_main)
235235
return -1;
236+
237+
/* custom stdin for run_main, mainly if stdin is/was an input file.
238+
* fileno(stdin) should remain 0, as posix mandates to use the smallest
239+
* free fd, which is 0 after the initial fclose in freopen. windows too.
240+
* to set stdin to the tty, use /dev/tty (posix) or con (windows).
241+
*/
242+
if (s1->run_stdin && !freopen(s1->run_stdin, "r", stdin)) {
243+
tcc_error_noabort("failed to reopen stdin from '%s'", s1->run_stdin);
244+
return -1;
245+
}
246+
236247
errno = 0; /* clean errno value */
237248
fflush(stdout);
238249
fflush(stderr);

0 commit comments

Comments
 (0)