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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// Copyright 2020-2021 Ian Jackson and contributors to Otter
// SPDX-License-Identifier: AGPL-3.0-or-later
// There is NO WARRANTY.

use crate::imports::*;
use crate::prelude::*;

#[macro_export]
macro_rules! ensure_eq {
  ($v1:expr, $v2:expr) => {
    ({
      let v1 = &$v1;
      let v2 = &$v2;
      if v1 != v2 {
        Err(anyhow!("ensure_eq failed: {} != {}: {:?} != {:?}",
                    stringify!($v1), stringify!($v2),
                    v1, v2))
      } else {
        Ok(())
      }
    }?)
  }
}

#[ext(pub, name=OrdExt)]
impl<T: Ord + Sized + Clone> T {
  fn update_max(&mut self, new: &Self) {
    if *new > *self { *self = new.clone() }
  }
}

#[ext(pub, name=SplitAtDelim)]
impl str {
  fn split_at_delim(&self, delim: char) -> (&Self, &Self) {
    match self.find(delim) {
      Some(index) => self.split_at(index),
      None => (self, ""),
    }
  }
}

#[derive(Copy,Clone,Debug,From,Into)]
#[derive(Hash,Eq,PartialEq,Serialize,Deserialize)]
#[serde(transparent)]
pub struct OldNew<T>([T; 2]);

#[derive(Copy,Clone,Debug)]
#[derive(Hash,Eq,PartialEq)]
pub enum OldNewIndex { Old, New }

impl<T> OldNew<T> {
  pub fn old(&self) -> &T { &self.0[0] }
  pub fn new(&self) -> &T { &self.0[1] }

  pub fn map<U, F: FnMut(&T) -> U>(&self, f: F) -> OldNew<U> {
    OldNew(
      self.iter().map(f)
        .collect::<ArrayVec<U,2>>()
        .into_inner()
        .unwrap_or_else(|_|panic!())
    )
  }

  pub fn as_refs(&self) -> OldNew<&T> {
    OldNew(
      self.iter()
        .collect::<ArrayVec<&T,2>>()
        .into_inner()
        .unwrap_or_else(|_|panic!())
    )
  }

  pub fn iter(&self) -> impl Iterator<Item=&T> {
    self.0.iter()
  }

  pub fn into_iter(self) -> impl Iterator<Item=T> {
    std::array::IntoIter::new(self.0)
  }
}

impl<T> Index<OldNewIndex> for OldNew<T> {
  type Output = T;
  fn index(&self, i: OldNewIndex) -> &T { &self.0[i as usize] }
}

/*
put trait OptionExt {
  type Output;
  fn get_or_try_insert_with<
      E: Error,
      F: FnOnce() -> Result<Output,E>,
    >(&mut self, f: F) -> Result<&mut Output, E>;
}

impl<T> OptionExt for Option<T> {
  type Output = T;
  fn get_or_try_insert_with<E,F>
    (&mut self, f: F) -> Result<&mut Output, E>
    where E: Error, F: FnOnce() -> Result<Output,E>,
  {
    if self.is_none() {
      *self = Some(f()?);
    }
    Ok(self.as_mut().unwrap())
  }
}
*/

// https://github.com/rust-lang/rust/issues/32255 :-(

#[ext(pub, name=LocalFileExt, supertraits=Sized)]
impl fs::File {
  #[throws(io::Error)]
  fn close(self) {
    let r = unsafe {
      let fd = self.into_raw_fd();
      libc::close(fd)
    };
    if r == 0 {
      ()
    } else if r == -1 {
      throw!(io::Error::last_os_error())
    } else {
      panic!("close(2) returned {}", r)
    }
  }
}


// todo #[derive(Clone)]
pub struct Thunk<U: Sync, F: Sync + FnOnce() -> U> (
  lazy_init::LazyTransform<F, U>
);

impl<T: Sync, F: Sync + FnOnce() -> T> Debug for Thunk<T, F> where T: Debug{
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self.0.get() {
      Some(inner) => write!(f, "Thunk({:?})", inner),
      None        => write!(f, "Thunk(...)"),
    }
  }
}

impl<T: Sync, F: Sync + FnOnce() -> T> Thunk<T, F> {
  pub fn new(f: F) -> Self {
    Thunk(
      lazy_init::LazyTransform::new(f)
    )
  }
  pub fn force_eval(thunk: &Self) {
    thunk.0.get_or_create(|f| f());
  }
  pub fn into_inner(thunk: Self) -> T {
    Thunk::force_eval(&thunk);
    thunk.0.into_inner().unwrap_or_else(|_|panic!())
  }
}

impl<T: Sync, F: Sync + FnOnce() -> T> Deref for Thunk<T, F> {
  type Target = T;
  fn deref(&self) -> &T {
    Thunk::force_eval(self);
    self.0.get().unwrap()
  }
}

impl<Y: Sync, E: Sync, F: Sync + FnOnce() -> Result<Y,E>>
  From<Thunk<Result<Y,E>, F>> for Result<Y,E>
{
  fn from(thunk: Thunk<Result<Y,E>, F>) -> Result<Y,E> {
    Thunk::into_inner(thunk)
  }
}

// todo: DerefMut

#[derive(Error,Clone,Copy,Debug,Eq,PartialEq,Serialize,Deserialize)]
#[error("error parsing Z coordinate")]
pub struct FooParseError;

pub mod timespec_serde {
  use super::*;

  #[derive(Serialize, Deserialize)]
  struct Timespec(i64, u32);

  #[throws(S::Error)]
  pub fn serialize<S:Serializer>(v: &TimeSpec, s: S) -> S::Ok {
    let v = Timespec(v.tv_sec().into(), v.tv_nsec().try_into().unwrap());
    Serialize::serialize(&v, s)?
  }
  #[throws(D::Error)]
  pub fn deserialize<'de, D:Deserializer<'de>>(d: D) -> TimeSpec {
    let Timespec(sec, nsec) = Deserialize::deserialize(d)?;
    libc::timespec { tv_sec: sec.into(), tv_nsec: nsec.into() }.into()
  }
}

pub fn toml_merge<'u,
                  S: 'u + AsRef<str>,
                  KV: IntoIterator<Item=(&'u S, &'u toml::Value)>
                  >(
  table: &mut toml::value::Table,
  updates: KV,
) {
  use toml::value::{Table, Value};
  type TME<'e> = toml::map::Entry<'e>;

  let mut kv = updates.into_iter().map(|(k, v)| (k.as_ref(), v));
  inner(table, &mut kv);

  fn inner<'u>(
    table: &mut Table,
    updates: &'u mut dyn Iterator<Item=(&'u str, &'u Value)>
  ) {
    for (k, v) in updates {
      let e = table.entry(k);
      match e {
        TME::Vacant(ve) => {
          ve.insert(v.clone());
        }
        TME::Occupied(mut oe) => match (oe.get_mut(), v) {
          (Value::Table(old), Value::Table(new)) => {
            toml_merge(old, new);
          }
          (Value::Array(old), Value::Array(new)) => {
            old.extend(new.iter().cloned());
          }
          (old, new) => {
            *old = new.clone();
          }
        }
      }
    }
  }
}

#[derive(Debug,Clone)]
pub struct JsonString<T:Serialize>(pub T);
impl<T> Serialize for JsonString<T> where T:Serialize {
  #[throws(S::Error)]
  fn serialize<S>(&self, s: S) -> S::Ok where S:Serializer {
    let json = serde_json::to_string(&self.0)
      .map_err(|e| <S::Error as serde::ser::Error>::custom(e))?;
    Serialize::serialize(&json, s)?
  }
}

#[macro_export]
macro_rules! deref_to_field {
  {$({ $($gen:tt)* })? $outer:ty, $inner:ty, $($field:tt)*} => {
    impl $(< $($gen)* >)? Deref for $outer {
      type Target = $inner;
      fn deref(&self) -> &$inner { &self.$($field)* }
    }
  }
}
#[macro_export]
macro_rules! deref_to_field_mut {
  {$({ $($gen:tt)* })? $outer:ty, $inner:ty, $($field:tt)*} => {
    deref_to_field!{ $({ $($gen)* })? $outer, $inner, $($field)*}
    impl $(< $($gen)* >)? DerefMut for $outer {
      fn deref_mut(&mut self) -> &mut $inner { &mut self.$($field)* }
    }
  }
}

#[derive(Debug)]
pub enum Loop<E> {
  Continue,
  Break,
  Error(E),
}
impl<E> From<E> for Loop<E> {
  fn from(e: E) -> Loop<E> { Loop::Error(e) }
}
impl Loop<Infallible> {
  pub fn ok<T>(t: T) -> Result<T,Loop<Infallible>> { Ok(t) }
}

pub trait IteratorExt<U,E,F>: Iterator
  where F: FnMut(Self::Item) -> Result<U,Loop<E>>,
{
  type Return: Iterator<Item=U>;
  fn map_loop(self, f: F) -> Self::Return where E: EmptyType;

  type TryReturn: Iterator<Item=Result<U,E>>;
  fn try_map_loop(self, f: F) -> Self::TryReturn;
}

pub trait EmptyType { fn diverge<T>(self) -> T; }

impl EmptyType for Infallible {
  fn diverge<T>(self) -> T { match self { } }
}

impl<T,U,E,F> IteratorExt<U,E,F> for T where
  T: Iterator,
  F: FnMut(Self::Item) -> Result<U,Loop<E>>,
{
  type Return = impl Iterator<Item=U>;
  fn map_loop(self, f: F) -> Self::Return where E: EmptyType {
    self
      .map(f)
      .filter(|i| !matches!(i, Err(Loop::Continue)))
      .take_while(|i| !matches!(i, Err(Loop::Break)))
      .map(|i| i.ok().unwrap())
  }

  type TryReturn = impl Iterator<Item=Result<U,E>>;
  fn try_map_loop(self, f: F) -> Self::TryReturn {
    self
      .map(f)
      .filter(|i| matches!(i, Err(Loop::Continue)))
      .take_while(|i| matches!(i, Err(Loop::Break)))
      .map(|i| match i {
        Ok(y) => Ok(y),
        Err(Loop::Error(e)) => Err(e),
        _ => panic!(),
      })
  }
}

#[macro_export] // <- otherwise bogus warning `unused_macros`
macro_rules! matches_doesnot_yn2bool {
  (=) => (true);
  (!) => (false);
}

#[macro_export]
macro_rules! matches_doesnot {
  ($v:expr,
   $(
     $yn:tt $($p:pat)|*
   ),* $(,)?
  ) => {
    match $v {
      $(
        $($p)|* => $crate::matches_doesnot_yn2bool!($yn),
      )*
    }
  }
}

#[test]
fn matches_doesnot_test() {
  assert!(
    matches_doesnot!(
      Some(42),
      = Some(_),
      ! None
    )
  );
  assert!(
    matches_doesnot!(
      Some(42),
      ! None,
      ! Some(3),
      = Some(_),
    )
  );
  assert!(
    matches_doesnot!(
      Some(1),
      = Some(1) | Some(2),
      ! Some(_) | None
    )
  );
  assert!(
    ! matches_doesnot!(
      Some(1),
      ! Some(1) | Some(2),
      = Some(_) | None
    )
  );
}

pub fn dbgc_helper(file: &'static str, line: u32,
                   values: &[(&'static str, &dyn Debug)]) {
  let buf = (||{
    let mut buf = String::new();
    write!(buf, "[{}:{}]", file, line)?;
    for (s, v) in values.iter() {
      write!(buf, " {}={:?}", s, v)?;
    }
    write!(buf, "\n")?;
    Ok::<_,fmt::Error>(buf)
  })();
  let buf = buf.unwrap_or_else(
    |e| format!("error formatting for dbgc! {}\n", e));
  eprint!("{}", buf);
}

#[macro_export]
macro_rules! dbgc {
    // NOTE: We cannot use `concat!` to make a static string as a format argument
    // of `eprintln!` because `file!` could contain a `{` or
    // `$val` expression could be a block (`{ .. }`), in which case the `eprintln!`
    // will be malformed.
    () => {
      dbgc_helper(std::file!(), std::line!(), &[])
    };
    ($val:expr $(,)?) => {
        // Use of `match` here is intentional because it affects the lifetimes
        // of temporaries - https://stackoverflow.com/a/48732525/1063961
        match $val {
            tmp => {
                dbgc_helper(std::file!(), std::line!(),
                            &[(std::stringify!($val), &tmp)]);
                tmp
            }
        }
    };
    ($($val:expr),+ $(,)?) => {
      dbgc_helper(std::file!(), std::line!(),
                  &[$((std::stringify!($val), &$val)),+])
    };
}

#[macro_export]
macro_rules! trace_dbg {
  ($msg:expr $(,$val:expr)*) => {
    use log::*;
    use Level::*;
    if log_enabled!(Trace) {
      let mut buf = format!("{}", &$msg);
      $( write!(&mut buf, " {}={:?}", stringify!($val), &$val).unwrap(); )*
      trace!("{}", buf);
    }
  }

}

#[macro_export]
macro_rules! want_failed_internal {
  { $variant:ident($binding:pat) = $input:expr, $x:expr, $($d:expr),* } => {
    InternalLogicError::new({
      #[allow(unused_mut)]
      let mut s = format!("wanted {}({}) = {}, but got {:?}",
	                  stringify!($variant), stringify!($binding),
                          stringify!($input), $x);
      $(
        write!(&mut s, " {}={:?}", stringify!($d), &$d).unwrap();
      )*
      s
    }).tolerate()
  }
}

#[macro_export]
macro_rules! want {
  { $variant:ident = $input:expr,
    ? $($d:expr),*
  } => (
    match $input {
      $variant(y) => Some(y),
      x => {
        want_failed_internal!{ $variant(_)=$input, x, $($d),* }
        None
      },
    };
  );
  { $variant:ident = $input:expr } => {
    want!( $variant = $input,
           ? )
  };
}

#[macro_export]
macro_rules! wants {
  { $($d:tt)* } => { want!(Some = $($d)*) }
}
#[macro_export]
macro_rules! wantok {
  { $($d:tt)* } => { want!(Ok = $($d)*) }
}

#[macro_export]
macro_rules! want_let {
  { $($variant:ident)::+($binding:pat) = $input:expr;
    else ? $($d:expr),*; $($otherwise:tt)*
  } => {
    let $binding = match $input {
      $($variant(y))::+ => y,
      x => {
        want_failed_internal!{
          $($variant)::+($binding)=$input, x, $($d),*
        }
        $($otherwise)*
      },
    };
  };
  { $($variant:ident)::+($binding:pat) = $input:expr;
    else $($otherwise:tt)*
  } => {
    want_let!{ $($variant($binding))::+ = $input; else ?; $($otherwise)* }
  };
}

macro_rules! entry_define_insert_remove {
  { $name:ident, $name_mod:ident, $entry:path, $into_key:ident } =>
  {
    #[allow(non_snake_case)]
    mod $name_mod {
      use $crate::imports::extend::ext;
      use $entry as Entry;
      use Entry::{Occupied, Vacant};
      #[ext(pub, name=EntryExt)]
      impl<'e,K,V> Entry<'e,K,V> where K: slotmap::Key {
        fn insert(self, v: V) {
          match self {
            Vacant(ve)   => { ve.insert(v); }
            Occupied(mut oe) => { oe.insert(v); }
          }
        }
        fn remove(self) -> (K, Option<V>) {
          match self {
            Vacant(ve)   => { let k = ve.$into_key();        (k, None)    }
            Occupied(oe) => { let (k,v) = oe.remove_entry(); (k, Some(v)) }
          }
        }
      }
    }
    pub use $name_mod::EntryExt as $name;
  }
}

entry_define_insert_remove!{
  SlotmapSparseSecondaryEntryExt,
  SlotmapSparseSecondaryEntryExt_mod,
  slotmap::sparse_secondary::Entry,
  key
}

#[derive(Debug,Copy,Clone)]
pub struct DigestRead<D: Digest, R: Read> {
  d: D,
  r: R,
}

impl<D: Digest, R: Read> DigestRead<D, R> {
  pub fn new(r: R) -> Self { DigestRead { r, d: D::new() } }
  pub fn into_inner(self) -> (D, R) { (self.d, self.r) }
  pub fn finish(self) -> digest::Output<D> {
    self.d.finalize()
  }
}

impl<D: Digest, R: Read> Read for DigestRead<D, R> {
  #[throws(io::Error)]
  fn read(&mut self, buf: &mut [u8]) -> usize {
    let count = self.r.read(buf)?;
    self.d.update(&buf[0..count]);
    count
  }
}

#[test]
fn test_digest_read() {
  let ibuffer = b"abc";
  let exp = Sha512Trunc256::digest(&ibuffer[..]);
  let inner = &ibuffer[..];
  let mut dr = DigestRead::<Sha512Trunc256,_>::new(inner);
  let mut obuffer = [0;4];
  assert_eq!( dr.read(&mut obuffer).unwrap(), 3 );
  assert_eq!( &obuffer, b"abc\0" );
  let got = dr.finish();
  assert_eq!( got, exp );
}

#[derive(Debug,Copy,Clone)]
pub struct DigestWrite<D: Digest, W: Write> {
  d: D,
  w: W,
}

impl<D: Digest, W: Write> DigestWrite<D, W> {
  pub fn new(w: W) -> Self { DigestWrite { w, d: D::new() } }
  pub fn into_inner(self) -> (D, W) { (self.d, self.w) }
  pub fn finish(self) -> (digest::Output<D>, W) {
    (self.d.finalize(), self.w)
  }
}
impl<D: Digest> DigestWrite<D, io::Sink> {
  pub fn sink() -> Self { DigestWrite::new(io::sink()) }

  #[throws(io::Error)]
  pub fn of<R>(r: &mut R) -> digest::Output<D> where R: Read {
    let mut dw = DigestWrite::<D,_>::sink();
    io::copy(r, &mut dw)?;
    dw.finish().0
  }
}

impl<D: Digest, W: Write> Write for DigestWrite<D, W> {
  #[throws(io::Error)]
  fn write(&mut self, buf: &[u8]) -> usize {
    let count = self.w.write(buf)?;
    self.d.update(&buf[0..count]);
    count
  }
  #[throws(io::Error)]
  fn flush(&mut self) { self.w.flush()? }
}

#[test]
fn test_digest_write() {
  let ibuffer = b"xyz";
  let exp = Sha512Trunc256::digest(&ibuffer[..]);
  let mut obuffer = [0;4];
  let inner = &mut obuffer[..];
  let mut dw = bundles::DigestWrite::new(inner);
  assert_eq!( dw.write(&ibuffer[..]).unwrap(), 3);
  let (got, recov) = dw.finish();
  assert_eq!( recov, b"\0" );
  assert_eq!( got, exp );
  assert_eq!( &obuffer, b"xyz\0" );
}

#[ext(pub, name=SeekExt)]
impl<T: io::Seek> T {
  #[throws(io::Error)]
  fn rewind(&mut self) { self.seek(io::SeekFrom::Start(0))? }
}

#[ext(pub)]
impl<T> Vec<T> {
  fn get_or_extend_with<F>(&mut self, i: usize, f: F) -> &mut T
  where F: FnMut() -> T {
    if self.get(i).is_none() {
      self.resize_with(i+1, f);
    }
    &mut self[i]
  }
}

#[ext(pub)]
impl<I,T> IndexVec<I,T> where I: index_vec::Idx {
  fn get_or_extend_with<F>(&mut self, i: I, f: F) -> &mut T
  where F: FnMut() -> T {
    self.raw.get_or_extend_with(i.index(), f)
  }
}

#[derive(Debug)]
pub struct AnyhowDisplay<'a>(pub &'a anyhow::Error);
impl Display for AnyhowDisplay<'_> {
  #[throws(fmt::Error)]
  fn fmt(&self, f: &mut fmt::Formatter) {
    let mut delim = "";
    self.0.for_each(&mut |s|{
      write!(f, "{}{}", delim, s)?;
      delim = ": ";
      Ok(())
    })?;
  }
}

#[ext(pub)]
impl anyhow::Error {
  fn for_each(&self, f: &mut dyn FnMut(&str) -> fmt::Result) -> fmt::Result {
    let mut done = String::new();
    for e in self.chain() {
      let s = e.to_string();
      if done.contains(&s) { continue }
      f(&s)?;
      done = s;
    }
    Ok(())
  }

  fn d(&self) -> AnyhowDisplay<'_> { AnyhowDisplay(self) }

  fn end_process(self, estatus: u8) -> ! {
    #[derive(Default,Debug)] struct Sol { any: bool, progname: String }
    impl Sol {
      fn nl(&mut self) {
        if self.any { eprintln!("") };
        self.any = false;
      }
      fn head(&mut self) {
        if ! self.any { eprint!("{}: error", &self.progname); }
        self.any = true
      }
    }
    let mut sol: Sol = Sol { any: false, progname: program_name() };
    self.for_each(&mut |s|{
      let long = s.len() > 80;
      if long && sol.any { sol.nl() }
      sol.head();
      eprint!(": {}", &s);
      if long { sol.nl() }
      Ok::<_,fmt::Error>(())
    }).unwrap();
    sol.nl();
    assert!(estatus > 0);
    exit(estatus.into());
  }
}

#[throws(Either<io::Error, io::Error>)]
pub fn io_copy_interactive<R,W>(read: &mut BufReader<R>, write: &mut W)
where R: Read, W: Write {
  loop {
    let buf = read.fill_buf().map_err(Either::Left)?;
    if buf.len() == 0 { break }

    let did = (||{
      let did = write.write(buf)?;
      if did == 0 { throw!(ErrorKind::WriteZero) }
      Ok::<_,io::Error>(did)
    })().map_err(Either::Right)?;
        
    read.consume(did);
    write.flush().map_err(Either::Right)?;
  }
}

pub struct SigPipeWriter<W>(pub W);

impl<W:Write> SigPipeWriter<W> {
  fn handle_err(e: io::Error) -> io::Error {
    if e.kind() != ErrorKind::BrokenPipe { return e }

    match (||{
      use nix::sys::signal::*;
      use Signal::SIGPIPE;
      unsafe {
        sigaction(SIGPIPE, &SigAction::new(
          SigHandler::SigDfl, SaFlags::empty(), SigSet::empty()))
          .context("sigaction")?;
      };
      raise(SIGPIPE).context("raise")?;
      Err::<Void,_>(anyhow!("continued after raise"))
    })() {
      Err(ae) => ae
        .context("attempt to die with SIGPIPE failed")
        .end_process(127),
      Ok(v) => match v { },
    }
  }
}

impl<W:Write> Write for SigPipeWriter<W> {
  fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
    self.0.write(buf).map_err(Self::handle_err)
  }
  fn flush(&mut self)             -> io::Result<()>    {
    self.0.flush()   .map_err(Self::handle_err)
  }
}

pub type RawStdout = SigPipeWriter<io::Stdout>;
impl RawStdout { pub fn new() -> Self { SigPipeWriter(io::stdout()) } }

pub struct CookedStdout(pub BufWriter<SigPipeWriter<io::Stdout>>);
impl CookedStdout {
  pub fn new() -> Self { Self(BufWriter::new(RawStdout::new())) }
  fn handle_err(e: io::Error) -> ! {
    AE::from(e).context("write stdout").end_process(EXIT_DISASTER as _);
  }
  fn must_flush(&mut self) {
    self.0.flush().unwrap_or_else(|e| Self::handle_err(e))
  }
}
impl Write for CookedStdout {
  #[throws(io::Error)]
  fn write(&mut self, buf: &[u8]) -> usize {
    let r = self.0.write(buf).unwrap_or_else(|e| Self::handle_err(e));
    if buf.contains(&b'\n') { self.flush()? }
    r
  }
  #[throws(io::Error)]
  fn flush(&mut self) { self.must_flush() }
}
impl Drop for CookedStdout {
  fn drop(&mut self) { self.must_flush() }
}

#[throws(fmt::Error)]
pub fn fmt_hex(f: &mut Formatter, buf: &[u8]) {
  for v in buf { write!(f, "{:02x}", v)?; }
}

#[throws(as Option)]
#[must_use]
pub fn parse_slice_hex(s: &str, buf: &mut [u8]) -> usize {
  let l = s.len();
  if l % 1 != 0 { throw!() }
  let l = l/2;
  if l > buf.len() { throw!() }

  for (h, o) in izip!(
    s.as_bytes().chunks(2),
    buf.iter_mut(),
  ) {
    let h = str::from_utf8(h).ok()?;
    *o = u8::from_str_radix(h,16).ok()?;
  }

  l
}

#[throws(as Option)]
pub fn parse_fixed_hex<const N: usize>(s: &str) -> [u8; N] {
  let mut buf = [0u8; N];
  let l = parse_slice_hex(s, &mut buf)?;
  if l != N { throw!() }
  buf
}

#[macro_export]
macro_rules! format_by_fmt_hex {
  ($trait:ty, for $self:ty, . $($memb:tt)+) => {
    impl $trait for $self {
      #[throws(fmt::Error)]
      fn fmt(&self, f: &mut Formatter) {
        fmt_hex(f, &self . $($memb)+)?
      }
    }
  }
}

#[test]
fn test_parse_hex(){
  assert_eq!( parse_fixed_hex(""),     Some([          ]) );
  assert_eq!( parse_fixed_hex("41"  ), Some([b'A'      ]) );
  assert_eq!( parse_fixed_hex("4165"), Some([b'A', b'e']) );
  assert_eq!( parse_fixed_hex("4165"), Some([b'A', b'e']) );
  assert_eq!( parse_fixed_hex("41"  ), None::<[_;0]>      );
  assert_eq!( parse_fixed_hex("41"  ), None::<[_;2]>      );
  assert_eq!( parse_fixed_hex("1"   ), None::<[_;1]>      );
  assert_eq!( parse_fixed_hex("xy"  ), None::<[_;1]>      );
}