src/c2html.c

Fri, 30 Aug 2013 11:23:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 30 Aug 2013 11:23:44 +0200
changeset 20
ebbf0776c1bc
parent 19
2e812df2b231
child 21
537aec525835
permissions
-rw-r--r--

replaced function static variables with struct members

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

mercurial