Initial work on automatic indent whitelisting

This commit is contained in:
pancake 2015-12-08 12:55:29 +01:00
parent cffee8748f
commit d24d069ade
6 changed files with 271 additions and 237 deletions

View File

@ -5,19 +5,18 @@
#define ut8 unsigned char
static ut8 twok_buf[4][4];
static int score =0;
static int moves =0;
#define INTERNAL static
static int score = 0;
static int moves = 0;
INTERNAL void twok_init() {
static void twok_init() {
int i, j;
score = 0;
for (i=0;i<4;i++)
for (j=0;j<4;j++)
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
twok_buf[i][j] = 0;
}
INTERNAL void twok_add() {
static void twok_add() {
int i, j;
while (true) {
i = r_num_rand (4);
@ -29,73 +28,77 @@ INTERNAL void twok_add() {
}
}
INTERNAL int twok_fin() {
int i,j;
for (i=0;i<4;i++)
for (j=0;j<4;j++)
static bool twok_fin() {
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (!twok_buf[i][j])
return 1;
for (i=0;i<4;i++)
for (j=0;j<3;j++)
if (twok_buf[i][j] == twok_buf[i][j+1])
return 1;
for (i=0;i<3;i++)
for (j=0;j<4;j++)
if (twok_buf[i][j] == twok_buf[i+1][j])
return 1;
return 0;
return true;
for (i = 0; i < 4; i++)
for (j = 0; j < 3; j++)
if (twok_buf[i][j] == twok_buf[i][j + 1])
return true;
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
if (twok_buf[i][j] == twok_buf[i + 1][j])
return true;
return false;
}
INTERNAL void twok_move(int u, int v) {
static void twok_move(int u, int v) {
int i, j, k;
int nKI, nKJ, nIK, nJK;
int moved = 0;
for(k = 0; k < 4; ++k) {
for(i = 0; i < 4; ++i) {
for(j = i + 1; j < 4 && !twok_buf[nKJ = u?k:v?j:3-j][nJK = !u?k:v?j:3-j]; ++j) ;
if(j == 4) continue;
nKI= u?k:v?i:3-i;
nIK= !u?k:v?i:3-i;
if(!twok_buf[nKI][nIK]){
for (k = 0; k < 4; ++k) {
for (i = 0; i < 4; ++i) {
for (j = i + 1; j < 4 && !twok_buf[nKJ = u? k: v? j: 3 - j][nJK = !u? k: v? j: 3 - j]; ++j)
;
if (j == 4) continue;
nKI = u? k: v? i: 3 - i;
nIK = !u? k: v? i: 3 - i;
if (!twok_buf[nKI][nIK]) {
twok_buf[nKI][nIK] = twok_buf[nKJ][nJK];
twok_buf[nKJ][nJK] = 0;
--i;
moved = 1;
}
else if(twok_buf[nKI][nIK] == twok_buf[nKJ][nJK]) {
} else if (twok_buf[nKI][nIK] == twok_buf[nKJ][nJK]) {
score += 1 << ++twok_buf[nKI][nIK];
twok_buf[nKJ][nJK] = 0;
moved = 1;
}
}
}
if(moved) {
if (moved) {
twok_add ();
moves++;
}
}
INTERNAL void twok_print() {
static void twok_print() {
char val0[32];
char val1[32];
char val2[32];
char val3[32];
int i;
#define VAL(x) if (twok_buf[i][x]){\
sprintf(val##x,"%4d",1<<twok_buf[i][x]); \
} else strcpy(val##x, " ");
#define VAL(x) \
if (twok_buf[i][x]) { \
sprintf (val##x, "%4d", 1 << twok_buf[i][x]); \
} else \
strcpy (val##x, " ");
printf (" +------+------+------+------+\n");
for (i = 0; i<4; i++) {
VAL(0); VAL(1);
VAL(2); VAL(3);
for (i = 0; i < 4; i++) {
VAL (0);
VAL (1);
VAL (2);
VAL (3);
printf (" | | | | |\n");
printf (" | %s | %s | %s | %s |\n",
val0,val1,val2,val3);
val0, val1, val2, val3);
printf (" | | | | |\n");
printf (" +------+------+------+------+\n");
}
printf ("Hexboard: 'hjkl' and 'q'uit\n");
for (i = 0; i<4; i++)
for (i = 0; i < 4; i++)
printf (" %02x %02x %02x %02x\n",
twok_buf[i][0], twok_buf[i][1],
twok_buf[i][2], twok_buf[i][3]);
@ -107,35 +110,35 @@ R_API void r_cons_2048() {
twok_init ();
twok_add ();
twok_add ();
while (twok_fin()) {
r_cons_clear00();
while (twok_fin ()) {
r_cons_clear00 ();
r_cons_printf ("[r2048] score: %d moves: %d\n",
score, moves);
score, moves);
r_cons_flush ();
twok_print();
twok_print ();
ch = r_cons_readchar ();
ch = r_cons_arrow_to_hjkl (ch);
switch(ch){
switch (ch) {
case 'h':
twok_move(1,1);
twok_move (1, 1);
break;
case 'j':
twok_move(0,0);
twok_move (0, 0);
break;
case 'k':
twok_move(0,1);
twok_move (0, 1);
break;
case 'l':
twok_move(1,0);
twok_move (1, 0);
break;
}
if (ch<1||ch =='q') break;
if (ch < 1 || ch == 'q') break;
}
r_cons_clear00();
r_cons_printf ("[r2048] score: %d\n", score );
r_cons_clear00 ();
r_cons_printf ("[r2048] score: %d\n", score);
r_cons_flush ();
twok_print();
r_cons_printf ("\n [r2048.score] %d\n", score );
twok_print ();
r_cons_printf ("\n [r2048.score] %d\n", score);
do {
ch = r_cons_any_key ("Press 'q' to quit.");
} while (ch != 'q' && ch >= 1);

View File

@ -2,52 +2,52 @@
#include <r_cons.h>
#define W(y) r_cons_canvas_write(c,y)
#define G(x,y) r_cons_canvas_gotoxy(c,x,y)
#define W(y) r_cons_canvas_write (c, y)
#define G(x, y) r_cons_canvas_gotoxy (c, x, y)
R_API void r_cons_canvas_free (RConsCanvas *c) {
R_API void r_cons_canvas_free(RConsCanvas *c) {
free (c->b);
free (c->attrs);
free (c);
}
R_API void r_cons_canvas_clear (RConsCanvas *c) {
R_API void r_cons_canvas_clear(RConsCanvas *c) {
int y;
if (c && c->b) {
memset (c->b, '\n', c->blen);
c->b[c->blen] = 0;
for (y = 0; y<c->h; y++)
c->b[ y * c->w ] = '\n';
if(c->attrs){
c->attrslen=0;
memset (c->attrs, 0, sizeof (*c->attrs)*c->blen);
for (y = 0; y < c->h; y++)
c->b[y * c->w] = '\n';
if (c->attrs) {
c->attrslen = 0;
memset (c->attrs, 0, sizeof (*c->attrs) * c->blen);
}
}
}
R_API RConsCanvas* r_cons_canvas_new (int w, int h) {
R_API RConsCanvas *r_cons_canvas_new(int w, int h) {
RConsCanvas *c;
if (w<1||h<1)
if (w < 1 || h < 1)
return NULL;
c = R_NEW0 (RConsCanvas);
if (!c) return NULL;
c->color = 0;
c->sx = 0;
c->sy = 0;
c->blen = (w+1)*h;
c->b = malloc (c->blen+1);
c->blen = (w + 1) * h;
c->b = malloc (c->blen + 1);
if (!c->b) {
free (c);
return NULL;
}
c->attrslen = 0;
c->attrs = calloc(sizeof(*c->attrs),c->blen+1);
c->attrs = calloc (sizeof (*c->attrs), c->blen + 1);
if (!c->attrs) {
free (c->b);
free (c);
return NULL;
}
c->attr=Color_RESET;
c->attr = Color_RESET;
c->w = w;
c->h = h;
c->x = c->y = 0;
@ -87,10 +87,10 @@ static int is_ansi_seq(const char *s) {
return s && *s == 0x1b && *(s + 1) == '[';
}
static int get_piece (const char *p, char *chr) {
static int get_piece(const char *p, char *chr) {
const char *q = p;
if (!p) return 0;
while (p && *p && *p != '\n' && !is_ansi_seq(p)) p++;
while (p && *p && *p != '\n' && !is_ansi_seq (p)) p++;
if (chr) *chr = *p;
return p - q;
}
@ -101,38 +101,38 @@ static char *prefixline(RConsCanvas *c, int *left) {
if (!c) return NULL;
if (strlen (c->b) < (c->y * c->w)) return NULL;
p = c->b + (c->y * c->w);
len = strlen(p)-1;
for (x = 0; (p[x] && x<c->x) && x < len; x++) {
len = strlen (p) - 1;
for (x = 0; (p[x] && x < c->x) && x < len; x++) {
if (p[x] == '\n')
p[x] = ' ';
}
if (left) *left = c->w - c->x;
return p+x;
return p + x;
}
static const char ** attr_at(RConsCanvas *c,int loc){
static const char **attr_at(RConsCanvas *c, int loc) {
int i, j, delta;
if (!c->color || c->attrslen==0)
if (!c->color || c->attrslen == 0)
return NULL;
j = c->attrslen / 2;
delta = c->attrslen / 2;
for (i=0; i<(c->attrslen); i++){
delta/=2;
if(delta == 0)
delta=1;
for (i = 0; i < (c->attrslen); i++) {
delta /= 2;
if (delta == 0)
delta = 1;
if (c->attrs[j].loc == loc)
return &c->attrs[j].a;
if(c->attrs[j].loc < loc) {
j+=delta;
if(j>=c->attrslen)
if (c->attrs[j].loc < loc) {
j += delta;
if (j >= c->attrslen)
break;
if(c->attrs[j].loc > loc && delta==1)
if (c->attrs[j].loc > loc && delta == 1)
break;
} else if(c->attrs[j].loc > loc) {
j-=delta;
if(j<=0)
} else if (c->attrs[j].loc > loc) {
j -= delta;
if (j <= 0)
break;
if(c->attrs[j].loc < loc && delta==1)
if (c->attrs[j].loc < loc && delta == 1)
break;
}
}
@ -140,22 +140,22 @@ static const char ** attr_at(RConsCanvas *c,int loc){
}
static void sort_attrs(RConsCanvas *c) {
int i,j;
int i, j;
RConsCanvasAttr value;
for (i = 1; i < c->attrslen; i++) {
value = c->attrs[i];
for (j = i-1; j>=0 && c->attrs[j].loc>value.loc; j--) {
c->attrs[j+1] = c->attrs[j];
for (j = i - 1; j >= 0 && c->attrs[j].loc > value.loc; j--) {
c->attrs[j + 1] = c->attrs[j];
}
c->attrs[j+1] = value;
c->attrs[j + 1] = value;
}
}
static void stamp_attr(RConsCanvas *c,int length){
static void stamp_attr(RConsCanvas *c, int length) {
int i;
const char ** s;
const char **s;
const int loc = c->x + (c->y * c->w);
s = attr_at(c, loc);
s = attr_at (c, loc);
if (s) {
//If theres already an attr there, just replace it.
@ -164,12 +164,12 @@ static void stamp_attr(RConsCanvas *c,int length){
c->attrs[c->attrslen].loc = loc;
c->attrs[c->attrslen].a = c->attr;
c->attrslen++;
sort_attrs(c);
sort_attrs (c);
}
for(i=0;i<length;i++){
s = attr_at(c,loc+i);
if(s)
for (i = 0; i < length; i++) {
s = attr_at (c, loc + i);
if (s)
*s = c->attr;
}
}
@ -179,7 +179,7 @@ static const char *set_attr(RConsCanvas *c, const char *s) {
const char *p = s;
char *color;
while (is_ansi_seq(p)) {
while (is_ansi_seq (p)) {
p += 2;
while (*p && *p != 'J' && *p != 'm' && *p != 'H') {
p++;
@ -188,7 +188,7 @@ static const char *set_attr(RConsCanvas *c, const char *s) {
}
if (p != s) {
color = r_str_ndup(s, p - s);
color = r_str_ndup (s, p - s);
c->attr = color;
}
return p;
@ -213,7 +213,7 @@ R_API void r_cons_canvas_write(RConsCanvas *c, const char *s) {
left = 0;
p = prefixline (c, &left);
slen = R_MIN (left, piece_len);
attr_len = slen <= 0 && s_part != s ? 1 : slen;
attr_len = slen <= 0 && s_part != s? 1: slen;
if (attr_len > 0) {
stamp_attr (c, attr_len);
}
@ -240,14 +240,14 @@ R_API void r_cons_canvas_write(RConsCanvas *c, const char *s) {
R_API char *r_cons_canvas_to_string(RConsCanvas *c) {
int x, y, olen = 0;
char *o;
const char* b;
const char**atr;
const char *b;
const char **atr;
int is_first = true;
if (!c) return NULL;
b = c->b;
o = calloc (sizeof(char),
(c->w * (c->h + 1)) * (CONS_MAX_ATTR_SZ));
o = calloc (sizeof (char),
(c->w * (c->h + 1)) * (CONS_MAX_ATTR_SZ));
if (!o) return NULL;
for (y = 0; y < c->h; y++) {
if (!is_first) {
@ -255,14 +255,14 @@ R_API char *r_cons_canvas_to_string(RConsCanvas *c) {
}
is_first = false;
for (x = 0; x<c->w; x++) {
for (x = 0; x < c->w; x++) {
const int p = x + (y * c->w);
atr = attr_at (c,p);
if(atr) {
atr = attr_at (c, p);
if (atr) {
strcat (o, *atr);
olen += strlen (*atr);
}
if (!b[p] || b[p]=='\n')
if (!b[p] || b[p] == '\n')
break;
o[olen++] = b[p];
}
@ -275,7 +275,7 @@ R_API void r_cons_canvas_print_region(RConsCanvas *c) {
char *o = r_cons_canvas_to_string (c);
if (o) {
char *p = r_str_trim_tail (o);
if(p) {
if (p) {
r_cons_strcat (p);
free (p);
} else {
@ -294,13 +294,13 @@ R_API void r_cons_canvas_print(RConsCanvas *c) {
R_API int r_cons_canvas_resize(RConsCanvas *c, int w, int h) {
void *newbuf = NULL;
const int blen = (w+1) * h;
const int blen = (w + 1) * h;
char *b = NULL;
if (!c || w < 0) return false;
b = realloc (c->b, blen+1);
b = realloc (c->b, blen + 1);
if (!b) return false;
c->b = b;
newbuf = realloc (c->attrs, sizeof (*c->attrs)*blen+1);
newbuf = realloc (c->attrs, sizeof (*c->attrs) * blen + 1);
if (!newbuf) {
free (c->b);
free (c->attrs);
@ -327,34 +327,34 @@ R_API void r_cons_canvas_box(RConsCanvas *c, int x, int y, int w, int h, const c
if (color) c->attr = color;
if (!c->color) c->attr = Color_RESET;
row = malloc (w+1);
row = malloc (w + 1);
if (!row)
return;
row[0] = roundcorners?'.':corner;
if (w>2)
memset (row+1, '-', w-2);
if (w>1)
row[w-1] = roundcorners?'.':corner;
row[0] = roundcorners? '.': corner;
if (w > 2)
memset (row + 1, '-', w - 2);
if (w > 1)
row[w - 1] = roundcorners? '.': corner;
row[w] = 0;
row_ptr = row;
x_mod = x;
if (x < -c->sx) {
x_mod = R_MIN(-c->sx, x_mod + w);
x_mod = R_MIN (-c->sx, x_mod + w);
row_ptr += x_mod - x;
}
if (G(x_mod, y)) {
W(row_ptr);
if (G (x_mod, y)) {
W (row_ptr);
}
if (G(x_mod, y+h-1)) {
row[0] = roundcorners?'\'':corner;
row[w-1] = roundcorners?'\'':corner;
W(row_ptr);
if (G (x_mod, y + h - 1)) {
row[0] = roundcorners? '\'': corner;
row[w - 1] = roundcorners? '\'': corner;
W (row_ptr);
}
for (i=1;i<h-1;i++) {
if (G(x, y+i)) W("|");
if (G(x+w-1, y+i)) W("|");
for (i = 1; i < h - 1; i++) {
if (G (x, y + i)) W ("|");
if (G (x + w - 1, y + i)) W ("|");
}
free (row);
if (color) c->attr = Color_RESET;
@ -366,19 +366,18 @@ R_API void r_cons_canvas_fill(RConsCanvas *c, int x, int y, int w, int h, char c
if (w < 0) return;
row = malloc (w+1);
row = malloc (w + 1);
memset (row, ch, w);
row[w] = 0;
for (i=0;i<h;i++) {
if (G(x, y+i))
W(row);
for (i = 0; i < h; i++) {
if (G (x, y + i))
W (row);
}
free (row);
}
R_API void r_cons_canvas_line (RConsCanvas *c, int x, int y, int x2, int y2,
RCanvasLineStyle *style) {
R_API void r_cons_canvas_line(RConsCanvas *c, int x, int y, int x2, int y2, RCanvasLineStyle *style) {
if (c->linemode) {
r_cons_canvas_line_square (c, x, y, x2, y2, style);
} else {

View File

@ -36,12 +36,12 @@ R_API char *r_cons_hud_string(const char *s) {
return NULL;
}
fl->free = free;
for (os=o, i=0; o[i]; i++) {
if (o[i]=='\n') {
for (os = o, i = 0; o[i]; i++) {
if (o[i] == '\n') {
o[i] = 0;
if (*os && *os != '#'){
if (*os && *os != '#') {
track = strdup (os);
if (!r_list_append (fl, track)){
if (!r_list_append (fl, track)) {
free (track);
break;
}
@ -55,19 +55,19 @@ R_API char *r_cons_hud_string(const char *s) {
return ret;
}
static char *strmatch (char *pos, char *buf) {
static char *strmatch(char *pos, char *buf) {
char *p, *os = buf;
for (p = buf; *p; p++) {
if (*p==' ') {
if (*p == ' ') {
*p = 0;
if (!r_str_casestr (pos, os)) {
//r_cons_printf ("FAIL ((%s), %s)\n", pos, os);
//r_cons_printf ("FAIL ((%s), %s)\n", pos, os);
*p = ' ';
return NULL;
}
//r_cons_printf ("CHK (%s)\n", os);
//r_cons_printf ("CHK (%s)\n", os);
*p = ' ';
os = p+1;
os = p + 1;
}
}
return (char *)r_str_casestr (pos, os);
@ -95,14 +95,14 @@ R_API char *r_cons_hud(RList *list, const char *prompt) {
if (!buf[0] || strmatch (pos, buf)) {
char *x = strchr (pos, '\t');
// remove \t.*
if (!choose || n>=choose) {
if (!choose || n >= choose) {
if (x) *x = 0;
p = strdup (pos);
for (j=0; p[j]; j++) {
for (j = 0; p[j]; j++) {
if (strchr (buf, p[j]))
p[j] = toupper ((unsigned char)p[j]);
}
r_cons_printf (" %c %s\n", first?'-':' ', p);
r_cons_printf (" %c %s\n", first? '-': ' ', p);
free (p);
if (x) *x = '\t';
if (first) match = pos;
@ -115,56 +115,55 @@ R_API char *r_cons_hud(RList *list, const char *prompt) {
ch = r_cons_readchar ();
nch = r_cons_arrow_to_hjkl (ch);
if (nch == 'j' && ch != 'j') {
if (choose+1 < n)
if (choose + 1 < n)
choose++;
} else if (nch == 'k' && ch != 'k') {
if (choose>=0)
if (choose >= 0)
choose--;
} else
switch (ch) {
case 9: // \t
if (choose+1 < n)
choose++;
else choose = 0;
break;
case 10: // \n
case 13: // \r
choose = 0;
// if (!*buf)
// return NULL;
if (n >= 1) {
//eprintf ("%s\n", buf);
//i = buf[0] = 0;
return strdup (match);
} // no match!
break;
case 23: // ^w
choose = 0;
i = buf[0] = 0;
break;
case 0x1b: // ESC
return NULL;
case 8: // bs
case 127: // bs
choose = 0;
if (i<1) return NULL;
buf[--i] = 0;
break;
default:
if (IS_PRINTABLE (ch)) {
if (i >= buf_size) {
break;
}
} else switch (ch) {
case 9: // \t
if (choose + 1 < n)
choose++;
else choose = 0;
break;
case 10: // \n
case 13: // \r
choose = 0;
if (i+1>=buf_size) {
// tOO MANy
break;
// if (!*buf)
// return NULL;
if (n >= 1) {
//eprintf ("%s\n", buf);
//i = buf[0] = 0;
return strdup (match);
} // no match!
break;
case 23: // ^w
choose = 0;
i = buf[0] = 0;
break;
case 0x1b: // ESC
return NULL;
case 8: // bs
case 127: // bs
choose = 0;
if (i < 1) return NULL;
buf[--i] = 0;
break;
default:
if (IS_PRINTABLE (ch)) {
if (i >= buf_size) {
break;
}
choose = 0;
if (i + 1 >= buf_size) {
// tOO MANy
break;
}
buf[i++] = ch;
buf[i] = 0;
}
buf[i++] = ch;
buf[i] = 0;
break;
}
break;
}
}
return NULL;
}
@ -172,12 +171,11 @@ R_API char *r_cons_hud(RList *list, const char *prompt) {
R_API char *r_cons_hud_path(const char *path, int dir) {
char *tmp = NULL, *ret = NULL;
RList *files;
if (path){
while (*path==' ')
if (path) {
while (*path == ' ')
path++;
tmp = (*path)? strdup(path): strdup ("./");
} else
tmp = strdup ("./");
tmp = (*path)? strdup (path): strdup ("./");
} else tmp = strdup ("./");
files = r_sys_dir (tmp);
if (files) {
@ -210,23 +208,23 @@ R_API char *r_cons_message(const char *msg) {
cols = r_cons_get_size (&rows);
r_cons_clear ();
r_cons_gotoxy ((cols-len)/2, rows/2); // XXX
r_cons_gotoxy ((cols - len) / 2, rows / 2); // XXX
/// TODO: add square, or talking clip here
r_cons_printf ("%s\n", msg);
r_cons_flush ();
r_cons_gotoxy (0, rows-2); // XXX
r_cons_gotoxy (0, rows - 2); // XXX
r_cons_any_key (NULL);
return NULL;
}
#ifdef MAIN
main() {
main () {
char *res;
RFList fl = r_flist_new (3);
r_flist_set (fl, 0, "foo is pure cow");
r_flist_set (fl, 1, "bla is kinda crazy");
r_flist_set (fl, 2, "funny to see you here");
r_cons_new ();
res = r_cons_hud (fl, NULL);
r_cons_clear ();

View File

@ -5,15 +5,11 @@
static RLine r_line_instance;
#define I r_line_instance
/* definitions to be removed */
int r_line_dietline_init();
void r_line_hist_free();
R_API RLine *r_line_singleton () {
R_API RLine *r_line_singleton() {
return &r_line_instance;
}
R_API RLine *r_line_new () {
R_API RLine *r_line_new() {
I.hist_up = NULL;
I.hist_down = NULL;
I.prompt = strdup ("> ");
@ -23,21 +19,21 @@ R_API RLine *r_line_new () {
return &I;
}
R_API void r_line_free () {
R_API void r_line_free() {
// XXX: prompt out of the heap?
free ((void*)I.prompt);
free ((void *)I.prompt);
I.prompt = NULL;
r_line_hist_free ();
}
// handle const or dynamic prompts?
R_API void r_line_set_prompt (const char *prompt) {
R_API void r_line_set_prompt(const char *prompt) {
free (I.prompt);
I.prompt = strdup (prompt);
}
// handle const or dynamic prompts?
R_API char *r_line_get_prompt () {
R_API char *r_line_get_prompt() {
return strdup (I.prompt);
}

9
sys/indent-whitelist.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
FILES="
libr/cons/hud.c
libr/cons/2048.c
libr/cons/line.c
libr/cons/canvas.c
"
sys/indent.sh -i ${FILES}
git commit sys/indent* ${FILES}

View File

@ -3,15 +3,16 @@
IFILE="$1"
if [ -z "${IFILE}" ]; then
echo "Usage: indent.sh [-i|-u] [file]"
echo " -i - indent in place (modify file"
echo " -u - indent in place (modify file"
echo "Usage: indent.sh [-i|-u] [file] [...]"
echo " -i indent in place (modify file)"
echo " -u unified diff of the file"
exit 1
fi
CWD="$PWD"
INPLACE=0
UNIFIED=0
ROOTDIR=/
if [ "${IFILE}" = "-i" ]; then
shift
@ -25,29 +26,57 @@ if [ "${IFILE}" = "-u" ]; then
IFILE="$1"
fi
indentFile() {
if [ ! -f "${IFILE}" ]; then
echo "Cannot find $IFILE"
return
fi
cp -f doc/clang-format ${CWD}/.clang-format
(
cd "$CWD"
clang-format "${IFILE}" > .tmp-format
# fix ternary conditional indent
cat .tmp-format | sed -e 's, \? ,? ,g' > .tmp-format2
cat .tmp-format2 | sed -e 's, : ,: ,g' > .tmp-format
mv .tmp-format .tmp-format2
# do not space before parenthesis on function signatures
awk '{if (/^static/ || /^R_API/) { gsub(/ \(/,"("); }; print;}' \
< .tmp-format2 > .tmp-format
# allow oneliner else statements
mv .tmp-format .tmp-format2
perl -ne 's/\telse\n[ \t]*/\telse /g;print' < .tmp-format2 | \
awk '{if (/\telse \t+/) {gsub(/\telse \t+/, "\telse ");} print;}' > .tmp-format
mv .tmp-format .tmp-format2
perl -ne 's/} else\n[ \t]*/} else /g;print' < .tmp-format2 | \
awk '{if (/} else \t+/) {gsub(/} else \t+/, "} else ");} print;}' > .tmp-format
# struct initializers with spaces wtf
mv .tmp-format .tmp-format2
awk '{gsub(/\{0\}/, "{ 0 }");print}' < .tmp-format2 > .tmp-format
if [ "$UNIFIED" = 1 ]; then
diff -ru "${IFILE}" .tmp-format
rm .tmp-format
elif [ "$INPLACE" = 1 ]; then
mv .tmp-format "${IFILE}"
else
cat .tmp-format
rm .tmp-format
fi
rm -f .tmp-format2
)
rm -f ${CWD}/.clang-format
}
while : ; do
[ "$PWD" = / ] && break
if [ -f doc/clang-format ]; then
cp -f doc/clang-format ${CWD}/.clang-format
(
cd "$CWD"
clang-format "${IFILE}" > .tmp-format
# fix ternary conditional indent
cat .tmp-format | sed -e 's, \? ,? ,g' > .tmp-format2
# do not space before parenthesis on function signatures
awk '{if (/^static/ || /^R_API/) { gsub(/ \(/,"("); }; print;}' \
< .tmp-format2 > .tmp-format
if [ "$UNIFIED" = 1 ]; then
diff -ru "${IFILE}" .tmp-format
rm .tmp-format
elif [ "$INPLACE" = 1 ]; then
mv .tmp-format $a
else
cat .tmp-format
rm .tmp-format
if [ -f doc/clang-format ]; then
ROOTDIR=$PWD
while : ; do
[ -z "${IFILE}" ] && break
indentFile
shift
IFILE="$1"
done
fi
)
rm -f ${CWD}/.clang-format
fi
cd ..
cd ..
done