src/c2html.c

Thu, 23 Jan 2014 14:17:06 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Jan 2014 14:17:06 +0100
changeset 22
f463693b5eeb
parent 21
537aec525835
child 23
f44a185b678b
permissions
-rw-r--r--

added command line parameters for header and footer file

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

mercurial