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
// This file is part of the uutils coreutils package.
//
// (c) Jimmy Lu <jimmy.lu.2011@gmail.com>
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (ToDO) fullname

use clap::{crate_version, Arg, ArgAction, Command};
use std::path::{is_separator, PathBuf};
use uucore::display::Quotable;
use uucore::error::{UResult, UUsageError};
use uucore::line_ending::LineEnding;
use uucore::{format_usage, help_about, help_usage};

static ABOUT: &str = help_about!("basename.md");

const USAGE: &str = help_usage!("basename.md");

pub mod options {
    pub static MULTIPLE: &str = "multiple";
    pub static NAME: &str = "name";
    pub static SUFFIX: &str = "suffix";
    pub static ZERO: &str = "zero";
}

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
    let args = args.collect_lossy();

    // Since options have to go before names,
    // if the first argument is not an option, then there is no option,
    // and that implies there is exactly one name (no option => no -a option),
    // so simple format is used
    if args.len() > 1 && !args[1].starts_with('-') {
        if args.len() > 3 {
            return Err(UUsageError::new(
                1,
                format!("extra operand {}", args[3].to_string().quote()),
            ));
        }
        let suffix = if args.len() > 2 { args[2].as_ref() } else { "" };
        println!("{}", basename(&args[1], suffix));
        return Ok(());
    }

    //
    // Argument parsing
    //
    let matches = uu_app().try_get_matches_from(args)?;

    // too few arguments
    if !matches.contains_id(options::NAME) {
        return Err(UUsageError::new(1, "missing operand".to_string()));
    }

    let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));

    let opt_suffix = matches.get_one::<String>(options::SUFFIX).is_some();
    let opt_multiple = matches.get_flag(options::MULTIPLE);
    let multiple_paths = opt_suffix || opt_multiple;
    let name_args_count = matches
        .get_many::<String>(options::NAME)
        .map(|n| n.len())
        .unwrap_or(0);

    // too many arguments
    if !multiple_paths && name_args_count > 2 {
        return Err(UUsageError::new(
            1,
            format!(
                "extra operand {}",
                matches
                    .get_many::<String>(options::NAME)
                    .unwrap()
                    .nth(2)
                    .unwrap()
                    .quote()
            ),
        ));
    }

    let suffix = if opt_suffix {
        matches.get_one::<String>(options::SUFFIX).unwrap()
    } else if !opt_multiple && name_args_count > 1 {
        matches
            .get_many::<String>(options::NAME)
            .unwrap()
            .nth(1)
            .unwrap()
    } else {
        ""
    };

    //
    // Main Program Processing
    //

    let paths: Vec<_> = if multiple_paths {
        matches.get_many::<String>(options::NAME).unwrap().collect()
    } else {
        matches
            .get_many::<String>(options::NAME)
            .unwrap()
            .take(1)
            .collect()
    };

    for path in paths {
        print!("{}{}", basename(path, suffix), line_ending);
    }

    Ok(())
}

pub fn uu_app() -> Command {
    Command::new(uucore::util_name())
        .version(crate_version!())
        .about(ABOUT)
        .override_usage(format_usage(USAGE))
        .infer_long_args(true)
        .arg(
            Arg::new(options::MULTIPLE)
                .short('a')
                .long(options::MULTIPLE)
                .help("support multiple arguments and treat each as a NAME")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new(options::NAME)
                .action(clap::ArgAction::Append)
                .value_hint(clap::ValueHint::AnyPath)
                .hide(true),
        )
        .arg(
            Arg::new(options::SUFFIX)
                .short('s')
                .long(options::SUFFIX)
                .value_name("SUFFIX")
                .help("remove a trailing SUFFIX; implies -a"),
        )
        .arg(
            Arg::new(options::ZERO)
                .short('z')
                .long(options::ZERO)
                .help("end each output line with NUL, not newline")
                .action(ArgAction::SetTrue),
        )
}

fn basename(fullname: &str, suffix: &str) -> String {
    // Remove all platform-specific path separators from the end.
    let path = fullname.trim_end_matches(is_separator);

    // If the path contained *only* suffix characters (for example, if
    // `fullname` were "///" and `suffix` were "/"), then `path` would
    // be left with the empty string. In that case, we set `path` to be
    // the original `fullname` to avoid returning the empty path.
    let path = if path.is_empty() { fullname } else { path };

    // Convert to path buffer and get last path component
    let pb = PathBuf::from(path);
    match pb.components().last() {
        Some(c) => {
            let name = c.as_os_str().to_str().unwrap();
            if name == suffix {
                name.to_string()
            } else {
                name.strip_suffix(suffix).unwrap_or(name).to_string()
            }
        }

        None => String::new(),
    }
}