Version 1.8.0.1
[socat.git] / xiohelp.c
blob1ba92a870a0f8c2ed97dc3a421c1f21a6b379e1d
1 /* source: xiohelp.c */
2 /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 /* this file contains the source for the help function */
7 #include "xiosysincludes.h"
8 #include "xioopen.h"
10 #include "xiohelp.h"
12 #if WITH_HELP
14 /* keep consistent with xioopts.h:enum e_types ! */
15 static const char *optiontypenames[] = {
16 "CONST", "BIN", "BOOL", "BYTE",
17 "INT", "INT/NULL", "LONG", "STRING", "PTRDIFF",
18 "SHORT", "SIZE_T", "SOCKADDR", "UNSIGNED-INT",
19 "UNSIGNED-LONG","UNSIGNED-SHORT","MODE_T", "GID_T",
20 "UID_T", "INT[3]", "STRUCT-TIMEVAL", "STRUCT-TIMESPEC",
21 "DOUBLE", "STRING-NULL", "LONG-LONG", "OFF_T",
22 "OFF64_T", "INT:INT", "INT:INTP", "INT:BIN",
23 "INT:STRING", "INT:INT:INT", "INT:INT:BIN", "INT:INT:STRING",
24 "INT:INT:GENERIC",
25 "IP4NAME", "IP4SOCK",
26 #if HAVE_STRUCT_LINGER
27 "STRUCT-LINGER",
28 #endif
29 #if HAVE_STRUCT_IP_MREQN
30 "STRUCT-IP_MREQN",
31 #elif HAVE_STRUCT_IP_MREQ
32 "STRUCT-IP_MREQ",
33 #endif
34 #if HAVE_STRUCT_IP_MREQ_SOURCE
35 "IP-MREQ-SOURCE",
36 #endif
37 "GENERIC",
38 } ;
41 /* keep consistent with xioopts.h:#define GROUP_* ! */
42 static const char *addressgroupnames[] = {
43 "FD", "FIFO", "CHR", "BLK",
44 "REG", "SOCKET", "READLINE", "undef",
45 "NAMED", "OPEN", "EXEC", "FORK",
46 "LISTEN", "SHELL", "CHILD", "RETRY",
47 "TERMIOS", "RANGE", "PTY", "PARENT",
48 "UNIX", "IP4", "IP6", "INTERFACE",
49 "UDP", "TCP", "SOCKS", "OPENSSL",
50 "PROCESS", "APPL", "HTTP", "undef",
51 "POSIXMQ", "SCTP", "DCCP", "UDPLITE"
52 } ;
54 /* keep consistent with xioopts.h:enum ephase ! */
55 static char *optionphasenames[] = {
56 "ALL", "INIT", "EARLY",
57 "PREOPEN", "OPEN", "PASTOPEN",
58 "PRESOCKET", "SOCKET", "PASTSOCKET",
59 "PREBIGEN", "BIGEN", "PASTBIGEN",
60 "FD",
61 "PREBIND", "BIND", "PASTBIND",
62 "PRELISTEN", "LISTEN", "PASTLISTEN",
63 "PRECONNECT", "CONNECT", "PASTCONNECT",
64 "PREACCEPT", "ACCEPT", "PASTACCEPT",
65 "CONNECTED",
66 "PREFORK", "FORK", "PASTFORK",
67 "LATE", "LATE2",
68 "PREEXEC", "EXEC", "PASTEXEC",
69 "SPECIFIC",
70 NULL
71 } ;
74 /* print a line about a single option */
75 static int xiohelp_option(FILE *of, const struct optname *on, const char *name) {
76 int chars;
77 int i, j;
78 groups_t groups;
79 bool occurred;
81 chars = fprintf(of, " %s", name);
82 i = (16 - chars + 7) / 8;
83 for (; i > 0; --i) { fputc('\t', of); }
84 fputc('\t', of);
86 fputs("groups=", of);
87 groups = on->desc->group;
88 occurred = false;
89 chars = 7;
90 for (j = 0; j < 8*sizeof(groups_t); ++j) {
91 if (groups & 1) {
92 if (occurred) {
93 fputc(',', of);
94 ++chars;
96 fputs(addressgroupnames[j], of);
97 chars += strlen(addressgroupnames[j]);
98 occurred = true;
100 groups >>= 1;
102 i = (24 - chars + 7) / 8;
103 for (; i > 0; --i) { fputc('\t', of); }
105 chars = fprintf(of, "phase=%s", optionphasenames[on->desc->phase]);
106 i = (24 - chars + 7) / 8;
107 for (; i > 0; --i) { fputc('\t', of); }
109 fprintf(of, "type=%s", optiontypenames[on->desc->type]);
110 fputc('\n', of);
111 return 0;
114 const char *xiohelp_opttypename(enum e_types typnum)
116 if (typnum < 0 || typnum >= TYPE_OVERFLOW) {
117 Warn2("%s(): invalid type number %d", __func__, typnum);
118 return "<invalid>";
120 return optiontypenames[typnum];
123 int xioopenhelp(FILE *of,
124 int level /* 0..only addresses, 1..and options */
126 const struct addrname *an;
127 const struct optname *on;
128 int i, j;
129 groups_t groups;
130 bool occurred;
132 fputs(" bi-address: /* is an address that may act both as data sync and source */\n", of);
133 fputs(" <single-address>\n", of);
134 fputs(" <single-address>!!<single-address>\n", of);
135 fputs(" single-address:\n", of);
136 fputs(" <address-head>[,<opts>]\n", of);
137 fputs(" address-head:\n", of);
138 an = &addressnames[0];
139 i = 0;
140 while (addressnames[i].name) {
141 if (!strcmp(an->name, an->desc->defname)) {
142 int chars, i;
144 /* it is a canonical address name */
145 chars = fprintf(of, " %s", an->name);
146 if (an->desc->syntax) {
147 fputs(an->desc->syntax, of);
148 chars += strlen(an->desc->syntax);
150 i = (40 - chars + 7) / 8;
151 for (; i > 0; --i) { fputc('\t', of); }
152 fputs("\tgroups=", of);
153 groups = an->desc->groups; occurred = false;
154 for (j = 0; j < 32; ++j) {
155 if (groups & 1) {
156 if (occurred) { fputc(',', of); }
157 fprintf(of, "%s", addressgroupnames[j]);
158 occurred = true;
160 groups >>= 1;
162 fputc('\n', of);
163 } else if (level == 2) {
164 int chars, i;
166 chars = fprintf(of, " %s", an->name);
167 i = (40 - chars + 7) / 8;
168 for (; i > 0; --i) { fputc('\t', of); }
170 fprintf(of, "\tis an alias name for %s\n", an->desc->defname);
172 ++an; ++i;
174 if (level == 2) {
175 fputs(" <num> is a short form for fd:<num>\n", of);
176 fputs(" <filename> is a short form for gopen:<filename>\n", of);
179 if (level <= 0) return 0;
181 fputs(" opts:\n", of);
182 fputs(" <opt>{,<opts>}:\n", of);
183 fputs(" opt:\n", of);
184 on = optionnames;
185 while (on->name != NULL) {
186 if (on->desc->nickname != NULL
187 && !strcmp(on->name, on->desc->nickname)) {
188 if (level == 2) {
189 int chars, i;
191 chars = fprintf(of, " %s", on->name);
192 i = (16 - chars + 7) / 8;
193 for (; i > 0; --i) { fputc('\t', of); }
195 fprintf(of, "\tis an alias for %s\n", on->desc->defname);
196 } else {
197 xiohelp_option(of, on, on->name);
199 } else if (on->desc->nickname == NULL &&
200 !strcmp(on->name, on->desc->defname)) {
201 xiohelp_option(of, on, on->name);
202 } else if (level == 2) {
203 if (!strcmp(on->name, on->desc->defname)) {
204 xiohelp_option(of, on, on->name);
205 } else {
206 int chars, i;
208 chars = fprintf(of, " %s", on->name);
209 i = (16 - chars + 7) / 8;
210 for (; i > 0; --i) { fputc('\t', of); }
212 fprintf(of, "\tis an alias for %s\n", on->desc->defname);
215 ++on;
217 fflush(of);
218 return 0;
221 /* This function may be used by address handling code to log syntax error */
222 int xiohelp_syntax(
223 const char *addr,
224 int expectnum,
225 int isnum,
226 const char *syntax)
228 Error5("%s: wrong number of parameters (%d instead of %d): usage: %s%s",
229 addr, isnum, expectnum, addr, syntax);
230 return -1;
233 #endif /* WITH_HELP */