GCC Code Coverage Report


src/
File: src/cmd/cmd.c
Date: 2026-03-31 17:48:22
Lines:
50/59
84.7%
Functions:
2/2
100.0%
Branches:
33/38
86.8%

Line Branch Exec Source
1 #include <argp.h>
2 #include <errno.h>
3 #include <glib.h>
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include "cmd/cmd.h"
10 #include "config/config.h"
11 #include "devs/devs.h"
12 #include "examples/curl.h"
13 #include "examples/git.h"
14 #include "examples/ini_parser.h"
15 #include "examples/tor.h"
16 #include "init/init.h"
17 #include "leech/leech.h"
18 #include "login/login.h"
19 #include "seed/seed.h"
20 #include "service/service.h"
21 #include "verify/verify.h"
22
23 #define KEY_USAGE 1
24
25 static error_t parse_opt(int key, char* arg, struct argp_state* state);
26
27 static const char doc[] =
28 "COMMANDS:\n"
29 "\n"
30 " init Create an empty GitTor repository\n"
31 " leech Clone a GitTor repository into a new directory\n"
32 " login Authenticate with the GitTor server\n"
33 " seed Share the current state of the repository\n"
34 " devs Manage who can contribute to this repository\n"
35 " verify Verify all commits are from authorized developers\n"
36 " config Get and set GitTor local or global configurations\n"
37 " service Manage the GitTor service\n"
38 "\n"
39 "OPTIONS:"
40 "\v";
41
42 static const struct argp_option options[] = {
43 {"path", 'p', "PATH", 0, "The path to the gittor repository", 0},
44 {"help", '?', NULL, 0, "Give this help list", -2},
45 {"usage", KEY_USAGE, NULL, 0, "Give a short usage message", -1},
46 {NULL}};
47
48 static struct argp argp = {
49 options, parse_opt, "COMMAND [ARGUMENTS...]", doc, NULL, NULL, NULL};
50
51 static bool helped;
52 221 static error_t parse_opt(int key, char* arg, struct argp_state* state) {
53 221 struct global_arguments* args = state->input;
54
55
6/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 37 times.
✓ Branch 5 taken 139 times.
221 switch (key) {
56 3 case 'p':
57 3 g_snprintf(args->path, sizeof(args->path), "%s", arg);
58 3 break;
59
60 2 case '?':
61 2 argp_help(&argp, stdout, ARGP_HELP_STD_HELP, state->name);
62 2 helped = true;
63 2 break;
64
65 1 case KEY_USAGE:
66 1 argp_help(&argp, stdout, ARGP_HELP_STD_USAGE, state->name);
67 1 helped = true;
68 1 break;
69
70 39 case ARGP_KEY_ARG:
71
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 33 times.
39 if (strcmp(arg, "init") == 0) {
72 6 return gittor_init(state);
73
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 32 times.
33 } else if (strcmp(arg, "leech") == 0) {
74 1 return gittor_leech(state);
75
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 31 times.
32 } else if (strcmp(arg, "seed") == 0) {
76 1 return gittor_seed(state);
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 } else if (strcmp(arg, "login") == 0) {
78 return gittor_login(state);
79
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 30 times.
31 } else if (strcmp(arg, "devs") == 0) {
80 1 return gittor_devs(state);
81
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28 times.
30 } else if (strcmp(arg, "verify") == 0) {
82 2 return gittor_verify(state);
83
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 11 times.
28 } else if (strcmp(arg, "config") == 0) {
84 17 return gittor_config(state);
85
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 } else if (strcmp(arg, "service") == 0) {
86 10 return gittor_service(state);
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (strcmp(arg, "tor") == 0) { // TODO(isaac): remove
88 torrent_example();
89 return 0;
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (strcmp(arg, "ini") == 0) { // TODO(isaac): remove
91 ini_parse();
92 return 0;
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (strcmp(arg, "git") == 0) { // TODO(isaac): remove
94 git_example();
95 return 0;
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (strcmp(arg, "www") == 0) { // TODO(isaac): remove
97 curl_example();
98 return 0;
99 } else {
100 1 argp_error(state, "%s is not a valid command", arg);
101 1 return ESRCH;
102 }
103 break;
104
105 37 case ARGP_KEY_END:
106
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
37 if (state->arg_num == 0 && !helped) {
107 1 argp_help(&argp, stdout, ARGP_HELP_STD_USAGE, state->name);
108 1 return EINVAL;
109 }
110 36 break;
111
112 139 default:
113 139 return ARGP_ERR_UNKNOWN;
114 }
115
116 42 return 0;
117 }
118
119 46 extern int cmd_parse(int argc, char** argv) {
120 // Defaults
121 struct global_arguments args;
122 46 helped = false;
123
124
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 45 times.
46 if (getcwd(args.path, sizeof(args.path)) == NULL) {
125
1/1
✓ Branch 1 taken 1 times.
1 perror("getcwd() error");
126 46 return errno;
127 }
128
129 // Parse command line arguments
130
1/1
✓ Branch 1 taken 45 times.
45 return argp_parse(&argp, argc, argv,
131 ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP, NULL, &args);
132 }
133