This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
fb-adb.c
119 lines (101 loc) · 2.58 KB
/
fb-adb.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in
* the LICENSE file in the root directory of this source tree. An
* additional grant of patent rights can be found in the PATENTS file
* in the same directory.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include "util.h"
#include "strutil.h"
#include "cmd.h"
#include "autocmd.h"
#include "timestamp.h"
#include "fs.h"
static bool
word_follows_adb_arg_p(const char* p)
{
for (; *p; ++p) {
if (strchr("spHP", *p)) {
if (*(p+1) == '\0')
return true;
return false;
}
}
return false;
}
static bool
adb_arg_p(char** argv, unsigned i)
{
if (argv[i] == NULL)
return false;
if (argv[i][0] == '-' && argv[i][1] == 'h')
return false;
if (argv[i][0] == '-' &&
argv[i][1] == '-' &&
argv[i][2] != '\0')
{
return false;
}
if (i > 1 && !strcmp(argv[i-1], "--"))
return false;
if (argv[i][0] == '-')
return true;
/* Catch FOO in pairs like -s FOO */
if (i > 1 &&
argv[i-1][0] == '-' &&
word_follows_adb_arg_p(argv[i-1]))
{
return true;
}
return false;
}
int
real_main(int argc, char** argv)
{
unsigned nr_adb_args = 0;
while (adb_arg_p(argv, nr_adb_args + 1))
nr_adb_args += 1;
unsigned non_adb_off = 1 + nr_adb_args;
char* prgarg = argv[non_adb_off];
if (prgarg == NULL)
die(EINVAL, "no sub-command given. Use --help for help.");
if (!strcmp(prgarg, "--version")) {
#ifdef HAVE_GIT_STAMP
xprintf(xstdout, "fb-adb %s git:%s\n",
PACKAGE_VERSION,
git_stamp);
#else
xprintf(xstdout, "fb-adb %s\n", PACKAGE_VERSION);
#endif
return 0;
}
if (!strcmp(prgarg, "-h") || !strcmp(prgarg, "--help"))
prgarg = "help";
const struct cmd* cmd = &autocmds[0];
while (cmd->main != NULL) {
if (!strcmp(cmd->name, prgarg))
break;
cmd++;
}
if (cmd->main != NULL) {
char* new_argv0 = xaprintf("%s %s", prgname, prgarg);
set_prgname(new_argv0);
argv[0] = new_argv0;
memmove(&argv[non_adb_off],
&argv[non_adb_off+1],
sizeof (*argv) * (argc - nr_adb_args - 1));
return cmd->main(argc - 1, (const char**) argv);
}
die(EINVAL, "unknown command %s", prgarg);
}