|
| 1 | +/* $Id$ */ |
| 2 | +/* Copyright (c) 2025 Pierre Pronchery <[email protected]> */ |
| 3 | +/* This file is part of DeforaOS System libApp */ |
| 4 | +/* This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, version 3 of the License. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +#include <unistd.h> |
| 19 | +#include <dirent.h> |
| 20 | +#include <stdio.h> |
| 21 | +#include <string.h> |
| 22 | +#include <System/error.h> |
| 23 | +#include <System/plugin.h> |
| 24 | +#include "App/apptransport.h" |
| 25 | +#include "../config.h" |
| 26 | + |
| 27 | +#ifndef PROGNAME_APPTRANSPORT |
| 28 | +# define PROGNAME_APPTRANSPORT "AppTransport" |
| 29 | +#endif |
| 30 | + |
| 31 | + |
| 32 | +/* AppTransport */ |
| 33 | +/* private */ |
| 34 | +/* prototypes */ |
| 35 | +static int _apptransport_list(void); |
| 36 | + |
| 37 | +static int _error(char const * message, int ret); |
| 38 | +static int _usage(void); |
| 39 | + |
| 40 | + |
| 41 | +/* functions */ |
| 42 | +/* apptransport_list */ |
| 43 | +static int _apptransport_list(void) |
| 44 | +{ |
| 45 | +#if defined(__APPLE__) |
| 46 | + char const soext[] = ".dylib"; |
| 47 | +#elif defined(__WIN32__) |
| 48 | + char const soext[] = ".dll"; |
| 49 | +#else |
| 50 | + char const soext[] = ".so"; |
| 51 | +#endif |
| 52 | + DIR * dir; |
| 53 | + struct dirent * de; |
| 54 | + size_t len; |
| 55 | + String * path; |
| 56 | + Plugin * plugin; |
| 57 | + AppTransportPluginDefinition * transport; |
| 58 | + |
| 59 | + if((dir = opendir(APP_TRANSPORT_PATH)) == NULL) |
| 60 | + return _error(APP_TRANSPORT_PATH, -1); |
| 61 | + while((de = readdir(dir)) != NULL) |
| 62 | + { |
| 63 | + if((len = strlen(de->d_name)) < sizeof(soext)) |
| 64 | + continue; |
| 65 | + if(strcmp(&de->d_name[len - sizeof(soext) + 1], soext) != 0) |
| 66 | + continue; |
| 67 | + if((path = string_new_append(APP_TRANSPORT_PATH "/", de->d_name, |
| 68 | + NULL)) == NULL) |
| 69 | + { |
| 70 | + _error(PROGNAME_APPTRANSPORT, -1); |
| 71 | + continue; |
| 72 | + } |
| 73 | + plugin = plugin_new_path(path); |
| 74 | + string_delete(path); |
| 75 | + if(plugin == NULL) |
| 76 | + { |
| 77 | + _error(PROGNAME_APPTRANSPORT, -1); |
| 78 | + continue; |
| 79 | + } |
| 80 | + if((transport = plugin_lookup(plugin, "transport")) == NULL) |
| 81 | + { |
| 82 | + _error(PROGNAME_APPTRANSPORT, -1); |
| 83 | + plugin_delete(plugin); |
| 84 | + continue; |
| 85 | + } |
| 86 | + de->d_name[len - sizeof(soext) + 1] = '\0'; |
| 87 | + printf("%s (%s): %s\n", de->d_name, transport->name, |
| 88 | + transport->description); |
| 89 | + plugin_delete(plugin); |
| 90 | + } |
| 91 | + closedir(dir); |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +/* error */ |
| 97 | +static int _error(char const * message, int ret) |
| 98 | +{ |
| 99 | + error_print(message); |
| 100 | + return ret; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +/* usage */ |
| 105 | +static int _usage(void) |
| 106 | +{ |
| 107 | + fputs("Usage: " PROGNAME_APPTRANSPORT " -l\n", stderr); |
| 108 | + return 1; |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +/* public */ |
| 113 | +/* functions */ |
| 114 | +/* main */ |
| 115 | +int main(int argc, char * argv[]) |
| 116 | +{ |
| 117 | + int o; |
| 118 | + int list = 0; |
| 119 | + |
| 120 | + while((o = getopt(argc, argv, "l")) != -1) |
| 121 | + switch(o) |
| 122 | + { |
| 123 | + case 'l': |
| 124 | + list = 1; |
| 125 | + break; |
| 126 | + default: |
| 127 | + return _usage(); |
| 128 | + } |
| 129 | + if(optind != argc || list == 0) |
| 130 | + return _usage(); |
| 131 | + return (_apptransport_list() == 0) ? 0 : 2; |
| 132 | +} |
0 commit comments