When apps_startup() fails, exit with a failure code and a message
[openssl.git] / apps / openssl.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/lhash.h>
16 #include <openssl/conf.h>
17 #include <openssl/x509.h>
18 #include <openssl/pem.h>
19 #include <openssl/ssl.h>
20 #ifndef OPENSSL_NO_ENGINE
21 # include <openssl/engine.h>
22 #endif
23 #include <openssl/err.h>
24 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
25 #include "s_apps.h"
26 /* Needed to get the other O_xxx flags. */
27 #ifdef OPENSSL_SYS_VMS
28 # include <unixio.h>
29 #endif
30 #define INCLUDE_FUNCTION_TABLE
31 #include "apps.h"
32
33
34 #ifdef OPENSSL_NO_CAMELLIA
35 # define FORMAT "%-15s"
36 # define COLUMNS 5
37 #else
38 # define FORMAT "%-18s"
39 # define COLUMNS 4
40 #endif
41
42 /* Special sentinel to exit the program. */
43 #define EXIT_THE_PROGRAM (-1)
44
45 /*
46  * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
47  * the base prototypes (we cast each variable inside the function to the
48  * required type of "FUNCTION*"). This removes the necessity for
49  * macro-generated wrapper functions.
50  */
51 static LHASH_OF(FUNCTION) *prog_init(void);
52 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
53 static void list_pkey(void);
54 static void list_type(FUNC_TYPE ft);
55 static void list_disabled(void);
56 char *default_config_file = NULL;
57
58 BIO *bio_in = NULL;
59 BIO *bio_out = NULL;
60 BIO *bio_err = NULL;
61
62 static int apps_startup()
63 {
64 #ifdef SIGPIPE
65     signal(SIGPIPE, SIG_IGN);
66 #endif
67
68     /* Set non-default library initialisation settings */
69     if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
70                              | OPENSSL_INIT_LOAD_CONFIG, NULL))
71         return 0;
72
73 #ifndef OPENSSL_NO_UI
74     setup_ui_method();
75 #endif
76
77     return 1;
78 }
79
80 static void apps_shutdown()
81 {
82 #ifndef OPENSSL_NO_UI
83     destroy_ui_method();
84 #endif
85 }
86
87 static char *make_config_name()
88 {
89     const char *t;
90     size_t len;
91     char *p;
92
93     if ((t = getenv("OPENSSL_CONF")) != NULL)
94         return OPENSSL_strdup(t);
95
96     t = X509_get_default_cert_area();
97     len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
98     p = app_malloc(len, "config filename buffer");
99     strcpy(p, t);
100 #ifndef OPENSSL_SYS_VMS
101     strcat(p, "/");
102 #endif
103     strcat(p, OPENSSL_CONF);
104
105     return p;
106 }
107
108 int main(int argc, char *argv[])
109 {
110     FUNCTION f, *fp;
111     LHASH_OF(FUNCTION) *prog = NULL;
112     char **copied_argv = NULL;
113     char *p, *pname;
114     char buf[1024];
115     const char *prompt;
116     ARGS arg;
117     int first, n, i, ret = 0;
118
119     arg.argv = NULL;
120     arg.size = 0;
121
122     /* Set up some of the environment. */
123     default_config_file = make_config_name();
124     bio_in = dup_bio_in(FORMAT_TEXT);
125     bio_out = dup_bio_out(FORMAT_TEXT);
126     bio_err = dup_bio_err(FORMAT_TEXT);
127
128 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
129     copied_argv = argv = copy_argv(&argc, argv);
130 #elif defined(_WIN32)
131     /*
132      * Replace argv[] with UTF-8 encoded strings.
133      */
134     win32_utf8argv(&argc, &argv);
135 #endif
136
137     p = getenv("OPENSSL_DEBUG_MEMORY");
138     if (p != NULL && strcmp(p, "on") == 0)
139         CRYPTO_set_mem_debug(1);
140     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
141
142     if (getenv("OPENSSL_FIPS")) {
143         BIO_printf(bio_err, "FIPS mode not supported.\n");
144         return 1;
145     }
146
147     if (!apps_startup()) {
148         BIO_printf(bio_err,
149                    "FATAL: Startup failure (dev note: apps_startup() failed)\n");
150         ERR_print_errors(bio_err);
151         ret = 1;
152         goto end;
153     }
154
155     prog = prog_init();
156     pname = opt_progname(argv[0]);
157
158     /* first check the program name */
159     f.name = pname;
160     fp = lh_FUNCTION_retrieve(prog, &f);
161     if (fp != NULL) {
162         argv[0] = pname;
163         ret = fp->func(argc, argv);
164         goto end;
165     }
166
167     /* If there is stuff on the command line, run with that. */
168     if (argc != 1) {
169         argc--;
170         argv++;
171         ret = do_cmd(prog, argc, argv);
172         if (ret < 0)
173             ret = 0;
174         goto end;
175     }
176
177     /* ok, lets enter interactive mode */
178     for (;;) {
179         ret = 0;
180         /* Read a line, continue reading if line ends with \ */
181         for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
182             prompt = first ? "OpenSSL> " : "> ";
183             p[0] = '\0';
184 #ifndef READLINE
185             fputs(prompt, stdout);
186             fflush(stdout);
187             if (!fgets(p, n, stdin))
188                 goto end;
189             if (p[0] == '\0')
190                 goto end;
191             i = strlen(p);
192             if (i <= 1)
193                 break;
194             if (p[i - 2] != '\\')
195                 break;
196             i -= 2;
197             p += i;
198             n -= i;
199 #else
200             {
201                 extern char *readline(const char *);
202                 extern void add_history(const char *cp);
203                 char *text;
204
205                 text = readline(prompt);
206                 if (text == NULL)
207                     goto end;
208                 i = strlen(text);
209                 if (i == 0 || i > n)
210                     break;
211                 if (text[i - 1] != '\\') {
212                     p += strlen(strcpy(p, text));
213                     free(text);
214                     add_history(buf);
215                     break;
216                 }
217
218                 text[i - 1] = '\0';
219                 p += strlen(strcpy(p, text));
220                 free(text);
221                 n -= i;
222             }
223 #endif
224         }
225
226         if (!chopup_args(&arg, buf)) {
227             BIO_printf(bio_err, "Can't parse (no memory?)\n");
228             break;
229         }
230
231         ret = do_cmd(prog, arg.argc, arg.argv);
232         if (ret == EXIT_THE_PROGRAM) {
233             ret = 0;
234             goto end;
235         }
236         if (ret != 0)
237             BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
238         (void)BIO_flush(bio_out);
239         (void)BIO_flush(bio_err);
240     }
241     ret = 1;
242  end:
243     OPENSSL_free(copied_argv);
244     OPENSSL_free(default_config_file);
245     lh_FUNCTION_free(prog);
246     OPENSSL_free(arg.argv);
247
248     BIO_free(bio_in);
249     BIO_free_all(bio_out);
250     apps_shutdown();
251 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
252     if (CRYPTO_mem_leaks(bio_err) <= 0)
253         ret = 1;
254 #endif
255     BIO_free(bio_err);
256     EXIT(ret);
257 }
258
259 const OPTIONS exit_options[] = {
260     {NULL}
261 };
262
263 static void list_cipher_fn(const EVP_CIPHER *c,
264                            const char *from, const char *to, void *arg)
265 {
266     if (c != NULL) {
267         BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
268     } else {
269         if (from == NULL)
270             from = "<undefined>";
271         if (to == NULL)
272             to = "<undefined>";
273         BIO_printf(arg, "%s => %s\n", from, to);
274     }
275 }
276
277 static void list_md_fn(const EVP_MD *m,
278                        const char *from, const char *to, void *arg)
279 {
280     if (m != NULL) {
281         BIO_printf(arg, "%s\n", EVP_MD_name(m));
282     } else {
283         if (from == NULL)
284             from = "<undefined>";
285         if (to == NULL)
286             to = "<undefined>";
287         BIO_printf((BIO *)arg, "%s => %s\n", from, to);
288     }
289 }
290
291 static void list_missing_help(void)
292 {
293     const FUNCTION *fp;
294     const OPTIONS *o;
295
296     for (fp = functions; fp->name != NULL; fp++) {
297         if ((o = fp->help) == NULL) {
298             BIO_printf(bio_out, "%s *\n", fp->name);
299             continue;
300         }
301         for ( ; o->name != NULL; o++) {
302             if (o->helpstr == NULL)
303                 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
304         }
305     }
306 }
307
308
309 /* Unified enum for help and list commands. */
310 typedef enum HELPLIST_CHOICE {
311     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
312     OPT_COMMANDS, OPT_DIGEST_COMMANDS,
313     OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
314     OPT_PK_ALGORITHMS, OPT_DISABLED, OPT_MISSING_HELP
315 } HELPLIST_CHOICE;
316
317 const OPTIONS list_options[] = {
318     {"help", OPT_HELP, '-', "Display this summary"},
319     {"commands", OPT_COMMANDS, '-', "List of standard commands"},
320     {"digest-commands", OPT_DIGEST_COMMANDS, '-',
321      "List of message digest commands"},
322     {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
323      "List of message digest algorithms"},
324     {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
325     {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
326      "List of cipher algorithms"},
327     {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
328      "List of public key algorithms"},
329     {"disabled", OPT_DISABLED, '-',
330      "List of disabled features"},
331     {"missing-help", OPT_MISSING_HELP, '-',
332      "List missing detailed help strings"},
333     {NULL}
334 };
335
336 int list_main(int argc, char **argv)
337 {
338     char *prog;
339     HELPLIST_CHOICE o;
340     int done = 0;
341
342     prog = opt_init(argc, argv, list_options);
343     while ((o = opt_next()) != OPT_EOF) {
344         switch (o) {
345         case OPT_EOF:  /* Never hit, but suppresses warning */
346         case OPT_ERR:
347             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
348             return 1;
349         case OPT_HELP:
350             opt_help(list_options);
351             break;
352         case OPT_COMMANDS:
353             list_type(FT_general);
354             break;
355         case OPT_DIGEST_COMMANDS:
356             list_type(FT_md);
357             break;
358         case OPT_DIGEST_ALGORITHMS:
359             EVP_MD_do_all_sorted(list_md_fn, bio_out);
360             break;
361         case OPT_CIPHER_COMMANDS:
362             list_type(FT_cipher);
363             break;
364         case OPT_CIPHER_ALGORITHMS:
365             EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
366             break;
367         case OPT_PK_ALGORITHMS:
368             list_pkey();
369             break;
370         case OPT_DISABLED:
371             list_disabled();
372             break;
373         case OPT_MISSING_HELP:
374             list_missing_help();
375             break;
376         }
377         done = 1;
378     }
379
380     if (!done) {
381         BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
382         return 1;
383     }
384
385     return 0;
386 }
387
388 typedef enum HELP_CHOICE {
389     OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
390 } HELP_CHOICE;
391
392 const OPTIONS help_options[] = {
393     {"help", OPT_hHELP, '-', "Display this summary"},
394     {NULL}
395 };
396
397
398 int help_main(int argc, char **argv)
399 {
400     FUNCTION *fp;
401     int i, nl;
402     FUNC_TYPE tp;
403     char *prog;
404     HELP_CHOICE o;
405
406     prog = opt_init(argc, argv, help_options);
407     while ((o = opt_next()) != OPT_hEOF) {
408         switch (o) {
409         case OPT_hERR:
410         case OPT_hEOF:
411             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
412             return 1;
413         case OPT_hHELP:
414             opt_help(help_options);
415             return 0;
416         }
417     }
418
419     if (opt_num_rest() != 0) {
420         BIO_printf(bio_err, "Usage: %s\n", prog);
421         return 1;
422     }
423
424     BIO_printf(bio_err, "\nStandard commands");
425     i = 0;
426     tp = FT_none;
427     for (fp = functions; fp->name != NULL; fp++) {
428         nl = 0;
429         if (((i++) % COLUMNS) == 0) {
430             BIO_printf(bio_err, "\n");
431             nl = 1;
432         }
433         if (fp->type != tp) {
434             tp = fp->type;
435             if (!nl)
436                 BIO_printf(bio_err, "\n");
437             if (tp == FT_md) {
438                 i = 1;
439                 BIO_printf(bio_err,
440                            "\nMessage Digest commands (see the `dgst' command for more details)\n");
441             } else if (tp == FT_cipher) {
442                 i = 1;
443                 BIO_printf(bio_err,
444                            "\nCipher commands (see the `enc' command for more details)\n");
445             }
446         }
447         BIO_printf(bio_err, FORMAT, fp->name);
448     }
449     BIO_printf(bio_err, "\n\n");
450     return 0;
451 }
452
453 int exit_main(int argc, char **argv)
454 {
455     return EXIT_THE_PROGRAM;
456 }
457
458 static void list_type(FUNC_TYPE ft)
459 {
460     FUNCTION *fp;
461     int i = 0;
462
463     for (fp = functions; fp->name != NULL; fp++)
464         if (fp->type == ft) {
465             if ((i++ % COLUMNS) == 0)
466                 BIO_printf(bio_out, "\n");
467             BIO_printf(bio_out, FORMAT, fp->name);
468         }
469     BIO_printf(bio_out, "\n");
470 }
471
472 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
473 {
474     FUNCTION f, *fp;
475
476     if (argc <= 0 || argv[0] == NULL)
477         return (0);
478     f.name = argv[0];
479     fp = lh_FUNCTION_retrieve(prog, &f);
480     if (fp == NULL) {
481         if (EVP_get_digestbyname(argv[0])) {
482             f.type = FT_md;
483             f.func = dgst_main;
484             fp = &f;
485         } else if (EVP_get_cipherbyname(argv[0])) {
486             f.type = FT_cipher;
487             f.func = enc_main;
488             fp = &f;
489         }
490     }
491     if (fp != NULL) {
492         return (fp->func(argc, argv));
493     }
494     if ((strncmp(argv[0], "no-", 3)) == 0) {
495         /*
496          * User is asking if foo is unsupported, by trying to "run" the
497          * no-foo command.  Strange.
498          */
499         f.name = argv[0] + 3;
500         if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
501             BIO_printf(bio_out, "%s\n", argv[0]);
502             return (0);
503         }
504         BIO_printf(bio_out, "%s\n", argv[0] + 3);
505         return 1;
506     }
507     if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
508         strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
509         /* Special value to mean "exit the program. */
510         return EXIT_THE_PROGRAM;
511
512     BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
513                argv[0]);
514     return (1);
515 }
516
517 static void list_pkey(void)
518 {
519     int i;
520
521     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
522         const EVP_PKEY_ASN1_METHOD *ameth;
523         int pkey_id, pkey_base_id, pkey_flags;
524         const char *pinfo, *pem_str;
525         ameth = EVP_PKEY_asn1_get0(i);
526         EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
527                                 &pinfo, &pem_str, ameth);
528         if (pkey_flags & ASN1_PKEY_ALIAS) {
529             BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
530             BIO_printf(bio_out, "\tAlias for: %s\n",
531                        OBJ_nid2ln(pkey_base_id));
532         } else {
533             BIO_printf(bio_out, "Name: %s\n", pinfo);
534             BIO_printf(bio_out, "\tType: %s Algorithm\n",
535                        pkey_flags & ASN1_PKEY_DYNAMIC ?
536                        "External" : "Builtin");
537             BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
538             if (pem_str == NULL)
539                 pem_str = "(none)";
540             BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
541         }
542
543     }
544 }
545
546 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
547 {
548     return strncmp(a->name, b->name, 8);
549 }
550
551 static unsigned long function_hash(const FUNCTION * a)
552 {
553     return OPENSSL_LH_strhash(a->name);
554 }
555
556 static int SortFnByName(const void *_f1, const void *_f2)
557 {
558     const FUNCTION *f1 = _f1;
559     const FUNCTION *f2 = _f2;
560
561     if (f1->type != f2->type)
562         return f1->type - f2->type;
563     return strcmp(f1->name, f2->name);
564 }
565
566 static void list_disabled(void)
567 {
568     BIO_puts(bio_out, "Disabled algorithms:\n");
569 #ifdef OPENSSL_NO_ARIA
570     BIO_puts(bio_out, "ARIA\n");
571 #endif
572 #ifdef OPENSSL_NO_BF
573     BIO_puts(bio_out, "BF\n");
574 #endif
575 #ifdef OPENSSL_NO_BLAKE2
576     BIO_puts(bio_out, "BLAKE2\n");
577 #endif
578 #ifdef OPENSSL_NO_CAMELLIA
579     BIO_puts(bio_out, "CAMELLIA\n");
580 #endif
581 #ifdef OPENSSL_NO_CAST
582     BIO_puts(bio_out, "CAST\n");
583 #endif
584 #ifdef OPENSSL_NO_CMAC
585     BIO_puts(bio_out, "CMAC\n");
586 #endif
587 #ifdef OPENSSL_NO_CMS
588     BIO_puts(bio_out, "CMS\n");
589 #endif
590 #ifdef OPENSSL_NO_COMP
591     BIO_puts(bio_out, "COMP\n");
592 #endif
593 #ifdef OPENSSL_NO_DES
594     BIO_puts(bio_out, "DES\n");
595 #endif
596 #ifdef OPENSSL_NO_DGRAM
597     BIO_puts(bio_out, "DGRAM\n");
598 #endif
599 #ifdef OPENSSL_NO_DH
600     BIO_puts(bio_out, "DH\n");
601 #endif
602 #ifdef OPENSSL_NO_DSA
603     BIO_puts(bio_out, "DSA\n");
604 #endif
605 #if defined(OPENSSL_NO_DTLS)
606     BIO_puts(bio_out, "DTLS\n");
607 #endif
608 #if defined(OPENSSL_NO_DTLS1)
609     BIO_puts(bio_out, "DTLS1\n");
610 #endif
611 #if defined(OPENSSL_NO_DTLS1_2)
612     BIO_puts(bio_out, "DTLS1_2\n");
613 #endif
614 #ifdef OPENSSL_NO_EC
615     BIO_puts(bio_out, "EC\n");
616 #endif
617 #ifdef OPENSSL_NO_EC2M
618     BIO_puts(bio_out, "EC2M\n");
619 #endif
620 #ifdef OPENSSL_NO_ENGINE
621     BIO_puts(bio_out, "ENGINE\n");
622 #endif
623 #ifdef OPENSSL_NO_GOST
624     BIO_puts(bio_out, "GOST\n");
625 #endif
626 #ifdef OPENSSL_NO_HEARTBEATS
627     BIO_puts(bio_out, "HEARTBEATS\n");
628 #endif
629 #ifdef OPENSSL_NO_IDEA
630     BIO_puts(bio_out, "IDEA\n");
631 #endif
632 #ifdef OPENSSL_NO_MD2
633     BIO_puts(bio_out, "MD2\n");
634 #endif
635 #ifdef OPENSSL_NO_MD4
636     BIO_puts(bio_out, "MD4\n");
637 #endif
638 #ifdef OPENSSL_NO_MD5
639     BIO_puts(bio_out, "MD5\n");
640 #endif
641 #ifdef OPENSSL_NO_MDC2
642     BIO_puts(bio_out, "MDC2\n");
643 #endif
644 #ifdef OPENSSL_NO_OCB
645     BIO_puts(bio_out, "OCB\n");
646 #endif
647 #ifdef OPENSSL_NO_OCSP
648     BIO_puts(bio_out, "OCSP\n");
649 #endif
650 #ifdef OPENSSL_NO_PSK
651     BIO_puts(bio_out, "PSK\n");
652 #endif
653 #ifdef OPENSSL_NO_RC2
654     BIO_puts(bio_out, "RC2\n");
655 #endif
656 #ifdef OPENSSL_NO_RC4
657     BIO_puts(bio_out, "RC4\n");
658 #endif
659 #ifdef OPENSSL_NO_RC5
660     BIO_puts(bio_out, "RC5\n");
661 #endif
662 #ifdef OPENSSL_NO_RMD160
663     BIO_puts(bio_out, "RMD160\n");
664 #endif
665 #ifdef OPENSSL_NO_RSA
666     BIO_puts(bio_out, "RSA\n");
667 #endif
668 #ifdef OPENSSL_NO_SCRYPT
669     BIO_puts(bio_out, "SCRYPT\n");
670 #endif
671 #ifdef OPENSSL_NO_SCTP
672     BIO_puts(bio_out, "SCTP\n");
673 #endif
674 #ifdef OPENSSL_NO_SEED
675     BIO_puts(bio_out, "SEED\n");
676 #endif
677 #ifdef OPENSSL_NO_SOCK
678     BIO_puts(bio_out, "SOCK\n");
679 #endif
680 #ifdef OPENSSL_NO_SRP
681     BIO_puts(bio_out, "SRP\n");
682 #endif
683 #ifdef OPENSSL_NO_SRTP
684     BIO_puts(bio_out, "SRTP\n");
685 #endif
686 #ifdef OPENSSL_NO_SSL3
687     BIO_puts(bio_out, "SSL3\n");
688 #endif
689 #ifdef OPENSSL_NO_TLS1
690     BIO_puts(bio_out, "TLS1\n");
691 #endif
692 #ifdef OPENSSL_NO_TLS1_1
693     BIO_puts(bio_out, "TLS1_1\n");
694 #endif
695 #ifdef OPENSSL_NO_TLS1_2
696     BIO_puts(bio_out, "TLS1_2\n");
697 #endif
698 #ifdef OPENSSL_NO_WHIRLPOOL
699     BIO_puts(bio_out, "WHIRLPOOL\n");
700 #endif
701 #ifndef ZLIB
702     BIO_puts(bio_out, "ZLIB\n");
703 #endif
704 }
705
706 static LHASH_OF(FUNCTION) *prog_init(void)
707 {
708     LHASH_OF(FUNCTION) *ret;
709     FUNCTION *f;
710     size_t i;
711
712     /* Sort alphabetically within category. For nicer help displays. */
713     for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
714     qsort(functions, i, sizeof(*functions), SortFnByName);
715
716     if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
717         return (NULL);
718
719     for (f = functions; f->name != NULL; f++)
720         (void)lh_FUNCTION_insert(ret, f);
721     return (ret);
722 }