source: rtems/c/build-tools/eolstrip.c @ 2b5944cf

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