[917a78b] | 1 | /* |
---|
[8bd38d6] | 2 | * Copyright (c) 2013, 2018 embedded brains GmbH. All rights reserved. |
---|
[917a78b] | 3 | * |
---|
| 4 | * embedded brains GmbH |
---|
| 5 | * Dornierstr. 4 |
---|
| 6 | * 82178 Puchheim |
---|
| 7 | * Germany |
---|
| 8 | * <rtems@embedded-brains.de> |
---|
| 9 | * |
---|
| 10 | * Redistribution and use in source and binary forms, with or without |
---|
| 11 | * modification, are permitted provided that the following conditions |
---|
| 12 | * are met: |
---|
| 13 | * 1. Redistributions of source code must retain the above copyright |
---|
| 14 | * notice, this list of conditions and the following disclaimer. |
---|
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 16 | * notice, this list of conditions and the following disclaimer in the |
---|
| 17 | * documentation and/or other materials provided with the distribution. |
---|
| 18 | * |
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 29 | * SUCH DAMAGE. |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | #include <rtems/netcmds-config.h> |
---|
[8bd38d6] | 33 | |
---|
| 34 | #include <rtems.h> |
---|
| 35 | #include <rtems/dhcpcd.h> |
---|
| 36 | |
---|
| 37 | #include <stdio.h> |
---|
| 38 | #include <stdlib.h> |
---|
| 39 | #include <string.h> |
---|
| 40 | |
---|
| 41 | typedef struct { |
---|
| 42 | rtems_dhcpcd_config config; |
---|
| 43 | char *argv[RTEMS_ZERO_LENGTH_ARRAY]; |
---|
| 44 | } dhcpcd_command_config; |
---|
| 45 | |
---|
| 46 | static void |
---|
| 47 | dhcpcd_command_destroy_config(const rtems_dhcpcd_config *config, int exit_code) |
---|
| 48 | { |
---|
| 49 | char **argv; |
---|
| 50 | |
---|
| 51 | (void)exit_code; |
---|
| 52 | |
---|
| 53 | argv = config->argv; |
---|
| 54 | while (*argv != NULL) { |
---|
| 55 | free(*argv); |
---|
| 56 | ++argv; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | free(RTEMS_DECONST(rtems_dhcpcd_config *, config)); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | static int |
---|
| 63 | dhcpcd_command(int argc, char **argv) |
---|
| 64 | { |
---|
| 65 | dhcpcd_command_config *config; |
---|
| 66 | rtems_status_code sc; |
---|
| 67 | int i; |
---|
| 68 | |
---|
| 69 | config = calloc(1, sizeof(*config) + (argc + 1) * sizeof(char *)); |
---|
| 70 | if (config == NULL) { |
---|
| 71 | fprintf(stdout, "dhcpcd error: not enough memory\n"); |
---|
| 72 | return 1; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | for (i = 0; i < argc; ++i) { |
---|
| 76 | config->argv[i] = strdup(argv[i]); |
---|
| 77 | if (config->argv[i] == NULL) { |
---|
| 78 | dhcpcd_command_destroy_config(&config->config, 0); |
---|
| 79 | fprintf(stdout, "dhcpcd error: not enough memory\n"); |
---|
| 80 | return 1; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | config->config.argc = argc; |
---|
| 85 | config->config.argv = &config->argv[0]; |
---|
| 86 | config->config.destroy = dhcpcd_command_destroy_config; |
---|
| 87 | |
---|
| 88 | sc = rtems_dhcpcd_start(&config->config); |
---|
| 89 | if (sc != RTEMS_SUCCESSFUL) { |
---|
| 90 | dhcpcd_command_destroy_config(&config->config, 0); |
---|
| 91 | fprintf(stdout, "dhcpcd start failed: %s\n", rtems_status_text(sc)); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | return 0; |
---|
| 95 | } |
---|
[917a78b] | 96 | |
---|
| 97 | rtems_shell_cmd_t rtems_shell_DHCPCD_Command = { |
---|
| 98 | .name = "dhcpcd", |
---|
| 99 | .usage = "dhcpcd [args]", |
---|
| 100 | .topic = "net", |
---|
[8bd38d6] | 101 | .command = dhcpcd_command |
---|
[917a78b] | 102 | }; |
---|