src/c2html.c

Fri, 30 Aug 2013 10:51:49 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 30 Aug 2013 10:51:49 +0200
changeset 19
2e812df2b231
parent 18
5085b57e3fd6
child 20
ebbf0776c1bc
permissions
-rw-r--r--

formatted with 4 spaces

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

mercurial