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