source: rtems/tools/build/cklength.c @ fe65110

4.104.114.84.95
Last change on this file since fe65110 was fe65110, checked in by Joel Sherrill <joel.sherrill@…>, on 05/31/95 at 17:20:57

fixing revision

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