History

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).

Avi Halachmi (:avih) committed 1 year ago

Commit f4e01bf1 parent 234e2dd4 files · +21 −1Browse files

Unified · Split

4 files
  1. libtcc.c +8 −0
  2. tcc.c +1 −1
  3. tcc.h +1 −0
  4. tccrun.c +11 −0
libtcc.c+8−0
@@ -1547,6 +1547,7 @@ enum { · 1546 unmodified lines
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[] = { · 84 unmodified lines
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) · 398 unmodified lines
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+1−1
@@ -35,7 +35,7 @@ static const char help[] = · 34 unmodified lines
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+1−0
@@ -976,6 +976,7 @@ struct TCCState { · 975 unmodified lines
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+11−0
@@ -233,6 +233,17 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) · 232 unmodified lines
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);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t