Tue, 23 Aug 2016 14:31:02 +0200
cleans up main function
src/c2html.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/c2html.c Tue Aug 23 14:24:57 2016 +0200 1.2 +++ b/src/c2html.c Tue Aug 23 14:31:02 2016 +0200 1.3 @@ -137,14 +137,14 @@ 1.4 }; 1.5 1.6 int main(int argc, char** argv) { 1.7 - int retcode = EXIT_SUCCESS; 1.8 - 1.9 + 1.10 + /* Default settings */ 1.11 Settings settings; 1.12 memset(&settings, 0, sizeof(settings)); 1.13 settings.showlinenumbers = 1; 1.14 - 1.15 enum source_type sourcetype = SOURCE_C; 1.16 1.17 + /* Parse command line */ 1.18 char optc; 1.19 while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { 1.20 switch (optc) { 1.21 @@ -186,26 +186,9 @@ 1.22 1.23 if (optind != argc-1) { 1.24 printhelp(); 1.25 - return 1; 1.26 + return EXIT_FAILURE; 1.27 } else { 1.28 - settings.infilename = argv[optind]; 1.29 - FILE *fout; 1.30 - if (settings.outfilename) { 1.31 - fout = fopen(settings.outfilename, "w"); 1.32 - if (!fout) { 1.33 - perror("Error opening output file"); 1.34 - return EXIT_FAILURE; 1.35 - } 1.36 - } else { 1.37 - fout = stdout; 1.38 - } 1.39 - 1.40 - char *filebuf = malloc(FILEBUF_SIZE); 1.41 - if (!filebuf) { 1.42 - perror("Error allocating file buffer"); 1.43 - return EXIT_FAILURE; 1.44 - } 1.45 - 1.46 + /* Configure highlighter */ 1.47 highlighter_t highlighter; 1.48 highlighter_t *hptr = &highlighter; 1.49 switch (sourcetype) { 1.50 @@ -220,16 +203,38 @@ 1.51 break; 1.52 default: /* should be unreachable */ 1.53 fprintf(stderr, "error in enum source_type\n"); 1.54 - retcode = EXIT_FAILURE; 1.55 - goto prog_end; 1.56 + return EXIT_FAILURE; 1.57 } 1.58 1.59 + /* Open output file */ 1.60 + settings.infilename = argv[optind]; 1.61 + FILE *fout; 1.62 + if (settings.outfilename) { 1.63 + fout = fopen(settings.outfilename, "w"); 1.64 + if (!fout) { 1.65 + perror("Error opening output file"); 1.66 + return EXIT_FAILURE; 1.67 + } 1.68 + } else { 1.69 + fout = stdout; 1.70 + } 1.71 + 1.72 + /* Allocate file buffer */ 1.73 + char *filebuf = malloc(FILEBUF_SIZE); 1.74 + if (!filebuf) { 1.75 + perror("Error allocating file buffer"); 1.76 + return EXIT_FAILURE; 1.77 + } 1.78 + 1.79 + /* Prepend header file */ 1.80 { 1.81 FILE *headerfile = fopen(settings.headerfile, "r"); 1.82 if (!headerfile) { 1.83 perror("Error opening header file"); 1.84 - retcode = EXIT_FAILURE; 1.85 - goto prog_end; 1.86 + if (fout != stdout) { 1.87 + fclose(fout); 1.88 + } 1.89 + return EXIT_FAILURE; 1.90 } 1.91 ucx_stream_copy(headerfile, fout, 1.92 (read_func) fread, (write_func) fwrite, 1.93 @@ -237,6 +242,7 @@ 1.94 fclose(headerfile); 1.95 } 1.96 1.97 + /* Process input file */ 1.98 FILE *inputfile = fopen(settings.infilename, "r"); 1.99 if (inputfile) { 1.100 UcxBuffer *content = ucx_buffer_new(NULL, 1.101 @@ -267,30 +273,32 @@ 1.102 ucx_buffer_free(content); 1.103 } else { 1.104 perror("Error opening input file"); 1.105 - retcode = EXIT_FAILURE; 1.106 + if (fout != stdout) { 1.107 + fclose(fout); 1.108 + } 1.109 + return EXIT_FAILURE; 1.110 } 1.111 1.112 + /* Append footer file */ 1.113 { 1.114 FILE *footerfile = fopen(settings.footerfile, "r"); 1.115 if (!footerfile) { 1.116 perror("Error opening footer file"); 1.117 - retcode = EXIT_FAILURE; 1.118 - goto prog_end; 1.119 + if (fout != stdout) { 1.120 + fclose(fout); 1.121 + } 1.122 + return EXIT_FAILURE; 1.123 } 1.124 ucx_stream_copy(footerfile, fout, 1.125 (read_func) fread, (write_func) fwrite, 1.126 filebuf, FILEBUF_SIZE, (size_t)-1); 1.127 fclose(footerfile); 1.128 } 1.129 + 1.130 1.131 free(filebuf); 1.132 - 1.133 - prog_end: 1.134 - if (fout != stdout) { 1.135 - fclose(fout); 1.136 - } 1.137 1.138 - return retcode; 1.139 + return EXIT_SUCCESS; 1.140 } 1.141 } 1.142