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:: { OsStr , 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 std:: ffi:: OsStr ;
9+
10+ use clap:: Command ;
11+ use uu_checksum_common:: { ChecksumCommand , checksum_main, default_checksum_app, options} ;
12+
13+ use uucore:: checksum:: compute:: OutputFormat ;
1714use uucore:: checksum:: {
18- AlgoKind , ChecksumError , SUPPORTED_ALGORITHMS , SizedAlgoKind , calculate_blake2b_length_str,
19- sanitize_sha2_sha3_length_str,
15+ AlgoKind , ChecksumError , calculate_blake2b_length_str, sanitize_sha2_sha3_length_str,
2016} ;
2117use uucore:: error:: UResult ;
2218use uucore:: hardware:: { HasHardwareFeatures as _, SimdPolicy } ;
23- use uucore:: line_ending:: LineEnding ;
24- use uucore:: { format_usage, show_error, translate} ;
19+ use uucore:: { show_error, translate} ;
2520
2621/// Print CPU hardware capability detection information to stderr
2722/// This matches GNU cksum's --debug behavior
@@ -47,26 +42,6 @@ fn print_cpu_debug_info() {
4742 }
4843}
4944
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-
7045/// cksum has a bunch of legacy behavior. We handle this in this function to
7146/// make sure they are self contained and "easier" to understand.
7247///
@@ -137,22 +112,6 @@ fn maybe_sanitize_length(
137112pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
138113 let matches = uucore:: clap_localization:: handle_clap_result ( uu_app ( ) , args) ?;
139114
140- let check = matches. get_flag ( options:: CHECK ) ;
141-
142- let check_flag = |flag| match ( check, matches. get_flag ( flag) ) {
143- ( _, false ) => Ok ( false ) ,
144- ( true , true ) => Ok ( true ) ,
145- ( false , true ) => Err ( ChecksumError :: CheckOnlyFlag ( flag. into ( ) ) ) ,
146- } ;
147-
148- // Each of the following flags are only expected in --check mode.
149- // If we encounter them otherwise, end with an error.
150- let ignore_missing = check_flag ( options:: IGNORE_MISSING ) ?;
151- let warn = check_flag ( options:: WARN ) ?;
152- let quiet = check_flag ( options:: QUIET ) ?;
153- let strict = check_flag ( options:: STRICT ) ?;
154- let status = check_flag ( options:: STATUS ) ?;
155-
156115 let algo_cli = matches
157116 . get_one :: < String > ( options:: ALGORITHM )
158117 . map ( AlgoKind :: from_cksum)
@@ -164,202 +123,36 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
164123
165124 let length = maybe_sanitize_length ( algo_cli, input_length) ?;
166125
167- // clap provides the default value -. So we unwrap() safety.
168- let files = matches
169- . get_many :: < OsString > ( options:: FILE )
170- . unwrap ( )
171- . map ( |s| s. as_os_str ( ) ) ;
172-
173- if check {
174- // cksum does not support '--check'ing legacy algorithms
175- if algo_cli. is_some_and ( AlgoKind :: is_legacy) {
176- return Err ( ChecksumError :: AlgorithmNotSupportedWithCheck . into ( ) ) ;
177- }
178-
179- let text_flag = matches. get_flag ( options:: TEXT ) ;
180- let binary_flag = matches. get_flag ( options:: BINARY ) ;
181- let tag = matches. get_flag ( options:: TAG ) ;
182-
183- if tag || binary_flag || text_flag {
184- return Err ( ChecksumError :: BinaryTextConflict . into ( ) ) ;
185- }
186-
187- // Execute the checksum validation based on the presence of files or the use of stdin
188-
189- let verbose = ChecksumVerbose :: new ( status, quiet, warn) ;
190- let opts = ChecksumValidateOptions {
191- ignore_missing,
192- strict,
193- verbose,
194- } ;
195-
196- return perform_checksum_validation ( files, algo_cli, length, opts) ;
197- }
198-
199- // Not --check
200-
201- // Print hardware debug info if requested
202- if matches. get_flag ( options:: DEBUG ) {
203- print_cpu_debug_info ( ) ;
204- }
205-
206- // Set the default algorithm to CRC when not '--check'ing.
207- let algo_kind = algo_cli. unwrap_or ( AlgoKind :: Crc ) ;
208-
209126 let ( tag, binary) = handle_tag_text_binary_flags ( std:: env:: args_os ( ) ) ?;
210127
211128 let output_format = OutputFormat :: from_cksum (
212- algo_kind ,
129+ algo_cli . unwrap_or ( AlgoKind :: Crc ) ,
213130 tag,
214131 binary,
215- matches. get_flag ( options:: RAW ) ,
216- matches. get_flag ( options:: BASE64 ) ,
132+ /* raw */ matches. get_flag ( options:: RAW ) ,
133+ /* base64 */ matches. get_flag ( options:: BASE64 ) ,
217134 ) ;
218135
219- let algo = SizedAlgoKind :: from_unsized ( algo_kind, length) ?;
220- let line_ending = LineEnding :: from_zero_flag ( matches. get_flag ( options:: ZERO ) ) ;
221-
222- let opts = ChecksumComputeOptions {
223- algo_kind : algo,
224- output_format,
225- line_ending,
226- } ;
227-
228- perform_checksum_computation ( opts, files) ?;
136+ // Print hardware debug info if requested
137+ if matches. get_flag ( options:: DEBUG ) {
138+ print_cpu_debug_info ( ) ;
139+ }
229140
230- Ok ( ( ) )
141+ checksum_main ( algo_cli , length , matches , output_format )
231142}
232143
233144pub fn uu_app ( ) -> Command {
234- Command :: new ( uucore:: util_name ( ) )
235- . version ( uucore:: crate_version!( ) )
236- . help_template ( uucore:: localized_help_template ( uucore:: util_name ( ) ) )
237- . about ( translate ! ( "cksum-about" ) )
238- . override_usage ( format_usage ( & translate ! ( "cksum-usage" ) ) )
239- . infer_long_args ( true )
240- . args_override_self ( true )
241- . arg (
242- Arg :: new ( options:: FILE )
243- . hide ( true )
244- . action ( ArgAction :: Append )
245- . value_parser ( ValueParser :: os_string ( ) )
246- . default_value ( "-" )
247- . hide_default_value ( true )
248- . value_hint ( clap:: ValueHint :: FilePath ) ,
249- )
250- . arg (
251- Arg :: new ( options:: ALGORITHM )
252- . long ( options:: ALGORITHM )
253- . short ( 'a' )
254- . help ( translate ! ( "cksum-help-algorithm" ) )
255- . value_name ( "ALGORITHM" )
256- . value_parser ( SUPPORTED_ALGORITHMS ) ,
257- )
258- . arg (
259- Arg :: new ( options:: UNTAGGED )
260- . long ( options:: UNTAGGED )
261- . help ( translate ! ( "cksum-help-untagged" ) )
262- . action ( ArgAction :: SetTrue )
263- . overrides_with ( options:: TAG ) ,
264- )
265- . arg (
266- Arg :: new ( options:: TAG )
267- . long ( options:: TAG )
268- . help ( translate ! ( "cksum-help-tag" ) )
269- . action ( ArgAction :: SetTrue )
270- . overrides_with ( options:: UNTAGGED ) ,
271- )
272- . arg (
273- Arg :: new ( options:: LENGTH )
274- . long ( options:: LENGTH )
275- . short ( 'l' )
276- . help ( translate ! ( "cksum-help-length" ) )
277- . action ( ArgAction :: Set ) ,
278- )
279- . arg (
280- Arg :: new ( options:: RAW )
281- . long ( options:: RAW )
282- . help ( translate ! ( "cksum-help-raw" ) )
283- . action ( ArgAction :: SetTrue ) ,
284- )
285- . arg (
286- Arg :: new ( options:: STRICT )
287- . long ( options:: STRICT )
288- . help ( translate ! ( "cksum-help-strict" ) )
289- . action ( ArgAction :: SetTrue ) ,
290- )
291- . arg (
292- Arg :: new ( options:: CHECK )
293- . short ( 'c' )
294- . long ( options:: CHECK )
295- . help ( translate ! ( "cksum-help-check" ) )
296- . action ( ArgAction :: SetTrue ) ,
297- )
298- . arg (
299- Arg :: new ( options:: BASE64 )
300- . long ( options:: BASE64 )
301- . help ( translate ! ( "cksum-help-base64" ) )
302- . action ( ArgAction :: SetTrue )
303- // Even though this could easily just override an earlier '--raw',
304- // GNU cksum does not permit these flags to be combined:
305- . conflicts_with ( options:: RAW ) ,
306- )
307- . arg (
308- Arg :: new ( options:: TEXT )
309- . long ( options:: TEXT )
310- . short ( 't' )
311- . hide ( true )
312- . overrides_with ( options:: BINARY )
313- . action ( ArgAction :: SetTrue ) ,
314- )
315- . arg (
316- Arg :: new ( options:: BINARY )
317- . long ( options:: BINARY )
318- . short ( 'b' )
319- . hide ( true )
320- . overrides_with ( options:: TEXT )
321- . action ( ArgAction :: SetTrue ) ,
322- )
323- . arg (
324- Arg :: new ( options:: WARN )
325- . short ( 'w' )
326- . long ( "warn" )
327- . help ( translate ! ( "cksum-help-warn" ) )
328- . action ( ArgAction :: SetTrue )
329- . overrides_with_all ( [ options:: STATUS , options:: QUIET ] ) ,
330- )
331- . arg (
332- Arg :: new ( options:: STATUS )
333- . long ( "status" )
334- . help ( translate ! ( "cksum-help-status" ) )
335- . action ( ArgAction :: SetTrue )
336- . overrides_with_all ( [ options:: WARN , options:: QUIET ] ) ,
337- )
338- . arg (
339- Arg :: new ( options:: QUIET )
340- . long ( options:: QUIET )
341- . help ( translate ! ( "cksum-help-quiet" ) )
342- . action ( ArgAction :: SetTrue )
343- . overrides_with_all ( [ options:: WARN , options:: STATUS ] ) ,
344- )
345- . arg (
346- Arg :: new ( options:: IGNORE_MISSING )
347- . long ( options:: IGNORE_MISSING )
348- . help ( translate ! ( "cksum-help-ignore-missing" ) )
349- . action ( ArgAction :: SetTrue ) ,
350- )
351- . arg (
352- Arg :: new ( options:: ZERO )
353- . long ( options:: ZERO )
354- . short ( 'z' )
355- . help ( translate ! ( "cksum-help-zero" ) )
356- . action ( ArgAction :: SetTrue ) ,
357- )
358- . arg (
359- Arg :: new ( options:: DEBUG )
360- . long ( options:: DEBUG )
361- . help ( translate ! ( "cksum-help-debug" ) )
362- . action ( ArgAction :: SetTrue ) ,
363- )
145+ default_checksum_app ( translate ! ( "cksum-about" ) , translate ! ( "cksum-usage" ) )
146+ . with_algo ( )
147+ . with_untagged ( )
148+ . with_tag ( true )
149+ . with_length ( )
150+ . with_raw ( )
151+ . with_check_and_opts ( )
152+ . with_base64 ( )
153+ . with_text ( false )
154+ . with_binary ( )
155+ . with_zero ( )
156+ . with_debug ( )
364157 . after_help ( translate ! ( "cksum-after-help" ) )
365158}
0 commit comments