X-Git-Url: https://git.distorted.org.uk/~mdw/xtoys/blobdiff_plain/203d0643442daebb70dc732a2aeff403154451b7..d72bdcdbbe512494f64102d18879a08163f231a3:/xwait.c diff --git a/xwait.c b/xwait.c index 45bb426..c74d2e2 100644 --- a/xwait.c +++ b/xwait.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: xwait.c,v 1.2 1998/11/18 21:25:06 mdw Exp $ + * $Id: xwait.c,v 1.7 1999/06/19 23:42:37 mdw Exp $ * * Wait until prodded by another X client * @@ -29,6 +29,22 @@ /*----- Revision history --------------------------------------------------* * * $Log: xwait.c,v $ + * Revision 1.7 1999/06/19 23:42:37 mdw + * Improve signal handling. + * + * Revision 1.6 1998/12/11 09:50:07 mdw + * Minor modifications to work with mLib and mgLib. + * + * Revision 1.5 1998/11/30 22:36:53 mdw + * Tidy up tabbing in help texts very slightly. + * + * Revision 1.4 1998/11/21 22:41:19 mdw + * Reap children which die before I get my signal handler installed. + * + * Revision 1.3 1998/11/21 22:30:27 mdw + * Support GNU-style long options throughout, and introduce proper help + * text to all programs. Update manual pages to match. + * * Revision 1.2 1998/11/18 21:25:06 mdw * Reap dead children as they arrive. The previous shell may have * carelessly left them behind. @@ -40,6 +56,7 @@ /*----- Header files ------------------------------------------------------*/ +#include #include #include #include @@ -52,6 +69,9 @@ #include #include +#include +#include + #include "xwait.h" /*----- Main code ---------------------------------------------------------*/ @@ -60,12 +80,24 @@ static void sigchld(int sig) { + int e = errno; while (waitpid(-1, 0, WNOHANG) > 0) ; + errno = e; } /* --- @main@ --- */ +static void version(FILE *fp) +{ + fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS); +} + +static void usage(FILE *fp) +{ + fprintf(fp, "Usage: %s [-f] [-d DISPLAY] [-a ATOM] [-m MSG]\n", QUIS); +} + int main(int argc, char *argv[]) { char *display = 0; @@ -82,11 +114,57 @@ int main(int argc, char *argv[]) /* --- Parse options --- */ + ego(argv[0]); + for (;;) { - int i = getopt(argc, argv, "d:a:m:f"); + static struct option opt[] = { + { "help", 0, 0, 'h' }, + { "usage", 0, 0, 'u' }, + { "version", 0, 0, 'v' }, + { "display", required_argument, 0, 'd' }, + { "atom", required_argument, 0, 'a' }, + { "msg", required_argument, 0, 'm' }, + { "force", 0, 0, 'f' }, + { 0, 0, 0, 0 } + }; + + int i = getopt_long(argc, argv, "d:a:m:f", opt, 0); if (i < 0) break; switch (i) { + case 'h': + version(stdout); + fputs("\n", stdout); + usage(stdout); + fputs( +"\n" +"Waits until signalled by `xtell' or `xshutdown'. Specifically, waits\n" +"until a property with name ATOM is written to the root window with\n" +"contents MSG.\n" +"\n" +"Options:\n" +"\n" +"-h, --help Display this help text\n" +"-u, --usage Display a short usage summary\n" +"-v, --version Display the program's version number\n" +"\n" +"-d, --display=DISPLAY Choose X display to connect to\n" +"-f, --force Run even if this property is waited for by another\n" +" process\n" +"-a, --atom=ATOM\t Choose property name to listen for\n" +"-m, --msg=MSG Choose value of property to wait for\n", + stdout); + exit(0); + break; + case 'u': + usage(stdout); + exit(0); + break; + case 'v': + version(stdout); + exit(0); + break; + case 'd': display = optarg; break; @@ -100,8 +178,7 @@ int main(int argc, char *argv[]) f |= f_force; break; default: - fprintf(stderr, - "Usage: xwait [-f] [-d DISPLAY] [-a ATOM] [-m MSG]\n"); + usage(stderr); exit(EXIT_FAILURE); break; } @@ -161,12 +238,26 @@ int main(int argc, char *argv[]) { struct sigaction sa; + sigset_t ss, oss; + + /* --- Set the handler up --- */ sa.sa_handler = sigchld; sigemptyset(&sa.sa_mask); sigaddset(&sa.sa_mask, SIGCHLD); - sa.sa_flags = 0; + sa.sa_flags = SA_NOCLDSTOP; +#ifdef SA_RESTART + sa.sa_flags |= SA_RESTART; +#endif sigaction(SIGCHLD, &sa, 0); + + /* --- Now reap any which have been waiting around so far --- */ + + sigemptyset(&ss); + sigaddset(&ss, SIGCHLD); + sigprocmask(SIG_BLOCK, &ss, &oss); + sigchld(SIGCHLD); + sigprocmask(SIG_SETMASK, &oss, 0); } /* --- Now wait for an event --- */