OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

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

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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