xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al. |
| 9 | * |
| 10 | * This software is licensed as described in the file COPYING, which |
| 11 | * you should have received as part of this distribution. The terms |
| 12 | * are also available at https://curl.se/docs/copyright.html. |
| 13 | * |
| 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 | * copies of the Software, and permit persons to whom the Software is |
| 16 | * furnished to do so, under the terms of the COPYING file. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | * SPDX-License-Identifier: curl |
| 22 | * |
| 23 | ***************************************************************************/ |
| 24 | #include "tool_setup.h" |
| 25 | |
| 26 | #include "strcase.h" |
| 27 | |
| 28 | #define ENABLE_CURLX_PRINTF |
| 29 | /* use our own printf() functions */ |
| 30 | #include "curlx.h" |
| 31 | |
| 32 | #include "tool_libinfo.h" |
| 33 | |
| 34 | #include "memdebug.h" /* keep this as LAST include */ |
| 35 | |
| 36 | /* global variable definitions, for libcurl run-time info */ |
| 37 | |
| 38 | static const char *no_protos = NULL; |
| 39 | |
| 40 | curl_version_info_data *curlinfo = NULL; |
| 41 | const char * const *built_in_protos = &no_protos; |
| 42 | |
| 43 | size_t proto_count = 0; |
| 44 | |
| 45 | const char *proto_file = NULL; |
| 46 | const char *proto_ftp = NULL; |
| 47 | const char *proto_ftps = NULL; |
| 48 | const char *proto_http = NULL; |
| 49 | const char *proto_https = NULL; |
| 50 | const char *proto_rtsp = NULL; |
| 51 | const char *proto_scp = NULL; |
| 52 | const char *proto_sftp = NULL; |
| 53 | const char *proto_tftp = NULL; |
| 54 | |
| 55 | static struct proto_name_tokenp { |
| 56 | const char *proto_name; |
| 57 | const char **proto_tokenp; |
| 58 | } const possibly_built_in[] = { |
| 59 | { "file", &proto_file }, |
| 60 | { "ftp", &proto_ftp }, |
| 61 | { "ftps", &proto_ftps }, |
| 62 | { "http", &proto_http }, |
| 63 | { "https", &proto_https }, |
| 64 | { "rtsp", &proto_rtsp }, |
| 65 | { "scp", &proto_scp }, |
| 66 | { "sftp", &proto_sftp }, |
| 67 | { "tftp", &proto_tftp }, |
| 68 | { NULL, NULL } |
| 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * libcurl_info_init: retrieves run-time information about libcurl, |
| 73 | * setting a global pointer 'curlinfo' to libcurl's run-time info |
| 74 | * struct, count protocols and flag those we are interested in. |
| 75 | */ |
| 76 | |
| 77 | CURLcode get_libcurl_info(void) |
| 78 | { |
| 79 | CURLcode result = CURLE_OK; |
| 80 | |
| 81 | /* Pointer to libcurl's run-time version information */ |
| 82 | curlinfo = curl_version_info(CURLVERSION_NOW); |
| 83 | if(!curlinfo) |
| 84 | return CURLE_FAILED_INIT; |
| 85 | |
| 86 | if(curlinfo->protocols) { |
| 87 | const char *const *builtin; |
| 88 | const struct proto_name_tokenp *p; |
| 89 | |
| 90 | built_in_protos = curlinfo->protocols; |
| 91 | |
| 92 | for(builtin = built_in_protos; !result && *builtin; builtin++) { |
| 93 | /* Identify protocols we are interested in. */ |
| 94 | for(p = possibly_built_in; p->proto_name; p++) |
| 95 | if(curl_strequal(p->proto_name, *builtin)) { |
| 96 | *p->proto_tokenp = *builtin; |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | proto_count = builtin - built_in_protos; |
| 101 | } |
| 102 | |
| 103 | return CURLE_OK; |
| 104 | } |
| 105 | |
| 106 | /* Tokenize a protocol name. |
| 107 | * Return the address of the protocol name listed by the library, or NULL if |
| 108 | * not found. |
| 109 | * Although this may seem useless, this always returns the same address for |
| 110 | * a given protocol and thus allows comparing pointers rather than strings. |
| 111 | * In addition, the returned pointer is not deallocated until the program ends. |
| 112 | */ |
| 113 | |
| 114 | const char *proto_token(const char *proto) |
| 115 | { |
| 116 | const char * const *builtin; |
| 117 | |
| 118 | if(!proto) |
| 119 | return NULL; |
| 120 | for(builtin = built_in_protos; *builtin; builtin++) |
| 121 | if(curl_strequal(*builtin, proto)) |
| 122 | break; |
| 123 | return *builtin; |
| 124 | } |