2019-04-07 05:22:11 +00:00
|
|
|
// fish_test_helper is a little program with no fish dependencies that acts like certain other
|
|
|
|
// programs, allowing fish to test its behavior.
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-11-19 01:11:16 +00:00
|
|
|
#include <csignal>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
2019-04-07 05:22:11 +00:00
|
|
|
static void become_foreground_then_print_stderr() {
|
|
|
|
if (tcsetpgrp(STDOUT_FILENO, getpgrp()) < 0) {
|
|
|
|
perror("tcsetgrp");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
usleep(1000000 / 4); //.25 secs
|
|
|
|
fprintf(stderr, "become_foreground_then_print_stderr done\n");
|
|
|
|
}
|
|
|
|
|
2019-07-16 04:30:58 +00:00
|
|
|
static void report_foreground() {
|
|
|
|
int was_fg = -1;
|
|
|
|
const auto grp = getpgrp();
|
|
|
|
for (;;) {
|
|
|
|
int is_fg = (tcgetpgrp(STDIN_FILENO) == grp);
|
|
|
|
if (is_fg != was_fg) {
|
|
|
|
was_fg = is_fg;
|
|
|
|
if (fputs(is_fg ? "foreground\n" : "background\n", stderr) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usleep(1000000 / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sigint_parent() {
|
|
|
|
// SIGINT the parent after 1 second, then exit
|
|
|
|
int parent = getppid();
|
|
|
|
usleep(1000000 / 4); //.25 secs
|
|
|
|
kill(parent, SIGINT);
|
|
|
|
fprintf(stderr, "Sent SIGINT to %d\n", parent);
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:06:16 +00:00
|
|
|
static void print_stdout_stderr() {
|
|
|
|
fprintf(stdout, "stdout\n");
|
|
|
|
fprintf(stderr, "stderr\n");
|
|
|
|
fflush(nullptr);
|
|
|
|
}
|
|
|
|
|
2019-04-07 05:22:11 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc <= 1) {
|
2019-07-16 04:30:58 +00:00
|
|
|
fprintf(stderr, "No commands given.\n");
|
2019-04-07 05:22:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "become_foreground_then_print_stderr")) {
|
|
|
|
become_foreground_then_print_stderr();
|
2019-07-16 04:30:58 +00:00
|
|
|
} else if (!strcmp(argv[i], "report_foreground")) {
|
|
|
|
report_foreground();
|
|
|
|
} else if (!strcmp(argv[i], "sigint_parent")) {
|
|
|
|
sigint_parent();
|
2019-10-13 23:06:16 +00:00
|
|
|
} else if (!strcmp(argv[i], "print_stdout_stderr")) {
|
|
|
|
print_stdout_stderr();
|
2019-04-07 05:22:11 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: Unknown command: %s\n", argv[0], argv[i]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|