summaryrefslogtreecommitdiff
path: root/src/main/help.c
blob: 2d1b927ce626f032ec4ffac53f53612a263ec243 (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
/*
 * Output command-line help to stdout.
 *
 * Copyright 2012 Andrew Wood, distributed under the Artistic License 2.0.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define N_(String) (String)

struct optdesc_s {
	char *optshort;
	char *optlong;
	char *param;
	char *description;
};


/*
 * Display command-line help.
 */
void display_help(void)
{
	struct optdesc_s optlist[] = {
		{"-p", "--progress", 0,
		 N_("show progress bar")},
		{"-t", "--timer", 0,
		 N_("show elapsed time")},
		{"-e", "--eta", 0,
		 N_("show estimated time of arrival (completion)")},
		{"-r", "--rate", 0,
		 N_("show data transfer rate counter")},
		{"-a", "--average-rate", 0,
		 N_("show data transfer average rate counter")},
		{"-b", "--bytes", 0,
		 N_("show number of bytes transferred")},
		{"-f", "--force", 0,
		 N_("output even if standard error is not a terminal")},
		{"-n", "--numeric", 0,
		 N_("output percentages, not visual information")},
		{"-q", "--quiet", 0,
		 N_("do not output any transfer information at all")},
		{"-c", "--cursor", 0,
		 N_("use cursor positioning escape sequences")},
		{"-W", "--wait", 0,
		 N_("display nothing until first byte transferred")},
		{"-s", "--size", N_("SIZE"),
		 N_("set estimated data size to SIZE bytes")},
		{"-l", "--line-mode", 0,
		 N_("count lines instead of bytes")},
		{"-i", "--interval", N_("SEC"),
		 N_("update every SEC seconds")},
		{"-w", "--width", N_("WIDTH"),
		 N_("assume terminal is WIDTH characters wide")},
		{"-H", "--height", N_("HEIGHT"),
		 N_("assume terminal is HEIGHT rows high")},
		{"-N", "--name", N_("NAME"),
		 N_("prefix visual information with NAME")},
		{"", 0, 0, 0},
		{"-L", "--rate-limit", N_("RATE"),
		 N_("limit transfer to RATE bytes per second")},
		{"-B", "--buffer-size", N_("BYTES"),
		 N_("use a buffer size of BYTES")},
		{"-R", "--remote", N_("PID"),
		 N_("update settings of process PID")},
		{"", 0, 0, 0},
		{"-h", "--help", 0,
		 N_("show this help and exit")},
		{"-V", "--version", 0,
		 N_("show version information and exit")},
		{0, 0, 0, 0}
	};
	int i, col1max = 0, tw = 77;
	char *optbuf;

	printf(_("Usage: %s [OPTION] [FILE]..."),	/* RATS: ignore */
	       PROGRAM_NAME);
	printf("\n%s\n\n",
	       _
	       ("Concatenate FILE(s), or standard input, to standard output,\n"
		"with monitoring."));

	for (i = 0; optlist[i].optshort; i++) {
		int width = 0;
		char *param;

		width = 2 + strlen(optlist[i].optshort);	/* RATS: ignore */
#ifdef HAVE_GETOPT_LONG
		if (optlist[i].optlong)
			width += 2 + strlen(optlist[i].optlong);	/* RATS: ignore */
#endif
		param = optlist[i].param;
		if (param)
			param = _(param);
		if (param)
			width += 1 + strlen(param);	/* RATS: ignore */

		if (width > col1max)
			col1max = width;
	}

	col1max++;

	optbuf = malloc(col1max + 16);
	if (optbuf == NULL) {
		fprintf(stderr, "%s: %s\n", PROGRAM_NAME, strerror(errno));
		exit(1);
	}

	for (i = 0; optlist[i].optshort; i++) {
		char *param;
		char *description;
		char *start;
		char *end;

		if (optlist[i].optshort[0] == 0) {
			printf("\n");
			continue;
		}

		param = optlist[i].param;
		if (param)
			param = _(param);
		description = optlist[i].description;
		if (description)
			description = _(description);

		sprintf(optbuf, "%s%s%s%s%s",	/* RATS: ignore (checked) */
			optlist[i].optshort,
#ifdef HAVE_GETOPT_LONG
			optlist[i].optlong ? ", " : "",
			optlist[i].optlong ? optlist[i].optlong : "",
#else
			"", "",
#endif
			param ? " " : "", param ? param : "");

		printf("  %-*s ", col1max - 2, optbuf);

		if (description == NULL) {
			printf("\n");
			continue;
		}

		start = description;

		while (strlen(start) /* RATS: ignore */ >tw - col1max) {
			end = start + tw - col1max;
			while ((end > start) && (end[0] != ' '))
				end--;
			if (end == start) {
				end = start + tw - col1max;
			} else {
				end++;
			}
			printf("%.*s\n%*s ", (int) (end - start), start,
			       col1max, "");
			if (end == start)
				end++;
			start = end;
		}

		printf("%s\n", start);
	}

	printf("\n");
	printf(_("Please report any bugs to %s."),	/* RATS: ignore */
	       BUG_REPORTS_TO);
	printf("\n");
}

/* EOF */