55
66// spell-checker:ignore (ToDO) fname, algo, bitlen
77
8- use clap:: builder:: ValueParser ;
9- use clap:: { Arg , ArgAction , Command } ;
10- use std:: ffi:: OsString ;
11- use uucore:: checksum:: compute:: {
12- ChecksumComputeOptions , OutputFormat , perform_checksum_computation,
13- } ;
14- use uucore:: checksum:: validate:: {
15- ChecksumValidateOptions , ChecksumVerbose , perform_checksum_validation,
16- } ;
8+ use clap:: Command ;
9+ use uu_checksum_common:: { ChecksumCommand , checksum_main, default_checksum_app, options} ;
10+
11+ use uucore:: checksum:: compute:: OutputFormat ;
1712use uucore:: checksum:: {
18- AlgoKind , ChecksumError , SUPPORTED_ALGORITHMS , SizedAlgoKind , calculate_blake2b_length_str,
19- sanitize_sha2_sha3_length_str,
13+ AlgoKind , ChecksumError , calculate_blake2b_length_str, sanitize_sha2_sha3_length_str,
2014} ;
2115use uucore:: error:: UResult ;
2216use uucore:: hardware:: { HasHardwareFeatures as _, SimdPolicy } ;
23- use uucore:: line_ending:: LineEnding ;
24- use uucore:: { format_usage, show_error, translate} ;
17+ use uucore:: { show_error, translate} ;
2518
2619/// Print CPU hardware capability detection information to stderr
2720/// This matches GNU cksum's --debug behavior
@@ -47,26 +40,6 @@ fn print_cpu_debug_info() {
4740 }
4841}
4942
50- mod options {
51- pub const ALGORITHM : & str = "algorithm" ;
52- pub const FILE : & str = "file" ;
53- pub const UNTAGGED : & str = "untagged" ;
54- pub const TAG : & str = "tag" ;
55- pub const LENGTH : & str = "length" ;
56- pub const RAW : & str = "raw" ;
57- pub const BASE64 : & str = "base64" ;
58- pub const CHECK : & str = "check" ;
59- pub const STRICT : & str = "strict" ;
60- pub const TEXT : & str = "text" ;
61- pub const BINARY : & str = "binary" ;
62- pub const STATUS : & str = "status" ;
63- pub const WARN : & str = "warn" ;
64- pub const IGNORE_MISSING : & str = "ignore-missing" ;
65- pub const QUIET : & str = "quiet" ;
66- pub const ZERO : & str = "zero" ;
67- pub const DEBUG : & str = "debug" ;
68- }
69-
7043/// cksum has a bunch of legacy behavior. We handle this in this function to
7144/// make sure they are self contained and "easier" to understand.
7245///
@@ -101,14 +74,6 @@ fn maybe_sanitize_length(
10174pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
10275 let matches = uucore:: clap_localization:: handle_clap_result ( uu_app ( ) , args) ?;
10376
104- let check = matches. get_flag ( options:: CHECK ) ;
105-
106- let ignore_missing = matches. get_flag ( options:: IGNORE_MISSING ) ;
107- let warn = matches. get_flag ( options:: WARN ) ;
108- let quiet = matches. get_flag ( options:: QUIET ) ;
109- let strict = matches. get_flag ( options:: STRICT ) ;
110- let status = matches. get_flag ( options:: STATUS ) ;
111-
11277 let algo_cli = matches
11378 . get_one :: < String > ( options:: ALGORITHM )
11479 . map ( AlgoKind :: from_cksum)
@@ -120,208 +85,36 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
12085
12186 let length = maybe_sanitize_length ( algo_cli, input_length) ?;
12287
123- // clap provides the default value -. So we unwrap() safety.
124- let files = matches
125- . get_many :: < OsString > ( options:: FILE )
126- . unwrap ( )
127- . map ( |s| s. as_os_str ( ) ) ;
128-
129- if check {
130- // cksum does not support '--check'ing legacy algorithms
131- if algo_cli. is_some_and ( AlgoKind :: is_legacy) {
132- return Err ( ChecksumError :: AlgorithmNotSupportedWithCheck . into ( ) ) ;
133- }
134-
135- let text_flag = matches. get_flag ( options:: TEXT ) ;
136- let binary_flag = matches. get_flag ( options:: BINARY ) ;
137- let tag = matches. get_flag ( options:: TAG ) ;
138-
139- if tag || binary_flag || text_flag {
140- return Err ( ChecksumError :: BinaryTextConflict . into ( ) ) ;
141- }
142-
143- // Execute the checksum validation based on the presence of files or the use of stdin
144-
145- let verbose = ChecksumVerbose :: new ( status, quiet, warn) ;
146- let opts = ChecksumValidateOptions {
147- ignore_missing,
148- strict,
149- verbose,
150- } ;
151-
152- return perform_checksum_validation ( files, algo_cli, length, opts) ;
153- }
154-
155- // Not --check
88+ let output_format = OutputFormat :: from_cksum (
89+ algo_cli. unwrap_or ( AlgoKind :: Crc ) ,
90+ // Making TAG default at clap blocks --untagged
91+ /* tag */
92+ !matches. get_flag ( options:: UNTAGGED ) ,
93+ /* binary */ matches. get_flag ( options:: BINARY ) ,
94+ /* raw */ matches. get_flag ( options:: RAW ) ,
95+ /* base64 */ matches. get_flag ( options:: BASE64 ) ,
96+ ) ;
15697
15798 // Print hardware debug info if requested
15899 if matches. get_flag ( options:: DEBUG ) {
159100 print_cpu_debug_info ( ) ;
160101 }
161102
162- // Set the default algorithm to CRC when not '--check'ing.
163- let algo_kind = algo_cli. unwrap_or ( AlgoKind :: Crc ) ;
164-
165- let algo = SizedAlgoKind :: from_unsized ( algo_kind, length) ?;
166- let line_ending = LineEnding :: from_zero_flag ( matches. get_flag ( options:: ZERO ) ) ;
167-
168- let opts = ChecksumComputeOptions {
169- algo_kind : algo,
170- output_format : OutputFormat :: from_cksum (
171- algo_kind,
172- // Making TAG default at clap blocks --untagged
173- /* tag */
174- !matches. get_flag ( options:: UNTAGGED ) ,
175- /* binary */ matches. get_flag ( options:: BINARY ) ,
176- /* raw */ matches. get_flag ( options:: RAW ) ,
177- /* base64 */ matches. get_flag ( options:: BASE64 ) ,
178- ) ,
179- line_ending,
180- } ;
181-
182- perform_checksum_computation ( opts, files) ?;
183-
184- Ok ( ( ) )
103+ checksum_main ( algo_cli, length, matches, output_format)
185104}
186105
187106pub fn uu_app ( ) -> Command {
188- Command :: new ( uucore:: util_name ( ) )
189- . version ( uucore:: crate_version!( ) )
190- . help_template ( uucore:: localized_help_template ( uucore:: util_name ( ) ) )
191- . about ( translate ! ( "cksum-about" ) )
192- . override_usage ( format_usage ( & translate ! ( "cksum-usage" ) ) )
193- . infer_long_args ( true )
194- . args_override_self ( true )
195- . arg (
196- Arg :: new ( options:: FILE )
197- . hide ( true )
198- . action ( ArgAction :: Append )
199- . value_parser ( ValueParser :: os_string ( ) )
200- . default_value ( "-" )
201- . hide_default_value ( true )
202- . value_hint ( clap:: ValueHint :: FilePath ) ,
203- )
204- . arg (
205- Arg :: new ( options:: ALGORITHM )
206- . long ( options:: ALGORITHM )
207- . short ( 'a' )
208- . help ( translate ! ( "cksum-help-algorithm" ) )
209- . value_name ( "ALGORITHM" )
210- . value_parser ( SUPPORTED_ALGORITHMS ) ,
211- )
212- . arg (
213- Arg :: new ( options:: UNTAGGED )
214- . long ( options:: UNTAGGED )
215- . help ( translate ! ( "cksum-help-untagged" ) )
216- . action ( ArgAction :: SetTrue )
217- . overrides_with ( options:: TAG ) ,
218- )
219- . arg (
220- Arg :: new ( options:: TAG )
221- . long ( options:: TAG )
222- . help ( translate ! ( "cksum-help-tag" ) )
223- . action ( ArgAction :: SetTrue )
224- . overrides_with ( options:: UNTAGGED )
225- . overrides_with ( options:: BINARY )
226- . overrides_with ( options:: TEXT ) ,
227- )
228- . arg (
229- Arg :: new ( options:: LENGTH )
230- . long ( options:: LENGTH )
231- . short ( 'l' )
232- . help ( translate ! ( "cksum-help-length" ) )
233- . action ( ArgAction :: Set ) ,
234- )
235- . arg (
236- Arg :: new ( options:: RAW )
237- . long ( options:: RAW )
238- . help ( translate ! ( "cksum-help-raw" ) )
239- . action ( ArgAction :: SetTrue ) ,
240- )
241- . arg (
242- Arg :: new ( options:: STRICT )
243- . long ( options:: STRICT )
244- . help ( translate ! ( "cksum-help-strict" ) )
245- . action ( ArgAction :: SetTrue )
246- . requires ( options:: CHECK ) ,
247- )
248- . arg (
249- Arg :: new ( options:: CHECK )
250- . short ( 'c' )
251- . long ( options:: CHECK )
252- . help ( translate ! ( "cksum-help-check" ) )
253- . action ( ArgAction :: SetTrue ) ,
254- )
255- . arg (
256- Arg :: new ( options:: BASE64 )
257- . long ( options:: BASE64 )
258- . help ( translate ! ( "cksum-help-base64" ) )
259- . action ( ArgAction :: SetTrue )
260- // Even though this could easily just override an earlier '--raw',
261- // GNU cksum does not permit these flags to be combined:
262- . conflicts_with ( options:: RAW ) ,
263- )
264- . arg (
265- Arg :: new ( options:: TEXT )
266- . long ( options:: TEXT )
267- . short ( 't' )
268- . hide ( true )
269- . overrides_with ( options:: BINARY )
270- . action ( ArgAction :: SetTrue )
271- . requires ( options:: UNTAGGED ) ,
272- )
273- . arg (
274- Arg :: new ( options:: BINARY )
275- . long ( options:: BINARY )
276- . short ( 'b' )
277- . hide ( true )
278- . overrides_with ( options:: TEXT )
279- . action ( ArgAction :: SetTrue ) ,
280- )
281- . arg (
282- Arg :: new ( options:: WARN )
283- . short ( 'w' )
284- . long ( "warn" )
285- . help ( translate ! ( "cksum-help-warn" ) )
286- . action ( ArgAction :: SetTrue )
287- . overrides_with_all ( [ options:: STATUS , options:: QUIET ] )
288- . requires ( options:: CHECK ) ,
289- )
290- . arg (
291- Arg :: new ( options:: STATUS )
292- . long ( "status" )
293- . help ( translate ! ( "cksum-help-status" ) )
294- . action ( ArgAction :: SetTrue )
295- . overrides_with_all ( [ options:: WARN , options:: QUIET ] )
296- . requires ( options:: CHECK ) ,
297- )
298- . arg (
299- Arg :: new ( options:: QUIET )
300- . long ( options:: QUIET )
301- . help ( translate ! ( "cksum-help-quiet" ) )
302- . action ( ArgAction :: SetTrue )
303- . overrides_with_all ( [ options:: WARN , options:: STATUS ] )
304- . requires ( options:: CHECK ) ,
305- )
306- . arg (
307- Arg :: new ( options:: IGNORE_MISSING )
308- . long ( options:: IGNORE_MISSING )
309- . help ( translate ! ( "cksum-help-ignore-missing" ) )
310- . action ( ArgAction :: SetTrue )
311- . requires ( options:: CHECK ) ,
312- )
313- . arg (
314- Arg :: new ( options:: ZERO )
315- . long ( options:: ZERO )
316- . short ( 'z' )
317- . help ( translate ! ( "cksum-help-zero" ) )
318- . action ( ArgAction :: SetTrue ) ,
319- )
320- . arg (
321- Arg :: new ( options:: DEBUG )
322- . long ( options:: DEBUG )
323- . help ( translate ! ( "cksum-help-debug" ) )
324- . action ( ArgAction :: SetTrue ) ,
325- )
107+ default_checksum_app ( translate ! ( "cksum-about" ) , translate ! ( "cksum-usage" ) )
108+ . with_algo ( )
109+ . with_untagged ( )
110+ . with_tag ( true )
111+ . with_length ( )
112+ . with_raw ( )
113+ . with_check_and_opts ( )
114+ . with_base64 ( )
115+ . with_text ( true , false )
116+ . with_binary ( true )
117+ . with_zero ( )
118+ . with_debug ( )
326119 . after_help ( translate ! ( "cksum-after-help" ) )
327120}
0 commit comments