fgbypid for bash
たわむれに、PID を指定して fg する fgbypid を bash の組み込みコマンドにする拡張モジュールを作ってみた。
参考にしたのはこれ
http://www.kernelthread.com/hanoi/html/lbm.html
と bash 添付の example/loadables。
Ubuntu では独自のビルド方法を採用していて、apt-get build-dep しても example/loadables はコンパイルされないようだ。
こんなときはちょっとだけ FreeBSD がなつかしくなる。
/* fgbypid: fgbypid.c gcc -fpic -I. -I.. -I../.. -I../../lib -I../../builtins\ -I/usr/src/bash-3.2/debian/bash-builtins/usr/include/bash \ -I/usr/src/bash-3.2/bash/include \ -c -o fgbypid.o fgbypid.c ld -x -Bshareable -o fgbypid fgbypid.o Usage: bash$ enable -f ./fgbypid fgbypid bash$ fgbypid PID */ #include <stdio.h> #include "builtins.h" #include "shell.h" #include "jobs.h" #ifndef errno extern int errno; #endif extern char **make_builtin_argv(); fgbypid_main(int argc, char **argv) { int pid; char* cmd = NULL; int jobno; if (argc != 2) { fprintf(stderr, "usage: %s PID\n", argv[0]); return(1); } pid = strtol(argv[1], (char **)NULL, 10); jobno = get_job_by_pid(pid, 0) + 1; if (jobno == 0) { return 1; } else { cmd = malloc(256); sprintf(cmd, "fg %d", jobno); parse_and_execute(cmd, "-c", 0x004); } return(0); } fgbypid_builtin(WORD_LIST *list) { char **v; int c, r; v = make_builtin_argv(list, &c); r = fgbypid_main(c, v); free(v); return r; } char *fgbypid_doc[] = { "Find the job which the specified process belongs to and set it", "foreground.", (char *)0 }; struct builtin fgbypid_struct = { "fgbypid", fgbypid_builtin, BUILTIN_ENABLED, fgbypid_doc, "fgbypid PID", 0 };