2006-12-06 17:25:32 -06:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1980 Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
/*
|
2013-01-26 10:07:51 -06:00
|
|
|
* 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
|
2006-12-06 17:25:41 -06:00
|
|
|
* added Native Language Support
|
|
|
|
* 1999-09-19 Bruno Haible <haible@clisp.cons.org>
|
|
|
|
* modified to work correctly in multi-byte locales
|
|
|
|
*/
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
#include <stdio.h>
|
2006-12-06 17:25:41 -06:00
|
|
|
#include <stdlib.h>
|
2011-05-23 15:42:15 -05:00
|
|
|
#include <getopt.h>
|
2012-04-04 12:39:15 -05:00
|
|
|
#include <unistd.h>
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2007-09-06 19:30:16 -05:00
|
|
|
#include "nls.h"
|
2011-05-23 15:42:15 -05:00
|
|
|
#include "strutils.h"
|
|
|
|
#include "c.h"
|
2017-01-19 13:27:37 -06:00
|
|
|
#include "widechar.h"
|
2012-04-04 12:39:15 -05:00
|
|
|
#include "closestream.h"
|
2006-12-06 17:25:35 -06:00
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
/*
|
|
|
|
COLRM removes unwanted columns from a file
|
|
|
|
Jeff Schriebman UC Berkeley 11-74
|
|
|
|
*/
|
|
|
|
|
2017-06-20 13:20:29 -05:00
|
|
|
static void __attribute__((__noreturn__)) usage(void)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2017-06-20 13:20:29 -05:00
|
|
|
FILE *out = stdout;
|
2011-05-23 15:42:15 -05:00
|
|
|
fprintf(out, _("\nUsage:\n"
|
|
|
|
" %s [startcol [endcol]]\n"),
|
|
|
|
program_invocation_short_name);
|
2014-12-22 15:57:17 -06:00
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(_("Filter out the specified columns.\n"), out);
|
|
|
|
|
2014-09-28 14:51:41 -05:00
|
|
|
fputs(USAGE_OPTIONS, out);
|
2017-06-29 08:52:16 -05:00
|
|
|
printf(USAGE_HELP_OPTIONS(16));
|
2011-05-23 15:42:15 -05:00
|
|
|
fprintf(out, _("%s reads from standard input and writes to standard output\n\n"),
|
|
|
|
program_invocation_short_name);
|
2017-06-29 08:52:16 -05:00
|
|
|
printf(USAGE_MAN_TAIL("colrm(1)"));
|
2017-06-20 13:20:29 -05:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-05-23 15:42:15 -05:00
|
|
|
}
|
|
|
|
|
2012-07-16 11:46:44 -05:00
|
|
|
static int process_input(unsigned long first, unsigned long last)
|
2011-05-23 15:42:15 -05:00
|
|
|
{
|
|
|
|
unsigned long ct = 0;
|
|
|
|
wint_t c;
|
|
|
|
unsigned long i;
|
|
|
|
int w;
|
2006-12-06 17:25:44 -06:00
|
|
|
int padding;
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2011-05-23 15:42:15 -05:00
|
|
|
for (;;) {
|
|
|
|
c = getwc(stdin);
|
|
|
|
if (c == WEOF)
|
|
|
|
return 0;
|
|
|
|
if (c == '\t')
|
|
|
|
w = ((ct + 8) & ~7) - ct;
|
|
|
|
else if (c == '\b')
|
|
|
|
w = (ct ? ct - 1 : 0) - ct;
|
|
|
|
else {
|
|
|
|
w = wcwidth(c);
|
|
|
|
if (w < 0)
|
|
|
|
w = 0;
|
|
|
|
}
|
|
|
|
ct += w;
|
|
|
|
if (c == '\n') {
|
|
|
|
putwc(c, stdout);
|
|
|
|
ct = 0;
|
|
|
|
continue;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2011-05-23 15:42:15 -05:00
|
|
|
}
|
|
|
|
if (!first || ct < first) {
|
|
|
|
putwc(c, stdout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2011-05-23 15:42:15 -05:00
|
|
|
|
|
|
|
for (i = ct - w + 1; i < first; i++)
|
2006-12-06 17:25:44 -06:00
|
|
|
putwc(' ', stdout);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2011-05-23 15:42:15 -05:00
|
|
|
/* Loop getting rid of characters */
|
2006-12-06 17:25:32 -06:00
|
|
|
while (!last || ct < last) {
|
2006-12-06 17:25:41 -06:00
|
|
|
c = getwc(stdin);
|
2006-12-06 17:25:53 -06:00
|
|
|
if (c == WEOF)
|
2011-05-23 15:42:15 -05:00
|
|
|
return 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
if (c == '\n') {
|
2006-12-06 17:25:41 -06:00
|
|
|
putwc(c, stdout);
|
2011-05-23 15:42:15 -05:00
|
|
|
return 1;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
if (c == '\t')
|
|
|
|
ct = (ct + 8) & ~7;
|
|
|
|
else if (c == '\b')
|
|
|
|
ct = ct ? ct - 1 : 0;
|
2006-12-06 17:25:44 -06:00
|
|
|
else {
|
|
|
|
w = wcwidth(c);
|
|
|
|
if (w < 0)
|
|
|
|
w = 0;
|
|
|
|
ct += w;
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2006-12-06 17:25:44 -06:00
|
|
|
padding = 0;
|
|
|
|
|
2011-05-23 15:42:15 -05:00
|
|
|
/* Output last of the line */
|
2006-12-06 17:25:32 -06:00
|
|
|
for (;;) {
|
2006-12-06 17:25:41 -06:00
|
|
|
c = getwc(stdin);
|
2006-12-06 17:25:53 -06:00
|
|
|
if (c == WEOF)
|
2006-12-06 17:25:32 -06:00
|
|
|
break;
|
2006-12-06 17:25:44 -06:00
|
|
|
if (c == '\n') {
|
|
|
|
putwc(c, stdout);
|
2011-05-23 15:42:15 -05:00
|
|
|
return 1;
|
2006-12-06 17:25:44 -06:00
|
|
|
}
|
|
|
|
if (padding == 0 && last < ct) {
|
2011-05-23 15:42:15 -05:00
|
|
|
for (i = last; i < ct; i++)
|
2006-12-06 17:25:44 -06:00
|
|
|
putwc(' ', stdout);
|
|
|
|
padding = 1;
|
|
|
|
}
|
|
|
|
putwc(c, stdout);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2011-05-23 15:42:15 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
unsigned long first = 0, last = 0;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
static const struct option longopts[] = {
|
2017-02-11 14:23:26 -06:00
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}
|
2011-05-23 15:42:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2019-06-13 06:22:32 -05:00
|
|
|
close_stdout_atexit();
|
2011-05-23 15:42:15 -05:00
|
|
|
|
|
|
|
while ((opt =
|
2020-10-19 15:57:04 -05:00
|
|
|
getopt_long(argc, argv, "Vh", longopts,
|
2011-05-23 15:42:15 -05:00
|
|
|
NULL)) != -1)
|
|
|
|
switch (opt) {
|
|
|
|
case 'V':
|
2019-04-16 08:14:13 -05:00
|
|
|
print_version(EXIT_SUCCESS);
|
2011-05-23 15:42:15 -05:00
|
|
|
case 'h':
|
2017-06-20 13:20:29 -05:00
|
|
|
usage();
|
2011-05-23 15:42:15 -05:00
|
|
|
default:
|
2016-12-19 06:13:34 -06:00
|
|
|
errtryhelp(EXIT_FAILURE);
|
2011-05-23 15:42:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
first = strtoul_or_err(*++argv, _("first argument"));
|
|
|
|
if (argc > 2)
|
|
|
|
last = strtoul_or_err(*++argv, _("second argument"));
|
|
|
|
|
|
|
|
while (process_input(first, last))
|
|
|
|
;
|
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
fflush(stdout);
|
2011-05-23 15:42:15 -05:00
|
|
|
return EXIT_SUCCESS;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|