-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheader.sh
48 lines (32 loc) · 1.32 KB
/
header.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
#!/bin/bash
## This script generates a .h file from a .c file
## The .h file will contain the function prototypes and the imports from the .c file
## The .h file will have the same base name as the .c file
## The .h file will have include guards with the format <BASE_NAME>_H
# Check if a .c file is provided as an argument
if [ -z "$1" ]; then
echo "Usage: $0 <filename.c>"
exit 1
fi
# Extract the base name of the file (without extension)
base_name=$(basename "$1" .c)
path=$(dirname "$1")
headername_uppercase="$(echo $base_name | tr '[:lower:]' '[:upper:]')"
# Create the .h file with the same base name
header_file="${path}/${base_name}.h"
echo "base_name: ${headername_uppercase}, path: $path"
# Add include guards to the .h file
echo "#ifndef ${headername_uppercase}_H" > "$header_file"
echo "#define ${headername_uppercase}_H" >> "$header_file"
echo "" >> "$header_file"
#get all the imports from the .c file
imports=$(grep -E "^#include" $1)
# Add the imports to the .h file
echo "$imports" >> "$header_file"
echo "" >> "$header_file"
# Add the function prototypes to the .h file
grep -E "^[a-zA-Z_].*\(" $1 | sed -E 's/\(.*\)/;/' >> "$header_file"
echo "" >> "$header_file"
# Close the include guards
echo "#endif // ${headername_uppercase}_H" >> "$header_file"
echo "Header file $header_file created successfully."