Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Pfeiffer, Götz
MultiCAN
Commits
1a3ebe70
Commit
1a3ebe70
authored
Feb 21, 2022
by
Pfeiffer, Götz
Browse files
Module tool: The formatting of arg.c was changed a little.
parent
d65bd189
Changes
1
Hide whitespace changes
Inline
Side-by-side
MultiCAN/src/tool/src/arg.c
View file @
1a3ebe70
...
...
@@ -6,7 +6,7 @@
Copyright 2022 Helmholtz-Zentrum Berlin für Materialien und Energie GmbH
<https://www.helmholtz-berlin.de>
Author: Goetz Pfeiffer <Goetz.Pfeiffer@helmholtz-berlin.de>
Author: Goetz Pfeiffer <Goetz.Pfeiffer@helmholtz-berlin.de>
This file is part of tool, the tool library which is part of MultiCAN.
...
...
@@ -25,7 +25,7 @@ along with MultiCAN. If not, see <https://www.gnu.org/licenses/>.
*/
/*
summary:
summary:
This module implements functions and datastructures to handle command line
arguments.
*/
...
...
@@ -228,7 +228,8 @@ constants can be or'ed (linked with '|') to combine several flags in
this field. See also <ar_opt_options> for further explanation of this
field.",6) @IT */
typedef
struct
{
int
flags
;
{
int
flags
;
char
opt_char
;
char
*
opt_string
;
int
return_val
;
...
...
@@ -240,7 +241,8 @@ all options, a programs can recognize in one variable, usually defined
in main(). Example how to define a variable of this type:
> static ar_option options[]=
> { { AR_CASE | AR_COMMAND , 'c', \"copy\", COPY },
> {
> { AR_CASE | AR_COMMAND , 'c', \"copy\", COPY },
> { AR_COMMAND , 'r', \"read\", READ },
> { AR_CASE | AR_SWITCH , 'f', \"full\", FULL },
> };
...
...
@@ -255,7 +257,8 @@ several constants, e.g, AR_CASE and AR_COMMAND are combined in the
<flag>-fields of each element of the array (each element of the type
<ar_option>.",4) @IT */
typedef
struct
{
ar_option
*
option_array
;
{
ar_option
*
option_array
;
int
no_of_options
;
}
ar_options
;
...
...
@@ -308,7 +311,8 @@ this structure, so you may skip the next part of explanations:
- <options>: A pointer to the <ar_options> structure, that shall be used.",
12) @IT*/
typedef
struct
{
int
no_of_args
;
/* includes not the filename !! */
{
int
no_of_args
;
/* includes not the filename !! */
int
current_index
;
char
*
current_arg
;
char
*
curr_char
;
/* char in 'current' - string */
...
...
@@ -358,7 +362,8 @@ explanation of <ar_options>.
options need a leading '-' and all long options need a leading '+' or
\"--\" to be recognized as options.", 4) @IT */
enum
ar_opt_options
{
AR_DEFAULT
=
0
,
AR_CASE
=
1
,
AR_SWITCH
=
2
,
AR_DEFAULT_TRUE
=
4
,
{
AR_DEFAULT
=
0
,
AR_CASE
=
1
,
AR_SWITCH
=
2
,
AR_DEFAULT_TRUE
=
4
,
AR_COMMAND
=
8
};
/* !AR_CASE: case insensitive,
...
...
@@ -397,7 +402,8 @@ They may be or'ed together in this field.
- <AR_GENERAL_ERROR>: An internal error. Shouldn't happen at normal
circumstances.", 6) @IT */
enum
ar_returns
{
AR_NO_MORE
=-
1
,
AR_NO_OPTION
=-
2
,
AR_UNKNOWN_OPTION
=-
3
,
{
AR_NO_MORE
=-
1
,
AR_NO_OPTION
=-
2
,
AR_UNKNOWN_OPTION
=-
3
,
AR_MISPLACED_SWITCH
=-
4
,
AR_IS_OPTION
=-
5
,
AR_IS_COMMAND
=
-
7
,
AR_UNKNOWN_COMMAND
=
-
8
,
AR_GENERAL_ERROR
=-
9
...
...
@@ -429,10 +435,12 @@ enum ar_returns
/* there seems to be no my_stricmp() on standard unix ??? */
static
int
my_stricmp
(
char
*
s1
,
char
*
s2
)
{
char
c1
,
c2
;
{
char
c1
,
c2
;
for
(;
(
*
s1
!=
0
)
||
(
*
s2
!=
0
);
s1
++
,
s2
++
)
{
c1
=
toupper
(
*
UCHAR_P
(
s1
));
c2
=
toupper
(
*
UCHAR_P
(
s2
));
{
c1
=
toupper
(
*
UCHAR_P
(
s1
));
c2
=
toupper
(
*
UCHAR_P
(
s2
));
if
(
c1
<
c2
)
return
(
-
1
);
if
(
c1
>
c2
)
...
...
@@ -455,14 +463,16 @@ static ar_option *ch_search_option(ar_options *opts,
char
*
switch_found
)
/* search for an option */
/* is_command: search for a command (no leading '-' or '+') */
{
ar_option
*
opt
;
{
ar_option
*
opt
;
char
ch
;
int
i
;
/* do char-compare */
ch
=
*
str
;
switch
(
str
[
1
])
{
case
'+'
:
{
case
'+'
:
case
'-'
:
*
switch_found
=
str
[
1
];
break
;
default
:
*
switch_found
=
0
;
...
...
@@ -470,17 +480,22 @@ static ar_option *ch_search_option(ar_options *opts,
};
opt
=
opts
->
option_array
;
for
(
i
=
1
;
i
<=
opts
->
no_of_options
;
i
++
,
opt
++
)
{
if
(
opt
->
flags
&
AR_COMMAND
)
{
if
(
opt
->
flags
&
AR_COMMAND
)
continue
;
if
(
opt
->
flags
&
AR_CASE
)
{
if
(
ch
==
opt
->
opt_char
)
{
*
code
=
AR_IS_OPTION
;
{
if
(
ch
==
opt
->
opt_char
)
{
*
code
=
AR_IS_OPTION
;
return
(
opt
);
/* found */
}
}
else
{
if
(
toupper
(
UCHAR
(
ch
))
==
toupper
(
UCHAR
(
opt
->
opt_char
)))
{
*
code
=
AR_IS_OPTION
;
{
if
(
toupper
(
UCHAR
(
ch
))
==
toupper
(
UCHAR
(
opt
->
opt_char
)))
{
*
code
=
AR_IS_OPTION
;
return
(
opt
);
/* found */
}
};
...
...
@@ -495,14 +510,16 @@ static ar_option *str_search_option(ar_options *opts,
char
*
switch_found
)
/* search for an option */
/* is_command: search for a command (no leading '-' or '+') */
{
ar_option
*
opt
;
{
ar_option
*
opt
;
char
string
[
256
];
char
*
ptr
;
int
i
;
/* do string-compare */
if
(
*
str
==
0
)
{
*
code
=
AR_GENERAL_ERROR
;
{
*
code
=
AR_GENERAL_ERROR
;
return
(
NULL
);
/* (this proc cannot work if str is an empty string) */
};
...
...
@@ -515,7 +532,8 @@ static ar_option *str_search_option(ar_options *opts,
for
(
ptr
=
string
;
*
ptr
!=
0
;
ptr
++
);
ptr
--
;
if
((
*
ptr
==
'+'
)
||
(
*
ptr
==
'-'
))
{
*
switch_found
=
*
ptr
;
{
*
switch_found
=
*
ptr
;
*
ptr
=
0
;
/* for the following string-comparision, see below */
}
else
...
...
@@ -523,17 +541,22 @@ static ar_option *str_search_option(ar_options *opts,
opt
=
opts
->
option_array
;
for
(
i
=
1
;
i
<=
opts
->
no_of_options
;
i
++
,
opt
++
)
{
if
(
opt
->
flags
&
AR_COMMAND
)
{
if
(
opt
->
flags
&
AR_COMMAND
)
continue
;
if
(
opt
->
flags
&
AR_CASE
)
{
if
(
strcmp
(
string
,
opt
->
opt_string
)
==
0
)
{
*
code
=
AR_IS_OPTION
;
{
if
(
strcmp
(
string
,
opt
->
opt_string
)
==
0
)
{
*
code
=
AR_IS_OPTION
;
return
(
opt
);
/* found */
}
}
else
{
if
(
my_stricmp
(
string
,
opt
->
opt_string
)
==
0
)
{
*
code
=
AR_IS_OPTION
;
{
if
(
my_stricmp
(
string
,
opt
->
opt_string
)
==
0
)
{
*
code
=
AR_IS_OPTION
;
return
(
opt
);
/* found */
}
};
...
...
@@ -548,48 +571,60 @@ static ar_option *search_command(ar_options *opts,
/* search for a command */
/* a command must not have a switch !!,
so, there's no search for a trailing '-' or '+' */
{
ar_option
*
opt
;
{
ar_option
*
opt
;
int
i
;
char
ch
;
/* do string-compare */
opt
=
opts
->
option_array
;
for
(
i
=
1
;
i
<=
opts
->
no_of_options
;
i
++
,
opt
++
)
{
if
(
!
(
opt
->
flags
&
AR_COMMAND
))
{
if
(
!
(
opt
->
flags
&
AR_COMMAND
))
continue
;
if
(
opt
->
flags
&
AR_CASE
)
{
if
(
strcmp
(
str
,
opt
->
opt_string
)
==
0
)
{
*
code
=
AR_IS_COMMAND
;
{
if
(
strcmp
(
str
,
opt
->
opt_string
)
==
0
)
{
*
code
=
AR_IS_COMMAND
;
return
(
opt
);
/* found */
}
}
else
{
if
(
my_stricmp
(
str
,
opt
->
opt_string
)
==
0
)
{
*
code
=
AR_IS_COMMAND
;
{
if
(
my_stricmp
(
str
,
opt
->
opt_string
)
==
0
)
{
*
code
=
AR_IS_COMMAND
;
return
(
opt
);
/* found */
}
};
};
if
(
str
[
1
]
!=
0
)
{
*
code
=
AR_UNKNOWN_COMMAND
;
{
*
code
=
AR_UNKNOWN_COMMAND
;
return
(
NULL
);
};
/* from here: command not found as a string, so search the chars */
ch
=
str
[
0
];
opt
=
opts
->
option_array
;
for
(
i
=
1
;
i
<=
opts
->
no_of_options
;
i
++
,
opt
++
)
{
if
(
!
(
opt
->
flags
&
AR_COMMAND
))
{
if
(
!
(
opt
->
flags
&
AR_COMMAND
))
continue
;
if
(
opt
->
flags
&
AR_CASE
)
{
if
(
ch
==
opt
->
opt_char
)
{
*
code
=
AR_IS_COMMAND
;
{
if
(
ch
==
opt
->
opt_char
)
{
*
code
=
AR_IS_COMMAND
;
return
(
opt
);
/* found */
}
}
else
{
if
(
toupper
(
UCHAR
(
ch
))
==
toupper
(
UCHAR
(
opt
->
opt_char
)))
{
*
code
=
AR_IS_COMMAND
;
{
if
(
toupper
(
UCHAR
(
ch
))
==
toupper
(
UCHAR
(
opt
->
opt_char
)))
{
*
code
=
AR_IS_COMMAND
;
return
(
opt
);
/* found */
}
};
...
...
@@ -608,7 +643,8 @@ static ar_option *search_command(ar_options *opts,
static
char
*
strspctok
(
char
*
s1
)
/* similar to strtok */
{
static
char
*
iptr
=
NULL
;
{
static
char
*
iptr
=
NULL
;
if
(
s1
!=
NULL
)
iptr
=
s1
;
...
...
@@ -620,14 +656,16 @@ static char *strspctok(char *s1)
for
(
;
isspace
(
*
UCHAR_P
(
s1
));
s1
++
);
if
(
*
s1
==
0
)
{
iptr
=
NULL
;
{
iptr
=
NULL
;
return
(
NULL
);
};
for
(
iptr
=
s1
;
(
!
isspace
(
*
UCHAR_P
(
iptr
)))
&&
(
*
iptr
!=
0
);
iptr
++
);
if
(
*
iptr
==
0
)
iptr
=
NULL
;
else
{
*
iptr
=
0
;
{
*
iptr
=
0
;
if
(
*
(
++
iptr
)
==
0
)
iptr
=
NULL
;
};
...
...
@@ -642,7 +680,8 @@ static boolean go_next_arg_word(ar_arguments *args)
"-1" or "+9a" is NOT recognized as an option ! */
/* "@@filename" opens the file [filename] and takes arguments in the lines
in this filename. Newlines are not passed to the arg-module !! */
{
char
ch
,
ch2
=
0
,
ch3
=
0
;
/* initialization is important ! */
{
char
ch
,
ch2
=
0
,
ch3
=
0
;
/* initialization is important ! */
startagain:
if
(
args
->
optfile
!=
NULL
)
...
...
@@ -652,7 +691,8 @@ static boolean go_next_arg_word(ar_arguments *args)
while
(
args
->
optfile_ptr
==
NULL
)
{
if
(
0
==
fgets
(
args
->
optfile_line
,
127
,
args
->
optfile
))
{
fclose
(
args
->
optfile
);
{
fclose
(
args
->
optfile
);
args
->
optfile
=
NULL
;
break
;
};
...
...
@@ -675,15 +715,18 @@ static boolean go_next_arg_word(ar_arguments *args)
/* get 3 characters from the argument */
ch
=
(
args
->
current_arg
)[
0
];
if
(
ch
!=
0
)
{
ch2
=
(
args
->
current_arg
)[
1
];
{
ch2
=
(
args
->
current_arg
)[
1
];
if
(
ch2
!=
0
)
ch3
=
(
args
->
current_arg
)[
2
];
};
if
((
ch
==
'@'
)
&&
(
ch2
==
'@'
))
{
/* it's a filename */
{
/* it's a filename */
if
(
NULL
==
(
args
->
optfile
=
fopen
(
(
args
->
current_arg
)
+
2
,
"rt"
)
))
{
printf
(
"ERROR in the arg-module, unable to open file %s
\n
"
,
{
printf
(
"ERROR in the arg-module, unable to open file %s
\n
"
,
(
args
->
current_arg
)
+
2
);
return
(
FALSE
);
};
...
...
@@ -701,13 +744,15 @@ static boolean go_next_arg_word(ar_arguments *args)
/* it's a number, not an option */
return
(
TRUE
);
if
(
ch
==
'+'
)
{
args
->
is_option
=
TRUE
;
{
args
->
is_option
=
TRUE
;
args
->
long_option
=
TRUE
;
args
->
curr_char
=
(
args
->
current_arg
)
+
1
;
/* skip the '+' */
return
(
TRUE
);
};
if
(
ch2
==
'-'
)
/* a second '-' found */
{
if
(
ch3
==
0
)
{
if
(
ch3
==
0
)
return
(
TRUE
);
/* no option */
args
->
is_option
=
TRUE
;
args
->
long_option
=
TRUE
;
...
...
@@ -722,7 +767,8 @@ static boolean go_next_arg_word(ar_arguments *args)
}
static
boolean
go_next_arg_char
(
ar_arguments
*
args
)
{
char
ch
;
{
char
ch
;
again:
ch
=
*
(
++
(
args
->
curr_char
));
...
...
@@ -738,11 +784,13 @@ static boolean next_argument(ar_arguments *args)
{
if
(
args
->
is_option
)
if
(
args
->
long_option
)
{
/* long option : */
{
/* long option : */
return
(
go_next_arg_word
(
args
));
}
else
{
/* short option */
{
/* short option */
if
(
go_next_arg_char
(
args
))
/* more argument-chars */
return
(
TRUE
);
...
...
@@ -785,7 +833,8 @@ correctly:
",2) @EX(2)*/
void
ar_init
(
ar_arguments
*
args
,
ar_options
*
options
,
int
argc
,
char
**
argv
)
{
args
->
no_of_args
=
argc
-
1
;
{
args
->
no_of_args
=
argc
-
1
;
args
->
current_index
=
0
;
args
->
current_arg
=
*
argv
;
args
->
curr_char
=
*
argv
;
...
...
@@ -833,7 +882,8 @@ void ar_init(ar_arguments *args, ar_options *options,
2) @EX(2)*/
void
ar_get_next_argument
(
ar_arguments
*
args
,
int
*
code
,
boolean
*
switch_
)
{
char
sw
;
{
char
sw
;
ar_option
*
opt
;
if
(
!
next_argument
(
args
))
...
...
@@ -843,15 +893,18 @@ void ar_get_next_argument(ar_arguments *args, int *code,
};
if
(
args
->
is_option
)
{
if
(
args
->
long_option
)
{
if
(
args
->
long_option
)
opt
=
str_search_option
(
args
->
options
,
code
,
args
->
curr_char
,
&
sw
);
else
opt
=
ch_search_option
(
args
->
options
,
code
,
args
->
curr_char
,
&
sw
);
if
(
opt
==
NULL
)
return
;
/* *code already set */
if
(
opt
->
flags
&
AR_SWITCH
)
{
switch
(
sw
)
{
case
'+'
:
*
switch_
=
TRUE
;
{
switch
(
sw
)
{
case
'+'
:
*
switch_
=
TRUE
;
break
;
case
'-'
:
*
switch_
=
FALSE
;
break
;
...
...
@@ -863,16 +916,19 @@ void ar_get_next_argument(ar_arguments *args, int *code,
else
if
(
sw
!=
0
)
/* switch where no switch is allowed */
{
*
code
=
AR_MISPLACED_SWITCH
;
{
*
code
=
AR_MISPLACED_SWITCH
;
return
;
};
}
else
{
/* a possible command */
{
/* a possible command */
if
(
args
->
accept_commands
)
opt
=
search_command
(
args
->
options
,
code
,
args
->
curr_char
);
else
{
/* return an error */
{
/* return an error */
*
code
=
AR_NO_OPTION
;
return
;
};
...
...
@@ -910,13 +966,16 @@ returned, and <par> points to the string of the command-line argument.
1) @EX*/
void
ar_get_parameter
(
ar_arguments
*
args
,
int
*
code
,
char
**
par
)
/* goes to next arg. and awaits a parameter */
{
*
par
=
NULL
;
{
*
par
=
NULL
;
if
(
!
next_argument
(
args
))
{
*
code
=
AR_NO_MORE
;
{
*
code
=
AR_NO_MORE
;
return
;
};
if
(
args
->
is_option
)
/* @@@ a '!' was removed, was prob. a bug ! */
{
*
code
=
AR_IS_OPTION
;
{
*
code
=
AR_IS_OPTION
;
return
;
};
*
par
=
args
->
current_arg
;
...
...
@@ -932,7 +991,9 @@ the argument that was the last to be examined.
this program.",1) @EX*/
char
*
ar_current_parameter
(
ar_arguments
*
args
)
/* return current argument-string */
{
return
(
args
->
current_arg
);
}
{
return
(
args
->
current_arg
);
}
/*@D_DESC("ar_switch_commands - enable/disable recognition of commands",
"This function enables and disables the recognition of commands. A
...
...
@@ -954,7 +1015,9 @@ an <AR_NO_OPTION> return-code when command-recognition is off.
FALSE: switch command-recognition off",
1) @EX*/
void
ar_switch_commands
(
ar_arguments
*
args
,
boolean
on
)
{
args
->
accept_commands
=
on
;
}
{
args
->
accept_commands
=
on
;
}
/*@EX*/
int
arg_args_from_str
(
char
*
str
,
char
**
argv_par
[])
...
...
@@ -972,7 +1035,8 @@ int arg_args_from_str(char *str, char **argv_par[])
p
=
buffer
;
for
(;;)
{
for
(;
isspace
(
*
UCHAR_P
(
p
));
p
++
);
{
for
(;
isspace
(
*
UCHAR_P
(
p
));
p
++
);
if
(
*
p
==
0
)
break
;
argv
[
argc
++
]
=
p
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment