2 * Copyright (c) 2015 Hans Petter Selasky. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/queue.h>
40 extern char **environ;
42 static int opt_verbose;
43 static char *opt_diff_tool;
45 #define BLOCK_SIZE 4096
46 #define BLOCK_MASK 0x100
47 #define BLOCK_ADD 0x200
50 TAILQ_ENTRY(block) entry;
57 typedef TAILQ_HEAD(, block) block_head_t;
68 size_t size = sizeof(*pb) + (2 * BLOCK_SIZE);
72 errx(EX_SOFTWARE, "Out of memory");
74 pb->data = (void *)(pb + 1);
75 pb->mask = pb->data + BLOCK_SIZE;
76 pb->length = BLOCK_SIZE;
81 write_block(int fd, block_head_t *ph)
88 TAILQ_FOREACH(ptr, ph, entry) {
89 if (write(fd, ptr->data, ptr->length) != ptr->length)
96 peek_block(block_head_t *pbh, uint64_t off)
100 TAILQ_FOREACH(ptr, pbh, entry) {
101 if (off < ptr->length)
107 return (ptr->data[off] | (ptr->mask[off] << 8));
111 set_block(block_head_t *pbh, uint64_t off, uint16_t ch)
115 TAILQ_FOREACH(ptr, pbh, entry) {
116 if (off < ptr->length)
122 ptr->data[off] = ch & 0xFF;
123 ptr->mask[off] = (ch >> 8) & 0xFF;
127 size_block(block_head_t *pbh)
132 TAILQ_FOREACH(ptr, pbh, entry)
138 diff_tool(block_head_t *pa, block_head_t *pb)
140 char ca[] = {"/tmp/diff.orig.XXXXXX"};
141 char cb[] = {"/tmp/diff.styled.XXXXXX"};
152 s = (sa > sb) ? sa : sb;
154 for (x = 0; x != s; x++) {
155 char cha = peek_block(pa, x) & 0xFF;
156 char chb = peek_block(pb, x) & 0xFF;
160 if (cha == '\n' && chb == 0 && x == sa - 1)
166 return (0); /* identical */
171 if (write_block(fa, pa) < 0 || write_block(fb, pb) < 0) {
176 err(EX_SOFTWARE, "Could not write data to temporary files");
181 snprintf(cc, sizeof(cc), "%s %s %s", opt_diff_tool, ca, cb);
190 diff_block(block_head_t *pa, block_head_t *pb)
192 uint64_t sa = size_block(pa);
193 uint64_t sb = size_block(pb);
199 s = (sa > sb) ? sa : sb;
201 for (y = x = 0; x != s; x++) {
202 char cha = peek_block(pa, x) & 0xFF;
203 char chb = peek_block(pb, x) & 0xFF;
209 if (cha == '\n' && chb == 0 && x == sa - 1)
213 printf("Style error:\n");
215 for (n = y; n < sa; n++) {
216 char ch = peek_block(pa, n) & 0xFF;
218 if (nonspace && ch == '\n')
225 printf("Style corrected:\n");
227 for (n = y; n < sb; n++) {
228 char ch = peek_block(pb, n) & 0xFF;
230 if (nonspace && ch == '\n')
237 for (n = y; n != x; n++) {
238 if ((peek_block(pa, n) & 0xFF) == '\t')
243 printf("^ %sdifference%s\n",
244 (isspace(cha) || isspace(chb)) ? "whitespace " : "",
245 (x >= sa || x >= sb) ? " in the end of a block" : "");
247 } else if (cha == '\n') {
255 free_block(block_head_t *pbh)
259 while ((ptr = TAILQ_FIRST(pbh))) {
260 TAILQ_REMOVE(pbh, ptr, entry);
266 cmd_popen(char *command, FILE **iop)
275 if (pipe(pdes + 2) < 0) {
285 switch ((pid = vfork())) {
286 case -1: /* Error. */
293 dup2(pdes[1], STDOUT_FILENO);
294 dup2(pdes[2], STDIN_FILENO);
297 execve("/bin/sh", argv, environ);
302 iop[0] = fdopen(pdes[3], "w");
303 iop[1] = fdopen(pdes[0], "r");
308 iop[0] = iop[1] = NULL;
312 cmd_block_process(block_head_t *pbh_in, block_head_t *pbh_out, char *cmd_str)
319 cmd_popen(cmd_str, pfd);
321 if (pfd[0] == NULL || pfd[1] == NULL)
322 errx(EX_SOFTWARE, "Cannot invoke command '%s'", cmd_str);
324 if (pbh_in != NULL) {
325 TAILQ_FOREACH(ptr, pbh_in, entry) {
326 if (fwrite(ptr->data, 1, ptr->length, pfd[0]) != ptr->length)
327 err(EX_SOFTWARE, "Cannot write all data to command '%s'", cmd_str);
337 len = fread(ptr->data, 1, BLOCK_SIZE, pfd[1]);
343 TAILQ_INSERT_TAIL(pbh_out, ptr, entry);
352 "indent_wrapper [-v] [-d] [-D] [-g <githash>]\n"
353 "\t" "[-s <svnrevision> ] [ -t <tool> ] [ -c <command> ]\n"
354 "\t" "-v Increase verbosity\n"
355 "\t" "-d Check output from git diff\n"
356 "\t" "-D Check output from svn diff\n"
357 "\t" "-c <cmd> Set custom command to produce diff\n"
358 "\t" "-g <hash> Check output from git hash\n"
359 "\t" "-s <rev> Check output from svn revision\n"
360 "\t" "-t <tool> Launch external diff tool\n"
363 "\t" "indent_wrapper -D\n"
364 "\t" "indent_wrapper -D -t meld\n"
365 "\t" "indent_wrapper -D -t \"diff -u\"\n");
370 main(int argc, char **argv)
372 block_head_t diff_head;
373 block_head_t diff_a_head;
374 block_head_t diff_b_head;
375 block_head_t indent_in_head;
376 block_head_t indent_out_head;
377 struct block *p1 = NULL;
378 struct block *p2 = NULL;
384 int inside_string = 0;
393 signal(SIGPIPE, &sigpipe);
397 while ((c = getopt(argc, argv, "dDvg:s:c:ht:")) != -1) {
403 opt_diff_tool = optarg;
406 snprintf(cmdbuf, sizeof(cmdbuf), "git show -U1000000 %s", optarg);
409 snprintf(cmdbuf, sizeof(cmdbuf), "git diff -U1000000");
412 snprintf(cmdbuf, sizeof(cmdbuf), "svn diff --diff-cmd=diff -x -U1000000");
415 snprintf(cmdbuf, sizeof(cmdbuf), "svn diff --diff-cmd=diff -x -U1000000 -r %s", optarg);
418 snprintf(cmdbuf, sizeof(cmdbuf), "%s", optarg);
427 cmd_block_process(NULL, &diff_head, cmdbuf);
429 TAILQ_INIT(&diff_a_head);
430 TAILQ_INIT(&diff_b_head);
432 size = size_block(&diff_head);
438 for (x = 0; x < size;) {
439 ch = peek_block(&diff_head, x);
442 if (ch == peek_block(&diff_head, x + 1) &&
443 ch == peek_block(&diff_head, x + 2) &&
444 ' ' == (peek_block(&diff_head, x + 3) & 0xFF))
448 for (x++; x != size; x++) {
449 ch = peek_block(&diff_head, x);
450 p1->mask[y1] = BLOCK_ADD >> 8;
452 if (y1 == BLOCK_SIZE) {
453 TAILQ_INSERT_TAIL(&diff_a_head, p1, entry);
457 if ((ch & 0xFF) == '\n')
462 if (ch == peek_block(&diff_head, x + 1) &&
463 ch == peek_block(&diff_head, x + 2) &&
464 ' ' == (peek_block(&diff_head, x + 3) & 0xFF))
468 for (x++; x != size; x++) {
469 ch = peek_block(&diff_head, x);
471 if (y2 == BLOCK_SIZE) {
472 TAILQ_INSERT_TAIL(&diff_b_head, p2, entry);
476 if ((ch & 0xFF) == '\n')
483 for (x++; x != size; x++) {
484 ch = peek_block(&diff_head, x);
486 if (y1 == BLOCK_SIZE) {
487 TAILQ_INSERT_TAIL(&diff_a_head, p1, entry);
492 if (y2 == BLOCK_SIZE) {
493 TAILQ_INSERT_TAIL(&diff_b_head, p2, entry);
497 if ((ch & 0xFF) == '\n')
502 for (x += 3; x != size; x++) {
503 ch = peek_block(&diff_head, x);
504 chn = peek_block(&diff_head, x + 1);
505 if ((ch & 0xFF) == '.') {
506 /* only accept .c and .h files */
507 do_parse = ((chn & 0xFF) == 'c' || (chn & 0xFF) == 'h');
509 if ((ch & 0xFF) == '\n')
515 /* skip till end of line */
516 for (; x < size; x++) {
517 ch = peek_block(&diff_head, x);
518 if ((ch & 0xFF) == '\n') {
526 TAILQ_INSERT_TAIL(&diff_a_head, p1, entry);
527 TAILQ_INSERT_TAIL(&diff_b_head, p2, entry);
529 /* first pass - verify input */
530 size = size_block(&diff_a_head);
531 for (x = 0; x != size; x++) {
532 ch = peek_block(&diff_a_head, x) & 0xFF;
533 if (!(ch & 0x80) && ch != '\t' && ch != '\r' && ch != '\n' &&
534 ch != ' ' && !isprint(ch))
535 errx(EX_SOFTWARE, "Non printable characters are not allowed: '%c'", ch);
536 else if (ch & 0x80) {
537 set_block(&diff_a_head, x, ch | BLOCK_MASK);
541 /* second pass - identify all comments */
542 for (x = 0; x < size; x++) {
543 ch = peek_block(&diff_a_head, x);
544 chn = peek_block(&diff_a_head, x + 1);
545 if ((ch & 0xFF) == '/' && (chn & 0xFF) == '/') {
546 set_block(&diff_a_head, x, ch | BLOCK_MASK);
547 set_block(&diff_a_head, x + 1, chn | BLOCK_MASK);
548 for (x += 2; x < size; x++) {
549 ch = peek_block(&diff_a_head, x);
550 if ((ch & 0xFF) == '\n')
552 set_block(&diff_a_head, x, ch | BLOCK_MASK);
554 } else if ((ch & 0xFF) == '/' && (chn & 0xFF) == '*') {
555 set_block(&diff_a_head, x, ch | BLOCK_MASK);
556 set_block(&diff_a_head, x + 1, chn | BLOCK_MASK);
557 for (x += 2; x < size; x++) {
558 ch = peek_block(&diff_a_head, x);
559 chn = peek_block(&diff_a_head, x + 1);
560 if ((ch & 0xFF) == '*' && (chn & 0xFF) == '/') {
561 set_block(&diff_a_head, x, ch | BLOCK_MASK);
562 set_block(&diff_a_head, x + 1, chn | BLOCK_MASK);
566 set_block(&diff_a_head, x, ch | BLOCK_MASK);
571 /* third pass - identify preprocessor tokens and strings */
572 for (x = 0; x < size; x++) {
573 ch = peek_block(&diff_a_head, x);
576 if (inside_string == 0 && (ch & 0xFF) == '#') {
577 int skip_newline = 0;
579 set_block(&diff_a_head, x, ch | BLOCK_MASK);
580 for (x++; x < size; x++) {
581 ch = peek_block(&diff_a_head, x);
582 if ((ch & 0xFF) == '\n') {
589 if ((ch & 0xFF) == '\\')
591 set_block(&diff_a_head, x, ch | BLOCK_MASK);
594 if ((ch & 0xFF) == '"' || (ch & 0xFF) == '\'') {
595 if (inside_string == 0) {
596 inside_string = (ch & 0xFF);
598 if (escape_char == 0 && inside_string == (ch & 0xFF))
602 set_block(&diff_a_head, x, ch | BLOCK_MASK);
603 } else if (inside_string != 0) {
604 if ((ch & 0xFF) == '\\')
605 escape_char = !escape_char;
608 set_block(&diff_a_head, x, ch | BLOCK_MASK);
612 /* fourth pass - identify function blocks */
613 if (opt_verbose > 0) {
614 chn = peek_block(&diff_a_head, x);
615 printf("L%02d%c|", recurse,
616 (chn & BLOCK_ADD) ? '+' : ' ');
618 for (x = 0; x < size; x++) {
619 ch = peek_block(&diff_a_head, x);
620 if (opt_verbose > 0) {
621 printf("%c", ch & 0xFF);
622 if ((ch & 0xFF) == '\n') {
623 chn = peek_block(&diff_a_head, x + 1);
624 printf("L%02d%c|", recurse,
625 (chn & BLOCK_ADD) ? '+' : ' ');
639 set_block(&diff_a_head, x, ch | BLOCK_MASK);
652 errx(EX_SOFTWARE, "Unbalanced parenthesis");
653 if (inside_string != 0)
654 errx(EX_SOFTWARE, "String without end");
656 /* fifth pass - on the same line statements */
657 for (x = 0; x < size; x++) {
658 ch = peek_block(&diff_a_head, x);
665 set_block(&diff_a_head, x, ch | BLOCK_MASK);
670 /* sixth pass - output relevant blocks to indent */
671 for (y1 = x = 0; x < size; x++) {
672 ch = peek_block(&diff_a_head, x);
673 if (ch & BLOCK_ADD) {
674 TAILQ_INIT(&indent_in_head);
678 for (; y1 < size; y1++) {
679 ch = peek_block(&diff_a_head, y1);
680 if (y1 > x && !(ch & (BLOCK_MASK | BLOCK_ADD)))
682 p2->data[y2++] = ch & 0xFF;
683 if (y2 == BLOCK_SIZE) {
684 TAILQ_INSERT_TAIL(&indent_in_head, p2, entry);
689 if (p2->data[y2] != '\n')
690 p2->data[y2++] = '\n';
692 TAILQ_INSERT_TAIL(&indent_in_head, p2, entry);
694 cmd_block_process(&indent_in_head, &indent_out_head,
717 "-ta -st -bad -bap -nbbb -nbc -br -nbs "
718 "-c41 -cd41 -cdb -ce -ci4 -cli0 -d0 -di8 -ndj -ei -nfc1 "
719 "-nfcb -i8 -ip8 -l79 -lc77 -ldi0 -nlp -npcs -psl -sc "
723 "-e 's/_HEAD [(]/_HEAD(/g' "
724 "-e 's/_ENTRY [(]/_ENTRY(/g' "
725 "-e 's/\t__aligned/ __aligned/g' "
726 "-e 's/\t__packed/ __packed/g' "
727 "-e 's/\t__unused/ __unused/g' "
728 "-e 's/\t__used/ __used/g' "
729 "-e 's/^#define /#define\t/g'");
731 if (opt_diff_tool != NULL) {
732 if (diff_tool(&indent_in_head, &indent_out_head))
735 if (diff_block(&indent_in_head, &indent_out_head))
738 free_block(&indent_in_head);
739 free_block(&indent_out_head);
741 } else if (!(ch & BLOCK_MASK)) {