summaryrefslogtreecommitdiff
path: root/cli/man.cxx
blob: 2cbc766b2c6a472287109305dfa6de7392ef0975 (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
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
// file      : cli/man.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <vector>
#include <iostream>

#include "man.hxx"

using namespace std;

namespace
{
  static string
  escape_line (const string& s, size_t b, size_t e)
  {
    string r;
    size_t n (e - b);

    // Escaping leading '.' with '\' is not sufficient.
    //
    if (n > 1 && s[b] == '\\' && s[b + 1] == '.')
      r = "\\&";

    r.append (s, b, n);
    return r;
  }

  static void
  wrap_lines (ostream& os, const string& d)
  {
    size_t b (0), e (0), i (0);
    for (size_t n (d.size ()); i < n; ++i)
    {
      // First handle preformatted text (.nf/.fi).
      //
      if (d.compare (i, 4, ".nf\n") == 0 && (i == 0 || d[i - 1] == '\n'))
      {
        assert (b == i); // We should have nothing accumulated.

        // Output everything until (and including) closing .fi as is.
        //
        e = d.find ("\n.fi", i + 4);
        assert (e != string::npos);
        e += 4; // Now points past 'i'.

        os << string (d, i, e - i);

        b = e;
        i = e - 1; // For ++i in loop header.
        continue;
      }

      if (d[i] == ' ' || d[i] == '\n')
        e = i;

      if (d[i] == '\n' || (i - b >= 78 && e != b))
      {
        os << escape_line (d, b, e) << endl;
        b = e = e + 1;
      }
    }

    // Flush the last line.
    //
    if (b != i)
      os << escape_line (d, b, i);
  }

  struct doc: traversal::doc, context
  {
    doc (context& c): context (c) {}

    virtual void
    traverse (type& ds)
    {
      if (ds.name ().compare (0, 3, "doc") != 0) // Ignore doc variables.
        return;

      // n = 1 - common doc string
      // n = 2 - arg string, common doc string
      // n > 2 - arg string, usage string, man string
      //
      size_t n (ds.size ());
      const string& d (n == 1 ? ds[0] : n == 2 ? ds[1] : ds[2]);

      if (d.empty ())
        return;

      std::set<string> arg_set;
      if (n > 1)
        translate_arg (ds[0], arg_set);

      string s (format (ot_man, translate (d, arg_set), true));

      wrap_lines (os, s);
      os << endl;
    }
  };

  struct option: traversal::option, context
  {
    option (context& c) : context (c) {}

    virtual void
    traverse (type& o)
    {
      using semantics::names;

      semantics::doc_strings const& doc (o.doc ());

      if (options.suppress_undocumented () && doc.empty ())
        return;

      names& n (o.named ());

      os << ".IP \"\\fB";

      for (names::name_iterator i (n.name_begin ()); i != n.name_end (); ++i)
      {
        if (i != n.name_begin ())
          os << "\\fP|\\fB";

        os << *i;
      }

      os << "\\fP";

      string type (o.type ().name ());

      std::set<string> arg_set;

      if (type != "bool" || doc.size () >= 3)
      {
        string s (
          translate_arg (
            doc.size () > 0 ? doc[0] : string ("<arg>"), arg_set));

        os << ' ' << format (ot_man, s, false);
      }

      os << "\"" << endl;

      string d;

      // If we have both the long and the short descriptions, use
      // the long one.
      //
      if (type == "bool" && doc.size () < 3)
      {
        if (doc.size () > 1)
          d = doc[1];
        else if (doc.size () > 0)
          d = doc[0];
      }
      else
      {
        if (doc.size () > 2)
          d = doc[2];
        else if (doc.size () > 1)
          d = doc[1];
      }

      // Format the documentation string.
      //
      d = format (ot_man, translate (d, arg_set), false);

      wrap_lines (os, d);
      os << endl;
    }
  };

  //
  //
  struct class_: traversal::class_, context
  {
    class_ (context& c)
        : context (c), option_ (c)
    {
      *this >> inherits_base_ >> *this;
      names_option_ >> option_;
    }

    virtual void
    traverse (type& c)
    {
      if (!options.exclude_base ())
        inherits (c, inherits_base_);

      names (c, names_option_);
    }

  private:
    traversal::inherits inherits_base_;

    option option_;
    traversal::names names_option_;
  };
}

void
generate_man (context& ctx)
{
  traversal::cli_unit unit;
  traversal::names unit_names;
  traversal::namespace_ ns;
  doc dc (ctx);
  class_ cl (ctx);

  unit >> unit_names;
  unit_names >> ns;
  unit_names >> dc;
  unit_names >> cl;

  traversal::names ns_names;

  ns >> ns_names;
  ns_names >> ns;
  ns_names >> dc;
  ns_names >> cl;

  if (ctx.options.class_ ().empty ())
    unit.dispatch (ctx.unit);
  else
  {
    for (vector<string>::const_iterator i (ctx.options.class_ ().begin ());
         i != ctx.options.class_ ().end (); ++i)
    {
      string n (*i);

      // Strip leading :: if present.
      //
      if (n.size () > 2 && n[0] == ':' && n[1] == ':')
        n = string (n, 2, string::npos);

      if (semantics::class_* c = ctx.unit.lookup<semantics::class_> ("", n))
        cl.traverse (*c);
      else
      {
        cerr << "error: class '" << *i << "' not found" << endl;
        throw generation_failed ();
      }
    }
  }
}