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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
/*
* SDB - Smart Development Bridge
*
* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact:
* Ho Namkoong <ho.namkoong@samsung.com>
* Yoonki Park <yoonki.park@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* - S-Core Co., Ltd
*
*/
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <dirent.h>
#include "sdb_constants.h"
#include "utils.h"
#include "sdb_client.h"
#include "strutils.h"
#include "auto_complete.h"
#include "file_sync_service.h"
#include "log.h"
static int parse_opt(int argc, char** argv);
static int parse_cmd(int argc, char** argv);
static int parse_emul(int argc, char** argv);
static int parse_dev(int argc, char** argv);
static int parse_serial(int argc, char** argv);
static int no_parse(int argc, char** argv);
static int parse_root(int argc, char** argv);
static int parse_install(int argc, char** argv);
static int parse_uninstall(int argc, char** argv);
static int parse_disconnect(int argc, char** argv);
static int parse_push(int argc, char** argv);
static int parse_pull(int argc, char** argv);
static int parse_shell(int argc, char** argv);
static int initialize_ac(int complete);
static void print_char_with_completion_flag(int argc, char** argv, char** not_complete_char);
static void print_element_with_completion_flag(int argc, AC_ELEMENT** argv, char** not_complete_char);
static void print_local_dirlist_with_complete_flag(int argc, char** argv);
static void print_local_dirlist(char* src_dir, char** not_complete_char);
static void print_remote_dirlist_with_complete_flag(int argc, char** argv);
static void print_remote_dirlist(char* src_dir, char** not_complete_char);
static int COMPLETE_FLAG = 0;
static FILE* AC_STDOUT = NULL;
static FILE* AC_STDERR = NULL;
static const char IFS = '\n';
static const char PATH_SEPARATOR = '\/';
static struct ac_element emulator_short = {
.keyword = "-e",
.func = parse_emul
};
static struct ac_element emulator_long = {
.keyword = "--emulator",
.func = parse_emul
};
static struct ac_element device_short = {
.keyword = "-d",
.func = parse_dev
};
static struct ac_element device_long = {
.keyword = "--device",
.func = parse_dev
};
static struct ac_element serial_short = {
.keyword = "-s",
.func = parse_serial
};
static struct ac_element serial_long = {
.keyword = "--serial",
.func = parse_serial
};
static struct ac_element ac_root = {
.keyword = "root",
.func = parse_root
};
static struct ac_element ac_swindow = {
.keyword = "status-window",
.func = no_parse
};
static struct ac_element ac_gserial = {
.keyword = "get-serialno",
.func = no_parse
};
static struct ac_element ac_gstate = {
.keyword = "get-state",
.func = no_parse
};
static struct ac_element ac_kserver = {
.keyword = "kill-server",
.func = no_parse
};
static struct ac_element ac_sserver = {
.keyword = "start-server",
.func = no_parse
};
static struct ac_element ac_version = {
.keyword = "version",
.func = no_parse
};
static struct ac_element ac_help = {
.keyword = "help",
.func = no_parse
};
static struct ac_element ac_forward = {
.keyword = "forward",
.func = no_parse
};
static struct ac_element ac_uninstall= {
.keyword = "uninstall",
.func = parse_uninstall
};
static struct ac_element ac_install= {
.keyword = "install",
.func = parse_install
};
static struct ac_element ac_dlog= {
.keyword = "dlog",
.func = no_parse
};
static struct ac_element ac_shell= {
.keyword = "shell",
.func = parse_shell
};
static struct ac_element ac_pull= {
.keyword = "pull",
.func = parse_pull
};
static struct ac_element ac_push= {
.keyword = "push",
.func = parse_push
};
static struct ac_element ac_disconnect= {
.keyword = "disconnect",
.func = parse_disconnect
};
static struct ac_element ac_connect= {
.keyword = "connect",
.func = no_parse
};
static struct ac_element ac_devices= {
.keyword = "devices",
.func = no_parse
};
static const AC_ELEMENT* pre_options[] = {&emulator_short, &emulator_long, &device_short, &device_long, &serial_short, &serial_long};
static const int pre_options_size = GET_ARRAY_SIZE(pre_options, AC_ELEMENT*);
static const AC_ELEMENT* commands[] = {&ac_root, &ac_swindow , &ac_gserial, &ac_gstate, &ac_kserver, &ac_sserver,
&ac_version, &ac_help, &ac_forward, &ac_uninstall, &ac_install, &ac_dlog, &ac_shell, &ac_pull, &ac_push, &ac_disconnect,
&ac_connect, &ac_devices};
static const int cmds_size = GET_ARRAY_SIZE(commands, AC_ELEMENT*);
static int initialize_ac(int complete) {
COMPLETE_FLAG = complete;
int ac_stdout_fd = dup(STDOUT_FILENO);
if(ac_stdout_fd < 0) {
fprintf(stderr, "error: exception happend while duplicating stdout '%s'\n", strerror(errno));
return -1;
}
AC_STDOUT = fdopen(ac_stdout_fd, "w");
close_on_exec(ac_stdout_fd);
int ac_stderr_fd = dup(STDOUT_FILENO);
if(ac_stderr_fd < 0) {
fprintf(stderr, "error: exception happend while duplicating stdout '%s'\n", strerror(errno));
return -1;
}
AC_STDERR = fdopen(ac_stderr_fd, "w");
close_on_exec(ac_stderr_fd);
int null_fd;
null_fd = unix_open("/dev/null", O_WRONLY);
if(null_fd < 0) {
sdb_close(null_fd);
fprintf(stderr, "error: exception happend while opening /dev/null '%s'\n", strerror(errno));
return -1;
}
if(dup2(null_fd, STDOUT_FILENO) < 0){
sdb_close(null_fd);
fprintf(stderr, "error: exception happend while duplicating /dev/null to the stdout '%s'\n", strerror(errno));
return -1;
}
if(dup2(null_fd, STDERR_FILENO) < 0){
sdb_close(null_fd);
fprintf(stderr, "error: exception happend while duplicating /dev/null to the stderr '%s'\n", strerror(errno));
return -1;
}
sdb_close(null_fd);
return 0;
}
int auto_complete(int argc, char** argv, int complete) {
if(initialize_ac(complete)) {
return -1;
}
if(!parse_opt(argc, argv)) {
return 1;
}
parse_cmd(argc, argv);
return 1;
}
static int parse_emul(int argc, char** argv) {
target_ttype = kTransportLocal;
parse_cmd(argc, argv);
return -1;
}
static int parse_dev(int argc, char** argv) {
target_ttype = kTransportUsb;
parse_cmd(argc, argv);
return -1;
}
static int parse_serial(int argc, char** argv) {
if(argc == 0) {
char* tmp = sdb_query("host:devices");
if(tmp) {
char* tokens[MAX_TOKENS];
int devices_number = tokenize(tmp, "\n", tokens, MAX_TOKENS);
int i = 0;
for(; i<devices_number; i++) {
char* tab = strchr(tokens[i], '\t');
if(tab != NULL) {
*tab = '\0';
}
rtrim(tokens[i]);
}
print_char_with_completion_flag(devices_number, tokens, argv);
}
return 0;
}
target_serial = argv[0];
parse_cmd(argc - 1, argv + 1);
return -1;
}
static int no_parse(int argc, char** argv) {
return 0;
}
static int parse_root(int argc, char** argv) {
if(argc == 0) {
char* root_options[] = {"on", "off"};
int ropt_size = GET_ARRAY_SIZE(root_options, char*);
print_char_with_completion_flag(ropt_size, (char**)root_options, argv);
}
return -1;
}
static int parse_uninstall(int argc, char** argv) {
if(argc == 0) {
char full_cmd[255];
if(COMPLETE_FLAG) {
snprintf(full_cmd, sizeof full_cmd, "shell:/usr/bin/pkgcmd -l | grep -E '\\[tpk\\]|\\[wgt\\]' | awk '{print $4}'");
}
else {
snprintf(full_cmd, sizeof full_cmd, "shell:/usr/bin/pkgcmd -l | grep -E '\\[tpk\\]|\\[wgt\\]' | awk '{print $4}' | grep '^\\[%s'", argv[0]);
}
int result = sdb_connect(full_cmd);
if(result < 0) {
return -1;
}
char pkg_ids[10000];
int lines = read_lines(result, pkg_ids, 10000);
char** pkg_id_tokens = malloc(sizeof(char*) * lines);
int pkg_id_number = tokenize(pkg_ids, "\n", pkg_id_tokens, lines);
int i = 0;
for(; i<pkg_id_number; i++) {
if(pkg_id_tokens[i][0] == '[') {
pkg_id_tokens[i]++;
char* end = strchr(pkg_id_tokens[i], ']');
if(end != NULL) {
*end = '\0';
fprintf(AC_STDOUT, "%s%c", pkg_id_tokens[i], IFS);
}
}
}
free(pkg_id_tokens);
}
return -1;
}
static int parse_disconnect(int argc, char** argv) {
if(argc == 0) {
char* tmp = sdb_query("host:remote_emul");
if(tmp) {
char* tokens[MAX_TOKENS];
int devices_number = tokenize(tmp, "\n", tokens, MAX_TOKENS);
int i = 0;
for(; i<devices_number; i++) {
char* tab = strchr(tokens[i], '\t');
if(tab != NULL) {
*tab = '\0';
}
rtrim(tokens[i]);
}
print_char_with_completion_flag(devices_number, tokens, argv);
}
return 0;
}
return -1;
}
static int parse_install(int argc, char** argv) {
if(argc == 0) {
print_local_dirlist_with_complete_flag(argc, argv);
}
return -1;
}
static int parse_push(int argc, char** argv) {
if(argc == 0) {
print_local_dirlist_with_complete_flag(argc, argv);
}
else if(argc == 1) {
argc--;
argv++;
print_remote_dirlist_with_complete_flag(argc, argv);
}
return -1;
}
static int parse_pull(int argc, char** argv) {
if(argc == 0) {
print_remote_dirlist_with_complete_flag(argc, argv);
}
else if(argc == 1) {
argc--;
argv++;
print_local_dirlist_with_complete_flag(argc, argv);
}
return -1;
}
static int parse_shell(int argc, char** argv) {
//TODO
return -1;
}
static int parse_cmd(int argc, char** argv) {
if(argc == 0) {
print_element_with_completion_flag(cmds_size, (AC_ELEMENT**)commands, argv);
return -1;
}
int i = 0;
for(; i<cmds_size; i++) {
if(!strcmp(commands[i]->keyword, argv[0])) {
commands[i]->func(--argc, ++argv);
return 0;
}
}
return -1;
}
static int parse_opt(int argc, char** argv) {
if(argc == 0) {
print_element_with_completion_flag(pre_options_size, (AC_ELEMENT**)pre_options, argv);
return -1;
}
int i = 0;
for(; i<pre_options_size; i++) {
if(!strcmp(pre_options[i]->keyword, argv[0])) {
pre_options[i]->func(--argc, ++argv);
return 0;
}
}
return -1;
}
static void print_element_with_completion_flag(int argc, AC_ELEMENT** argv, char** not_complete_char) {
int i = 0;
if(COMPLETE_FLAG) {
for(; i<argc; i++) {
fprintf(AC_STDOUT, "%s%c", argv[i]->keyword, IFS);
}
}
else {
int len = strnlen(not_complete_char[0], 255);
for(; i<argc; i++) {
if(!strncmp(not_complete_char[0], argv[i]->keyword, len)) {
fprintf(AC_STDOUT, "%s%c", argv[i]->keyword, IFS);
}
}
}
}
static void print_char_with_completion_flag(int argc, char** argv, char** not_complete_char) {
int i = 0;
if(COMPLETE_FLAG) {
for(; i<argc; i++) {
fprintf(AC_STDOUT, "%s%c", argv[i], IFS);
}
}
else {
int len = strnlen(not_complete_char[0], 255);
for(; i<argc; i++) {
if(!strncmp(not_complete_char[0], argv[i], len)) {
fprintf(AC_STDOUT, "%s%c", argv[i], IFS);
}
}
}
}
static void print_local_dirlist_with_complete_flag(int argc, char** argv) {
if(COMPLETE_FLAG) {
print_local_dirlist(NULL, argv);
}
else {
char* src = strndup(argv[0], PATH_MAX);
char* last = strlchr(src, '/');
if(last != NULL) {
*(++last) = '\0';
print_local_dirlist(src, argv);
}
else {
print_local_dirlist(NULL, argv);
}
free(src);
}
}
static void print_local_dirlist(char* src_dir, char** not_complete_char) {
DIR* d;
int pwd_flag = 0;
if(src_dir == NULL) {
pwd_flag = 1;
src_dir = strdup("./");
}
d = opendir(src_dir);
if(d == 0) {
goto finalize;
}
struct dirent* de;
struct stat statbuf;
while((de = readdir(d))) {
char* file_name = de->d_name;
if(file_name[0] == '.') {
if(file_name[1] == 0) {
continue;
}
if((file_name[1] == '.') && (file_name[2] == 0)) {
continue;
}
}
char src_full_path[PATH_MAX];
append_file(src_full_path, src_dir, file_name, PATH_MAX);
// get file stat
if(stat(src_full_path, &statbuf) == -1)
{
fprintf(stderr, "error: exception occurred while getting file stat: %s\n", src_full_path);
goto finalize;
}
char* src_ptr = src_full_path;
if(pwd_flag) {
src_ptr += 2;
}
if(COMPLETE_FLAG) {
fprintf(AC_STDOUT, "%s%c", src_ptr, IFS);
}
else {
int len = strnlen(not_complete_char[0], PATH_MAX);
if(!strncmp(not_complete_char[0], src_ptr, len)) {
if(S_ISDIR(statbuf.st_mode))
fprintf(AC_STDOUT, "%s%c%c", src_ptr, PATH_SEPARATOR, IFS);
else
fprintf(AC_STDOUT, "%s%c", src_ptr, IFS);
}
}
}
finalize:
closedir(d);
if(pwd_flag) {
free(src_dir);
}
}
static void print_remote_dirlist_with_complete_flag(int argc, char** argv) {
if(COMPLETE_FLAG) {
print_remote_dirlist("/", argv);
}
else {
char* src = strndup(argv[0], PATH_MAX);
char* last = strlchr(src, '/');
if(last != NULL) {
*(++last) = '\0';
print_remote_dirlist(src, argv);
}
free(src);
}
}
static void print_remote_dirlist(char* src_dir, char** not_complete_char) {
int fd = sdb_connect("sync:");
if(fd < 0) {
goto finalize;
}
int len;
len = strnlen(src_dir, SYNC_CHAR_MAX + 1);
if(len > SYNC_CHAR_MAX) {
goto finalize;
}
SYNC_MSG msg;
msg.req.id = sync_list;
msg.req.namelen = htoll(len);
if(writex(fd, &msg.req, sizeof(msg.req)) ||
writex(fd, src_dir, len)) {
goto finalize;
}
while(1) {
if(readx(fd, &msg.dent, sizeof(msg.dent)) ||
msg.dent.id == sync_done ||
msg.dent.id != sync_dent) {
goto finalize;
}
len = ltohl(msg.dent.namelen);
if(len > 256) {
goto finalize;
}
char file_name[257];
if(readx(fd, file_name, len)) {
goto finalize;
}
file_name[len] = 0;
if(file_name[0] == '.') {
if(file_name[1] == 0) {
continue;
}
if((file_name[1] == '.') && (file_name[2] == 0)) {
continue;
}
}
char src_full_path[PATH_MAX];
append_file(src_full_path, src_dir, file_name, PATH_MAX);
if(COMPLETE_FLAG) {
fprintf(AC_STDOUT, "%s%c", src_full_path, IFS);
}
else {
int len = strnlen(not_complete_char[0], PATH_MAX);
if(!strncmp(not_complete_char[0], src_full_path, len)) {
if(S_ISDIR(msg.dent.mode))
fprintf(AC_STDOUT, "%s%c%c", src_full_path, PATH_SEPARATOR, IFS);
else
fprintf(AC_STDOUT, "%s%c", src_full_path, IFS);
}
}
}
finalize:
msg.req.id = sync_quit;
msg.req.namelen = 0;
writex(fd, &msg.req, sizeof(msg.req));
sdb_close(fd);
}
|