1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// Copyright (C) 2010 David Sugar, Tycho Softworks.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
#include <ucommon/ucommon.h>
using namespace UCOMMON_NAMESPACE;
static shell::flagopt helpflag('h',"--help", _TEXT("display this list"));
static shell::flagopt althelp('?', NULL, NULL);
static shell::charopt delim('d', "--delim", _TEXT("set deliminter between arguments"));
static shell::flagopt directory('D', "--directory", _TEXT("expand directory into file arguments"));
static shell::flagopt lines('l', "--lines", _TEXT("list arguments on separate lines"));
static shell::stringopt quote('q', "--quote", _TEXT("set quote for each argument"), "string", "");
static shell::flagopt recursive('R', "--recursive", _TEXT("recursive directory scan"));
static shell::flagopt follow('F', "--follow", _TEXT("follow symlinks"));
static shell::flagopt rflag('r', "--reverse", _TEXT("reverse order of arguments"));
static char prefix[80] = {0, 0};
static char suffix[80] = {0, 0};
static void output(bool middle, const char *arg)
{
if(is(lines))
shell::printf("%s%s%s\n", prefix, arg, suffix);
else if(middle)
shell::printf("%c%s%s%s", *delim, prefix, arg, suffix);
else
shell::printf("%s%s%s", prefix, arg, suffix);
}
static void dirpath(bool middle, String path, bool top = true)
{
char filename[128];
String subdir;
fsys_t dir(path, fsys::ACCESS_DIRECTORY);
unsigned count = 0;
while(is(dir) && fsys::read(dir, filename, sizeof(filename))) {
if(*filename == '.')
continue;
++count;
subdir = (String)path + (String)"/" + (String)filename;
output(middle, subdir);
middle = true;
if(fsys::isdir(*subdir)) {
if(is(follow) || is(recursive)) {
if(!fsys::islink(*subdir) || is(follow))
dirpath(true, subdir, false);
}
}
}
if(top && !count)
output(middle, path);
}
PROGRAM_MAIN(argc, argv)
{
unsigned count = 0;
char *ep;
bool middle = false;
shell::bind("args");
shell args(argc, argv);
if(is(helpflag) || is(althelp)) {
printf("%s\n", _TEXT("Usage: args [options] arguments..."));
printf("%s\n\n", _TEXT("Echo command line arguments"));
printf("%s\n", _TEXT("Options:"));
shell::help();
printf("\n%s\n", _TEXT("Report bugs to dyfet@gnu.org"));
PROGRAM_EXIT(0);
}
if(!args())
PROGRAM_EXIT(0);
if(quote[0]) {
if(!quote[1]) {
prefix[0] = quote[0];
suffix[0] = quote[0];
}
else if(!quote[2]) {
prefix[0] = quote[0];
suffix[0] = quote[1];
}
else if(quote[0] == '<') {
String::set(prefix, sizeof(prefix), *quote);
snprintf(suffix, sizeof(suffix), "</%s", *quote + 1);
}
else if(quote[0] == '(') {
String::set(prefix, sizeof(prefix), quote);
ep = strchr((char *)*quote, ')');
if(ep)
*ep = 0;
suffix[0] = ')';
}
else {
String::set(prefix, sizeof(prefix), quote);
String::set(suffix, sizeof(suffix), quote);
}
}
if(is(rflag)) {
count = args();
while(count--) {
if(fsys::isdir(args[count]) && (is(directory) || is(recursive) || is(follow)))
dirpath(middle, (String)args[count]);
else
output(middle, args[count]);
middle = true;
}
}
else while(count < args()) {
if(fsys::isdir(args[count]) && (is(directory) || is(recursive) || is(follow)))
dirpath(middle, (String)args[count++]);
else
output(middle, (String)args[count++]);
middle = true;
}
if(!lines)
shell::printf("\n");
PROGRAM_EXIT(0);
}
|