src/c2html.c

Thu, 23 Jan 2014 09:19:37 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Jan 2014 09:19:37 +0100
changeset 21
537aec525835
parent 20
ebbf0776c1bc
child 22
f463693b5eeb
permissions
-rw-r--r--

structured source code

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 *
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
4 * Copyright 2014 Mike Becker. All rights reserved.
1
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
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
37 #include "javacodegen.h"
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
38 #include "ccodegen.h"
18
5085b57e3fd6 fixed highlighting for java
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
39
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
40 #define INPUTBUF_SIZE 2048
16
fa0bcd0444eb prepared java highlighting
Mike Becker <universe@uap-core.de>
parents: 15
diff changeset
41
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
42 typedef struct {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
43 char* outfilename;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
44 char* infilename;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
45 int highlight;
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
46 } settings_t;
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
47
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
48 typedef struct {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
49 size_t count;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
50 size_t capacity;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
51 size_t maxlinewidth;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
52 char** lines;
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
53 } inputfile_t;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
54
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
55 inputfile_t *inputfilebuffer(size_t capacity) {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
56 inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
57 inputfile->lines = (char**) malloc(capacity * sizeof(char*));
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
58 inputfile->capacity = capacity;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
59 inputfile->count = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
60 inputfile->maxlinewidth = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
61
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
62 return inputfile;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
63 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
64
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
65 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
66 char *l = (char*) malloc(width+1);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
67 memcpy(l, line, width);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
68 l[width] = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
69 if (inputfile->count >= inputfile->capacity) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
70 inputfile->capacity <<= 1;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
71 inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
72 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
73 inputfile->lines[inputfile->count] = l;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
74 inputfile->maxlinewidth =
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
75 width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
76 inputfile->count++;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
77 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
78
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
79 void freeinputfilebuffer(inputfile_t *inputfile) {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
80 for (int i = 0 ; i < inputfile->count ; i++) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
81 free(inputfile->lines[i]);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
82 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
83 free(inputfile->lines);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
84 free(inputfile);
0
e78e463e1aa9 init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 }
e78e463e1aa9 init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
87 inputfile_t *readinput(char *filename) {
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
88
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
89 int fd = open(filename, O_RDONLY);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
90 if (fd == -1) return NULL;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
91
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
92 inputfile_t *inputfile = inputfilebuffer(512);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
93
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
94 char buf[INPUTBUF_SIZE];
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
95 ssize_t r;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
96
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
97 size_t maxlinewidth = 256;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
98 char *line = (char*) malloc(maxlinewidth);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
99 size_t col = 0;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
100
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
101 while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
102 for (size_t i = 0 ; i < r ; i++) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
103 if (col >= maxlinewidth-4) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
104 maxlinewidth <<= 1;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
105 line = realloc(line, maxlinewidth);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
106 }
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 if (buf[i] == '\n') {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
109 line[col++] = '\n';
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
110 line[col] = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
111 addline(inputfile, line, col);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
112 col = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
113 } else {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
114 line[col++] = buf[i];
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
115 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
116 }
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
117 }
19
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 free(line);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
120
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
121 close(fd);
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 return inputfile;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
124 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
125
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
126 void printhelp() {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
127 printf("Formats source code using HTML.\n\nUsage:\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
128 " c2html [Options] FILE\n\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
129 " Options:\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
130 " -h Prints this help message\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
131 " -j Highlight Java instead of C source code\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
132 " -o <output> Output file (stdout, if not specified)\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
133 " -p Disable highlighting (plain text)\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
134 "\n");
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
135
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
136
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
137 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
138
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
139 int lnint(size_t lnc) {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
140 int w = 1, p = 1;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
141 while ((p*=10) < lnc) w++;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
142 return w;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
143 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
144
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
145 int main(int argc, char** argv) {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
146 settings_t settings;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
147 settings.outfilename = NULL;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
148 settings.highlight = 1;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
149
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
150 highlighter_t highlighter;
20
ebbf0776c1bc replaced function static variables with struct members
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
151 memset(&highlighter, 0, sizeof(highlighter));
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
152 highlighter.isdirective = iscdirective;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
153 highlighter.istype = isctype;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
154 highlighter.keywords = ckeywords;
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
155 highlighter.parser = cparseline;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
156
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
157 char optc;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
158 while ((optc = getopt(argc, argv, "hjo:p")) != -1) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
159 switch (optc) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
160 case 'o':
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
161 if (!(optarg[0] == '-' && optarg[1] == 0)) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
162 settings.outfilename = optarg;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
163 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
164 break;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
165 case 'j':
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
166 highlighter.isdirective = isjdirective;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
167 highlighter.istype = isjtype;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
168 highlighter.keywords = jkeywords;
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
169 highlighter.parser = jparseline;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
170 break;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
171 case 'p':
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
172 settings.highlight = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
173 break;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
174 case 'h':
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
175 printhelp();
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
176 return 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
177 default:
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
178 return 1;
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
179 }
19
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
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
182 if (optind != argc-1) {
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
183 printhelp();
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
184 return 1;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
185 } else {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
186 settings.infilename = argv[optind];
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
187
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
188 inputfile_t *inputfile = readinput(settings.infilename);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
189 if (inputfile) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
190 FILE *fout;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
191 char *line;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
192 if (settings.highlight) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
193 line = (char*) malloc(inputfile->maxlinewidth*64);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
194 } else {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
195 line = NULL;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
196 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
197 if (settings.outfilename) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
198 fout = fopen(settings.outfilename, "w");
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
199 } else {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
200 fout = stdout;
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 fprintf(fout, "<pre>\n");
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
203 int lnw = lnint(inputfile->count);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
204 for (int i = 0 ; i < inputfile->count ; i++) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
205 if (settings.highlight) {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
206 highlighter.parser(inputfile->lines[i], line, &highlighter);
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
207 } else {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
208 line = inputfile->lines[i];
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
209 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
210 fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
211 lnw, i+1, line);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
212 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
213 if (settings.highlight) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
214 free(line);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
215 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
216 fprintf(fout, "</pre>\n");
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 if (fout != stdout) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
219 fclose(fout);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
220 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
221
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
222 freeinputfilebuffer(inputfile);
12
7ce5c4b51959 option for plaintext
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
223 }
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
224
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
225 return 0;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
226 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
227 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
228

mercurial