|
| 1 | +#include "internal/command_line_parser.h" |
| 2 | +#include "internal/global_timeline.h" |
| 3 | + |
| 4 | +#include <errno.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <string.h> |
| 8 | + |
| 9 | +typedef HT_ErrorCode(*HT_CommandLineArgumentParser)(int, char**, int); |
| 10 | + |
| 11 | +typedef struct |
| 12 | +{ |
| 13 | + const char* argument; |
| 14 | + const char* help; |
| 15 | + HT_CommandLineArgumentParser parser; |
| 16 | + HT_Boolean is_flag; |
| 17 | +} HT_CommandLineArgument; |
| 18 | + |
| 19 | +static HT_ErrorCode print_help(int argc, char** argv, int pos); |
| 20 | +static HT_ErrorCode set_global_timeline_buffer_size(int argc, char** argv, int pos); |
| 21 | + |
| 22 | +HT_CommandLineArgument arguments[] = { |
| 23 | + { |
| 24 | + "--ht-global-timeline-buffer-size", |
| 25 | + "Set Global Timeline buffer size", |
| 26 | + set_global_timeline_buffer_size, |
| 27 | + HT_FALSE |
| 28 | + }, |
| 29 | + { |
| 30 | + "--ht-help", "Print this help and exits the process", |
| 31 | + print_help, |
| 32 | + HT_TRUE |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +static HT_ErrorCode |
| 37 | +set_global_timeline_buffer_size(int argc, char** argv, int pos) |
| 38 | +{ |
| 39 | + if (pos + 1 >= argc) |
| 40 | + { |
| 41 | + return HT_ERR_MISSING_ARGUMENT; |
| 42 | + } |
| 43 | + |
| 44 | + const char* s = argv[pos + 1]; |
| 45 | + char* end; |
| 46 | + errno = 0; |
| 47 | + unsigned long value = strtoul(s, &end, 10); |
| 48 | + |
| 49 | + if (errno == ERANGE || |
| 50 | + (sizeof(size_t) < sizeof(unsigned long) && (unsigned long)SIZE_MAX < value)) |
| 51 | + { |
| 52 | + return HT_ERR_OUT_OF_RANGE; |
| 53 | + } |
| 54 | + |
| 55 | + if (end - s == 0) |
| 56 | + { |
| 57 | + return HT_ERR_INVALID_FORMAT; |
| 58 | + } |
| 59 | + |
| 60 | + ht_global_timeline_set_buffer_size((size_t) value); |
| 61 | + return HT_ERR_OK; |
| 62 | +} |
| 63 | + |
| 64 | +static HT_ErrorCode |
| 65 | +print_help(int argc, char** argv, int pos) |
| 66 | +{ |
| 67 | + HT_UNUSED(argc); |
| 68 | + HT_UNUSED(argv); |
| 69 | + HT_UNUSED(pos); |
| 70 | + |
| 71 | + printf("HawkTracer options:\n"); |
| 72 | + |
| 73 | + for (unsigned int x = 0; x < sizeof(arguments) / sizeof(arguments[0]); x++) |
| 74 | + { |
| 75 | + printf(" %s %s %s\n", |
| 76 | + arguments[x].argument, |
| 77 | + arguments[x].is_flag ? " " : "VALUE", |
| 78 | + arguments[x].help); |
| 79 | + } |
| 80 | + |
| 81 | + exit(0); |
| 82 | + |
| 83 | + return HT_ERR_OK; |
| 84 | +} |
| 85 | + |
| 86 | +void |
| 87 | +ht_command_line_parse_args(int argc, char** argv) |
| 88 | +{ |
| 89 | + for (int i = 1; i < argc; i++) |
| 90 | + { |
| 91 | + for (unsigned int x = 0; x < sizeof(arguments) / sizeof(arguments[0]); x++) |
| 92 | + { |
| 93 | + const char* argname = arguments[x].argument; |
| 94 | + if (strcmp(argname, argv[i]) != 0) |
| 95 | + { |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + HT_ErrorCode error = arguments[x].parser(argc, argv, i); |
| 100 | + |
| 101 | + if (error != HT_ERR_OK) |
| 102 | + { |
| 103 | + printf("Failed to process argument %s. Error code: %d\n", |
| 104 | + argname, error); |
| 105 | + } |
| 106 | + |
| 107 | + if (arguments[x].is_flag == HT_FALSE) |
| 108 | + { |
| 109 | + i++; |
| 110 | + } |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments