Am 10.03.2017 19:07, schrieb Maxime Coste:
> Hello,
> 
> here is my stab at an implementation of patch, I hope this is the proper
> patch format and the code is in a good enough shape.
> 
> ---
>  AUTHORS                                |   3 +
>  coreutils/paste.c                      | 160 
> +++++++++++++++++++++++++++++++++
>  docs/posix_conformance.txt             |   8 +-
>  testsuite/paste/paste                  |  20 +++++
>  testsuite/paste/paste-back-cuted-lines |   9 ++
>  testsuite/paste/paste-multi-stdin      |  16 ++++
>  testsuite/paste/paste-pairs            |  16 ++++
>  testsuite/paste/paste-separate         |  19 ++++
>  8 files changed, 250 insertions(+), 1 deletion(-)
>  create mode 100644 coreutils/paste.c
>  create mode 100644 testsuite/paste/paste
>  create mode 100644 testsuite/paste/paste-back-cuted-lines
>  create mode 100644 testsuite/paste/paste-multi-stdin
>  create mode 100644 testsuite/paste/paste-pairs
>  create mode 100644 testsuite/paste/paste-separate
> 
> diff --git a/AUTHORS b/AUTHORS
> index fa58697f7..5c9a634c9 100644
> --- a/AUTHORS
> +++ b/AUTHORS
> @@ -178,3 +178,6 @@ Mike Frysinger <[email protected]>
>  
>  Jie Zhang <[email protected]>
>      fixed two bugs in msh and hush (exitcode of killed processes)
> +
> +Maxime Coste <[email protected]>
> +    paste implementation
> diff --git a/coreutils/paste.c b/coreutils/paste.c
> new file mode 100644
> index 000000000..ab0ad43c5
> --- /dev/null
> +++ b/coreutils/paste.c
> @@ -0,0 +1,160 @@
> +/* vi: set sw=4 ts=4: */
> +/*
> + * paste.c - implementation of the posix paste command
> + *
> + * Written by Maxime Coste <[email protected]>
> + *
> + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
> + */
> +//config:config PASTE
> +//config:    bool "paste"
> +//config:    default y
> +//config:    help
> +//config:      paste is used to paste lines of different files together
> +//config:      and write the result to stdout
> +
> +//applet:IF_PASTE(APPLET_NOEXEC(paste, paste, BB_DIR_USR_BIN, BB_SUID_DROP, 
> paste))
> +
> +//kbuild:lib-$(CONFIG_PASTE) += paste.o
> +
> +//usage:#define paste_trivial_usage
> +//usage:       "[OPTIONS] [FILE]..."
> +//usage:#define paste_full_usage "\n\n"
> +//usage:       "Paste lines from each input files together using a 
> tabulation character\n"
> +//usage:     "\n     -d LIST use delimiters from LIST instead of tabulations"
> +//usage:     "\n     -s      paste lines of each input files separately"
> +//usage:
> +//usage:#define paste_example_usage
> +//usage:       "# write out directory in four columns\n"
> +//usage:       "$ ls | paste - - - -\n"
> +//usage:       "# combine pairs of lines from a file into single lines\n"
> +//usage:       "$ paste -s -d '\t\n' file\n"
> +
> +#include "libbb.h"
> +
> +static const char optstring[] = "d:s";
> +#define PASTE_OPT_DELIMITERS (1 << 0)
> +#define PASTE_OPT_SEPARATE   (1 << 1)
> +
> +#define PASTE_MAX_FILES 32
> +
> +static char parse_escaped_delimiter(char c)
> +{
> +     switch (c) {
> +             case 'n': return '\n';
> +             case 't': return '\t';
> +             case '\\': return '\\';
> +             case '0': return 0;
> +     }
> +     bb_error_msg_and_die("invalid escaped delimiter %c", c);
> +     return 0;
> +}
> +
> +static char get_next_delimiter(char* delimiters, char** current)
> +{
> +     char res = **current;
> +     if (res == '\\')
> +             res = parse_escaped_delimiter(*(++(*current)));
> +     if (*(++(*current)) == 0)
> +             *current = delimiters;
> +     return res;
> +}
> +
> +static void paste_files(FILE** files, int file_count, char* delimiters)
> +{
> +     char *lines[PASTE_MAX_FILES];
> +     char *current_delimiter;
> +     char delim;
> +     int active_files = file_count;
> +
> +     while (active_files > 0) {
> +             current_delimiter = delimiters;
> +             for (int i = 0; i < file_count; ++i) {

IMHO it is better to put 'int i' in the definition section and remove the int 
here.
It make things harder to read (for me), same for the other locations.

> +                     if (files[i] == NULL)
> +                             continue;
> +
> +                     if ((lines[i] = xmalloc_fgetline(files[i])) == NULL) {
improve readability and make two lines:
        lines[i] = xmalloc_fgetline(files[i]);
        if (!lines[i]) ..
> +                             fclose_if_not_stdin(files[i]);
> +                             files[i] = NULL;
> +                             --active_files;
> +                     }
> +             }
> +
> +             if (active_files == 0)
> +                     break;
> +
> +             for (int i = 0; i < file_count; ++i) {
> +                     if (lines[i] != NULL)
> +                     {
> +                             fputs(lines[i], stdout);
> +                             free(lines[i]);
> +                     }
> +
> +                     if (i == file_count-1)
> +                             fputs("\n", stdout);
> +                     else if ((delim = get_next_delimiter(delimiters, 
> &current_delimiter)) != 0)
> +                             fputc(delim, stdout);
> +             }
> +     }
> +}
> +
> +static void paste_files_separate(FILE** files, int file_count, char* 
> delimiters)
> +{
> +     char *line, *next_line;
> +     char *current_delimiter;
> +     int end;
> +
> +     for (int i = 0; i < file_count; ++i) {
> +             line = NULL;
> +             current_delimiter = delimiters;
> +             while ((next_line = xmalloc_fgets(files[i])) != NULL) {
> +                     if (line != NULL) {
> +                             end = strlen(line)-1;
> +                             line[end] = get_next_delimiter(delimiters, 
> &current_delimiter);
> +                             fputs(line, stdout);
> +                             free(line);
> +                     }
> +                     line = next_line;
> +             }
> +             if (line) {
> +                     fputs(line, stdout);
> +                     free(line);
> +             }
> +             fclose_if_not_stdin(files[i]);
> +     }
> +}
> +
> +int paste_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
> +int paste_main(int argc, char **argv)
> +{
> +     char *delimiters;
> +     unsigned opt;
> +     FILE *files[PASTE_MAX_FILES]; // Posix mandates at least 12 file 
> operands
> +
> +     opt = getopt32(argv, optstring, &delimiters);
> +     argc -= optind;
> +     argv += optind;
> +
> +     if (!(opt & PASTE_OPT_DELIMITERS))
> +             delimiters = (char*)"\t";
> +     else if (delimiters[0] == 0)
> +             bb_error_msg_and_die("empty delimiters is not supported");

perhaps you can do this:
        char *delimiters="\t";
then the case 'empty delimiters' can never happen (note: i do not remember 
getopt32
in all details).


> +
> +     if (argc == 0)
> +             files[0] = stdin;
> +     else if (argc <= PASTE_MAX_FILES) {
> +             for (int i = 0; i < argc; ++i) {
> +                     if (!(files[i] = fopen_or_warn_stdin(argv[i])))
> +                             xfunc_die();
> +             }
> +     }
> +     else
> +             bb_error_msg_and_die("this implementation of paste only 
> supports up to %d file operands", PASTE_MAX_FILES);
> +

it is enough to check for valid argc. the rest will follow naturally.

        if (argc >= PASTE_MAX_FILES)
                bb_error_msg_and_die("this implementation of paste only 
supports up to %d file operands", PASTE_MAX_FILES);

        files[0] = stdin;
        for (i = 0; i < argc; ++i) {
                files[i] = fopen_or_warn_stdin(argv[i]);
                if (!files[i])
                        xfunc_die();
        }
        
Perhaps you can replace
        FILE *files[PASTE_MAX_FILES];
with:
        
        FILE **files;
        ....
        files=xzalloc(sizeof(*files)*argc);

then there is even less to check ....

just my two cents, everything is untested ..

re,
 wh


> +     if (opt & PASTE_OPT_SEPARATE)
> +             paste_files_separate(files, argc == 0 ? 1 : argc, delimiters);
> +     else
> +             paste_files(files, argc == 0 ? 1 : argc, delimiters);
> +
> +     fflush_stdout_and_exit(0);
> +}
> diff --git a/docs/posix_conformance.txt b/docs/posix_conformance.txt
> index c0582dc23..8b9112020 100644
> --- a/docs/posix_conformance.txt
> +++ b/docs/posix_conformance.txt
> @@ -22,7 +22,7 @@ POSIX Tools supported only as shell built-ins (ash shell):
>  POSIX Tools not supported:
>    asa, at, batch, bc, c99, command, compress, csplit, ex, fc, file,
>    gencat, getconf, iconv, join, link, locale, localedef, lp, m4,
> -  mailx, newgrp, nl, paste, pathchk, pax, pr, qalter, qdel, qhold, qmove,
> +  mailx, newgrp, nl, pathchk, pax, pr, qalter, qdel, qhold, qmove,
>    qmsg, qrerun, qrls, qselect, qsig, qstat, qsub, tabs, talk, tput,
>    tsort, unlink, uucp, uustat, uux
>  
> @@ -469,6 +469,12 @@ od POSIX options
>    -x              |  no    | no        |
>  od Busybox specific options: None
>  
> +paste POSIX options
> + option           | exists | compliant | remarks
> +  -d list         |  yes   | yes       |
> +  -s              |  yes   | yes       |
> +paste Busybox specific options: None
> +
>  patch POSIX options
>   option           | exists | compliant | remarks
>    -D define       |  no    | no        |
> diff --git a/testsuite/paste/paste b/testsuite/paste/paste
> new file mode 100644
> index 000000000..349b49d49
> --- /dev/null
> +++ b/testsuite/paste/paste
> @@ -0,0 +1,20 @@
> +cat > foo <<EOF
> +foo1
> +foo2
> +foo3
> +EOF
> +
> +cat > bar <<EOF
> +bar1
> +bar2
> +bar3
> +EOF
> +
> +cat > baz <<EOF
> +foo1 bar1
> +foo2 bar2
> +foo3 bar3
> +EOF
> +
> +busybox paste foo bar > qux
> +diff -u baz qux
> diff --git a/testsuite/paste/paste-back-cuted-lines 
> b/testsuite/paste/paste-back-cuted-lines
> new file mode 100644
> index 000000000..4059b3ec5
> --- /dev/null
> +++ b/testsuite/paste/paste-back-cuted-lines
> @@ -0,0 +1,9 @@
> +cat > foo <<EOF
> +this is the first line
> +this is the second line
> +this is the third line
> +EOF
> +cut -b 1-13 -n foo > foo1
> +cut -b 14- -n foo > foo2
> +paste -d '\0' foo1 foo2 > bar
> +cmp foo bar
> diff --git a/testsuite/paste/paste-multi-stdin 
> b/testsuite/paste/paste-multi-stdin
> new file mode 100644
> index 000000000..834c0c52f
> --- /dev/null
> +++ b/testsuite/paste/paste-multi-stdin
> @@ -0,0 +1,16 @@
> +cat > foo <<EOF
> +line1
> +line2
> +line3
> +line4
> +line5
> +line6
> +EOF
> +
> +cat > bar <<EOF
> +line1        line2   line3
> +line4        line5   line6
> +EOF
> +
> +paste - - - < foo > baz
> +cmp bar baz
> diff --git a/testsuite/paste/paste-pairs b/testsuite/paste/paste-pairs
> new file mode 100644
> index 000000000..90725fa87
> --- /dev/null
> +++ b/testsuite/paste/paste-pairs
> @@ -0,0 +1,16 @@
> +cat > foo <<EOF
> +foo1
> +bar1
> +foo2
> +bar2
> +foo3
> +EOF
> +
> +cat > bar <<EOF
> +foo1 bar1
> +foo2 bar2
> +foo3
> +EOF
> +
> +busybox paste -s -d "\t\n" foo > baz
> +cmp bar baz
> diff --git a/testsuite/paste/paste-separate b/testsuite/paste/paste-separate
> new file mode 100644
> index 000000000..40793fb31
> --- /dev/null
> +++ b/testsuite/paste/paste-separate
> @@ -0,0 +1,19 @@
> +cat > foo <<EOF
> +foo1
> +foo2
> +foo3
> +EOF
> +
> +cat > bar <<EOF
> +bar1
> +bar2
> +bar3
> +EOF
> +
> +cat > baz <<EOF
> +foo1 foo2    foo3
> +bar1 bar2    bar3
> +EOF
> +
> +busybox paste -s foo bar > qux
> +cmp baz qux
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to