Wed, 10 Jul 2013 13:45:26 +0200
option for plaintext
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 | |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
40 | const char* keywords[] = { |
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", |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
43 | "long", "register", "return", "short", "signed", "sizeof", "static", "struct", |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
44 | "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
45 | }; |
4 | 46 | |
11 | 47 | typedef struct { |
48 | char* outfilename; | |
49 | char* infilename; | |
12 | 50 | int highlight; |
11 | 51 | } settings_t; |
4 | 52 | |
53 | typedef struct { | |
54 | size_t count; | |
55 | size_t capacity; | |
56 | size_t maxlinewidth; | |
57 | char** lines; | |
58 | } inputfile_t; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
59 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
60 | 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
|
61 | 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
|
62 | 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
|
63 | inputfile->capacity = capacity; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
64 | inputfile->count = 0; |
4 | 65 | inputfile->maxlinewidth = 0; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
66 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
67 | return inputfile; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
68 | } |
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 | 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
|
71 | 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
|
72 | memcpy(l, line, width); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
73 | l[width] = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
74 | if (inputfile->count >= inputfile->capacity) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
75 | inputfile->capacity <<= 1; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
76 | 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
|
77 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
78 | inputfile->lines[inputfile->count] = l; |
4 | 79 | inputfile->maxlinewidth = |
80 | 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
|
81 | inputfile->count++; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
82 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
83 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
84 | void freeinputfilebuffer(inputfile_t *inputfile) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
85 | 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
|
86 | free(inputfile->lines[i]); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
87 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
88 | free(inputfile->lines); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
89 | free(inputfile); |
0 | 90 | } |
91 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
92 | inputfile_t *readinput(char *filename) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
93 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
94 | 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
|
95 | if (fd == -1) return NULL; |
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 | inputfile_t *inputfile = inputfilebuffer(512); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
98 | |
4 | 99 | char buf[INPUTBUF_SIZE]; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
100 | ssize_t r; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
101 | |
4 | 102 | size_t maxlinewidth = 256; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
103 | char *line = (char*) malloc(maxlinewidth); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
104 | size_t col = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
105 | |
4 | 106 | 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
|
107 | 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
|
108 | if (col >= maxlinewidth-4) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
109 | maxlinewidth <<= 1; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
110 | line = realloc(line, maxlinewidth); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
111 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
112 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
113 | if (buf[i] == '\n') { |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
114 | line[col++] = '\n'; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
115 | line[col] = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
116 | addline(inputfile, line, col); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
117 | col = 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
118 | } else { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
119 | line[col++] = buf[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 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
122 | } |
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 | free(line); |
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 | close(fd); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
127 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
128 | return inputfile; |
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 | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
131 | 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
|
132 | if (c == '>') { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
133 | dest[dp++] = '&'; dest[dp++] = 'g'; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
134 | dest[dp++] = 't'; dest[dp++] = ';'; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
135 | } else if (c == '<') { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
136 | dest[dp++] = '&'; dest[dp++] = 'l'; |
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 { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
139 | dest[dp++] = c; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
140 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
141 | |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
142 | return dp; |
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 | int iskeyword(char *word) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
146 | for (int i = 0 ; keywords[i] ; i++) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
147 | if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
148 | return 1; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
149 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
150 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
151 | return 0; |
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 | #define istype(word, len) (word[len-2] == '_' && word[len-1] == 't') |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
155 | |
4 | 156 | void parseline(char *src, char *dest) { |
157 | size_t sp = 0, dp = 0; | |
158 | /* indent */ | |
159 | while (isspace(src[sp])) { | |
160 | dest[dp++] = src[sp++]; | |
161 | } | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
162 | char word[WORDBUF_SIZE]; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
163 | memset(word, 0, WORDBUF_SIZE); |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
164 | size_t wp = 0; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
165 | int closespan; |
4 | 166 | for (char c = src[sp] ; c ; c=src[++sp]) { |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
167 | if (!isalnum(c) && c != '_') { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
168 | /* interpret word int_t */ |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
169 | if (wp > 0 && wp < WORDBUF_SIZE) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
170 | if (iskeyword(word)) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
171 | memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
172 | dp += 29; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
173 | closespan = 1; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
174 | } else if (istype(word, wp)) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
175 | memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
176 | dp += 26; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
177 | closespan = 1; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
178 | } else { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
179 | closespan = 0; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
180 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
181 | for (int i = 0 ; i < wp ; i++) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
182 | dp = writeescapedchar(dest, dp, word[i]); |
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 | if (closespan) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
185 | memcpy(&(dest[dp]), "</span>", 7); |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
186 | dp += 7; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
187 | } |
6 | 188 | memset(word, 0, WORDBUF_SIZE); |
189 | wp = 0; | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
190 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
191 | dp = writeescapedchar(dest, dp, c); |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
192 | } else { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
193 | /* read word */ |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
194 | if (wp < WORDBUF_SIZE) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
195 | word[wp++] = c; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
196 | } else if (wp == WORDBUF_SIZE) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
197 | for (int i = 0 ; i < WORDBUF_SIZE ; i++) { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
198 | dp = writeescapedchar(dest, dp, word[i]); |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
199 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
200 | wp++; |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
201 | dp = writeescapedchar(dest, dp, c); |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
202 | } else { |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
203 | dp = writeescapedchar(dest, dp, c); |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
204 | } |
4 | 205 | } |
206 | } | |
207 | dest[dp] = 0; | |
208 | } | |
209 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
210 | void printhelp() { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
211 | printf("Formats source code using HTML.\n\nUsage:\n" |
11 | 212 | " c2html [Options] FILE\n\n" |
213 | " Options:\n" | |
214 | " -h Prints this help message\n" | |
215 | " -o <output> Output file (if not specified, stdout is used)\n" | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
216 | "\n"); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
217 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
218 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
219 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
220 | |
4 | 221 | 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
|
222 | int w = 1, p = 1; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
223 | while ((p*=10) < lnc) w++; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
224 | return w; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
225 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
226 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
227 | 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
|
228 | |
11 | 229 | settings_t settings; |
230 | settings.outfilename = NULL; | |
12 | 231 | settings.highlight = 1; |
11 | 232 | |
233 | char optc; | |
12 | 234 | while ((optc = getopt(argc, argv, "ho:p")) != -1) { |
11 | 235 | switch (optc) { |
236 | case 'o': | |
237 | if (!(optarg[0] == '-' && optarg[1] == 0)) { | |
238 | settings.outfilename = optarg; | |
239 | } | |
240 | break; | |
12 | 241 | case 'p': |
242 | settings.highlight = 0; | |
243 | break; | |
11 | 244 | case 'h': |
245 | printhelp(); | |
246 | return 0; | |
247 | default: | |
248 | return 1; | |
249 | } | |
250 | } | |
251 | ||
252 | if (optind != argc-1) { | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
253 | printhelp(); |
11 | 254 | return 1; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
255 | } else { |
11 | 256 | settings.infilename = argv[optind]; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
257 | |
11 | 258 | 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
|
259 | if (inputfile) { |
11 | 260 | FILE *fout; |
261 | if (settings.outfilename) { | |
262 | fout = fopen(settings.outfilename, "w"); | |
263 | } else { | |
264 | fout = stdout; | |
265 | } | |
266 | fprintf(fout, "<pre>\n"); | |
12 | 267 | char *line = (char*) malloc(inputfile->maxlinewidth |
268 | * (settings.highlight?64:0)); | |
4 | 269 | 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
|
270 | for (int i = 0 ; i < inputfile->count ; i++) { |
12 | 271 | if (settings.highlight) { |
272 | parseline(inputfile->lines[i], line); | |
273 | } else { | |
274 | line = inputfile->lines[i]; | |
275 | } | |
11 | 276 | fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s", |
4 | 277 | lnw, i, line); |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
278 | } |
4 | 279 | free(line); |
11 | 280 | fprintf(fout, "</pre>\n"); |
281 | ||
282 | if (fout != stdout) { | |
283 | fclose(fout); | |
284 | } | |
285 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
286 | freeinputfilebuffer(inputfile); |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
287 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
288 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
289 | return 0; |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
290 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
291 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
292 |