xref: /openbmc/u-boot/scripts/dtc/util.c (revision d6fc90ced42e9fb77185cedfe45d0a2c7ac78508)
1c0e032e0STom Rini /*
2c0e032e0STom Rini  * Copyright 2011 The Chromium Authors, All Rights Reserved.
3c0e032e0STom Rini  * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
4c0e032e0STom Rini  *
5c0e032e0STom Rini  * util_is_printable_string contributed by
6c0e032e0STom Rini  *	Pantelis Antoniou <pantelis.antoniou AT gmail.com>
7c0e032e0STom Rini  *
8c0e032e0STom Rini  * This program is free software; you can redistribute it and/or
9c0e032e0STom Rini  * modify it under the terms of the GNU General Public License as
10c0e032e0STom Rini  * published by the Free Software Foundation; either version 2 of the
11c0e032e0STom Rini  * License, or (at your option) any later version.
12c0e032e0STom Rini  *
13c0e032e0STom Rini  *  This program is distributed in the hope that it will be useful,
14c0e032e0STom Rini  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15c0e032e0STom Rini  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16c0e032e0STom Rini  *  General Public License for more details.
17c0e032e0STom Rini  *
18c0e032e0STom Rini  *  You should have received a copy of the GNU General Public License
19c0e032e0STom Rini  *  along with this program; if not, write to the Free Software
20c0e032e0STom Rini  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
21c0e032e0STom Rini  *                                                                   USA
22c0e032e0STom Rini  */
23c0e032e0STom Rini 
24c0e032e0STom Rini #include <ctype.h>
25c0e032e0STom Rini #include <stdio.h>
26c0e032e0STom Rini #include <stdlib.h>
27c0e032e0STom Rini #include <stdarg.h>
28c0e032e0STom Rini #include <string.h>
29c0e032e0STom Rini #include <assert.h>
30c0e032e0STom Rini 
31c0e032e0STom Rini #include <errno.h>
32c0e032e0STom Rini #include <fcntl.h>
33c0e032e0STom Rini #include <unistd.h>
34c0e032e0STom Rini 
35c0e032e0STom Rini #include "libfdt.h"
36c0e032e0STom Rini #include "util.h"
37c0e032e0STom Rini #include "version_gen.h"
38c0e032e0STom Rini 
xstrdup(const char * s)39c0e032e0STom Rini char *xstrdup(const char *s)
40c0e032e0STom Rini {
41c0e032e0STom Rini 	int len = strlen(s) + 1;
42c0e032e0STom Rini 	char *d = xmalloc(len);
43c0e032e0STom Rini 
44c0e032e0STom Rini 	memcpy(d, s, len);
45c0e032e0STom Rini 
46c0e032e0STom Rini 	return d;
47c0e032e0STom Rini }
48c0e032e0STom Rini 
49c0e032e0STom Rini /* based in part from (3) vsnprintf */
xasprintf(char ** strp,const char * fmt,...)50c0e032e0STom Rini int xasprintf(char **strp, const char *fmt, ...)
51c0e032e0STom Rini {
52c0e032e0STom Rini 	int n, size = 128;	/* start with 128 bytes */
53c0e032e0STom Rini 	char *p;
54c0e032e0STom Rini 	va_list ap;
55c0e032e0STom Rini 
56c0e032e0STom Rini 	/* initial pointer is NULL making the fist realloc to be malloc */
57c0e032e0STom Rini 	p = NULL;
58c0e032e0STom Rini 	while (1) {
59c0e032e0STom Rini 		p = xrealloc(p, size);
60c0e032e0STom Rini 
61c0e032e0STom Rini 		/* Try to print in the allocated space. */
62c0e032e0STom Rini 		va_start(ap, fmt);
63c0e032e0STom Rini 		n = vsnprintf(p, size, fmt, ap);
64c0e032e0STom Rini 		va_end(ap);
65c0e032e0STom Rini 
66c0e032e0STom Rini 		/* If that worked, return the string. */
67c0e032e0STom Rini 		if (n > -1 && n < size)
68c0e032e0STom Rini 			break;
69c0e032e0STom Rini 		/* Else try again with more space. */
70c0e032e0STom Rini 		if (n > -1)	/* glibc 2.1 */
71c0e032e0STom Rini 			size = n + 1; /* precisely what is needed */
72c0e032e0STom Rini 		else		/* glibc 2.0 */
73c0e032e0STom Rini 			size *= 2; /* twice the old size */
74c0e032e0STom Rini 	}
75c0e032e0STom Rini 	*strp = p;
76c0e032e0STom Rini 	return strlen(p);
77c0e032e0STom Rini }
78c0e032e0STom Rini 
join_path(const char * path,const char * name)79c0e032e0STom Rini char *join_path(const char *path, const char *name)
80c0e032e0STom Rini {
81c0e032e0STom Rini 	int lenp = strlen(path);
82c0e032e0STom Rini 	int lenn = strlen(name);
83c0e032e0STom Rini 	int len;
84c0e032e0STom Rini 	int needslash = 1;
85c0e032e0STom Rini 	char *str;
86c0e032e0STom Rini 
87c0e032e0STom Rini 	len = lenp + lenn + 2;
88c0e032e0STom Rini 	if ((lenp > 0) && (path[lenp-1] == '/')) {
89c0e032e0STom Rini 		needslash = 0;
90c0e032e0STom Rini 		len--;
91c0e032e0STom Rini 	}
92c0e032e0STom Rini 
93c0e032e0STom Rini 	str = xmalloc(len);
94c0e032e0STom Rini 	memcpy(str, path, lenp);
95c0e032e0STom Rini 	if (needslash) {
96c0e032e0STom Rini 		str[lenp] = '/';
97c0e032e0STom Rini 		lenp++;
98c0e032e0STom Rini 	}
99c0e032e0STom Rini 	memcpy(str+lenp, name, lenn+1);
100c0e032e0STom Rini 	return str;
101c0e032e0STom Rini }
102c0e032e0STom Rini 
util_is_printable_string(const void * data,int len)103c0e032e0STom Rini bool util_is_printable_string(const void *data, int len)
104c0e032e0STom Rini {
105c0e032e0STom Rini 	const char *s = data;
106c0e032e0STom Rini 	const char *ss, *se;
107c0e032e0STom Rini 
108c0e032e0STom Rini 	/* zero length is not */
109c0e032e0STom Rini 	if (len == 0)
110c0e032e0STom Rini 		return 0;
111c0e032e0STom Rini 
112c0e032e0STom Rini 	/* must terminate with zero */
113c0e032e0STom Rini 	if (s[len - 1] != '\0')
114c0e032e0STom Rini 		return 0;
115c0e032e0STom Rini 
116c0e032e0STom Rini 	se = s + len;
117c0e032e0STom Rini 
118c0e032e0STom Rini 	while (s < se) {
119c0e032e0STom Rini 		ss = s;
120c0e032e0STom Rini 		while (s < se && *s && isprint((unsigned char)*s))
121c0e032e0STom Rini 			s++;
122c0e032e0STom Rini 
123c0e032e0STom Rini 		/* not zero, or not done yet */
124c0e032e0STom Rini 		if (*s != '\0' || s == ss)
125c0e032e0STom Rini 			return 0;
126c0e032e0STom Rini 
127c0e032e0STom Rini 		s++;
128c0e032e0STom Rini 	}
129c0e032e0STom Rini 
130c0e032e0STom Rini 	return 1;
131c0e032e0STom Rini }
132c0e032e0STom Rini 
133c0e032e0STom Rini /*
134c0e032e0STom Rini  * Parse a octal encoded character starting at index i in string s.  The
135c0e032e0STom Rini  * resulting character will be returned and the index i will be updated to
136c0e032e0STom Rini  * point at the character directly after the end of the encoding, this may be
137c0e032e0STom Rini  * the '\0' terminator of the string.
138c0e032e0STom Rini  */
get_oct_char(const char * s,int * i)139c0e032e0STom Rini static char get_oct_char(const char *s, int *i)
140c0e032e0STom Rini {
141c0e032e0STom Rini 	char x[4];
142c0e032e0STom Rini 	char *endx;
143c0e032e0STom Rini 	long val;
144c0e032e0STom Rini 
145c0e032e0STom Rini 	x[3] = '\0';
146c0e032e0STom Rini 	strncpy(x, s + *i, 3);
147c0e032e0STom Rini 
148c0e032e0STom Rini 	val = strtol(x, &endx, 8);
149c0e032e0STom Rini 
150c0e032e0STom Rini 	assert(endx > x);
151c0e032e0STom Rini 
152c0e032e0STom Rini 	(*i) += endx - x;
153c0e032e0STom Rini 	return val;
154c0e032e0STom Rini }
155c0e032e0STom Rini 
156c0e032e0STom Rini /*
157c0e032e0STom Rini  * Parse a hexadecimal encoded character starting at index i in string s.  The
158c0e032e0STom Rini  * resulting character will be returned and the index i will be updated to
159c0e032e0STom Rini  * point at the character directly after the end of the encoding, this may be
160c0e032e0STom Rini  * the '\0' terminator of the string.
161c0e032e0STom Rini  */
get_hex_char(const char * s,int * i)162c0e032e0STom Rini static char get_hex_char(const char *s, int *i)
163c0e032e0STom Rini {
164c0e032e0STom Rini 	char x[3];
165c0e032e0STom Rini 	char *endx;
166c0e032e0STom Rini 	long val;
167c0e032e0STom Rini 
168c0e032e0STom Rini 	x[2] = '\0';
169c0e032e0STom Rini 	strncpy(x, s + *i, 2);
170c0e032e0STom Rini 
171c0e032e0STom Rini 	val = strtol(x, &endx, 16);
172c0e032e0STom Rini 	if (!(endx  > x))
173c0e032e0STom Rini 		die("\\x used with no following hex digits\n");
174c0e032e0STom Rini 
175c0e032e0STom Rini 	(*i) += endx - x;
176c0e032e0STom Rini 	return val;
177c0e032e0STom Rini }
178c0e032e0STom Rini 
get_escape_char(const char * s,int * i)179c0e032e0STom Rini char get_escape_char(const char *s, int *i)
180c0e032e0STom Rini {
181c0e032e0STom Rini 	char	c = s[*i];
182c0e032e0STom Rini 	int	j = *i + 1;
183c0e032e0STom Rini 	char	val;
184c0e032e0STom Rini 
185c0e032e0STom Rini 	switch (c) {
186c0e032e0STom Rini 	case 'a':
187c0e032e0STom Rini 		val = '\a';
188c0e032e0STom Rini 		break;
189c0e032e0STom Rini 	case 'b':
190c0e032e0STom Rini 		val = '\b';
191c0e032e0STom Rini 		break;
192c0e032e0STom Rini 	case 't':
193c0e032e0STom Rini 		val = '\t';
194c0e032e0STom Rini 		break;
195c0e032e0STom Rini 	case 'n':
196c0e032e0STom Rini 		val = '\n';
197c0e032e0STom Rini 		break;
198c0e032e0STom Rini 	case 'v':
199c0e032e0STom Rini 		val = '\v';
200c0e032e0STom Rini 		break;
201c0e032e0STom Rini 	case 'f':
202c0e032e0STom Rini 		val = '\f';
203c0e032e0STom Rini 		break;
204c0e032e0STom Rini 	case 'r':
205c0e032e0STom Rini 		val = '\r';
206c0e032e0STom Rini 		break;
207c0e032e0STom Rini 	case '0':
208c0e032e0STom Rini 	case '1':
209c0e032e0STom Rini 	case '2':
210c0e032e0STom Rini 	case '3':
211c0e032e0STom Rini 	case '4':
212c0e032e0STom Rini 	case '5':
213c0e032e0STom Rini 	case '6':
214c0e032e0STom Rini 	case '7':
215c0e032e0STom Rini 		j--; /* need to re-read the first digit as
216c0e032e0STom Rini 		      * part of the octal value */
217c0e032e0STom Rini 		val = get_oct_char(s, &j);
218c0e032e0STom Rini 		break;
219c0e032e0STom Rini 	case 'x':
220c0e032e0STom Rini 		val = get_hex_char(s, &j);
221c0e032e0STom Rini 		break;
222c0e032e0STom Rini 	default:
223c0e032e0STom Rini 		val = c;
224c0e032e0STom Rini 	}
225c0e032e0STom Rini 
226c0e032e0STom Rini 	(*i) = j;
227c0e032e0STom Rini 	return val;
228c0e032e0STom Rini }
229c0e032e0STom Rini 
utilfdt_read_err_len(const char * filename,char ** buffp,off_t * len)230c0e032e0STom Rini int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
231c0e032e0STom Rini {
232c0e032e0STom Rini 	int fd = 0;	/* assume stdin */
233c0e032e0STom Rini 	char *buf = NULL;
234c0e032e0STom Rini 	off_t bufsize = 1024, offset = 0;
235c0e032e0STom Rini 	int ret = 0;
236c0e032e0STom Rini 
237c0e032e0STom Rini 	*buffp = NULL;
238c0e032e0STom Rini 	if (strcmp(filename, "-") != 0) {
239c0e032e0STom Rini 		fd = open(filename, O_RDONLY);
240c0e032e0STom Rini 		if (fd < 0)
241c0e032e0STom Rini 			return errno;
242c0e032e0STom Rini 	}
243c0e032e0STom Rini 
244c0e032e0STom Rini 	/* Loop until we have read everything */
245c0e032e0STom Rini 	buf = xmalloc(bufsize);
246c0e032e0STom Rini 	do {
247c0e032e0STom Rini 		/* Expand the buffer to hold the next chunk */
248c0e032e0STom Rini 		if (offset == bufsize) {
249c0e032e0STom Rini 			bufsize *= 2;
250c0e032e0STom Rini 			buf = xrealloc(buf, bufsize);
251c0e032e0STom Rini 		}
252c0e032e0STom Rini 
253c0e032e0STom Rini 		ret = read(fd, &buf[offset], bufsize - offset);
254c0e032e0STom Rini 		if (ret < 0) {
255c0e032e0STom Rini 			ret = errno;
256c0e032e0STom Rini 			break;
257c0e032e0STom Rini 		}
258c0e032e0STom Rini 		offset += ret;
259c0e032e0STom Rini 	} while (ret != 0);
260c0e032e0STom Rini 
261c0e032e0STom Rini 	/* Clean up, including closing stdin; return errno on error */
262c0e032e0STom Rini 	close(fd);
263c0e032e0STom Rini 	if (ret)
264c0e032e0STom Rini 		free(buf);
265c0e032e0STom Rini 	else
266c0e032e0STom Rini 		*buffp = buf;
267c0e032e0STom Rini 	*len = bufsize;
268c0e032e0STom Rini 	return ret;
269c0e032e0STom Rini }
270c0e032e0STom Rini 
utilfdt_read_err(const char * filename,char ** buffp)271c0e032e0STom Rini int utilfdt_read_err(const char *filename, char **buffp)
272c0e032e0STom Rini {
273c0e032e0STom Rini 	off_t len;
274c0e032e0STom Rini 	return utilfdt_read_err_len(filename, buffp, &len);
275c0e032e0STom Rini }
276c0e032e0STom Rini 
utilfdt_read_len(const char * filename,off_t * len)277c0e032e0STom Rini char *utilfdt_read_len(const char *filename, off_t *len)
278c0e032e0STom Rini {
279c0e032e0STom Rini 	char *buff;
280c0e032e0STom Rini 	int ret = utilfdt_read_err_len(filename, &buff, len);
281c0e032e0STom Rini 
282c0e032e0STom Rini 	if (ret) {
283c0e032e0STom Rini 		fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
284c0e032e0STom Rini 			strerror(ret));
285c0e032e0STom Rini 		return NULL;
286c0e032e0STom Rini 	}
287c0e032e0STom Rini 	/* Successful read */
288c0e032e0STom Rini 	return buff;
289c0e032e0STom Rini }
290c0e032e0STom Rini 
utilfdt_read(const char * filename)291c0e032e0STom Rini char *utilfdt_read(const char *filename)
292c0e032e0STom Rini {
293c0e032e0STom Rini 	off_t len;
294c0e032e0STom Rini 	return utilfdt_read_len(filename, &len);
295c0e032e0STom Rini }
296c0e032e0STom Rini 
utilfdt_write_err(const char * filename,const void * blob)297c0e032e0STom Rini int utilfdt_write_err(const char *filename, const void *blob)
298c0e032e0STom Rini {
299c0e032e0STom Rini 	int fd = 1;	/* assume stdout */
300c0e032e0STom Rini 	int totalsize;
301c0e032e0STom Rini 	int offset;
302c0e032e0STom Rini 	int ret = 0;
303c0e032e0STom Rini 	const char *ptr = blob;
304c0e032e0STom Rini 
305c0e032e0STom Rini 	if (strcmp(filename, "-") != 0) {
306c0e032e0STom Rini 		fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
307c0e032e0STom Rini 		if (fd < 0)
308c0e032e0STom Rini 			return errno;
309c0e032e0STom Rini 	}
310c0e032e0STom Rini 
311c0e032e0STom Rini 	totalsize = fdt_totalsize(blob);
312c0e032e0STom Rini 	offset = 0;
313c0e032e0STom Rini 
314c0e032e0STom Rini 	while (offset < totalsize) {
315c0e032e0STom Rini 		ret = write(fd, ptr + offset, totalsize - offset);
316c0e032e0STom Rini 		if (ret < 0) {
317c0e032e0STom Rini 			ret = -errno;
318c0e032e0STom Rini 			break;
319c0e032e0STom Rini 		}
320c0e032e0STom Rini 		offset += ret;
321c0e032e0STom Rini 	}
322c0e032e0STom Rini 	/* Close the file/stdin; return errno on error */
323c0e032e0STom Rini 	if (fd != 1)
324c0e032e0STom Rini 		close(fd);
325c0e032e0STom Rini 	return ret < 0 ? -ret : 0;
326c0e032e0STom Rini }
327c0e032e0STom Rini 
328c0e032e0STom Rini 
utilfdt_write(const char * filename,const void * blob)329c0e032e0STom Rini int utilfdt_write(const char *filename, const void *blob)
330c0e032e0STom Rini {
331c0e032e0STom Rini 	int ret = utilfdt_write_err(filename, blob);
332c0e032e0STom Rini 
333c0e032e0STom Rini 	if (ret) {
334c0e032e0STom Rini 		fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename,
335c0e032e0STom Rini 			strerror(ret));
336c0e032e0STom Rini 	}
337c0e032e0STom Rini 	return ret ? -1 : 0;
338c0e032e0STom Rini }
339c0e032e0STom Rini 
utilfdt_decode_type(const char * fmt,int * type,int * size)340c0e032e0STom Rini int utilfdt_decode_type(const char *fmt, int *type, int *size)
341c0e032e0STom Rini {
342c0e032e0STom Rini 	int qualifier = 0;
343c0e032e0STom Rini 
344c0e032e0STom Rini 	if (!*fmt)
345c0e032e0STom Rini 		return -1;
346c0e032e0STom Rini 
347c0e032e0STom Rini 	/* get the conversion qualifier */
348c0e032e0STom Rini 	*size = -1;
349c0e032e0STom Rini 	if (strchr("hlLb", *fmt)) {
350c0e032e0STom Rini 		qualifier = *fmt++;
351c0e032e0STom Rini 		if (qualifier == *fmt) {
352c0e032e0STom Rini 			switch (*fmt++) {
353c0e032e0STom Rini /* TODO:		case 'l': qualifier = 'L'; break;*/
354c0e032e0STom Rini 			case 'h':
355c0e032e0STom Rini 				qualifier = 'b';
356c0e032e0STom Rini 				break;
357c0e032e0STom Rini 			}
358c0e032e0STom Rini 		}
359c0e032e0STom Rini 	}
360c0e032e0STom Rini 
361c0e032e0STom Rini 	/* we should now have a type */
362c0e032e0STom Rini 	if ((*fmt == '\0') || !strchr("iuxs", *fmt))
363c0e032e0STom Rini 		return -1;
364c0e032e0STom Rini 
365c0e032e0STom Rini 	/* convert qualifier (bhL) to byte size */
366c0e032e0STom Rini 	if (*fmt != 's')
367c0e032e0STom Rini 		*size = qualifier == 'b' ? 1 :
368c0e032e0STom Rini 				qualifier == 'h' ? 2 :
369c0e032e0STom Rini 				qualifier == 'l' ? 4 : -1;
370c0e032e0STom Rini 	*type = *fmt++;
371c0e032e0STom Rini 
372c0e032e0STom Rini 	/* that should be it! */
373c0e032e0STom Rini 	if (*fmt)
374c0e032e0STom Rini 		return -1;
375c0e032e0STom Rini 	return 0;
376c0e032e0STom Rini }
377c0e032e0STom Rini 
utilfdt_print_data(const char * data,int len)378c0e032e0STom Rini void utilfdt_print_data(const char *data, int len)
379c0e032e0STom Rini {
380c0e032e0STom Rini 	int i;
381c0e032e0STom Rini 	const char *s;
382c0e032e0STom Rini 
383c0e032e0STom Rini 	/* no data, don't print */
384c0e032e0STom Rini 	if (len == 0)
385c0e032e0STom Rini 		return;
386c0e032e0STom Rini 
387c0e032e0STom Rini 	if (util_is_printable_string(data, len)) {
388c0e032e0STom Rini 		printf(" = ");
389c0e032e0STom Rini 
390c0e032e0STom Rini 		s = data;
391c0e032e0STom Rini 		do {
392c0e032e0STom Rini 			printf("\"%s\"", s);
393c0e032e0STom Rini 			s += strlen(s) + 1;
394c0e032e0STom Rini 			if (s < data + len)
395c0e032e0STom Rini 				printf(", ");
396c0e032e0STom Rini 		} while (s < data + len);
397c0e032e0STom Rini 
398c0e032e0STom Rini 	} else if ((len % 4) == 0) {
399*d6fc90ceSTom Rini 		const fdt32_t *cell = (const fdt32_t *)data;
400c0e032e0STom Rini 
401c0e032e0STom Rini 		printf(" = <");
402c0e032e0STom Rini 		for (i = 0, len /= 4; i < len; i++)
403c0e032e0STom Rini 			printf("0x%08x%s", fdt32_to_cpu(cell[i]),
404c0e032e0STom Rini 			       i < (len - 1) ? " " : "");
405c0e032e0STom Rini 		printf(">");
406c0e032e0STom Rini 	} else {
407c0e032e0STom Rini 		const unsigned char *p = (const unsigned char *)data;
408c0e032e0STom Rini 		printf(" = [");
409c0e032e0STom Rini 		for (i = 0; i < len; i++)
410c0e032e0STom Rini 			printf("%02x%s", *p++, i < len - 1 ? " " : "");
411c0e032e0STom Rini 		printf("]");
412c0e032e0STom Rini 	}
413c0e032e0STom Rini }
414c0e032e0STom Rini 
util_version(void)415*d6fc90ceSTom Rini void NORETURN util_version(void)
416c0e032e0STom Rini {
417c0e032e0STom Rini 	printf("Version: %s\n", DTC_VERSION);
418c0e032e0STom Rini 	exit(0);
419c0e032e0STom Rini }
420c0e032e0STom Rini 
util_usage(const char * errmsg,const char * synopsis,const char * short_opts,struct option const long_opts[],const char * const opts_help[])421*d6fc90ceSTom Rini void NORETURN util_usage(const char *errmsg, const char *synopsis,
422*d6fc90ceSTom Rini 			 const char *short_opts,
423*d6fc90ceSTom Rini 			 struct option const long_opts[],
424c0e032e0STom Rini 			 const char * const opts_help[])
425c0e032e0STom Rini {
426c0e032e0STom Rini 	FILE *fp = errmsg ? stderr : stdout;
427c0e032e0STom Rini 	const char a_arg[] = "<arg>";
428c0e032e0STom Rini 	size_t a_arg_len = strlen(a_arg) + 1;
429c0e032e0STom Rini 	size_t i;
430c0e032e0STom Rini 	int optlen;
431c0e032e0STom Rini 
432c0e032e0STom Rini 	fprintf(fp,
433c0e032e0STom Rini 		"Usage: %s\n"
434c0e032e0STom Rini 		"\n"
435c0e032e0STom Rini 		"Options: -[%s]\n", synopsis, short_opts);
436c0e032e0STom Rini 
437c0e032e0STom Rini 	/* prescan the --long opt length to auto-align */
438c0e032e0STom Rini 	optlen = 0;
439c0e032e0STom Rini 	for (i = 0; long_opts[i].name; ++i) {
440c0e032e0STom Rini 		/* +1 is for space between --opt and help text */
441c0e032e0STom Rini 		int l = strlen(long_opts[i].name) + 1;
442c0e032e0STom Rini 		if (long_opts[i].has_arg == a_argument)
443c0e032e0STom Rini 			l += a_arg_len;
444c0e032e0STom Rini 		if (optlen < l)
445c0e032e0STom Rini 			optlen = l;
446c0e032e0STom Rini 	}
447c0e032e0STom Rini 
448c0e032e0STom Rini 	for (i = 0; long_opts[i].name; ++i) {
449c0e032e0STom Rini 		/* helps when adding new applets or options */
450c0e032e0STom Rini 		assert(opts_help[i] != NULL);
451c0e032e0STom Rini 
452c0e032e0STom Rini 		/* first output the short flag if it has one */
453c0e032e0STom Rini 		if (long_opts[i].val > '~')
454c0e032e0STom Rini 			fprintf(fp, "      ");
455c0e032e0STom Rini 		else
456c0e032e0STom Rini 			fprintf(fp, "  -%c, ", long_opts[i].val);
457c0e032e0STom Rini 
458c0e032e0STom Rini 		/* then the long flag */
459c0e032e0STom Rini 		if (long_opts[i].has_arg == no_argument)
460c0e032e0STom Rini 			fprintf(fp, "--%-*s", optlen, long_opts[i].name);
461c0e032e0STom Rini 		else
462c0e032e0STom Rini 			fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
463c0e032e0STom Rini 				(int)(optlen - strlen(long_opts[i].name) - a_arg_len), "");
464c0e032e0STom Rini 
465c0e032e0STom Rini 		/* finally the help text */
466c0e032e0STom Rini 		fprintf(fp, "%s\n", opts_help[i]);
467c0e032e0STom Rini 	}
468c0e032e0STom Rini 
469c0e032e0STom Rini 	if (errmsg) {
470c0e032e0STom Rini 		fprintf(fp, "\nError: %s\n", errmsg);
471c0e032e0STom Rini 		exit(EXIT_FAILURE);
472c0e032e0STom Rini 	} else
473c0e032e0STom Rini 		exit(EXIT_SUCCESS);
474c0e032e0STom Rini }
475