2020-04-13 10:25:27 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2020-08-02 16:11:19 -04:00
|
|
|
#include <string.h>
|
2020-04-13 10:25:27 -04:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2020-08-02 16:11:19 -04:00
|
|
|
/* Execute code from target file on the target machine.
|
|
|
|
fname can be "-" for stdin.
|
2020-04-13 10:25:27 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2020-08-02 16:11:19 -04:00
|
|
|
if (argc != 3) {
|
|
|
|
fprintf(stderr, "Usage: ./exec device fname\n");
|
2020-04-13 10:25:27 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2020-08-02 16:11:19 -04:00
|
|
|
FILE *fp = stdin;
|
|
|
|
if (strcmp(argv[2], "-") != 0) {
|
|
|
|
fp = fopen(argv[2], "r");
|
2020-11-17 17:02:10 -05:00
|
|
|
if (fp == NULL) {
|
|
|
|
fprintf(stderr, "Could not open %s\n", argv[2]);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-08-02 16:11:19 -04:00
|
|
|
}
|
2020-07-02 11:36:53 -04:00
|
|
|
int fd = ttyopen(argv[1]);
|
2020-04-13 10:25:27 -04:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "Could not open %s\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
set_blocking(fd, 0);
|
2020-08-02 16:11:19 -04:00
|
|
|
int c = getc(fp);
|
2020-04-13 10:25:27 -04:00
|
|
|
while (c != EOF) {
|
|
|
|
if (c == '\n') c = '\r';
|
|
|
|
write(fd, &c, 1);
|
2020-08-02 16:11:19 -04:00
|
|
|
usleep(1000); // let it breathe
|
2020-04-13 10:25:27 -04:00
|
|
|
while (read(fd, &c, 1) == 1) {
|
|
|
|
putchar(c);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2020-08-02 16:11:19 -04:00
|
|
|
c = getc(fp);
|
|
|
|
}
|
|
|
|
if (fd > 0) {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
if (strcmp(argv[2], "-") != 0) {
|
|
|
|
fclose(fp);
|
2020-04-13 10:25:27 -04:00
|
|
|
}
|
|
|
|
printf("Done!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|