[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup net_app
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * Generic application parsing functions implementation.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdio.h>
|
---|
[19f857a] | 38 | #include <str.h>
|
---|
[21580dd] | 39 |
|
---|
[849ed54] | 40 | #include <socket.h>
|
---|
| 41 | #include <net_err.h>
|
---|
[21580dd] | 42 |
|
---|
| 43 | #include "parse.h"
|
---|
| 44 |
|
---|
[3be62bc] | 45 | int parse_address_family(const char * name){
|
---|
| 46 | if(str_lcmp(name, "AF_INET", 7) == 0){
|
---|
| 47 | return AF_INET;
|
---|
| 48 | }else if(str_lcmp(name, "AF_INET6", 8) == 0){
|
---|
| 49 | return AF_INET6;
|
---|
| 50 | }
|
---|
| 51 | return EAFNOSUPPORT;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[aadf01e] | 54 | int parse_parameter_int(int argc, char ** argv, int * index, int * value, const char * name, int offset){
|
---|
| 55 | char * rest;
|
---|
[21580dd] | 56 |
|
---|
[aadf01e] | 57 | if(offset){
|
---|
| 58 | *value = strtol(argv[*index] + offset, &rest, 10);
|
---|
| 59 | }else if((*index) + 1 < argc){
|
---|
| 60 | ++ (*index);
|
---|
| 61 | *value = strtol(argv[*index], &rest, 10);
|
---|
[21580dd] | 62 | }else{
|
---|
[aadf01e] | 63 | fprintf(stderr, "Command line error: missing %s\n", name);
|
---|
[21580dd] | 64 | return EINVAL;
|
---|
| 65 | }
|
---|
[aadf01e] | 66 | if(rest && (*rest)){
|
---|
| 67 | fprintf(stderr, "Command line error: %s unrecognized (%d: %s)\n", name, * index, argv[*index]);
|
---|
[21580dd] | 68 | return EINVAL;
|
---|
| 69 | }
|
---|
| 70 | return EOK;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[3be62bc] | 73 | int parse_parameter_name_int(int argc, char ** argv, int * index, int * value, const char * name, int offset, int (*parse_value)(const char * value)){
|
---|
| 74 | ERROR_DECLARE;
|
---|
| 75 |
|
---|
| 76 | char * parameter;
|
---|
| 77 |
|
---|
| 78 | ERROR_PROPAGATE(parse_parameter_string(argc, argv, index, ¶meter, name, offset));
|
---|
| 79 | *value = (*parse_value)(parameter);
|
---|
| 80 | if((*value) == ENOENT){
|
---|
| 81 | fprintf(stderr, "Command line error: unrecognized %s value (%d: %s)\n", name, * index, parameter);
|
---|
| 82 | return ENOENT;
|
---|
| 83 | }
|
---|
| 84 | return EOK;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[aadf01e] | 87 | int parse_parameter_string(int argc, char ** argv, int * index, char ** value, const char * name, int offset){
|
---|
| 88 | if(offset){
|
---|
| 89 | *value = argv[*index] + offset;
|
---|
| 90 | }else if((*index) + 1 < argc){
|
---|
| 91 | ++ (*index);
|
---|
| 92 | *value = argv[*index];
|
---|
[21580dd] | 93 | }else{
|
---|
[aadf01e] | 94 | fprintf(stderr, "Command line error: missing %s\n", name);
|
---|
[21580dd] | 95 | return EINVAL;
|
---|
| 96 | }
|
---|
| 97 | return EOK;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[3be62bc] | 100 | int parse_protocol_family(const char * name){
|
---|
| 101 | if(str_lcmp(name, "PF_INET", 7) == 0){
|
---|
| 102 | return PF_INET;
|
---|
| 103 | }else if(str_lcmp(name, "PF_INET6", 8) == 0){
|
---|
| 104 | return PF_INET6;
|
---|
| 105 | }
|
---|
| 106 | return EPFNOSUPPORT;
|
---|
| 107 | }
|
---|
[21580dd] | 108 |
|
---|
[3be62bc] | 109 | int parse_socket_type(const char * name){
|
---|
| 110 | if(str_lcmp(name, "SOCK_DGRAM", 11) == 0){
|
---|
| 111 | return SOCK_DGRAM;
|
---|
| 112 | }else if(str_lcmp(name, "SOCK_STREAM", 12) == 0){
|
---|
| 113 | return SOCK_STREAM;
|
---|
[21580dd] | 114 | }
|
---|
[3be62bc] | 115 | return ESOCKTNOSUPPORT;
|
---|
[21580dd] | 116 | }
|
---|
| 117 |
|
---|
[aadf01e] | 118 | void print_unrecognized(int index, const char * parameter){
|
---|
[3be62bc] | 119 | fprintf(stderr, "Command line error: unrecognized argument (%d: %s)\n", index, parameter);
|
---|
[21580dd] | 120 | }
|
---|
| 121 |
|
---|
| 122 | /** @}
|
---|
| 123 | */
|
---|