/[dtapublic]/projs/emts/trunk/src/ets_toolcol/emts_ifsfscan/c_main.c
ViewVC logotype

Diff of /projs/emts/trunk/src/ets_toolcol/emts_ifsfscan/c_main.c

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

revision 201 by dashley, Sun Jul 15 03:07:20 2018 UTC revision 202 by dashley, Sun Jul 15 17:17:37 2018 UTC
# Line 34  Line 34 
34  #include <time.h>  #include <time.h>
35  #include <windows.h>  #include <windows.h>
36    
   
37  #define FCMIOF_HORIZONTAL_BAR_SEP_CHAR    ('-')  #define FCMIOF_HORIZONTAL_BAR_SEP_CHAR    ('-')
38  #define FCMIOF_LINE_LEN                   (78)  #define FCMIOF_LINE_LEN                   (78)
39    
# Line 50  const char * const license_preamble[] = Line 49  const char * const license_preamble[] =
49    
50  const char * const license_text[] =  const char * const license_text[] =
51  {  {
52          "Permission is hereby granted, free of charge, to any person obtaining a copy",     "Permission is hereby granted, free of charge, to any person obtaining a copy",
53          "of this software and associated documentation files(the \"Software\"), to deal",     "of this software and associated documentation files(the \"Software\"), to deal",
54          "in the Software without restriction, including without limitation the rights",     "in the Software without restriction, including without limitation the rights",
55          "to use, copy, modify, merge, publish, distribute, sublicense, and / or sell",     "to use, copy, modify, merge, publish, distribute, sublicense, and / or sell",
56          "copies of the Software, and to permit persons to whom the Software is",     "copies of the Software, and to permit persons to whom the Software is",
57          "furnished to do so, subject to the following conditions:",     "furnished to do so, subject to the following conditions:",
58          "",     "",
59          "The above copyright notice and this permission notice shall be included in",     "The above copyright notice and this permission notice shall be included in",
60          "all copies or substantial portions of the Software.",     "all copies or substantial portions of the Software.",
61          "",     "",
62          "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",
63          "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,",     "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,",
64          "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE",     "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE",
65          "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER",     "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER",
66          "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,",     "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,",
67          "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",
68          "SOFTWARE."     "SOFTWARE."
69  };  };
70    
71  const char * const prog_desc_text[] =  const char * const prog_desc_text[] =
72  {  {
73          "ifsfscan (mnemonic: Ill-Formed Source File SCAN) is a program for detecting",     "ifsfscan (mnemonic: Ill-Formed Source File SCAN) is a program for detecting",
74          "gross formatting errors in source files (Windows/ASCII text files only).  The",     "gross formatting errors in source files (Windows/ASCII text files only).  The",
75     "errors detected are non-ASCII characters, tab characters, abnormal",     "errors detected are non-ASCII characters, tab characters, abnormal",
76     "end-of-line characters, and trailing whitespace on lines.",     "end-of-line characters, and trailing whitespace on lines.",
77  };  };
78    
79  const char * const prog_help_text[] =  const char * const prog_help_text[] =
80  {  {
81          "Usage:  ifsfscan [filename_or_wildcard [filename_or_wildcard [...]]]",     "Usage:  ifsfscan [filename_or_wildcard [filename_or_wildcard [...]]]",
82          "",     "",
83          "Notes:",     "Notes:",
84     "   (1) : Wildcards (\"*\", \"?\") are processed by Windows, so Windows is",     "   (1) : Wildcards (\"*\", \"?\") are processed by Windows, so Windows is",
85     "         the arbiter of which wildcards are accepted and how they expand.",     "         the arbiter of which wildcards are accepted and how they expand.",
86     "   (2) : This program never writes to a file, so it cannot destroy data",     "   (2) : This program never writes to a file, so it cannot destroy data",
87          "         (except, possibly, by stdout output redirected to a file).",     "         (except, possibly, by stdout output redirected to a file).",
88          "   (3) : This program accepts no options (like \"-help\" or \"-verbose\").",     "   (3) : This program accepts no options (like \"-help\" or \"-verbose\").",
89          "   (4) : This program accepts only Windows line endings (13-10).",     "   (4) : This program accepts only Windows line endings (13-10).",
90          "         This program is incompatible with *nix and *nix files.",     "         This program is incompatible with *nix and *nix files.",
91     "   (5) : This program accepts only the ASCII character set (it will not",     "   (5) : This program accepts only the ASCII character set (it will not",
92     "         process UTF-8 or any other encodings).",     "         process UTF-8 or any other encodings).",
93  };  };
# Line 99  const char * const prog_help_text[] = Line 98  const char * const prog_help_text[] =
98  void CCMFATAL_fatal(const char *desc, const char *file, size_t line)  void CCMFATAL_fatal(const char *desc, const char *file, size_t line)
99  {  {
100     printf("\n");     printf("\n");
101          printf("Fatal error.  Must terminate execution.\n");     printf("Fatal error.  Must terminate execution.\n");
102          printf("File: %s, Line: %zu.\n", file, line);     printf("File: %s, Line: %zu.\n", file, line);
103          printf("Error description: %s\n", desc);     printf("Error description: %s\n", desc);
104          exit(4);  //Error code 4 for error termination.     exit(4);  //Error code 4 for error termination.
105  }  }
106    
107  //--------------------------------------------------------------------------------  //--------------------------------------------------------------------------------
# Line 128  void USERASSERT_assert(int assertion, co Line 127  void USERASSERT_assert(int assertion, co
127  //--------------------------------------------------------------------------------  //--------------------------------------------------------------------------------
128  void *CCMALLOC_malloc(size_t size)  void *CCMALLOC_malloc(size_t size)
129  {  {
130          void *rv;     void *rv;
131    
132          rv = malloc(size);     rv = malloc(size);
133    
134          if (!rv)     if (!rv)
135          {     {
136                  CCMFATAL_fatal("NULL pointer from malloc()--probable out of memory.",        CCMFATAL_fatal("NULL pointer from malloc()--probable out of memory.",
137                          __FILE__,           __FILE__,
138                          __LINE__);           __LINE__);
139          }     }
140    
141     memset(rv, 0, size);     memset(rv, 0, size);
142    
143          return(rv);     return(rv);
144  }  }
145    
146  void *CCMALLOC_calloc(size_t num, size_t size)  void *CCMALLOC_calloc(size_t num, size_t size)
147  {  {
148          void *rv;     void *rv;
149    
150          rv = calloc(num, size);     rv = calloc(num, size);
151    
152          if (!rv)     if (!rv)
153          {     {
154                  CCMFATAL_fatal("NULL pointer from calloc()--probable out of memory.",        CCMFATAL_fatal("NULL pointer from calloc()--probable out of memory.",
155                          __FILE__,           __FILE__,
156                          __LINE__);           __LINE__);
157          }     }
158    
159     memset(rv, 0, size);     memset(rv, 0, size);
160    
161          return(rv);     return(rv);
162  }  }
163    
164  void *CCMALLOC_realloc(void *memblock, size_t size)  void *CCMALLOC_realloc(void *memblock, size_t size)
165  {  {
166          void *rv;     void *rv;
167    
168          rv = realloc(memblock, size);     rv = realloc(memblock, size);
169    
170          if ((!rv) && (size))     if ((!rv) && (size))
171          {     {
172                  CCMFATAL_fatal("NULL pointer from realloc()--probable out of memory.",        CCMFATAL_fatal("NULL pointer from realloc()--probable out of memory.",
173                          __FILE__,           __FILE__,
174                          __LINE__);           __LINE__);
175          }     }
176    
177          return(rv);     return(rv);
178  }  }
179    
   
180  void CCMALLOC_free(void *memblock)  void CCMALLOC_free(void *memblock)
181  {  {
182          free(memblock);     free(memblock);
183  }  }
184    
185  //--------------------------------------------------------------------------------  //--------------------------------------------------------------------------------
# Line 189  void CCMALLOC_free(void *memblock) Line 187  void CCMALLOC_free(void *memblock)
187  //--------------------------------------------------------------------------------  //--------------------------------------------------------------------------------
188  int CHARFUNC_digit_to_val(char digit)  int CHARFUNC_digit_to_val(char digit)
189  {  {
190          switch (digit)     switch (digit)
191          {     {
192          case '0':  return(0);     case '0':  return(0);
193                  break;        break;
194          case '1':  return(1);     case '1':  return(1);
195                  break;        break;
196          case '2':  return(2);     case '2':  return(2);
197                  break;        break;
198          case '3':  return(3);     case '3':  return(3);
199                  break;        break;
200          case '4':  return(4);     case '4':  return(4);
201                  break;        break;
202          case '5':  return(5);     case '5':  return(5);
203                  break;        break;
204          case '6':  return(6);     case '6':  return(6);
205                  break;        break;
206          case '7':  return(7);     case '7':  return(7);
207                  break;        break;
208          case '8':  return(8);     case '8':  return(8);
209                  break;        break;
210          case '9':  return(9);     case '9':  return(9);
211                  break;        break;
212          default:   return(-1);     default:   return(-1);
213                  break;        break;
214          }     }
215  }  }
216    
217  char CHARFUNC_nibble_to_lc_hex_digit(int nibble)  char CHARFUNC_nibble_to_lc_hex_digit(int nibble)
218  {  {
219          switch (nibble & 0x0F)     switch (nibble & 0x0F)
220          {     {
221          case  0:     case  0:
222                  return('0');        return('0');
223                  break;        break;
224          case  1:     case  1:
225                  return('1');        return('1');
226                  break;        break;
227          case  2:     case  2:
228                  return('2');        return('2');
229                  break;        break;
230          case  3:     case  3:
231                  return('3');        return('3');
232                  break;        break;
233          case  4:     case  4:
234                  return('4');        return('4');
235                  break;        break;
236          case  5:     case  5:
237                  return('5');        return('5');
238                  break;        break;
239          case  6:     case  6:
240                  return('6');        return('6');
241                  break;        break;
242          case  7:     case  7:
243                  return('7');        return('7');
244                  break;        break;
245          case  8:     case  8:
246                  return('8');        return('8');
247                  break;        break;
248          case  9:     case  9:
249                  return('9');        return('9');
250                  break;        break;
251          case 10:     case 10:
252                  return('a');        return('a');
253                  break;        break;
254          case 11:     case 11:
255                  return('b');        return('b');
256                  break;        break;
257          case 12:     case 12:
258                  return('c');        return('c');
259                  break;        break;
260          case 13:     case 13:
261                  return('d');        return('d');
262                  break;        break;
263          case 14:     case 14:
264                  return('e');        return('e');
265                  break;        break;
266          case 15:     case 15:
267                  return('f');        return('f');
268                  break;        break;
269          default:     default:
270        USERASSERT_assert(0, __FILE__, __LINE__);        USERASSERT_assert(0, __FILE__, __LINE__);
271                  return('?');        return('?');
272                  break;        break;
273          }     }
274  }  }
275    
276  void CHARFUNC_int_to_lc_hex_rev(int arg, char *s)  void CHARFUNC_int_to_lc_hex_rev(int arg, char *s)
277  {  {
278          int i;     int i;
279    
280     USERASSERT_assert(s != NULL, __FILE__, __LINE__);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
281    
282     for (i = 0; i<8; i++)     for (i = 0; i<8; i++)
283          {     {
284                  s[i] = CHARFUNC_nibble_to_lc_hex_digit(arg);        s[i] = CHARFUNC_nibble_to_lc_hex_digit(arg);
285                  arg >>= 4;        arg >>= 4;
286          }     }
287  }  }
288    
289  //--------------------------------------------------------------------------------  //--------------------------------------------------------------------------------
# Line 359  const char *STRING_vcinfo(size_t which) Line 357  const char *STRING_vcinfo(size_t which)
357  //--------------------------------------------------------------------------------  //--------------------------------------------------------------------------------
358  int FCMIOF_get_line_len(void)  int FCMIOF_get_line_len(void)
359  {  {
360          return(FCMIOF_LINE_LEN);     return(FCMIOF_LINE_LEN);
361  }  }
362    
363  void FCMIOF_stream_repchar(FILE *s, char c, unsigned n)  void FCMIOF_stream_repchar(FILE *s, char c, unsigned n)
364  {  {
365     USERASSERT_assert(s != NULL, __FILE__, __LINE__);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
366    
367          while (n--)     while (n--)
368                  fprintf(s, "%c", c);        fprintf(s, "%c", c);
369  }  }
370    
371  void FCMIOF_repchar(char c, unsigned n)  void FCMIOF_repchar(char c, unsigned n)
372  {  {
373          while (n--)     while (n--)
374                  printf("%c", c);        printf("%c", c);
375  }  }
376    
377  void FCMIOF_hline(void)  void FCMIOF_hline(void)
378  {  {
379          FCMIOF_repchar(FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN);     FCMIOF_repchar(FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN);
380          printf("\n");     printf("\n");
381  }  }
382    
383  void FCMIOF_stream_hline(FILE *s)  void FCMIOF_stream_hline(FILE *s)
384  {  {
385     USERASSERT_assert(s != NULL, __FILE__, __LINE__);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
386    
387          FCMIOF_stream_repchar(s, FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN);     FCMIOF_stream_repchar(s, FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN);
388          fprintf(s, "\n");     fprintf(s, "\n");
389  }  }
390    
391  void FCMIOF_stream_bannerheading(FILE *f,  void FCMIOF_stream_bannerheading(FILE *f,
392          char *s,     char *s,
393          int n_extra_lines)     int n_extra_lines)
394  {  {
395          const int lr_padding = 3;     const int lr_padding = 3;
396          /* The number of spaces on each side of what is printed.     /* The number of spaces on each side of what is printed.
397          */     */
398          int i;     int i;
399          /* General iteration variable.     /* General iteration variable.
400          */     */
401    
402          int n_asterisks;     int n_asterisks;
403          int input_arg_len;     int input_arg_len;
404          int n_left_spaces;     int n_left_spaces;
405          int n_right_spaces;     int n_right_spaces;
406    
407          /* Check the file pointer, string pointer, and other par.     /* Check the file pointer, string pointer, and other par.
408          */     */
409     USERASSERT_assert(f != NULL, __FILE__, __LINE__);     USERASSERT_assert(f != NULL, __FILE__, __LINE__);
410     USERASSERT_assert(s != NULL, __FILE__, __LINE__);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
411     USERASSERT_assert(n_extra_lines >= 0, __FILE__, __LINE__);     USERASSERT_assert(n_extra_lines >= 0, __FILE__, __LINE__);
412    
413          /* Print the right number of solid lines of asterisks to the     /* Print the right number of solid lines of asterisks to the
414          ** standard output.     ** standard output.
415          */     */
416          for (i = 0; i<n_extra_lines; i++)     for (i = 0; i<n_extra_lines; i++)
417          {     {
418                  FCMIOF_stream_repchar(f, '*', FCMIOF_LINE_LEN);        FCMIOF_stream_repchar(f, '*', FCMIOF_LINE_LEN);
419                  fprintf(f, "\n");        fprintf(f, "\n");
420          }     }
421    
422          /* Figure out how many asterisks to print on each side of the     /* Figure out how many asterisks to print on each side of the
423          ** argument, and how many spaces.  We also need to figure out     ** argument, and how many spaces.  We also need to figure out
424          ** how many characters of the input argument to print--if there     ** how many characters of the input argument to print--if there
425          ** are too many characters, we need to truncate.     ** are too many characters, we need to truncate.
426          */     */
427          input_arg_len = strlen(s);     input_arg_len = strlen(s);
428          if (input_arg_len > (FCMIOF_LINE_LEN - 2 * lr_padding - 2))     if (input_arg_len > (FCMIOF_LINE_LEN - 2 * lr_padding - 2))
429                  input_arg_len = FCMIOF_LINE_LEN - 2 * lr_padding - 2;        input_arg_len = FCMIOF_LINE_LEN - 2 * lr_padding - 2;
430    
431          n_asterisks = (FCMIOF_LINE_LEN - 2 * lr_padding - input_arg_len) / 2;     n_asterisks = (FCMIOF_LINE_LEN - 2 * lr_padding - input_arg_len) / 2;
432    
433          n_left_spaces = lr_padding;     n_left_spaces = lr_padding;
434    
435          if ((FCMIOF_LINE_LEN - 2 * lr_padding - input_arg_len) % 2)     if ((FCMIOF_LINE_LEN - 2 * lr_padding - input_arg_len) % 2)
436          {     {
437                  /* Odd, need to pad the right by one. */        /* Odd, need to pad the right by one. */
438                  n_right_spaces = lr_padding + 1;        n_right_spaces = lr_padding + 1;
439          }     }
440          else     else
441          {     {
442                  n_right_spaces = lr_padding;        n_right_spaces = lr_padding;
443          }     }
444    
445          /* Print the text. */     /* Print the text. */
446          FCMIOF_stream_repchar(f, '*', n_asterisks);     FCMIOF_stream_repchar(f, '*', n_asterisks);
447          FCMIOF_stream_repchar(f, ' ', n_left_spaces);     FCMIOF_stream_repchar(f, ' ', n_left_spaces);
448          for (i = 0; i<input_arg_len; i++)     for (i = 0; i<input_arg_len; i++)
449                  fprintf(f, "%c", s[i]);        fprintf(f, "%c", s[i]);
450          FCMIOF_stream_repchar(f, ' ', n_right_spaces);     FCMIOF_stream_repchar(f, ' ', n_right_spaces);
451          FCMIOF_stream_repchar(f, '*', n_asterisks);     FCMIOF_stream_repchar(f, '*', n_asterisks);
452          fprintf(f, "\n");     fprintf(f, "\n");
453    
454          /* Print the right number of solid lines of asterisks to the     /* Print the right number of solid lines of asterisks to the
455          ** standard output.     ** standard output.
456          */     */
457          for (i = 0; i<n_extra_lines; i++)     for (i = 0; i<n_extra_lines; i++)
458          {     {
459                  FCMIOF_stream_repchar(f, '*', FCMIOF_LINE_LEN);        FCMIOF_stream_repchar(f, '*', FCMIOF_LINE_LEN);
460                  fprintf(f, "\n");        fprintf(f, "\n");
461          }     }
462  }  }
463    
464  void FCMIOF_bannerheading(char *s, int n_extra_lines)  void FCMIOF_bannerheading(char *s, int n_extra_lines)
# Line 468  void FCMIOF_bannerheading(char *s, int n Line 466  void FCMIOF_bannerheading(char *s, int n
466     USERASSERT_assert(s != NULL, __FILE__, __LINE__);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
467     USERASSERT_assert(n_extra_lines >= 0, __FILE__, __LINE__);     USERASSERT_assert(n_extra_lines >= 0, __FILE__, __LINE__);
468    
469          FCMIOF_stream_bannerheading(stdout, s, n_extra_lines);     FCMIOF_stream_bannerheading(stdout, s, n_extra_lines);
470  }  }
471    
472  void FCMIOF_time_stream(FILE *s, time_t ltime)  void FCMIOF_time_stream(FILE *s, time_t ltime)
473  {  {
474          char *p;     char *p;
475    
476     USERASSERT_assert(s != NULL, __FILE__, __LINE__);     USERASSERT_assert(s != NULL, __FILE__, __LINE__);
477    
478          time(&ltime);     time(&ltime);
479    
480          p = ctime(&ltime);     p = ctime(&ltime);
481    
482          if (p)     if (p)
483          {     {
484                  int i;        int i;
485    
486                  for (i = 11; i<19; i++)        for (i = 11; i<19; i++)
487                          fprintf(s, "%c", p[i]);           fprintf(s, "%c", p[i]);
488                  fprintf(s, " ");        fprintf(s, " ");
489                  for (i = 0; i<10; i++)        for (i = 0; i<10; i++)
490                          fprintf(s, "%c", p[i]);           fprintf(s, "%c", p[i]);
491                  fprintf(s, " ");        fprintf(s, " ");
492                  for (i = 20; i<24; i++)        for (i = 20; i<24; i++)
493                          fprintf(s, "%c", p[i]);           fprintf(s, "%c", p[i]);
494          }     }
495          else     else
496          {     {
497                  fprintf(s, "??? ??? ?? ??:??:?? ????");        fprintf(s, "??? ??? ?? ??:??:?? ????");
498          }     }
499  }  }
500    
501  int is_legal_non_eol_character(char c)  int is_legal_non_eol_character(char c)
# Line 577  void process_opened_handle(FILE *f) Line 575  void process_opened_handle(FILE *f)
575     char in_c, prev_c;     char in_c, prev_c;
576     unsigned char in_uc;     unsigned char in_uc;
577     int in_i;     int in_i;
578      
579     in_i = fgetc(f);     in_i = fgetc(f);
580    
581     if (in_i == EOF)     if (in_i == EOF)
# Line 868  int c_main(int argc, char **argv) Line 866  int c_main(int argc, char **argv)
866     }     }
867    
868     return 0;     return 0;
 }  
869    }

Legend:
Removed from v.201  
changed lines
  Added in v.202

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25