/[dtapublic]/projs/dtats/trunk/projs/2018/20180112_ifsfscan/c_main.c
ViewVC logotype

Diff of /projs/dtats/trunk/projs/2018/20180112_ifsfscan/c_main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 164 by dashley, Sat Jul 7 20:34:59 2018 UTC revision 193 by dashley, Sat Jul 14 21:20:02 2018 UTC
# Line 1  Line 1 
1  //$Header$  //$Header$
2  //-------------------------------------------------------------------------------------------------  //-------------------------------------------------------------------------------------------------
3  //This file is part of "ifsfscan", a program for identifying and correcting gross formatting  //Copyright (c) 2018, David T. Ashley
4  //anomalies in source files.  //
5  //-------------------------------------------------------------------------------------------------  //This file is part of "ifsfscan", a program for identifying gross formatting anomalies in source
6  //This source code and any program in which it is compiled/used is provided under the MIT License,  //files (Windows/ASCII text files only).
7    //
8    //This source code and any program in which it is compiled/used is licensed under the MIT License,
9  //reproduced below.  //reproduced below.
10  //-------------------------------------------------------------------------------------------------  //
11  //Permission is hereby granted, free of charge, to any person obtaining a copy of  //Permission is hereby granted, free of charge, to any person obtaining a copy of
12  //this software and associated documentation files(the "Software"), to deal in the  //this software and associated documentation files(the "Software"), to deal in the
13  //Software without restriction, including without limitation the rights to use,  //Software without restriction, including without limitation the rights to use,
# Line 18  Line 20 
20  //  //
21  //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE  //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
24  //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# Line 26  Line 28 
28  //-------------------------------------------------------------------------------------------------  //-------------------------------------------------------------------------------------------------
29  #include <stdio.h>  #include <stdio.h>
30  #include <stdlib.h>  #include <stdlib.h>
31    #include <string.h>
32    #include <time.h>
33    
34    #define FCMIOF_HORIZONTAL_BAR_SEP_CHAR    ('-')
35    #define FCMIOF_LINE_LEN                   (78)
36    
37    //The last column allowed for a characters below is Column 82 (or it will be
38    //less than aesthetic).
39    const char * const license_preamble[] =
40    {
41       "ifsfscan, (c) 2018 David T. Ashley (dashley@gmail.com)",
42       "This program's source files, executable files, and all other related files",
43       "(such as Visual Studio project files) are licensed under \"The MIT License\",",
44       "reproduced below."
45    };
46    
47  static const char * const license_text[] =  const char * const license_text[] =
48  {  {
49          "Permission is hereby granted, free of charge, to any person obtaining a copy of",          "Permission is hereby granted, free of charge, to any person obtaining a copy",
50          "this software and associated documentation files(the \"Software\"), to deal in the",          "of this software and associated documentation files(the \"Software\"), to deal",
51          "Software without restriction, including without limitation the rights to use,",          "in the Software without restriction, including without limitation the rights",
52          "copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the",          "to use, copy, modify, merge, publish, distribute, sublicense, and / or sell",
53          "Software, and to permit persons to whom the Software is furnished to do so,"          "copies of the Software, and to permit persons to whom the Software is",
54          "subject to the following conditions :",          "furnished to do so, subject to the following conditions:",
55          "",          "",
56          "The above copyright notice and this permission notice shall be included in all",          "The above copyright notice and this permission notice shall be included in",
57          "copies or substantial portions of the Software.",          "all copies or substantial portions of the Software.",
58          "",          "",
59          "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR",          "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR",
60          "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,",          "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,",
61          "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE",          "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE",
62          "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER",          "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER",
63          "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,",          "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,",
64          "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE"          "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE",
65          "SOFTWARE."          "SOFTWARE."
66  };  };
67    
68  static const char * const prog_desc_text[] =  const char * const prog_desc_text[] =
69  {  {
70          "ifsfscan (mnemonic: Ill-Formed Source File SCAN) is a program for detecting and",          "ifsfscan (mnemonic: Ill-Formed Source File SCAN) is a program for detecting",
71          "correcting gross formatting errors in source files.  The errors detected and",          "gross formatting errors in source files (Windows/ASCII text files only).  The",
72          "corrected include non-ASCII characters, tab characters, trailing whitespace on",     "errors detected are non-ASCII characters, tab characters, abnormal",
73          "lines, and other errors.",     "end-of-line characters, and trailing whitespace on lines.",
74  };  };
75    
76  static const char * const prog_help_text[] =  const char * const prog_help_text[] =
77  {  {
78          "Usage:  ifsfscan <options> <files_no_wildcards>",          "Usage:  ifsfscan [filename_or_wildcard [filename_or_wildcard [...]]]",
         "",  
         "Options:",  
         "   -fixall",  
         "      Fixes all known formatting issues non-interactively.  For users who",  
         "      understand the program's behavior, this is the most common option used.",  
         "   -fixnone (default)",  
         "      Identifies formatting issues, but does make any corrections.",  
79          "",          "",
80          "Notes:",          "Notes:",
81          "   ( 1) : THIS PROGRAM CAN DESTROY DATA.  This program does not make backup",     "   (1) : Wildcards (\"*\", \"?\") are processed by Windows, so Windows is",
82          "          files.  If this program were used to make \"corrections\" on a binary",     "         the arbiter of which wildcards are accepted and how they expand.",
83          "          file, the file would likely be irreparably corrupted.",     "   (2) : This program never writes to a file, so it cannot destroy data",
84          "   ( 2) : This program does not process wildcards.",          "         (except, possibly, by stdout output redirected to a file).",
85          "   ( 3) : This program is limited by available memory.  This program parses a",          "   (3) : This program accepts no options (like \"-help\" or \"-verbose\").",
86          "          source file, writing the corrected file to a RAM buffer, then finally",          "   (4) : This program accepts only Windows line endings (13-10).",
87          "          overwrites the source file from the RAM buffer.  The practical limit",          "         This program is incompatible with *nix and *nix files.",
88          "          for the size of the file that can be corrected using this program is",     "   (5) : This program accepts only the ASCII character set (it will not",
89          "          probably a couple gigabytes.",     "         process UTF-8 or any other encodings).",
         "   ( 4) : The license of this program does not prohibit copying the source code",  
         "          to another version control repository or simply \"stealing\" the",  
         "          program and source code for any other use.  The MIT License is a very",  
         "          unrestrictive open-source license."  
90  };  };
91    
   
92  //--------------------------------------------------------------------------------  //--------------------------------------------------------------------------------
93  //  T E R M I N A T I O N    F U N C T I O N S  //  T E R M I N A T I O N    F U N C T I O N S
94  //--------------------------------------------------------------------------------  //--------------------------------------------------------------------------------
95  static void CCMFATAL_fatal(const char *desc, const char *file, size_t line)  void CCMFATAL_fatal(const char *desc, const char *file, size_t line)
96  {  {
97       printf("\n");
98          printf("Fatal error.  Must terminate execution.\n");          printf("Fatal error.  Must terminate execution.\n");
99          printf("File: %s, Line: %zu.\n", file, line);          printf("File: %s, Line: %zu.\n", file, line);
100          printf("Error description: %s\n", desc);          printf("Error description: %s\n", desc);
# Line 98  static void CCMFATAL_fatal(const char *d Line 104  static void CCMFATAL_fatal(const char *d
104  //--------------------------------------------------------------------------------  //--------------------------------------------------------------------------------
105  //  A S S E R T I O N    F U N C T I O N S  //  A S S E R T I O N    F U N C T I O N S
106  //--------------------------------------------------------------------------------  //--------------------------------------------------------------------------------
107    void USERASSERT_assert(int assertion, const char *file, size_t line)
108    {
109       if (! assertion)
110       {
111          printf("\n");
112          printf("Assertion failed.  It is necessary to use the source code to diagnose\n");
113          printf("and resolve this error.\n");
114          printf("File: %s, Line: %zu.\n", file, line);
115          exit(4);  //Error code 4 for error termination.
116       }
117    }
118    
119  //--------------------------------------------------------------------------------  //--------------------------------------------------------------------------------
120  //  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  //  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
# Line 118  void *CCMALLOC_malloc(size_t size) Line 135  void *CCMALLOC_malloc(size_t size)
135                          __LINE__);                          __LINE__);
136          }          }
137    
138       memset(rv, 0, size);
139    
140          return(rv);          return(rv);
141  }  }
142    
# Line 134  void *CCMALLOC_calloc(size_t num, size_t Line 153  void *CCMALLOC_calloc(size_t num, size_t
153                          __LINE__);                          __LINE__);
154          }          }
155    
156       memset(rv, 0, size);
157    
158          return(rv);          return(rv);
159  }  }
160    
# Line 159  void CCMALLOC_free(void *memblock) Line 180  void CCMALLOC_free(void *memblock)
180          free(memblock);          free(memblock);
181  }  }
182    
183  #if 0  //--------------------------------------------------------------------------------
184    //  C H A R A C T E R    F U N C T I O N S
185    //--------------------------------------------------------------------------------
186  int CHARFUNC_digit_to_val(char digit)  int CHARFUNC_digit_to_val(char digit)
187  {  {
188          switch (digit)          switch (digit)
# Line 190  int CHARFUNC_digit_to_val(char digit) Line 212  int CHARFUNC_digit_to_val(char digit)
212          }          }
213  }  }
214    
   
215  char CHARFUNC_nibble_to_lc_hex_digit(int nibble)  char CHARFUNC_nibble_to_lc_hex_digit(int nibble)
216  {  {
217          switch (nibble & 0x0F)          switch (nibble & 0x0F)
# Line 244  char CHARFUNC_nibble_to_lc_hex_digit(int Line 265  char CHARFUNC_nibble_to_lc_hex_digit(int
265                  return('f');                  return('f');
266                  break;                  break;
267          default:          default:
268                  assert(0);        USERASSERT_assert(0, __FILE__, __LINE__);
269                  return('?');                  return('?');
270                  break;                  break;
271          }          }
272  }  }
273    
   
274  void CHARFUNC_int_to_lc_hex_rev(int arg, char *s)  void CHARFUNC_int_to_lc_hex_rev(int arg, char *s)
275  {  {
276          int i;          int i;
277    
278          assert(s != NULL);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
279    
280          for (i = 0; i<8; i++)     for (i = 0; i<8; i++)
281          {          {
282                  s[i] = CHARFUNC_nibble_to_lc_hex_digit(arg);                  s[i] = CHARFUNC_nibble_to_lc_hex_digit(arg);
283                  arg >>= 4;                  arg >>= 4;
284          }          }
285  }  }
286    
287  #define FCMIOF_HORIZONTAL_BAR_SEP_CHAR    ('-')  //--------------------------------------------------------------------------------
288  #define FCMIOF_LINE_LEN                   (78)  //  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
289    //--------------------------------------------------------------------------------
290    int STRING_contains_wildcard(const char *s)
291    {
292       if (strchr(s, '?') != NULL)
293          return(1);
294       else if (strchr(s, '*') != NULL)
295          return(1);
296       else
297          return(0);
298    }
299    
300    int STRING_is_longer_than_maxpath(const char *s)
301    {
302       if (_MAX_PATH <= 5)
303          return(1);
304       else if (strlen(s) > (_MAX_PATH - 5))
305          return(1);
306       else
307          return(0);
308    }
309    
310    int STRING_contains_terminating_backslash(const char *s)
311    {
312       size_t i;
313    
314       i = strlen(s);
315    
316  //08/16/01:  Visual inspection OK.     if (i == 0)
317       {
318          return(0);
319       }
320       else
321       {
322          do
323          {
324             i--;
325             if (s[i] == '\\')
326                return(1);
327             else if ((s[i] != ' ') && (s[i] != '\t'))
328                return(0);
329          } while (i != 0);
330          return(0);
331       }
332    }
333    
334    const char *STRING_vcinfo(size_t which)
335    {
336       static const char * const vcinfo[] =
337       {
338          "$Header$",
339          "$Date$",
340          "$Revision$",
341          "$Author$",
342          "$HeadURL$",
343          "$Id"
344       };
345    
346    }
347    
348    //--------------------------------------------------------------------------------
349    //  F O R M A T T E D    O U T P U T    F U N C T I O N S
350    //--------------------------------------------------------------------------------
351  int FCMIOF_get_line_len(void)  int FCMIOF_get_line_len(void)
352  {  {
353          return(FCMIOF_LINE_LEN);          return(FCMIOF_LINE_LEN);
354  }  }
355    
   
 //08/16/01:  Visual inspection OK.  
356  void FCMIOF_stream_repchar(FILE *s, char c, unsigned n)  void FCMIOF_stream_repchar(FILE *s, char c, unsigned n)
357  {  {
358          assert(s != NULL);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
359    
360          while (n--)          while (n--)
361                  fprintf(s, "%c", c);                  fprintf(s, "%c", c);
362  }  }
363    
   
 //08/16/01:  Visual inspection OK.  
364  void FCMIOF_repchar(char c, unsigned n)  void FCMIOF_repchar(char c, unsigned n)
365  {  {
366          while (n--)          while (n--)
367                  printf("%c", c);                  printf("%c", c);
368  }  }
369    
   
 //08/16/01:  Visual inspection OK.  
370  void FCMIOF_hline(void)  void FCMIOF_hline(void)
371  {  {
372          FCMIOF_repchar(FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN);          FCMIOF_repchar(FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN);
373          printf("\n");          printf("\n");
374  }  }
375    
   
 //08/16/01:  Visual inspection OK.  
376  void FCMIOF_stream_hline(FILE *s)  void FCMIOF_stream_hline(FILE *s)
377  {  {
378          assert(s != NULL);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
379    
380          FCMIOF_stream_repchar(s, FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN);          FCMIOF_stream_repchar(s, FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN);
381          fprintf(s, "\n");          fprintf(s, "\n");
382  }  }
383    
   
 //08/16/01:  Visual inspection OK.  
384  void FCMIOF_stream_bannerheading(FILE *f,  void FCMIOF_stream_bannerheading(FILE *f,
385          char *s,          char *s,
386          int n_extra_lines)          int n_extra_lines)
# Line 330  void FCMIOF_stream_bannerheading(FILE *f Line 399  void FCMIOF_stream_bannerheading(FILE *f
399    
400          /* Check the file pointer, string pointer, and other par.          /* Check the file pointer, string pointer, and other par.
401          */          */
402          assert(f != NULL);     USERASSERT_assert(f != NULL, __FILE__, __LINE__);
403          assert(s != NULL);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
404          assert(n_extra_lines >= 0);     USERASSERT_assert(n_extra_lines >= 0, __FILE__, __LINE__);
405    
406          /* Print the right number of solid lines of asterisks to the          /* Print the right number of solid lines of asterisks to the
407          ** standard output.          ** standard output.
# Line 385  void FCMIOF_stream_bannerheading(FILE *f Line 454  void FCMIOF_stream_bannerheading(FILE *f
454          }          }
455  }  }
456    
   
 //08/16/01:  Visual inspection OK.  
457  void FCMIOF_bannerheading(char *s, int n_extra_lines)  void FCMIOF_bannerheading(char *s, int n_extra_lines)
458  {  {
459          assert(s != NULL);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
460          assert(n_extra_lines >= 0);     USERASSERT_assert(n_extra_lines >= 0, __FILE__, __LINE__);
461    
462          FCMIOF_stream_bannerheading(stdout, s, n_extra_lines);          FCMIOF_stream_bannerheading(stdout, s, n_extra_lines);
463  }  }
464    
   
465  void FCMIOF_time_stream(FILE *s, time_t ltime)  void FCMIOF_time_stream(FILE *s, time_t ltime)
466  {  {
467          char *p;          char *p;
468    
469          assert(s != NULL);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
470    
471          time(&ltime);          time(&ltime);
472    
# Line 425  void FCMIOF_time_stream(FILE *s, time_t Line 491  void FCMIOF_time_stream(FILE *s, time_t
491          }          }
492  }  }
493    
494  #endif  void process_filename_or_wildcard(const char *fname_or_wildcard)
495    {
496       //Incoming pointer is worthy of an assertion.  The OS should not every deliver
497       //a NULL pointer or empty string to us.
498       USERASSERT_assert(fname_or_wildcard != NULL,     __FILE__, __LINE__);
499       USERASSERT_assert(strlen(fname_or_wildcard) > 0, __FILE__, __LINE__);
500    
501    }
502    
503    void emit_no_par_documentation(void)
504    {
505       size_t i;
506    
507       FCMIOF_stream_hline(stdout);
508       for (i = 0; i < (sizeof(prog_desc_text) / sizeof(prog_desc_text[0])); i++)
509          printf("%s\n", prog_desc_text[i]);
510       FCMIOF_stream_hline(stdout);
511       for (i = 0; i < (sizeof(license_preamble) / sizeof(license_preamble[0])); i++)
512          printf("%s\n", license_preamble[i]);
513       FCMIOF_stream_hline(stdout);
514       for (i = 0; i < (sizeof(license_text) / sizeof(license_text[0])); i++)
515          printf("%s\n", license_text[i]);
516       FCMIOF_stream_hline(stdout);
517       for (i = 0; i < (sizeof(prog_help_text) / sizeof(prog_help_text[0])); i++)
518          printf("%s\n", prog_help_text[i]);
519       FCMIOF_stream_hline(stdout);
520    }
521    
522    void emit_execution_preamble(void)
523    {
524       FCMIOF_stream_hline(stdout);
525       printf("Use \"ifsfscan\" with no parameters to obtain license and help information.\n");
526       FCMIOF_stream_hline(stdout);
527    }
528    
529  int c_main(int argc, char **argv)  int c_main(int argc, char **argv)
530  {  {
531       int i;
532    
533     if (argc <= 1)     if (argc <= 1)
534     {     {
535        //This is most likely someone trying to figure out what the program is or does.        //This is  most likely someone trying to figure out what the program is or does.
536        //Treat this the same as a request for help.        //Spit everything.
537          emit_no_par_documentation();
538       }
539       else
540       {
541          emit_execution_preamble();
542    
543          //Every argument beyond the program name has to be either a file name or
544          //wildcard.  Just process them in order.
545          for (i = 1; i < argc; i++)
546             process_filename_or_wildcard(argv[i]);
547    
548          FCMIOF_stream_hline(stdout);
549     }     }
550    
551     return 0;     return 0;

Legend:
Removed from v.164  
changed lines
  Added in v.193

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25