Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line check #11

Merged
merged 7 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ include config.mk

include oapi-cli.mk

main.c: osc-api.json call_list arguments-list.json config.sh main_tpl.c cognac_gen.sh mk_args.c.sh
bin/line_check: bin/line_check.c
$(CC) -O3 bin/line_check.c -o bin/line_check

main.c: bin/line_check osc-api.json call_list arguments-list.json config.sh main_tpl.c cognac_gen.sh mk_args.c.sh
./cognac_gen.sh main_tpl.c main.c c

osc_sdk.c: osc-api.json call_list arguments-list.json config.sh lib.c cognac_gen.sh construct_data.c.sh mk_args.c.sh
osc_sdk.c: bin/line_check osc-api.json call_list arguments-list.json config.sh lib.c cognac_gen.sh construct_data.c.sh mk_args.c.sh
./cognac_gen.sh lib.c osc_sdk.c c

osc_sdk.h: osc-api.json call_list arguments-list.json config.sh lib.h cognac_gen.sh mk_args.c.sh
osc_sdk.h: bin/line_check osc-api.json call_list arguments-list.json config.sh lib.h cognac_gen.sh mk_args.c.sh
./cognac_gen.sh lib.h osc_sdk.h c

oapi-cli-completion.bash: osc-api.json call_list arguments-list.json config.sh oapi-cli-completion-tpl.bash cognac_gen.sh
oapi-cli-completion.bash: bin/line_check osc-api.json call_list arguments-list.json config.sh oapi-cli-completion-tpl.bash cognac_gen.sh
./cognac_gen.sh oapi-cli-completion-tpl.bash oapi-cli-completion.bash bash

config.sh:
Expand All @@ -55,7 +58,7 @@ call_list: osc-api.json


clean:
rm -vf osc-api.json call_list osc_sdk.c arguments-list.json osc_sdk.h main.c oapi-cli config.sh oapi-cli-completion.bash
rm -vf osc-api.json call_list osc_sdk.c arguments-list.json osc_sdk.h main.c oapi-cli config.sh oapi-cli-completion.bash bin/line_check

.PHONY: clean list_api_version help

34 changes: 34 additions & 0 deletions bin/line_check.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>

#define BUF_SIZE 2048

int main(int ac, char **av) {

char buf[BUF_SIZE];
size_t s;
static int bufs_cnt[256];
int i, j;

if (ac > 254) {
return -1;
}

while ((s = read(0, buf, BUF_SIZE)) > 0) {
for (i = 0; i < s; ++i) {
for (j = 1; j < ac; ++j) {
if (!av[j][bufs_cnt[j]]) {
printf("%s\n", av[j]);
return 0;
}
if (av[j][bufs_cnt[j]] == buf[i])
++bufs_cnt[j];
else
bufs_cnt[j] = 0;
}
}
}

return -1;
}
142 changes: 81 additions & 61 deletions cognac_gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dash_this_arg()
cur="$cur.$arg"
if [ "ref" == $( echo "$t" | cut -d ' ' -f 1) ]; then
local st=$( echo $t | cut -f 2 -d ' ' )
local st_info=$(jq .components.schemas.$st <<< $OSC_API_JSON)
local st_info=$(jq .components.schemas.$st < osc-api.json)
local st_args=$(json-search -K properties <<< "$st_info"- | tr -d '"[],')

for st_arg in $st_args; do
Expand All @@ -43,7 +43,7 @@ dash_this()

if [ "ref" == $( echo "$t" | cut -d ' ' -f 1) ]; then
local st=$( echo $t | cut -f 2 -d ' ' )
local st_info=$(jq .components.schemas.$st <<< $OSC_API_JSON)
local st_info=$(jq .components.schemas.$st < osc-api.json)
local st_args=$(json-search -K properties <<< "$st_info"- | tr -d '"[],')

for st_arg in $st_args; do
Expand Down Expand Up @@ -95,15 +95,39 @@ EOF
elif [ 'array integer' == "$type" -o 'array string' == "$type" -o 'array double' == "$type" ]; then


convertor=""
local convertor=""
local null_val='""'
if [ 'array integer' == "$type" ]; then
convertor=atoi
null_val=0
elif [ 'array double' == "$type" ]; then
convertor=atof
null_val=0.0
fi
cat <<EOF
${indent_plus} TRY(!aa, "$a argument missing\n");
${indent_plus} s->${snake_a}_str = aa;
${indent_plus} if (aret == '.') {
${indent_plus} int pos;
${indent_plus} char *endptr;
${indent_plus} int last = 0;
${indent_plus} char *dot_pos = strchr(str, '.');

${indent_plus} TRY(!(dot_pos++), "$a argument missing\n");
${indent_plus} pos = strtoul(dot_pos, &endptr, 0);
${indent_plus} TRY(endptr == dot_pos, "$a require an index\n");
${indent_plus} if (s->${snake_a}) {
${indent_plus} for (; s->${snake_a}[last]; ++last);
${indent_plus} }
${indent_plus} if (pos < last) {
${indent_plus} s->${snake_a}[pos] = ${convertor}(aa);
${indent_plus} } else {
${indent_plus} for (int i = last; i < pos; ++i)
${indent_plus} SET_NEXT(s->${snake_a}, $null_val, pa);
${indent_plus} SET_NEXT(s->${snake_a}, ${convertor}(aa), pa);
${indent_plus} }
${indent_plus} } else {
${indent_plus} TRY(!aa, "$a argument missing\n");
${indent_plus} s->${snake_a}_str = aa;
${indent_plus} }
$indent_base } else if (!(aret = strcmp(str, "$a[]")) || aret == '=') {
${indent_plus} TRY(!aa, "$a[] argument missing\n");
${indent_plus} SET_NEXT(s->${snake_a}, ${convertor}(aa), pa);
Expand Down Expand Up @@ -174,55 +198,39 @@ EOF

replace_args()
{
API_VERSION=$(json-search -R version <<< $OSC_API_JSON)
API_VERSION=$(json-search -R version < osc-api.json)
SDK_VERSION=$(cat sdk-version)
while IFS= read -r line
do
#check ____args____ here
grep ____args____ <<< "$line" > /dev/null
have_args=$?
grep ____func_code____ <<< "$line" > /dev/null
have_func_code=$?
grep ____functions_proto____ <<< "$line" > /dev/null
have_func_protos=$?
grep ____cli_parser____ <<< "$line" > /dev/null
have_cli_parser=$?
grep ____complex_struct_func_parser____ <<< "$line" > /dev/null
have_complex_struct_func_parser=$?
grep ____complex_struct_to_string_func____ <<< "$line" > /dev/null
have_complex_struct_to_string_func=$?
grep ____call_list_dec____ <<< "$line" > /dev/null
have_call_list_dec=$?
grep ____call_list_descriptions____ <<< "$line" > /dev/null
have_call_list_descr=$?
grep ____call_list_args_descriptions____ <<< "$line" > /dev/null
have_call_list_arg_descr=$?

if [ $have_args == 0 ]; then

arg_check=$(bin/line_check ____args____ ____func_code____ ____functions_proto____ ____cli_parser____ ____complex_struct_func_parser____ ____complex_struct_to_string_func____ ____call_list_dec____ ____call_list_descriptions____ ____call_list_args_descriptions____ <<< "$line")

if [ "$arg_check" == "____args____" ]; then
./mk_args.${lang}.sh
elif [ $have_call_list_descr == 0 ]; then
elif [ "$arg_check" == "____call_list_descriptions____" ]; then
DELIMES=$(cut -d '(' -f 2 <<< $line | tr -d ')')
D1=$(cut -d ';' -f 1 <<< $DELIMES | tr -d "'")
D2=$(cut -d ';' -f 2 <<< $DELIMES | tr -d "'")
D3=$(cut -d ';' -f 3 <<< $DELIMES | tr -d "'")
for x in $CALL_LIST ; do
echo -en $D1
local required=$(echo $OSC_API_JSON | json-search ${x}Request | json-search required 2>&1 | tr -d '[]\n"' | tr -s ' ' | sed 's/nothing found//g')
local required=$(json-search ${x}Request < osc-api.json | json-search required 2>&1 | tr -d '[]\n"' | tr -s ' ' | sed 's/nothing found//g')
local usage_required=$( for a in $(echo $required | tr -d ','); do echo -n " --${a}=${a,,}"; done )
local usage="\"Usage: oapi-cli $x ${usage_required} [OPTIONS]\n\""
local call_desc=$(echo $OSC_API_JSON | jq .paths.\""/$x"\".description | sed 's/<br \/>//g' | tr -d '"' | fold -s | sed 's/^/"/;s/$/\\n"/')
local call_desc=$(jq .paths.\""/$x"\".description < osc-api.json | sed 's/<br \/>//g' | tr -d '"' | fold -s | sed 's/^/"/;s/$/\\n"/')

echo $usage $call_desc \""\nRequired Argument:" $required "\n\""
echo -en $D2
done
echo -ne $D3
elif [ $have_call_list_arg_descr == 0 ]; then
elif [ "$arg_check" == "____call_list_args_descriptions____" ]; then
DELIMES=$(cut -d '(' -f 2 <<< $line | tr -d ')')
D1=$(cut -d ';' -f 1 <<< $DELIMES | tr -d "'")
D2=$(cut -d ';' -f 2 <<< $DELIMES | tr -d "'")
D3=$(cut -d ';' -f 3 <<< $DELIMES | tr -d "'")
for x in $CALL_LIST ; do
st_info=$(json-search -s ${x}Request ./osc-api.json)
st_info=$(json-search -s ${x}Request < osc-api.json)
A_LST=$(json-search -K properties <<< $st_info | tr -d '",[]')

echo -en $D1
Expand All @@ -235,7 +243,7 @@ replace_args()
echo -en $D2
done
echo -ne $D3
elif [ $have_call_list_dec == 0 ]; then
elif [ "$arg_check" == "____call_list_dec____" ]; then
DELIMES=$(cut -d '(' -f 2 <<< $line | tr -d ')')
D1=$(cut -d ';' -f 1 <<< $DELIMES | tr -d "'")
D2=$(cut -d ';' -f 2 <<< $DELIMES | tr -d "'")
Expand All @@ -246,36 +254,45 @@ replace_args()
echo -en $D2
done
echo -ne $D3
elif [ $have_complex_struct_to_string_func == 0 ]; then
COMPLEX_STRUCT=$(jq .components <<< $OSC_API_JSON | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response | grep -v Request)
elif [ "$arg_check" == "____complex_struct_to_string_func____" ]; then
COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response | grep -v Request)

for s in $COMPLEX_STRUCT; do
struct_name=$(to_snakecase <<< $s)

echo "static int ${struct_name}_setter(struct ${struct_name} *args, struct osc_str *data);"
A_LST=$(jq .components.schemas.$s < osc-api.json | json-search -Kn properties | tr -d '",[]')
if [ "$A_LST" != "null" ]; then
echo "static int ${struct_name}_setter(struct ${struct_name} *args, struct osc_str *data);"
fi
done
for s in $COMPLEX_STRUCT; do
struct_name=$(to_snakecase <<< $s)
cat <<EOF
A_LST=$(jq .components.schemas.$s < osc-api.json | json-search -Kn properties | tr -d '",[]')
if [ "$A_LST" != "null" ]; then
cat <<EOF
static int ${struct_name}_setter(struct ${struct_name} *args, struct osc_str *data) {
int count_args = 0;
int ret = 0;
EOF
A_LST=$(jq .components.schemas.$s <<< $OSC_API_JSON | json-search -K properties | tr -d '",[]')

./construct_data.c.sh $s complex_struct
cat <<EOF
./construct_data.c.sh $s complex_struct
cat <<EOF

return !!ret;
}
EOF
fi
done
elif [ $have_complex_struct_func_parser == 0 ]; then
COMPLEX_STRUCT=$(jq .components <<< $OSC_API_JSON | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response | grep -v Request)
elif [ "$arg_check" == "____complex_struct_func_parser____" ]; then
COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response | grep -v Request)

# prototypes
for s in $COMPLEX_STRUCT; do
struct_name=$(to_snakecase <<< $s)
echo "int ${struct_name}_parser(void *s, char *str, char *aa, struct ptr_array *pa);"
A_LST=$(jq .components.schemas.$s < osc-api.json | json-search -Kn properties | tr -d '",[]')
if [ "$A_LST" != "null" ]; then
echo "int ${struct_name}_parser(void *s, char *str, char *aa, struct ptr_array *pa);"
fi
done
echo "" #add a \n

Expand All @@ -284,31 +301,34 @@ EOF
#for s in "skip"; do
struct_name=$(to_snakecase <<< $s)

echo "int ${struct_name}_parser(void *v_s, char *str, char *aa, struct ptr_array *pa) {"
A_LST=$(jq .components.schemas.$s <<< $OSC_API_JSON | json-search -K properties | tr -d '",[]')
echo " struct $struct_name *s = v_s;"
echo " int aret = 0;"
for a in $A_LST; do
t=$(get_type2 "$s" "$a")
snake_n=$(to_snakecase <<< $a)
A_LST=$(jq .components.schemas.$s < osc-api.json | json-search -Kn properties | tr -d '",[]')
if [ "$A_LST" != "null" ]; then
echo "int ${struct_name}_parser(void *v_s, char *str, char *aa, struct ptr_array *pa) {"

echo " if ((aret = argcmp(str, \"$a\")) == 0 || aret == '=') {"
cli_c_type_parser "$a" "$t" " "
done
cat <<EOF
echo " struct $struct_name *s = v_s;"
echo " int aret = 0;"
for a in $A_LST; do
t=$(get_type2 "$s" "$a")
snake_n=$(to_snakecase <<< $a)

echo " if ((aret = strcmp(str, \"$a\")) == 0 || aret == '=' || aret == '.') {"
cli_c_type_parser "$a" "$t" " "
done
cat <<EOF
{
fprintf(stderr, "'%s' not an argumemt of '$s'\n", str);
return -1;
}
EOF
echo " return 0;"
echo -e '}\n'
echo " return 0;"
echo -e '}\n'
fi
done

elif [ $have_cli_parser == 0 ] ; then
elif [ "$arg_check" == "____cli_parser____" ] ; then
for l in $CALL_LIST; do
snake_l=$(to_snakecase <<< $l)
arg_list=$(json-search ${l}Request osc-api.json \
arg_list=$(json-search ${l}Request < osc-api.json \
| json-search -K properties \
| tr -d "[]\"," | sed '/^$/d')

Expand Down Expand Up @@ -365,7 +385,7 @@ EOF
snake_a=$(to_snakecase <<< $a)

cat <<EOF
if ((aret = argcmp(next_a, "$a")) == 0 || aret == '=' ) {
if ((aret = strcmp(next_a, "$a")) == 0 || aret == '=' || aret == '.') {
char *eq_ptr = strchr(next_a, '=');
if (eq_ptr) {
TRY((!*eq_ptr), "$a argument missing\n");
Expand Down Expand Up @@ -413,15 +433,15 @@ EOF
} else
EOF
done
elif [ $have_func_protos == 0 ] ; then
elif [ "$arg_check" == "____functions_proto____" ] ; then
for l in $CALL_LIST; do
local snake_l=$(to_snakecase <<< $l)
echo "int osc_${snake_l}(struct osc_env *e, struct osc_str *out, struct osc_${snake_l}_arg *args);"
done
elif [ $have_func_code == 0 ]; then
elif [ "$arg_check" == "____func_code____" ]; then
for x in $CALL_LIST; do
local snake_x=$(to_snakecase <<< $x)
local args=$(json-search ${x}Request <<< $OSC_API_JSON \
local args=$(json-search ${x}Request < osc-api.json \
| json-search -K properties | tr -d "[]\",")
dashed_args=""
for arg in $args; do
Expand Down
8 changes: 6 additions & 2 deletions construct_data.c.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ func=$1
source ./helper.sh

if [ "complex_struct" == "$2" ]; then
args=$(jq .components.schemas.$func <<< $OSC_API_JSON | json-search -K properties | tr -d '",[]')
args=$(jq .components.schemas.$func < osc-api.json | json-search -Kn properties | tr -d '",[]')
alias get_type=get_type2
else
args=$(json-search ${func}Request osc-api.json | json-search -K properties | tr -d "\n[],\"" | sed 's/ / /g')
args=$(json-search ${func}Request < osc-api.json | json-search -Kn properties | tr -d "\n[],\"" | sed 's/ / /g')
fi

if [ "$args" == "null" ]; then
exit
fi

for x in $args ;do
Expand Down
Loading
Loading