1 |
//$Header$ |
2 |
//------------------------------------------------------------------------------------------------- |
3 |
//This file is part of "ifsfscan", a program for identifying and correcting gross formatting |
4 |
//anomalies in source files. |
5 |
//------------------------------------------------------------------------------------------------- |
6 |
//This source code and any program in which it is compiled/used is provided under the MIT License, |
7 |
//reproduced below. |
8 |
//------------------------------------------------------------------------------------------------- |
9 |
//Permission is hereby granted, free of charge, to any person obtaining a copy of |
10 |
//this software and associated documentation files(the "Software"), to deal in the |
11 |
//Software without restriction, including without limitation the rights to use, |
12 |
//copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the |
13 |
//Software, and to permit persons to whom the Software is furnished to do so, |
14 |
//subject to the following conditions : |
15 |
// |
16 |
//The above copyright notice and this permission notice shall be included in all |
17 |
//copies or substantial portions of the Software. |
18 |
// |
19 |
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 |
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 |
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
22 |
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 |
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 |
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25 |
//SOFTWARE. |
26 |
//------------------------------------------------------------------------------------------------- |
27 |
#include <stdio.h> |
28 |
#include <stdlib.h> |
29 |
|
30 |
static const char * const license_text[] = |
31 |
{ |
32 |
"Permission is hereby granted, free of charge, to any person obtaining a copy of", |
33 |
"this software and associated documentation files(the \"Software\"), to deal in the", |
34 |
"Software without restriction, including without limitation the rights to use,", |
35 |
"copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the", |
36 |
"Software, and to permit persons to whom the Software is furnished to do so," |
37 |
"subject to the following conditions :", |
38 |
"", |
39 |
"The above copyright notice and this permission notice shall be included in all", |
40 |
"copies or substantial portions of the Software.", |
41 |
"", |
42 |
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR", |
43 |
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,", |
44 |
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE", |
45 |
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER", |
46 |
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,", |
47 |
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE" |
48 |
"SOFTWARE." |
49 |
}; |
50 |
|
51 |
static const char * const prog_desc_text[] = |
52 |
{ |
53 |
"ifsfscan (mnemonic: Ill-Formed Source File SCAN) is a program for detecting and", |
54 |
"correcting gross formatting errors in source files. The errors detected and", |
55 |
"corrected include non-ASCII characters, tab characters, trailing whitespace on", |
56 |
"lines, and other errors.", |
57 |
}; |
58 |
|
59 |
static const char * const prog_help_text[] = |
60 |
{ |
61 |
"Usage: ifsfscan <options> <files_no_wildcards>", |
62 |
"", |
63 |
"Options:", |
64 |
" -fixall", |
65 |
" Fixes all known formatting issues non-interactively. For users who", |
66 |
" understand the program's behavior, this is the most common option used.", |
67 |
" -fixnone (default)", |
68 |
" Identifies formatting issues, but does make any corrections.", |
69 |
"", |
70 |
"Notes:", |
71 |
" ( 1) : THIS PROGRAM CAN DESTROY DATA. This program does not make backup", |
72 |
" files. If this program were used to make \"corrections\" on a binary", |
73 |
" file, the file would likely be irreparably corrupted.", |
74 |
" ( 2) : This program does not process wildcards.", |
75 |
" ( 3) : This program is limited by available memory. This program parses a", |
76 |
" source file, writing the corrected file to a RAM buffer, then finally", |
77 |
" overwrites the source file from the RAM buffer. The practical limit", |
78 |
" for the size of the file that can be corrected using this program is", |
79 |
" probably a couple gigabytes.", |
80 |
" ( 4) : The license of this program does not prohibit copying the source code", |
81 |
" to another version control repository or simply \"stealing\" the", |
82 |
" program and source code for any other use. The MIT License is a very", |
83 |
" unrestrictive open-source license." |
84 |
}; |
85 |
|
86 |
|
87 |
//-------------------------------------------------------------------------------- |
88 |
// T E R M I N A T I O N F U N C T I O N S |
89 |
//-------------------------------------------------------------------------------- |
90 |
static void CCMFATAL_fatal(const char *desc, const char *file, size_t line) |
91 |
{ |
92 |
printf("Fatal error. Must terminate execution.\n"); |
93 |
printf("File: %s, Line: %zu.\n", file, line); |
94 |
printf("Error description: %s\n", desc); |
95 |
exit(4); //Error code 4 for error termination. |
96 |
} |
97 |
|
98 |
//-------------------------------------------------------------------------------- |
99 |
// A S S E R T I O N F U N C T I O N S |
100 |
//-------------------------------------------------------------------------------- |
101 |
|
102 |
//-------------------------------------------------------------------------------- |
103 |
// 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 |
104 |
//-------------------------------------------------------------------------------- |
105 |
//These functions form a layer over the standard library so that conditions of |
106 |
//concern can be more easily trapped. |
107 |
//-------------------------------------------------------------------------------- |
108 |
void *CCMALLOC_malloc(size_t size) |
109 |
{ |
110 |
void *rv; |
111 |
|
112 |
rv = malloc(size); |
113 |
|
114 |
if (!rv) |
115 |
{ |
116 |
CCMFATAL_fatal("NULL pointer from malloc()--probable out of memory.", |
117 |
__FILE__, |
118 |
__LINE__); |
119 |
} |
120 |
|
121 |
return(rv); |
122 |
} |
123 |
|
124 |
void *CCMALLOC_calloc(size_t num, size_t size) |
125 |
{ |
126 |
void *rv; |
127 |
|
128 |
rv = calloc(num, size); |
129 |
|
130 |
if (!rv) |
131 |
{ |
132 |
CCMFATAL_fatal("NULL pointer from calloc()--probable out of memory.", |
133 |
__FILE__, |
134 |
__LINE__); |
135 |
} |
136 |
|
137 |
return(rv); |
138 |
} |
139 |
|
140 |
void *CCMALLOC_realloc(void *memblock, size_t size) |
141 |
{ |
142 |
void *rv; |
143 |
|
144 |
rv = realloc(memblock, size); |
145 |
|
146 |
if ((!rv) && (size)) |
147 |
{ |
148 |
CCMFATAL_fatal("NULL pointer from realloc()--probable out of memory.", |
149 |
__FILE__, |
150 |
__LINE__); |
151 |
} |
152 |
|
153 |
return(rv); |
154 |
} |
155 |
|
156 |
|
157 |
void CCMALLOC_free(void *memblock) |
158 |
{ |
159 |
free(memblock); |
160 |
} |
161 |
|
162 |
#if 0 |
163 |
|
164 |
int CHARFUNC_digit_to_val(char digit) |
165 |
{ |
166 |
switch (digit) |
167 |
{ |
168 |
case '0': return(0); |
169 |
break; |
170 |
case '1': return(1); |
171 |
break; |
172 |
case '2': return(2); |
173 |
break; |
174 |
case '3': return(3); |
175 |
break; |
176 |
case '4': return(4); |
177 |
break; |
178 |
case '5': return(5); |
179 |
break; |
180 |
case '6': return(6); |
181 |
break; |
182 |
case '7': return(7); |
183 |
break; |
184 |
case '8': return(8); |
185 |
break; |
186 |
case '9': return(9); |
187 |
break; |
188 |
default: return(-1); |
189 |
break; |
190 |
} |
191 |
} |
192 |
|
193 |
|
194 |
char CHARFUNC_nibble_to_lc_hex_digit(int nibble) |
195 |
{ |
196 |
switch (nibble & 0x0F) |
197 |
{ |
198 |
case 0: |
199 |
return('0'); |
200 |
break; |
201 |
case 1: |
202 |
return('1'); |
203 |
break; |
204 |
case 2: |
205 |
return('2'); |
206 |
break; |
207 |
case 3: |
208 |
return('3'); |
209 |
break; |
210 |
case 4: |
211 |
return('4'); |
212 |
break; |
213 |
case 5: |
214 |
return('5'); |
215 |
break; |
216 |
case 6: |
217 |
return('6'); |
218 |
break; |
219 |
case 7: |
220 |
return('7'); |
221 |
break; |
222 |
case 8: |
223 |
return('8'); |
224 |
break; |
225 |
case 9: |
226 |
return('9'); |
227 |
break; |
228 |
case 10: |
229 |
return('a'); |
230 |
break; |
231 |
case 11: |
232 |
return('b'); |
233 |
break; |
234 |
case 12: |
235 |
return('c'); |
236 |
break; |
237 |
case 13: |
238 |
return('d'); |
239 |
break; |
240 |
case 14: |
241 |
return('e'); |
242 |
break; |
243 |
case 15: |
244 |
return('f'); |
245 |
break; |
246 |
default: |
247 |
assert(0); |
248 |
return('?'); |
249 |
break; |
250 |
} |
251 |
} |
252 |
|
253 |
|
254 |
void CHARFUNC_int_to_lc_hex_rev(int arg, char *s) |
255 |
{ |
256 |
int i; |
257 |
|
258 |
assert(s != NULL); |
259 |
|
260 |
for (i = 0; i<8; i++) |
261 |
{ |
262 |
s[i] = CHARFUNC_nibble_to_lc_hex_digit(arg); |
263 |
arg >>= 4; |
264 |
} |
265 |
} |
266 |
|
267 |
#define FCMIOF_HORIZONTAL_BAR_SEP_CHAR ('-') |
268 |
#define FCMIOF_LINE_LEN (78) |
269 |
|
270 |
|
271 |
//08/16/01: Visual inspection OK. |
272 |
int FCMIOF_get_line_len(void) |
273 |
{ |
274 |
return(FCMIOF_LINE_LEN); |
275 |
} |
276 |
|
277 |
|
278 |
//08/16/01: Visual inspection OK. |
279 |
void FCMIOF_stream_repchar(FILE *s, char c, unsigned n) |
280 |
{ |
281 |
assert(s != NULL); |
282 |
|
283 |
while (n--) |
284 |
fprintf(s, "%c", c); |
285 |
} |
286 |
|
287 |
|
288 |
//08/16/01: Visual inspection OK. |
289 |
void FCMIOF_repchar(char c, unsigned n) |
290 |
{ |
291 |
while (n--) |
292 |
printf("%c", c); |
293 |
} |
294 |
|
295 |
|
296 |
//08/16/01: Visual inspection OK. |
297 |
void FCMIOF_hline(void) |
298 |
{ |
299 |
FCMIOF_repchar(FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN); |
300 |
printf("\n"); |
301 |
} |
302 |
|
303 |
|
304 |
//08/16/01: Visual inspection OK. |
305 |
void FCMIOF_stream_hline(FILE *s) |
306 |
{ |
307 |
assert(s != NULL); |
308 |
|
309 |
FCMIOF_stream_repchar(s, FCMIOF_HORIZONTAL_BAR_SEP_CHAR, FCMIOF_LINE_LEN); |
310 |
fprintf(s, "\n"); |
311 |
} |
312 |
|
313 |
|
314 |
//08/16/01: Visual inspection OK. |
315 |
void FCMIOF_stream_bannerheading(FILE *f, |
316 |
char *s, |
317 |
int n_extra_lines) |
318 |
{ |
319 |
const int lr_padding = 3; |
320 |
/* The number of spaces on each side of what is printed. |
321 |
*/ |
322 |
int i; |
323 |
/* General iteration variable. |
324 |
*/ |
325 |
|
326 |
int n_asterisks; |
327 |
int input_arg_len; |
328 |
int n_left_spaces; |
329 |
int n_right_spaces; |
330 |
|
331 |
/* Check the file pointer, string pointer, and other par. |
332 |
*/ |
333 |
assert(f != NULL); |
334 |
assert(s != NULL); |
335 |
assert(n_extra_lines >= 0); |
336 |
|
337 |
/* Print the right number of solid lines of asterisks to the |
338 |
** standard output. |
339 |
*/ |
340 |
for (i = 0; i<n_extra_lines; i++) |
341 |
{ |
342 |
FCMIOF_stream_repchar(f, '*', FCMIOF_LINE_LEN); |
343 |
fprintf(f, "\n"); |
344 |
} |
345 |
|
346 |
/* Figure out how many asterisks to print on each side of the |
347 |
** argument, and how many spaces. We also need to figure out |
348 |
** how many characters of the input argument to print--if there |
349 |
** are too many characters, we need to truncate. |
350 |
*/ |
351 |
input_arg_len = strlen(s); |
352 |
if (input_arg_len > (FCMIOF_LINE_LEN - 2 * lr_padding - 2)) |
353 |
input_arg_len = FCMIOF_LINE_LEN - 2 * lr_padding - 2; |
354 |
|
355 |
n_asterisks = (FCMIOF_LINE_LEN - 2 * lr_padding - input_arg_len) / 2; |
356 |
|
357 |
n_left_spaces = lr_padding; |
358 |
|
359 |
if ((FCMIOF_LINE_LEN - 2 * lr_padding - input_arg_len) % 2) |
360 |
{ |
361 |
/* Odd, need to pad the right by one. */ |
362 |
n_right_spaces = lr_padding + 1; |
363 |
} |
364 |
else |
365 |
{ |
366 |
n_right_spaces = lr_padding; |
367 |
} |
368 |
|
369 |
/* Print the text. */ |
370 |
FCMIOF_stream_repchar(f, '*', n_asterisks); |
371 |
FCMIOF_stream_repchar(f, ' ', n_left_spaces); |
372 |
for (i = 0; i<input_arg_len; i++) |
373 |
fprintf(f, "%c", s[i]); |
374 |
FCMIOF_stream_repchar(f, ' ', n_right_spaces); |
375 |
FCMIOF_stream_repchar(f, '*', n_asterisks); |
376 |
fprintf(f, "\n"); |
377 |
|
378 |
/* Print the right number of solid lines of asterisks to the |
379 |
** standard output. |
380 |
*/ |
381 |
for (i = 0; i<n_extra_lines; i++) |
382 |
{ |
383 |
FCMIOF_stream_repchar(f, '*', FCMIOF_LINE_LEN); |
384 |
fprintf(f, "\n"); |
385 |
} |
386 |
} |
387 |
|
388 |
|
389 |
//08/16/01: Visual inspection OK. |
390 |
void FCMIOF_bannerheading(char *s, int n_extra_lines) |
391 |
{ |
392 |
assert(s != NULL); |
393 |
assert(n_extra_lines >= 0); |
394 |
|
395 |
FCMIOF_stream_bannerheading(stdout, s, n_extra_lines); |
396 |
} |
397 |
|
398 |
|
399 |
void FCMIOF_time_stream(FILE *s, time_t ltime) |
400 |
{ |
401 |
char *p; |
402 |
|
403 |
assert(s != NULL); |
404 |
|
405 |
time(<ime); |
406 |
|
407 |
p = ctime(<ime); |
408 |
|
409 |
if (p) |
410 |
{ |
411 |
int i; |
412 |
|
413 |
for (i = 11; i<19; i++) |
414 |
fprintf(s, "%c", p[i]); |
415 |
fprintf(s, " "); |
416 |
for (i = 0; i<10; i++) |
417 |
fprintf(s, "%c", p[i]); |
418 |
fprintf(s, " "); |
419 |
for (i = 20; i<24; i++) |
420 |
fprintf(s, "%c", p[i]); |
421 |
} |
422 |
else |
423 |
{ |
424 |
fprintf(s, "??? ??? ?? ??:??:?? ????"); |
425 |
} |
426 |
} |
427 |
|
428 |
#endif |
429 |
|
430 |
int c_main(int argc, char **argv) |
431 |
{ |
432 |
if (argc <= 1) |
433 |
{ |
434 |
//This is most likely someone trying to figure out what the program is or does. |
435 |
//Treat this the same as a request for help. |
436 |
} |
437 |
|
438 |
return 0; |
439 |
} |