I have source code of a command line C game
, that waits for user's stdin
, and outputs stdout
. I want to give input from a text field through to the main
of this C
code and take the output in a text area field for example.
int mainer(int argc, char **argv) {
int i;
int hitme;
char ch;
prelim();
if (argc > 1) { // look for -f option
if (strcmp(argv[1], "-f")== 0) {
coordfixed = 1;
argc--;
argv++;
}
}
if (argc > 1) {
fromcommandline = 1;
line[0] = '\0';
while (--argc > 0) {
strcat(line, *(++argv));
strcat(line, " ");
}
}
else fromcommandline = 0;
// some other code
}
From the example here, I should do the following:
let args = [" ", "regular", "short", ""]
var cargs = args.map { strdup($0) }
let result = mainer(Int32(args.count), &cargs)
for ptr in cargs { free(ptr) }
How could I call the main function
, and keep it alive and keep giving to it arguments to make it act as command line
.