source: rtems/tools/build/eolstrip.c @ 0cf5bd6

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