##// END OF EJS Templates
chg: add util function abortmsgerrno to print error with errno...
Jun Wu -
r28788:57a78a64 default
parent child Browse files
Show More
@@ -1,157 +1,173
1 1 /*
2 2 * Utility functions
3 3 *
4 4 * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org>
5 5 *
6 6 * This software may be used and distributed according to the terms of the
7 7 * GNU General Public License version 2 or any later version.
8 8 */
9 9
10 #include <errno.h>
10 11 #include <signal.h>
11 12 #include <stdarg.h>
12 13 #include <stdio.h>
13 14 #include <stdlib.h>
14 15 #include <string.h>
15 16 #include <sys/types.h>
16 17 #include <sys/wait.h>
17 18 #include <unistd.h>
18 19
19 20 #include "util.h"
20 21
21 22 static int colorenabled = 0;
22 23
23 24 static inline void fsetcolor(FILE *fp, const char *code)
24 25 {
25 26 if (!colorenabled)
26 27 return;
27 28 fprintf(fp, "\033[%sm", code);
28 29 }
29 30
31 static void vabortmsgerrno(int no, const char *fmt, va_list args)
32 {
33 fsetcolor(stderr, "1;31");
34 fputs("chg: abort: ", stderr);
35 vfprintf(stderr, fmt, args);
36 if (no != 0)
37 fprintf(stderr, " (errno = %d, %s)", no, strerror(no));
38 fsetcolor(stderr, "");
39 fputc('\n', stderr);
40 exit(255);
41 }
42
30 43 void abortmsg(const char *fmt, ...)
31 44 {
32 45 va_list args;
33 46 va_start(args, fmt);
34 fsetcolor(stderr, "1;31");
35 fputs("chg: abort: ", stderr);
36 vfprintf(stderr, fmt, args);
37 fsetcolor(stderr, "");
38 fputc('\n', stderr);
47 vabortmsgerrno(0, fmt, args);
39 48 va_end(args);
49 }
40 50
41 exit(255);
51 void abortmsgerrno(const char *fmt, ...)
52 {
53 int no = errno;
54 va_list args;
55 va_start(args, fmt);
56 vabortmsgerrno(no, fmt, args);
57 va_end(args);
42 58 }
43 59
44 60 static int debugmsgenabled = 0;
45 61
46 62 void enablecolor(void)
47 63 {
48 64 colorenabled = 1;
49 65 }
50 66
51 67 void enabledebugmsg(void)
52 68 {
53 69 debugmsgenabled = 1;
54 70 }
55 71
56 72 void debugmsg(const char *fmt, ...)
57 73 {
58 74 if (!debugmsgenabled)
59 75 return;
60 76
61 77 va_list args;
62 78 va_start(args, fmt);
63 79 fsetcolor(stderr, "1;30");
64 80 fputs("chg: debug: ", stderr);
65 81 vfprintf(stderr, fmt, args);
66 82 fsetcolor(stderr, "");
67 83 fputc('\n', stderr);
68 84 va_end(args);
69 85 }
70 86
71 87 void *mallocx(size_t size)
72 88 {
73 89 void *result = malloc(size);
74 90 if (!result)
75 91 abortmsg("failed to malloc");
76 92 return result;
77 93 }
78 94
79 95 void *reallocx(void *ptr, size_t size)
80 96 {
81 97 void *result = realloc(ptr, size);
82 98 if (!result)
83 99 abortmsg("failed to realloc");
84 100 return result;
85 101 }
86 102
87 103 /*
88 104 * Execute a shell command in mostly the same manner as system(), with the
89 105 * give environment variables, after chdir to the given cwd. Returns a status
90 106 * code compatible with the Python subprocess module.
91 107 */
92 108 int runshellcmd(const char *cmd, const char *envp[], const char *cwd)
93 109 {
94 110 enum { F_SIGINT = 1, F_SIGQUIT = 2, F_SIGMASK = 4, F_WAITPID = 8 };
95 111 unsigned int doneflags = 0;
96 112 int status = 0;
97 113 struct sigaction newsa, oldsaint, oldsaquit;
98 114 sigset_t oldmask;
99 115
100 116 /* block or mask signals just as system() does */
101 117 memset(&newsa, 0, sizeof(newsa));
102 118 newsa.sa_handler = SIG_IGN;
103 119 newsa.sa_flags = 0;
104 120 if (sigemptyset(&newsa.sa_mask) < 0)
105 121 goto done;
106 122 if (sigaction(SIGINT, &newsa, &oldsaint) < 0)
107 123 goto done;
108 124 doneflags |= F_SIGINT;
109 125 if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0)
110 126 goto done;
111 127 doneflags |= F_SIGQUIT;
112 128
113 129 if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0)
114 130 goto done;
115 131 if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0)
116 132 goto done;
117 133 doneflags |= F_SIGMASK;
118 134
119 135 pid_t pid = fork();
120 136 if (pid < 0)
121 137 goto done;
122 138 if (pid == 0) {
123 139 sigaction(SIGINT, &oldsaint, NULL);
124 140 sigaction(SIGQUIT, &oldsaquit, NULL);
125 141 sigprocmask(SIG_SETMASK, &oldmask, NULL);
126 142 if (cwd && chdir(cwd) < 0)
127 143 _exit(127);
128 144 const char *argv[] = {"sh", "-c", cmd, NULL};
129 145 if (envp) {
130 146 execve("/bin/sh", (char **)argv, (char **)envp);
131 147 } else {
132 148 execv("/bin/sh", (char **)argv);
133 149 }
134 150 _exit(127);
135 151 } else {
136 152 if (waitpid(pid, &status, 0) < 0)
137 153 goto done;
138 154 doneflags |= F_WAITPID;
139 155 }
140 156
141 157 done:
142 158 if (doneflags & F_SIGINT)
143 159 sigaction(SIGINT, &oldsaint, NULL);
144 160 if (doneflags & F_SIGQUIT)
145 161 sigaction(SIGQUIT, &oldsaquit, NULL);
146 162 if (doneflags & F_SIGMASK)
147 163 sigprocmask(SIG_SETMASK, &oldmask, NULL);
148 164
149 165 /* no way to report other errors, use 127 (= shell termination) */
150 166 if (!(doneflags & F_WAITPID))
151 167 return 127;
152 168 if (WIFEXITED(status))
153 169 return WEXITSTATUS(status);
154 170 if (WIFSIGNALED(status))
155 171 return -WTERMSIG(status);
156 172 return 127;
157 173 }
@@ -1,30 +1,31
1 1 /*
2 2 * Utility functions
3 3 *
4 4 * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org>
5 5 *
6 6 * This software may be used and distributed according to the terms of the
7 7 * GNU General Public License version 2 or any later version.
8 8 */
9 9
10 10 #ifndef UTIL_H_
11 11 #define UTIL_H_
12 12
13 13 #ifdef __GNUC__
14 14 #define PRINTF_FORMAT_ __attribute__((format(printf, 1, 2)))
15 15 #else
16 16 #define PRINTF_FORMAT_
17 17 #endif
18 18
19 19 void abortmsg(const char *fmt, ...) PRINTF_FORMAT_;
20 void abortmsgerrno(const char *fmt, ...) PRINTF_FORMAT_;
20 21
21 22 void enablecolor(void);
22 23 void enabledebugmsg(void);
23 24 void debugmsg(const char *fmt, ...) PRINTF_FORMAT_;
24 25
25 26 void *mallocx(size_t size);
26 27 void *reallocx(void *ptr, size_t size);
27 28
28 29 int runshellcmd(const char *cmd, const char *envp[], const char *cwd);
29 30
30 31 #endif /* UTIL_H_ */
General Comments 0
You need to be logged in to leave comments. Login now