source: rtems/c/build-tools/cklength.c @ 7593d56c

4.104.114.84.95
Last change on this file since 7593d56c was c62d36f, checked in by Joel Sherrill <joel.sherrill@…>, on 10/06/95 at 20:01:20

SPARC merged and successfully tested w/o interrupt support

  • Property mode set to 100644
File size: 8.7 KB
Line 
1/*
2 *  cklength - check the length of lines in a file
3 *
4 *  This program check to see if the files passed to it on the command line
5 *  contain a line which exceeds the maximum allowable length.  The default
6 *  maximum line length is 80.
7 *
8 *  usage:  cklength  [ -v ] [ arg ... ] files...
9 *           -l length   -- maximum line length
10 *           -v          -- verbose
11 *
12 * $Id$
13 * $Log$
14 * Revision 1.3  1995/10/06 16:32:20  joel
15 * moved to new development machine and went to gcc 2.7.0
16 *
17 * Revision 1.2  1995/05/31  16:44:26  joel
18 * fixing revision
19 *
20 * Revision 1.1.1.1  1995/05/11  17:35:11  joel
21 * Release 3.1.15
22 *
23 */
24
25#define GETOPTARGS "l:nNv"
26
27char *USAGE = "\
28usage:  cklength  [ -v ] [ arg ... ] files... \n\
29            -l length   -- maximum line length\n\
30            -n          -- report line numbers for offending lines\n\
31            -N          -- report line numbers and length for offending lines\n\
32            -v          -- verbose\n\
33\n\
34Print the name of files which have at least 1 line which exceeds the\n\
35maximum line length.  The default maximum line length is 80.\n\
36";
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <fcntl.h>
41#include <ctype.h>
42#include <stdlib.h>
43#include <unistd.h>
44#include <string.h>
45#include <memory.h>
46#include <stdarg.h>
47
48#define BUFFER_SIZE     512
49
50#define SUCCESS         0
51#define FAILURE         -1
52#define Failed(x)       (((int) (x)) == FAILURE)
53#define TRUE    1
54#define FALSE   0
55#define STREQ(a,b)      (strcmp(a,b) == 0)
56#define NUMELEMS(arr)   (sizeof(arr) / sizeof(arr[0]))
57
58/*
59 * Definitions for unsigned "ints"; especially for use in data structures
60 *  that will be shared among (potentially) different cpu's (we punt on
61 *  byte ordering problems tho)
62 */
63
64typedef unsigned char   u8;
65typedef unsigned short  u16;
66typedef unsigned long   u32;
67
68/*
69 * vars controlled by command line options
70 */
71
72int verbose = FALSE;                    /* be verbose */
73int report_line_numbers = FALSE;        /* report line numbers of offenders */
74int report_line_length = FALSE;         /* report line length of offenders */
75
76int line_length = 80;                   /* maximum allowable line length */
77
78extern char *optarg;                    /* getopt(3) control vars */
79extern int optind, opterr;
80extern int errno;
81
82char *progname;                         /* for error() */
83
84int process(char *arg);
85void error(int errn, ...);
86long getparm(char *s, long min, long max, char *msg);
87
88#define ERR_ERRNO  (1<<((sizeof(int) * 8) - 2)) /* hi bit; use 'errno' */
89#define ERR_FATAL  (ERR_ERRNO / 2)              /* fatal error ; no return */
90#define ERR_ABORT  (ERR_ERRNO / 4)              /* fatal error ; abort */
91#define ERR_MASK   (ERR_ERRNO | ERR_FATAL | ERR_ABORT) /* all */
92
93#define stol(p) strtol(p, (char **) NULL, 0)
94int  Open(), Read(), Write();
95
96int
97main(int argc, char **argv, char **env)
98{
99    register int c;
100    int showusage = FALSE;                      /* usage error? */
101    int rc = 0;
102
103    /*
104     * figure out invocation leaf-name
105     */
106
107    if ((progname = strrchr(argv[0], '/')) == (char *) NULL)
108        progname = argv[0];
109    else
110        progname++;
111
112    argv[0] = progname;                         /* for getopt err reporting */
113
114    /*
115     *  Check options and arguments.
116     */
117
118    opterr = 0;                                 /* we'll report all errors */
119    while ((c = getopt(argc, argv, GETOPTARGS)) != EOF)
120        switch (c)
121        {
122            case 'l':                            /* line length */
123                line_length = atoi( optarg );
124                if ( line_length < 0 || line_length > BUFFER_SIZE )
125                  error(ERR_FATAL, "(%d) is illegal line length\n",line_length);
126                break;
127
128            case 'n':                           /* toggle report_line_numbers */
129                report_line_numbers = ! report_line_numbers;
130                break;
131
132            case 'N':                           /* toggle both reports */
133                report_line_numbers = ! report_line_numbers;
134                report_line_length  = ! report_line_length;
135                break;
136
137            case 'v':                           /* toggle verbose */
138                verbose = ! verbose;
139                break;
140
141            case '?':
142                showusage = TRUE;
143        }
144
145    if (showusage)
146    {
147        (void) fprintf(stderr, "%s", USAGE);
148        exit(1);
149    }
150
151    /*
152     *  traverse and process the arguments
153     */
154
155    for ( ; argv[optind]; optind++)
156        if (Failed(process(argv[optind])))
157            rc = FAILURE;
158
159    return rc;
160}
161
162
163/*
164 * process(arg)
165 */
166
167int
168process(char *arg)
169{
170  FILE   *in;
171  char   *bptr;
172  char    buffer[ BUFFER_SIZE ];
173  int     line_number;
174  int     length;
175  int     count;
176  int     rc = SUCCESS;  /* succeed by default */
177
178  in = fopen( arg, "r" );
179  if (!in)
180    error( ERR_ERRNO | ERR_FATAL, "Unable to open file (%s)\n", arg );
181
182  count = 0;
183
184  for ( line_number=1 ; ; line_number++ ) {
185    bptr = fgets( buffer, BUFFER_SIZE, in );
186    if (!bptr)
187      break;
188
189    /*
190     *  Don't count the carriage return.
191     */
192
193    length = strlen( buffer ) - 1;
194
195    if ( length <= line_length )
196      continue;
197
198    if ( count == 0 ) {
199      fprintf( stderr, "%s\n", arg );
200      if ( !report_line_numbers )
201        break;
202    }
203
204    if ( verbose )
205      fprintf( stderr, "TOO LONG:%d: %s\n", line_number, buffer );
206
207    if ( report_line_numbers ) {
208      if ( report_line_length )
209        fprintf( stderr, "%d: %d\n" , line_number, length );
210      else
211        fprintf( stderr, "%d\n" , line_number );
212    }
213
214    count++;
215
216  }
217
218  fclose( in );
219  return rc;
220}
221
222/*
223 * error(errn, arglist)
224 *      report an error to stderr using printf(3) conventions.
225 *      Any output is preceded by '<progname>: '
226 *
227 * Uses ERR_FATAL bit to request exit(errn)
228 *      ERR_ABORT to request abort()
229 *      ERR_ERRNO to indicate use of errno instead of argument.
230 *
231 * If resulting 'errn' is non-zero, it is assumed to be an 'errno' and its
232 *      associated error message is appended to the output.
233 */
234
235/*VARARGS*/
236
237void
238error(int error_flag, ...)
239{
240    va_list arglist;
241    register char *format;
242    extern char *sys_errlist[];
243    extern int sys_nerr;
244    int local_errno;
245
246    extern int errno;
247
248    (void) fflush(stdout);          /* in case stdout/stderr same */
249
250    local_errno = error_flag & ~ERR_MASK;
251    if (error_flag & ERR_ERRNO)     /* use errno? */
252        local_errno = errno;
253
254    va_start(arglist, error_flag);
255    format = va_arg(arglist, char *);
256    (void) fprintf(stderr, "%s: ", progname);
257    (void) vfprintf(stderr, format, arglist);
258    va_end(arglist);
259
260    if (local_errno)
261        if ((local_errno > 0) && (local_errno < sys_nerr))
262            (void) fprintf(stderr, " (%s)\n", sys_errlist[local_errno]);
263        else
264            (void) fprintf(stderr, " (unknown errno=%d)\n", local_errno);
265    else
266        (void) fprintf(stderr, "\n");
267
268    (void) fflush(stderr);
269
270    if (error_flag & (ERR_FATAL | ERR_ABORT))
271    {
272        if (error_flag & ERR_FATAL)
273        {
274            error(0, local_errno ? "fatal error, exiting" : "exiting");
275            exit(local_errno);
276        }
277        else
278        {
279            error(0, "fatal error, aborting");
280            abort();
281        }
282    }
283}
284
285long
286getparm(char *s,
287        long min,
288        long max,
289        char *msg)
290{
291    long val;
292
293    if ( ! strchr("0123456789-", *s))
294    {
295        error(ERR_FATAL, "'%s' is not a number", s);
296        return min;
297    }
298
299    val = strtol(s, (char **) NULL, 0);
300    if ((val < min) || (val > max))
301    {
302        if (min == max)
303            error(ERR_FATAL, "%s can only be %ld", s, min);
304        else
305            error(ERR_FATAL, "%s must be between %ld and %ld", msg, min, max);
306    }
307
308    return val;
309}
310
311
312/*
313 * Open()
314 *      Perform open(2), returning the file descriptor.  Prints
315 *      error message if open fails.
316 */
317
318int
319Open(char *file,
320     int oflag,
321     int mode)
322{
323    int O_fd;
324
325    if (Failed(O_fd = open(file, oflag, mode)))
326        error(
327          ERR_ERRNO | ERR_FATAL,
328          "open('%s', 0x%x, 0%o) failed", file, oflag, mode
329        );
330
331    return O_fd;
332}
333
334/*
335 * Read()
336 *      Perform read(2); prints error message if fails.
337 */
338
339int
340Read(int file,
341     char *buffer,
342     unsigned int count)
343{
344    int nbytes;
345
346    if (Failed(nbytes = read(file, buffer, count)))
347        error(
348          ERR_ERRNO | ERR_FATAL,
349          "read(%d, 0x%x, %d) failed", file, buffer, count
350         );
351
352    return nbytes;
353}
354
355/*
356 * Write()
357 *      Perform write(2); prints error message if fails.
358 */
359
360int
361Write(int file,
362      char *buffer,
363      unsigned int count)
364{
365    int nbytes;
366
367    if (Failed(nbytes = write(file, buffer, count)))
368        error(
369          ERR_ERRNO | ERR_FATAL,
370          "write(%d, 0x%x, %d) failed", file, buffer, count
371        );
372
373    return nbytes;
374}
Note: See TracBrowser for help on using the repository browser.