ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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