File: doc.cpp

package info (click to toggle)
libwibble 1.1-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,088 kB
  • ctags: 2,743
  • sloc: cpp: 14,551; makefile: 191; perl: 87; sh: 26
file content (462 lines) | stat: -rw-r--r-- 11,498 bytes parent folder | download
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
#include <wibble/commandline/doc.h>
#include <wibble/text/wordwrap.h>
#include <locale.h>
#include <errno.h>
#include <cstdlib>
#include <set>
#include <cstdio>
#ifdef _WIN32
#include <ctime>
#endif

using namespace std;

namespace wibble {
namespace commandline {


class HelpWriter
{
	// Width of the console
	std::ostream& out;
	int m_width;

public:
	HelpWriter(std::ostream& out);

	// Write 'size' spaces to out
	void pad(size_t size);

	// Output an item from a list.  The first bulletsize columns will be used to
	// output bullet, the rest will have text, wordwrapped and properly aligned
	void outlist(const std::string& bullet, size_t bulletsize, const std::string& text);

	void outstring(const std::string& str);
};

HelpWriter::HelpWriter(std::ostream& out) : out(out)
{
	char* columns = getenv("COLUMNS");
	m_width = columns ? atoi(columns) : 80;
}

void HelpWriter::pad(size_t size)
{
	for (size_t i = 0; i < size; i++) out << " ";
}

void HelpWriter::outlist(const std::string& bullet, size_t bulletsize, const std::string& text)
{
	text::WordWrap wrapper(text);
	size_t rightcol = m_width - bulletsize;

	out << bullet;
	pad(bulletsize - bullet.size());
	out << wrapper.get(rightcol);
	out << endl;

	while (wrapper.hasData())
	{
		pad(bulletsize);
		out << wrapper.get(rightcol);
		out << endl;
	}
}

void HelpWriter::outstring(const std::string& str)
{
	text::WordWrap wrapper(str);

	while (wrapper.hasData())
	{
		out << wrapper.get(m_width);
		out << endl;
	}
}

void Help::outputOptions(ostream& out, HelpWriter& writer, const Engine& p)
{
	// Compute size of option display
	size_t maxLeftCol = 0;
	for (vector<OptionGroup*>::const_iterator i = p.groups().begin();
			i != p.groups().end(); i++)
	{
		if ((*i)->hidden) continue;
		for (vector<Option*>::const_iterator j = (*i)->options.begin();
				j != (*i)->options.end(); j++)
		{
			if ((*j)->hidden) continue;
			size_t w = (*j)->fullUsage().size();
			if (w > maxLeftCol)
				maxLeftCol = w;
		}
	}
	for (vector<Option*>::const_iterator j = p.options().begin();
			j != p.options().end(); j++)
	{
		if ((*j)->hidden) continue;
		size_t w = (*j)->fullUsage().size();
		if (w > maxLeftCol)
			maxLeftCol = w;
	}

	if (maxLeftCol)
	{
		// Output the options
		out << endl;
		out << "Options are:" << endl;
		for (vector<OptionGroup*>::const_iterator i = p.groups().begin();
				i != p.groups().end(); i++)
		{
			if ((*i)->hidden) continue;
			if (!(*i)->description.empty())
			{
				out << endl;
				writer.outstring((*i)->description + ":");
				out << endl;
			}
			for (vector<Option*>::const_iterator j = (*i)->options.begin();
					j != (*i)->options.end(); j++)
			{
				if ((*j)->hidden) continue;
				writer.outlist(" " + (*j)->fullUsage(), maxLeftCol + 3, (*j)->description);
			}
		}
		if (!p.options().empty())
		{
			out << endl;
			writer.outstring("Other options:");
			out << endl;
			for (vector<Option*>::const_iterator j = p.options().begin();
					j != p.options().end(); j++)
			{
				if ((*j)->hidden) continue;
				writer.outlist(" " + (*j)->fullUsage(), maxLeftCol + 3, (*j)->description);
			}
		}
	}
}

void Help::outputVersion(std::ostream& out)
{
	out << m_app << " version " << m_ver << endl;
}

void Help::outputHelp(std::ostream& out, const Engine& p)
{
	HelpWriter writer(out);

	if (!p.commands().empty())
	{
		// Dig informations from p
		const std::vector<Engine*>& commands = p.commands();

		// Compute the maximum length of alias names
		size_t maxAliasSize = 0;
		for (vector<Engine*>::const_iterator i = commands.begin();
				i != commands.end(); i++)
		{
			if ((*i)->hidden) continue;
			const string& str = (*i)->primaryAlias;
			if (maxAliasSize < str.size())
				maxAliasSize = str.size();
		}

		out << "Usage: " << m_app << " [options] " << p.usage << endl;
		out << endl;
		writer.outstring("Description: " + p.description);
		out << endl;
		out << "Commands are:" << endl;
		out << endl;

		// Print the commands
		for (vector<Engine*>::const_iterator i = commands.begin();
				i != commands.end(); i++)
		{
			if ((*i)->hidden) continue;
			string aliases;
			const vector<string>& v = (*i)->aliases;
			if (!v.empty())
			{
				aliases += "  May also be invoked as ";
				for (vector<string>::const_iterator j = v.begin();
						j != v.end(); j++)
					if (j == v.begin())
						aliases += *j;
					else
						aliases += " or " + *j;
				aliases += ".";
			}

			writer.outlist(" " + (*i)->primaryAlias, maxAliasSize + 3, (*i)->description + "." + aliases);
		}
	} else {
 		// FIXME the || m_app == thing is a workaround...
		if (p.primaryAlias.empty() || m_app == p.primaryAlias)
			out << "Usage: " << m_app << " [options] " << p.usage << endl;
		else
			out << "Usage: " << m_app << " [options] " << p.primaryAlias << " [options] " << p.usage << endl;
		out << endl;

		if (!p.aliases.empty())
		{
			out << "Command aliases: ";
			for (vector<string>::const_iterator i = p.aliases.begin();
					i != p.aliases.end(); i++)
				if (i == p.aliases.begin())
					out << *i;
				else
					out << ", " << *i;
			out << "." << endl;
			out << endl;
		}
		writer.outstring("Description: " + p.description);
	}

	if (p.hasOptions())
		outputOptions(out, writer, p);

	out << endl;
}

static string toupper(const std::string& str)
{
	string res;
	for (size_t i = 0; i < str.size(); i++)
		res += ::toupper(str[i]);
	return res;
}

static string man_date()
{
	time_t tnow = time(0);
	char* source_date_epoch = std::getenv("SOURCE_DATE_EPOCH");
	if (source_date_epoch) {
		std::istringstream iss(source_date_epoch);
		iss >> tnow;
		if (iss.fail() || !iss.eof()) {
			std::cerr << "Error: Cannot parse SOURCE_DATE_EPOCH as integer\n";
			exit(27);
		}
	}
	struct tm* now = gmtime(&tnow);
	char buf[20];
	const char* oldlocale = setlocale(LC_TIME, "C");
	string res(buf, strftime(buf, 20, "%B %d, %Y", now));
	setlocale(LC_TIME, oldlocale);
	return res;
}

void Manpage::outputParagraph(std::ostream& out, const std::string& str)
{
	for (size_t i = 0; i < str.size(); i++)
		switch (str[i])
		{
			case '-':
				out << "\\-";
				break;
			case '\n':
				out << "\n.br\n";
				break;
			default:
				out << str[i];
		}
	out << '\n';
}

void Manpage::outputOption(std::ostream& out, const Option* o)
{
	out << ".TP" << endl;
	out << ".B " << o->fullUsageForMan() << endl;
	out << o->description << "." << endl;
}

void Manpage::runHooks(std::ostream& out, const std::string& section, where where)
{
	for (std::vector<Hook>::const_iterator i = hooks.begin();
			i != hooks.end(); i++)
		if (i->section == section && i->placement == where)
			out << i->text;
}

void Manpage::startSection(std::ostream& out, const std::string& name)
{
	runHooks(out, name, BEFORE);
	out << ".SH " << name << endl;
	runHooks(out, name, BEGINNING);
	lastSection = name;
}

void Manpage::endSection(std::ostream& out)
{
	runHooks(out, lastSection, END);
	lastSection.clear();
}

void Manpage::outputOptions(std::ostream& out, const Engine& p)
{
	for (vector<OptionGroup*>::const_iterator i = p.groups().begin();
			i != p.groups().end(); i++)
	{
		if ((*i)->hidden) continue;
		if (!(*i)->description.empty())
			out << endl << (*i)->description << ":" << endl;
		for (vector<Option*>::const_iterator j = (*i)->options.begin();
				j != (*i)->options.end(); ++j)
		{
			if ((*j)->hidden) continue;
			outputOption(out, *j);
		}
		out << ".PP" << endl;
	}

	if (!p.options().empty())
	{
		out << endl;
		out << "Other options:" << endl;
		for (vector<Option*>::const_iterator j = p.options().begin();
				j != p.options().end(); ++j)
		{
			if ((*j)->hidden) continue;
			outputOption(out, *j);
		}
	}
}

void Manpage::output(std::ostream& out, const Engine& p)
{
	// Manpage header
	out << ".TH " << toupper(m_app) << " " << m_section << " \"" << man_date() << "\" \"" << m_ver << "\"" << endl;

	startSection(out, "NAME");
	out << p.name() << " \\- " << p.description << endl;
	endSection(out);

	startSection(out, "SYNOPSIS");
	out << "\\fB" << p.name() << "\\fP [options] " << p.usage << endl;
	endSection(out);

	startSection(out, "DESCRIPTION");
	if (!p.longDescription.empty())
		outputParagraph(out, p.longDescription);
	endSection(out);

	if (!p.commands().empty())
	{
		const vector<Engine*>& commands = p.commands();

		startSection(out, "COMMANDS");
		out << "\\fB" << p.name() << "\\fP accepts a non-switch argument, that indicates what is the operation that should be performed:" << endl;
		for (vector<Engine*>::const_iterator i = commands.begin();
				i != commands.end(); i++)
		{
			if ((*i)->hidden) continue;
			out << ".TP" << endl;
			out << "\\fB" << (*i)->primaryAlias << "\\fP";

			const vector<string>& v = (*i)->aliases;
			for (vector<string>::const_iterator j = v.begin(); j != v.end(); j++)
				out << " or \\fB" << *j << "\\fP";

			out << " " << (*i)->usage << endl;
			out << ".br" << endl;
			if ((*i)->longDescription.empty())
				outputParagraph(out, (*i)->description);
			else
				outputParagraph(out, (*i)->longDescription);
		}
		endSection(out);
	}

	startSection(out, "OPTIONS");
	out << "This program follows the usual GNU command line syntax, with long options starting with two dashes (`\\-')." << endl << endl;
	if (!p.commands().empty())
		out << "Every one of the commands listed above has its own set of options.  To keep this manpage readable, all the options are presented together.  Please refer to \"\\fB" << p.name() << "\\fP help \\fIcommand\\fP\" to see which options are accepted by a given command." << endl;

	// Output the general options
	outputOptions(out, p);

	// Output group-specific options
	if (!p.commands().empty())
	{
		const vector<Engine*>& commands = p.commands();
		for (vector<Engine*>::const_iterator i = commands.begin();
				i != commands.end(); i++)
		{
			if ((*i)->hidden) continue;
			out << "\\fBOptions for command " << (*i)->primaryAlias << "\\fP" << endl;
			out << ".br" << endl;
			outputOptions(out, **i);
		}
	}
	endSection(out);

	startSection(out, "AUTHOR");
	out << "\\fB" << p.name() << "\\fP is maintained by " << m_author << "." << endl << endl;
	out << "This manpage has been automatically generated by the " << m_app << " program." << endl;
	endSection(out);
}

static string readline(FILE* in)
{
	string res;
	int c;
	while ((c = getc(in)) != EOF && c != '\n')
		res += c;
	return res;
}

void Manpage::readHooks(const std::string& file)
{
	FILE* in = fopen(file.c_str(), "r");
	if (!in) throw exception::File(file, "opening for reading");
	string section;
	commandline::Manpage::where placement = commandline::Manpage::BEFORE;
	string text;
	while (!feof(in))
	{
		string line(readline(in));
		if (line.empty())
			continue;
		if (line[0] == '|')
		{
			text += line.substr(1) + "\n";
		}
		else if (isalpha(line[0]))
		{
			if (!section.empty())
			{
				addHook(section, placement, text);
				text.clear();
			}
			size_t sep = line.find(' ');
			if (sep == string::npos)
			{
				fclose(in);
				throw exception::Consistency("expected two words in line: " + line);
			}
			section = line.substr(0, sep);
			string w(line, sep+1);
			if (w == "before")
			{
				placement = commandline::Manpage::BEFORE;
			} else if (w == "beginning") {
				placement = commandline::Manpage::BEGINNING;
			} else if (w == "end") {
				placement = commandline::Manpage::END;
			} else {
				fclose(in);
				throw exception::Consistency("expected 'before', 'beginning' or 'end' in line: " + line);
			}
		}
	}
	if (!section.empty())
		addHook(section, placement, text);
	fclose(in);
}

}
}


// vim:set ts=4 sw=4: