xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/testutil/options.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1*b0d17251Schristos /*
2*b0d17251Schristos  * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6*b0d17251Schristos  * in the file LICENSE in the source distribution or at
7*b0d17251Schristos  * https://www.openssl.org/source/license.html
8*b0d17251Schristos  */
9*b0d17251Schristos 
10*b0d17251Schristos #include "../testutil.h"
11*b0d17251Schristos #include "internal/nelem.h"
12*b0d17251Schristos #include "tu_local.h"
13*b0d17251Schristos #include "output.h"
14*b0d17251Schristos 
15*b0d17251Schristos 
16*b0d17251Schristos static int used[100] = { 0 };
17*b0d17251Schristos 
test_skip_common_options(void)18*b0d17251Schristos int test_skip_common_options(void)
19*b0d17251Schristos {
20*b0d17251Schristos     OPTION_CHOICE_DEFAULT o;
21*b0d17251Schristos 
22*b0d17251Schristos     while ((o = (OPTION_CHOICE_DEFAULT)opt_next()) != OPT_EOF) {
23*b0d17251Schristos         switch (o) {
24*b0d17251Schristos         case OPT_TEST_CASES:
25*b0d17251Schristos            break;
26*b0d17251Schristos         default:
27*b0d17251Schristos         case OPT_ERR:
28*b0d17251Schristos             return 0;
29*b0d17251Schristos         }
30*b0d17251Schristos     }
31*b0d17251Schristos     return 1;
32*b0d17251Schristos }
33*b0d17251Schristos 
test_get_argument_count(void)34*b0d17251Schristos size_t test_get_argument_count(void)
35*b0d17251Schristos {
36*b0d17251Schristos     return opt_num_rest();
37*b0d17251Schristos }
38*b0d17251Schristos 
test_get_argument(size_t n)39*b0d17251Schristos char *test_get_argument(size_t n)
40*b0d17251Schristos {
41*b0d17251Schristos     char **argv = opt_rest();
42*b0d17251Schristos 
43*b0d17251Schristos     OPENSSL_assert(n < sizeof(used));
44*b0d17251Schristos     if ((int)n >= opt_num_rest() || argv == NULL)
45*b0d17251Schristos         return NULL;
46*b0d17251Schristos     used[n] = 1;
47*b0d17251Schristos     return argv[n];
48*b0d17251Schristos }
49*b0d17251Schristos 
opt_check_usage(void)50*b0d17251Schristos void opt_check_usage(void)
51*b0d17251Schristos {
52*b0d17251Schristos     int i;
53*b0d17251Schristos     char **argv = opt_rest();
54*b0d17251Schristos     int n, arg_count = opt_num_rest();
55*b0d17251Schristos 
56*b0d17251Schristos     if (arg_count > (int)OSSL_NELEM(used))
57*b0d17251Schristos         n = (int)OSSL_NELEM(used);
58*b0d17251Schristos     else
59*b0d17251Schristos         n = arg_count;
60*b0d17251Schristos     for (i = 0; i < n; i++) {
61*b0d17251Schristos         if (used[i] == 0)
62*b0d17251Schristos             test_printf_stderr("Warning ignored command-line argument %d: %s\n",
63*b0d17251Schristos                                i, argv[i]);
64*b0d17251Schristos     }
65*b0d17251Schristos     if (i < arg_count)
66*b0d17251Schristos         test_printf_stderr("Warning arguments %d and later unchecked\n", i);
67*b0d17251Schristos }
68*b0d17251Schristos 
opt_printf_stderr(const char * fmt,...)69*b0d17251Schristos int opt_printf_stderr(const char *fmt, ...)
70*b0d17251Schristos {
71*b0d17251Schristos     va_list ap;
72*b0d17251Schristos     int ret;
73*b0d17251Schristos 
74*b0d17251Schristos     va_start(ap, fmt);
75*b0d17251Schristos     ret = test_vprintf_stderr(fmt, ap);
76*b0d17251Schristos     va_end(ap);
77*b0d17251Schristos     return ret;
78*b0d17251Schristos }
79*b0d17251Schristos 
80