##// END OF EJS Templates
chg: enable clang-format on all .c and .h files...
Augie Fackler -
r35977:9724f549 default
parent child Browse files
Show More
@@ -38,11 +38,13 b' struct cmdserveropts {'
38 38 const char **args;
39 39 };
40 40
41 static void initcmdserveropts(struct cmdserveropts *opts) {
41 static void initcmdserveropts(struct cmdserveropts *opts)
42 {
42 43 memset(opts, 0, sizeof(struct cmdserveropts));
43 44 }
44 45
45 static void freecmdserveropts(struct cmdserveropts *opts) {
46 static void freecmdserveropts(struct cmdserveropts *opts)
47 {
46 48 free(opts->args);
47 49 opts->args = NULL;
48 50 opts->argsize = 0;
@@ -59,12 +61,8 b' static size_t testsensitiveflag(const ch'
59 61 const char *name;
60 62 size_t narg;
61 63 } flags[] = {
62 {"--config", 1},
63 {"--cwd", 1},
64 {"--repo", 1},
65 {"--repository", 1},
66 {"--traceback", 0},
67 {"-R", 1},
64 {"--config", 1}, {"--cwd", 1}, {"--repo", 1},
65 {"--repository", 1}, {"--traceback", 0}, {"-R", 1},
68 66 };
69 67 size_t i;
70 68 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); ++i) {
@@ -89,21 +87,21 b' static size_t testsensitiveflag(const ch'
89 87 /*
90 88 * Parse argv[] and put sensitive flags to opts->args
91 89 */
92 static void setcmdserverargs(struct cmdserveropts *opts,
93 int argc, const char *argv[])
90 static void setcmdserverargs(struct cmdserveropts *opts, int argc,
91 const char *argv[])
94 92 {
95 93 size_t i, step;
96 94 opts->argsize = 0;
97 95 for (i = 0, step = 1; i < (size_t)argc; i += step, step = 1) {
98 96 if (!argv[i])
99 continue; /* pass clang-analyse */
97 continue; /* pass clang-analyse */
100 98 if (strcmp(argv[i], "--") == 0)
101 99 break;
102 100 size_t n = testsensitiveflag(argv[i]);
103 101 if (n == 0 || i + n > (size_t)argc)
104 102 continue;
105 opts->args = reallocx(opts->args,
106 (n + opts->argsize) * sizeof(char *));
103 opts->args =
104 reallocx(opts->args, (n + opts->argsize) * sizeof(char *));
107 105 memcpy(opts->args + opts->argsize, argv + i,
108 106 sizeof(char *) * n);
109 107 opts->argsize += n;
@@ -180,8 +178,8 b' static void setcmdserveropts(struct cmds'
180 178 r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename);
181 179 if (r < 0 || (size_t)r >= sizeof(opts->sockname))
182 180 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
183 r = snprintf(opts->initsockname, sizeof(opts->initsockname),
184 "%s.%u", opts->sockname, (unsigned)getpid());
181 r = snprintf(opts->initsockname, sizeof(opts->initsockname), "%s.%u",
182 opts->sockname, (unsigned)getpid());
185 183 if (r < 0 || (size_t)r >= sizeof(opts->initsockname))
186 184 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
187 185 }
@@ -208,11 +206,14 b' static void execcmdserver(const struct c'
208 206 const char *hgcmd = gethgcmd();
209 207
210 208 const char *baseargv[] = {
211 hgcmd,
212 "serve",
213 "--cmdserver", "chgunix",
214 "--address", opts->initsockname,
215 "--daemon-postexec", "chdir:/",
209 hgcmd,
210 "serve",
211 "--cmdserver",
212 "chgunix",
213 "--address",
214 opts->initsockname,
215 "--daemon-postexec",
216 "chdir:/",
216 217 };
217 218 size_t baseargvsize = sizeof(baseargv) / sizeof(baseargv[0]);
218 219 size_t argsize = baseargvsize + opts->argsize + 1;
@@ -237,7 +238,7 b' static hgclient_t *retryconnectcmdserver'
237 238
238 239 debugmsg("try connect to %s repeatedly", opts->initsockname);
239 240
240 unsigned int timeoutsec = 60; /* default: 60 seconds */
241 unsigned int timeoutsec = 60; /* default: 60 seconds */
241 242 const char *timeoutenv = getenv("CHGTIMEOUT");
242 243 if (timeoutenv)
243 244 sscanf(timeoutenv, "%u", &timeoutsec);
@@ -246,7 +247,7 b' static hgclient_t *retryconnectcmdserver'
246 247 hgclient_t *hgc = hgc_open(opts->initsockname);
247 248 if (hgc) {
248 249 debugmsg("rename %s to %s", opts->initsockname,
249 opts->sockname);
250 opts->sockname);
250 251 int r = rename(opts->initsockname, opts->sockname);
251 252 if (r != 0)
252 253 abortmsgerrno("cannot rename");
@@ -270,7 +271,7 b' cleanup:'
270 271 if (WIFEXITED(pst)) {
271 272 if (WEXITSTATUS(pst) == 0)
272 273 abortmsg("could not connect to cmdserver "
273 "(exited with status 0)");
274 "(exited with status 0)");
274 275 debugmsg("cmdserver exited with status %d", WEXITSTATUS(pst));
275 276 exit(WEXITSTATUS(pst));
276 277 } else if (WIFSIGNALED(pst)) {
@@ -284,8 +285,8 b' cleanup:'
284 285 /* Connect to a cmdserver. Will start a new server on demand. */
285 286 static hgclient_t *connectcmdserver(struct cmdserveropts *opts)
286 287 {
287 const char *sockname = opts->redirectsockname[0] ?
288 opts->redirectsockname : opts->sockname;
288 const char *sockname =
289 opts->redirectsockname[0] ? opts->redirectsockname : opts->sockname;
289 290 debugmsg("try connect to %s", sockname);
290 291 hgclient_t *hgc = hgc_open(sockname);
291 292 if (hgc)
@@ -339,8 +340,8 b' static int runinstructions(struct cmdser'
339 340 unlink(*pinst + 7);
340 341 } else if (strncmp(*pinst, "redirect ", 9) == 0) {
341 342 int r = snprintf(opts->redirectsockname,
342 sizeof(opts->redirectsockname),
343 "%s", *pinst + 9);
343 sizeof(opts->redirectsockname), "%s",
344 *pinst + 9);
344 345 if (r < 0 || r >= (int)sizeof(opts->redirectsockname))
345 346 abortmsg("redirect path is too long (%d)", r);
346 347 needreconnect = 1;
@@ -365,10 +366,9 b' static int runinstructions(struct cmdser'
365 366 */
366 367 static int isunsupported(int argc, const char *argv[])
367 368 {
368 enum {
369 SERVE = 1,
370 DAEMON = 2,
371 SERVEDAEMON = SERVE | DAEMON,
369 enum { SERVE = 1,
370 DAEMON = 2,
371 SERVEDAEMON = SERVE | DAEMON,
372 372 };
373 373 unsigned int state = 0;
374 374 int i;
@@ -378,7 +378,7 b' static int isunsupported(int argc, const'
378 378 if (i == 0 && strcmp("serve", argv[i]) == 0)
379 379 state |= SERVE;
380 380 else if (strcmp("-d", argv[i]) == 0 ||
381 strcmp("--daemon", argv[i]) == 0)
381 strcmp("--daemon", argv[i]) == 0)
382 382 state |= DAEMON;
383 383 }
384 384 return (state & SERVEDAEMON) == SERVEDAEMON;
@@ -401,9 +401,9 b' int main(int argc, const char *argv[], c'
401 401
402 402 if (getenv("CHGINTERNALMARK"))
403 403 abortmsg("chg started by chg detected.\n"
404 "Please make sure ${HG:-hg} is not a symlink or "
405 "wrapper to chg. Alternatively, set $CHGHG to the "
406 "path of real hg.");
404 "Please make sure ${HG:-hg} is not a symlink or "
405 "wrapper to chg. Alternatively, set $CHGHG to the "
406 "path of real hg.");
407 407
408 408 if (isunsupported(argc - 1, argv + 1))
409 409 execoriginalhg(argv);
@@ -435,11 +435,11 b' int main(int argc, const char *argv[], c'
435 435 hgc_close(hgc);
436 436 if (++retry > 10)
437 437 abortmsg("too many redirections.\n"
438 "Please make sure %s is not a wrapper which "
439 "changes sensitive environment variables "
440 "before executing hg. If you have to use a "
441 "wrapper, wrap chg instead of hg.",
442 gethgcmd());
438 "Please make sure %s is not a wrapper which "
439 "changes sensitive environment variables "
440 "before executing hg. If you have to use a "
441 "wrapper, wrap chg instead of hg.",
442 gethgcmd());
443 443 }
444 444
445 445 setupsignalhandler(hgc_peerpid(hgc), hgc_peerpgid(hgc));
@@ -7,7 +7,7 b''
7 7 * GNU General Public License version 2 or any later version.
8 8 */
9 9
10 #include <arpa/inet.h> /* for ntohl(), htonl() */
10 #include <arpa/inet.h> /* for ntohl(), htonl() */
11 11 #include <assert.h>
12 12 #include <ctype.h>
13 13 #include <errno.h>
@@ -26,16 +26,15 b''
26 26 #include "procutil.h"
27 27 #include "util.h"
28 28
29 enum {
30 CAP_GETENCODING = 0x0001,
31 CAP_RUNCOMMAND = 0x0002,
32 /* cHg extension: */
33 CAP_ATTACHIO = 0x0100,
34 CAP_CHDIR = 0x0200,
35 CAP_SETENV = 0x0800,
36 CAP_SETUMASK = 0x1000,
37 CAP_VALIDATE = 0x2000,
38 CAP_SETPROCNAME = 0x4000,
29 enum { CAP_GETENCODING = 0x0001,
30 CAP_RUNCOMMAND = 0x0002,
31 /* cHg extension: */
32 CAP_ATTACHIO = 0x0100,
33 CAP_CHDIR = 0x0200,
34 CAP_SETENV = 0x0800,
35 CAP_SETUMASK = 0x1000,
36 CAP_VALIDATE = 0x2000,
37 CAP_SETPROCNAME = 0x4000,
39 38 };
40 39
41 40 typedef struct {
@@ -44,15 +43,15 b' typedef struct {'
44 43 } cappair_t;
45 44
46 45 static const cappair_t captable[] = {
47 {"getencoding", CAP_GETENCODING},
48 {"runcommand", CAP_RUNCOMMAND},
49 {"attachio", CAP_ATTACHIO},
50 {"chdir", CAP_CHDIR},
51 {"setenv", CAP_SETENV},
52 {"setumask", CAP_SETUMASK},
53 {"validate", CAP_VALIDATE},
54 {"setprocname", CAP_SETPROCNAME},
55 {NULL, 0}, /* terminator */
46 {"getencoding", CAP_GETENCODING},
47 {"runcommand", CAP_RUNCOMMAND},
48 {"attachio", CAP_ATTACHIO},
49 {"chdir", CAP_CHDIR},
50 {"setenv", CAP_SETENV},
51 {"setumask", CAP_SETUMASK},
52 {"validate", CAP_VALIDATE},
53 {"setprocname", CAP_SETPROCNAME},
54 {NULL, 0}, /* terminator */
56 55 };
57 56
58 57 typedef struct {
@@ -88,8 +87,8 b' static void enlargecontext(context_t *ct'
88 87 if (newsize <= ctx->maxdatasize)
89 88 return;
90 89
91 newsize = defaultdatasize
92 * ((newsize + defaultdatasize - 1) / defaultdatasize);
90 newsize = defaultdatasize *
91 ((newsize + defaultdatasize - 1) / defaultdatasize);
93 92 ctx->data = reallocx(ctx->data, newsize);
94 93 ctx->maxdatasize = newsize;
95 94 debugmsg("enlarge context buffer to %zu", ctx->maxdatasize);
@@ -126,12 +125,12 b' static void readchannel(hgclient_t *hgc)'
126 125 enlargecontext(&hgc->ctx, hgc->ctx.datasize);
127 126
128 127 if (isupper(hgc->ctx.ch) && hgc->ctx.ch != 'S')
129 return; /* assumes input request */
128 return; /* assumes input request */
130 129
131 130 size_t cursize = 0;
132 131 while (cursize < hgc->ctx.datasize) {
133 132 rsize = recv(hgc->sockfd, hgc->ctx.data + cursize,
134 hgc->ctx.datasize - cursize, 0);
133 hgc->ctx.datasize - cursize, 0);
135 134 if (rsize < 1)
136 135 abortmsg("failed to read data block");
137 136 cursize += rsize;
@@ -176,19 +175,19 b' static void writeblockrequest(const hgcl'
176 175 /* Build '\0'-separated list of args. argsize < 0 denotes that args are
177 176 * terminated by NULL. */
178 177 static void packcmdargs(context_t *ctx, const char *const args[],
179 ssize_t argsize)
178 ssize_t argsize)
180 179 {
181 180 ctx->datasize = 0;
182 181 const char *const *const end = (argsize >= 0) ? args + argsize : NULL;
183 182 for (const char *const *it = args; it != end && *it; ++it) {
184 const size_t n = strlen(*it) + 1; /* include '\0' */
183 const size_t n = strlen(*it) + 1; /* include '\0' */
185 184 enlargecontext(ctx, ctx->datasize + n);
186 185 memcpy(ctx->data + ctx->datasize, *it, n);
187 186 ctx->datasize += n;
188 187 }
189 188
190 189 if (ctx->datasize > 0)
191 --ctx->datasize; /* strip last '\0' */
190 --ctx->datasize; /* strip last '\0' */
192 191 }
193 192
194 193 /* Extract '\0'-separated list of args to new buffer, terminated by NULL */
@@ -199,7 +198,7 b' static const char **unpackcmdargsnul(con'
199 198 const char *s = ctx->data;
200 199 const char *e = ctx->data + ctx->datasize;
201 200 for (;;) {
202 if (nargs + 1 >= maxnargs) { /* including last NULL */
201 if (nargs + 1 >= maxnargs) { /* including last NULL */
203 202 maxnargs += 256;
204 203 args = reallocx(args, maxnargs * sizeof(args[0]));
205 204 }
@@ -237,7 +236,7 b' static void handlesystemrequest(hgclient'
237 236 {
238 237 context_t *ctx = &hgc->ctx;
239 238 enlargecontext(ctx, ctx->datasize + 1);
240 ctx->data[ctx->datasize] = '\0'; /* terminate last string */
239 ctx->data[ctx->datasize] = '\0'; /* terminate last string */
241 240
242 241 const char **args = unpackcmdargsnul(ctx);
243 242 if (!args[0] || !args[1] || !args[2])
@@ -269,8 +268,8 b' static void handleresponse(hgclient_t *h'
269 268 for (;;) {
270 269 readchannel(hgc);
271 270 context_t *ctx = &hgc->ctx;
272 debugmsg("response read from channel %c, size %zu",
273 ctx->ch, ctx->datasize);
271 debugmsg("response read from channel %c, size %zu", ctx->ch,
272 ctx->datasize);
274 273 switch (ctx->ch) {
275 274 case 'o':
276 275 fwrite(ctx->data, sizeof(ctx->data[0]), ctx->datasize,
@@ -299,7 +298,7 b' static void handleresponse(hgclient_t *h'
299 298 default:
300 299 if (isupper(ctx->ch))
301 300 abortmsg("cannot handle response (ch = %c)",
302 ctx->ch);
301 ctx->ch);
303 302 }
304 303 }
305 304 }
@@ -366,8 +365,8 b' static void readhello(hgclient_t *hgc)'
366 365
367 366 static void updateprocname(hgclient_t *hgc)
368 367 {
369 int r = snprintf(hgc->ctx.data, hgc->ctx.maxdatasize,
370 "chg[worker/%d]", (int)getpid());
368 int r = snprintf(hgc->ctx.data, hgc->ctx.maxdatasize, "chg[worker/%d]",
369 (int)getpid());
371 370 if (r < 0 || (size_t)r >= hgc->ctx.maxdatasize)
372 371 abortmsg("insufficient buffer to write procname (r = %d)", r);
373 372 hgc->ctx.datasize = (size_t)r;
@@ -387,7 +386,7 b' static void attachio(hgclient_t *hgc)'
387 386 static const int fds[3] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
388 387 struct msghdr msgh;
389 388 memset(&msgh, 0, sizeof(msgh));
390 struct iovec iov = {ctx->data, ctx->datasize}; /* dummy payload */
389 struct iovec iov = {ctx->data, ctx->datasize}; /* dummy payload */
391 390 msgh.msg_iov = &iov;
392 391 msgh.msg_iovlen = 1;
393 392 char fdbuf[CMSG_SPACE(sizeof(fds))];
@@ -552,7 +551,7 b' pid_t hgc_peerpid(const hgclient_t *hgc)'
552 551 * the last string is guaranteed to be NULL.
553 552 */
554 553 const char **hgc_validate(hgclient_t *hgc, const char *const args[],
555 size_t argsize)
554 size_t argsize)
556 555 {
557 556 assert(hgc);
558 557 if (!(hgc->capflags & CAP_VALIDATE))
@@ -22,9 +22,9 b' pid_t hgc_peerpgid(const hgclient_t *hgc'
22 22 pid_t hgc_peerpid(const hgclient_t *hgc);
23 23
24 24 const char **hgc_validate(hgclient_t *hgc, const char *const args[],
25 size_t argsize);
25 size_t argsize);
26 26 int hgc_runcommand(hgclient_t *hgc, const char *const args[], size_t argsize);
27 27 void hgc_attachio(hgclient_t *hgc);
28 28 void hgc_setenv(hgclient_t *hgc, const char *const envp[]);
29 29
30 #endif /* HGCLIENT_H_ */
30 #endif /* HGCLIENT_H_ */
@@ -54,7 +54,7 b' static void handlestopsignal(int sig)'
54 54 goto error;
55 55
56 56 forwardsignal(sig);
57 if (raise(sig) < 0) /* resend to self */
57 if (raise(sig) < 0) /* resend to self */
58 58 goto error;
59 59 if (sigaction(sig, &sa, &oldsa) < 0)
60 60 goto error;
@@ -205,8 +205,8 b' pid_t setuppager(const char *pagercmd, c'
205 205 close(pipefds[0]);
206 206 close(pipefds[1]);
207 207
208 int r = execle("/bin/sh", "/bin/sh", "-c", pagercmd, NULL,
209 envp);
208 int r =
209 execle("/bin/sh", "/bin/sh", "-c", pagercmd, NULL, envp);
210 210 if (r < 0) {
211 211 abortmsgerrno("cannot start pager '%s'", pagercmd);
212 212 }
@@ -62,7 +62,8 b' void abortmsgerrno(const char *fmt, ...)'
62 62 static int debugmsgenabled = 0;
63 63 static double debugstart = 0;
64 64
65 static double now() {
65 static double now()
66 {
66 67 struct timeval t;
67 68 gettimeofday(&t, NULL);
68 69 return t.tv_usec / 1e6 + t.tv_sec;
@@ -32,4 +32,4 b' void *reallocx(void *ptr, size_t size);'
32 32
33 33 int runshellcmd(const char *cmd, const char *envp[], const char *cwd);
34 34
35 #endif /* UTIL_H_ */
35 #endif /* UTIL_H_ */
@@ -1,12 +1,5 b''
1 1 # Files that just need to be migrated to the formatter.
2 2 # Do not add new files here!
3 contrib/chg/chg.c
4 contrib/chg/hgclient.c
5 contrib/chg/hgclient.h
6 contrib/chg/procutil.c
7 contrib/chg/procutil.h
8 contrib/chg/util.c
9 contrib/chg/util.h
10 3 contrib/hgsh/hgsh.c
11 4 mercurial/cext/base85.c
12 5 mercurial/cext/bdiff.c
General Comments 0
You need to be logged in to leave comments. Login now