Skip to content

Commit 0e58990

Browse files
authored
Merge pull request #120 from saurabhraghuvanshii/Clang-format
feat: add `.clang-format`
2 parents a4a98be + 47af467 commit 0e58990

File tree

6 files changed

+114
-101
lines changed

6 files changed

+114
-101
lines changed

.clang-format

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: LLVM
4+
AlignArrayOfStructures: Left
5+
ColumnLimit: 100

.github/workflows/build.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,38 @@ permissions:
1313
contents: read
1414

1515
jobs:
16+
clang-format-check:
17+
name: Clang Format Check
18+
runs-on: macos-15
19+
timeout-minutes: 10
20+
steps:
21+
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
22+
with:
23+
fetch-depth: 1
24+
25+
- name: Install Clang Format
26+
run: |
27+
brew update
28+
brew install clang-format
29+
clang-format --version
30+
31+
- name: Run Clang Format Check
32+
run: |
33+
# Format all source - if the source is formatted properly this will do nothing
34+
clang-format -i *.c *.h client/*.c
35+
36+
# Do we have unwanted changes?
37+
if ! git diff-index --quiet HEAD; then
38+
# Show the changes
39+
echo "ERROR: Code is not properly formatted."
40+
echo
41+
git --no-pager diff
42+
43+
echo
44+
echo "Please run clang-format locally and commit the changes."
45+
exit 1
46+
fi
47+
1648
build:
1749
name: Build
1850
strategy:

cli.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ static void print_usage(const char *argv0) {
3030
printf("\n");
3131
printf("--socket-group=GROUP socket group name (default: "
3232
"\"" CLI_DEFAULT_SOCKET_GROUP "\")\n");
33-
printf(
34-
"--vmnet-mode=(host|shared|bridged) vmnet mode (default: \"shared\")\n");
33+
printf("--vmnet-mode=(host|shared|bridged) vmnet mode (default: \"shared\")\n");
3534
printf("--vmnet-interface=INTERFACE interface used for "
3635
"--vmnet=bridged, e.g., \"en0\"\n");
3736
printf("--vmnet-gateway=IP gateway used for "
@@ -83,18 +82,18 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
8382
}
8483

8584
const struct option longopts[] = {
86-
{"socket-group", required_argument, NULL, CLI_OPT_SOCKET_GROUP},
87-
{"vmnet-mode", required_argument, NULL, CLI_OPT_VMNET_MODE},
88-
{"vmnet-interface", required_argument, NULL, CLI_OPT_VMNET_INTERFACE},
89-
{"vmnet-gateway", required_argument, NULL, CLI_OPT_VMNET_GATEWAY},
90-
{"vmnet-dhcp-end", required_argument, NULL, CLI_OPT_VMNET_DHCP_END},
91-
{"vmnet-mask", required_argument, NULL, CLI_OPT_VMNET_MASK},
92-
{"vmnet-interface-id", required_argument, NULL, CLI_OPT_VMNET_INTERFACE_ID},
93-
{"vmnet-nat66-prefix", required_argument, NULL, CLI_OPT_VMNET_NAT66_PREFIX},
94-
{"pidfile", required_argument, NULL, 'p'},
95-
{"help", no_argument, NULL, 'h'},
96-
{"version", no_argument, NULL, 'v'},
97-
{0, 0, 0, 0},
85+
{"socket-group", required_argument, NULL, CLI_OPT_SOCKET_GROUP },
86+
{"vmnet-mode", required_argument, NULL, CLI_OPT_VMNET_MODE },
87+
{"vmnet-interface", required_argument, NULL, CLI_OPT_VMNET_INTERFACE },
88+
{"vmnet-gateway", required_argument, NULL, CLI_OPT_VMNET_GATEWAY },
89+
{"vmnet-dhcp-end", required_argument, NULL, CLI_OPT_VMNET_DHCP_END },
90+
{"vmnet-mask", required_argument, NULL, CLI_OPT_VMNET_MASK },
91+
{"vmnet-interface-id", required_argument, NULL, CLI_OPT_VMNET_INTERFACE_ID},
92+
{"vmnet-nat66-prefix", required_argument, NULL, CLI_OPT_VMNET_NAT66_PREFIX},
93+
{"pidfile", required_argument, NULL, 'p' },
94+
{"help", no_argument, NULL, 'h' },
95+
{"version", no_argument, NULL, 'v' },
96+
{0, 0, 0, 0 },
9897
};
9998
int opt = 0;
10099
while ((opt = getopt_long(argc, argv, "hvp:", longopts, NULL)) != -1) {
@@ -158,8 +157,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
158157

159158
/* fill default */
160159
if (res->socket_group == NULL)
161-
res->socket_group =
162-
strdup(CLI_DEFAULT_SOCKET_GROUP); /* use strdup to make it freeable */
160+
res->socket_group = strdup(CLI_DEFAULT_SOCKET_GROUP); /* use strdup to make it freeable */
163161
if (res->vmnet_mode == 0)
164162
res->vmnet_mode = VMNET_SHARED_MODE;
165163
if (res->vmnet_gateway != NULL && res->vmnet_dhcp_end == NULL) {
@@ -182,8 +180,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
182180
res->vmnet_dhcp_end = strdup(end_static);
183181
}
184182
if (res->vmnet_gateway != NULL && res->vmnet_mask == NULL)
185-
res->vmnet_mask =
186-
strdup("255.255.255.0"); /* use strdup to make it freeable */
183+
res->vmnet_mask = strdup("255.255.255.0"); /* use strdup to make it freeable */
187184
if (uuid_is_null(res->vmnet_interface_id)) {
188185
uuid_generate_random(res->vmnet_interface_id);
189186
}
@@ -196,7 +193,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
196193
if (res->vmnet_gateway == NULL) {
197194
if (res->vmnet_mode != VMNET_BRIDGED_MODE) {
198195
WARN("--vmnet-gateway=IP should be explicitly specified to "
199-
"avoid conflicting with other applications");
196+
"avoid conflicting with other applications");
200197
}
201198
if (res->vmnet_dhcp_end != NULL) {
202199
ERROR("--vmnet-dhcp-end=IP requires --vmnet-gateway=IP");
@@ -213,8 +210,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
213210
}
214211
struct in_addr dummy;
215212
if (!inet_aton(res->vmnet_gateway, &dummy)) {
216-
ERRORF("invalid address \"%s\" was specified for --vmnet-gateway",
217-
res->vmnet_gateway);
213+
ERRORF("invalid address \"%s\" was specified for --vmnet-gateway", res->vmnet_gateway);
218214
goto error;
219215
}
220216
}

client/main.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ int main(int argc, char *argv[]) {
2727
}
2828
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
2929
if (connect(socket_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
30-
fprintf(stderr, "Failed to connect to \"%s\": %s\n", socket_path,
31-
strerror(errno));
30+
fprintf(stderr, "Failed to connect to \"%s\": %s\n", socket_path, strerror(errno));
3231
exit(EXIT_FAILURE);
3332
}
3433
char **child_argv = argv + 2;
@@ -38,8 +37,7 @@ int main(int argc, char *argv[]) {
3837
for (int i = 0; child_argv[i] != NULL; i++)
3938
fprintf(stderr, "child_argv[%d]: \"%s\"\n", i, child_argv[i]);
4039
if (execvp(child_argv[0], child_argv) < 0) {
41-
fprintf(stderr, "Failed to exec \"%s\": %s\n", child_argv[0],
42-
strerror(errno));
40+
fprintf(stderr, "Failed to exec \"%s\": %s\n", child_argv[0], strerror(errno));
4341
exit(EXIT_FAILURE);
4442
}
4543
return 0;

log.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
extern bool debug;
66

7-
#define DEBUGF(fmt, ...) \
8-
do { \
9-
if (debug) \
10-
fprintf(stderr, "DEBUG| " fmt "\n", __VA_ARGS__); \
7+
#define DEBUGF(fmt, ...) \
8+
do { \
9+
if (debug) \
10+
fprintf(stderr, "DEBUG| " fmt "\n", __VA_ARGS__); \
1111
} while (0)
1212

13-
#define INFOF(fmt, ...) fprintf(stderr, "INFO | " fmt "\n", __VA_ARGS__)
14-
#define ERROR(msg) fprintf(stderr, "ERROR| " msg "\n")
15-
#define ERRORF(fmt, ...) fprintf(stderr, "ERROR| " fmt "\n", __VA_ARGS__)
16-
#define ERRORN(name) ERRORF(name ": %s", strerror(errno))
17-
#define WARN(msg) fprintf(stderr, "WARN | " msg "\n")
18-
#define WARNF(fmt, ...) fprintf(stderr, "WARN | " fmt "\n", __VA_ARGS__)
13+
#define INFOF(fmt, ...) fprintf(stderr, "INFO | " fmt "\n", __VA_ARGS__)
14+
#define ERROR(msg) fprintf(stderr, "ERROR| " msg "\n")
15+
#define ERRORF(fmt, ...) fprintf(stderr, "ERROR| " fmt "\n", __VA_ARGS__)
16+
#define ERRORN(name) ERRORF(name ": %s", strerror(errno))
17+
#define WARN(msg) fprintf(stderr, "WARN | " msg "\n")
18+
#define WARNF(fmt, ...) fprintf(stderr, "WARN | " fmt "\n", __VA_ARGS__)
1919

2020
#endif /* SOCKET_VMNET_LOG_H */

0 commit comments

Comments
 (0)