//$Header$ //------------------------------------------------------------------------------------------------- //Copyright (c) 2018, David T. Ashley // //This file is part of "ifsfscan", a program for identifying gross formatting anomalies in source //files (Windows/ASCII text files only). // //This source code and any program in which it is compiled/used is licensed under the MIT License, //reproduced below. // //Permission is hereby granted, free of charge, to any person obtaining a copy of //this software and associated documentation files(the "Software"), to deal in the //Software without restriction, including without limitation the rights to use, //copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the //Software, and to permit persons to whom the Software is furnished to do so, //subject to the following conditions : // //The above copyright notice and this permission notice shall be included in all //copies or substantial portions of the Software. // //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE //SOFTWARE. //------------------------------------------------------------------------------------------------- #include #include #include #include #define FCMIOF_HORIZONTAL_BAR_SEP_CHAR ('-') #define FCMIOF_LINE_LEN (78) //The last column allowed for a characters below is Column 82 (or it will be //less than aesthetic). const char * const license_preamble[] = { "ifsfscan, (c) 2018 David T. Ashley (dashley@gmail.com)", "This program's source files, executable files, and all other related files", "(such as Visual Studio project files) are licensed under \"The MIT License\",", "reproduced below." }; const char * const license_text[] = { "Permission is hereby granted, free of charge, to any person obtaining a copy", "of this software and associated documentation files(the \"Software\"), to deal", "in the Software without restriction, including without limitation the rights", "to use, copy, modify, merge, publish, distribute, sublicense, and / or sell", "copies of the Software, and to permit persons to whom the Software is", "furnished to do so, subject to the following conditions:", "", "The above copyright notice and this permission notice shall be included in", "all copies or substantial portions of the Software.", "", "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR", "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,", "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE", "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER", "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,", "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE", "SOFTWARE." }; const char * const prog_desc_text[] = { "ifsfscan (mnemonic: Ill-Formed Source File SCAN) is a program for detecting", "gross formatting errors in source files (Windows/ASCII text files only). The", "errors detected are non-ASCII characters, tab characters, abnormal", "end-of-line characters, and trailing whitespace on lines.", }; const char * const prog_help_text[] = { "Usage: ifsfscan [filename_or_wildcard [filename_or_wildcard [...]]]", "", "Notes:", " (1) : Wildcards (\"*\", \"?\") are processed by Windows, so Windows is", " the arbiter of which wildcards are accepted and how they expand.", " (2) : This program never writes to a file, so it cannot destroy data", " (except, possibly, by stdout output redirected to a file).", " (3) : This program accepts no options (like \"-help\" or \"-verbose\").", " (4) : This program accepts only Windows line endings (13-10).", " This program is incompatible with *nix and *nix files.", " (5) : This program accepts only the ASCII character set (it will not", " process UTF-8 or any other encodings).", }; //-------------------------------------------------------------------------------- // T E R M I N A T I O N F U N C T I O N S //-------------------------------------------------------------------------------- void CCMFATAL_fatal(const char *desc, const char *file, size_t line) { printf("\n"); printf("Fatal error. Must terminate execution.\n"); printf("File: %s, Line: %zu.\n", file, line); printf("Error description: %s\n", desc); exit(4); //Error code 4 for error termination. } //-------------------------------------------------------------------------------- // A S S E R T I O N F U N C T I O N S //-------------------------------------------------------------------------------- void USERASSERT_assert(int assertion, const char *file, size_t line) { if (! assertion) { printf("\n"); printf("Assertion failed. It is necessary to use the source code to diagnose\n"); printf("and resolve this error.\n"); printf("File: %s, Line: %zu.\n", file, line); exit(4); //Error code 4 for error termination. } } //-------------------------------------------------------------------------------- // M E M O R Y A L L O C A T I O N F U N C T I O N S //-------------------------------------------------------------------------------- //These functions form a layer over the standard library so that conditions of //concern can be more easily trapped. //-------------------------------------------------------------------------------- void *CCMALLOC_malloc(size_t size) { void *rv; rv = malloc(size); if (!rv) { CCMFATAL_fatal("NULL pointer from malloc()--probable out of memory.", __FILE__, __LINE__); } memset(rv, 0, size); return(rv); } void *CCMALLOC_calloc(size_t num, size_t size) { void *rv; rv = calloc(num, size); if (!rv) { CCMFATAL_fatal("NULL pointer from calloc()--probable out of memory.", __FILE__, __LINE__); } memset(rv, 0, size); return(rv); } void *CCMALLOC_realloc(void *memblock, size_t size) { void *rv; rv = realloc(memblock, size); if ((!rv) && (size)) { CCMFATAL_fatal("NULL pointer from realloc()--probable out of memory.", __FILE__, __LINE__); } return(rv); } void CCMALLOC_free(void *memblock) { free(memblock); } //-------------------------------------------------------------------------------- // C H A R A C T E R F U N C T I O N S //-------------------------------------------------------------------------------- int CHARFUNC_digit_to_val(char digit) { switch (digit) { case '0': return(0); break; case '1': return(1); break; case '2': return(2); break; case '3': return(3); break; case '4': return(4); break; case '5': return(5); break; case '6': return(6); break; case '7': return(7); break; case '8': return(8); break; case '9': return(9); break; default: return(-1); break; } } char CHARFUNC_nibble_to_lc_hex_digit(int nibble) { switch (nibble & 0x0F) { case 0: return('0'); break; case 1: return('1'); break; case 2: return('2'); break; case 3: return('3'); break; case 4: return('4'); break; case 5: return('5'); break; case 6: return('6'); break; case 7: return('7'); break; case 8: return('8'); break; case 9: return('9'); break; case 10: return('a'); break; case 11: return('b'); break; case 12: return('c'); break; case 13: return('d'); break; case 14: return('e'); break; case 15: return('f'); break; default: USERASSERT_assert(0, __FILE__, __LINE__); return('?'); break; } } void CHARFUNC_int_to_lc_hex_rev(int arg, char *s) { int i; USERASSERT_assert(s != NULL, __FILE__, __LINE__); for (i = 0; i<8; i++) { s[i] = CHARFUNC_nibble_to_lc_hex_digit(arg); arg >>= 4; } } //-------------------------------------------------------------------------------- // S T R I N G A N D C H A R A C T E R A R R A Y F U N C T I O N S //-------------------------------------------------------------------------------- int STRING_contains_wildcard(const char *s) { if (strchr(s, '?') != NULL) return(1); else if (strchr(s, '*') != NULL) return(1); else return(0); } int STRING_is_longer_than_maxpath(const char *s) { if (_MAX_PATH <= 5) return(1); else if (strlen(s) > (_MAX_PATH - 5)) return(1); else return(0); } int STRING_contains_terminating_backslash(const char *s) { size_t i; i = strlen(s); if (i == 0) { return(0); } else { do { i--; if (s[i] == '\\') return(1); else if ((s[i] != ' ') && (s[i] != '\t')) return(0); } while (i != 0); return(0); } } const char *STRING_vcinfo(size_t which) { static const char * const vcinfo[] = { "$Author$", "$Date$", "$Header$", "$HeadURL$", "$Id$", "$Revision$", "$URL$", }; } //-------------------------------------------------------------------------------- // F O R M A T T E D O U T P U T F U N C T I O N S //-------------------------------------------------------------------------------- int FCMIOF_get_line_len(void) { return(FCMIOF_LINE_LEN); } void FCMIOF_stream_repchar(FILE *s, char c, unsigned n) { USERASSERT_assert(s != NULL, __FILE__, __LINE__); while (n--) fprintf(s, "%c", c); } void FCMIOF_repchar(char c, unsigned n) { while (n--) printf("%c", c); } void FCMIOF_hline(void) { FCMIOF_repchar(FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN); printf("\n"); } void FCMIOF_stream_hline(FILE *s) { USERASSERT_assert(s != NULL, __FILE__, __LINE__); FCMIOF_stream_repchar(s, FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN); fprintf(s, "\n"); } void FCMIOF_stream_bannerheading(FILE *f, char *s, int n_extra_lines) { const int lr_padding = 3; /* The number of spaces on each side of what is printed. */ int i; /* General iteration variable. */ int n_asterisks; int input_arg_len; int n_left_spaces; int n_right_spaces; /* Check the file pointer, string pointer, and other par. */ USERASSERT_assert(f != NULL, __FILE__, __LINE__); USERASSERT_assert(s != NULL, __FILE__, __LINE__); USERASSERT_assert(n_extra_lines >= 0, __FILE__, __LINE__); /* Print the right number of solid lines of asterisks to the ** standard output. */ for (i = 0; i (FCMIOF_LINE_LEN - 2 * lr_padding - 2)) input_arg_len = FCMIOF_LINE_LEN - 2 * lr_padding - 2; n_asterisks = (FCMIOF_LINE_LEN - 2 * lr_padding - input_arg_len) / 2; n_left_spaces = lr_padding; if ((FCMIOF_LINE_LEN - 2 * lr_padding - input_arg_len) % 2) { /* Odd, need to pad the right by one. */ n_right_spaces = lr_padding + 1; } else { n_right_spaces = lr_padding; } /* Print the text. */ FCMIOF_stream_repchar(f, '*', n_asterisks); FCMIOF_stream_repchar(f, ' ', n_left_spaces); for (i = 0; i= 0, __FILE__, __LINE__); FCMIOF_stream_bannerheading(stdout, s, n_extra_lines); } void FCMIOF_time_stream(FILE *s, time_t ltime) { char *p; USERASSERT_assert(s != NULL, __FILE__, __LINE__); time(<ime); p = ctime(<ime); if (p) { int i; for (i = 11; i<19; i++) fprintf(s, "%c", p[i]); fprintf(s, " "); for (i = 0; i<10; i++) fprintf(s, "%c", p[i]); fprintf(s, " "); for (i = 20; i<24; i++) fprintf(s, "%c", p[i]); } else { fprintf(s, "??? ??? ?? ??:??:?? ????"); } } void process_filename_or_wildcard(const char *fname_or_wildcard) { //Incoming pointer is worthy of an assertion. The OS should not every deliver //a NULL pointer or empty string to us. USERASSERT_assert(fname_or_wildcard != NULL, __FILE__, __LINE__); USERASSERT_assert(strlen(fname_or_wildcard) > 0, __FILE__, __LINE__); } void emit_no_par_documentation(void) { size_t i; FCMIOF_stream_hline(stdout); for (i = 0; i < (sizeof(prog_desc_text) / sizeof(prog_desc_text[0])); i++) printf("%s\n", prog_desc_text[i]); FCMIOF_stream_hline(stdout); for (i = 0; i < (sizeof(license_preamble) / sizeof(license_preamble[0])); i++) printf("%s\n", license_preamble[i]); FCMIOF_stream_hline(stdout); for (i = 0; i < (sizeof(license_text) / sizeof(license_text[0])); i++) printf("%s\n", license_text[i]); FCMIOF_stream_hline(stdout); for (i = 0; i < (sizeof(prog_help_text) / sizeof(prog_help_text[0])); i++) printf("%s\n", prog_help_text[i]); FCMIOF_stream_hline(stdout); } void emit_execution_preamble(void) { FCMIOF_stream_hline(stdout); printf("Use \"ifsfscan\" with no parameters to obtain license and help information.\n"); FCMIOF_stream_hline(stdout); } int c_main(int argc, char **argv) { int i; if (argc <= 1) { //This is most likely someone trying to figure out what the program is or does. //Spit everything. emit_no_par_documentation(); } else { emit_execution_preamble(); //Every argument beyond the program name has to be either a file name or //wildcard. Just process them in order. for (i = 1; i < argc; i++) process_filename_or_wildcard(argv[i]); FCMIOF_stream_hline(stdout); } return 0; }