5
5
6
6
// spell-checker:ignore (ToDO) getpriority execvp setpriority nstr PRIO cstrs ENOENT
7
7
8
+ use clap:: { Arg , ArgAction , Command } ;
8
9
use libc:: { PRIO_PROCESS , c_char, c_int, execvp} ;
10
+ use std:: collections:: HashMap ;
9
11
use std:: ffi:: { CString , OsString } ;
10
12
use std:: io:: { Error , Write } ;
11
13
use std:: ptr;
12
14
13
- use clap:: { Arg , ArgAction , Command } ;
14
- use uucore:: locale:: get_message;
15
+ use uucore:: locale:: { get_message, get_message_with_args} ;
15
16
use uucore:: {
16
17
error:: { UClapError , UResult , USimpleError , UUsageError , set_exit_code} ,
17
18
format_usage, show_error,
@@ -96,7 +97,6 @@ fn standardize_nice_args(mut args: impl uucore::Args) -> impl uucore::Args {
96
97
if saw_n {
97
98
v. push ( "-n" . into ( ) ) ;
98
99
}
99
-
100
100
v. into_iter ( )
101
101
}
102
102
@@ -111,7 +111,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
111
111
if Error :: last_os_error ( ) . raw_os_error ( ) . unwrap ( ) != 0 {
112
112
return Err ( USimpleError :: new (
113
113
125 ,
114
- format ! ( "getpriority: {}" , Error :: last_os_error( ) ) ,
114
+ get_message_with_args (
115
+ "nice-error-getpriority" ,
116
+ HashMap :: from ( [ ( "error" . to_string ( ) , Error :: last_os_error ( ) . to_string ( ) ) ] ) ,
117
+ ) ,
115
118
) ) ;
116
119
}
117
120
@@ -120,15 +123,21 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
120
123
if !matches. contains_id ( options:: COMMAND ) {
121
124
return Err ( UUsageError :: new (
122
125
125 ,
123
- "A command must be given with an adjustment." ,
126
+ get_message ( "nice-error- command-required- with- adjustment" ) ,
124
127
) ) ;
125
128
}
126
- match nstr. parse ( ) {
129
+ match nstr. parse :: < i32 > ( ) {
127
130
Ok ( num) => num,
128
131
Err ( e) => {
129
132
return Err ( USimpleError :: new (
130
133
125 ,
131
- format ! ( "\" {nstr}\" is not a valid number: {e}" ) ,
134
+ get_message_with_args (
135
+ "nice-error-invalid-number" ,
136
+ HashMap :: from ( [
137
+ ( "value" . to_string ( ) , nstr. clone ( ) ) ,
138
+ ( "error" . to_string ( ) , e. to_string ( ) ) ,
139
+ ] ) ,
140
+ ) ,
132
141
) ) ;
133
142
}
134
143
}
@@ -147,17 +156,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
147
156
// isn't writable. The GNU test suite checks specifically that the
148
157
// exit code when failing to write the advisory is 125, but Rust
149
158
// will produce an exit code of 101 when it panics.
150
- if unsafe { libc:: setpriority ( PRIO_PROCESS , 0 , niceness) } == -1
151
- && write ! (
152
- std:: io:: stderr( ) ,
153
- "{}: warning: setpriority: {}" ,
154
- uucore:: util_name( ) ,
155
- Error :: last_os_error( )
156
- )
157
- . is_err ( )
158
- {
159
- set_exit_code ( 125 ) ;
160
- return Ok ( ( ) ) ;
159
+ if unsafe { libc:: setpriority ( PRIO_PROCESS , 0 , niceness) } == -1 {
160
+ let warning_msg = get_message_with_args (
161
+ "nice-warning-setpriority" ,
162
+ HashMap :: from ( [
163
+ ( "util_name" . to_string ( ) , uucore:: util_name ( ) . to_string ( ) ) ,
164
+ ( "error" . to_string ( ) , Error :: last_os_error ( ) . to_string ( ) ) ,
165
+ ] ) ,
166
+ ) ;
167
+
168
+ if write ! ( std:: io:: stderr( ) , "{}" , warning_msg) . is_err ( ) {
169
+ set_exit_code ( 125 ) ;
170
+ return Ok ( ( ) ) ;
171
+ }
161
172
}
162
173
163
174
let cstrs: Vec < CString > = matches
@@ -172,7 +183,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
172
183
execvp ( args[ 0 ] , args. as_mut_ptr ( ) ) ;
173
184
}
174
185
175
- show_error ! ( "execvp: {}" , Error :: last_os_error( ) ) ;
186
+ show_error ! (
187
+ "{}" ,
188
+ get_message_with_args(
189
+ "nice-error-execvp" ,
190
+ HashMap :: from( [ ( "error" . to_string( ) , Error :: last_os_error( ) . to_string( ) ) ] )
191
+ )
192
+ ) ;
193
+
176
194
let exit_code = if Error :: last_os_error ( ) . raw_os_error ( ) . unwrap ( ) as c_int == libc:: ENOENT {
177
195
127
178
196
} else {
@@ -193,7 +211,7 @@ pub fn uu_app() -> Command {
193
211
Arg :: new ( options:: ADJUSTMENT )
194
212
. short ( 'n' )
195
213
. long ( options:: ADJUSTMENT )
196
- . help ( "add N to the niceness (default is 10)" )
214
+ . help ( get_message ( "nice-help-adjustment" ) )
197
215
. action ( ArgAction :: Set )
198
216
. overrides_with ( options:: ADJUSTMENT )
199
217
. allow_hyphen_values ( true ) ,
0 commit comments