File: opt.c

package info (click to toggle)
libstatgrab 0.92.1-1.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,252 kB
  • sloc: ansic: 12,623; sh: 4,364; perl: 467; makefile: 282
file content (91 lines) | stat: -rw-r--r-- 2,567 bytes parent folder | download | duplicates (3)
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
/*
 * libstatgrab
 * https://libstatgrab.org
 * Copyright (C) 2003-2004 Peter Saunders
 * Copyright (C) 2003-2019 Tim Bishop
 * Copyright (C) 2003-2013 Adam Sampson
 * Copyright (C) 2012-2019 Jens Rehsack
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <tools.h>
#include "testlib.h"

int
get_params(struct opt_def *opt_defs, int argc, char **argv) {
	char optlst[256] = { '\0' };
	struct opt_def *opt_def_map[256];
	size_t optlst_idx = 0;
	struct opt_def *opt_l;
	int ch;

	memset( opt_def_map, 0, sizeof(opt_def_map) );

	for( opt_l = opt_defs; opt_l && opt_l->optchr; ++opt_l ) {
		if( opt_def_map[(unsigned)(opt_l->optchr)] != NULL )
			die( EINVAL, "duplicate option def for '%c'", opt_l->optchr );
		opt_def_map[(unsigned)(opt_l->optchr)] = opt_l;
		optlst[optlst_idx++] = opt_l->optchr;
		if( opt_l->opttype != opt_flag )
			optlst[optlst_idx++] = ':';
	}
	optlst[optlst_idx] = '\0';

	while( (ch = getopt(argc, argv, optlst)) >= 0 ) {
		if( ch >= 256 ) {
			die( EINVAL, "getopt error, rc = %d", ch );
		}
		if( opt_def_map[ch] != 0 ) {
			switch(opt_def_map[ch]->opttype) {
			case opt_flag:
				opt_def_map[ch]->optarg.b = true;
				break;

			case opt_bool:
				{
					unsigned tmp;
					if( 1 != sscanf( optarg, "%u", &tmp ) )
						die( EINVAL, "getopt: invalid argument for -%c: %s", ch, optarg );
					opt_def_map[ch]->optarg.b = tmp != 0;
				}

				break;

			case opt_signed:
				if( 1 != sscanf( optarg, "%ld", &opt_def_map[ch]->optarg.s ) )
					die( EINVAL, "getopt: invalid argument for -%c: %s", ch, optarg );

				break;

			case opt_unsigned:
				if( 1 != sscanf( optarg, "%lu", &opt_def_map[ch]->optarg.u ) )
					die( EINVAL, "getopt: invalid argument for -%c: %s", ch, optarg );

				break;

			case opt_str:
				opt_def_map[ch]->optarg.str = strdup(optarg);
				break;
			}
		}
		else {
			return ch;
		}
	}

	return 0;
}