qcmd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #define _GNU_SOURCE
  2. #include <libnotify/notification.h>
  3. #include <libnotify/notify.h>
  4. #include <glib-2.0/glib.h>
  5. #include <glib-2.0/glib-unix.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <stdio.h>
  11. #include <fcntl.h>
  12. #include <signal.h>
  13. #include <sys/wait.h>
  14. #include <getopt.h>
  15. #include "term.h"
  16. #define READ_BUF_SIZE 4096
  17. #ifdef DEBUG
  18. #define LOG(args...) do {printf("[qcmd] "); printf(args); printf("\n");} while(0)
  19. #else
  20. #define LOG(args...) do {} while(0)
  21. #endif
  22. struct qcmd {
  23. GMainLoop *loop;
  24. NotifyNotification *not;
  25. int cmd_pid;
  26. int cmd_fd;
  27. char *cmd;
  28. int close_on_exit;
  29. struct term terminal;
  30. };
  31. struct qcmd state = {0};
  32. static void clean_up()
  33. {
  34. g_object_unref(G_OBJECT(state.not));
  35. notify_uninit();
  36. close(state.cmd_fd);
  37. }
  38. void cmd_exit()
  39. {
  40. LOG("cmd_exit()");
  41. int status;
  42. pid_t pid = wait(&status);
  43. if (pid > 0) {
  44. if (WIFEXITED(status)) {
  45. if (WEXITSTATUS(status) != 0) {
  46. LOG("setting urgency to critical ...");
  47. notify_notification_set_urgency(state.not, NOTIFY_URGENCY_CRITICAL);
  48. LOG("urgency set");
  49. }
  50. }
  51. }
  52. if (state.close_on_exit) {
  53. notify_notification_close(state.not, NULL);
  54. }
  55. }
  56. static void exit_qcmd() {
  57. kill(state.cmd_pid, SIGKILL);
  58. cmd_exit();
  59. g_main_loop_quit(state.loop);
  60. }
  61. static void not_closed_callback(gpointer data)
  62. {
  63. LOG("not_closed_callback()");
  64. exit_qcmd();
  65. }
  66. static gboolean cmd_read_callback(gint fd, GIOCondition condition, gpointer user_data)
  67. {
  68. char read_buf[READ_BUF_SIZE];
  69. char *output;
  70. if (condition == G_IO_HUP) {
  71. return FALSE;
  72. }
  73. int size = 0;
  74. if ((size = read(fd, read_buf, READ_BUF_SIZE - 1)) > 0) {
  75. LOG("read %d bytes from pipe", size);
  76. read_buf[size] = '\0';
  77. term_put_string(&state.terminal, read_buf);
  78. }
  79. output = term_to_string(&state.terminal);
  80. notify_notification_update(state.not, state.cmd, output, NULL);
  81. notify_notification_show(state.not, NULL);
  82. free(output);
  83. return TRUE;
  84. }
  85. static int cmd_open(char *cmd, int *pid) {
  86. int pt = getpt();
  87. int p = fork();
  88. switch (p) {
  89. case -1:
  90. LOG("Error forking process.");
  91. exit(1);
  92. case 0:
  93. if (-1 == grantpt(pt)) {
  94. exit(1);
  95. }
  96. if (-1 == unlockpt(pt)) {
  97. exit(1);
  98. }
  99. char *pts_path = ptsname(pt);
  100. if (NULL == pts_path) {
  101. exit(1);
  102. }
  103. close(pt);
  104. int pts_fd = open(pts_path, O_RDWR);
  105. if (-1 == pts_fd) {
  106. exit(1);
  107. }
  108. dup2(pts_fd, 1);
  109. dup2(pts_fd, 2);
  110. dup2(pts_fd, 3);
  111. int pts_fl = fcntl(pts_fd, F_GETFL);
  112. if (-1 == pts_fl) {
  113. exit(1);
  114. }
  115. if (-1 == fcntl(pts_fd, F_SETFL, pts_fl | O_NONBLOCK)) {
  116. exit(1);
  117. }
  118. execl("/bin/sh", "sh", "-c", cmd, (char *) NULL);
  119. exit(1);
  120. default:
  121. *pid = p;
  122. return pt;
  123. }
  124. }
  125. void print_help()
  126. {
  127. printf(
  128. "usage: qcmd [options] [command]\n"
  129. " -h, --help Display this help text.\n"
  130. " -r, --rows Number of rows in terminal output.\n"
  131. " -c, --cols Number of columns in terminal output.\n"
  132. " -C, --close-on-exit Close the notification on command exit.\n"
  133. );
  134. }
  135. int main(int argc, char *argv[])
  136. {
  137. char *cmd;
  138. int rows = 10;
  139. int cols = 80;
  140. int c;
  141. int option_index = 0;
  142. int cmd_pid;
  143. int cmd_fd;
  144. while (1) {
  145. static struct option long_options[] = {
  146. {"help", no_argument, 0, 'h'},
  147. {"close-on-exit", no_argument, 0, 'C'},
  148. {0, 0, 0, 0},
  149. };
  150. c = getopt_long(argc, argv, "hCc:r:", long_options, &option_index);
  151. if (c == -1) {
  152. break;
  153. }
  154. switch (c) {
  155. case 'h':
  156. print_help();
  157. return EXIT_SUCCESS;
  158. case 'C':
  159. state.close_on_exit = 1;
  160. break;
  161. case 'r':
  162. rows = atoi(optarg);
  163. break;
  164. case 'c':
  165. cols = atoi(optarg);
  166. break;
  167. }
  168. }
  169. if (optind < argc) {
  170. cmd = argv[optind];
  171. }
  172. if (cmd == NULL || strlen(cmd) == 0) {
  173. printf("error: no command specified.\n");
  174. print_help();
  175. return EXIT_FAILURE;
  176. }
  177. if (rows < 1 || rows > 100) {
  178. printf("error: invalid value %u for argument rows.\n", rows);
  179. return EXIT_FAILURE;
  180. }
  181. if (cols < 1 || cols > 100) {
  182. printf("error: invalid value %u for argument cols.\n", cols);
  183. return EXIT_FAILURE;
  184. }
  185. term_init(&state.terminal, rows, cols);
  186. signal(SIGCHLD, cmd_exit);
  187. LOG("Forking off command '%s'...", cmd);
  188. cmd_fd = cmd_open(cmd, &cmd_pid);
  189. if (-1 == cmd_fd) {
  190. LOG("Error running command.");
  191. return EXIT_FAILURE;
  192. }
  193. state.cmd_fd = cmd_fd;
  194. state.cmd = cmd;
  195. LOG("Process started with PID %u.", cmd_pid);
  196. state.cmd_pid = cmd_pid;
  197. notify_init("qcmd");
  198. NotifyNotification *not = notify_notification_new(cmd, " ", NULL);
  199. state.not = not;
  200. g_signal_connect(G_OBJECT(not),
  201. "closed",
  202. G_CALLBACK(not_closed_callback),
  203. NULL);
  204. notify_notification_set_timeout(not, 0);
  205. notify_notification_show(not, NULL);
  206. g_unix_fd_add(cmd_fd, G_IO_IN | G_IO_HUP | G_IO_ERR, cmd_read_callback, &state);
  207. state.loop = g_main_loop_new(NULL, FALSE);
  208. g_main_loop_run(state.loop);
  209. LOG("Main loop exited.");
  210. clean_up(&state);
  211. return EXIT_SUCCESS;
  212. }