summaryrefslogtreecommitdiff
path: root/clean_spaces/clean_spaces.c
blob: 9a148a42c68eaa235c264c5a2a89a3a7f0079807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h>
#include <errno.h>

#ifdef __APPLE__
#define MAP_POPULATE 0
#endif

struct item
{
    struct item* next;
    char* filename;
};

struct context
{
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    struct item* head;
    struct item* items_pool;
    int free_items;
    int done;
    int nb_workers;
    char* output;
    int allocated_output;
};


static char* filter[] =
{
    "c","cpp","cxx","h","hrc","hxx","idl","inl","java","map","pl","pm","sdi","sh","src","tab","xcu","xml"
};

static int _is_filter_candidat(char* extension)
{
    int first = 0;
    int last = sizeof(filter)/sizeof(char*);
    int next;
    int cmp;
    char* cursor;

    while(last > first)
    {
        next = (first + last) >> 1;
        cursor = filter[next];
        cmp = strcmp(cursor, extension);
        if(cmp > 0)
        {
            last = next;
        }
        else if(cmp < 0)
        {
            first = next + 1;
        }
        else
        {
            return 1;
        }
    }
    return 0;
}

static struct item* _wait_for_item(struct context* context)
{
struct item* item;

    pthread_mutex_lock(&context->mutex);
    while(context->head == NULL)
    {
        if(context->done)
        {
            break;
        }
        pthread_cond_wait(&context->cond, &(context->mutex));
    }
    item = context->head;
    if(item)
    {
        context->head = item->next;
        if(item->next)
        {
            pthread_cond_broadcast(&context->cond);
        }
    }
    pthread_mutex_unlock(&context->mutex);
    return item;
}

int _post_item(struct context* context, struct item* item)
{
    pthread_mutex_lock(&context->mutex);
    item->next = context->head;
    context->head = item;
    if(!item->next)
    {
        pthread_cond_signal(&context->cond);
    }
    pthread_mutex_unlock(&context->mutex);
}

static int _do_one_file(char* filename, char** output, int* allocated_output)
{
int fd;
int col;
int rewrite;
char* input;
char* end;
char* cursor_in;
char* after_last_non_space;
char* cursor_out = NULL;
off_t size = 0;
struct stat s;

//  fprintf(stderr,"process %s\n", filename);
    /* look for the extension, ignore pure dot filename */
    cursor_in = filename + strlen(filename);
    while(cursor_in > filename && *cursor_in != '.')
    {
        cursor_in -= 1;
    }
    if(cursor_in == filename)
    {
        return 0;
    }
    /* check that the extention is candidat for filtering */
    if(!_is_filter_candidat(cursor_in + 1))
    {
        return 0;
    }
    if(stat(filename, &s))
    {
        fprintf(stderr, "*** Error on stat for %s\n", filename);
        return 0;
    }
    /* we filter only non-zero sized regular file */
    if(S_ISREG(s.st_mode))
    {
        size = s.st_size;
    }
    if(!size)
    {
        return 0;
    }
    fd = open(filename, O_RDONLY);
    if(fd != -1)
    {
        input = mmap( NULL, size, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0);
        if(input != MAP_FAILED)
        {
            cursor_in = input;
            end = input;
            end += size;
            after_last_non_space = cursor_in;
            col = 0;
            rewrite = 0;
            /* first find the first occurence if any of things needing a rewrite */
            while(cursor_in < end)
            {
                /* short-cut the most common case */
                if(*cursor_in > 32)
                {
                    cursor_in += 1;
                    col += 1;
                    after_last_non_space = cursor_in;
                }
                else if(*cursor_in == '\n')
                {
                    if(cursor_in != after_last_non_space)
                    {
                        rewrite = 1;
                        break;
                    }
                    else
                    {
                        cursor_in += 1;
                        after_last_non_space = cursor_in;
                        col = 0;
                    }
                }
                else if(*cursor_in == ' ')
                {
                    cursor_in += 1;
                    col += 1;
                }
                else if(*cursor_in == '\t')
                {
                    rewrite = 1;
                    break;
                }
                else
                {
                    cursor_in += 1;
                    col += 1;
                    after_last_non_space = cursor_in;
                }
            }
            close(fd);
            if(rewrite)
            {
                /* since we need a rewrite, we need to copy the beginning of the file
                 * al the way to the first anomaly and fix the current anomally */
                /* in theory teh file could be all tabs... so the output could grow 4 times */
                if((4 * size) >= *allocated_output)
                {
                    int new_size = (((size+1) * 4) + 32768) & ~32767; /* round up to the next 32K */

                    *output = realloc(*output, new_size);
//                  fprintf(stderr, "realloc from %d to %d\n", allocated_output, new_size);
                    *allocated_output = new_size;
                }
                if(*output)
                {
                    int pre_len = 0;

                    cursor_out = *output;

                    if(*cursor_in == '\t')
                    {
                        pre_len = (int)(cursor_in - input);
                        if(pre_len > 1)
                        {
                            memcpy(*output, input, pre_len - 1);
                            cursor_out += (pre_len - 1);
                        }
                        /* from now on after_last_non_space point into the output buffer *
                         * technicaly it always have, but up to now the output buffer was
                         * also the input buffer */
                        pre_len = (int)(after_last_non_space - input);
                        after_last_non_space = *output;
                        after_last_non_space += pre_len;

                        /* expend the tab to the correct number of spaces */
                        pre_len = (~col & 3);
                        switch( (unsigned char)pre_len)
                        {
                        case 3:
                            *cursor_out++ = ' ';
                        case 2:
                            *cursor_out++ = ' ';
                        case 1:
                            *cursor_out++ = ' ';
                        default:
                            *cursor_out++ = ' ';
                        }
                        col += pre_len + 1;
                        cursor_in += 1;
                    }
                    else if(*cursor_in == '\n')
                    {
                        pre_len = (int)(after_last_non_space - input);
                        if(pre_len > 0)
                        {
                            memcpy(*output, input, pre_len);
                            cursor_out += (pre_len);
                        }
                        *cursor_out++ = '\n';
                        cursor_in += 1;
                        after_last_non_space = cursor_out;
                        col = 0;
                    }
                    else
                    {
                        /* that should not happen */
                        abort();
                    }
                    /* clean-up the rest of the file as-needed*/
                    while(cursor_in < end)
                    {
                        /* short-cut the most common case */
                        if(*cursor_in > 32)
                        {
                            *cursor_out++ = *cursor_in++;
                            col += 1;
                            after_last_non_space = cursor_out;
                        }
                        else if(*cursor_in == '\n')
                        {
                            if(cursor_out != after_last_non_space)
                            {
                                *after_last_non_space++ = *cursor_in++;
                                cursor_out = after_last_non_space;
                            }
                            else
                            {
                                *cursor_out++ = *cursor_in++;
                                after_last_non_space = cursor_out;
                            }
                            col = 0;
                        }
                        else if(*cursor_in == ' ')
                        {
                            *cursor_out++ = *cursor_in++;
                            col += 1;
                        }
                        else if(*cursor_in == '\t')
                        {
                            pre_len = (~col & 3);
                            /* we cast to unsigned char to help to compiler optimize the case jump table */
                            switch( (unsigned char)pre_len)
                            {
                            case 3:
                                *cursor_out++ = ' ';
                            case 2:
                                *cursor_out++ = ' ';
                            case 1:
                                *cursor_out++ = ' ';
                            default:
                                *cursor_out++ = ' ';
                            }
                            col += pre_len + 1;
                            cursor_in += 1;
                        }
                        else
                        {
                            *cursor_out++ = *cursor_in++;
                            col += 1;
                            after_last_non_space = cursor_out;
                        }
                    }
                    if(after_last_non_space != cursor_out)
                    {
                        /* we have space on the last line without \n at the end */
                        *after_last_non_space++ = '\n';
                        cursor_out = after_last_non_space;
                    }
                    fd = open(filename, O_WRONLY | O_TRUNC);
                    if(fd != -1)
                    {
                        if(cursor_out == *output)
                        {
                            /* empty_file */
                        }
                        else
                        {
                            ssize_t written;

                            written = write(fd, *output, (size_t)(cursor_out - *output));
                            if(written != (ssize_t)(cursor_out - *output))
                            {
                                fprintf(stderr, "*** Error writing %s\n", filename);
                            }
                        }
                        close(fd);
                    }
                    else
                    {
                        fprintf(stderr, "*** Error re-opening %s for write\n", filename);
                    }
                }
                else
                {
                    abort();
                }
            }
            munmap(input, size);
        }
        else
        {
            close(fd);
        }
    }
    else
    {
        fprintf(stderr, "*** Error on open for %s\n", filename);
    }
    return 0;
}

static void* _worker_proc(struct context* context)
{
int rc = 0;
char* output = NULL;
int allocated_output = 1024*1024;
struct item* item;

    output = malloc(allocated_output);
    while((item = _wait_for_item(context)) != NULL)
    {
        _do_one_file(item->filename, &output, &allocated_output);
    }
    return NULL;
}

static struct item* _get_item(struct context* context)
{
struct item* item;

    if(context->free_items <= 0)
    {
        /* yes this leak... we we don't care. it is not worth the effort
         * to synchornize stuff to know when to recycle an item
         * i.e when it is safe to free a items_pool block
         */
        context->items_pool = (struct item*)calloc(4096, sizeof(struct item));
        context->free_items = 4095;
    }
    item = &(context->items_pool[context->free_items]);
    context->free_items -= 1;
    return item;
}

static char* _consume_input(struct context* context, char* fn_cursor, char* fn_tail)
{
char* filename;
struct item* item;

    while(fn_cursor <= fn_tail)
    {
        filename = fn_cursor;
        while(*fn_cursor && *fn_cursor != '\n')
        {
            fn_cursor += 1;
        }
        if(*fn_cursor =='\n')
        {
            *fn_cursor = 0;
            fn_cursor += 1;
            if(context->nb_workers > 1)
            {
                item = _get_item(context);
                item->filename = filename;
//              fprintf(stderr, "post %s\n", filename);
                _post_item(context, item);
            }
            else
            {
                _do_one_file(filename, &context->output, &context->allocated_output);
            }
        }
        else
        {
            fn_cursor = filename;
            break;
        }
    }
    return fn_cursor;
}

static void _usage(void)
{
    puts("Usage: clean_spaces [-p <nb_of_worker_thread>\n");
    puts("stdin contain the list of file to process (one file per line)\n");
}

int main(int argc, char** argv)
{
int rc = 0;
int i;
struct context context;
pthread_t* workers_tid;
char filename[2048];
pthread_mutexattr_t mutex_attribute;
pthread_condattr_t cond_attribute;
char* fn_buffer;
/* Note:FN_BUFFER_SIZE has been sized to fit the largest output expected
 * from git ls-files by a margin factor > 4 so we don't care for
 * case where stdin is biger than that. actually we fail with rc=1 if that
 * happen
 */
#define FN_BUFFER_SIZE (2*1024*1024)
char* fn_cursor;
char* fn_head;
char* fn_tail;
int fn_used = 0;
int fn_read = 0;
char* output = NULL;

    memset(&context, 0, sizeof(struct context));
    context.nb_workers = sysconf(_SC_NPROCESSORS_ONLN);
    if(context.nb_workers < 1)
    {
        context.nb_workers = 1;
    }

    for( i = 1; !rc && i < argc; i++)
    {
        if(!strcmp(argv[i], "-h"))
        {
            _usage();
            return 0;
        }
        else if(!strcmp(argv[i], "-p"))
        {
            i += 1;
            if( i < argc)
            {
                context.nb_workers = atoi(argv[i]);
                if(context.nb_workers < 0 || context.nb_workers > 512)
                {
                    _usage();
                    return 8;
                }
            }
            else
            {
                _usage();
                return 8;
            }
        }
        else
        {
            _usage();
            return 8;
        }
    }

    if(context.nb_workers > 1)
    {
        workers_tid = calloc(context.nb_workers, sizeof(pthread_t));

        pthread_mutexattr_init(&mutex_attribute);
        pthread_condattr_init(&cond_attribute);
        pthread_mutex_init(&context.mutex, &mutex_attribute);
        pthread_cond_init(&context.cond, &cond_attribute);

        for(i = 0; i < context.nb_workers; i++)
        {
            pthread_create(&(workers_tid[i]), NULL, (void* (*)(void*))_worker_proc, &context);
        }
    }
    else
    {
        context.allocated_output = 1024*1024;
        context.output = malloc(context.allocated_output);
    }

    fn_buffer = malloc(FN_BUFFER_SIZE);
    fn_tail = fn_cursor = fn_buffer;

    for(;;)
    {
        fn_read = read(STDIN_FILENO, fn_buffer + fn_used, FN_BUFFER_SIZE - fn_used);
        if(fn_read > 0)
        {
            fn_used += fn_read;
            fn_tail += fn_read;
            *fn_tail = 0;
            fn_cursor = _consume_input(&context, fn_cursor, fn_tail);
            if(fn_used == FN_BUFFER_SIZE)
            {
                rc = 1;
                break;
            }
        }
        else
        {
            if(fn_read == 0)
            {
                break;
            }
            else
            {
                if(errno != EINTR)
                {
                    rc = -1;
                    break;
                }
            }
        }
    }
    if(context.nb_workers > 1)
    {
        context.done = 1;
        pthread_cond_broadcast(&context.cond);
        for( i = 0; i < context.nb_workers; i++)
        {
            pthread_join(workers_tid[i], NULL);
        }
    }
    return rc;
}