cmDocumentation.cxx 17.2 KB
Newer Older
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
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "cmDocumentation.h"

#include "cmSystemTools.h"

//----------------------------------------------------------------------------
static const cmDocumentationEntry cmDocumentationStandardOptions[] =
{
  {"--copyright", "Print the CMake copyright and exit.", 0},
  {"--usage", "Print usage information and exit.",
   "Usage describes the basic command line interface and its options."},
  {"--help", "Print full help and exit.",
   "Full help displays most of the documentation provided by the UNIX "
   "man page.  It is provided for use on non-UNIX platforms, but is "
   "also convenient if the man page is not installed."},
  {"--help-html", "Print full help in HTML format.",
   "This option is used by CMake authors to help produce web pages."},
  {"--man", "Print a UNIX man page and exit.",
   "This option is used by CMake authors to generate the UNIX man page."},
  {"--version", "Show program name/version banner and exit.", 0},
  {0,0,0}
};

//----------------------------------------------------------------------------
const cmDocumentationEntry cmDocumentationCopyright[] =
{
  {0,
   "Copyright (c) 2002 Kitware, Inc., Insight Consortium.\n"
   "All rights reserved.\n", 0},
  {0,
   "Redistribution and use in source and binary forms, with or without "
   "modification, are permitted provided that the following conditions are "
   "met:\n", 0},
  {" * ",
   "Redistributions of source code must retain the above copyright notice, "
   "this list of conditions and the following disclaimer.\n", 0},
  {" * ",
   "Redistributions in binary form must reproduce the above copyright "
   "notice, this list of conditions and the following disclaimer in the "
   "documentation and/or other materials provided with the distribution.\n",
   0},
  {" * ",
   "The names of Kitware, Inc., the Insight Consortium, or the names of "
   "any consortium members, or of any contributors, may not be used to "
   "endorse or promote products derived from this software without "
   "specific prior written permission.\n", 0},
  {" * ",
   "Modified source versions must be plainly marked as such, and must "
   "not be misrepresented as being the original software.\n", 0},
  {0,
   "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "
   "``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT "
   "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR "
   "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR "
   "CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, "
   "EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, "
   "PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR "
   "PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF "
   "LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING "
   "NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS "
   "SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n", 0},
  {0, 0, 0}
};

//----------------------------------------------------------------------------
cmDocumentation::cmDocumentation()
{
  this->Commands = 0;
  this->Description = 0;
  this->Name = 0;
  this->UsageHelp = 0;  
  this->SetOptions(0);
}

//----------------------------------------------------------------------------
void cmDocumentation::PrintManSection(std::ostream& os,
                                      const cmDocumentationEntry* section,
                                      const char* name)
{
  if(!section) { return; }
  os << ".SH " << name << "\n";
  for(const cmDocumentationEntry* op = section; op->brief; ++op)
    {
    if(op->name)
      {
      os << ".TP\n"
         << ".B " << op->name << "\n"
103
         << op->brief << "\n\n";
104
105
106
107
      if(op->full)
        {
        this->PrintFull(os, op->full, 0, 0);
        }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
      }
    else
      {
      os << ".PP\n"
         << op->brief << "\n";
      }
    }  
}

//----------------------------------------------------------------------------
void cmDocumentation::PrintHelpSection(std::ostream& os,
                                       const cmDocumentationEntry* section)
{
  if(!section) { return; }
  for(const cmDocumentationEntry* op = section; op->brief; ++op)
    {
    if(op->name)
      {
      os << "  " << op->name << "\n"
         << "       ";
      this->PrintColumn(os, 70, "       ", op->brief);
      if(op->full)
        {
        os << "\n"
132
           << "\n"
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
           << "       ";
        this->PrintColumn(os, 70, "       ", op->full);
        }
      os << "\n";
      }
    else
      {
      this->PrintColumn(os, 77, "", op->brief);
      os << "\n";
      }
    os << "\n";
    }  
}

//----------------------------------------------------------------------------
void cmDocumentation::PrintHTMLEscapes(std::ostream& os, const char* text)
{
  static cmDocumentationEntry escapes[] =
  {
    {"<", "&lt;", 0},
    {">", "&gt;", 0},
    {"&", "&amp;", 0},
155
    {"\n", "<br>", 0},
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    {0,0,0}
  };
  for(const char* p = text; *p; ++p)
    {
    bool found = false;
    for(const cmDocumentationEntry* op = escapes; !found && op->name; ++op)
      {
      if(op->name[0] == *p)
        {
        os << op->brief;
        found = true;
        }
      }
    if(!found)
      {
      os << *p;
      }
    }
}

176
//----------------------------------------------------------------------------
177
void cmDocumentation::PrintHTMLPreformatted(std::ostream& os, const char* text)
178
{
179
180
181
  os << "<pre>";
  cmDocumentation::PrintHTMLEscapes(os, text);
  os << "</pre>";
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
//----------------------------------------------------------------------------
void cmDocumentation::PrintHelpHTMLSection(std::ostream& os,
                                           const cmDocumentationEntry* section,
                                           const char* header)
{
  if(!section) { return; }
  if(header)
    {
    os << "<h2>" << header << "</h2>\n";
    }
  for(const cmDocumentationEntry* op = section; op->brief;)
    {
    if(op->name)
      {
      os << "<ul>\n";
      for(;op->name;++op)
        {
        os << "  <li>\n";
        os << "    <b><code>";
        this->PrintHTMLEscapes(os, op->name);
        os << "</code></b>: ";
        this->PrintHTMLEscapes(os, op->brief);
        if(op->full)
          {
208
          os << "<br>";
209
210
211
          this->PrintFull(os, op->full,
                          &cmDocumentation::PrintHTMLPreformatted,
                          &cmDocumentation::PrintHTMLEscapes);
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
          }
        os << "\n";
        os << "  </li>\n";
        }
      os << "</ul>\n";
      }
    else
      {
      this->PrintHTMLEscapes(os, op->brief);
      os << "\n";
      ++op;
      }
    }
}

//----------------------------------------------------------------------------
void cmDocumentation::PrintUsageSection(std::ostream& os,
                                        const cmDocumentationEntry* section)
{
  if(!section) { return; }
  std::ios::fmtflags flags = os.flags();
  os.setf(flags | std::ios::left);
  for(const cmDocumentationEntry* op = section; op->brief; ++op)
    {
    if(op->name)
      {
      os << "  ";
      os.width(25);
      os << op->name << "= " << op->brief << "\n";
      }
    else
      {
      os << "\n";
      this->PrintColumn(os, 74, "", op->brief);
      os << "\n";
      }
    }  
  os.setf(flags);
}

//----------------------------------------------------------------------------
void cmDocumentation::PrintUsage(std::ostream& os)
{
  os << "Usage:\n";
  this->PrintUsageSection(os, this->UsageHelp);
  this->PrintUsageSection(os, &this->Options[0]);
}

//----------------------------------------------------------------------------
void cmDocumentation::PrintHelp(std::ostream& os)
{
  os << "Usage:\n";
  os << "\n";
  this->PrintHelpSection(os, this->UsageHelp);
  this->PrintHelpSection(os, this->Description);
  os << "--------------------------------------------------------------------------\n";
  this->PrintHelpSection(os, &this->Options[0]);
  os << "--------------------------------------------------------------------------\n";
  this->PrintHelpSection(os, this->Commands);
}

//----------------------------------------------------------------------------
void cmDocumentation::PrintHelpHTML(std::ostream& os)
{
  os << "<html>\n"
     << "<body>\n";
  os << "<h2>Using CMake</h2>\n";
  if(this->UsageHelp)
    {
    os << "<blockquote><code>\n";
    this->PrintHelpHTMLSection(os, this->UsageHelp, 0);
    os << "</code></blockquote>\n";
    }
  this->PrintHelpHTMLSection(os, this->Description, 0);
  this->PrintHelpHTMLSection(os, &this->Options[0], "Command-line Options");
  this->PrintHelpHTMLSection(os, this->Commands, "CMakeLists.txt Commands");
  os << "</body>\n"
     << "</html>\n";
}

//----------------------------------------------------------------------------
void cmDocumentation::PrintManPage(std::ostream& os)
{
  os << ".TH CMake 1 \""
     << cmSystemTools::GetCurrentDateTime("%B %d, %Y").c_str()
     << "\" \"CMake " CMake_VERSION_STRING "\"\n";
  this->PrintManSection(os, this->Name, "NAME");
  this->PrintManSection(os, this->UsageHelp, "SYNOPSIS");
  this->PrintManSection(os, this->Description, "DESCRIPTION");
  this->PrintManSection(os, &this->Options[0], "OPTIONS");
  this->PrintManSection(os, this->Commands, "COMMANDS");
  this->PrintManSection(os, cmDocumentationCopyright, "COPYRIGHT");
  os << ".SH MAILING LIST\n";
  os << "For help and discussion about using cmake, a mailing list is\n"
     << "provided at\n"
     << ".B cmake@www.cmake.org.\n"
     << "Please first read the full documentation at\n"
     << ".B http://www.cmake.org\n"
     << "before posting questions to the list.\n";
  os << ".SH AUTHOR\n"
     << "This manual page was generated by \"cmake --man\".\n";
}

//----------------------------------------------------------------------------
void cmDocumentation::PrintCopyright(std::ostream& os)
{
  os << "CMake version " CMake_VERSION_STRING "\n";
  for(const cmDocumentationEntry* op = cmDocumentationCopyright;
      op->brief; ++op)
    {
    if(op->name)
      {
      os << " * ";
      this->PrintColumn(os, 74, "   ", op->brief);
      }
    else
      {
      this->PrintColumn(os, 77, "", op->brief);
      }
    os << "\n";
    }
}

//----------------------------------------------------------------------------
void cmDocumentation::PrintVersion(std::ostream& os)
{
  os << "CMake version " CMake_VERSION_STRING "\n";
}

//----------------------------------------------------------------------------
void cmDocumentation::PrintColumn(std::ostream& os, int width,
                                  const char* indent, const char* text)
{
  // Print text arranged in a column of fixed witdh indented by the
  // "indent" text.
  const char* l = text;
  int column = 0;
  bool newSentence = false;
350
  bool firstLine = true;
351
352
  bool lastHadBlanks = false;
  
353
354
355
356
357
  // Count leading blanks in the text.
  int blanks = 0;
  for(const char* b = l; *b == ' '; ++b) { ++blanks; }
  
  // Loop until the end of the text.
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
  while(*l)
    {
    // Parse the next word.
    const char* r = l;
    while(*r && (*r != '\n') && (*r != ' ')) { ++r; }
    
    // Does it fit on this line?
    if(r-l < (width-column-(newSentence?1:0)))
      {
      // Word fits on this line.
      if(r > l)
        {
        if(column)
          {
          // Not first word on line.  Separate from the previous word
          // by a space, or two if this is a new sentence.
          if(newSentence)
            {
            os << "  ";
            column += 2;
            }
          else
            {
            os << " ";
            column += 1;
            }
          }
        else
          {
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
          // If this is the first line not beginning in a blank after
          // a sequence of lines beginning in blanks, add an extra
          // newline.
          if(blanks)
            {
            lastHadBlanks = true;
            }
          else
            {
            if(lastHadBlanks)
              {
              os << "\n";
              }
            lastHadBlanks = false;
            }
            
403
404
          // First word on line.  Print indentation unless this is the
          // first line.
405
406
407
408
409
410
411
412
413
414
          os << (firstLine?"":indent);
          
          // Further indent by leading blanks from the text on this
          // line.
          for(int i = 0; i < blanks; ++i)
            {
            os << " ";
            ++column;
            }
          blanks = 0;
415
416
417
418
419
420
421
422
423
424
425
426
427
          }
        
        // Print the word.
        os.write(l, static_cast<long>(r-l));
        newSentence = (*(r-1) == '.');
        }
      
      if(*r == '\n')
        {
        // Text provided a newline.  Start a new line.
        os << "\n";
        ++r;
        column = 0;
428
429
430
431
        firstLine = false;
        
        // Count leading blanks in the text.
        for(const char* b = r; *b == ' '; ++b) { ++blanks; }
432
433
434
435
436
437
438
439
440
441
442
        }
      else
        {
        // No provided newline.  Continue this line.
        column += static_cast<long>(r-l);
        }
      }
    else
      {
      // Word does not fit on this line.  Start a new line.
      os << "\n";
443
      firstLine = false;
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
      if(r > l)
        {
        os << indent;
        os.write(l, static_cast<long>(r-l));
        column = static_cast<long>(r-l);
        newSentence = (*(r-1) == '.');
        }
      }
    
    // Move to beginning of next word.  Skip over whitespace.
    l = r;
    while(*l && (*l == ' ')) { ++l; }    
    }
}

//----------------------------------------------------------------------------
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
void cmDocumentation::PrintFull(std::ostream& os, const char* text,
                                void (*pPreform)(std::ostream&, const char*),
                                void (*pNormal)(std::ostream&, const char*))
{
  const char* line = text;
  while(*line)
    {
    // Any lines starting in a space are treated as preformatted text.
    std::string preformatted;
    while(*line == ' ')
      {
      for(char ch = *line; ch && ch != '\n'; ++line, ch = *line)
        {
        preformatted.append(1, ch);
        }
      if(*line)
        {
        ++line;
        preformatted.append(1, '\n');
        }
      }
    if(preformatted.length())
      {
      if(pPreform)
        {
        pPreform(os, preformatted.c_str());
        }
      else
        {
        os << preformatted << "\n";
        }
      }
    
    // Other lines are treated as normal text.
    std::string normal;
    for(char ch = *line; ch && ch != '\n'; ++line, ch = *line)
      {
      normal.append(1, ch);
      }
    if(*line)
      {
      ++line;
      normal.append(1, '\n');
      }
    if(normal.length())
      {
      if(pNormal)
        {
        pNormal(os, normal.c_str());
        }
      else
        {
        os << normal << "\n";
        }
      }
    }
}

//----------------------------------------------------------------------------
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
void cmDocumentation::Print(Type ht, std::ostream& os)
{
  switch (ht)
    {
    case cmDocumentation::Usage:     this->PrintUsage(os); break;
    case cmDocumentation::Help:      this->PrintHelp(os); break;
    case cmDocumentation::HelpHTML:  this->PrintHelpHTML(os); break;
    case cmDocumentation::Man:       this->PrintManPage(os); break;
    case cmDocumentation::Copyright: this->PrintCopyright(os); break;
    case cmDocumentation::Version:   this->PrintVersion(os); break;
    default: break;
    }
}

//----------------------------------------------------------------------------
cmDocumentation::Type cmDocumentation::CheckOptions(int argc, char** argv)
{
  for(int i=1; i < argc; ++i)
    {
    if((strcmp(argv[i], "/?") == 0) ||
       (strcmp(argv[i], "-usage") == 0) ||
       (strcmp(argv[i], "--usage") == 0))
      {
      return cmDocumentation::Usage;
      }
    if((strcmp(argv[i], "-help") == 0) ||
       (strcmp(argv[i], "--help") == 0))
      {
      return cmDocumentation::Help;
      }
    if(strcmp(argv[i], "--help-html") == 0)
      {
      return cmDocumentation::HelpHTML;
      }
    if(strcmp(argv[i], "--man") == 0)
      {
      return cmDocumentation::Man;
      }
    if(strcmp(argv[i], "--copyright") == 0)
      {
      return cmDocumentation::Copyright;
      }
    if(strcmp(argv[i], "--version") == 0)
      {
      return cmDocumentation::Version;
      }
    }
  return cmDocumentation::None;
}

//----------------------------------------------------------------------------
void cmDocumentation::SetOptions(const cmDocumentationEntry* d)
{
  this->Options.erase(this->Options.begin(), this->Options.end());
  if(d)
    {
    for(const cmDocumentationEntry* op = d; op->brief; ++op)
      {
      this->Options.push_back(*op);
      }
    }
  for(const cmDocumentationEntry* op = cmDocumentationStandardOptions;
      op->brief; ++op)
    {
    this->Options.push_back(*op);
    }
  cmDocumentationEntry empty = {0,0,0};
  this->Options.push_back(empty);
}