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
// Copyright 2018 oooutlk@outlook.com. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! # pals = Processes' Arguments LiSt
//!
//! The main API is `pals()` which returns `ProcList` if succeeded.
//!
//! ```rust,no_run
//! let proc_list = pals::pals().unwrap();
//! ```
//!
//! ## `ProcList` can be regarded as a list of `procs()`.
//!
//! ```rust,no_run
//! let proc_list = pals::pals().unwrap();
//! let procs = proc_list.procs();
//! ```
//!
//! The following items can be accessed:
//!
//! 1. pid -- Process::pid.
//!
//! 2. ppid -- Process::ppid.
//!
//! 3. nul-terminated arguments in one single string -- Process::arguments.
//!
//! 4. argument iterator -- `Process::argv()`.
//!
//! 5. parent process -- `ProcList::parent_of()`.
//!
//! ```rust,no_run
//! let proc_list = pals::pals().unwrap();
//! let mut procs = proc_list.procs();
//! let first = procs.next().unwrap();
//! println!( "pid:{:?}, ppid:{:?}", first.pid, first.ppid );
//! println!( "arguments:{}", first.arguments );
//! println!( "argv:\n{}", first
//!     .argv()
//!     .enumerate()
//!     .fold( String::new(),
//!         |acc,(i,arg)| format!( "{}\narg #{}:{}", acc, i, arg ))
//! );
//! println!( "parent's pid:{:?}", proc_list.parent_of( first.pid ));
//! ```
//!
//! ## `ProcList` can be regarded as a list of `Proc` trees.
//!
//! Besides items mentioned above, the following extra items can be accessed:
//!
//! 6. parent nodes -- `Proc::parent()`.
//!
//! 7. all child nodes -- `Proc::children()`.
//!
//! ```rust
//! use pals::{Proc, pals};
//!
//! pals().map( |proc_list| {
//!     fn assert_ppid_is_parent_pid( proc: Proc ) {
//!         proc.parent()
//!             .map( |parent| assert_eq!( parent.pid, proc.ppid ));
//!         proc.children()
//!             .for_each( |subproc| assert_ppid_is_parent_pid( subproc ));
//!     }
//!
//!     proc_list
//!         .children()
//!         .for_each( |proc| assert_ppid_is_parent_pid( proc ))
//! }).unwrap();
//! ```
//!
//! ## `ProcList` can be converted to `trees::Forest`.
//!
//! ```rust,no_run
//! use pals::{Process, pals};
//! use trees::Forest;
//!
//! let proc_list = pals().unwrap();
//!
//! let bfs = proc_list
//!     .bfs()
//!     .map( ToOwned::to_owned ); // &Process -> Process
//!
//! let forest = Forest::<Process>::from( bfs );
//! ```
//!
//! # Binary utility
//!
//! See README.md for more.

use anyhow::anyhow;

#[cfg( unix )]
use anyhow::Context;

#[cfg( target_os = "freebsd" )]
use freebsd::parse_id;

#[cfg( target_os = "freebsd" )]
use serde::{Deserialize, de};

use std::{
    fmt::{self, Debug, Display},
    mem,
    ops::Deref,
    str,
};

#[cfg( not( target_os = "windows" ))]
use std::process::Command;

use trees::bfs::{BfsForest, Split, Splitted};

/// Process ID.
#[derive( Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord )]
#[cfg_attr( target_os = "freebsd", derive( Deserialize ))]
pub struct Pid( pub u32 );

#[cfg( target_os = "freebsd" )]
mod freebsd;
#[cfg( target_os = "freebsd" )]
use freebsd::{PsOutput, pals_from_procfs};

#[cfg( target_os = "linux" )]
mod linux;
#[cfg( target_os = "linux" )]
use linux::pals_from_procfs;

#[cfg( windows )]
mod windows;

/// Process list. It can be viewed as a list of process or a list of process
/// trees.
#[derive( Debug )]
#[cfg_attr( target_os = "freebsd", derive( Deserialize ))]
pub struct ProcList {
    #[cfg_attr( target_os = "freebsd", serde( rename = "process" ))]
    procs : Vec<ProcNode>,
    #[cfg_attr( target_os = "freebsd", serde( skip ))]
    root  : Link,
}

impl Default for ProcList {
    fn default() -> Self { ProcList{ procs: Vec::new(), root: Link::default() }}
}

impl ProcList {
    /// Gets parent process' pid of the process the pid of which is given.
    pub fn parent_of( &self, pid: Pid ) -> Option<&Process> {
        self.locate( pid ).and_then( |index| {
            let process = &self.procs[ index ].process;
            if process.pid != process.ppid {
                Some( process )
            } else {
                None
            }
        })
    }

    fn locate( &self, pid: Pid ) -> Option<usize> {
        self.procs.binary_search_by_key( &pid, ProcNode::id ).ok()
    }

    fn adopt( &mut self, parent: Option<usize>, child: usize ) -> anyhow::Result<()> {
        let mut index = parent;
        let child_pid = self.procs[ child ].pid;
        let node_count = self.procs[ child ].link.size.descendants + 1;
        while let Some( i ) = index {
            self.procs[i].link.size.descendants += node_count;
            let pid = self.procs[i].pid;
            if pid == self.procs[i].ppid {
                break;
            } else if pid == child_pid {
                return Err( anyhow!( "error: {:?} caused circuit in process tree.", pid ));
            }
            index = self.locate( self.procs[i].ppid );
        }

        match parent {
            Some( parent ) => {
                self.procs[ parent ].link.size.degree += 1;
                self.procs[ child ].link.sib = self.procs[ parent ].link.child;
                self.procs[ child ].link.parent = parent;
                self.procs[ parent ].link.child = child;
            },
            None => {
                self.root.size.degree += 1;
                self.procs[ child ].link.sib = self.root.child;
                self.root.child = child;
            },
        }

        self.root.size.descendants += node_count;

        Ok(())
    }

    /// Returns all detected running processes.
    ///
    /// # Examples
    ///
    /// ```text
    /// .............
    /// .  ProcList .
    /// .   /   \   .
    /// .  1     4  .
    /// . / \   / \ .
    /// .2   3 5   6.
    /// .............
    /// ```
    ///
    /// This method will returns #1, #2, #3, #4, #5, #6.
    pub fn procs( &self ) -> impl Iterator<Item=&ProcNode> {
        self.procs.iter()
    }

    /// Returns all detected running process trees.
    ///
    /// # Examples
    ///
    /// ```text
    /// .............
    /// .  ProcList .
    /// .   /   \   .
    /// .  1     4  .
    /// . / \   / \ .
    /// .2   3 5   6.
    /// .............
    /// ```
    ///
    /// This method will returns #1 and #4.
    ///
    /// Use `.children()` recursively to get descendant processes, e.g. #2.
    pub fn children<'a, 's:'a>( &'s self ) -> Proc<'a> {
        Proc {
            proc_list : self,
            index     : self.root.child,
            degree    : self.root.size.degree,
        }
    }

    /// Returns processes in breadth first search. This method helps to convert
    /// `ProcList` into `trees::Forest`.
    ///
    ///  See [trees](https://crates.io/crates/trees) for more.
    pub fn bfs<'a, 's:'a>( &'s self ) -> BfsForest<Splitted<Proc<'a>>> {
        BfsForest::from( self.children(), self.root.size )
    }
}

impl Display for ProcList {
    fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result {
        f.write_str( "[" )?;
        let mut children = self.children();
        if let Some( first ) = children.next() {
            first.display( f, 1, true, true )?;
        }
        for child in children {
            child.display( f, 1, false, false )?;
        }
        f.write_str( "]" )
    }
}

/// A process agent by which pid,ppid,arguments/argv, parent and child
/// processes can be accessed.
#[derive( Copy, Clone )]
pub struct Proc<'a> {
    proc_list : &'a ProcList,
    index     : usize,
    degree    : usize,
}

impl<'a> Proc<'a> {
    /// Gets parent process.
    pub fn parent( &self ) -> Option<Self> {
        let parent = self.proc_list.procs[ self.index ].link.parent;
        if parent == Link::null() {
            None
        } else {
            let degree = self.proc_list.procs[ parent ].link.size.degree;
            Some( Proc{ proc_list: self.proc_list, index: parent, degree })
        }
    }

    /// Gets child processes.
    pub fn children( &self ) -> Self {
        let link = &self.proc_list.procs[ self.index ].link;
        Proc {
            proc_list : self.proc_list,
            index     : link.child,
            degree    : link.size.degree,
        }
    }

    fn display( &self, f: &mut fmt::Formatter, ident: usize, is_first_child: bool, is_first_line: bool ) -> fmt::Result {
        if !is_first_line {
            if is_first_child {
                f.write_str("\n")?;
            } else {
                f.write_str(",\n")?;
            }
            for _ in 0..ident {
                f.write_str(" ")?;
            }
        }
        let process = &self.proc_list.procs[ self.index ].process;
        write!( f, "{{cmd: \"{}\", pid:{}"
            , escape8259::escape( &process.command )
            , process.pid.0
        )?;

        let mut ends_with_pid = true;

        let argv = process.argv_to_string();
        if !argv.is_empty() {
            ends_with_pid = false;
            write!( f, ", args:[{}]", argv )?;
        }

        let mut subprocs = self.children();
        if let Some( first ) = subprocs.next() {
            ends_with_pid = false;
            f.write_str( ", subs:[" )?;
            first.display( f, ident+1, true, false )?;
            for proc in subprocs {
                proc.display( f, ident+1, false, false )?;
            }
            f.write_str("]")?;
        }
        if ends_with_pid {
            f.write_str(" ")?;
        }
        f.write_str("}")
    }
}

impl<'a> Iterator for Proc<'a> {
    type Item = Self;

    fn next( &mut self ) -> Option<Self::Item> {
        if self.index == Link::null() {
            None
        } else {
            let index = self.index;
            let link = &self.proc_list.procs[ index ].link;
            self.index = link.sib;
            self.degree -= 1;
            Some( Proc{ proc_list: self.proc_list, index, degree: link.size.degree })
        }
    }

    fn size_hint( &self) -> (usize, Option<usize>) {
        (self.degree, Some(self.degree) )
    }
}

impl<'a> ExactSizeIterator for Proc<'a> {}

impl<'a> Deref for Proc<'a> {
    type Target = Process;

    fn deref( &self ) -> &Process {
        if self.index == Link::null() {
            panic!( "bad deref to `Proc`." );
        }
        &self.proc_list.procs[ self.index ]
    }
}

impl<'a> Debug for Proc<'a> {
    fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result {
        self.deref().fmt( f )
    }
}

impl<'a> Split for Proc<'a> {
    type Item = &'a Process;
    type Iter = Self;

    fn split( self ) -> (Self::Item, Self::Iter, usize) {
        let process = &self.proc_list.procs[ self.index ];
        let descendants = process.link.size.descendants;
        (process, self.children(), descendants)
    }
}

#[doc( hidden )]
#[derive( Debug )]
#[cfg_attr( target_os = "freebsd", derive( Deserialize ))]
pub struct ProcNode {
    #[cfg_attr( target_os = "freebsd", serde( flatten ))]
    pub process : Process,
    #[cfg_attr( target_os = "freebsd", serde( skip ))]
        link    : Link,
}

impl Deref for ProcNode {
    type Target = Process;

    fn deref( &self ) -> &Process { &self.process }
}

/// Process holding pid, ppid, command name and arguments of one or more
///  nul-terminated argument(s).
#[derive( Clone, Debug, PartialEq, Eq )]
#[cfg_attr( target_os = "freebsd", derive( Deserialize ))]
pub struct Process {
    #[cfg_attr( target_os = "freebsd", serde( deserialize_with = "parse_id" ))]
    pub pid       : Pid,
    #[cfg_attr( target_os = "freebsd", serde( deserialize_with = "parse_id" ))]
    pub ppid      : Pid,
    pub command   : String,
    pub arguments : String,
}

impl ProcNode {
    fn id( &self ) -> Pid { self.pid }

    pub(crate) fn from( process: Process ) -> Self { ProcNode{ process, link: Link::default() }}
}

fn argv_iter( argv: &str ) -> impl Iterator<Item=&str> {
    argv.trim_end_matches('\0').split( '\0' )
}

impl Process {
    /// Returns arguments iterator.
    pub fn argv( &self ) -> impl Iterator<Item=&str> {
        argv_iter( &self.arguments )
    }

    fn argv_to_string( &self ) -> String {
        let mut s = String::new();
        if !self.arguments.is_empty() {
            let mut argv = self.argv();
            if let Some( arg ) = argv.next() {
                s.push_str( &format!( " \"{}\"", escape8259::escape( arg )));
                for arg in argv {
                    s.push_str( &format!( ", \"{}\"", escape8259::escape( arg )));
                }
                s.push(' ');
            }
        }
        s
    }
}

#[derive( Debug )]
struct Link {
    size   : trees::Size,
    parent : usize,
    child  : usize,
    sib    : usize,
}

impl Link {
    const fn null() -> usize { std::usize::MAX }
}

impl Default for Link {
    fn default() -> Self {
        Link {
            parent : Link::null(),
            child  : Link::null(),
            sib    : Link::null(),
            size   : trees::Size::default(),
        }
    }
}

/// Dumps running processes' arguments into a list/forest.
#[cfg( target_os = "freebsd" )]
pub fn pals() -> anyhow::Result<ProcList> {
    pals_from_procfs( "/proc" )
        .or_else( |_| pals_from_ps_json() )
        .or_else( |_| pals_from_ps_ww() )
}

/// Dumps running processes' arguments into a list/forest.
#[cfg( target_os = "linux" )]
pub fn pals() -> anyhow::Result<ProcList> {
    pals_from_procfs( "/proc" )
        .or_else( |_| pals_from_ps_ww() )
}

/// Dumps running processes' arguments into a list/forest.
#[cfg( target_os = "windows" )]
pub fn pals() -> anyhow::Result<ProcList> {
    windows::pals_from_wmic()
}

/// Dumps running processes' arguments into a list/forest.
#[cfg( not( any( target_os = "freebsd",  target_os = "linux",  target_os = "windows" )))]
pub fn pals() -> anyhow::Result<ProcList> {
    pals_from_ps_ww()
}

#[cfg( not( target_os = "windows" ))]
pub(crate) fn pals_from_ps_ww() -> anyhow::Result<ProcList> {
    let output = String::from_utf8(
        Command::new("ps")
            .args( &[ "awwo", "pid,ppid,comm,args" ])
            .output()?
            .stdout
    )?;
    from_table( &output )
}

#[cfg( target_os = "freebsd" )]
fn pals_from_ps_json() -> anyhow::Result<ProcList> {
    let output = String::from_utf8(
        Command::new("ps")
            .args( &[ "a", "--libxo", "json", "-o", "pid,ppid,comm,args" ])
            .output()?
            .stdout
    )?;
    from_json( &output )
}

#[cfg( target_os = "freebsd" )]
fn from_json( json: &str ) -> anyhow::Result<ProcList> {
    let ps_output: PsOutput = serde_json::from_str( json )?;

    build_tree( ps_output.proc_list, ArgvStatus::Parsing )
}

#[cfg( unix )]
fn from_table( table: &str ) -> anyhow::Result<ProcList> {
    let mut lines = table.lines();
    let header = lines.next().context("ps command should generate some output.")?;

    let ppid_col = header.find(" PID")
        .context( "PID column should be generated by ps command.")? + 4;
    let comm_col = header.find(" PPID")
        .context("PPID column should be generated by ps command.")? + 5;
    let args_col = header.rfind("COMMAND")
        .context("COMMAND column should be generated by ps command.")?;

    let procs = lines.try_fold( Vec::new(), |mut procs, line| -> anyhow::Result<Vec<ProcNode>> {
        let bytes     = line.as_bytes();
        let pid       = Pid( u32::from_str_radix( str::from_utf8( &bytes[         ..ppid_col ])?.trim(), 10 )? );
        let ppid      = Pid( u32::from_str_radix( str::from_utf8( &bytes[ ppid_col..comm_col ])?.trim(), 10 )? );
        let command   = str::from_utf8( &bytes[ comm_col..args_col ])?.trim().to_owned();
        let arguments = str::from_utf8( &bytes[ args_col..         ])?.trim().to_owned();

        procs.push( ProcNode::from( Process{ pid, ppid, command, arguments }));
        Ok( procs )
    })?;

    build_tree( ProcList{ procs, root: Link::default() }, ArgvStatus::Parsing )
}

enum ArgvStatus {
    Parsing,
    #[cfg( unix )]
    Splitted,
}

impl ArgvStatus {
    fn needs_parsing( &self ) -> bool {
        match self {
            ArgvStatus::Parsing  => true,
            #[cfg( unix )]
            ArgvStatus::Splitted => false,
        }
    }
}

fn build_tree( mut proc_list: ProcList, argv_status: ArgvStatus ) -> anyhow::Result<ProcList> {
    proc_list.procs.sort_unstable_by_key( ProcNode::id );

    for i in 0..proc_list.procs.len() {
        let parent = if proc_list.procs[i].pid != proc_list.procs[i].ppid {
            proc_list.locate( proc_list.procs[i].ppid )
        } else {
            None
        };
        proc_list.adopt( parent, i )?;

        if argv_status.needs_parsing() {
            let arguments = &mut proc_list.procs[i].process.arguments;

            #[cfg( unix )]
            {
                let mut args = String::with_capacity( arguments.len() + 8 );
                mem::swap( &mut args, arguments );
                let mut splitted_args = cmdline_words_parser::parse_posix( &mut args );
                while let Some( arg ) = splitted_args.next() {
                    arguments.push_str( arg );
                    arguments.push( '\0' );
                }
            }

            #[cfg( windows )]
            {
                let mut splitted_argv = win_argv( arguments );
                mem::swap( arguments, &mut splitted_argv );
            }
        }
    }
    Ok( proc_list )
}

/// Splits command line into arguments, aka argv, using rules defined on Windows
/// platform. See this
/// [doc](https://docs.microsoft.com/en-us/previous-versions/17w5ykft(v=vs.85))
/// for more.
pub fn win_argv( cmdline: &str ) -> String {
    let mut argv = String::new();

    enum Status {
        Backslashes( usize ),
        Normal,
        Spaces,
    }

    let mut arg = String::new();
    let mut status = Status::Normal;
    let mut quoting = false;

    for ch in cmdline.chars() {
        match status {
            Status::Backslashes( ref mut count ) => match ch {
                '\\' => *count += 1,
                '"' => {
                    for _ in 0..(*count/2) {
                        arg.push('\\');
                    }
                    if *count%2 == 0 {
                        quoting = true;
                    } else {
                        arg.push('"');
                    }
                    status = Status::Normal;
                },
                _ => {
                    for _ in 0..*count {
                        arg.push('\\');
                    }
                    arg.push( ch );
                    status = Status::Normal;
                },
            },
            Status::Normal => match ch {
                '"' => quoting = !quoting,
                ' '| '\t' => {
                    if quoting {
                        arg.push( ch );
                    } else {
                        status = Status::Spaces;
                        argv.push_str( &arg );
                        arg.clear();
                        argv.push('\0');
                    }
                },
                '\\' => status = Status::Backslashes( 1 ),
                _ => arg.push( ch ),
            },
            Status::Spaces => match ch {
                '"' => {
                    status = Status::Normal;
                    quoting = true;
                },
                '\\' => status = Status::Backslashes( 1 ),
                ' ' | '\t' => (),
                _ => {
                    status = Status::Normal;
                    arg.push( ch );
                },
            },
        }
    }

    if !arg.is_empty() {
        argv.push_str( &arg );
        argv.push('\0');
    }

    argv
}

#[cfg( test )]
mod tests {
    #[cfg( target_os = "windows" )]
    use crate::windows::from_utf16_file;

    use std::{
        env,
        path::{Path, PathBuf},
    };

    use super::*;

    #[cfg( target_os = "freebsd" )]
    const JSON: &'static str = r#"{
        "process-information": {
            "process": [
                {
                    "pid": "1004",
                    "ppid": "1002",
                    "command": "alphabet",
                    "arguments": "alphabet --alpha 0 --beta 1 --gamma 2"
                },
                {
                    "pid": "1005",
                    "ppid": "1001",
                    "command": "cargo",
                    "arguments": "cargo build --no-default-features --featurues \"nigthly no_std\""
                },
                {
                    "pid": "1002",
                    "ppid": "1001",
                    "command": "cargo",
                    "arguments": "cargo check"
                },
                {
                    "pid": "1003",
                    "ppid": "1002",
                    "command": "foo",
                    "arguments": "foo -bar -baz"
                },

                {
                    "pid": "1006",
                    "ppid": "1005",
                    "command": "ping",
                    "arguments": "ping 192.168.1.1"
                },
                {
                    "pid": "1007",
                    "ppid": "1005",
                    "command": "ps",
                    "arguments": "ps aux"
                },
                {
                    "pid": "1001",
                    "ppid": "999",
                    "command": "tcsh",
                    "arguments": "-tcsh (tcsh)"
                }
            ]
        }
}"#;

    #[cfg( unix )]
    const TABLE: &'static str = r#" PID PPID COMMAND  COMMAND
1004 1002 alphabet alphabet --alpha 0 --beta 1 --gamma 2
1005 1001 cargo    cargo build --no-default-features --featurues "nigthly no_std"
1002 1001 cargo    cargo check
1003 1002 foo      foo -bar -baz
1006 1005 ping     ping 192.168.1.1
1007 1005 ps       ps aux
1001  999 tcsh     -tcsh (tcsh)"#;

    #[cfg( unix )]
    fn fake_procfs() -> PathBuf {
        let manifest_dir = env::var( "CARGO_MANIFEST_DIR" ).unwrap();

        #[cfg( target_os = "freebsd" )]
        let path = Path::new( &manifest_dir ).join( "test/freebsd/proc" );

        #[cfg( target_os = "linux" )]
        let path = Path::new( &manifest_dir ).join( "test/linux/proc" );

        path
    }

    #[cfg( target_os = "windows" )]
    fn fake_wmic_utf16_file_path() -> PathBuf {
        let manifest_dir = env::var( "CARGO_MANIFEST_DIR" ).unwrap();
        Path::new( &manifest_dir ).join( "test" ).join("windows").join( "pals.output" )
    }

    #[test]
    fn ps() {
        #[cfg( target_os = "freebsd" )]
        assert!( pals_from_ps_json().is_ok() );

        #[cfg( unix )]
        assert!( pals_from_ps_ww().is_ok() );
    }

    #[test]
    fn sort() {
        #[cfg( target_os = "freebsd" )]
        (|| {
            let proc_list = from_json( JSON ).unwrap();
            assert_eq!( proc_list.procs().map( |proc| proc.pid.0 ).collect::<Vec<_>>(),
                vec![ 1001, 1002, 1003, 1004, 1005, 1006, 1007 ]);
        })();

        #[cfg( unix )]
        (|| {
            let proc_list = from_table( TABLE ).unwrap();
            assert_eq!( proc_list.procs().map( |proc| proc.pid.0 ).collect::<Vec<_>>(),
                vec![ 1001, 1002, 1003, 1004, 1005, 1006, 1007 ]);
        })();

        #[cfg( any( target_os = "freebsd", target_os = "linux" ))]
        (|| {
            let proc_list = pals_from_procfs( &fake_procfs() ).unwrap();
            assert_eq!( proc_list.procs().map( |proc| proc.pid.0 ).collect::<Vec<_>>(),
                vec![ 1001, 1002, 1003, 1004, 1005, 1006, 1007 ]);
        })();

        #[cfg( target_os = "windows" )]
        (|| {
            let path = fake_wmic_utf16_file_path();
            let proc_list = from_utf16_file( &path ).unwrap();
            assert_eq!( proc_list.procs().map( |proc| proc.pid.0 ).collect::<Vec<_>>(),
                vec![ 1001, 1002, 1003, 1004, 1005, 1006, 1007 ]);
        })();
    }

    #[test]
    fn bfs() {
        #[cfg( target_os = "freebsd" )]
        (|| {
            let proc_list = from_json( JSON ).unwrap();
            let visits = proc_list.bfs().iter
                .map( |visit| (visit.data.pid.0, visit.size.degree, visit.size.descendants) )
                .collect::<Vec<_>>();
            assert_eq!( visits, vec![
                (1001, 2, 6),
                (1005, 2, 2),
                (1002, 2, 2),
                (1007, 0, 0),
                (1006, 0, 0),
                (1004, 0, 0),
                (1003, 0, 0),
            ]);
        })();

        #[cfg( unix )]
        (|| {
            let proc_list = from_table( TABLE ).unwrap();
            let visits = proc_list.bfs().iter
                .map( |visit| (visit.data.pid.0, visit.size.degree, visit.size.descendants) )
                .collect::<Vec<_>>();
            assert_eq!( visits, vec![
                (1001, 2, 6),
                (1005, 2, 2),
                (1002, 2, 2),
                (1007, 0, 0),
                (1006, 0, 0),
                (1004, 0, 0),
                (1003, 0, 0),
            ]);
        })();

        #[cfg( any( target_os = "freebsd", target_os = "linux" ))]
        (|| {
            let proc_list = pals_from_procfs( &fake_procfs() ).unwrap();
            let visits = proc_list.bfs().iter
                .map( |visit| (visit.data.pid.0, visit.size.degree, visit.size.descendants) )
                .collect::<Vec<_>>();
            assert_eq!( visits, vec![
                (1001, 2, 6),
                (1005, 2, 2),
                (1002, 2, 2),
                (1007, 0, 0),
                (1006, 0, 0),
                (1004, 0, 0),
                (1003, 0, 0),
            ]);
        })();

        #[cfg( target_os = "windows" )]
        (|| {
            let path = fake_wmic_utf16_file_path();
            let proc_list = from_utf16_file( &path ).unwrap();
            let visits = proc_list.bfs().iter
                .map( |visit| (visit.data.pid.0, visit.size.degree, visit.size.descendants) )
                .collect::<Vec<_>>();
            assert_eq!( visits, vec![
                (1001, 2, 6),
                (1005, 2, 2),
                (1002, 2, 2),
                (1007, 0, 0),
                (1006, 0, 0),
                (1004, 0, 0),
                (1003, 0, 0),
            ]);
        })();
    }

    #[test]
    fn forest() {
        macro_rules! process {
            ($pid:expr, $ppid:expr, $comm:expr, $args:expr) => {
                Process{ pid: Pid($pid), ppid: Pid($ppid), command: $comm.to_owned(), arguments: $args.to_owned(), }
            }
        }

        let p1 = &process!( 1001,  999, "tcsh"    , "-tcsh\0(tcsh)\0"                                                    );
        let p5 = &process!( 1005, 1001, "cargo"   , "cargo\0build\0--no-default-features\0--featurues\0nigthly no_std\0" );
        let p2 = &process!( 1002, 1001, "cargo"   , "cargo\0check\0"                                                     );
        let p7 = &process!( 1007, 1005, "ps"      , "ps\0aux\0"                                                          );
        let p6 = &process!( 1006, 1005, "ping"    , "ping\0192.168.1.1\0"                                                );
        let p4 = &process!( 1004, 1002, "alphabet", "alphabet\0--alpha\00\0--beta\01\0--gamma\02\0"                      );
        let p3 = &process!( 1003, 1002, "foo"     , "foo\0-bar\0-baz\0"                                                  );

        #[cfg( target_os = "freebsd" )]
        (|| {
            let proc_list = from_json( JSON ).unwrap();
            let bfs = proc_list.bfs();
            let forest = trees::Forest::<&Process>::from( bfs );
            let expected = trees::Forest::<&Process>::from_tuple((
                (p1, (p5, p7, p6), (p2, p4, p3), ),
            ));
            assert_eq!( forest, expected );
        })();

        #[cfg( unix )]
        (|| {
            let proc_list = from_table( TABLE ).unwrap();
            let bfs = proc_list.bfs();
            let forest = trees::Forest::<&Process>::from( bfs );
            let expected = trees::Forest::<&Process>::from_tuple((
                (p1, (p5, p7, p6), (p2, p4, p3), ),
            ));
            assert_eq!( forest, expected );
        })();

        #[cfg( any( target_os = "freebsd", target_os = "linux" ))]
        (|| {
            let proc_list = pals_from_procfs( &fake_procfs() ).unwrap();
            let bfs = proc_list.bfs();
            let forest = trees::Forest::<&Process>::from( bfs );
            let expected = trees::Forest::<&Process>::from_tuple((
                (p1, (p5, p7, p6), (p2, p4, p3), ),
            ));
            assert_eq!( forest, expected );
        })();

        #[cfg( target_os = "windows" )]
        (|| {
            let path = fake_wmic_utf16_file_path();
            let proc_list = from_utf16_file( &path ).unwrap();
            let bfs = proc_list.bfs();
            let forest = trees::Forest::<&Process>::from( bfs );
            let expected = trees::Forest::<&Process>::from_tuple((
                (p1, (p5, p7, p6), (p2, p4, p3), ),
            ));
            assert_eq!( forest, expected );
        })();
    }

    #[test]
    fn win_argv_works() {
        assert_eq!( argv_iter( &win_argv( r#""abc" d e"#       )).collect::<Vec<_>>(), vec![  "abc"    , "d"    , "e" ]);
        assert_eq!( argv_iter( &win_argv( r#"a\\\b d"e f"g h"# )).collect::<Vec<_>>(), vec![r#"a\\\b"# , "de fg", "h" ]);
        assert_eq!( argv_iter( &win_argv( r#"a\\\"b c d"#      )).collect::<Vec<_>>(), vec![r#"a\"b"#  , "c"    , "d" ]);
        assert_eq!( argv_iter( &win_argv( r#"a\\\\"b c" d e"#  )).collect::<Vec<_>>(), vec![r#"a\\b c"#, "d"    , "e" ]);
    }

    #[test]
    fn display() {

        #[cfg( any( target_os = "freebsd", target_os = "linux" ))]
        {
            let proc_list = pals_from_procfs( &fake_procfs() ).unwrap();
            assert_eq!( proc_list.to_string(),
r#"[{cmd: "tcsh", pid:1001, args:[ "-tcsh", "(tcsh)" ], subs:[
  {cmd: "cargo", pid:1005, args:[ "cargo", "build", "--no-default-features", "--featurues", "nigthly no_std" ], subs:[
   {cmd: "ps", pid:1007, args:[ "ps", "aux" ]},
   {cmd: "ping", pid:1006, args:[ "ping", "192.168.1.1" ]}]},
  {cmd: "cargo", pid:1002, args:[ "cargo", "check" ], subs:[
   {cmd: "alphabet", pid:1004, args:[ "alphabet", "--alpha", "0", "--beta", "1", "--gamma", "2" ]},
   {cmd: "foo", pid:1003, args:[ "foo", "-bar", "-baz" ]}]}]}]"# );
        }
    }
}