aboutsummaryrefslogtreecommitdiff
path: root/bpkg/help.cxx
blob: 6c1608853588e8d16046705b60d6d6e87ecf9463 (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
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
// file      : bpkg/help.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bpkg/help>

#ifndef _WIN32
#  include <unistd.h>    // close()
#else
#  include <io.h>        // _close()
#endif

#include <chrono>
#include <thread>    // this_thread::sleep_for()
#include <iostream>

#include <butl/process>
#include <butl/fdstream>

#include <bpkg/types>
#include <bpkg/utility>
#include <bpkg/diagnostics>

#include <bpkg/bpkg-options>

using namespace std;
using namespace butl;

namespace bpkg
{
  struct pager
  {
    pager (const common_options& co, const string& name)
    {
      cstrings args;
      string prompt;

      bool up (co.pager_specified ()); // User's pager.

      if (up)
      {
        if (co.pager ().empty ())
          return; // No pager should be used.

        args.push_back (co.pager ().c_str ());
      }
      else
      {
        // By default try less.
        //
        prompt = "-Ps" + name + " (press q to quit, h for help)";

        args.push_back ("less");
        args.push_back ("-R");            // Handle ANSI color.
        args.push_back (prompt.c_str ());
      }

      // Add extra pager options.
      //
      for (const string& o: co.pager_option ())
        args.push_back (o.c_str ());

      args.push_back (nullptr);

      if (verb >= 2)
        print_process (args);

      // Ignore errors and go without a pager unless the pager was specified
      // by the user.
      //
      try
      {
        p_ = process (args.data (), -1); // Redirect child's stdin to a pipe.

        // Wait a bit and see if the pager has exited before reading
        // anything (e.g., because exec() couldn't find the program).
        // If you know a cleaner way to handle this, let me know (no,
        // a select()-based approach doesn't work; the pipe is buffered
        // and therefore is always ready for writing).
        //
        this_thread::sleep_for (chrono::milliseconds (50));

        bool r;
        if (p_.try_wait (r))
        {
#ifndef _WIN32
          ::close (p_.out_fd);
#else
          _close (p_.out_fd);
#endif
          if (up)
            fail << "pager " << args[0] << " exited unexpectedly";
        }
        else
          os_.open (p_.out_fd);
      }
      catch (const process_error& e)
      {
        // Ignore unless it was a user-specified pager.
        //
        if (up)
          error << "unable to execute " << args[0] << ": " << e.what ();

        if (e.child ())
          exit (1);

        if (up)
          throw failed ();
      }
    }

    std::ostream&
    stream ()
    {
      return os_.is_open () ? os_ : std::cout;
    }

    bool
    wait ()
    {
      os_.close ();
      return p_.wait ();
    }

    ~pager () {wait ();}

  private:
    process p_;
    ofdstream os_;
  };

  int
  help (const help_options& o, const string& t, usage_function* usage)
  {
    if (usage == nullptr) // Not a command.
    {
      if (t.empty ())             // General help.
        usage = &print_bpkg_usage;
      else if (t == "common-options")  // Help topics.
        usage = &print_bpkg_common_options_long_usage;
      else
        fail << "unknown bpkg command/help topic '" << t << "'" <<
          info << "run 'bpkg help' for more information";
    }

    pager p (o, "bpkg " + (t.empty () ? "help" : t));
    usage (p.stream (), cli::usage_para::none);

    // If the pager failed, assume it has issued some diagnostics.
    //
    return p.wait () ? 0 : 1;
  }
}