Commits

Anonymous committed 829057e

cleanup and better sigchld handling

Comments (0)

Files changed (1)

 	void (*func)(int);
 };
 
-void erase_usr(void);
-void my_write(int fd, const char *buf, size_t count);
-void print_usr(void);
-void quit(int sig);
-void run(void);
-void sigchld(int sig);
-int  start_pos(int start, int cur, int len, int cols);
-void stop(int unused);
-void term_set(int unused);
-void usage(void);
+static void erase_usr(void);
+static void my_write(int fd, const char *buf, size_t count);
+static void print_usr(void);
+static void quit(int sig);
+static void run(void);
+static void sigchld(int sig);
+static int  start_pos(int start, int cur, int len, int cols);
+static void stop(int unused);
+static void term_set(int unused);
+static void usage(void);
 
 struct sig sig_table[] = {
 	{SIGINT,  "SIGINT" , quit    },
 	{0},
 };
 
-char  buf[4096], usrbuf[4096];
-char  scrl_prompt_left[]  = "<";
-char  scrl_prompt_right[] = ">";
-char  nrml_prompt_default[] = ">";
-char *nrml_prompt = nrml_prompt_default;
-char  word_separators[] = " \t~!@#$%^&*-+=\\|/?.,><";
+static char  buf[4096], usrbuf[4096];
+static char  scrl_prompt_left[]  = "<";
+static char  scrl_prompt_right[] = ">";
+static char  nrml_prompt_default[] = ">";
+static char *nrml_prompt = nrml_prompt_default;
+static char  word_separators[] = " \t~!@#$%^&*-+=\\|/?.,><";
 
-int start = 0, cur = 0, len = 0;
-int mfd;
-pid_t child;
+static int start = 0, cur = 0, len = 0;
+static int mfd;
+static pid_t child;
 
-struct termios set, dfl;
+static struct termios set, dfl;
 
-void usage(void)
+void erase_usr(void)
 {
-	printf("Usage: srw [-p prompt] cmd [args]...\n");
-	exit(1);
-}
-
-void term_set(int unused)
-{
-	tcsetattr(STDIN, TCSANOW, &set);
-	print_usr();
-}
-
-void quit(int sig)
-{
-	tcsetattr(STDIN, TCSANOW, &dfl);
-	exit(sig);
-}
-
-void sigchld(int sig)
-{
-	pid_t pid;
-
-	while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
-		if (pid == child) {
-			printf("\n");
-			warnx("subprocess died");
-			quit(sig);
-		}
-	}
-}
-
-void stop(int unused)
-{
-	tcsetattr(STDIN, TCSANOW, &dfl);
-	kill(-getpid(), SIGSTOP);
+	cursor_beg();
+	cursor_clr();
+	fflush(stdout);
 }
 
 void my_write(int fd, const char *buf, size_t count)
 			warn("failed to write fd = %d", fd);
 }
 
-void erase_usr(void)
-{
-	cursor_beg();
-	cursor_clr();
-	fflush(stdout);
-}
-
-int start_pos(int start, int cur, int len, int cols)
-{
-	int ret;
-
-	ret = start;
-	ret = MIN(ret, len - cols);
-	ret = MAX(ret, 0);
-	ret = MAX(ret, cur - cols);
-	ret = MIN(ret, cur);
-
-	return ret;
-}
-
 void print_usr(void)
 {
 	int cols, left_prompt_len, right_prompt_len = 0;
 	fflush(stdout);
 }
 
+void quit(int sig)
+{
+	tcsetattr(STDIN, TCSANOW, &dfl);
+	exit(sig);
+}
+
 void run(void)
 {
 	static int escape = 0;
 	}
 }
 
+void sigchld(int sig)
+{
+	pid_t pid;
+	int status;
+
+	while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
+		if (pid == child && (WIFEXITED(status) || WIFSIGNALED(status))) {
+			printf("\n");
+			warnx("%s %d", WIFEXITED(status) ? "subprocess exited with status" : "subprocess terminated by signal",
+				  WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status));
+			quit(WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status));
+		}
+	}
+}
+
+int start_pos(int start, int cur, int len, int cols)
+{
+	int ret;
+
+	ret = start;
+	ret = MIN(ret, len - cols);
+	ret = MAX(ret, 0);
+	ret = MAX(ret, cur - cols);
+	ret = MIN(ret, cur);
+
+	return ret;
+}
+
+void stop(int unused)
+{
+	tcsetattr(STDIN, TCSANOW, &dfl);
+	kill(-getpid(), SIGSTOP);
+}
+
+void term_set(int unused)
+{
+	tcsetattr(STDIN, TCSANOW, &set);
+	print_usr();
+}
+
+void usage(void)
+{
+	printf("Usage: srw [-p prompt] cmd [args]...\n");
+	exit(1);
+}
+
 int main(int argc, char **argv)
 {
 	int i;