Wed, 10 Jul 2013 14:25:41 +0200
added -p to help message
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 | |
7 | 40 | #define istype(word, len) (word[len-2] == '_' && word[len-1] == 't') |
41 | #define isdirective(word) (word[0] == '#') | |
42 | ||
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
43 | const char* keywords[] = { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
44 | "auto", "break", "case", "char", "const", "continue", "default", "do", |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
45 | "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
46 | "long", "register", "return", "short", "signed", "sizeof", "static", "struct", |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
47 | "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
48 | }; |
4 | 49 | |
11 | 50 | typedef struct { |
51 | char* outfilename; | |
52 | char* infilename; | |
12 | 53 | int highlight; |
11 | 54 | } settings_t; |
4 | 55 | |
56 | typedef struct { | |
57 | size_t count; | |
58 | size_t capacity; | |
59 | size_t maxlinewidth; | |
60 | char** lines; | |
61 | } inputfile_t; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
62 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
63 | 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
|
64 | 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
|
65 | 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
|
66 | inputfile->capacity = capacity; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
67 | inputfile->count = 0; |
4 | 68 | inputfile->maxlinewidth = 0; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
69 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
70 | return inputfile; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
71 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
72 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
73 | 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
|
74 | 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
|
75 | memcpy(l, line, width); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
76 | l[width] = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
77 | if (inputfile->count >= inputfile->capacity) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
78 | inputfile->capacity <<= 1; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
79 | 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
|
80 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
81 | inputfile->lines[inputfile->count] = l; |
4 | 82 | inputfile->maxlinewidth = |
83 | 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
|
84 | inputfile->count++; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
85 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
86 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
87 | void freeinputfilebuffer(inputfile_t *inputfile) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
88 | 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
|
89 | free(inputfile->lines[i]); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
90 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
91 | free(inputfile->lines); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
92 | free(inputfile); |
0 | 93 | } |
94 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
95 | inputfile_t *readinput(char *filename) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
96 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
97 | 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
|
98 | if (fd == -1) return NULL; |
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 | inputfile_t *inputfile = inputfilebuffer(512); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
101 | |
4 | 102 | char buf[INPUTBUF_SIZE]; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
103 | ssize_t r; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
104 | |
4 | 105 | size_t maxlinewidth = 256; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
106 | char *line = (char*) malloc(maxlinewidth); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
107 | size_t col = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
108 | |
4 | 109 | 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
|
110 | 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
|
111 | if (col >= maxlinewidth-4) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
112 | maxlinewidth <<= 1; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
113 | line = realloc(line, maxlinewidth); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
114 | } |
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 | if (buf[i] == '\n') { |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
117 | line[col++] = '\n'; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
118 | line[col] = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
119 | addline(inputfile, line, col); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
120 | col = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
121 | } else { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
122 | line[col++] = buf[i]; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
123 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
124 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
125 | } |
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 | free(line); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
128 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
129 | close(fd); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
130 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
131 | return inputfile; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
132 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
133 | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
134 | 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
|
135 | if (c == '>') { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
136 | dest[dp++] = '&'; dest[dp++] = 'g'; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
137 | dest[dp++] = 't'; dest[dp++] = ';'; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
138 | } else if (c == '<') { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
139 | dest[dp++] = '&'; dest[dp++] = 'l'; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
140 | dest[dp++] = 't'; dest[dp++] = ';'; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
141 | } else { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
142 | dest[dp++] = c; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
143 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
144 | |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
145 | return dp; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
146 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
147 | |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
148 | int iskeyword(char *word) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
149 | for (int i = 0 ; keywords[i] ; i++) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
150 | if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
151 | return 1; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
152 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
153 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
154 | return 0; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
155 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
156 | |
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
|
157 | 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
|
158 | 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
|
159 | 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
|
160 | 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
|
161 | } |
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
|
162 | } |
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
|
163 | 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
|
164 | } |
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
|
165 | |
4 | 166 | void parseline(char *src, char *dest) { |
167 | size_t sp = 0, dp = 0; | |
168 | /* indent */ | |
169 | while (isspace(src[sp])) { | |
170 | dest[dp++] = src[sp++]; | |
171 | } | |
10
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
172 | |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
173 | static char word[WORDBUF_SIZE]; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
174 | static char includefile[FILENAME_MAX]; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
175 | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
176 | memset(word, 0, WORDBUF_SIZE); |
10
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
177 | size_t wp = 0, ifp = 0; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
178 | int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; |
8 | 179 | static int iscommentml; |
7 | 180 | int isescaping = 0; |
8 | 181 | |
182 | if (iscommentml) { | |
183 | iscomment = 1; | |
184 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); | |
185 | dp += 29; | |
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 | |
4 | 188 | for (char c = src[sp] ; c ; c=src[++sp]) { |
8 | 189 | /* comments */ |
190 | if (c == '/') { | |
191 | if (iscommentml && sp > 0 && src[sp-1] == '*') { | |
192 | iscomment = 0; | |
193 | iscommentml = 0; | |
194 | memcpy(&(dest[dp]), "/</span>", 8); | |
195 | dp += 8; | |
196 | continue; | |
197 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { | |
198 | iscomment = 1; | |
199 | iscommentml = (src[sp+1] == '*'); | |
200 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); | |
201 | dp += 29; | |
202 | } | |
203 | } | |
204 | ||
205 | if (iscomment) { | |
206 | if (c == '\n') { | |
7 | 207 | memcpy(&(dest[dp]), "</span>", 7); |
208 | dp += 7; | |
209 | } | |
8 | 210 | dp = writeescapedchar(dest, dp, c); |
10
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
211 | } else if (isinclude) { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
212 | if (c == '<') { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
213 | 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
|
214 | dp += 32; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
215 | dp = writeescapedchar(dest, dp, c); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
216 | } else if (c == '\"') { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
217 | if (parseinclude) { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
218 | dest[dp++] = '\"'; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
219 | dest[dp++] = '>'; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
220 | memcpy(&(dest[dp]), includefile, ifp); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
221 | dp += ifp; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
222 | |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
223 | dp = writeescapedchar(dest, dp, c); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
224 | memcpy(&(dest[dp]), "</a>", 4); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
225 | dp += 4; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
226 | parseinclude = 0; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
227 | } else { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
228 | 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
|
229 | dp += 35; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
230 | dp = writeescapedchar(dest, dp, c); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
231 | ifp = 0; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
232 | includefile[ifp++] = '\"'; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
233 | parseinclude = 1; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
234 | } |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
235 | } else if (c == '>') { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
236 | dp = writeescapedchar(dest, dp, c); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
237 | memcpy(&(dest[dp]), "</span>", 7); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
238 | dp += 7; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
239 | } else { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
240 | if (parseinclude) { |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
241 | includefile[ifp++] = c; |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
242 | } |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
243 | dp = writeescapedchar(dest, dp, c); |
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
244 | } |
7 | 245 | } else { |
8 | 246 | /* strings */ |
247 | if (!isescaping && (c == '\'' || c == '\"')) { | |
248 | isstring ^= 1; | |
249 | if (isstring) { | |
250 | memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); | |
251 | dp += 28; | |
7 | 252 | dp = writeescapedchar(dest, dp, c); |
253 | } else { | |
254 | dp = writeescapedchar(dest, dp, c); | |
8 | 255 | memcpy(&(dest[dp]), "</span>", 7); |
256 | dp += 7; | |
257 | } | |
258 | } else { | |
259 | if (isstring) { | |
260 | dp = writeescapedchar(dest, dp, c); | |
10
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
261 | } else if (!isalnum(c) && c != '_' && c != '#' && c != '.') { |
8 | 262 | /* interpret word int_t */ |
263 | if (wp > 0 && wp < WORDBUF_SIZE) { | |
264 | int closespan = 1; | |
265 | if (iskeyword(word)) { | |
266 | memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); | |
267 | dp += 29; | |
268 | } else if (istype(word, wp)) { | |
269 | memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); | |
270 | dp += 26; | |
271 | } else if (isdirective(word)) { | |
10
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
272 | isinclude = !strncmp("#include", word, WORDBUF_SIZE); |
8 | 273 | memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31); |
274 | 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
|
275 | } 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
|
276 | 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
|
277 | dp += 32; |
8 | 278 | } else { |
279 | closespan = 0; | |
280 | } | |
281 | for (int i = 0 ; i < wp ; i++) { | |
282 | dp = writeescapedchar(dest, dp, word[i]); | |
283 | } | |
284 | if (closespan) { | |
285 | memcpy(&(dest[dp]), "</span>", 7); | |
286 | dp += 7; | |
287 | } | |
288 | } | |
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
|
289 | 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
|
290 | wp = 0; |
8 | 291 | dp = writeescapedchar(dest, dp, c); |
292 | } else { | |
293 | /* read word */ | |
294 | if (wp < WORDBUF_SIZE) { | |
295 | word[wp++] = c; | |
296 | } else if (wp == WORDBUF_SIZE) { | |
297 | for (int i = 0 ; i < WORDBUF_SIZE ; i++) { | |
298 | dp = writeescapedchar(dest, dp, word[i]); | |
299 | } | |
300 | wp++; | |
301 | dp = writeescapedchar(dest, dp, c); | |
302 | } else { | |
303 | dp = writeescapedchar(dest, dp, c); | |
304 | } | |
7 | 305 | } |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
306 | } |
8 | 307 | |
308 | isescaping = !isescaping & (c == '\\'); | |
4 | 309 | } |
310 | } | |
311 | dest[dp] = 0; | |
312 | } | |
313 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
314 | void printhelp() { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
315 | printf("Formats source code using HTML.\n\nUsage:\n" |
11 | 316 | " c2html [Options] FILE\n\n" |
317 | " Options:\n" | |
318 | " -h Prints this help message\n" | |
319 | " -o <output> Output file (if not specified, stdout is used)\n" | |
14 | 320 | " -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
|
321 | "\n"); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
322 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
323 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
324 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
325 | |
4 | 326 | 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
|
327 | int w = 1, p = 1; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
328 | while ((p*=10) < lnc) w++; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
329 | return w; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
330 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
331 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
332 | 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
|
333 | |
11 | 334 | settings_t settings; |
335 | settings.outfilename = NULL; | |
12 | 336 | settings.highlight = 1; |
11 | 337 | |
338 | char optc; | |
12 | 339 | while ((optc = getopt(argc, argv, "ho:p")) != -1) { |
11 | 340 | switch (optc) { |
341 | case 'o': | |
342 | if (!(optarg[0] == '-' && optarg[1] == 0)) { | |
343 | settings.outfilename = optarg; | |
344 | } | |
345 | break; | |
12 | 346 | case 'p': |
347 | settings.highlight = 0; | |
348 | break; | |
11 | 349 | case 'h': |
350 | printhelp(); | |
351 | return 0; | |
352 | default: | |
353 | return 1; | |
354 | } | |
355 | } | |
356 | ||
357 | if (optind != argc-1) { | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
358 | printhelp(); |
11 | 359 | return 1; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
360 | } else { |
11 | 361 | settings.infilename = argv[optind]; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
362 | |
11 | 363 | 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
|
364 | if (inputfile) { |
11 | 365 | FILE *fout; |
366 | if (settings.outfilename) { | |
367 | fout = fopen(settings.outfilename, "w"); | |
368 | } else { | |
369 | fout = stdout; | |
370 | } | |
371 | fprintf(fout, "<pre>\n"); | |
12 | 372 | char *line = (char*) malloc(inputfile->maxlinewidth |
373 | * (settings.highlight?64:0)); | |
4 | 374 | 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
|
375 | for (int i = 0 ; i < inputfile->count ; i++) { |
12 | 376 | if (settings.highlight) { |
377 | parseline(inputfile->lines[i], line); | |
378 | } else { | |
379 | line = inputfile->lines[i]; | |
380 | } | |
11 | 381 | 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
|
382 | lnw, i+1, line); |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
383 | } |
4 | 384 | free(line); |
11 | 385 | fprintf(fout, "</pre>\n"); |
386 | ||
387 | if (fout != stdout) { | |
388 | fclose(fout); | |
389 | } | |
390 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
391 | freeinputfilebuffer(inputfile); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
392 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
393 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
394 | return 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
395 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
396 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
397 |