forked from korsakov/ohcount
-
Notifications
You must be signed in to change notification settings - Fork 74
/
detector.c
781 lines (722 loc) · 23.8 KB
/
detector.c
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
// detector.c written by Mitchell Foral. mitchell<att>caladbolg.net.
// See COPYING for license information.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "detector.h"
#include "languages.h"
#include "log.h"
#include "hash/cppheader_hash.h"
#include "hash/disambiguatefunc_hash.h"
#include "hash/extension_hash.h"
#include "hash/filename_hash.h"
#define ISBINARY(x) (x[0] == '\1')
#define ISAMBIGUOUS(x) (x[0] == '\2')
#define DISAMBIGUATEWHAT(x) &x[1]
#ifdef _WIN32
# include <fcntl.h>
# define mkstemp(p) _open(_mktemp(p), _O_CREAT | _O_SHORT_LIVED | _O_EXCL)
#endif
const char *ohcount_detect_language(SourceFile *sourcefile) {
const char *language = NULL;
char *p, *pe;
int length;
// Attempt to detect using Emacs mode line (/^-\*-\s*mode[\s:]*\w/i).
char line[81] = { '\0' }, buf[81];
p = ohcount_sourcefile_get_contents(sourcefile);
pe = p;
char *eof = p + ohcount_sourcefile_get_contents_size(sourcefile);
while (pe < eof) {
// Get the contents of the first line.
while (pe < eof && *pe != '\r' && *pe != '\n') pe++;
length = (pe - p <= sizeof(line)) ? pe - p : sizeof(line);
strncpy(line, p, length);
line[length] = '\0';
if (*line == '#' && *(line + 1) == '!') {
// First line was sh-bang; loop to get contents of second line.
while (*pe == '\r' || *pe == '\n') pe++;
p = pe;
} else break;
}
p = strstr(line, "-*-");
if (p) {
p += 3;
while (*p == ' ' || *p == '\t') p++;
// detect "mode" (any capitalization)
if (strncasecmp(p, "mode", 4) == 0) {
p += 4;
while (*p == ' ' || *p == '\t' || *p == ':') p++;
}
pe = p;
while (!isspace(*pe) && *pe != ';' && pe != strstr(pe, "-*-")) pe++;
length = (pe - p <= sizeof(buf)) ? pe - p : sizeof(buf);
strncpy(buf, p, length);
buf[length] = '\0';
// Special case for "c" or "C" emacs mode header: always means C, not C++
if (strcasecmp(buf, "c") == 0) {
return LANG_C;
}
// First try it with the language name.
struct LanguageMap *rl = ohcount_hash_language_from_name(buf, length);
if (rl) language = rl->name;
if(!language) {
// Then try it with the extension table.
struct ExtensionMap *re = ohcount_hash_language_from_ext(buf, length);
if (re) language = re->value;
}
if (!language) {
// Try the lower-case version of this modeline.
for (pe = buf; pe < buf+length; pe++) *pe = tolower(*pe);
// First try it with the language name.
rl = ohcount_hash_language_from_name(buf, length);
if (rl) language = rl->name;
}
if (!language) {
// Then try it with the extension table.
struct ExtensionMap *re = ohcount_hash_language_from_ext(buf, length);
if (re) language = re->value;
}
}
// Attempt to detect based on file extension.
if(!language) {
length = strlen(sourcefile->ext);
struct ExtensionMap *re = ohcount_hash_language_from_ext(sourcefile->ext,
length);
if (re) language = re->value;
if (!language) {
// Try the lower-case version of this extension.
char lowerext[length + 1];
strncpy(lowerext, sourcefile->ext, length);
lowerext[length] = '\0';
for (p = lowerext; p < lowerext + length; p++) *p = tolower(*p);
struct ExtensionMap *re = ohcount_hash_language_from_ext(lowerext, length);
if (re) language = re->value;
}
}
// Attempt to detect based on filename.
if(!language) {
length = strlen(sourcefile->filename);
struct FilenameMap *rf =
ohcount_hash_language_from_filename(sourcefile->filename, length);
if (rf) language = rf->value;
}
// Attempt to detect based on Unix 'file' command.
if(!language) {
int tmpfile = 0;
char *path = sourcefile->filepath;
if (sourcefile->diskpath)
path = sourcefile->diskpath;
if (access(path, F_OK) != 0) { // create temporary file
path = malloc(21);
strncpy(path, "/tmp/ohcount_XXXXXXX\0", 21);
int fd = mkstemp(path);
char *contents = ohcount_sourcefile_get_contents(sourcefile);
log_it("contents:");
log_it(contents);
length = contents ? strlen(contents) : 0;
if (write(fd, contents, length) != length) {
fprintf(stderr, "src/detector.c: Could not write temporary file %s.\n", path);
exit(1);
}
close(fd);
tmpfile = 1;
}
char command[strlen(path) + 11];
sprintf(command, "file -b '%s'", path);
FILE *f = popen(command, "r");
if (f) {
if (fgets(line, sizeof(line), f) == NULL) {
fprintf(stderr, "src/detector.c: fgets() failed\n");
exit(1);
}
char *eol = line + strlen(line);
for (p = line; p < eol; p++) *p = tolower(*p);
p = strstr(line, "script text");
if (p && p == line) { // /^script text(?: executable)? for \w/
p = strstr(line, "for ");
if (p) {
p += 4;
pe = p;
while (isalnum(*pe)) pe++;
length = pe - p;
strncpy(buf, p, length);
buf[length] = '\0';
struct LanguageMap *rl = ohcount_hash_language_from_name(buf, length);
if (rl) language = rl->name;
}
} else if (p) { // /(\w+)(?: -\w+)* script text/
do {
p--;
pe = p;
while (*p == ' ') p--;
while (p != line && isalnum(*(p - 1))) p--;
if (p != line && *(p - 1) == '-') p--;
} while (*p == '-'); // Skip over any switches.
length = pe - p;
strncpy(buf, p, length);
buf[length] = '\0';
struct LanguageMap *rl = ohcount_hash_language_from_name(buf, length);
if (rl) language = rl->name;
} else if (strstr(line, "xml")) language = LANG_XML;
pclose(f);
if (tmpfile) {
remove(path);
free(path);
}
}
}
if (language) {
if (ISAMBIGUOUS(language)) {
// Call the appropriate function for disambiguation.
length = strlen(DISAMBIGUATEWHAT(language));
struct DisambiguateFuncsMap *rd =
ohcount_hash_disambiguate_func_from_id(DISAMBIGUATEWHAT(language),
length);
if (rd) language = rd->value(sourcefile);
} else language = ISBINARY(language) ? NULL : language;
}
return language;
}
const char *disambiguate_aspx(SourceFile *sourcefile) {
char *p = ohcount_sourcefile_get_contents(sourcefile);
char *eof = p + ohcount_sourcefile_get_contents_size(sourcefile);
for (; p < eof; p++) {
// /<%@\s*Page[^>]+Language="VB"[^>]+%>/
p = strstr(p, "<%@");
if (!p)
break;
char *pe = strstr(p, "%>");
if (p && pe) {
p += 3;
const int length = pe - p;
char buf[length];
strncpy(buf, p, length);
buf[length] = '\0';
char *eol = buf + strlen(buf);
for (p = buf; p < eol; p++) *p = tolower(*p);
p = buf;
while (*p == ' ' || *p == '\t') p++;
if (strncmp(p, "page", 4) == 0) {
p += 4;
if (strstr(p, "language=\"vb\""))
return LANG_VB_ASPX;
}
}
}
return LANG_CS_ASPX;
}
const char *disambiguate_b(SourceFile *sourcefile) {
char *p = ohcount_sourcefile_get_contents(sourcefile);
char *eof = p + ohcount_sourcefile_get_contents_size(sourcefile);
while (p < eof) {
// /(implement[ \t])|(include[ \t]+"[^"]*";)|
// ((return|break|continue).*;|(pick|case).*\{)/
if (strncmp(p, "implement", 9) == 0 &&
(*(p + 9) == ' ' || *(p + 9) == '\t'))
return LANG_LIMBO;
else if (strncmp(p, "include", 7) == 0 &&
(*(p + 7) == ' ' || *(p + 7) == '\t')) {
p += 7;
while (*p == ' ' || *p == '\t') p++;
if (*p == '"') {
while (*p != '"' && p < eof) p++;
if (*p == '"' && *(p + 1) == ';')
return LANG_LIMBO;
}
} else if (strncmp(p, "return", 6) == 0 ||
strncmp(p, "break", 5) == 0 ||
strncmp(p, "continue", 8) == 0) {
if (strstr(p, ";"))
return LANG_LIMBO;
} else if (strncmp(p, "pick", 4) == 0 ||
strncmp(p, "case", 4) == 0) {
if (strstr(p, "{"))
return LANG_LIMBO;
}
p++;
}
return disambiguate_basic(sourcefile);
}
const char *disambiguate_basic(SourceFile *sourcefile) {
char *p, *pe;
int length;
// Attempt to detect based on file contents.
char line[81];
p = ohcount_sourcefile_get_contents(sourcefile);
pe = p;
char *eof = p + ohcount_sourcefile_get_contents_size(sourcefile);
while (pe < eof) {
// Get a line at a time.
while (pe < eof && *pe != '\r' && *pe != '\n') pe++;
length = (pe - p <= sizeof(line)) ? pe - p : sizeof(line);
strncpy(line, p, length);
line[length] = '\0';
char *line_end = pe;
p = line;
if (isdigit(*p)) {
// /^\d+\s+\w/
p++;
while (isdigit(*p)) p++;
if (*p == ' ' || *p == '\t') {
p++;
while (*p == ' ' || *p == '\t') p++;
if (isalnum(*p))
return LANG_CLASSIC_BASIC;
}
}
// Next line.
pe = line_end;
while (*pe == '\r' || *pe == '\n') pe++;
p = pe;
}
// Attempt to detect from associated VB files in file context.
char **filenames = sourcefile->filenames;
if (filenames) {
int i;
for (i = 0; filenames[i] != NULL; i++) {
pe = filenames[i] + strlen(filenames[i]);
p = pe;
while (p > filenames[i] && *(p - 1) != '.') p--;
length = pe - p;
if (length == 3 &&
(strncmp(p, "frm", length) == 0 ||
strncmp(p, "frx", length) == 0 ||
strncmp(p, "vba", length) == 0 ||
strncmp(p, "vbp", length) == 0 ||
strncmp(p, "vbs", length) == 0)) {
return LANG_VISUALBASIC;
}
}
}
return LANG_STRUCTURED_BASIC;
}
const char *disambiguate_cs(SourceFile *sourcefile) {
// Attempt to detect based on file contents.
char *contents = ohcount_sourcefile_get_contents(sourcefile);
if (contents && strstr(contents, "<?cs"))
return LANG_CLEARSILVER_TEMPLATE;
else
return LANG_CSHARP;
}
const char *disambiguate_fortran(SourceFile *sourcefile) {
char *p, *pe;
p = ohcount_sourcefile_get_contents(sourcefile);
char *eof = p + ohcount_sourcefile_get_contents_size(sourcefile);
while (p < eof) {
if (*p == ' ' && p + 5 < eof) {
int i;
for (i = 1; i <= 5; i++)
if (!isdigit(*(p + i)) && *(p + i) != ' ')
return LANG_FORTRANFIXED; // definately not f77
// Possibly fixed (doesn't match /^\s*\d+\s*$/).
pe = p;
while (*pe == ' ' || *pe == '\t') pe++;
if (pe - p <= 5) {
if (!isdigit(*pe))
return LANG_FORTRANFIXED;
while (isdigit(*pe)) pe++;
while (*pe == ' ' || *pe == '\t') pe++;
if (*pe != '\r' && *pe != '\n' && pe - p == 5)
return LANG_FORTRANFIXED;
}
}
while (*p != '\r' && *p != '\n' && *p != '&' && p < eof) p++;
if (*p == '&') {
p++;
// Look for free-form continuation.
while (*p == ' ' || *p == '\t') p++;
if (*p == '\r' || *p == '\n') {
pe = p;
while (*pe == '\r' || *pe == '\n' || *pe == ' ' || *pe == '\t') pe++;
if (*pe == '&')
return LANG_FORTRANFREE;
}
}
while (*p == '\r' || *p == '\n') p++;
}
return LANG_FORTRANFREE; // might as well be free-form
}
const char *disambiguate_h(SourceFile *sourcefile) {
char *p, *pe, *bof;
int length;
// If the directory contains a matching *.m file, likely Objective C.
length = strlen(sourcefile->filename);
if (strcmp(sourcefile->ext, "h") == 0) {
char path[length];
strncpy(path, sourcefile->filename, length);
path[length] = '\0';
*(path + length - 1) = 'm';
char **filenames = sourcefile->filenames;
if (filenames) {
int i;
for (i = 0; filenames[i] != NULL; i++)
if (strcmp(path, filenames[i]) == 0)
return LANG_OBJECTIVE_C;
}
}
// Attempt to detect based on file contents.
char line[81], buf[81];
bof = ohcount_sourcefile_get_contents(sourcefile);
p = bof;
pe = p;
char *eof = p + ohcount_sourcefile_get_contents_size(sourcefile);
while (pe < eof) {
// Get a line at a time.
while (pe < eof && *pe != '\r' && *pe != '\n') pe++;
length = (pe - p <= sizeof(line)) ? pe - p : sizeof(line);
strncpy(line, p, length);
line[length] = '\0';
char *eol = line + strlen(line);
char *line_end = pe;
// Look for C++ headers.
if (*line == '#') {
p = line + 1;
while (*p == ' ' || *p == '\t') p++;
if (strncmp(p, "include", 7) == 0 &&
(*(p + 7) == ' ' || *(p + 7) == '\t')) {
// /^#\s*include\s+[<"][^>"]+[>"]/
p += 8;
while (*p == ' ' || *p == '\t') p++;
if (*p == '<' || *p == '"') {
// Is the header file a C++ header file?
p++;
pe = p;
while (pe < eol && *pe != '>' && *pe != '"') pe++;
length = pe - p;
strncpy(buf, p, length);
buf[length] = '\0';
if (ohcount_hash_is_cppheader(buf, length))
return LANG_CPP;
// Is the extension for the header file a C++ file?
p = pe;
while (p > line && *(p - 1) != '.') p--;
length = pe - p;
strncpy(buf, p, length);
buf[length] = '\0';
struct ExtensionMap *re = ohcount_hash_language_from_ext(buf, length);
if (re && strcmp(re->value, LANG_CPP) == 0)
return LANG_CPP;
}
}
}
// Look for C++ keywords.
p = line;
while (p < eol) {
if (islower(*p) && p != bof && !isalnum(*(p - 1)) && *(p - 1) != '_') {
pe = p;
while (islower(*pe)) pe++;
if (!isalnum(*pe) && *pe != '_') {
length = pe - p;
strncpy(buf, p, length);
buf[length] = '\0';
if (strcmp(buf, "class") == 0 ||
strcmp(buf, "namespace") == 0 ||
strcmp(buf, "template") == 0 ||
strcmp(buf, "typename") == 0)
return LANG_CPP;
}
p = pe + 1;
} else p++;
}
// Next line.
pe = line_end;
while (*pe == '\r' || *pe == '\n') pe++;
p = pe;
}
// Nothing to suggest C++.
return LANG_C;
}
const char *disambiguate_in(SourceFile *sourcefile) {
char *p, *pe;
int length;
const char *language = NULL;
p = sourcefile->filepath;
pe = p + strlen(p) - 3;
if (strstr(p, ".") <= pe) {
// Only if the filename has an extension prior to the .in
length = pe - p;
char buf[length];
strncpy(buf, p, length);
buf[length] = '\0';
SourceFile *undecorated = ohcount_sourcefile_new(buf);
p = ohcount_sourcefile_get_contents(sourcefile);
if (!p) {
return NULL;
}
// The filepath without the '.in' extension does not exist on disk. The
// sourcefile->diskpath field must be set incase the detector needs to run
// 'file -b' on the file.
ohcount_sourcefile_set_diskpath(undecorated, sourcefile->filepath);
ohcount_sourcefile_set_contents(undecorated, p);
undecorated->filenames = sourcefile->filenames;
language = ohcount_sourcefile_get_language(undecorated);
ohcount_sourcefile_free(undecorated);
}
return language;
}
const char *disambiguate_inc(SourceFile *sourcefile) {
char *p = ohcount_sourcefile_get_contents(sourcefile);
if (p) {
char *eof = p + strlen(p);
while (p < eof) {
if (*p == '\0')
return BINARY;
else if (*p == '?' && strncmp(p + 1, "php", 3) == 0)
return LANG_PHP;
p++;
}
}
return NULL;
}
const char *disambiguate_m(SourceFile *sourcefile) {
char *p, *pe;
int length;
// Attempt to detect based on a weighted heuristic of file contents.
int matlab_score = 0;
int objective_c_score = 0;
int limbo_score = 0;
int octave_syntax_detected = 0;
int i, has_h_headers = 0, has_c_files = 0;
char **filenames = sourcefile->filenames;
if (filenames) {
for (i = 0; filenames[i] != NULL; i++) {
p = filenames[i];
pe = p + strlen(p);
if (pe - p >= 4) {
if (*(pe - 4) == '.' && *(pe - 3) == 'c' &&
((*(pe - 2) == 'p' && *(pe - 1) == 'p') ||
(*(pe - 2) == '+' && *(pe - 1) == '+') ||
(*(pe - 2) == 'x' && *(pe - 1) == 'x'))) {
has_c_files = 1;
break; // short circuit
}
} else if (pe - p >= 3) {
if (*(pe - 3) == '.' && *(pe - 2) == 'c' && *(pe - 1) == 'c') {
has_c_files = 1;
break; // short circuit
}
} else if (pe - p >= 2) {
if (*(pe - 2) == '.') {
if (*(pe - 1) == 'h')
has_h_headers = 1;
else if (*(pe - 1) == 'c' || *(pe - 1) == 'C') {
has_c_files = 1;
break; // short circuit
}
}
}
}
}
if (has_h_headers && !has_c_files)
objective_c_score += 5;
char line[81], buf[81];
p = ohcount_sourcefile_get_contents(sourcefile);
pe = p;
char *eof = p + ohcount_sourcefile_get_contents_size(sourcefile);
while (pe < eof) {
// Get a line at a time.
while (pe < eof && *pe != '\r' && *pe != '\n') pe++;
length = (pe - p <= sizeof(line)) ? pe - p : sizeof(line);
strncpy(line, p, length);
line[length] = '\0';
char *eol = line + strlen(line);
char *line_end = pe;
// Look for tell-tale lines.
p = line;
while (*p == ' ' || *p == '\t') p++;
if (*p == '%') { // Matlab comment
matlab_score++;
} else if (*p == '#' && strncmp(p, "#import", 7) == 0) { // Objective C
objective_c_score++;
} else if (*p == '#') { // Limbo or Octave comment
while (*p == '#') p++;
if (*p == ' ' || *p == '\t') {
limbo_score++;
matlab_score++;
octave_syntax_detected = 1;
}
} else if (*p == '/' && *(p + 1) == '/' || *(p + 1) == '*') {
objective_c_score++; // Objective C comment
} else if (*p == '+' || *p == '-') { // Objective C method signature
objective_c_score++;
} else if (*p == '@' || *p == '#') { // Objective C method signature
if (strncmp(p, "@implementation", 15) == 0 ||
strncmp(p, "@interface", 10) == 0)
objective_c_score++;
} else if (strncmp(p, "function", 8) == 0) { // Matlab or Octave function
p += 8;
while (*p == ' ' || *p == '\t') p++;
if (*p == '(')
matlab_score++;
} else if (strncmp(p, "include", 7) == 0) { // Limbo include
// /^include[ \t]+"[^"]+\.m";/
p += 7;
if (*p == ' ' || *p == '\t') {
while (*p == ' ' || *p == '\t') p++;
if (*p == '"') {
while (*p != '"' && p < eol) p++;
if (*p == '"' && *(p - 2) == '.' && *(p - 1) == 'm')
limbo_score++;
}
}
}
// Look for Octave keywords.
p = line;
while (p < eol) {
if (islower(*p) && p != line && !isalnum(*(p - 1))) {
pe = p;
while (islower(*pe) || *pe == '_') pe++;
if (!isalnum(*pe)) {
length = pe - p;
strncpy(buf, p, length);
buf[length] = '\0';
if (strcmp(buf, "end_try_catch") == 0 ||
strcmp(buf, "end_unwind_protect") == 0 ||
strcmp(buf, "endfunction") == 0 ||
strcmp(buf, "endwhile") == 0)
octave_syntax_detected = 1;
}
p = pe + 1;
} else p++;
}
// Look for Limbo declarations
p = line;
while (p < eol) {
if (*p == ':' && (*(p + 1) == ' ' || *(p + 1) == '\t')) {
// /:[ \t]+(module|adt|fn ?\(|con[ \t])/
p += 2;
if (strncmp(p, "module", 6) == 0 && !isalnum(*(p + 6)) ||
strncmp(p, "adt", 3) == 0 && !isalnum(*(p + 3)) ||
strncmp(p, "fn", 2) == 0 &&
(*(p + 2) == ' ' && *(p + 3) == '(' || *(p + 2) == '(') ||
strncmp(p, "con", 3) == 0 &&
(*(p + 3) == ' ' || *(p + 3) == '\t'))
limbo_score++;
} else p++;
}
// Next line.
pe = line_end;
while (*pe == '\r' || *pe == '\n') pe++;
p = pe;
}
if (limbo_score > objective_c_score && limbo_score > matlab_score)
return LANG_LIMBO;
else if (objective_c_score > matlab_score)
return LANG_OBJECTIVE_C;
else
return octave_syntax_detected ? LANG_OCTAVE : LANG_MATLAB;
}
#include <pcre.h>
const char *disambiguate_pp(SourceFile *sourcefile) {
char *p = ohcount_sourcefile_get_contents(sourcefile);
char *eof = p + ohcount_sourcefile_get_contents_size(sourcefile);
/* prepare regular expressions */
pcre *re;
const char *error;
int erroffset;
re = pcre_compile("(define\\s+\\w+\\s*\\(|class \\s+\\w+\\s*{)", 0, &error, &erroffset, NULL);
for (; p < eof; p++) {
if (strncmp(p, "$include", 8) == 0 ||
strncmp(p, "$INCLUDE", 8) == 0 ||
strncmp(p, "end.", 4) == 0)
return LANG_PASCAL;
if (strncmp(p, "enable =>", 9) == 0 ||
strncmp(p, "ensure =>", 9) == 0 ||
strncmp(p, "content =>", 10) == 0 ||
strncmp(p, "source =>", 9) == 0 ||
strncmp(p, "include ", 8) == 0)
return LANG_PUPPET;
/* regexp for checking for define and class declarations */
int rc;
int ovector[30];
rc = pcre_exec(re, NULL, p, strnlen(p, 100), 0, 0, ovector, 30);
if(rc > 0) {
return LANG_PUPPET;
}
}
return LANG_PASCAL;
}
const char *disambiguate_pl(SourceFile *sourcefile) {
// Attempt to detect based on file contents.
char *contents = ohcount_sourcefile_get_contents(sourcefile);
if (contents && strstr(contents, "#!/usr/bin/perl"))
return LANG_PERL;
else if (contents && strstr(contents, "#!/usr/local/bin/perl"))
return LANG_PERL;
else if (contents && strstr(contents, ":-"))
return LANG_PROLOG;
else
return LANG_PERL;
}
#define QMAKE_SOURCES_SPACE "SOURCES +="
#define QMAKE_SOURCES "SOURCES+="
#define QMAKE_CONFIG_SPACE "CONFIG +="
#define QMAKE_CONFIG "CONFIG+="
const char *disambiguate_pro(SourceFile *sourcefile) {
char *p = ohcount_sourcefile_get_contents(sourcefile);
char *eof = p + ohcount_sourcefile_get_contents_size(sourcefile);
for (; p < eof; p++) {
if (strncmp(p, QMAKE_SOURCES_SPACE, strlen(QMAKE_SOURCES_SPACE)) == 0 ||
strncmp(p, QMAKE_SOURCES, strlen(QMAKE_SOURCES)) == 0 ||
strncmp(p, QMAKE_CONFIG_SPACE, strlen(QMAKE_CONFIG_SPACE)) == 0 ||
strncmp(p, QMAKE_CONFIG, strlen(QMAKE_CONFIG)) == 0)
return LANG_MAKE; // really QMAKE
}
return LANG_IDL_PVWAVE;
}
const char *disambiguate_st(SourceFile *sourcefile) {
char *p, *pe;
int length;
// Attempt to detect based on file contents.
int found_assignment = 0, found_block_start = 0, found_block_end = 0;
char line[81];
p = ohcount_sourcefile_get_contents(sourcefile);
pe = p;
char *eof = p + ohcount_sourcefile_get_contents_size(sourcefile);
while (pe < eof) {
// Get a line at a time.
while (pe < eof && *pe != '\r' && *pe != '\n') pe++;
length = (pe - p <= sizeof(line)) ? pe - p : sizeof(line);
strncpy(line, p, length);
line[length] = '\0';
char *eol = line + strlen(line);
char *line_end = pe;
for (p = line; p < eol; p++) {
if (*p == ':') {
p++;
while (p < eol && (*p == ' ' || *p == '\t')) p++;
if (*p == '=')
found_assignment = 1;
else if (*p == '[')
found_block_start = 1;
} else if (*p == ']' && *(p + 1) == '.') found_block_end = 1;
if (found_assignment && found_block_start && found_block_end)
return LANG_SMALLTALK;
}
// Next line.
pe = line_end;
while (*pe == '\r' || *pe == '\n') pe++;
p = pe;
}
return NULL;
}
int ohcount_is_binary_filename(const char *filename) {
char *p = (char *)filename + strlen(filename);
while (p > filename && *(p - 1) != '.') p--;
if (p > filename) {
struct ExtensionMap *re;
int length = strlen(p);
re = ohcount_hash_language_from_ext(p, length);
if (re) return ISBINARY(re->value);
// Try the lower-case version of this extension.
char lowerext[length];
strncpy(lowerext, p, length);
lowerext[length] = '\0';
for (p = lowerext; p < lowerext + length; p++) *p = tolower(*p);
re = ohcount_hash_language_from_ext(lowerext, length);
if (re) return ISBINARY(re->value);
}
return 0;
}