src/c2html.c

Fri, 04 Mar 2016 14:03:06 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 04 Mar 2016 14:03:06 +0100
changeset 33
d7b475e4363c
parent 30
0bfd4d6f086a
child 35
abaf2489c549
permissions
-rw-r--r--

fixed macros with digits not highlighted

23
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
1 /*
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
3 *
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
4 * Copyright 2015 Mike Becker. All rights reserved.
23
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
5 *
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
6 * Redistribution and use in source and binary forms, with or without
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
7 * modification, are permitted provided that the following conditions are met:
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
8 *
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
10 * notice, this list of conditions and the following disclaimer.
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
11 *
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
14 * documentation and/or other materials provided with the distribution.
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
15 *
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
27 *
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
28 */
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
29 #include "c2html.h"
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
30
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
31 inputfile_t *inputfilebuffer(size_t capacity) {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
32 inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
33 inputfile->lines = (char**) malloc(capacity * sizeof(char*));
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
34 inputfile->capacity = capacity;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
35 inputfile->count = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
36 inputfile->maxlinewidth = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
37
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
38 return inputfile;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
39 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
40
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
41 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
42 char *l = (char*) malloc(width+1);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
43 memcpy(l, line, width);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
44 l[width] = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
45 if (inputfile->count >= inputfile->capacity) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
46 inputfile->capacity <<= 1;
25
f82aa7afe872 more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
47 inputfile->lines = realloc(inputfile->lines,
f82aa7afe872 more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
48 sizeof(char*)*inputfile->capacity);
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
49 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
50 inputfile->lines[inputfile->count] = l;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
51 inputfile->maxlinewidth =
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
52 width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
53 inputfile->count++;
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
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
56 void freeinputfilebuffer(inputfile_t *inputfile) {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
57 for (int i = 0 ; i < inputfile->count ; i++) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
58 free(inputfile->lines[i]);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
59 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
60 free(inputfile->lines);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
61 free(inputfile);
0
e78e463e1aa9 init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 }
e78e463e1aa9 init project
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
64 inputfile_t *readinput(char *filename) {
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
65
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
66 int fd = open(filename, O_RDONLY);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
67 if (fd == -1) return NULL;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
68
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
69 inputfile_t *inputfile = inputfilebuffer(512);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
70
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
71 char buf[INPUTBUF_SIZE];
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
72 ssize_t r;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
73
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
74 size_t maxlinewidth = 256;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
75 char *line = (char*) malloc(maxlinewidth);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
76 size_t col = 0;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
77
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
78 while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
79 for (size_t i = 0 ; i < r ; i++) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
80 if (col >= maxlinewidth-4) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
81 maxlinewidth <<= 1;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
82 line = realloc(line, maxlinewidth);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
83 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
84
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
85 if (buf[i] == '\n') {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
86 line[col++] = '\n';
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
87 line[col] = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
88 addline(inputfile, line, col);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
89 col = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
90 } else {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
91 line[col++] = buf[i];
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
92 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
93 }
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
94 }
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
95
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
96 free(line);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
97
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
98 close(fd);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
99
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
100 return inputfile;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
101 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
102
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
103 void printhelp() {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
104 printf("Formats source code using HTML.\n\nUsage:\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
105 " c2html [Options] FILE\n\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
106 " Options:\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
107 " -h Prints this help message\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
108 " -j Highlight Java instead of C source code\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
109 " -o <output> Output file (stdout, if not specified)\n"
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
110 " -H <header> Prepend header file\n"
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
111 " -F <footer> Append footer file\n"
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
112 " -p Disable highlighting (plain text)\n"
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
113 " -l Disable line numbers\n"
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
114 "\n");
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 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
118
4
323f674931fe introduced parser
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
119 int lnint(size_t lnc) {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
120 int w = 1, p = 1;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
121 while ((p*=10) < lnc) w++;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
122 return w;
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
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
125 int copyfile(char *filename, FILE *dest) {
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
126 if (!filename) {
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
127 return 0;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
128 }
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
129
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
130 FILE *src = fopen(filename, "r");
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
131 if (src) {
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
132 char buf[4096];
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
133 int r;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
134 while ((r = fread(buf, 1, 4096, src)) > 0) {
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
135 fwrite(buf, 1, r, dest);
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
136 }
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
137 fclose(src);
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
138 return 0;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
139 } else {
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
140 return -1;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
141 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
142 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
143
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
144 #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream)
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
145 int formatfile(
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
146 highlighter_t *highlighter,
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
147 inputfile_t *in,
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
148 fmt_write_func out,
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
149 void *stream,
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
150 _Bool showln) {
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
151 // formats an input file and writes the result to out
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
152
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
153 char *line = malloc(in->maxlinewidth*64);
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
154 if(!line) {
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
155 return 1;
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
156 }
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
157 WRITECONST(stream, out, "<pre>\n");
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
158
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
159 int lnw = lnint(in->count);
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
160 for (int i = 0 ; i < in->count ; i++) {
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
161 if (highlighter) {
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
162 highlighter->parser(in->lines[i], line, highlighter);
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
163 } else {
30
0bfd4d6f086a added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
164 char *c = in->lines[i];
0bfd4d6f086a added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
165 size_t dp = 0;
0bfd4d6f086a added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
166 while (*c) {
0bfd4d6f086a added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
167 dp = writeescapedchar(line, dp, *c);
0bfd4d6f086a added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
168 c++;
0bfd4d6f086a added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
169 }
0bfd4d6f086a added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
170 line[dp] = '\0';
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
171 }
30
0bfd4d6f086a added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
172
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
173 // write line number
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
174 if (showln) {
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
175 WRITECONST(stream, out, "<span class=\"c2html-lineno\">");
27
53fd8595378c added links for line numbers
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
176 char lnbuf[128];
53fd8595378c added links for line numbers
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
177 int len;
53fd8595378c added links for line numbers
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
178 // line number link
53fd8595378c added links for line numbers
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
179 len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">",
53fd8595378c added links for line numbers
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
180 i+1, i+1);
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
181 out(lnbuf, 1, len, stream);
27
53fd8595378c added links for line numbers
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
182 // justified line number
53fd8595378c added links for line numbers
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
183 len = snprintf(lnbuf, 128, "%*d ", lnw, i+1);
53fd8595378c added links for line numbers
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
184 out(lnbuf, 1, len, stream);
53fd8595378c added links for line numbers
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
185 WRITECONST(stream, out, "</a></span> ");
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
186 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
187
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
188 // write formated (or plain) code line
30
0bfd4d6f086a added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
189 out(line, 1, strlen(line), stream);
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
190 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
191
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
192 WRITECONST(stream, out, "</pre>\n");
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
193 free(line);
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
194 return 0;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
195 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
196
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
197 void init_c_highlighter(highlighter_t *highlighter) {
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
198 memset(highlighter, 0, sizeof(highlighter_t));
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
199 highlighter->isdirective = iscdirective;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
200 highlighter->istype = isctype;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
201 highlighter->keywords = ckeywords;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
202 highlighter->parser = cparseline;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
203 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
204
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
205 void init_java_highlighter(highlighter_t *highlighter) {
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
206 memset(highlighter, 0, sizeof(highlighter_t));
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
207 highlighter->isdirective = isjdirective;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
208 highlighter->istype = isjtype;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
209 highlighter->keywords = jkeywords;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
210 highlighter->parser = jparseline;
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
211 }
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
212
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
213 int main(int argc, char** argv) {
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
214 int retcode = EXIT_SUCCESS;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
215
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
216 settings_t settings;
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
217 memset(&settings, 0, sizeof(settings));
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
218 settings.highlight = 1;
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
219 settings.showlinenumbers = 1;
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
220
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
221 int lang = C2HTML_C;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
222
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
223 char optc;
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
224 while ((optc = getopt(argc, argv, "hljo:pH:F:")) != -1) {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
225 switch (optc) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
226 case 'o':
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
227 if (!(optarg[0] == '-' && optarg[1] == 0)) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
228 settings.outfilename = optarg;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
229 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
230 break;
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
231 case 'F':
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
232 settings.footerfile = optarg;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
233 break;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
234 case 'H':
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
235 settings.headerfile = optarg;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
236 break;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
237 case 'j':
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
238 lang = C2HTML_JAVA;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
239 break;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
240 case 'p':
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
241 settings.highlight = 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
242 break;
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
243 case 'l':
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
244 settings.showlinenumbers = 0;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
245 break;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
246 case 'h':
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
247 printhelp();
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
248 return 0;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
249 default:
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
250 return 1;
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
251 }
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
252 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
253
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
254 if (optind != argc-1) {
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
255 printhelp();
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
256 return 1;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
257 } else {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
258 settings.infilename = argv[optind];
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
259 FILE *fout;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
260 if (settings.outfilename) {
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
261 fout = fopen(settings.outfilename, "w");
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
262 if (!fout) {
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
263 perror("Error opening output file");
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
264 return -1;
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
265 }
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
266 } else {
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
267 fout = stdout;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
268 }
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
269
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
270 if (copyfile(settings.headerfile, fout)) {
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
271 perror("Error opening header file");
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
272 retcode = -1;
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
273 goto prog_end;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
274 }
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
275
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
276 highlighter_t highlighter;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
277 highlighter_t *hptr = &highlighter;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
278 switch (lang) {
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
279 case C2HTML_C:
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
280 init_c_highlighter(&highlighter);
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
281 break;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
282 case C2HTML_JAVA:
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
283 init_java_highlighter(&highlighter);
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
284 break;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
285 default:
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
286 hptr = NULL;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
287 break;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
288 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
289 if (!settings.highlight) {
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
290 hptr = NULL;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
291 }
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
292
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
293 inputfile_t *inputfile = readinput(settings.infilename);
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
294 if (inputfile) {
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
295 formatfile(
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
296 hptr,
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
297 inputfile,
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
298 (fmt_write_func)fwrite,
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
299 fout,
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
300 settings.showlinenumbers);
25
f82aa7afe872 more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
301 freeinputfilebuffer(inputfile);
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
302 } else {
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
303 perror("Error opening input file");
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
304 retcode = -1;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
305 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
306
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
307 if (copyfile(settings.footerfile, fout)) {
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
308 perror("Error opening footer file");
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
309 retcode = -1;
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
310 }
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
311
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
312 prog_end:
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
313 if (fout != stdout) {
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
314 fclose(fout);
12
7ce5c4b51959 option for plaintext
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
315 }
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
316
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
317 return retcode;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
318 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
319 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
320

mercurial