OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [tools/] [build/] [cklength.c] - Blame information for rev 173

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.