| 1 |
/* |
| 2 |
* Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "internal/e_os.h" |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include "internal/common.h" |
| 15 |
#include <openssl/bio.h> |
| 16 |
#include <openssl/crypto.h> |
| 17 |
#include <openssl/trace.h> |
| 18 |
#include <openssl/lhash.h> |
| 19 |
#include <openssl/conf.h> |
| 20 |
#include <openssl/x509.h> |
| 21 |
#include <openssl/pem.h> |
| 22 |
#include <openssl/ssl.h> |
| 23 |
#ifndef OPENSSL_NO_ENGINE |
| 24 |
#include <openssl/engine.h> |
| 25 |
#endif |
| 26 |
#include <openssl/err.h> |
| 27 |
/* Needed to get the other O_xxx flags. */ |
| 28 |
#ifdef OPENSSL_SYS_VMS |
| 29 |
#include <unixio.h> |
| 30 |
#endif |
| 31 |
#include "apps.h" |
| 32 |
#include "progs.h" |
| 33 |
|
| 34 |
/* |
| 35 |
* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with |
| 36 |
* the base prototypes (we cast each variable inside the function to the |
| 37 |
* required type of "FUNCTION*"). This removes the necessity for |
| 38 |
* macro-generated wrapper functions. |
| 39 |
*/ |
| 40 |
static LHASH_OF(FUNCTION) *prog_init(void); |
| 41 |
static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]); |
| 42 |
char *default_config_file = NULL; |
| 43 |
|
| 44 |
BIO *bio_in = NULL; |
| 45 |
BIO *bio_out = NULL; |
| 46 |
BIO *bio_err = NULL; |
| 47 |
|
| 48 |
static void warn_deprecated(const FUNCTION *fp) |
| 49 |
{ |
| 50 |
if (fp->deprecated_version != NULL) |
| 51 |
BIO_printf(bio_err, "The command %s was deprecated in version %s.", |
| 52 |
fp->name, fp->deprecated_version); |
| 53 |
else |
| 54 |
BIO_printf(bio_err, "The command %s is deprecated.", fp->name); |
| 55 |
if (strcmp(fp->deprecated_alternative, DEPRECATED_NO_ALTERNATIVE) != 0) |
| 56 |
BIO_printf(bio_err, " Use '%s' instead.", fp->deprecated_alternative); |
| 57 |
BIO_printf(bio_err, "\n"); |
| 58 |
} |
| 59 |
|
| 60 |
static int apps_startup(void) |
| 61 |
{ |
| 62 |
const char *use_libctx = NULL; |
| 63 |
#ifdef SIGPIPE |
| 64 |
signal(SIGPIPE, SIG_IGN); |
| 65 |
#endif |
| 66 |
|
| 67 |
/* Set non-default library initialisation settings */ |
| 68 |
if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN |
| 69 |
| OPENSSL_INIT_LOAD_CONFIG, |
| 70 |
NULL)) |
| 71 |
return 0; |
| 72 |
|
| 73 |
(void)setup_ui_method(); |
| 74 |
(void)setup_engine_loader(); |
| 75 |
|
| 76 |
/* |
| 77 |
* NOTE: This is an undocumented feature required for testing only. |
| 78 |
* There are no guarantees that it will exist in future builds. |
| 79 |
*/ |
| 80 |
use_libctx = getenv("OPENSSL_TEST_LIBCTX"); |
| 81 |
if (use_libctx != NULL) { |
| 82 |
/* Set this to "1" to create a global libctx */ |
| 83 |
if (strcmp(use_libctx, "1") == 0) { |
| 84 |
if (app_create_libctx() == NULL) |
| 85 |
return 0; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
static void apps_shutdown(void) |
| 93 |
{ |
| 94 |
app_providers_cleanup(); |
| 95 |
OSSL_LIB_CTX_free(app_get0_libctx()); |
| 96 |
destroy_engine_loader(); |
| 97 |
destroy_ui_method(); |
| 98 |
} |
| 99 |
|
| 100 |
#ifndef OPENSSL_NO_TRACE |
| 101 |
typedef struct tracedata_st { |
| 102 |
BIO *bio; |
| 103 |
unsigned int ingroup : 1; |
| 104 |
} tracedata; |
| 105 |
|
| 106 |
static size_t internal_trace_cb(const char *buf, size_t cnt, |
| 107 |
int category, int cmd, void *vdata) |
| 108 |
{ |
| 109 |
int ret = 0; |
| 110 |
tracedata *trace_data = vdata; |
| 111 |
char buffer[256], *hex; |
| 112 |
CRYPTO_THREAD_ID tid; |
| 113 |
|
| 114 |
switch (cmd) { |
| 115 |
case OSSL_TRACE_CTRL_BEGIN: |
| 116 |
if (trace_data->ingroup) { |
| 117 |
BIO_printf(bio_err, "ERROR: tracing already started\n"); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
trace_data->ingroup = 1; |
| 121 |
|
| 122 |
tid = CRYPTO_THREAD_get_current_id(); |
| 123 |
hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid)); |
| 124 |
BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ", |
| 125 |
hex == NULL ? "<null>" : hex, |
| 126 |
OSSL_trace_get_category_name(category)); |
| 127 |
OPENSSL_free(hex); |
| 128 |
BIO_set_prefix(trace_data->bio, buffer); |
| 129 |
break; |
| 130 |
case OSSL_TRACE_CTRL_WRITE: |
| 131 |
if (!trace_data->ingroup) { |
| 132 |
BIO_printf(bio_err, "ERROR: writing when tracing not started\n"); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
if (cnt > INT_MAX) |
| 136 |
cnt = INT_MAX; |
| 137 |
|
| 138 |
ret = BIO_write(trace_data->bio, buf, (int)cnt); |
| 139 |
break; |
| 140 |
case OSSL_TRACE_CTRL_END: |
| 141 |
if (!trace_data->ingroup) { |
| 142 |
BIO_printf(bio_err, "ERROR: finishing when tracing not started\n"); |
| 143 |
return 0; |
| 144 |
} |
| 145 |
trace_data->ingroup = 0; |
| 146 |
|
| 147 |
BIO_set_prefix(trace_data->bio, NULL); |
| 148 |
|
| 149 |
break; |
| 150 |
} |
| 151 |
|
| 152 |
return ret < 0 ? 0 : ret; |
| 153 |
} |
| 154 |
|
| 155 |
DEFINE_STACK_OF(tracedata) |
| 156 |
static STACK_OF(tracedata) *trace_data_stack; |
| 157 |
|
| 158 |
static void tracedata_free(tracedata *data) |
| 159 |
{ |
| 160 |
BIO_free_all(data->bio); |
| 161 |
OPENSSL_free(data); |
| 162 |
} |
| 163 |
|
| 164 |
static void cleanup_trace(void) |
| 165 |
{ |
| 166 |
sk_tracedata_pop_free(trace_data_stack, tracedata_free); |
| 167 |
} |
| 168 |
|
| 169 |
static void setup_trace_category(int category) |
| 170 |
{ |
| 171 |
BIO *channel; |
| 172 |
tracedata *trace_data; |
| 173 |
BIO *bio = NULL; |
| 174 |
|
| 175 |
if (OSSL_trace_enabled(category)) |
| 176 |
return; |
| 177 |
|
| 178 |
bio = BIO_new(BIO_f_prefix()); |
| 179 |
channel = BIO_push(bio, dup_bio_err(FORMAT_TEXT)); |
| 180 |
trace_data = OPENSSL_zalloc(sizeof(*trace_data)); |
| 181 |
|
| 182 |
if (trace_data == NULL |
| 183 |
|| bio == NULL |
| 184 |
|| (trace_data->bio = channel) == NULL |
| 185 |
|| OSSL_trace_set_callback(category, internal_trace_cb, |
| 186 |
trace_data) |
| 187 |
== 0 |
| 188 |
|| sk_tracedata_push(trace_data_stack, trace_data) == 0) { |
| 189 |
|
| 190 |
fprintf(stderr, |
| 191 |
"warning: unable to setup trace callback for category '%s'.\n", |
| 192 |
OSSL_trace_get_category_name(category)); |
| 193 |
|
| 194 |
OSSL_trace_set_callback(category, NULL, NULL); |
| 195 |
BIO_free_all(channel); |
| 196 |
OPENSSL_free(trace_data); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
static void setup_trace(const char *str) |
| 201 |
{ |
| 202 |
char *val; |
| 203 |
|
| 204 |
/* |
| 205 |
* We add this handler as early as possible to ensure it's executed |
| 206 |
* as late as possible, i.e. after the TRACE code has done its cleanup |
| 207 |
* (which happens last in OPENSSL_cleanup). |
| 208 |
*/ |
| 209 |
atexit(cleanup_trace); |
| 210 |
|
| 211 |
trace_data_stack = sk_tracedata_new_null(); |
| 212 |
val = OPENSSL_strdup(str); |
| 213 |
|
| 214 |
if (val != NULL) { |
| 215 |
char *valp = val; |
| 216 |
char *item; |
| 217 |
|
| 218 |
for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) { |
| 219 |
int category = OSSL_trace_get_category_num(item); |
| 220 |
|
| 221 |
if (category == OSSL_TRACE_CATEGORY_ALL) { |
| 222 |
while (++category < OSSL_TRACE_CATEGORY_NUM) |
| 223 |
setup_trace_category(category); |
| 224 |
break; |
| 225 |
} else if (category > 0) { |
| 226 |
setup_trace_category(category); |
| 227 |
} else { |
| 228 |
fprintf(stderr, |
| 229 |
"warning: unknown trace category: '%s'.\n", item); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
OPENSSL_free(val); |
| 235 |
} |
| 236 |
#endif /* OPENSSL_NO_TRACE */ |
| 237 |
|
| 238 |
static char *help_argv[] = { "help", NULL }; |
| 239 |
static char *version_argv[] = { "version", NULL }; |
| 240 |
|
| 241 |
int main(int argc, char *argv[]) |
| 242 |
{ |
| 243 |
FUNCTION f, *fp; |
| 244 |
LHASH_OF(FUNCTION) *prog = NULL; |
| 245 |
char *pname; |
| 246 |
const char *fname; |
| 247 |
ARGS arg; |
| 248 |
int global_help = 0; |
| 249 |
int global_version = 0; |
| 250 |
int ret = 0; |
| 251 |
char *sec_mem_char = NULL; |
| 252 |
#ifndef OPENSSL_NO_SECURE_MEMORY |
| 253 |
char *sec_mem_minsize_char = NULL; |
| 254 |
#endif |
| 255 |
|
| 256 |
arg.argv = NULL; |
| 257 |
arg.size = 0; |
| 258 |
|
| 259 |
/* Set up some of the environment. */ |
| 260 |
bio_in = dup_bio_in(FORMAT_TEXT); |
| 261 |
bio_out = dup_bio_out(FORMAT_TEXT); |
| 262 |
bio_err = dup_bio_err(FORMAT_TEXT); |
| 263 |
|
| 264 |
#if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
| 265 |
argv = copy_argv(&argc, argv); |
| 266 |
#elif defined(_WIN32) |
| 267 |
/* Replace argv[] with UTF-8 encoded strings. */ |
| 268 |
win32_utf8argv(&argc, &argv); |
| 269 |
#endif |
| 270 |
|
| 271 |
#ifndef OPENSSL_NO_TRACE |
| 272 |
setup_trace(getenv("OPENSSL_TRACE")); |
| 273 |
#endif |
| 274 |
|
| 275 |
sec_mem_char = getenv("OPENSSL_SEC_MEM"); |
| 276 |
if (sec_mem_char != NULL) { |
| 277 |
#ifndef OPENSSL_NO_SECURE_MEMORY |
| 278 |
long sec_mem = 0; |
| 279 |
long sec_mem_minsize = 0; |
| 280 |
char *end = NULL; |
| 281 |
|
| 282 |
errno = 0; |
| 283 |
sec_mem = strtol(sec_mem_char, &end, 0); |
| 284 |
if (errno != 0 || *end != 0 || end == sec_mem_char) { |
| 285 |
BIO_printf(bio_err, |
| 286 |
"FATAL: could not convert OPENSSL_SEC_MEM (%s) to number\n", |
| 287 |
sec_mem_char); |
| 288 |
ret = EXIT_FAILURE; |
| 289 |
goto end; |
| 290 |
} |
| 291 |
|
| 292 |
/* |
| 293 |
* Try to fetch the minsize if given, if not use the default value. |
| 294 |
*/ |
| 295 |
sec_mem_minsize_char = getenv("OPENSSL_SEC_MEM_MINSIZE"); |
| 296 |
if (sec_mem_minsize_char != NULL) { |
| 297 |
errno = 0; |
| 298 |
sec_mem_minsize = strtol(sec_mem_minsize_char, &end, 0); |
| 299 |
if (errno != 0 || *end != 0 || end == sec_mem_minsize_char) { |
| 300 |
BIO_printf(bio_err, |
| 301 |
"FATAL: could not convert OPENSSL_SEC_MEM_MINSIZE (%s) to number\n", |
| 302 |
sec_mem_minsize_char); |
| 303 |
ret = 1; |
| 304 |
goto end; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
ret = CRYPTO_secure_malloc_init(sec_mem, sec_mem_minsize); |
| 309 |
if (ret != 1) { |
| 310 |
BIO_printf(bio_err, |
| 311 |
"FATAL: could not initialize secure memory\n"); |
| 312 |
ERR_print_errors(bio_err); |
| 313 |
ret = 1; |
| 314 |
goto end; |
| 315 |
} |
| 316 |
#else |
| 317 |
BIO_printf(bio_err, |
| 318 |
"FATAL: OPENSSL_SEC_MEM environment variable was set, but " |
| 319 |
"openssl was compiled without secure memory support.\n"); |
| 320 |
#endif |
| 321 |
} |
| 322 |
|
| 323 |
if ((fname = "apps_startup", !apps_startup()) |
| 324 |
|| (fname = "prog_init", (prog = prog_init()) == NULL)) { |
| 325 |
BIO_printf(bio_err, |
| 326 |
"FATAL: Startup failure (dev note: %s()) for %s\n", |
| 327 |
fname, argv[0]); |
| 328 |
ERR_print_errors(bio_err); |
| 329 |
ret = 1; |
| 330 |
goto end; |
| 331 |
} |
| 332 |
pname = opt_progname(argv[0]); |
| 333 |
|
| 334 |
default_config_file = CONF_get1_default_config_file(); |
| 335 |
if (default_config_file == NULL) |
| 336 |
app_bail_out("%s: could not get default config file\n", pname); |
| 337 |
|
| 338 |
/* first check the program name */ |
| 339 |
f.name = pname; |
| 340 |
fp = lh_FUNCTION_retrieve(prog, &f); |
| 341 |
if (fp == NULL) { |
| 342 |
/* We assume we've been called as 'openssl ...' */ |
| 343 |
global_help = argc > 1 |
| 344 |
&& (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0 |
| 345 |
|| strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--h") == 0); |
| 346 |
global_version = argc > 1 |
| 347 |
&& (strcmp(argv[1], "-version") == 0 || strcmp(argv[1], "--version") == 0 |
| 348 |
|| strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--v") == 0); |
| 349 |
|
| 350 |
argc--; |
| 351 |
argv++; |
| 352 |
opt_appname(argc == 1 || global_help ? "help" : global_version ? "version" |
| 353 |
: argv[0]); |
| 354 |
} else { |
| 355 |
argv[0] = pname; |
| 356 |
} |
| 357 |
|
| 358 |
/* |
| 359 |
* If there's no command, assume "help". If there's an override for help |
| 360 |
* or version run those, otherwise run the command given. |
| 361 |
*/ |
| 362 |
ret = (argc == 0) || global_help |
| 363 |
? do_cmd(prog, 1, help_argv) |
| 364 |
: global_version |
| 365 |
? do_cmd(prog, 1, version_argv) |
| 366 |
: do_cmd(prog, argc, argv); |
| 367 |
|
| 368 |
end: |
| 369 |
OPENSSL_free(default_config_file); |
| 370 |
lh_FUNCTION_free(prog); |
| 371 |
OPENSSL_free(arg.argv); |
| 372 |
if (!app_RAND_write()) |
| 373 |
ret = EXIT_FAILURE; |
| 374 |
|
| 375 |
BIO_free(bio_in); |
| 376 |
BIO_free_all(bio_out); |
| 377 |
apps_shutdown(); |
| 378 |
BIO_free_all(bio_err); |
| 379 |
#ifndef OPENSSL_NO_SECURE_MEMORY |
| 380 |
CRYPTO_secure_malloc_done(); |
| 381 |
#endif |
| 382 |
EXIT(ret); |
| 383 |
} |
| 384 |
|
| 385 |
typedef enum HELP_CHOICE { |
| 386 |
OPT_hERR = -1, |
| 387 |
OPT_hEOF = 0, |
| 388 |
OPT_hHELP |
| 389 |
} HELP_CHOICE; |
| 390 |
|
| 391 |
const OPTIONS help_options[] = { |
| 392 |
{ OPT_HELP_STR, 1, '-', "Usage: help [options] [command]\n" }, |
| 393 |
|
| 394 |
OPT_SECTION("General"), |
| 395 |
{ "help", OPT_hHELP, '-', "Display this summary" }, |
| 396 |
|
| 397 |
OPT_PARAMETERS(), |
| 398 |
{ "command", 0, 0, "Name of command to display help (optional)" }, |
| 399 |
{ NULL } |
| 400 |
}; |
| 401 |
|
| 402 |
int help_main(int argc, char **argv) |
| 403 |
{ |
| 404 |
FUNCTION *fp; |
| 405 |
int i, nl; |
| 406 |
FUNC_TYPE tp; |
| 407 |
char *prog; |
| 408 |
HELP_CHOICE o; |
| 409 |
DISPLAY_COLUMNS dc; |
| 410 |
char *new_argv[3]; |
| 411 |
|
| 412 |
prog = opt_init(argc, argv, help_options); |
| 413 |
while ((o = opt_next()) != OPT_hEOF) { |
| 414 |
switch (o) { |
| 415 |
case OPT_hERR: |
| 416 |
case OPT_hEOF: |
| 417 |
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
| 418 |
return 1; |
| 419 |
case OPT_hHELP: |
| 420 |
opt_help(help_options); |
| 421 |
return 0; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
/* One optional argument, the command to get help for. */ |
| 426 |
if (opt_num_rest() == 1) { |
| 427 |
new_argv[0] = opt_rest()[0]; |
| 428 |
new_argv[1] = "--help"; |
| 429 |
new_argv[2] = NULL; |
| 430 |
return do_cmd(prog_init(), 2, new_argv); |
| 431 |
} |
| 432 |
if (!opt_check_rest_arg(NULL)) { |
| 433 |
BIO_printf(bio_err, "Usage: %s\n", prog); |
| 434 |
return 1; |
| 435 |
} |
| 436 |
|
| 437 |
calculate_columns(functions, &dc); |
| 438 |
BIO_printf(bio_err, "%s:\n\nStandard commands", prog); |
| 439 |
i = 0; |
| 440 |
tp = FT_none; |
| 441 |
for (fp = functions; fp->name != NULL; fp++) { |
| 442 |
nl = 0; |
| 443 |
if (i++ % dc.columns == 0) { |
| 444 |
BIO_printf(bio_err, "\n"); |
| 445 |
nl = 1; |
| 446 |
} |
| 447 |
if (fp->type != tp) { |
| 448 |
tp = fp->type; |
| 449 |
if (!nl) |
| 450 |
BIO_printf(bio_err, "\n"); |
| 451 |
if (tp == FT_md) { |
| 452 |
i = 1; |
| 453 |
BIO_printf(bio_err, |
| 454 |
"\nMessage Digest commands (see the `dgst' command for more details)\n"); |
| 455 |
} else if (tp == FT_cipher) { |
| 456 |
i = 1; |
| 457 |
BIO_printf(bio_err, |
| 458 |
"\nCipher commands (see the `enc' command for more details)\n"); |
| 459 |
} |
| 460 |
} |
| 461 |
BIO_printf(bio_err, "%-*s", dc.width, fp->name); |
| 462 |
} |
| 463 |
BIO_printf(bio_err, "\n\n"); |
| 464 |
return 0; |
| 465 |
} |
| 466 |
|
| 467 |
static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]) |
| 468 |
{ |
| 469 |
FUNCTION f, *fp; |
| 470 |
|
| 471 |
if (argc <= 0 || argv[0] == NULL) |
| 472 |
return 0; |
| 473 |
memset(&f, 0, sizeof(f)); |
| 474 |
f.name = argv[0]; |
| 475 |
fp = lh_FUNCTION_retrieve(prog, &f); |
| 476 |
if (fp == NULL) { |
| 477 |
if (EVP_get_digestbyname(argv[0])) { |
| 478 |
f.type = FT_md; |
| 479 |
f.func = dgst_main; |
| 480 |
fp = &f; |
| 481 |
} else if (EVP_get_cipherbyname(argv[0])) { |
| 482 |
f.type = FT_cipher; |
| 483 |
f.func = enc_main; |
| 484 |
fp = &f; |
| 485 |
} |
| 486 |
} |
| 487 |
if (fp != NULL) { |
| 488 |
if (fp->deprecated_alternative != NULL) |
| 489 |
warn_deprecated(fp); |
| 490 |
return fp->func(argc, argv); |
| 491 |
} |
| 492 |
f.name = argv[0]; |
| 493 |
if (CHECK_AND_SKIP_PREFIX(f.name, "no-")) { |
| 494 |
/* |
| 495 |
* User is asking if foo is unsupported, by trying to "run" the |
| 496 |
* no-foo command. Strange. |
| 497 |
*/ |
| 498 |
if (lh_FUNCTION_retrieve(prog, &f) == NULL) { |
| 499 |
BIO_printf(bio_out, "%s\n", argv[0]); |
| 500 |
return 0; |
| 501 |
} |
| 502 |
BIO_printf(bio_out, "%s\n", argv[0] + 3); |
| 503 |
return 1; |
| 504 |
} |
| 505 |
|
| 506 |
BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n", |
| 507 |
argv[0]); |
| 508 |
return 1; |
| 509 |
} |
| 510 |
|
| 511 |
static int function_cmp(const FUNCTION *a, const FUNCTION *b) |
| 512 |
{ |
| 513 |
return strncmp(a->name, b->name, 8); |
| 514 |
} |
| 515 |
|
| 516 |
static unsigned long function_hash(const FUNCTION *a) |
| 517 |
{ |
| 518 |
return OPENSSL_LH_strhash(a->name); |
| 519 |
} |
| 520 |
|
| 521 |
static int SortFnByName(const void *_f1, const void *_f2) |
| 522 |
{ |
| 523 |
const FUNCTION *f1 = _f1; |
| 524 |
const FUNCTION *f2 = _f2; |
| 525 |
|
| 526 |
if (f1->type != f2->type) |
| 527 |
return f1->type - f2->type; |
| 528 |
return strcmp(f1->name, f2->name); |
| 529 |
} |
| 530 |
|
| 531 |
static LHASH_OF(FUNCTION) *prog_init(void) |
| 532 |
{ |
| 533 |
static LHASH_OF(FUNCTION) *ret = NULL; |
| 534 |
static int prog_inited = 0; |
| 535 |
FUNCTION *f; |
| 536 |
size_t i; |
| 537 |
|
| 538 |
if (prog_inited) |
| 539 |
return ret; |
| 540 |
|
| 541 |
prog_inited = 1; |
| 542 |
|
| 543 |
/* Sort alphabetically within category. For nicer help displays. */ |
| 544 |
for (i = 0, f = functions; f->name != NULL; ++f, ++i) |
| 545 |
; |
| 546 |
qsort(functions, i, sizeof(*functions), SortFnByName); |
| 547 |
|
| 548 |
if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL) |
| 549 |
return NULL; |
| 550 |
|
| 551 |
for (f = functions; f->name != NULL; f++) |
| 552 |
(void)lh_FUNCTION_insert(ret, f); |
| 553 |
return ret; |
| 554 |
} |