Skip to content

Commit

Permalink
basenc: add utility
Browse files Browse the repository at this point in the history
basenc is a brand-new gnu core utility (added less than 3 years ago!),
which enables some more encodings.
  • Loading branch information
miDeb committed Aug 5, 2021
1 parent 158a8a3 commit b8c383e
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 34 deletions.
1 change: 1 addition & 0 deletions .vscode/cspell.dictionaries/workspace.wordlist.txt
Expand Up @@ -68,6 +68,7 @@ splitn
trunc

# * uutils
basenc
chcon
chgrp
chmod
Expand Down
43 changes: 41 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -35,6 +35,7 @@ feat_common_core = [
"base32",
"base64",
"basename",
"basenc",
"cat",
"cksum",
"comm",
Expand Down Expand Up @@ -245,6 +246,7 @@ arch = { optional=true, version="0.0.7", package="uu_arch", path="src/uu/arc
base32 = { optional=true, version="0.0.7", package="uu_base32", path="src/uu/base32" }
base64 = { optional=true, version="0.0.7", package="uu_base64", path="src/uu/base64" }
basename = { optional=true, version="0.0.7", package="uu_basename", path="src/uu/basename" }
basenc = { optional=true, version="0.0.7", package="uu_basenc", path="src/uu/basenc" }
cat = { optional=true, version="0.0.7", package="uu_cat", path="src/uu/cat" }
chgrp = { optional=true, version="0.0.7", package="uu_chgrp", path="src/uu/chgrp" }
chmod = { optional=true, version="0.0.7", package="uu_chmod", path="src/uu/chmod" }
Expand Down
29 changes: 15 additions & 14 deletions README.md
Expand Up @@ -369,20 +369,21 @@ To contribute to uutils, please see [CONTRIBUTING](CONTRIBUTING.md).
| base32 | date | runcon |
| base64 | dd | stty |
| basename | df | |
| cat | expr | |
| chgrp | install | |
| chmod | join | |
| chown | ls | |
| chroot | more | |
| cksum | numfmt | |
| comm | od (`--strings` and 128-bit data types missing) |
| csplit | pr | |
| cut | printf | |
| dircolors | sort | |
| dirname | split | |
| du | tac | |
| echo | tail | |
| env | test | |
| basenc | expr | |
| cat | install | |
| chgrp | join | |
| chmod | ls | |
| chown | more | |
| chroot | numfmt | |
| cksum | od (`--strings` and 128-bit data types missing) |
| comm | pr | |
| csplit | printf | |
| cut | sort | |
| dircolors | split | |
| dirname | tac | |
| du | tail | |
| echo | test | |
| env | | |
| expand | | |
| factor | | |
| false | | |
Expand Down
18 changes: 14 additions & 4 deletions src/uu/base32/src/base_common.rs
Expand Up @@ -34,7 +34,7 @@ pub mod options {
}

impl Config {
fn from(app_name: &str, options: clap::ArgMatches) -> Result<Config, String> {
pub fn from(app_name: &str, options: &clap::ArgMatches) -> Result<Config, String> {
let file: Option<String> = match options.values_of(options::FILE) {
Some(mut values) => {
let name = values.next().unwrap();
Expand Down Expand Up @@ -85,7 +85,7 @@ pub fn parse_base_cmd_args(
let arg_list = args
.collect_str(InvalidEncodingHandling::ConvertLossy)
.accept_any();
Config::from(name, app.get_matches_from(arg_list))
Config::from(name, &app.get_matches_from(arg_list))
}

pub fn base_app<'a>(name: &str, version: &'a str, about: &'a str) -> App<'static, 'a> {
Expand Down Expand Up @@ -145,8 +145,18 @@ pub fn handle_input<R: Read>(
}

if !decode {
let encoded = data.encode();
wrap_print(&data, encoded);
match data.encode() {
Ok(s) => {
wrap_print(&data, s);
}
Err(_) => {
eprintln!(
"{}: error: invalid input (length must be multiple of 4 characters)",
name
);
exit!(1)
}
}
} else {
match data.decode() {
Ok(s) => {
Expand Down
25 changes: 25 additions & 0 deletions src/uu/basenc/Cargo.toml
@@ -0,0 +1,25 @@
[package]
name = "uu_basenc"
version = "0.0.7"
authors = ["uutils developers"]
license = "MIT"
description = "basenc ~ (uutils) decode/encode input"

homepage = "https://github.com/uutils/coreutils"
repository = "https://github.com/uutils/coreutils/tree/master/src/uu/basenc"
keywords = ["coreutils", "uutils", "cross-platform", "cli", "utility"]
categories = ["command-line-utilities"]
edition = "2018"

[lib]
path = "src/basenc.rs"

[dependencies]
clap = { version = "2.33", features = ["wrap_help"] }
uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features = ["encoding"] }
uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" }
uu_base32 = { version=">=0.0.6", package="uu_base32", path="../base32"}

[[bin]]
name = "basenc"
path = "src/main.rs"
95 changes: 95 additions & 0 deletions src/uu/basenc/src/basenc.rs
@@ -0,0 +1,95 @@
// This file is part of the uutils coreutils package.
//
// (c) Jordy Dickinson <jordy.dickinson@gmail.com>
// (c) Jian Zeng <anonymousknight96@gmail.com>
//
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.

//spell-checker:ignore (args) lsbf msbf

#[macro_use]
extern crate uucore;

use clap::{crate_version, App, Arg};
use uu_base32::base_common::{self, Config};

use uucore::{encoding::Format, InvalidEncodingHandling};

use std::io::{stdin, Read};

static ABOUT: &str = "
With no FILE, or when FILE is -, read standard input.
When decoding, the input may contain newlines in addition to the bytes of
the formal alphabet. Use --ignore-garbage to attempt to recover
from any other non-alphabet bytes in the encoded stream.
";

static BASE_CMD_PARSE_ERROR: i32 = 1;

const ENCODINGS: &[(&str, Format)] = &[
("base64", Format::Base64),
("base64url", Format::Base64Url),
("base32", Format::Base32),
("base32hex", Format::Base32Hex),
("base16", Format::Base16),
("base2lsbf", Format::Base2Lsbf),
("base2msbf", Format::Base2Msbf),
("z85", Format::Z85),
// common abbreviations. TODO: once we have clap 3.0 we can use `AppSettings::InferLongArgs` to get all abbreviations automatically
("base2l", Format::Base2Lsbf),
("base2m", Format::Base2Msbf),
];

fn get_usage() -> String {
format!("{0} [OPTION]... [FILE]", executable!())
}

pub fn uu_app() -> App<'static, 'static> {
let mut app = base_common::base_app(executable!(), crate_version!(), ABOUT);
for encoding in ENCODINGS {
app = app.arg(Arg::with_name(encoding.0).long(encoding.0));
}
app
}

fn parse_cmd_args(args: impl uucore::Args) -> (Config, Format) {
let usage = get_usage();
let matches = uu_app().usage(&usage[..]).get_matches_from(
args.collect_str(InvalidEncodingHandling::ConvertLossy)
.accept_any(),
);
let format = ENCODINGS
.iter()
.find(|encoding| matches.is_present(encoding.0))
.unwrap_or_else(|| {
show_usage_error!("missing encoding type");
std::process::exit(1)
})
.1;
(
Config::from("basenc", &matches).unwrap_or_else(|s| crash!(BASE_CMD_PARSE_ERROR, "{}", s)),
format,
)
}

pub fn uumain(args: impl uucore::Args) -> i32 {
let name = executable!();
let (config, format) = parse_cmd_args(args);
// Create a reference to stdin so we can return a locked stdin from
// parse_base_cmd_args
let stdin_raw = stdin();
let mut input: Box<dyn Read> = base_common::get_input(&config, &stdin_raw);

base_common::handle_input(
&mut input,
format,
config.wrap_cols,
config.ignore_garbage,
config.decode,
name,
);

0
}
1 change: 1 addition & 0 deletions src/uu/basenc/src/main.rs
@@ -0,0 +1 @@
uucore_procs::main!(uu_basenc);
6 changes: 4 additions & 2 deletions src/uucore/Cargo.toml
Expand Up @@ -27,7 +27,9 @@ nix = { version="<= 0.13", optional=true }
platform-info = { version="<= 0.1", optional=true }
time = { version="<= 0.1.43", optional=true }
# * "problem" dependencies (pinned)
data-encoding = { version="~2.1", optional=true } ## data-encoding: require v2.1; but v2.2.0 breaks the build for MinSRV v1.31.0
data-encoding = { version="2.1", optional=true }
data-encoding-macro = { version="0.1.12", optional=true }
z85 = { version="3.0.3", optional=true }
libc = { version="0.2.15, <= 0.2.85", optional=true } ## libc: initial utmp support added in v0.2.15; but v0.2.68 breaks the build for MinSRV v1.31.0

[dev-dependencies]
Expand All @@ -43,7 +45,7 @@ termion = "1.5"
[features]
default = []
# * non-default features
encoding = ["data-encoding", "thiserror"]
encoding = ["data-encoding", "data-encoding-macro", "z85", "thiserror"]
entries = ["libc"]
fs = ["libc"]
fsext = ["libc", "time"]
Expand Down

0 comments on commit b8c383e

Please sign in to comment.