-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_options.c.template
119 lines (102 loc) · 3.29 KB
/
process_options.c.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// -*- mode:c -*-
/*
* Use this Bash builtin template for more control over the
* options than afforded by _internal_getopts_. This version
* does not automatically handle number options, but it would
* not be hard to add that feature.
*
*/
#include <builtins.h>
// Prevent multiple inclusion of shell.h:
#ifndef EXECUTION_FAILURE
#include <shell.h>
#endif
#include <builtins/bashgetopt.h> // for internal_getopt(), etc
#include <builtins/common.h> // for no_options()
#include <stdio.h>
static int <FNAME>(WORD_LIST *list)
{
int retval = EXECUTION_SUCCESS;
// String pointers that can be set with options.
// Set to default values where appropriate.
const char *name_handle = NULL;
const char *name_action = NULL;
const char *name_result_value = "ATE_VALUE";
const char *name_result_array = "ATE_ARRAY";
int flag_verbose = 0;
// Option parsing status variables
const char *cur_arg;
const char **pending_option = NULL;
WORD_LIST *ptr = list;
while (ptr)
{
cur_arg = ptr->word->word;
if (pending_option)
{
*pending_option = cur_arg;
pending_option = NULL;
}
else if (*cur_arg == '-' && cur_arg[1])
{
switch(cur_arg[1])
{
case 'a':
// Immediate resolution when no space between option and value:
if (cur_arg[2])
name_result_array = &cur_arg[2];
// Defer getting option value until next loop:
else
pending_option = &name_result_array;
break;
case 'v':
if (cur_arg[2])
name_result_value = &cur_arg[2];
else
pending_option = &name_result_value;
break;
case 'V':
flag_verbose = 1;
break;
}
}
// Positional options
else if (name_handle == NULL)
name_handle = cur_arg;
else if (name_action == NULL)
name_action = cur_arg;
ptr = ptr->next;
}
if (name_handle == NULL || name_action == NULL)
{
fprintf(stderr, "Handle and action values are required.\n");
builtin_usage();
retval = EX_BADUSAGE;
goto exit_for_error;
}
printf("Variable name_handle is set to '%s'\n", name_handle);
printf("Variable name_action is set to '%s'\n", name_action);
printf("Variable name_result_value is set to '%s'\n", name_result_value);
printf("Variable name_result_array is set to '%s'\n", name_result_array);
exit_for_error:
return retval;
}
static char *desc_ate[] = {
"<FNAME> is a cool Bash builtin",
"",
"Options:",
" -a array Use _array_ as the name of array to be used for",
" returning multi-value results when appropriate",
" for the action type.",
" -v value Use _value_ as the name of the shell variable to",
" be used for return single value results when",
" appropriate for the action type.",
(char*)NULL // end of array marker
};
struct builtin ate_struct = {
.name = "<FNAME>",
.function = <FNAME>,
.flags = BUILTIN_ENABLED,
.long_doc = desc_<FNAME>,
.short_doc = "<FNAME> [-a array_name] [-v value_name] handle_name action_name [...]",
.handle = 0
};