Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012, 2014 Jonas 'Sortie' Termansen.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* column.c
|
|
|
|
* Columnate lists.
|
|
|
|
*/
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-10-19 15:12:12 -04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2012-03-01 08:36:56 -05:00
|
|
|
#include <errno.h>
|
|
|
|
#include <error.h>
|
2014-06-28 13:14:31 -04:00
|
|
|
#include <locale.h>
|
2016-02-28 18:40:20 -05:00
|
|
|
#include <stdbool.h>
|
2014-06-28 13:14:31 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-03-01 08:36:56 -05:00
|
|
|
#include <string.h>
|
2012-07-29 18:35:23 -04:00
|
|
|
#include <termios.h>
|
2014-06-28 13:14:31 -04:00
|
|
|
#include <wchar.h>
|
2012-03-01 08:36:56 -05:00
|
|
|
|
2014-06-28 13:14:31 -04:00
|
|
|
struct line
|
|
|
|
{
|
|
|
|
const char* string;
|
|
|
|
size_t display_width;
|
|
|
|
};
|
2012-03-01 08:36:56 -05:00
|
|
|
|
2014-06-28 13:14:31 -04:00
|
|
|
size_t measure_line_display_width(const char* string)
|
2012-10-22 16:46:23 -04:00
|
|
|
{
|
2014-06-28 13:14:31 -04:00
|
|
|
mbstate_t ps;
|
|
|
|
memset(&ps, 0, sizeof(ps));
|
|
|
|
|
|
|
|
size_t index = 0;
|
|
|
|
size_t result = 0;
|
|
|
|
|
2012-10-22 16:46:23 -04:00
|
|
|
bool escaped = false;
|
2014-06-28 13:14:31 -04:00
|
|
|
|
|
|
|
while ( true )
|
2012-10-22 16:46:23 -04:00
|
|
|
{
|
2014-06-28 13:14:31 -04:00
|
|
|
wchar_t wc;
|
|
|
|
size_t consumed = mbrtowc(&wc, string + index, SIZE_MAX, &ps);
|
|
|
|
if ( consumed == (size_t) -2 )
|
|
|
|
return result + 1;
|
|
|
|
if ( consumed == (size_t) -1 )
|
2012-10-22 16:46:23 -04:00
|
|
|
{
|
2014-06-28 13:14:31 -04:00
|
|
|
index++;
|
|
|
|
if ( !escaped )
|
|
|
|
result++;
|
|
|
|
memset(&ps, 0, sizeof(ps));
|
2012-10-22 16:46:23 -04:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-28 13:14:31 -04:00
|
|
|
if ( consumed == (size_t) 0 )
|
|
|
|
return result;
|
|
|
|
index += consumed;
|
|
|
|
if ( wc == L'\e' )
|
2012-10-22 16:46:23 -04:00
|
|
|
{
|
|
|
|
escaped = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-28 13:14:31 -04:00
|
|
|
if ( escaped )
|
|
|
|
{
|
|
|
|
if ( ('a' <= wc && wc <= 'z') || ('A' <= wc && wc <= 'Z') )
|
|
|
|
escaped = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int wc_width = wcwidth(wc);
|
|
|
|
result += 0 <= wc_width ? wc_width : 1;
|
2012-03-01 08:36:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 13:14:31 -04:00
|
|
|
bool append_lines_from_file(FILE* fp,
|
|
|
|
const char* fpname,
|
|
|
|
struct line** lines_ptr,
|
|
|
|
size_t* lines_used_ptr,
|
|
|
|
size_t* lines_length_ptr,
|
|
|
|
bool flag_empty)
|
2012-03-01 08:36:56 -05:00
|
|
|
{
|
|
|
|
while ( true )
|
|
|
|
{
|
2014-06-28 13:14:31 -04:00
|
|
|
char* string = NULL;
|
|
|
|
size_t string_size;
|
2014-12-28 09:51:49 -05:00
|
|
|
errno = 0;
|
2014-06-28 13:14:31 -04:00
|
|
|
ssize_t string_length = getline(&string, &string_size, fp);
|
|
|
|
if ( string_length < 0 )
|
2012-03-01 08:36:56 -05:00
|
|
|
{
|
2015-05-11 09:49:54 -04:00
|
|
|
free(string);
|
2016-05-15 10:32:38 -04:00
|
|
|
if ( ferror(fp) )
|
|
|
|
{
|
|
|
|
error(0, errno, "getline: `%s'", fpname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-03-01 08:36:56 -05:00
|
|
|
}
|
2016-05-15 10:32:38 -04:00
|
|
|
if ( string[string_length-1] == '\n' )
|
2014-06-28 13:14:31 -04:00
|
|
|
string[--string_length] = '\0';
|
|
|
|
size_t display_width = measure_line_display_width(string);
|
|
|
|
if ( display_width == 0 && !flag_empty )
|
|
|
|
{
|
|
|
|
free(string);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( *lines_used_ptr == *lines_length_ptr )
|
|
|
|
{
|
|
|
|
size_t new_length = 2 * *lines_length_ptr;
|
|
|
|
if ( !new_length )
|
|
|
|
new_length = 64;
|
|
|
|
size_t new_size = sizeof(struct line) * new_length;
|
|
|
|
struct line* new_lines = (struct line*) realloc(*lines_ptr, new_size);
|
|
|
|
if ( !new_lines )
|
|
|
|
{
|
|
|
|
error(0, errno, "realloc lines array");
|
|
|
|
free(string);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*lines_ptr = new_lines;
|
|
|
|
*lines_length_ptr = new_length;
|
|
|
|
}
|
|
|
|
struct line* line = (*lines_ptr) + (*lines_used_ptr)++;
|
|
|
|
line->string = string;
|
|
|
|
line->display_width = display_width;
|
2012-03-01 08:36:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
static void compact_arguments(int* argc, char*** argv)
|
2012-03-01 08:36:56 -05:00
|
|
|
{
|
2014-06-28 13:14:31 -04:00
|
|
|
for ( int i = 0; i < *argc; i++ )
|
2012-03-01 08:36:56 -05:00
|
|
|
{
|
2014-06-28 13:14:31 -04:00
|
|
|
while ( i < *argc && !(*argv)[i] )
|
|
|
|
{
|
|
|
|
for ( int n = i; n < *argc; n++ )
|
|
|
|
(*argv)[n] = (*argv)[n+1];
|
|
|
|
(*argc)--;
|
|
|
|
}
|
2012-03-01 08:36:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
static void help(FILE* fp, const char* argv0)
|
2012-03-01 08:36:56 -05:00
|
|
|
{
|
2014-06-28 13:14:31 -04:00
|
|
|
fprintf(fp, "Usage: %s [OPTION]... FILE...\n", argv0);
|
|
|
|
fprintf(fp, "Columnate lists.\n");
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fprintf(fp, " -c COLUMNS output is formatted for a display COLUMNS wide.\n");
|
|
|
|
fprintf(fp, " -e do not ignore empty lines..\n");
|
|
|
|
fprintf(fp, " --help display this help and exit\n");
|
|
|
|
fprintf(fp, " --version output version information and exit\n");
|
2012-03-01 08:36:56 -05:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
static void version(FILE* fp, const char* argv0)
|
2012-03-01 08:36:56 -05:00
|
|
|
{
|
2013-11-04 07:42:45 -05:00
|
|
|
fprintf(fp, "%s (Sortix) %s\n", argv0, VERSIONSTR);
|
2012-03-01 08:36:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2014-06-28 13:14:31 -04:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
|
|
|
size_t display_columns = 80;
|
|
|
|
#if defined(TIOCGWINSZ)
|
2013-10-19 15:12:12 -04:00
|
|
|
struct winsize ws;
|
|
|
|
if ( ioctl(1, TIOCGWINSZ, &ws) == 0 )
|
2014-06-28 13:14:31 -04:00
|
|
|
display_columns = ws.ws_col;
|
|
|
|
#elif defined(__sortix__)
|
|
|
|
struct winsize ws;
|
|
|
|
if ( tcgetwinsize(1, &ws) == 0 )
|
|
|
|
display_columns = ws.ws_col;
|
2013-10-19 15:12:12 -04:00
|
|
|
#endif
|
2012-07-29 18:35:23 -04:00
|
|
|
|
2014-06-28 13:14:31 -04:00
|
|
|
bool flag_empty = false;
|
2012-03-01 08:36:56 -05:00
|
|
|
|
2014-06-28 13:14:31 -04:00
|
|
|
const char* argv0 = argv[0];
|
2012-03-01 08:36:56 -05:00
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
const char* arg = argv[i];
|
2015-07-29 19:08:49 -04:00
|
|
|
if ( arg[0] != '-' || !arg[1] )
|
2014-06-28 13:14:31 -04:00
|
|
|
continue;
|
2012-03-01 10:59:53 -05:00
|
|
|
argv[i] = NULL;
|
2014-06-28 13:14:31 -04:00
|
|
|
if ( !strcmp(arg, "--") )
|
|
|
|
break;
|
|
|
|
if ( arg[1] != '-' )
|
2012-03-01 08:36:56 -05:00
|
|
|
{
|
2016-02-28 18:40:20 -05:00
|
|
|
char c;
|
|
|
|
while ( (c = *++arg) ) switch ( c )
|
2014-06-28 13:14:31 -04:00
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
{
|
|
|
|
const char* parameter;
|
|
|
|
if ( *(arg + 1) )
|
|
|
|
parameter = arg + 1;
|
|
|
|
else if ( i + 1 == argc )
|
|
|
|
{
|
|
|
|
error(0, 0, "option requires an argument -- '%c'", c);
|
|
|
|
fprintf(stderr, "Try '%s --help' for more information\n", argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parameter = argv[i+1];
|
|
|
|
argv[++i] = NULL;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
char* endptr = (char*) parameter;
|
|
|
|
display_columns = (size_t) strtoul((char*) parameter, &endptr, 10);
|
|
|
|
if ( errno == ERANGE )
|
|
|
|
error(1, 0, "option -c `%s' is out of range", parameter);
|
|
|
|
if ( *endptr )
|
|
|
|
error(1, 0, "option -c `%s' is invalid", parameter);
|
|
|
|
} break;
|
|
|
|
case 'e': flag_empty = true; break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
|
|
|
|
help(stderr, argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( !strcmp(arg, "--help") )
|
|
|
|
help(stdout, argv0), exit(0);
|
|
|
|
else if ( !strcmp(arg, "--version") )
|
|
|
|
version(stdout, argv0), exit(0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: unknown option: %s\n", argv0, arg);
|
|
|
|
help(stderr, argv0);
|
|
|
|
exit(1);
|
2012-03-01 08:36:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-21 19:52:29 -04:00
|
|
|
|
2014-06-28 13:14:31 -04:00
|
|
|
compact_arguments(&argc, &argv);
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
struct line* lines = NULL;
|
|
|
|
size_t lines_used = 0;
|
|
|
|
size_t lines_length = 0;
|
|
|
|
|
2012-03-01 08:36:56 -05:00
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
const char* arg = argv[i];
|
2014-06-28 13:14:31 -04:00
|
|
|
FILE* fp = fopen(arg, "r");
|
|
|
|
if ( !fp )
|
|
|
|
{
|
|
|
|
error(0, errno, "`%s'", arg);
|
|
|
|
success = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( !append_lines_from_file(fp, arg, &lines, &lines_used,
|
|
|
|
&lines_length, flag_empty) )
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
success = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( argc <= 1 )
|
|
|
|
{
|
|
|
|
if ( !append_lines_from_file(stdin, "<stdin>", &lines, &lines_used,
|
|
|
|
&lines_length, flag_empty) )
|
|
|
|
success = false;
|
2012-03-01 08:36:56 -05:00
|
|
|
}
|
|
|
|
|
2014-06-28 13:14:31 -04:00
|
|
|
for ( size_t rows = 1; lines_used != 0; rows++ )
|
2012-03-01 08:36:56 -05:00
|
|
|
{
|
2014-06-28 13:14:31 -04:00
|
|
|
size_t total_columns_width = 0;
|
|
|
|
size_t column_width = 0;
|
|
|
|
size_t row = 0;
|
|
|
|
size_t columns = 0;
|
|
|
|
for ( size_t line_index = 0; line_index < lines_used; line_index++ )
|
|
|
|
{
|
|
|
|
if ( column_width < lines[line_index].display_width )
|
|
|
|
column_width = lines[line_index].display_width;
|
|
|
|
if ( ++row == rows || line_index + 1 == lines_used )
|
|
|
|
{
|
|
|
|
if ( columns != 0 )
|
|
|
|
total_columns_width += 2;
|
|
|
|
total_columns_width += column_width;
|
|
|
|
if ( display_columns < total_columns_width )
|
|
|
|
break;
|
|
|
|
row = 0;
|
|
|
|
column_width = 0;
|
|
|
|
columns++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( display_columns < total_columns_width && columns != 1 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( columns == 1 )
|
|
|
|
rows = lines_used;
|
|
|
|
|
2016-01-07 18:46:44 -05:00
|
|
|
size_t* column_widths = (size_t*) calloc(sizeof(size_t), columns);
|
2014-06-28 13:14:31 -04:00
|
|
|
if ( !column_widths )
|
|
|
|
error(1, errno, "calloc column widths");
|
|
|
|
|
|
|
|
for ( size_t column = 0; column < columns; column++ )
|
|
|
|
{
|
|
|
|
size_t column_width = 0;
|
|
|
|
for ( size_t i = 0; i < rows; i++ )
|
|
|
|
{
|
|
|
|
size_t line_index = column * rows + i;
|
|
|
|
if ( lines_used <= line_index )
|
|
|
|
break;
|
|
|
|
if ( column_width < lines[line_index].display_width )
|
|
|
|
column_width = lines[line_index].display_width;
|
|
|
|
}
|
|
|
|
column_widths[column] = column_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( size_t row = 0; row < rows; row++ )
|
|
|
|
{
|
|
|
|
for ( size_t column = 0; column < columns; column++ )
|
|
|
|
{
|
|
|
|
size_t line_index = column * rows + row;
|
|
|
|
struct line* line = &lines[line_index];
|
|
|
|
if ( lines_used <= line_index )
|
|
|
|
break;
|
|
|
|
if ( column != 0 )
|
|
|
|
printf(" ");
|
|
|
|
printf("%s", line->string);
|
|
|
|
size_t new_line_index = (column + 1) * rows + row;
|
|
|
|
for ( size_t i = line->display_width;
|
|
|
|
i < column_widths[column] && new_line_index < lines_used;
|
|
|
|
i++ )
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(column_widths);
|
|
|
|
|
|
|
|
if ( ferror(stdout) )
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
break;
|
2012-03-01 08:36:56 -05:00
|
|
|
}
|
|
|
|
|
2014-06-28 13:14:31 -04:00
|
|
|
return success ? 0 : 1;
|
2012-03-01 08:36:56 -05:00
|
|
|
}
|