bash_builtins/
args.rs

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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Module to implement the arguments processor.

use crate::{ffi, Error};
use std::ffi::CStr;
use std::marker::PhantomData;
use std::mem;
use std::os::raw::c_int;
use std::str::Utf8Error;

/// This structure provides access to the command-line arguments passed to the
/// builtin.
///
/// An instance for this type is sent as an argument of [`Builtin::call`].
///
/// # Options
///
/// The [`options`] method parses the arguments with the `internal_getopt`
/// function provided by bash, like most builtins, and extract them as values
/// of a type implementing [`BuiltinOptions`].
///
/// If the builtin does not expect any option, call to [`no_options`] before
/// doing anything else.
///
/// # Free Arguments
///
/// The iterators returned by [`raw_arguments`], [`string_arguments`], and
/// [`path_arguments`] yield the argument values.
///
/// If you use [`options`] before any of the `<type>_arguments` methods, the
/// first item of the iteration is the first argument after the last parsed
/// option.
///
/// The example below shows how to extract options and then process free
/// arguments.
///
/// If the builtin accepts options, but no free arguments, call to
/// [`finished`] after [`options`].
///
/// If the builtin does not expect any option or free argument, call to
/// [`finished`] after [`no_options`].
///
/// # Example
///
/// ```
/// use std::io::{stdout, BufWriter, Write};
/// use bash_builtins::{Args, Builtin, BuiltinOptions, Result};
///
/// struct SomeName;
///
/// #[derive(BuiltinOptions)]
/// enum Opt {
///     #[opt = 'f']
///     Foo,
///
///     #[opt = 'b']
///     Bar(i64),
/// }
///
/// impl Builtin for SomeName {
///     fn call(&mut self, args: &mut Args) -> Result<()> {
///         let mut foo = false;
///         let mut bar = 0;
///
///         for option in args.options() {
///             match option? {
///                 Opt::Foo => foo = true,
///                 Opt::Bar(b) => bar = b,
///             }
///         }
///
///         let stdout_handle = stdout();
///         let mut output = BufWriter::new(stdout_handle.lock());
///
///         writeln!(&mut output, "{}, {}", foo, bar)?;
///
///         for path in args.path_arguments() {
///             writeln!(&mut output, "{}", path.display())?;
///         }
///
///         Ok(())
///     }
/// }
/// ```
///
/// [`Builtin::call`]: crate::Builtin::call
/// [`BuiltinOptions`]: bash_builtins_macro::BuiltinOptions
/// [`finished`]: Args::finished
/// [`no_options`]: Args::no_options
/// [`options`]: Args::options
/// [`path_arguments`]: Args::path_arguments
/// [`raw_arguments`]: Args::raw_arguments
/// [`string_arguments`]: Args::string_arguments
pub struct Args {
    word_list: *const ffi::WordList,
    reset_pending: bool,
}

impl Args {
    /// Create a new instance to wrap the `word_list` created by bash.
    ///
    /// # Safety
    ///
    /// The method is unsafe for two reasons:
    ///
    /// * The caller has to provide a valid `word_list` pointer, which
    ///   is sent by bash when the builtin function is invoked.
    ///
    /// * This `Args` instance has to be dropped before returning to bash,
    ///   since the `word_list` address will be reused by bash to something
    ///   else.
    ///
    ///   This requirement is accomplished by using a mutable reference
    ///   (`&mut Args`) when calling to `Builtin::call`. Thus, users can't
    ///   keep a copy of this `Args` instance.
    #[doc(hidden)]
    pub unsafe fn new(word_list: *const ffi::WordList) -> Args {
        Args {
            word_list,
            reset_pending: true,
        }
    }

    /// Returns `true` if there are no more arguments.
    pub fn is_empty(&self) -> bool {
        self.word_list.is_null()
    }

    /// Returns an iterator to parse the command-line arguments with the
    /// `getops` function provided by bash.
    ///
    /// The generic type `T` implements the [`BuiltinOptions`] trait. See its
    /// documentation for details on how to create the parser.
    ///
    /// # Example
    ///
    /// See the [example](struct.Args.html#example) in the [`Args`](self::Args)
    /// documentation.
    ///
    /// [`BuiltinOptions`]: derive.BuiltinOptions.html
    pub fn options<'a, T>(&'a mut self) -> impl Iterator<Item = crate::Result<T>> + 'a
    where
        T: crate::BuiltinOptions<'a> + 'a,
    {
        self.ensure_reset();
        OptionsIterator {
            args: self,
            phantom: PhantomData,
        }
    }

    /// Returns an iterator to get the arguments passed to the builtin.
    ///
    /// Each item is an instance of [`CStr`], and its lifetime is bound to the
    /// [`Args`] instance.
    ///
    /// If this method is called after [`options`](Args::options), the first
    /// item of the iteration is the first argument after the last parsed
    /// option.
    ///
    /// It is recommended to use [`path_arguments`] if the builtin expects file
    /// names as arguments, or [`string_arguments`] if it expects valid UTF-8
    /// strings.
    ///
    /// # Example
    ///
    /// See [`path_arguments`] for an example.
    ///
    /// [`CStr`]: std::ffi::CStr
    /// [`path_arguments`]: Args::path_arguments
    /// [`string_arguments`]: Args::string_arguments
    pub fn raw_arguments(&mut self) -> impl Iterator<Item = &'_ CStr> {
        self.ensure_reset();

        WordListIterator(self)
    }

    /// Like [`raw_arguments`], but items are [`Path`] instances.
    ///
    /// Use this iterator if the builtin arguments are file names.
    ///
    /// # Example
    ///
    /// ```
    /// use std::path::Path;
    /// use bash_builtins::{Args, Builtin, Result};
    ///
    /// struct SomeName;
    ///
    /// impl Builtin for SomeName {
    ///     fn call(&mut self, args: &mut Args) -> Result<()> {
    ///         args.no_options()?;
    ///
    ///         for path in args.path_arguments() {
    ///             process(path)?;
    ///         }
    ///
    ///         Ok(())
    ///     }
    /// }
    ///
    /// fn process(path: &Path) -> Result<()> {
    ///     // …
    /// #   let _ = path;
    ///     Ok(())
    /// }
    /// ```
    ///
    /// [`raw_arguments`]: Args::raw_arguments
    /// [`Path`]: std::path::Path
    #[cfg(unix)]
    #[cfg_attr(docsrs, doc(cfg(unix)))]
    pub fn path_arguments(&mut self) -> impl Iterator<Item = &'_ std::path::Path> {
        use std::ffi::OsStr;
        use std::os::unix::ffi::OsStrExt;
        use std::path::Path;

        self.raw_arguments()
            .map(|a| Path::new(OsStr::from_bytes(a.to_bytes())))
    }

    /// Like [`raw_arguments`], but each item is a string reference if the
    /// argument contains valid UTF-8 data, or a [`Utf8Error`] otherwise.
    ///
    /// [`Utf8Error`]: std::str::Utf8Error
    /// [`raw_arguments`]: Args::raw_arguments
    pub fn string_arguments(&mut self) -> impl Iterator<Item = Result<&'_ str, Utf8Error>> {
        self.raw_arguments()
            .map(|a| std::str::from_utf8(a.to_bytes()))
    }

    /// Returns an error if there are more arguments to be processed.
    ///
    /// If the builtin accepts options but no free arguments, then this method
    /// should be called after [`options`].
    ///
    /// # Example
    ///
    /// ```
    /// # use bash_builtins::{Args, Builtin, BuiltinOptions, Result};
    /// # struct SomeName;
    /// #[derive(BuiltinOptions)]
    /// enum Opt {
    ///     // Builtin options.
    /// #   #[opt = 'a'] A,
    /// }
    ///
    /// impl Builtin for SomeName {
    ///     fn call(&mut self, args: &mut Args) -> Result<()> {
    ///         for option in args.options() {
    ///             match option? {
    ///                 // Parse options.
    /// #               Opt::A => ()
    ///             }
    ///         }
    ///
    ///         // This builtin does not accept free arguments.
    ///         args.finished()?;
    ///
    /// #       fn run_builtin_with_options() -> Result<()> { Ok(()) }
    ///         run_builtin_with_options()?;
    ///
    ///         Ok(())
    ///     }
    /// }
    /// ```
    ///
    /// [`options`]: Args::options
    pub fn finished(&mut self) -> crate::Result<()> {
        if self.word_list.is_null() {
            Ok(())
        } else {
            crate::log::error("too many arguments");
            Err(Error::Usage)
        }
    }

    /// Returns an error if any option is passed as the first argument.
    ///
    /// If the builtin expects no options, then call this method before doing
    /// anything else.
    ///
    /// It uses the `no_options` function provided by bash. The special option
    /// `--help` is handled properly.
    ///
    /// # Example
    ///
    /// ```
    /// // Builtin to convert to uppercase its arguments.
    ///
    /// # use std::io::{self, BufWriter, Write};
    /// # use bash_builtins::{Args, Builtin, Result};
    /// # struct Upcase;
    /// impl Builtin for Upcase {
    ///     fn call(&mut self, args: &mut Args) -> Result<()> {
    ///         args.no_options()?;
    ///
    ///         let stdout_handle = io::stdout();
    ///         let mut output = BufWriter::new(stdout_handle.lock());
    ///
    ///         for argument in args.string_arguments() {
    ///             writeln!(&mut output, "{}", argument?.to_uppercase())?;
    ///         }
    ///
    ///         Ok(())
    ///     }
    /// }
    /// ```
    pub fn no_options(&mut self) -> crate::Result<()> {
        if unsafe { ffi::no_options(self.word_list) } == 0 {
            Ok(())
        } else {
            Err(Error::Usage)
        }
    }

    /// Reset `internal_getopt` state.
    #[inline]
    fn ensure_reset(&mut self) {
        if mem::take(&mut self.reset_pending) {
            unsafe {
                ffi::reset_internal_getopt();
                ffi::list_optarg = std::ptr::null();
                ffi::list_optopt = 0;
            };
        }
    }
}

struct WordListIterator<'a>(&'a mut Args);

impl<'a> Iterator for WordListIterator<'a> {
    type Item = &'a CStr;

    fn next(&mut self) -> Option<Self::Item> {
        if self.0.word_list.is_null() {
            return None;
        }

        let word = unsafe {
            let current = &*self.0.word_list;
            self.0.word_list = current.next;
            CStr::from_ptr((*current.word).word)
        };

        Some(word)
    }
}

/// Trait implemented by the `BuiltinOptions` derive macro.
#[doc(hidden)]
pub trait BuiltinOptions<'a>: Sized {
    fn options() -> &'static [u8];

    fn from_option(opt: c_int, arg: Option<&'a CStr>) -> crate::Result<Self>;
}

struct OptionsIterator<'a, T> {
    args: &'a mut Args,
    phantom: PhantomData<T>,
}

impl<'a, T: BuiltinOptions<'a>> Iterator for OptionsIterator<'a, T> {
    type Item = crate::Result<T>;

    fn next(&mut self) -> Option<Self::Item> {
        let opt =
            unsafe { ffi::internal_getopt(self.args.word_list, T::options().as_ptr().cast()) };

        match opt {
            ffi::GETOPT_EOF => {
                self.args.word_list = unsafe { ffi::loptend };
                None
            }

            ffi::GETOPT_HELP => {
                crate::log::show_help();
                Some(Err(Error::Usage))
            }

            _ => Some(T::from_option(opt, unsafe { Self::optarg() })),
        }
    }
}

impl<'a, T> OptionsIterator<'a, T> {
    unsafe fn optarg() -> Option<&'a CStr> {
        let optarg = ffi::list_optarg;
        if optarg.is_null() {
            None
        } else {
            Some(::std::ffi::CStr::from_ptr(optarg))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ffi::{WordDesc, WordList};

    #[test]
    fn update_word_list_after_arguments() {
        let words = [
            WordDesc {
                word: b"abc\0".as_ptr().cast(),
                flags: 0,
            },
            WordDesc {
                word: b"def\0".as_ptr().cast(),
                flags: 0,
            },
        ];

        let wl1 = WordList {
            word: &words[1],
            next: std::ptr::null(),
        };

        let wl0 = WordList {
            word: &words[0],
            next: &wl1,
        };

        let mut args = unsafe { Args::new(&wl0) };

        let mut string_args = args.string_arguments();
        assert_eq!(string_args.next(), Some(Ok("abc")));
        assert_eq!(string_args.next(), Some(Ok("def")));
        assert_eq!(string_args.next(), None);
        drop(string_args);

        args.finished().unwrap();
    }
}