src/c2html.c

changeset 11
c59fe73459fd
parent 6
d10f7570add4
child 12
7ce5c4b51959
equal deleted inserted replaced
6:d10f7570add4 11:c59fe73459fd
42 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", 42 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
43 "long", "register", "return", "short", "signed", "sizeof", "static", "struct", 43 "long", "register", "return", "short", "signed", "sizeof", "static", "struct",
44 "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL 44 "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL
45 }; 45 };
46 46
47 typedef struct {
48 char* outfilename;
49 char* infilename;
50 } settings_t;
47 51
48 typedef struct { 52 typedef struct {
49 size_t count; 53 size_t count;
50 size_t capacity; 54 size_t capacity;
51 size_t maxlinewidth; 55 size_t maxlinewidth;
202 dest[dp] = 0; 206 dest[dp] = 0;
203 } 207 }
204 208
205 void printhelp() { 209 void printhelp() {
206 printf("Formats source code using HTML.\n\nUsage:\n" 210 printf("Formats source code using HTML.\n\nUsage:\n"
207 " c2html [FILE...]" 211 " c2html [Options] FILE\n\n"
212 " Options:\n"
213 " -h Prints this help message\n"
214 " -o <output> Output file (if not specified, stdout is used)\n"
208 "\n"); 215 "\n");
209 216
210 217
211 } 218 }
212 219
216 return w; 223 return w;
217 } 224 }
218 225
219 int main(int argc, char** argv) { 226 int main(int argc, char** argv) {
220 227
221 if (argc == 1) { 228 settings_t settings;
229 settings.outfilename = NULL;
230
231 char optc;
232 while ((optc = getopt(argc, argv, "ho:")) != -1) {
233 switch (optc) {
234 case 'o':
235 if (!(optarg[0] == '-' && optarg[1] == 0)) {
236 settings.outfilename = optarg;
237 }
238 break;
239 case 'h':
240 printhelp();
241 return 0;
242 default:
243 return 1;
244 }
245 }
246
247 if (optind != argc-1) {
222 printhelp(); 248 printhelp();
223 return 0; 249 return 1;
224 } else { 250 } else {
251 settings.infilename = argv[optind];
225 252
226 inputfile_t *inputfile = readinput(argv[1]); 253 inputfile_t *inputfile = readinput(settings.infilename);
227 if (inputfile) { 254 if (inputfile) {
228 printf("<pre>\n"); 255 FILE *fout;
256 if (settings.outfilename) {
257 fout = fopen(settings.outfilename, "w");
258 } else {
259 fout = stdout;
260 }
261 fprintf(fout, "<pre>\n");
229 char *line = (char*) malloc(inputfile->maxlinewidth*64); 262 char *line = (char*) malloc(inputfile->maxlinewidth*64);
230 int lnw = lnint(inputfile->count); 263 int lnw = lnint(inputfile->count);
231 for (int i = 0 ; i < inputfile->count ; i++) { 264 for (int i = 0 ; i < inputfile->count ; i++) {
232 parseline(inputfile->lines[i], line); 265 parseline(inputfile->lines[i], line);
233 printf("<span class=\"c2html-lineno\">%*d:</span> %s", 266 fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
234 lnw, i, line); 267 lnw, i, line);
235 } 268 }
236 free(line); 269 free(line);
237 printf("</pre>\n"); 270 fprintf(fout, "</pre>\n");
271
272 if (fout != stdout) {
273 fclose(fout);
274 }
275
238 freeinputfilebuffer(inputfile); 276 freeinputfilebuffer(inputfile);
239 } 277 }
240 278
241 return 0; 279 return 0;
242 } 280 }

mercurial