-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_c_files.sh
executable file
·98 lines (74 loc) · 2.39 KB
/
make_c_files.sh
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
#!/bin/bash
# Copyright (c) 2021, Laurence Lundblade.
#
# Created by Laurence Lundblade on 6/5/2021.
#
# SPDX-License-Identifier: BSD-3-Clause
#
# See BSD-3-Clause license in README.md
# This takes an indicator to produce either source or header and the
# name of file containing encoded CBOR and outputs a C static variable
# for the contents of the file
# The first argument is the operation type, either "source" or "header"
operation_type=$1
# The second argument is the file with encoded CBOR that is the input
cbor_input_file=$2
header_preamble='
/* This file is automatically generated from CBOR-diag files
* or hex format files and should not be edited.
*/
#define EAT_TEST_USEFUL_BUF(test_name) ((struct q_useful_buf_c){test_name, sizeof(test_name)})
'
source_preamble='
/* This file is automatically generated from CBOR-diag files
* or hex format files and should not be edited.
*/
'
if [[ $operation_type != *_preamble ]] ; then
# Process the path name down to a variable name. If there is a
# directory name in the path, then that is prepended to the name. If
# not, then it is not prepended.
# Prefix all test with this
prefix="eat_test_"
f1=$(basename $cbor_input_file)
file=${f1%.*}
full_path=$(dirname $cbor_input_file)
last_path=$(basename $full_path)
if [[ $last_path == $full_path ]] ; then
name=${prefix}$file
else
name=${prefix}${last_path}_${file}
fi
fi
case $operation_type in
source)
# Being asked to produce the source file, the one with the
# contents of the CBOR file in a const variable. xxd does all
# the work. It is necessary to pipe the file into xxd so this
# can control the variable name.
echo "const unsigned char ${name}[] = {"
cat $cbor_input_file | xxd -i
echo "};"
echo
;;
header)
# Being asked to produce the header file, the empty definition
# of the variable. The size is included so the sizeof operator
# works on it. Must pipe into wc so the file name is not in
# the wc output
size=`cat $cbor_input_file | wc -c | tr -d ' '`
echo "extern const unsigned char ${name}[${size}];"
echo
echo
;;
source_preamble)
echo "$source_preamble"
;;
header_preamble)
echo "$header_preamble"
;;
*)
# a bad operation type was give
exit -1
;;
esac