From c0dedd05e81ac03a1793abd8cbfacf8c546e976f Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 24 Jan 2005 07:00:02 +0000 Subject: [PATCH] Sort rewrite to be SUSv3 compliant. New config option, updated help, and a couple of infrastructure bits. --- coreutils/Config.in | 12 ++ coreutils/sort.c | 350 +++++++++++++++++++++++++++++++++++++-------- include/libbb.h | 1 + include/usage.h | 50 +++++-- libbb/get_line_from_file.c | 8 +- 5 files changed, 351 insertions(+), 70 deletions(-) diff --git a/coreutils/Config.in b/coreutils/Config.in index e1f0516..4aff5ce 100644 --- a/coreutils/Config.in +++ b/coreutils/Config.in @@ -398,6 +398,18 @@ config CONFIG_SORT help sort is used to sort lines of text in specified files. +config CONFIG_SORT_BIG + bool " full SuSv3 compliant sort (Support -ktcsbdfiozgM)" + default y + depends on CONFIG_SORT + help + Without this, sort only supports -r, -u, and an integer version + of -n. Selecting this adds sort keys, floating point support, and + more. This adds a little over 3k to a nonstatic build on x86. + + The SuSv3 sort standard is available at: + http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html + config CONFIG_STTY bool "stty" default n diff --git a/coreutils/sort.c b/coreutils/sort.c index 8cc4d88..c701b5e 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c @@ -1,8 +1,8 @@ /* vi: set sw=4 ts=4: */ /* - * Mini sort implementation for busybox + * SuS3 compliant sort implementation for busybox * - * Copyright (C) 2000 by Matt Kraai + * Copyright (C) 2004 by Rob Landley * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,83 +18,321 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * + * See SuS3 sort standard at: + * http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html */ -/* BB_AUDIT SUSv3 _NOT_ compliant -- a number of options are not supported. */ -/* http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html */ - -/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) - * - * Now does proper error checking on i/o. Plus some space savings. - */ - +#include +#include #include #include #include +#include #include #include "busybox.h" -#include "libcoreutils/coreutils.h" -static int compare_ascii(const void *x, const void *y) +int global_flags; + +/* + sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...] + sort -c [-bdfinru][-t char][-k keydef][file] +*/ + +/* These are sort types */ +#define FLAG_n 1 /* Numeric sort */ +#define FLAG_g 2 /* Sort using strtod() */ +#define FLAG_M 4 /* Sort date */ +/* ucsz apply to root level only, not keys. b at root level implies bb */ +#define FLAG_u 8 /* Unique */ +#define FLAG_c 16 /* Check: no output, exit(!ordered) */ +#define FLAG_s 32 /* Stable sort, no ascii fallback at end */ +#define FLAG_z 64 /* Input is null terminated, not \n */ +/* These can be applied to search keys, the previous four can't */ +#define FLAG_b 128 /* Ignore leading blanks */ +#define FLAG_r 256 /* Reverse */ +#define FLAG_d 512 /* Ignore !(isalnum()|isspace()) */ +#define FLAG_f 1024 /* Force uppercase */ +#define FLAG_i 2048 /* Ignore !isprint() */ +#define FLAG_bb 32768 /* Ignore trailing blanks */ + + +#ifdef CONFIG_SORT_BIG +char key_separator; + +struct sort_key { - return strcmp(*(char **)x, *(char **)y); -} + struct sort_key *next_key; /* linked list */ + unsigned short range[4]; /* start word, start char, end word, end char */ + int flags; +} *key_list; -static int compare_numeric(const void *x, const void *y) +static char *get_key(char *str, struct sort_key *key, int flags) { - int z = atoi(*(char **)x) - atoi(*(char **)y); - return z ? z : strcmp(*(char **)x, *(char **)y); + int start=0,end,len,i,j; + + /* Special case whole string, so we don't have to make a copy */ + if(key->range[0]==1 && !key->range[1] && !key->range[2] && !key->range[3] + && !(flags&(FLAG_b&FLAG_d&FLAG_f&FLAG_i&FLAG_bb))) return str; + /* Find start of key on first pass, end on second pass*/ + len=strlen(str); + + for(j=0;j<2;j++) { + if(!key->range[2*j]) end=len; + /* Loop through fields */ + else { + end=0; + for(i=1;irange[2*j]+j;i++) { + /* Skip leading blanks or first separator */ + if(str[end]) { + if(key_separator) { + if(str[end]==key_separator) end++; + } else if(isspace(str[end])) + while(isspace(str[end])) end++; + } + /* Skip body of key */ + for(;str[end];end++) { + if(key_separator) { + if(str[end]==key_separator) break; + } else if(isspace(str[end])) break; + } + } + } + if(!j) start=end; + } + /* Key with explicit separator starts after separator */ + if(key_separator && str[start]==key_separator) start++; + /* Strip leading whitespace if necessary */ + if(flags&FLAG_b) while(isspace(str[start])) start++; + /* Strip trailing whitespace if necessary */ + if(flags&FLAG_bb) while(end>start && isspace(str[end-1])) end--; + /* Handle offsets on start and end */ + if(key->range[3]) { + end+=key->range[3]-1; + if(end>len) end=len; + } + if(key->range[1]) { + start+=key->range[1]-1; + if(start>len) start=len; + } + /* Make the copy */ + if(endnext_key); + return *pkey=xcalloc(1,sizeof(struct sort_key)); +} - int flags; +#define GET_LINE(fp) (global_flags&FLAG_z) ? bb_get_chunk_from_file(fp) \ + : bb_get_chomped_line_from_file(fp) +#else +#define GET_LINE(fp) bb_get_chomped_line_from_file(fp) +#endif - bb_default_error_retval = 2; +/* Iterate through keys list and perform comparisons */ +static int compare_keys(const void *xarg, const void *yarg) +{ + int flags=global_flags,retval=0; + char *x,*y; - flags = bb_getopt_ulflags(argc, argv, "nru"); - if (flags & 1) { - compare = compare_numeric; - } +#ifdef CONFIG_SORT_BIG + struct sort_key *key; + + for(key=key_list;!retval && key;key=key->next_key) { + flags=(key->flags) ? key->flags : global_flags; + /* Chop out and modify key chunks, handling -dfib */ + x=get_key(*(char **)xarg,key,flags); + y=get_key(*(char **)yarg,key,flags); +#else + /* This curly bracket serves no purpose but to match the nesting + level of the for() loop we're not using */ + { + x=*(char **)xarg; + y=*(char **)yarg; +#endif + /* Perform actual comparison */ + switch(flags&7) { + default: + bb_error_msg_and_die("Unknown sort type."); + break; + /* Ascii sort */ + case 0: + retval=strcmp(x,y); + break; +#ifdef CONFIG_SORT_BIG + case FLAG_g: + { + char *xx,*yy; + double dx=strtod(x,&xx), dy=strtod(y,&yy); + /* not numbers < NaN < -infinity < numbers < +infinity) */ + if(x==xx) retval=(y==yy ? 0 : -1); + else if(y==yy) retval=1; + else if(isnan(dx)) retval=isnan(dy) ? 0 : -1; + else if(isnan(dy)) retval=1; + else if(isinf(dx)) { + if(dx<0) retval=((isinf(dy) && dy<0) ? 0 : -1); + else retval=((isinf(dy) && dy>0) ? 0 : 1); + } else if(isinf(dy)) retval=dy<0 ? 1 : -1; + else retval=dx>dy ? 1 : (dxdy ? 1 : (dx0) { + line=index(optlist,c); + if(!line) bb_show_usage(); + switch(*line) { +#ifdef CONFIG_SORT_BIG + case 'o': + if(outfile) bb_error_msg_and_die("Too many -o."); + outfile=bb_xfopen(optarg,"w"); + break; + case 't': + if(key_separator || optarg[1]) + bb_error_msg_and_die("Too many -t."); + key_separator=*optarg; + break; + /* parse sort key */ + case 'k': + { + struct sort_key *key=add_key(); + char *temp, *temp2; + + temp=optarg; + for(i=0;*temp;) { + /* Start of range */ + key->range[2*i]=(unsigned short)strtol(temp,&temp,10); + if(*temp=='.') + key->range[(2*i)+1]=(unsigned short)strtol(temp+1,&temp,10); + for(;*temp;temp++) { + if(*temp==',' && !i++) { + temp++; + break; + } /* no else needed: fall through to syntax error + because comma isn't in optlist */ + temp2=index(optlist,*temp); + flag=(1<<(temp2-optlist)); + if(!temp2 || (flag>FLAG_M && flagflags|=flag; + } + } + break; + } +#endif + default: + global_flags|=(1<<(line-optlist)); + /* global b strips leading and trailing spaces */ + if(global_flags&FLAG_b) global_flags|=FLAG_bb; + break; } - bb_xferror(fp, *argv); - bb_fclose_nonstdin(fp); - } while (*++argv); - - /* sort it */ - qsort(lines, nlines, sizeof(char *), compare); - - /* print it */ - i = 0; - --nlines; - if ((inc = 1 - (flags & 2)) < 0) { /* reverse */ - i = nlines; } - flags &= 4; - - while (nlines >= 0) { - if (!flags || !nlines || strcmp(lines[i+inc], lines[i])) { - puts(lines[i]); + /* Open input files and read data */ + for(i=argv[optind] ? optind : optind-1;argv[i];i++) { + if(irange[0]=1; + /* handle -c */ + if(global_flags&FLAG_c) { + int j=(global_flags&FLAG_u) ? -1 : 0; + for(i=1;ij) { + fprintf(stderr,"Check line %d\n",i); + return 1; + } + return 0; + } +#endif + /* Perform the actual sort */ + qsort(lines,linecount,sizeof(char *),compare_keys); + /* handle -u */ + if(global_flags&FLAG_u) { + for(flag=0,i=1;i