aboutsummaryrefslogtreecommitdiff
path: root/tests/pager/driver.cxx
blob: ca3c3b9673fe5014816fe062b36f0373814f7f02 (plain)
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
// file      : tests/pager/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <ios>      // ios_base::failure
#include <vector>
#include <string>
#include <utility>  // move()
#include <sstream>
#include <iostream>
#endif

// Other includes.

#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.pager;
#else
#include <libbutl/pager.mxx>
#endif

using namespace std;
using namespace butl;

int
main (int argc, const char* argv[])
{
  bool child (false);
  bool interactive (false);
  string pgprog;
  vector<string> pgopts;

  assert (argc > 0);

  int i (1);
  for (; i != argc; ++i)
  {
    string v (argv[i]);
    if (pgprog.empty ())
    {
      if (v == "-c")
        child = true;
      else if (v == "-i")
        interactive = true;
      else
      {
        pgprog = move (v);
        interactive = true;
      }
    }
    else
      pgopts.emplace_back (move (v));
  }

  if (i != argc)
  {
    if (!child)
      cerr << "usage: " << argv[0] << " [-c] [-i] [<pager> [<options>]]"
           << endl;

    return 1;
  }

  const char* s (R"delim(
class fdstream_base
{
protected:
  fdstream_base () = default;
  fdstream_base (int fd): buf_ (fd) {}

protected:
  fdbuf buf_;
};

class ifdstream: fdstream_base, public std::istream
{
public:
  ifdstream (): std::istream (&buf_) {}
  ifdstream (int fd): fdstream_base (fd), std::istream (&buf_) {}

  void close () {buf_.close ();}
  void open (int fd) {buf_.open (fd);}
  bool is_open () const {return buf_.is_open ();}
};
)delim");

  if (child)
  {
    string il;
    string ol;
    istringstream is (s);
    do
    {
      getline (cin, il);
      getline (is, ol);
    }
    while (cin.good () && is.good () && il == ol);
    return cin.eof () && !cin.bad () && is.eof () ? 0 : 1;
  }

  try
  {
    string prog (argv[0]);
    vector<string> opts ({"-c"});

    pager p ("pager test",
             false,
             interactive ? (pgprog.empty () ? nullptr : &pgprog) : &prog,
             interactive ? (pgopts.empty () ? nullptr : &pgopts) : &opts);

    p.stream () << s;

    assert (p.wait ());
  }
  catch (const ios_base::failure&)
  {
    assert (false);
  }
  catch (const system_error&)
  {
    assert (false);
  }
}