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