-
Notifications
You must be signed in to change notification settings - Fork 6
/
dtpstree.cpp
1282 lines (1030 loc) · 26.2 KB
/
dtpstree.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// DT PS Tree
//
// Douglas Thrift
//
// dtpstree.cpp
/* Copyright 2010 Douglas Thrift
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cerrno>
#include <climits>
#include <clocale>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#ifndef __GLIBC__
#include <libgen.h>
#endif
#ifdef HAVE_TERMCAP_H
#include <termcap.h>
#elif defined(HAVE_NCURSES_TERMCAP_H)
#include <ncurses/termcap.h>
#elif defined(HAVE_NCURSES_TERM_H)
#include <ncurses/ncurses.h>
#include <ncurses/term.h>
#elif defined(HAVE_TERM_H)
#include <curses.h>
#include <term.h>
#endif
#include <err.h>
#include <fcntl.h>
#include <getopt.h>
#include <kvm.h>
#include <paths.h>
#include <pwd.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <vis.h>
#include "foreach.hpp"
namespace kvm
{
#if HAVE_DECL_KERN_PROC_PROC
const int All(KERN_PROC_PROC);
#elif HAVE_DECL_KERN_PROC_KTHREAD
const int All(KERN_PROC_KTHREAD);
#else
const int All(KERN_PROC_ALL);
#endif
template <typename Type>
inline Type *getprocs(kvm_t *kd, int &count);
template <typename Type>
inline char **getargv(kvm_t *kd, const Type *proc);
template <typename Type>
inline pid_t pid(Type *proc);
template <typename Type>
inline pid_t ppid(Type *proc);
template <typename Type>
inline uid_t ruid(Type *proc);
template <typename Type>
inline char *comm(Type *proc);
#ifndef HAVE_STRUCT_KINFO_PROC2
#ifdef HAVE_KINFO_NEWABI
typedef kinfo_proc Proc;
const int Flags(KVM_NO_FILES);
template <>
inline kinfo_proc *getprocs(kvm_t *kd, int &count)
{
return kvm_getprocs(kd, All, 0, sizeof (kinfo_proc), &count);
}
template <>
inline char **getargv(kvm_t *kd, const kinfo_proc *proc)
{
return kvm_getargv(kd, proc, 0);
}
#else
typedef kinfo_proc Proc;
const int Flags(O_RDONLY);
template <>
inline kinfo_proc *getprocs(kvm_t *kd, int &count)
{
return kvm_getprocs(kd, All, 0, &count);
}
template <>
inline char **getargv(kvm_t *kd, const kinfo_proc *proc)
{
return kvm_getargv(kd, proc, 0);
}
#endif
#else
typedef kinfo_proc2 Proc;
const int Flags(KVM_NO_FILES);
template <>
inline kinfo_proc2 *getprocs(kvm_t *kd, int &count)
{
return kvm_getproc2(kd, All, 0, sizeof (kinfo_proc2), &count);
}
template <>
inline char **getargv(kvm_t *kd, const kinfo_proc2 *proc)
{
return kvm_getargv2(kd, proc, 0);
}
#endif
template <>
inline pid_t pid(Proc *proc)
{
# ifdef HAVE_STRUCT_KINFO_PROCX_KI_PID
return proc->ki_pid;
# elif defined(HAVE_STRUCT_KINFO_PROCX_KP_PID)
return proc->kp_pid;
# elif defined(HAVE_STRUCT_KINFO_PROCX_P_PID)
return proc->p_pid;
# endif
}
template <>
inline pid_t ppid(Proc *proc)
{
# ifdef HAVE_STRUCT_KINFO_PROCX_KI_PPID
return proc->ki_ppid;
# elif defined(HAVE_STRUCT_KINFO_PROCX_KP_PPID)
return proc->kp_ppid;
# elif defined(HAVE_STRUCT_KINFO_PROCX_P_PPID)
return proc->p_ppid;
# endif
}
template <>
inline uid_t ruid(Proc *proc)
{
# ifdef HAVE_STRUCT_KINFO_PROCX_KI_RUID
return proc->ki_ruid;
# elif defined(HAVE_STRUCT_KINFO_PROCX_KP_RUID)
return proc->kp_ruid;
# elif defined(HAVE_STRUCT_KINFO_PROCX_P_RUID)
return proc->p_ruid;
# endif
}
template <>
inline char *comm(Proc *proc)
{
# ifdef HAVE_STRUCT_KINFO_PROCX_KI_COMM
return proc->ki_comm;
# elif defined(HAVE_STRUCT_KINFO_PROCX_KP_COMM)
return proc->kp_comm;
# elif defined(HAVE_STRUCT_KINFO_PROCX_P_COMM)
return proc->p_comm;
# endif
}
}
enum Flags
{
Arguments = 0x0001,
Ascii = 0x0002,
NoCompact = 0x0004,
Glob = 0x0008,
Vt100 = 0x0010,
Highlight = 0x0020,
ShowKernel = 0x0040,
Long = 0x0080,
NumericSort = 0x0100,
ShowPids = 0x0200,
Regex = 0x0400,
ShowTitles = 0x0800,
UidChanges = 0x1000,
Unicode = 0x2000,
Pid = 0x4000,
User = 0x8000
};
enum Escape { None, BoxDrawing, Bright };
struct Segment
{
size_t width_;
Escape escape_;
char *string_;
inline Segment(size_t width, Escape escape, char *string) : width_(width), escape_(escape), string_(string) {}
};
struct Branch
{
std::string indentation_;
bool done_;
inline Branch(size_t indentation) : indentation_(indentation, ' '), done_(false) {}
};
class Tree
{
const uint16_t &flags_;
bool vt100_;
wchar_t horizontal_, vertical_, upAndRight_, verticalAndRight_, downAndHorizontal_;
size_t maxWidth_, width_;
bool max_, suppress_;
std::vector<Segment> segments_;
std::vector<Branch> branches_;
bool first_, last_;
size_t duplicate_;
public:
Tree(const uint16_t &flags) : flags_(flags), vt100_(false), maxWidth_(0), width_(0), max_(false), suppress_(false), duplicate_(0)
{
bool tty(isatty(1));
if (flags & Ascii)
{
ascii:
horizontal_ = L'-';
vertical_ = L'|';
upAndRight_ = L'`';
verticalAndRight_ = L'|';
downAndHorizontal_ = L'+';
}
else if (flags & Unicode)
{
unicode:
if (!std::setlocale(LC_CTYPE, ""))
goto vt100;
horizontal_ = L'\x2500';
vertical_ = L'\x2502';
upAndRight_ = L'\x2514';
verticalAndRight_ = L'\x251c';
downAndHorizontal_ = L'\x252c';
wchar_t wides[] = { horizontal_, vertical_, upAndRight_, verticalAndRight_, downAndHorizontal_ };
char *buffer = new char[MB_CUR_MAX];
for (int index(0); index != sizeof (wides) / sizeof (*wides); ++index)
{
int size;
if ((size = std::wctomb(buffer, wides[index])) == -1)
{
delete [] buffer;
goto vt100;
}
wchar_t wide;
if (std::mbtowc(&wide, buffer, size) == -1)
{
delete [] buffer;
goto vt100;
}
if (wide != wides[index])
{
delete [] buffer;
goto vt100;
}
}
delete [] buffer;
}
else if (flags & Vt100)
{
vt100:
vt100_ = true;
horizontal_ = L'\x71';
vertical_ = L'\x78';
upAndRight_ = L'\x6d';
verticalAndRight_ = L'\x74';
downAndHorizontal_ = L'\x77';
}
else if (tty)
goto unicode;
else
goto ascii;
if (!(flags & Long) && tty)
{
# if !defined(HAVE_TERMCAP_H) && !defined(HAVE_NCURSES_TERMCAP_H)
int code;
if (setupterm(NULL, 1, &code) == OK)
{
maxWidth_ = tigetnum(const_cast<char *>("cols"));
if (tigetflag(const_cast<char *>("am")) && !tigetflag(const_cast<char *>("xenl")))
suppress_ = true;
}
# else
char buffer[1024], *term(std::getenv("TERM"));
if (term != NULL && tgetent(buffer, term) == 1)
{
maxWidth_ = tgetnum("co");
if (tgetflag("am") && !tgetflag("xn"))
suppress_ = true;
}
# endif
else
maxWidth_ = 80;
}
}
void print(const std::string &string, bool highlight, size_t duplicate)
{
Escape escape(vt100_ ? BoxDrawing : None);
if (!first_ || flags_ & Arguments)
{
size_t last(branches_.size() - 1);
_foreach (std::vector<Branch>, branch, branches_)
{
size_t width(branch->indentation_.size() + 2);
if (_index == last)
{
wchar_t line;
if (last_)
{
branch->done_ = true;
line = upAndRight_;
}
else
line = verticalAndRight_;
print(width, escape, "%s%lc%lc", branch->indentation_.c_str(), line, horizontal_);
}
else
print(width, escape, "%s%lc ", branch->indentation_.c_str(), branch->done_ ? ' ' : vertical_);
}
}
else if (branches_.size())
{
wchar_t line;
if (last_)
{
branches_.back().done_ = true;
line = horizontal_;
}
else
line = downAndHorizontal_;
print(3, escape, "%lc%lc%lc", horizontal_, line, horizontal_);
}
size_t size(0);
if (duplicate)
{
std::ostringstream string;
string << duplicate << "*[";
size = string.str().size();
print(size, None, "%s", string.str().c_str());
++duplicate_;
}
print(string.size(), highlight ? Bright : None, "%s", string.c_str());
branches_.push_back(Branch(!(flags_ & Arguments) ? size + string.size() + 1 : 2));
}
inline void printArg(const std::string &arg, bool last)
{
if (max_)
return;
size_t width(arg.size() + 1);
width_ += width;
char *string;
if (maxWidth_ && !(flags_ & Long))
if (width_ > maxWidth_ || !last && width_ + 3 >= maxWidth_)
{
width -= width_ - maxWidth_;
width_ = maxWidth_;
max_ = true;
ssize_t size(static_cast<ssize_t>(width) - 4);
if (size < -3)
return;
else if (size < 1)
{
string = static_cast<char *>(std::malloc(size += 5));
snprintf(string, size, " ...");
}
else
asprintf(&string, " %s...", arg.substr(0, size).c_str());
}
else
goto print;
else
print:
asprintf(&string, " %s", arg.c_str());
segments_.push_back(Segment(width, None, string));
}
inline void pop(bool children)
{
branches_.pop_back();
if (!(flags_ & Arguments) && !children)
done();
}
void done()
{
if (duplicate_)
{
print(duplicate_, None, "%s", std::string(duplicate_, ']').c_str());
duplicate_ = 0;
}
size_t last(segments_.size() - 1);
_foreach (std::vector<Segment>, segment, segments_)
{
const char *begin, *end;
switch (segment->escape_)
{
case BoxDrawing:
begin = !_index || (segment - 1)->escape_ != BoxDrawing ? "\033(0\017" : "";
end = _index == last || (segment + 1)->escape_ != BoxDrawing ? "\033(B\017" : "";
break;
case Bright:
begin = "\033[1m";
end = "\033[22m";
break;
default:
begin = end = ""; break;
}
std::printf("%s%s%s", begin, segment->string_, end);
std::free(segment->string_);
}
segments_.clear();
if (suppress_ && width_ == maxWidth_)
std::fflush(stdout);
else
std::printf("\n");
width_ = 0;
max_ = false;
}
inline Tree &operator()(bool first, bool last)
{
first_ = first;
last_ = last;
return *this;
}
private:
void print(size_t width, Escape escape, const char * format, ...)
{
if (max_)
return;
std::va_list args;
va_start(args, format);
char *string;
vasprintf(&string, format, args);
va_end(args);
width_ += width;
if (maxWidth_ && !(flags_ & Long))
if (width_ > maxWidth_)
{
width -= width_ - maxWidth_;
width_ = maxWidth_;
max_ = true;
bool previous = !width;
if (previous)
{
std::free(string);
const Segment &segment(segments_.back());
width = segment.width_;
string = segment.string_;
}
std::wstring wide(width - 1, '\0');
std::mbstowcs(const_cast<wchar_t *>(wide.data()), string, wide.size());
std::free(string);
wide += L'+';
size_t size(std::wcstombs(NULL, wide.c_str(), 0) + 1);
string = static_cast<char *>(std::malloc(size));
std::wcstombs(string, wide.c_str(), size);
if (previous)
{
segments_.back().string_ = string;
return;
}
}
segments_.push_back(Segment(width, escape, string));
}
};
template <typename Type>
struct Proc
{
typedef std::multimap<pid_t, Proc<Type> *> PidMap;
typedef std::multimap<std::string, Proc<Type> *> NameMap;
private:
const uint16_t &flags_;
kvm_t *kd_;
Type *proc_;
mutable std::string name_, print_;
Proc<Type> *parent_;
PidMap childrenByPid_;
NameMap childrenByName_;
bool highlight_, root_;
int8_t compact_;
size_t duplicate_;
public:
inline Proc(const uint16_t &flags, kvm_t *kd, Type *proc) : flags_(flags), kd_(kd), proc_(proc), parent_(NULL), highlight_(false), root_(false), compact_(-1), duplicate_(0) {}
inline const std::string &name() const
{
if (name_.empty())
name_ = visual(kvm::comm(proc_));
return name_;
}
inline pid_t parent() const { return kvm::ppid(proc_); }
inline pid_t pid() const { return kvm::pid(proc_); }
inline void child(Proc *proc)
{
if (proc == this)
return;
proc->parent_ = this;
childrenByPid_.insert(typename PidMap::value_type(proc->pid(), proc));
childrenByName_.insert(typename NameMap::value_type(proc->name(), proc));
}
inline void highlight()
{
highlight_ = true;
if (parent_)
parent_->highlight();
}
inline bool compact()
{
if (compact_ == -1)
compact_ = compact(childrenByName_);
return compact_;
}
bool root(uid_t uid)
{
if (flags_ & User)
{
if (uid == this->uid())
{
Proc *parent(parent_);
while (parent)
{
if (parent->uid() == uid)
return false;
parent = parent->parent_;
}
return root_ = true;
}
return false;
}
return root_ = !parent_;
}
inline void printByPid(Tree &tree) const
{
print(tree, childrenByPid_);
}
inline void printByName(Tree &tree) const
{
print(tree, childrenByName_);
}
static bool compact(NameMap &names)
{
Proc *previous(NULL);
bool compact(true);
_tforeach (NameMap, name, names)
{
Proc *proc(name->second);
if (proc->duplicate_)
continue;
size_t duplicate(proc->compact());
if (compact && duplicate && (!previous || proc->print() == previous->print()))
previous = proc;
else
compact = false;
size_t count(names.count(name->first));
if (!duplicate || count == 1)
continue;
_forall(typename NameMap::iterator, n4me, (++name)--, names.upper_bound(name->first))
{
Proc *pr0c(n4me->second);
if (pr0c->compact() && Proc::compact(proc, pr0c))
duplicate += ++pr0c->duplicate_;
}
if (duplicate != 1)
proc->duplicate_ = duplicate;
}
return compact;
}
private:
inline std::string visual(const char *string) const
{
std::string visual(std::strlen(string) * 4 + 1, '\0');
visual.resize(strvis(const_cast<char *>(visual.data()), string, VIS_TAB | VIS_NL | VIS_NOSLASH));
return visual;
}
template <typename Map>
void print(Tree &tree, const Map &children) const
{
if (duplicate_ == 1)
return;
print(tree);
size_t size(children.size()), last(size - 1);
_tforeach (const Map, child, children)
{
Proc<Type> *proc(child->second);
bool l4st(_index + (proc->duplicate_ ? proc->duplicate_ - 1 : 0) == last);
if (!l4st)
{
l4st = true;
for (++child; child != _end; ++child)
if (child->second->duplicate_ != 1)
{
l4st = false;
break;
}
--child;
}
proc->print(tree(!_index, l4st), proc->template children<Map>());
if (l4st)
break;
}
tree.pop(size);
}
void print(Tree &tree) const
{
tree.print(print(), highlight_, duplicate_);
if (flags_ & Arguments)
{
char **argv(kvm::getargv(kd_, proc_));
if (argv && *argv)
for (++argv; *argv; ++argv)
tree.printArg(visual(*argv), !*(argv + 1));
tree.done();
}
}
const std::string &print() const
{
if (print_.empty())
{
std::ostringstream print;
if (flags_ & ShowTitles)
{
char **argv(kvm::getargv(kd_, proc_));
if (argv)
print << visual(*argv);
else
print << name();
}
else
print << name();
bool p1d(flags_ & ShowPids), args(flags_ & Arguments);
bool change(flags_ & UidChanges && (root_ ? !(flags_ & User) && uid() : parent_ && uid() != parent_->uid()));
bool parens((p1d || change) && !args);
if (parens)
print << '(';
if (p1d)
{
if (!parens)
print << ',';
print << pid();
}
if (change)
{
if (!parens || p1d)
print << ',';
passwd *user(getpwuid(uid()));
print << user->pw_name;
}
if (parens)
print << ')';
print_ = print.str();
}
return print_;
}
inline uid_t uid() const { return kvm::ruid(proc_); }
template <typename Map>
inline const Map &children() const;
inline bool hasChildren() const { return childrenByName_.size(); }
inline Proc<Type> *child() const { return childrenByName_.begin()->second; }
inline static bool compact(Proc<Type> *one, Proc<Type> *two)
{
if (one->print() != two->print())
return false;
if (one->hasChildren() != two->hasChildren())
return false;
if (one->hasChildren() && !compact(one->child(), two->child()))
return false;
if (two->highlight_)
one->highlight_ = true;
return true;
}
};
template <> template <>
inline const Proc<kvm::Proc>::PidMap &Proc<kvm::Proc>::children() const
{
return childrenByPid_;
}
template <> template <>
inline const Proc<kvm::Proc>::NameMap &Proc<kvm::Proc>::children() const
{
return childrenByName_;
}
static void help(char *program, option options[], int code = 0)
{
std::printf("Usage: %s [options] [PID|USER]\n\nOptions:\n", basename(program));
for (option *option(options); option->name; ++option)
{
std::string name(option->name);
std::ostringstream arguments;
switch (option->val)
{
case 'g':
case 'r':
arguments << '-' << static_cast<char>(option->val) << "PATTERN, --" << name << "=PATTERN"; break;
case 'H':
if (name != "highlight")
continue;
arguments << "-H[PID], --highlight[=PID]"; break;
case 0:
if (name == "pid")
arguments << "PID, --pid=PID";
else if (name == "user")
arguments << "USER, --user=USER";
else
goto argument;
break;
case 'c':
if (name != "no-compact")
continue;
default:
arguments << '-' << static_cast<char>(option->val) << ", ";
argument:
arguments << "--" << name;
}
const char *description("");
switch (option->val)
{
case 'a':
description = "show command line arguments"; break;
case 'A':
description = "use ASCII line drawing characters"; break;
case 'c':
description = "don't compact identical subtrees"; break;
case 'g':
description = "show only trees rooted at processes with names\n that match the shell pattern PATTERN"; break;
case 'G':
description = "use VT100 line drawing characters"; break;
case 'h':
description = "show this help message and exit"; break;
case 'H':
description = "highlight the current process (or PID) and its\n ancestors"; break;
case 'k':
description = "show kernel processes"; break;
case 'l':
description = "don't truncate long lines"; break;
case 'n':
description = "sort output by PID"; break;
case 'p':
description = "show PIDs; implies -c"; break;
case 'r':
description = "show only trees rooted at processes with names\n that match the regular expression PATTERN"; break;
case 's':
description = "show parents of the selected process"; break;
case 't':
description = "show process titles"; break;
case 'u':
description = "show uid transitions"; break;
case 'U':
description = "use Unicode line drawing characters"; break;
case 'V':
description = "show version information and exit"; break;
case 0:
if (name == "pid")
description = "show only the tree rooted at the process PID";
else if (name == "user")
description = "show only trees rooted at processes of USER";
}
std::printf(" %-27s %s\n", arguments.str().c_str(), description);
}
std::exit(code);
}
template <typename Type, long minimum, long maximum>
static Type value(char *program, option options[], bool *success = NULL)
{
char *end;
long value(std::strtol(optarg, &end, 0));
errno = 0;
if (optarg == end || *end != '\0')
if (success)
*success = false;
else
{
warnx("Number is invalid: \"%s\"", optarg);
help(program, options, 1);
}
else if (value < minimum || value == LONG_MIN && errno == ERANGE)
{
warnx("Number is too small: \"%s\"", optarg);
help(program, options, 1);
}
else if (value > maximum || value == LONG_MAX && errno == ERANGE)
{
warnx("Number is too large: \"%s\"", optarg);
help(program, options, 1);
}
else if (success)
*success = true;
return value;
}
static uint16_t options(int argc, char *argv[], char *&glob, pid_t &hpid, pid_t &pid, char *®ex, char *&user)
{
option options[] = {
{ "arguments", no_argument, NULL, 'a' },
{ "ascii", no_argument, NULL, 'A' },
{ "compact", no_argument, NULL, 'c' },
{ "no-compact", no_argument, NULL, 'c' },
{ "glob", required_argument, NULL, 'g' },
{ "vt100", no_argument, NULL, 'G' },
{ "help", no_argument, NULL, 'h' },
{ "highlight", optional_argument, NULL, 'H' },
{ "highlight-all", no_argument, NULL, 'H' },
{ "highlight-pid", required_argument, NULL, 'H' },
{ "show-kernel", no_argument, NULL, 'k' },
{ "long", no_argument, NULL, 'l' },
{ "numeric-sort", no_argument, NULL, 'n' },
{ "show-pids", no_argument, NULL, 'p' },
{ "regex", required_argument, NULL, 'r' },
{ "show-parents", no_argument, NULL, 's' },
{ "show-titles", no_argument, NULL, 't' },