mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-02-17 23:12:46 +08:00
Add some new tricks to fish_test_helper
Allow fish_test_helper to report if it is in the foreground, and to SIGINT its parent.
This commit is contained in:
parent
8bb343879e
commit
a40a4c0c54
|
@ -1,6 +1,7 @@
|
|||
// fish_test_helper is a little program with no fish dependencies that acts like certain other
|
||||
// programs, allowing fish to test its behavior.
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -15,14 +16,41 @@ static void become_foreground_then_print_stderr() {
|
|||
fprintf(stderr, "become_foreground_then_print_stderr done\n");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc <= 1) {
|
||||
fprintf(stderr, "No commands given.");
|
||||
fprintf(stderr, "No commands given.\n");
|
||||
return 0;
|
||||
}
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (!strcmp(argv[i], "become_foreground_then_print_stderr")) {
|
||||
become_foreground_then_print_stderr();
|
||||
} else if (!strcmp(argv[i], "report_foreground")) {
|
||||
report_foreground();
|
||||
} else if (!strcmp(argv[i], "sigint_parent")) {
|
||||
sigint_parent();
|
||||
} else {
|
||||
fprintf(stderr, "%s: Unknown command: %s\n", argv[0], argv[i]);
|
||||
return EXIT_FAILURE;
|
||||
|
|
Loading…
Reference in New Issue
Block a user