Wed, 10 Jul 2013 17:57:03 +0200
implemented java highlighting
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
1 | /* |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
3 | * |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
4 | * Copyright 2013 Mike Becker. All rights reserved. |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
5 | * |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
8 | * |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
11 | * |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
15 | * |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
27 | * |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
28 | */ |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
29 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
30 | #include <stdio.h> |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
31 | #include <stdlib.h> |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
32 | #include <string.h> |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
33 | #include <fcntl.h> |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
34 | #include <unistd.h> |
4 | 35 | #include <ctype.h> |
36 | ||
37 | #define INPUTBUF_SIZE 2048 | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
38 | #define WORDBUF_SIZE 16 |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
39 | |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
40 | const char* ckeywords[] = { |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
41 | "auto", "break", "case", "char", "const", "continue", "default", "do", |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
42 | "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
43 | "long", "register", "return", "short", "signed", "sizeof", "static", |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
44 | "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
45 | "while", NULL |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
46 | }; |
4 | 47 | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
48 | const char* jkeywords[] = { |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
49 | "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
50 | "package", "synchronized", "boolean", "do", "if", "private", "this", |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
51 | "break", "double", "implements", "protected", "throw", "byte", "else", |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
52 | "import", "public", "throws", "case", "enum", "instanceof", "return", |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
53 | "transient", "catch", "extends", "int", "short", "try", "char", "final", |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
54 | "interface", "static", "void", "class", "finally", "long", "strictfp", |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
55 | "volatile", "const", "float", "native", "super", "while", NULL |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
56 | }; |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
57 | |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
58 | int isctype(char *word, size_t len) { |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
59 | return (word[len-2] == '_' && word[len-1] == 't'); |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
60 | } |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
61 | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
62 | int iscdirective(char *word) { |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
63 | return (word[0] == '#'); |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
64 | } |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
65 | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
66 | int isjtype(char *word, size_t len) { |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
67 | return isupper(word[0]); |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
68 | } |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
69 | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
70 | int isjdirective(char *word) { |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
71 | return word[0] == '@'; |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
72 | } |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
73 | |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
74 | typedef struct { |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
75 | const char** keywords; |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
76 | int(*istype)(char*,size_t); |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
77 | int(*isdirective)(char*); |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
78 | } highlighter_t; |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
79 | |
11 | 80 | typedef struct { |
81 | char* outfilename; | |
82 | char* infilename; | |
12 | 83 | int highlight; |
11 | 84 | } settings_t; |
4 | 85 | |
86 | typedef struct { | |
87 | size_t count; | |
88 | size_t capacity; | |
89 | size_t maxlinewidth; | |
90 | char** lines; | |
91 | } inputfile_t; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
92 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
93 | inputfile_t *inputfilebuffer(size_t capacity) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
94 | inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
95 | inputfile->lines = (char**) malloc(capacity * sizeof(char*)); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
96 | inputfile->capacity = capacity; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
97 | inputfile->count = 0; |
4 | 98 | inputfile->maxlinewidth = 0; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
99 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
100 | return inputfile; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
101 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
102 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
103 | void addline(inputfile_t *inputfile, char* line, size_t width) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
104 | char *l = (char*) malloc(width+1); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
105 | memcpy(l, line, width); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
106 | l[width] = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
107 | if (inputfile->count >= inputfile->capacity) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
108 | inputfile->capacity <<= 1; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
109 | inputfile->lines = realloc(inputfile->lines, inputfile->capacity); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
110 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
111 | inputfile->lines[inputfile->count] = l; |
4 | 112 | inputfile->maxlinewidth = |
113 | width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
114 | inputfile->count++; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
115 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
116 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
117 | void freeinputfilebuffer(inputfile_t *inputfile) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
118 | for (int i = 0 ; i < inputfile->count ; i++) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
119 | free(inputfile->lines[i]); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
120 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
121 | free(inputfile->lines); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
122 | free(inputfile); |
0 | 123 | } |
124 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
125 | inputfile_t *readinput(char *filename) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
126 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
127 | int fd = open(filename, O_RDONLY); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
128 | if (fd == -1) return NULL; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
129 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
130 | inputfile_t *inputfile = inputfilebuffer(512); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
131 | |
4 | 132 | char buf[INPUTBUF_SIZE]; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
133 | ssize_t r; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
134 | |
4 | 135 | size_t maxlinewidth = 256; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
136 | char *line = (char*) malloc(maxlinewidth); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
137 | size_t col = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
138 | |
4 | 139 | while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) { |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
140 | for (size_t i = 0 ; i < r ; i++) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
141 | if (col >= maxlinewidth-4) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
142 | maxlinewidth <<= 1; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
143 | line = realloc(line, maxlinewidth); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
144 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
145 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
146 | if (buf[i] == '\n') { |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
147 | line[col++] = '\n'; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
148 | line[col] = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
149 | addline(inputfile, line, col); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
150 | col = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
151 | } else { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
152 | line[col++] = buf[i]; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
153 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
154 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
155 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
156 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
157 | free(line); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
158 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
159 | close(fd); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
160 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
161 | return inputfile; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
162 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
163 | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
164 | size_t writeescapedchar(char *dest, size_t dp, char c) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
165 | if (c == '>') { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
166 | dest[dp++] = '&'; dest[dp++] = 'g'; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
167 | dest[dp++] = 't'; dest[dp++] = ';'; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
168 | } else if (c == '<') { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
169 | dest[dp++] = '&'; dest[dp++] = 'l'; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
170 | dest[dp++] = 't'; dest[dp++] = ';'; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
171 | } else { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
172 | dest[dp++] = c; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
173 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
174 | |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
175 | return dp; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
176 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
177 | |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
178 | int iskeyword(char *word, const char** keywords) { |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
179 | for (int i = 0 ; keywords[i] ; i++) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
180 | if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
181 | return 1; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
182 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
183 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
184 | return 0; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
185 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
186 | |
9
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
187 | int iscapsonly(char *word, size_t wp) { |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
188 | for (size_t i = 0 ; i < wp ; i++) { |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
189 | if (!isupper(word[i]) && word[i] != '_') { |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
190 | return 0; |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
191 | } |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
192 | } |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
193 | return 1; |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
194 | } |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
195 | |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
196 | void parseline(char *src, char *dest, highlighter_t *highlighter) { |
4 | 197 | size_t sp = 0, dp = 0; |
198 | /* indent */ | |
199 | while (isspace(src[sp])) { | |
200 | dest[dp++] = src[sp++]; | |
201 | } | |
10
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
202 | |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
203 | static char word[WORDBUF_SIZE]; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
204 | static char includefile[FILENAME_MAX]; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
205 | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
206 | memset(word, 0, WORDBUF_SIZE); |
10
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
207 | size_t wp = 0, ifp = 0; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
208 | int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; |
8 | 209 | static int iscommentml; |
7 | 210 | int isescaping = 0; |
8 | 211 | |
212 | if (iscommentml) { | |
213 | iscomment = 1; | |
214 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); | |
215 | dp += 29; | |
216 | } | |
9
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
217 | |
4 | 218 | for (char c = src[sp] ; c ; c=src[++sp]) { |
8 | 219 | /* comments */ |
220 | if (c == '/') { | |
221 | if (iscommentml && sp > 0 && src[sp-1] == '*') { | |
222 | iscomment = 0; | |
223 | iscommentml = 0; | |
224 | memcpy(&(dest[dp]), "/</span>", 8); | |
225 | dp += 8; | |
226 | continue; | |
227 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { | |
228 | iscomment = 1; | |
229 | iscommentml = (src[sp+1] == '*'); | |
230 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); | |
231 | dp += 29; | |
232 | } | |
233 | } | |
234 | ||
235 | if (iscomment) { | |
236 | if (c == '\n') { | |
7 | 237 | memcpy(&(dest[dp]), "</span>", 7); |
238 | dp += 7; | |
239 | } | |
8 | 240 | dp = writeescapedchar(dest, dp, c); |
10
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
241 | } else if (isinclude) { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
242 | if (c == '<') { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
243 | memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
244 | dp += 32; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
245 | dp = writeescapedchar(dest, dp, c); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
246 | } else if (c == '\"') { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
247 | if (parseinclude) { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
248 | dest[dp++] = '\"'; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
249 | dest[dp++] = '>'; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
250 | memcpy(&(dest[dp]), includefile, ifp); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
251 | dp += ifp; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
252 | |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
253 | dp = writeescapedchar(dest, dp, c); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
254 | memcpy(&(dest[dp]), "</a>", 4); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
255 | dp += 4; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
256 | parseinclude = 0; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
257 | } else { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
258 | memcpy(&(dest[dp]), "<a class=\"c2html-userinclude\" href=", 35); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
259 | dp += 35; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
260 | dp = writeescapedchar(dest, dp, c); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
261 | ifp = 0; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
262 | includefile[ifp++] = '\"'; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
263 | parseinclude = 1; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
264 | } |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
265 | } else if (c == '>') { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
266 | dp = writeescapedchar(dest, dp, c); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
267 | memcpy(&(dest[dp]), "</span>", 7); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
268 | dp += 7; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
269 | } else { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
270 | if (parseinclude) { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
271 | includefile[ifp++] = c; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
272 | } |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
273 | dp = writeescapedchar(dest, dp, c); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
274 | } |
7 | 275 | } else { |
8 | 276 | /* strings */ |
277 | if (!isescaping && (c == '\'' || c == '\"')) { | |
278 | isstring ^= 1; | |
279 | if (isstring) { | |
280 | memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); | |
281 | dp += 28; | |
7 | 282 | dp = writeescapedchar(dest, dp, c); |
283 | } else { | |
284 | dp = writeescapedchar(dest, dp, c); | |
8 | 285 | memcpy(&(dest[dp]), "</span>", 7); |
286 | dp += 7; | |
287 | } | |
288 | } else { | |
289 | if (isstring) { | |
290 | dp = writeescapedchar(dest, dp, c); | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
291 | } else if (!isalnum(c) && c!='_' && c!='#' && c!='.' && c!='@') { |
8 | 292 | /* interpret word int_t */ |
293 | if (wp > 0 && wp < WORDBUF_SIZE) { | |
294 | int closespan = 1; | |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
295 | if (iskeyword(word, highlighter->keywords)) { |
8 | 296 | memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); |
297 | dp += 29; | |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
298 | } else if (highlighter->istype(word, wp)) { |
8 | 299 | memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); |
300 | dp += 26; | |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
301 | } else if (highlighter->isdirective(word)) { |
10
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
302 | isinclude = !strncmp("#include", word, WORDBUF_SIZE); |
8 | 303 | memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31); |
304 | dp += 31; | |
9
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
305 | } else if (iscapsonly(word, wp)) { |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
306 | memcpy(&(dest[dp]), "<span class=\"c2html-macroconst\">", 32); |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
307 | dp += 32; |
8 | 308 | } else { |
309 | closespan = 0; | |
310 | } | |
311 | for (int i = 0 ; i < wp ; i++) { | |
312 | dp = writeescapedchar(dest, dp, word[i]); | |
313 | } | |
314 | if (closespan) { | |
315 | memcpy(&(dest[dp]), "</span>", 7); | |
316 | dp += 7; | |
317 | } | |
318 | } | |
9
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
319 | memset(word, 0, WORDBUF_SIZE); |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
320 | wp = 0; |
8 | 321 | dp = writeescapedchar(dest, dp, c); |
322 | } else { | |
323 | /* read word */ | |
324 | if (wp < WORDBUF_SIZE) { | |
325 | word[wp++] = c; | |
326 | } else if (wp == WORDBUF_SIZE) { | |
327 | for (int i = 0 ; i < WORDBUF_SIZE ; i++) { | |
328 | dp = writeescapedchar(dest, dp, word[i]); | |
329 | } | |
330 | wp++; | |
331 | dp = writeescapedchar(dest, dp, c); | |
332 | } else { | |
333 | dp = writeescapedchar(dest, dp, c); | |
334 | } | |
7 | 335 | } |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
336 | } |
8 | 337 | |
338 | isescaping = !isescaping & (c == '\\'); | |
4 | 339 | } |
340 | } | |
341 | dest[dp] = 0; | |
342 | } | |
343 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
344 | void printhelp() { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
345 | printf("Formats source code using HTML.\n\nUsage:\n" |
11 | 346 | " c2html [Options] FILE\n\n" |
347 | " Options:\n" | |
348 | " -h Prints this help message\n" | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
349 | " -j Highlight Java instead of C source code\n" |
11 | 350 | " -o <output> Output file (if not specified, stdout is used)\n" |
14 | 351 | " -p Disable highlighting (plain text)\n" |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
352 | "\n"); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
353 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
354 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
355 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
356 | |
4 | 357 | int lnint(size_t lnc) { |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
358 | int w = 1, p = 1; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
359 | while ((p*=10) < lnc) w++; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
360 | return w; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
361 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
362 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
363 | int main(int argc, char** argv) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
364 | |
11 | 365 | settings_t settings; |
366 | settings.outfilename = NULL; | |
12 | 367 | settings.highlight = 1; |
11 | 368 | |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
369 | highlighter_t highlighter; |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
370 | highlighter.isdirective = iscdirective; |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
371 | highlighter.istype = isctype; |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
372 | highlighter.keywords = ckeywords; |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
373 | |
11 | 374 | char optc; |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
375 | while ((optc = getopt(argc, argv, "hjo:p")) != -1) { |
11 | 376 | switch (optc) { |
377 | case 'o': | |
378 | if (!(optarg[0] == '-' && optarg[1] == 0)) { | |
379 | settings.outfilename = optarg; | |
380 | } | |
381 | break; | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
382 | case 'j': |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
383 | highlighter.isdirective = isjdirective; |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
384 | highlighter.istype = isjtype; |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
385 | highlighter.keywords = jkeywords; |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
386 | break; |
12 | 387 | case 'p': |
388 | settings.highlight = 0; | |
389 | break; | |
11 | 390 | case 'h': |
391 | printhelp(); | |
392 | return 0; | |
393 | default: | |
394 | return 1; | |
395 | } | |
396 | } | |
397 | ||
398 | if (optind != argc-1) { | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
399 | printhelp(); |
11 | 400 | return 1; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
401 | } else { |
11 | 402 | settings.infilename = argv[optind]; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
403 | |
11 | 404 | inputfile_t *inputfile = readinput(settings.infilename); |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
405 | if (inputfile) { |
11 | 406 | FILE *fout; |
15 | 407 | char *line; |
408 | if (settings.highlight) { | |
409 | line = (char*) malloc(inputfile->maxlinewidth*64); | |
410 | } else { | |
411 | line = NULL; | |
412 | } | |
11 | 413 | if (settings.outfilename) { |
414 | fout = fopen(settings.outfilename, "w"); | |
415 | } else { | |
416 | fout = stdout; | |
417 | } | |
418 | fprintf(fout, "<pre>\n"); | |
4 | 419 | int lnw = lnint(inputfile->count); |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
420 | for (int i = 0 ; i < inputfile->count ; i++) { |
12 | 421 | if (settings.highlight) { |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
422 | parseline(inputfile->lines[i], line, &highlighter); |
12 | 423 | } else { |
424 | line = inputfile->lines[i]; | |
425 | } | |
11 | 426 | fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s", |
9
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
427 | lnw, i+1, line); |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
428 | } |
15 | 429 | if (settings.highlight) { |
430 | free(line); | |
431 | } | |
11 | 432 | fprintf(fout, "</pre>\n"); |
433 | ||
434 | if (fout != stdout) { | |
435 | fclose(fout); | |
436 | } | |
437 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
438 | freeinputfilebuffer(inputfile); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
439 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
440 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
441 | return 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
442 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
443 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
444 |