Subversion Repositories planix.SVN

Rev

Rev 49 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 49 Rev 59
Line 1... Line 1...
1
/*
1
/*
2
 * SPDX-License-Identifier: BSD-3-Clause
-
 
3
 *
-
 
4
 * Copyright (c) 1989, 1993
2
 * Copyright (c) 1989, 1993
5
 *	The Regents of the University of California.  All rights reserved.
3
 *	The Regents of the University of California.  All rights reserved.
-
 
4
 * Copyright (c) 2019
-
 
5
 * 	The PLANIX Project. All rights reserved
6
 *
6
 *
7
 * This code is derived from software contributed to Berkeley by
7
 * This code is derived from software contributed to Berkeley by
8
 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
8
 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
9
 *
9
 *
10
 * Redistribution and use in source and binary forms, with or without
10
 * Redistribution and use in source and binary forms, with or without
Line 29... Line 29...
29
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
 * SUCH DAMAGE.
32
 * SUCH DAMAGE.
33
 */
33
 */
34
 
-
 
35
#ifndef lint
-
 
36
static const char copyright[] =
-
 
37
"@(#) Copyright (c) 1989, 1993\n\
-
 
38
	The Regents of the University of California.  All rights reserved.\n";
-
 
39
static const char sccsid[] = "@(#)cut.c	8.3 (Berkeley) 5/4/95";
-
 
40
#endif /* not lint */
-
 
41
 
34
 
42
#include <ctype.h>
35
#include <ctype.h>
43
#include <errno.h>
36
#include <errno.h>
44
#include <limits.h>
37
#include <limits.h>
45
#include <locale.h>
38
#include <locale.h>
46
#include <stdio.h>
39
#include <stdio.h>
47
#include <stdlib.h>
40
#include <stdlib.h>
48
#include <string.h>
41
#include <string.h>
49
#include <unistd.h>
42
#include <unistd.h>
50
#include <wchar.h>
43
#include <wchar.h>
51
 
44
 
-
 
45
#include <err.h>
-
 
46
 
52
 
47
 
53
static int	bflag;
48
static int	bflag;
54
static int	cflag;
49
static int	cflag;
55
static wchar_t	dchar;
50
static wchar_t	dchar;
56
static char	dcharmb[MB_LEN_MAX + 1];
51
static char	dcharmb[MB_LEN_MAX + 1];
Line 60... Line 55...
60
static int	sflag;
55
static int	sflag;
61
static int	wflag;
56
static int	wflag;
62
 
57
 
63
static size_t	autostart, autostop, maxval;
58
static size_t	autostart, autostop, maxval;
64
static char *	positions;
59
static char *	positions;
65
 
60
 
66
static int	b_cut(FILE *, const char *);
61
static int	b_cut(FILE *, const char *);
67
static int	b_n_cut(FILE *, const char *);
62
static int	b_n_cut(FILE *, const char *);
68
static int	c_cut(FILE *, const char *);
63
static int	c_cut(FILE *, const char *);
69
static int	f_cut(FILE *, const char *);
64
static int	f_cut(FILE *, const char *);
70
static void	get_list(char *);
65
static void	get_list(char *);
Line 95... Line 90...
95
		case 'c':
90
		case 'c':
96
			get_list(optarg);
91
			get_list(optarg);
97
			cflag = 1;
92
			cflag = 1;
98
			break;
93
			break;
99
		case 'd':
94
		case 'd':
100
			n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL);
95
//			n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL);
-
 
96
			n = mbtowc(&dchar, optarg, MB_LEN_MAX);
101
			if (dchar == '\0' || n != strlen(optarg))
97
			if (dchar == '\0' || n != strlen(optarg))
102
				errx(1, "bad delimiter");
98
				errx(1, "bad delimiter");
103
			strcpy(dcharmb, optarg);
99
			strcpy(dcharmb, optarg);
104
			dflag = 1;
100
			dflag = 1;
105
			break;
101
			break;
Line 154... Line 150...
154
			}
150
			}
155
		}
151
		}
156
	else
152
	else
157
		rval = fcn(stdin, "stdin");
153
		rval = fcn(stdin, "stdin");
158
	exit(rval);
154
	exit(rval);
-
 
155
	return rval;
159
}
156
}
160
 
157
 
161
static void
158
static void
162
get_list(char *list)
159
get_list(char *list)
163
{
160
{
Line 237... Line 234...
237
		memset((char *)positions + oldnpos, 0, npos - oldnpos);
234
		memset((char *)positions + oldnpos, 0, npos - oldnpos);
238
	}
235
	}
239
}
236
}
240
 
237
 
241
static int
238
static int
242
b_cut(FILE *fp, const char *fname __unused)
239
b_cut(FILE *fp, const char *fname )
243
{
240
{
244
	int ch, col;
241
	int ch, col;
245
	char *pos;
242
	char *pos;
246
 
243
 
247
	ch = 0;
244
	ch = 0;
Line 280... Line 277...
280
	int canwrite, clen, warned;
277
	int canwrite, clen, warned;
281
	mbstate_t mbs;
278
	mbstate_t mbs;
282
 
279
 
283
	memset(&mbs, 0, sizeof(mbs));
280
	memset(&mbs, 0, sizeof(mbs));
284
	warned = 0;
281
	warned = 0;
-
 
282
 
-
 
283
	lbuf = NULL;
-
 
284
 
285
	while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
285
//	while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
-
 
286
	while ((lbuflen = getline(&lbuf,0, fp)) != -1) {
286
		for (col = 0; lbuflen > 0; col += clen) {
287
		for (col = 0; lbuflen > 0; col += clen) {
287
			if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
288
			if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
288
				if (!warned) {
289
				if (!warned) {
289
					warn("%s", fname);
290
					warn("%s", fname);
290
					warned = 1;
291
					warned = 1;
291
				}
292
				}
292
				memset(&mbs, 0, sizeof(mbs));
293
				memset(&mbs, 0, sizeof(mbs));
293
				clen = 1;
294
				clen = 1;
294
			}
295
			}
295
			if (clen == 0 || *lbuf == '\n')
296
			if (clen == 0 || *lbuf == '\n')
296
				break;
297
				break;
297
			if (col < maxval && !positions[1 + col]) {
298
			if (col < maxval && !positions[1 + col]) {
298
				/*
299
				/*
299
				 * Print the character if (1) after an initial
300
				 * Print the character if (1) after an initial
300
				 * segment of un-selected bytes, the rest of
301
				 * segment of un-selected bytes, the rest of
301
				 * it is selected, and (2) the last byte is
302
				 * it is selected, and (2) the last byte is
Line 329... Line 330...
329
			lbuflen -= clen;
330
			lbuflen -= clen;
330
		}
331
		}
331
		if (lbuflen > 0)
332
		if (lbuflen > 0)
332
			putchar('\n');
333
			putchar('\n');
333
	}
334
	}
-
 
335
	free(lbuf);
334
	return (warned);
336
	return (warned);
335
}
337
}
336
 
338
 
337
static int
339
static int
338
c_cut(FILE *fp, const char *fname)
340
c_cut(FILE *fp, const char *fname)
Line 360... Line 362...
360
				while ((ch = getwc(fp)) != WEOF && ch != '\n');
362
				while ((ch = getwc(fp)) != WEOF && ch != '\n');
361
		}
363
		}
362
		(void)putwchar('\n');
364
		(void)putwchar('\n');
363
	}
365
	}
364
out:
366
out:
-
 
367
 
365
	if (ferror(fp)) {
368
	if (ferror(fp)) {
366
		warn("%s", fname);
369
		warn("%s", fname);
367
		return (1);
370
		return (1);
368
	}
371
	}
369
	return (0);
372
	return (0);
370
}
373
}
371
 
374
 
372
static int
375
static int
373
is_delim(wchar_t ch)
376
is_delim(wchar_t ch)
374
{
377
{
375
	if (wflag) {
378
	if (wflag) {
376
		if (ch == ' ' || ch == '\t')
379
		if (ch == ' ' || ch == '\t')
377
			return 1;
380
			return 1;
Line 379... Line 382...
379
		if (ch == dchar)
382
		if (ch == dchar)
380
			return 1;
383
			return 1;
381
	}
384
	}
382
	return 0;
385
	return 0;
383
}
386
}
384
 
387
 
385
static int
388
static int
386
f_cut(FILE *fp, const char *fname)
389
f_cut(FILE *fp, const char *fname)
387
{
390
{
388
	wchar_t ch;
391
	wchar_t ch;
389
	int field, i, isdelim;
392
	int field, i, isdelim;
Line 391... Line 394...
391
	int output;
394
	int output;
392
	char *lbuf, *mlbuf;
395
	char *lbuf, *mlbuf;
393
	size_t clen, lbuflen, reallen;
396
	size_t clen, lbuflen, reallen;
394
 
397
 
395
	mlbuf = NULL;
398
	mlbuf = NULL;
-
 
399
 
-
 
400
	lbuf = NULL;
-
 
401
 
-
 
402
 
396
	while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
403
	//while ((lbuf = fgetln(fp, &lbuflen)) != NULL) 
-
 
404
	while ((lbuflen = getline(&lbuf,0, fp)) != -1) 
-
 
405
	{
397
		reallen = lbuflen;
406
		reallen = lbuflen;
398
		/* Assert EOL has a newline. */
407
		/* Assert EOL has a newline. */
399
		if (*(lbuf + lbuflen - 1) != '\n') {
408
		if (*(lbuf + lbuflen - 1) != '\n') {
400
			/* Can't have > 1 line with no trailing newline. */
409
			/* Can't have > 1 line with no trailing newline. */
401
			mlbuf = malloc(lbuflen + 1);
410
			mlbuf = malloc(lbuflen + 1);
Line 406... Line 415...
406
			lbuf = mlbuf;
415
			lbuf = mlbuf;
407
			reallen++;
416
			reallen++;
408
		}
417
		}
409
		output = 0;
418
		output = 0;
410
		for (isdelim = 0, p = lbuf;; p += clen) {
419
		for (isdelim = 0, p = lbuf;; p += clen) {
411
			clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL);
420
//			clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL);
-
 
421
			clen = mbtowc(&ch, p, lbuf + reallen - p);
412
			if (clen == (size_t)-1 || clen == (size_t)-2) {
422
			if (clen == (size_t)-1 || clen == (size_t)-2) {
413
				warnc(EILSEQ, "%s", fname);
423
//				warnc(EILSEQ, "%s", fname);
-
 
424
				free(lbuf);
414
				free(mlbuf);
425
				free(mlbuf);
415
				return (1);
426
				return (1);
416
			}
427
			}
417
			if (clen == 0)
428
			if (clen == 0)
418
				clen = 1;
429
				clen = 1;
Line 432... Line 443...
432
		for (field = maxval, p = lbuf; field; --field, ++pos) {
443
		for (field = maxval, p = lbuf; field; --field, ++pos) {
433
			if (*pos && output++)
444
			if (*pos && output++)
434
				for (i = 0; dcharmb[i] != '\0'; i++)
445
				for (i = 0; dcharmb[i] != '\0'; i++)
435
					putchar(dcharmb[i]);
446
					putchar(dcharmb[i]);
436
			for (;;) {
447
			for (;;) {
437
				clen = mbrtowc(&ch, p, lbuf + reallen - p,
448
//				clen = mbrtowc(&ch, p, lbuf + reallen - p,
438
				    NULL);
449
//				    NULL);
-
 
450
				clen = mbtowc(&ch, p, lbuf + reallen - p);
439
				if (clen == (size_t)-1 || clen == (size_t)-2) {
451
				if (clen == (size_t)-1 || clen == (size_t)-2) {
440
					warnc(EILSEQ, "%s", fname);
452
//					warnc(EILSEQ, "%s", fname);
-
 
453
					free(lbuf);
441
					free(mlbuf);
454
					free(mlbuf);
442
					return (1);
455
					return (1);
443
				}
456
				}
444
				if (clen == 0)
457
				if (clen == 0)
445
					clen = 1;
458
					clen = 1;
Line 469... Line 482...
469
				for (; (ch = *p) != '\n'; ++p);
482
				for (; (ch = *p) != '\n'; ++p);
470
		}
483
		}
471
		(void)putchar('\n');
484
		(void)putchar('\n');
472
	}
485
	}
473
	free(mlbuf);
486
	free(mlbuf);
-
 
487
	free(lbuf);
-
 
488
 
474
	return (0);
489
	return (0);
475
}
490
}
476
 
491
 
477
static void
492
static void
478
usage(void)
493
usage(void)