src/c2html.c

Fri, 21 Jun 2013 13:32:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 21 Jun 2013 13:32:31 +0200
changeset 10
925172e535a9
parent 9
6b1fba10c4cb
child 13
fe74bf2d5f27
permissions
-rw-r--r--

includes (with links in user includes)

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
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
35 #include <ctype.h>
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
36
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
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
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
40 #define istype(word, len) (word[len-2] == '_' && word[len-1] == 't')
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
41 #define isdirective(word) (word[0] == '#')
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
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
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
49
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
50
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
51 typedef struct {
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
52 size_t count;
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
53 size_t capacity;
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
54 size_t maxlinewidth;
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
55 char** lines;
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
56 } inputfile_t;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
57
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
58 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
59 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
60 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
61 inputfile->capacity = capacity;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
62 inputfile->count = 0;
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
63 inputfile->maxlinewidth = 0;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
64
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
65 return inputfile;
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
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
68 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
69 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
70 memcpy(l, line, width);
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
71 l[width] = 0;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
72 if (inputfile->count >= inputfile->capacity) {
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
73 inputfile->capacity <<= 1;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
74 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
75 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
76 inputfile->lines[inputfile->count] = l;
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
77 inputfile->maxlinewidth =
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
78 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
79 inputfile->count++;
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
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
82 void freeinputfilebuffer(inputfile_t *inputfile) {
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
83 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
84 free(inputfile->lines[i]);
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 free(inputfile->lines);
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
87 free(inputfile);
0
e78e463e1aa9 init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 }
e78e463e1aa9 init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
90 inputfile_t *readinput(char *filename) {
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
91
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
92 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
93 if (fd == -1) return NULL;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
94
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
95 inputfile_t *inputfile = inputfilebuffer(512);
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
96
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
97 char buf[INPUTBUF_SIZE];
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
98 ssize_t r;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
99
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
100 size_t maxlinewidth = 256;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
101 char *line = (char*) malloc(maxlinewidth);
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
102 size_t col = 0;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
103
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
104 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
105 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
106 if (col >= maxlinewidth-4) {
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
107 maxlinewidth <<= 1;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
108 line = realloc(line, maxlinewidth);
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
109 }
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 if (buf[i] == '\n') {
5
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
112 line[col++] = '\n';
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
113 line[col] = 0;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
114 addline(inputfile, line, col);
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
115 col = 0;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
116 } else {
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
117 line[col++] = buf[i];
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
118 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
119 }
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 free(line);
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 close(fd);
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 return inputfile;
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
5
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
129 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
130 if (c == '>') {
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
131 dest[dp++] = '&'; dest[dp++] = 'g';
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
132 dest[dp++] = 't'; dest[dp++] = ';';
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
133 } else if (c == '<') {
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
134 dest[dp++] = '&'; dest[dp++] = 'l';
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
135 dest[dp++] = 't'; dest[dp++] = ';';
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
136 } else {
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
137 dest[dp++] = c;
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
138 }
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
139
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
140 return dp;
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
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
143 int iskeyword(char *word) {
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
144 for (int i = 0 ; keywords[i] ; i++) {
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
145 if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
146 return 1;
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 }
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
149 return 0;
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
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
152 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
153 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
154 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
155 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
156 }
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 }
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 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
159 }
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
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
161 void parseline(char *src, char *dest) {
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
162 size_t sp = 0, dp = 0;
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
163 /* indent */
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
164 while (isspace(src[sp])) {
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
165 dest[dp++] = src[sp++];
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
166 }
10
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
167
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
168 static char word[WORDBUF_SIZE];
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
169 static char includefile[FILENAME_MAX];
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
170
5
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
171 memset(word, 0, WORDBUF_SIZE);
10
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
172 size_t wp = 0, ifp = 0;
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
173 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
8
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
174 static int iscommentml;
7
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
175 int isescaping = 0;
8
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
176
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
177 if (iscommentml) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
178 iscomment = 1;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
179 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
180 dp += 29;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
181 }
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
182
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
183 for (char c = src[sp] ; c ; c=src[++sp]) {
8
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
184 /* comments */
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
185 if (c == '/') {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
186 if (iscommentml && sp > 0 && src[sp-1] == '*') {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
187 iscomment = 0;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
188 iscommentml = 0;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
189 memcpy(&(dest[dp]), "/</span>", 8);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
190 dp += 8;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
191 continue;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
192 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
193 iscomment = 1;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
194 iscommentml = (src[sp+1] == '*');
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
195 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
196 dp += 29;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
197 }
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
198 }
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
199
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
200 if (iscomment) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
201 if (c == '\n') {
7
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
202 memcpy(&(dest[dp]), "</span>", 7);
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
203 dp += 7;
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
204 }
8
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
205 dp = writeescapedchar(dest, dp, c);
10
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
206 } else if (isinclude) {
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
207 if (c == '<') {
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
208 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
209 dp += 32;
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
210 dp = writeescapedchar(dest, dp, c);
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
211 } else if (c == '\"') {
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
212 if (parseinclude) {
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
213 dest[dp++] = '\"';
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
214 dest[dp++] = '>';
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
215 memcpy(&(dest[dp]), includefile, ifp);
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
216 dp += ifp;
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
217
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
218 dp = writeescapedchar(dest, dp, c);
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
219 memcpy(&(dest[dp]), "</a>", 4);
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
220 dp += 4;
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
221 parseinclude = 0;
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
222 } else {
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
223 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
224 dp += 35;
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
225 dp = writeescapedchar(dest, dp, c);
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
226 ifp = 0;
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
227 includefile[ifp++] = '\"';
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
228 parseinclude = 1;
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
229 }
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
230 } else if (c == '>') {
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
231 dp = writeescapedchar(dest, dp, c);
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
232 memcpy(&(dest[dp]), "</span>", 7);
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
233 dp += 7;
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
234 } else {
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
235 if (parseinclude) {
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
236 includefile[ifp++] = c;
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
237 }
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
238 dp = writeescapedchar(dest, dp, c);
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
239 }
7
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
240 } else {
8
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
241 /* strings */
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
242 if (!isescaping && (c == '\'' || c == '\"')) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
243 isstring ^= 1;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
244 if (isstring) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
245 memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
246 dp += 28;
7
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
247 dp = writeescapedchar(dest, dp, c);
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
248 } else {
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
249 dp = writeescapedchar(dest, dp, c);
8
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
250 memcpy(&(dest[dp]), "</span>", 7);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
251 dp += 7;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
252 }
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
253 } else {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
254 if (isstring) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
255 dp = writeescapedchar(dest, dp, c);
10
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
256 } else if (!isalnum(c) && c != '_' && c != '#' && c != '.') {
8
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
257 /* interpret word int_t */
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
258 if (wp > 0 && wp < WORDBUF_SIZE) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
259 int closespan = 1;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
260 if (iskeyword(word)) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
261 memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
262 dp += 29;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
263 } else if (istype(word, wp)) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
264 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
265 dp += 26;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
266 } else if (isdirective(word)) {
10
925172e535a9 includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
267 isinclude = !strncmp("#include", word, WORDBUF_SIZE);
8
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
268 memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
269 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
270 } 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
271 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
272 dp += 32;
8
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
273 } else {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
274 closespan = 0;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
275 }
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
276 for (int i = 0 ; i < wp ; i++) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
277 dp = writeescapedchar(dest, dp, word[i]);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
278 }
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
279 if (closespan) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
280 memcpy(&(dest[dp]), "</span>", 7);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
281 dp += 7;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
282 }
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
283 }
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
284 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
285 wp = 0;
8
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
286 dp = writeescapedchar(dest, dp, c);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
287 } else {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
288 /* read word */
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
289 if (wp < WORDBUF_SIZE) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
290 word[wp++] = c;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
291 } else if (wp == WORDBUF_SIZE) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
292 for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
293 dp = writeescapedchar(dest, dp, word[i]);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
294 }
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
295 wp++;
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
296 dp = writeescapedchar(dest, dp, c);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
297 } else {
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
298 dp = writeescapedchar(dest, dp, c);
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
299 }
7
02cf8ac736e6 strings and directives
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
300 }
5
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
301 }
8
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
302
417cd3b29f97 comments
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
303 isescaping = !isescaping & (c == '\\');
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
304 }
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
305 }
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
306 dest[dp] = 0;
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
307 }
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
308
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
309 void printhelp() {
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
310 printf("Formats source code using HTML.\n\nUsage:\n"
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
311 " c2html [FILE...]"
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
312 "\n");
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
313
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
314
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
315 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
316
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
317 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
318 int w = 1, p = 1;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
319 while ((p*=10) < lnc) w++;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
320 return w;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
321 }
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 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
324
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
325 if (argc == 1) {
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
326 printhelp();
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
327 return 0;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
328 } else {
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
329
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
330 inputfile_t *inputfile = readinput(argv[1]);
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
331 if (inputfile) {
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
332 printf("<pre>\n");
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
333 char *line = (char*) malloc(inputfile->maxlinewidth*64);
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
334 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
335 for (int i = 0 ; i < inputfile->count ; i++) {
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
336 parseline(inputfile->lines[i], line);
5
412f1896874b support for keywords and types
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
337 printf("<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
338 lnw, i+1, line);
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
339 }
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
340 free(line);
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
341 printf("</pre>\n");
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
342 freeinputfilebuffer(inputfile);
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
343 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
344
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
345 return 0;
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
346 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
347 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
348

mercurial