diff options
author | ho.namkoong <ho.namkoong@samsung.com> | 2013-12-06 19:48:18 +0900 |
---|---|---|
committer | ho.namkoong <ho.namkoong@samsung.com> | 2013-12-11 17:28:49 +0900 |
commit | 16e2bd44b6669e7f49a506bc7dbc0ef62443331e (patch) | |
tree | be3fda5b5164573fd7d867b8998044da5eeeba94 | |
parent | 931f131211f2a545811732678730c84f50873342 (diff) | |
download | sdb-16e2bd44b6669e7f49a506bc7dbc0ef62443331e.tar.gz sdb-16e2bd44b6669e7f49a506bc7dbc0ef62443331e.tar.bz2 sdb-16e2bd44b6669e7f49a506bc7dbc0ef62443331e.zip |
SDB: Implement sdb autocomplete
Add sdb tab autocomplete feature in Linux
Change-Id: If3cca4389d595fe81ee2a005ea4a6a6bc0df2cc4
Signed-off-by: ho.namkoong <ho.namkoong@samsung.com>
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | package/sdb-autocomplete | 24 | ||||
-rw-r--r-- | src/auto_complete.c | 624 | ||||
-rw-r--r-- | src/auto_complete.h | 39 | ||||
-rw-r--r-- | src/command_function.c | 135 | ||||
-rw-r--r-- | src/command_function.h | 42 | ||||
-rwxr-xr-x | src/commandline.c | 34 | ||||
-rw-r--r-- | src/commandline.h | 6 | ||||
-rw-r--r-- | src/file_sync_client.c | 6 | ||||
-rw-r--r-- | src/file_sync_client.h | 2 | ||||
-rw-r--r-- | src/file_sync_functions.c | 59 | ||||
-rw-r--r-- | src/file_sync_functions.h | 6 | ||||
-rwxr-xr-x | src/sdb.c | 20 | ||||
-rw-r--r-- | src/sdb_client.c | 47 | ||||
-rw-r--r-- | src/sdb_client.h | 17 | ||||
-rw-r--r-- | src/sdb_model.c | 4 | ||||
-rw-r--r-- | src/sdb_model.h | 6 | ||||
-rwxr-xr-x | src/sockets.c | 11 | ||||
-rwxr-xr-x | src/strutils.c | 53 | ||||
-rw-r--r-- | src/strutils.h | 7 | ||||
-rwxr-xr-x | src/transport.c | 31 | ||||
-rwxr-xr-x | src/transport.h | 2 |
22 files changed, 968 insertions, 211 deletions
@@ -19,7 +19,7 @@ endif ifeq ($(HOST_OS),linux) LOCAL_USB_SRC := src/usb_linux.c LOCAL_UTIL_SRC := src/utils_unix.c - LOCAL_OTHER_SRC := src/fdevent.c src/fdevent_unix.c + LOCAL_OTHER_SRC := src/fdevent.c src/fdevent_unix.c src/auto_complete.c LOCAL_LFLAGS := -lrt -lpthread -ludev LOCAL_CFLAGS := -DOS_LINUX -DHAVE_FORKEXEC -DHAVE_TERMIO_H -DHAVE_SYMLINKS -DSDB_HOST=1 -DSDB_HOST_ON_TARGET=1 -D_FILE_OFFSET_BITS=64 endif @@ -27,7 +27,7 @@ endif ifeq ($(HOST_OS),darwin) LOCAL_USB_SRC := src/usb_darwin.c LOCAL_UTIL_SRC := src/utils_unix.c - LOCAL_OTHER_SRC := src/fdevent.c src/fdevent_unix.c + LOCAL_OTHER_SRC := src/fdevent.c src/fdevent_unix.c src/auto_complete.c LOCAL_LFLAGS := -lpthread -framework CoreFoundation -framework IOKit -framework Carbon LOCAL_CFLAGS := -DOS_DARWIN -DHAVE_FORKEXEC -DHAVE_TERMIO_H -DHAVE_SYMLINKS -mmacosx-version-min=10.4 -DSDB_HOST=1 -DSDB_HOST_ON_TARGET=1 endif diff --git a/package/sdb-autocomplete b/package/sdb-autocomplete new file mode 100644 index 0000000..2a9d0e6 --- /dev/null +++ b/package/sdb-autocomplete @@ -0,0 +1,24 @@ +_sdb() +{ + SDB_DIR=/home/nkho/work/public/sdb + SDB_AUTOCOMPLETE=${SDB_DIR}/bin/sdb + COMPREPLY=() +# cur="${COMP_WORDS[COMP_CWORD]}" + ARGS="autocomplete,${COMP_CWORD}" + + local IFS=$',' + for ((i=1; i < $((${COMP_CWORD} + 1)) ; i++)) + do + convertedarg=$(eval eval echo \$\{COMP_WORDS\[i\]\}) + ARGS="${ARGS}${IFS}${convertedarg}" + done + + next=($(${SDB_AUTOCOMPLETE} ${ARGS})) + local IFS=$'\n' + COMPREPLY=(${next}) +# COMPREPLY=($(compgen -W "${next}" -- ${cur})) + + return 0 +} + +complete -o filenames -F _sdb sdb diff --git a/src/auto_complete.c b/src/auto_complete.c new file mode 100644 index 0000000..7b1de67 --- /dev/null +++ b/src/auto_complete.c @@ -0,0 +1,624 @@ +/* +* 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" + +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 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 = "install", + .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) { + fprintf(stderr, "error: exception happend while opening /dev/null '%s'\n", strerror(errno)); + return -1; + } + + if(dup2(null_fd, STDOUT_FILENO) < 0){ + 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){ + fprintf(stderr, "error: exception happend while duplicating /dev/null to the stderr '%s'\n", strerror(errno)); + return -1; + } + + 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); + } + } + } + } + + 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); + no_parse(--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; + + 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); + + 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], 255); + if(!strncmp(not_complete_char[0], src_ptr, len)) { + 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); + + if(COMPLETE_FLAG) { + fprintf(AC_STDOUT, "%s%c", src_full_path, IFS); + } + else { + int len = strnlen(not_complete_char[0], 255); + if(!strncmp(not_complete_char[0], src_full_path, len)) { + 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); +} diff --git a/src/auto_complete.h b/src/auto_complete.h new file mode 100644 index 0000000..64f8ac6 --- /dev/null +++ b/src/auto_complete.h @@ -0,0 +1,39 @@ +/* +* 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 +* +*/ + +#ifndef AUTO_COMPLETE_H_ +#define AUTO_COMPLETE_H_ + +typedef struct ac_element AC_ELEMENT; +struct ac_element { + char* keyword; + int (*func)(int argc, char** argv); +}; + + +int auto_complete(int argc, char** argv, int complete); + +#endif /* AUTO_COMPLETE_H_ */ diff --git a/src/command_function.c b/src/command_function.c index d588ecc..32acb56 100644 --- a/src/command_function.c +++ b/src/command_function.c @@ -48,16 +48,16 @@ static const char *APP_PATH_PREFIX="/opt/apps"; static void __inline__ format_host_command(char* buffer, size_t buflen, const char* command, transport_type ttype, const char* serial); static int get_pkgtype_file_name(const char* file_name); -static int get_pkgtype_from_app_id(const char* app_id, void** extargv); -static int kill_gdbserver_if_running(const char* process_cmd, void** extargv); -static int verify_gdbserver_exist(void** extargv); +static int get_pkgtype_from_app_id(const char* app_id); +static int kill_gdbserver_if_running(const char* process_cmd); +static int verify_gdbserver_exist(); -int da(int argc, char ** argv, void** extargv) { +int da(int argc, char ** argv) { char full_cmd[PATH_MAX] = "shell:/usr/bin/da_command"; append_args(full_cmd, --argc, (const char**)++argv, PATH_MAX-1); D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - int result = __sdb_command(full_cmd, extargv); + int result = __sdb_command(full_cmd); if(result < 0) { return 1; @@ -65,12 +65,12 @@ int da(int argc, char ** argv, void** extargv) { return 0; } -int oprofile(int argc, char ** argv, void** extargv) { +int oprofile(int argc, char ** argv) { char full_cmd[PATH_MAX] = "shell:/usr/bin/oprofile_command"; append_args(full_cmd, --argc, (const char**)++argv, PATH_MAX- 1); D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - int result = __sdb_command(full_cmd, extargv); + int result = __sdb_command(full_cmd); if(result < 0) { return 1; @@ -78,7 +78,7 @@ int oprofile(int argc, char ** argv, void** extargv) { return 0; } -int launch(int argc, char ** argv, void** extargv) { +int launch(int argc, char ** argv) { int i; int result = 0; char pkgid[11] = {0,}; @@ -97,7 +97,7 @@ int launch(int argc, char ** argv, void** extargv) { return -1; } - if(SDB_HIGHER_THAN_2_2_3(extargv)) { + if(SDB_HIGHER_THAN_2_2_3()) { int full_len = PATH_MAX - 1; strncat(fullcommand, WHITE_SPACE, full_len); strncat(fullcommand, SDB_LAUNCH_SCRIPT, full_len); @@ -106,7 +106,7 @@ int launch(int argc, char ** argv, void** extargv) { strncat(fullcommand, argv[i], full_len); } - return __sdb_command(fullcommand, extargv); + return __sdb_command(fullcommand); } for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-p")) { @@ -219,7 +219,7 @@ int launch(int argc, char ** argv, void** extargv) { strncat(fullcommand, buf, sizeof(fullcommand)-1); } } else if (mode == 1) { - if (verify_gdbserver_exist(extargv) < 0) { + if (verify_gdbserver_exist() < 0) { return -1; } if (!port) { @@ -231,7 +231,7 @@ int launch(int argc, char ** argv, void** extargv) { } else { snprintf(buf, sizeof(buf), "%s/gdbserver/gdbserver :%d %s/%s/bin/%s", SDK_TOOL_PATH, port, APP_PATH_PREFIX, pkgid, exe); } - if (kill_gdbserver_if_running(buf, extargv) < 0) { + if (kill_gdbserver_if_running(buf) < 0) { fprintf(stderr, "Gdbserver is already running on your target.\nAn gdb is going to connect the previous gdbserver process.\n"); return -1; } @@ -243,19 +243,19 @@ int launch(int argc, char ** argv, void** extargv) { } D("launch command: [%s]\n", fullcommand); - result = __sdb_command(fullcommand, extargv); + result = __sdb_command(fullcommand); sdb_close(result); return result; } -int devices(int argc, char ** argv, void** extargv) { +int devices(int argc, char ** argv) { char *tmp; char full_cmd[PATH_MAX]; snprintf(full_cmd, sizeof full_cmd, "host:%s", argv[0]); D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - tmp = sdb_query(full_cmd, extargv); + tmp = sdb_query(full_cmd); if(tmp) { printf("List of devices attached \n"); printf("%s", tmp); @@ -265,7 +265,7 @@ int devices(int argc, char ** argv, void** extargv) { } } -int __disconnect(int argc, char ** argv, void** extargv) { +int __disconnect(int argc, char ** argv) { char full_cmd[PATH_MAX]; char* tmp; @@ -275,7 +275,7 @@ int __disconnect(int argc, char ** argv, void** extargv) { snprintf(full_cmd, sizeof full_cmd, "host:disconnect:"); } D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - tmp = sdb_query(full_cmd, extargv); + tmp = sdb_query(full_cmd); if(tmp) { printf("%s\n", tmp); return 0; @@ -284,12 +284,12 @@ int __disconnect(int argc, char ** argv, void** extargv) { } } -int __connect(int argc, char ** argv, void** extargv) { +int __connect(int argc, char ** argv) { char full_cmd[PATH_MAX]; char * tmp; snprintf(full_cmd, sizeof full_cmd, "host:connect:%s", argv[1]); D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - tmp = sdb_query(full_cmd, extargv); + tmp = sdb_query(full_cmd); if(tmp) { printf("%s\n", tmp); return 0; @@ -298,14 +298,14 @@ int __connect(int argc, char ** argv, void** extargv) { } } -int device_con(int argc, char ** argv, void** extargv) { +int device_con(int argc, char ** argv) { char *tmp; char full_cmd[PATH_MAX]; snprintf(full_cmd, sizeof full_cmd, "host:device_con:%s:%s", argv[1], argv[2]); D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - tmp = sdb_query(full_cmd, extargv); + tmp = sdb_query(full_cmd); if(tmp != NULL) { printf("%s", tmp); @@ -315,12 +315,12 @@ int device_con(int argc, char ** argv, void** extargv) { return 1; } -int get_state_serialno(int argc, char ** argv, void** extargv) { +int get_state_serialno(int argc, char ** argv) { char full_cmd[PATH_MAX]; - format_host_command(full_cmd, sizeof full_cmd, argv[0], *(transport_type *)extargv[1], (char*)extargv[0]); + format_host_command(full_cmd, sizeof full_cmd, argv[0], target_ttype, target_serial); LOG_INFO(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - char* tmp = sdb_query(full_cmd, extargv); + char* tmp = sdb_query(full_cmd); if(tmp) { printf("%s\n", tmp); return 0; @@ -329,11 +329,11 @@ int get_state_serialno(int argc, char ** argv, void** extargv) { } } -int root(int argc, char ** argv, void** extargv) { +int root(int argc, char ** argv) { char full_cmd[20]; snprintf(full_cmd, sizeof(full_cmd), "root:%s", argv[1]); D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - int fd = sdb_connect(full_cmd, extargv); + int fd = sdb_connect(full_cmd); if(fd >= 0) { read_and_dump(fd); @@ -343,9 +343,7 @@ int root(int argc, char ** argv, void** extargv) { return 1; } -int status_window(int argc, char ** argv, void** extargv) { - char* serial = (char *)extargv[0]; - transport_type* ttype = (transport_type*)extargv[1]; +int status_window(int argc, char ** argv) { char full_cmd[PATH_MAX]; char *state = 0; @@ -364,7 +362,7 @@ int status_window(int argc, char ** argv, void** extargv) { } #endif - format_host_command(full_cmd, sizeof full_cmd, "get-state", *ttype, serial); + format_host_command(full_cmd, sizeof full_cmd, "get-state", target_ttype, target_serial); for(;;) { sdb_sleep_ms(250); @@ -375,7 +373,7 @@ int status_window(int argc, char ** argv, void** extargv) { } D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - state = sdb_query(full_cmd, extargv); + state = sdb_query(full_cmd); if(state) { if(laststate && !strcmp(state,laststate)){ @@ -395,9 +393,9 @@ int status_window(int argc, char ** argv, void** extargv) { return 0; } -int kill_server(int argc, char ** argv, void** extargv) { +int kill_server(int argc, char ** argv) { int fd; - fd = _sdb_connect("host:kill", extargv); + fd = _sdb_connect("host:kill"); if(fd == -1) { fprintf(stderr,"* server not running *\n"); return 1; @@ -405,16 +403,15 @@ int kill_server(int argc, char ** argv, void** extargv) { return 0; } -int start_server(int argc, char ** argv, void** extargv) { - return sdb_connect("host:start-server", extargv); +int start_server(int argc, char ** argv) { + return sdb_connect("host:start-server"); } -int version(int argc, char ** argv, void** extargv) { - transport_type ttype = *(transport_type*)extargv[1]; +int version(int argc, char ** argv) { - if (ttype == kTransportUsb || ttype == kTransportLocal) { + if (target_ttype == kTransportUsb || target_ttype == kTransportLocal) { char* VERSION_QUERY ="shell:rpm -qa | grep sdbd"; - send_shellcommand(VERSION_QUERY, extargv); + send_shellcommand(VERSION_QUERY); } else { fprintf(stdout, "Smart Development Bridge version %d.%d.%d\n", SDB_VERSION_MAJOR, SDB_VERSION_MINOR, SDB_VERSION_PATCH); @@ -422,25 +419,23 @@ int version(int argc, char ** argv, void** extargv) { return 0; } -int forward(int argc, char ** argv, void** extargv) { - char* serial = (char *)extargv[0]; - transport_type ttype = *(transport_type*)extargv[1]; +int forward(int argc, char ** argv) { char full_cmd[PATH_MAX]; char prefix[NAME_MAX]; - get_host_prefix(prefix, NAME_MAX, ttype, serial, host); + get_host_prefix(prefix, NAME_MAX, target_ttype, target_serial, host); snprintf(full_cmd, sizeof full_cmd, "%sforward:%s;%s",prefix, argv[1], argv[2]); D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - if(sdb_command(full_cmd, extargv) < 0) { + if(sdb_command(full_cmd) < 0) { return 1; } return 0; } -int dlog(int argc, char ** argv, void** extargv) { - D("dlog with serial: %s\n", (char*)extargv[0]); +int dlog(int argc, char ** argv) { + D("dlog with serial: %s\n", target_serial); char full_cmd[PATH_MAX] = "shell:/usr/bin/dlogutil"; @@ -453,11 +448,11 @@ int dlog(int argc, char ** argv, void** extargv) { strncat(full_cmd, quoted_string, sizeof(full_cmd)-1); } - send_shellcommand(full_cmd, extargv); + send_shellcommand(full_cmd); return 0; } -int push(int argc, char ** argv, void** extargv) { +int push(int argc, char ** argv) { int i=0; int utf8 = 0; @@ -468,19 +463,19 @@ int push(int argc, char ** argv, void** extargv) { } for (i=1; i<argc-1; i++) { - do_sync_copy(argv[i], argv[argc-1], (FILE_FUNC*)&LOCAL_FILE_FUNC, (FILE_FUNC*)&REMOTE_FILE_FUNC, utf8, extargv); + do_sync_copy(argv[i], argv[argc-1], (FILE_FUNC*)&LOCAL_FILE_FUNC, (FILE_FUNC*)&REMOTE_FILE_FUNC, utf8); } return 0; } -int pull(int argc, char ** argv, void** extargv) { +int pull(int argc, char ** argv) { if (argc == 2) { - return do_sync_copy(argv[1], ".", (FILE_FUNC*)&REMOTE_FILE_FUNC, (FILE_FUNC*)&LOCAL_FILE_FUNC, 0, extargv); + return do_sync_copy(argv[1], ".", (FILE_FUNC*)&REMOTE_FILE_FUNC, (FILE_FUNC*)&LOCAL_FILE_FUNC, 0); } - return do_sync_copy(argv[1], argv[2], (FILE_FUNC*)&REMOTE_FILE_FUNC, (FILE_FUNC*)&LOCAL_FILE_FUNC, 0, extargv); + return do_sync_copy(argv[1], argv[2], (FILE_FUNC*)&REMOTE_FILE_FUNC, (FILE_FUNC*)&LOCAL_FILE_FUNC, 0); } -int shell(int argc, char ** argv, void** extargv) { +int shell(int argc, char ** argv) { char buf[4096]; int r; @@ -488,7 +483,7 @@ int shell(int argc, char ** argv, void** extargv) { if(argc < 2) { D("starting interactive shell\n"); - r = interactive_shell(extargv); + r = interactive_shell(); return r; } @@ -508,7 +503,7 @@ int shell(int argc, char ** argv, void** extargv) { } D("interactive shell loop. buff=%s\n", buf); - fd = sdb_connect(buf, extargv); + fd = sdb_connect(buf); if(fd >= 0) { D("about to read_and_dump(fd=%d)\n", fd); read_and_dump(fd); @@ -523,9 +518,9 @@ int shell(int argc, char ** argv, void** extargv) { return r; } -int forkserver(int argc, char** argv, void** extargv) { +int forkserver(int argc, char** argv) { if(!strcmp(argv[1], "server")) { - int r = sdb_main(1, get_server_port()); + int r = sdb_main(1, DEFAULT_SDB_PORT); return r; } else { @@ -534,7 +529,7 @@ int forkserver(int argc, char** argv, void** extargv) { } } -int install(int argc, char **argv, void** extargv) { +int install(int argc, char **argv) { char* srcpath = argv[1]; const char* filename = sdb_dirstop(srcpath); @@ -557,7 +552,7 @@ int install(int argc, char **argv, void** extargv) { } D("Push file: %s to %s\n", srcpath, destination); - if(do_sync_copy(srcpath, destination, (FILE_FUNC*)&LOCAL_FILE_FUNC, (FILE_FUNC*)&REMOTE_FILE_FUNC, 0, extargv)) { + if(do_sync_copy(srcpath, destination, (FILE_FUNC*)&LOCAL_FILE_FUNC, (FILE_FUNC*)&REMOTE_FILE_FUNC, 0)) { return 1; } @@ -572,26 +567,26 @@ int install(int argc, char **argv, void** extargv) { } D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - if(__sdb_command(full_cmd, extargv) < 0) { + if(__sdb_command(full_cmd) < 0) { return 1; } const char* SHELL_REMOVE_CMD = "shell:rm %s"; snprintf(full_cmd, sizeof full_cmd, SHELL_REMOVE_CMD, destination); D(COMMANDLINE_MSG_FULL_CMD, "remove", full_cmd); - if(__sdb_command(full_cmd, extargv) < 0) { + if(__sdb_command(full_cmd) < 0) { return 1; } return 0; } -int uninstall(int argc, char **argv, void** extargv) { +int uninstall(int argc, char **argv) { char* appid = argv[1]; const char* SHELL_UNINSTALL_CMD ="shell:/usr/bin/pkgcmd -u -t %s -n %s -q"; char full_cmd[PATH_MAX]; int result = 0; - int tpk = get_pkgtype_from_app_id(appid, extargv); + int tpk = get_pkgtype_from_app_id(appid); if(tpk == 1) { snprintf(full_cmd, sizeof full_cmd, SHELL_UNINSTALL_CMD, "tpk", appid); } @@ -602,7 +597,7 @@ int uninstall(int argc, char **argv, void** extargv) { return 1; } D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); - result = __sdb_command(full_cmd, extargv); + result = __sdb_command(full_cmd); if(result < 0) { return 1; @@ -633,13 +628,13 @@ static int get_pkgtype_file_name(const char* file_name) { } // Returns 0 if pkg type is wgt. Returns 1 if pkg type is tpk. Returns minus if exception happens. -static int get_pkgtype_from_app_id(const char* app_id, void** extargv) { +static int get_pkgtype_from_app_id(const char* app_id) { char* GET_PKG_TYPE_CMD = "shell:/usr/bin/pkgcmd -l | grep %s | awk '{print $2}'"; char full_cmd[PATH_MAX]; snprintf(full_cmd, sizeof full_cmd, GET_PKG_TYPE_CMD, app_id); - int result = sdb_connect(full_cmd, extargv); + int result = sdb_connect(full_cmd); if(result < 0) { return result; } @@ -687,13 +682,13 @@ static int get_pkgtype_from_app_id(const char* app_id, void** extargv) { * kill gdbserver if running */ -static int kill_gdbserver_if_running(const char* process_cmd, void** extargv) { +static int kill_gdbserver_if_running(const char* process_cmd) { char cmd[512] = {}; char buf[512] = {}; // hopefully, it is not going to happen, but check executable gdbserver is existed snprintf(cmd, sizeof(cmd), "shell:/usr/bin/da_command process | grep '%s' | grep -v grep | wc -l", process_cmd); - int result = sdb_connect(cmd, extargv); + int result = sdb_connect(cmd); if(result < 0) { return -1; @@ -728,12 +723,12 @@ static void __inline__ format_host_command(char* buffer, size_t buflen, const c /* * returns -1 if gdbserver exists */ -static int verify_gdbserver_exist(void** extargv) { +static int verify_gdbserver_exist() { char cmd[512] = {}; char buf[512] = {}; snprintf(cmd, sizeof(cmd), "shell:%s/gdbserver/gdbserver --version 1>/dev/null", SDK_TOOL_PATH); - int result = sdb_connect(cmd, extargv); + int result = sdb_connect(cmd); if(result < 0) { sdb_close(result); diff --git a/src/command_function.h b/src/command_function.h index 42816ee..7722eb3 100644 --- a/src/command_function.h +++ b/src/command_function.h @@ -34,26 +34,26 @@ #define PATH_MAX 4096 #endif -int da(int argc, char ** argv, void** extargv); -int oprofile(int argc, char ** argv, void** extargv); -int launch(int argc, char ** argv, void** extargv); -int devices(int argc, char ** argv, void** extargv); -int __disconnect(int argc, char ** argv, void** extargv); -int __connect(int argc, char ** argv, void** extargv); -int device_con(int argc, char ** argv, void** extargv); -int get_state_serialno(int argc, char ** argv, void** extargv); -int root(int argc, char ** argv, void** extargv); -int status_window(int argc, char ** argv, void** extargv); -int start_server(int argc, char ** argv, void** extargv); -int kill_server(int argc, char ** argv, void** extargv); -int version(int argc, char ** argv, void** extargv); -int forward(int argc, char ** argv, void** extargv); -int push(int argc, char ** argv, void** extargv); -int pull(int argc, char ** argv, void** extargv); -int dlog(int argc, char ** argv, void** extargv); -int install(int argc, char **argv, void** extargv); -int uninstall(int argc, char **argv, void** extargv); -int forkserver(int argc, char** argv, void** extargv); -int shell(int argc, char ** argv, void** extargv); +int da(int argc, char ** argv); +int oprofile(int argc, char ** argv); +int launch(int argc, char ** argv); +int devices(int argc, char ** argv); +int __disconnect(int argc, char ** argv); +int __connect(int argc, char ** argv); +int device_con(int argc, char ** argv); +int get_state_serialno(int argc, char ** argv); +int root(int argc, char ** argv); +int status_window(int argc, char ** argv); +int start_server(int argc, char ** argv); +int kill_server(int argc, char ** argv); +int version(int argc, char ** argv); +int forward(int argc, char ** argv); +int push(int argc, char ** argv); +int pull(int argc, char ** argv); +int dlog(int argc, char ** argv); +int install(int argc, char **argv); +int uninstall(int argc, char **argv); +int forkserver(int argc, char** argv); +int shell(int argc, char ** argv); #endif /* COMMAND_FUNCTION_H_ */ diff --git a/src/commandline.c b/src/commandline.c index 2558b45..907bbc4 100755 --- a/src/commandline.c +++ b/src/commandline.c @@ -148,11 +148,11 @@ static void *stdin_read_thread(void *x) return 0; } -int interactive_shell(void** extargv) +int interactive_shell() { sdb_thread_t thr; - int fd = sdb_connect("shell:", extargv); + int fd = sdb_connect("shell:"); if(fd < 0) { return 1; } @@ -178,19 +178,17 @@ int interactive_shell(void** extargv) return 0; } -int send_shellcommand(char* buf, void** extargv) +int send_shellcommand(char* buf) { int fd, ret; - char* serial = (char *)extargv[0]; - transport_type ttype = *(transport_type*)extargv[1]; for(;;) { - fd = sdb_connect(buf, extargv); + fd = sdb_connect(buf); if(fd >= 0) break; fprintf(stderr,"- waiting for device -\n"); sdb_sleep_ms(1000); - do_cmd(ttype, serial, "wait-for-device", 0); + do_cmd(target_ttype, target_serial, "wait-for-device", 0); } read_and_dump(fd); @@ -236,8 +234,8 @@ static int do_cmd(transport_type ttype, char* serial, char *cmd, ...) return process_cmdline(argc, argv); } -int __sdb_command(const char* cmd, void** extargv) { - int result = sdb_connect(cmd, extargv); +int __sdb_command(const char* cmd) { + int result = sdb_connect(cmd); if(result < 0) { return result; @@ -262,10 +260,6 @@ const char* get_basename(const char* filename) } } -int get_server_port() { - return DEFAULT_SDB_PORT; -} - static void create_opt_list(LIST_NODE** opt_list) { OPTION* serial = NULL; @@ -444,10 +438,6 @@ int process_cmdline(int argc, char** argv) { argc = argc - parsed_argc; argv = argv + parsed_argc; - int server_port = get_server_port(); - void* extraarg[3]; - extraarg[2] = &server_port; - INPUT_OPTION* opt_s = get_inputopt(input_opt_list, (char*)COMMANDLINE_SERIAL_SHORT_OPT); if(opt_s != NULL) { serial = opt_s->value; @@ -457,7 +447,7 @@ int process_cmdline(int argc, char** argv) { snprintf(buf, sizeof(buf), "host:serial-match:%s", serial); - tmp = sdb_query(buf, extraarg); + tmp = sdb_query(buf); if (tmp) { serial = strdup(tmp); } else { @@ -503,13 +493,13 @@ int process_cmdline(int argc, char** argv) { } return 1; } - extraarg[0] = serial; - extraarg[1] = &ttype; - int (*Func)(int, char**, void**) = command->Func; + target_serial = serial; + target_ttype = ttype; + int (*Func)(int, char**) = command->Func; free_list(cmd_list, NULL); free_list(input_opt_list, NULL); free_list(opt_list, NULL); - return Func(argc, argv, extraarg); + return Func(argc, argv); } print_help(opt_list, cmd_list); diff --git a/src/commandline.h b/src/commandline.h index 92486c9..0962070 100644 --- a/src/commandline.h +++ b/src/commandline.h @@ -43,11 +43,11 @@ #define INPUT_FD 0 #endif -int send_shellcommand(char* buf, void** extargv); +int send_shellcommand(char* buf); int process_cmdline(int argc, char** argv); void read_and_dump(int fd); -int interactive_shell(void** extargv); +int interactive_shell(); int get_server_port(); -int __sdb_command(const char* cmd, void** extargv); +int __sdb_command(const char* cmd); #endif /* COMMANDLINE_H_ */ diff --git a/src/file_sync_client.c b/src/file_sync_client.c index 6cdcbb6..4c8d991 100644 --- a/src/file_sync_client.c +++ b/src/file_sync_client.c @@ -154,7 +154,7 @@ static void free_copyinfo(void* data) { } } -int do_sync_copy(char* srcp, char* dstp, FILE_FUNC* srcF, FILE_FUNC* dstF, int is_utf8, void** ext_argv) { +int do_sync_copy(char* srcp, char* dstp, FILE_FUNC* srcF, FILE_FUNC* dstF, int is_utf8) { char copy_flag[7]; if(srcF->local) { @@ -173,8 +173,8 @@ int do_sync_copy(char* srcp, char* dstp, FILE_FUNC* srcF, FILE_FUNC* dstF, int i int pushed = 0; int skiped = 0; - src_fd = srcF->initialize(srcp, ext_argv); - dst_fd = dstF->initialize(dstp, ext_argv); + src_fd = srcF->initialize(srcp); + dst_fd = dstF->initialize(dstp); if(src_fd < 0 || dst_fd < 0) { return 1; } diff --git a/src/file_sync_client.h b/src/file_sync_client.h index 8381a1c..2d31386 100644 --- a/src/file_sync_client.h +++ b/src/file_sync_client.h @@ -30,6 +30,6 @@ #include "file_sync_functions.h" void create_copy_info(COPY_INFO** info, char* srcp, char* dstp); -int do_sync_copy(char* srcp, char* dstp, FILE_FUNC* srcF, FILE_FUNC* dstF, int is_utf8, void** ext_argv); +int do_sync_copy(char* srcp, char* dstp, FILE_FUNC* srcF, FILE_FUNC* dstF, int is_utf8); #endif /* FILE_SYNC_CLIENT_H_ */ diff --git a/src/file_sync_functions.c b/src/file_sync_functions.c index 385af7a..1628fe1 100644 --- a/src/file_sync_functions.c +++ b/src/file_sync_functions.c @@ -47,49 +47,49 @@ const unsigned sync_okay = MKSYNC('O','K','A','Y'); const unsigned sync_fail = MKSYNC('F','A','I','L'); const unsigned sync_quit = MKSYNC('Q','U','I','T'); -const FILE_FUNC LOCAL_FILE_FUNC = { - 1, - initialize_local, - finalize_local, - _stat_local, - is_directory_common, - readopen_local, - readclose_local, - writeopen_local, - writeclose_local, - readfile_local, - writefile_local, - getdirlist_local, +const struct file_function LOCAL_FILE_FUNC = { + .local = 1, + .initialize=initialize_local, + .finalize=finalize_local, + ._stat=_stat_local, + .is_dir=is_directory_common, + .readopen=readopen_local, + .readclose=readclose_local, + .writeopen=writeopen_local, + .writeclose=writeclose_local, + .readfile=readfile_local, + .writefile=writefile_local, + .get_dirlist=getdirlist_local, }; -const FILE_FUNC REMOTE_FILE_FUNC = { - 0, - initialize_remote, - finalize_remote, - _stat_remote, - is_directory_common, - readopen_remote, - readclose_remote, - writeopen_remote, - writeclose_remote, - readfile_remote, - writefile_remote, - getdirlist_remote, +const struct file_function REMOTE_FILE_FUNC = { + .local=0, + .initialize=initialize_remote, + .finalize=finalize_remote, + ._stat=_stat_remote, + .is_dir=is_directory_common, + .readopen=readopen_remote, + .readclose=readclose_remote, + .writeopen=writeopen_remote, + .writeclose=writeclose_remote, + .readfile=readfile_remote, + .writefile=writefile_remote, + .get_dirlist=getdirlist_remote, }; static int sync_readstat(int fd, const char *path, struct stat* st); //return > 0 fd, = 0 success, < 0 fail. -int initialize_local(char* path, void** extargv) { +int initialize_local(char* path) { D("initialize local file '%s'\n", path); return 0; } //return fd -int initialize_remote(char* path, void** extargv) { +int initialize_remote(char* path) { D("initialize remote file '%s'\n", path); - int fd = sdb_connect("sync:", extargv); + int fd = sdb_connect("sync:"); if(fd < 0) { return -1; @@ -546,7 +546,6 @@ int getdirlist_remote(int fd, char* src_dir, char* dst_dir, LIST_NODE** dirlist) COPY_INFO* info; create_copy_info(&info, src_full_path, dst_full_path); prepend(dirlist, info); - D("!!!!!!!!!!!!!!!!!!!\n"); } D("getting list of remote file 'fd:%d' '%s' is done\n", fd, src_dir); return fd; diff --git a/src/file_sync_functions.h b/src/file_sync_functions.h index 6897c90..2ea3e09 100644 --- a/src/file_sync_functions.h +++ b/src/file_sync_functions.h @@ -46,8 +46,8 @@ struct _file_buf { }; typedef struct _file_buf FILE_BUFFER; -int initialize_local(char* path, void** extargv); -int initialize_remote(char* path, void** extargv); +int initialize_local(char* path); +int initialize_remote(char* path); void finalize_local(int fd); void finalize_remote(int fd); @@ -80,7 +80,7 @@ int getdirlist_remote(int fd, char* src_dir, char* dst_dir, LIST_NODE** dirlist) struct file_function { int local; - int(*initialize)(char* path, void** extargv); + int(*initialize)(char* path); void(*finalize)(int fd); int(*_stat)(int fd, char* path, struct stat* st, int show_error); int(*is_dir)(char* path, struct stat* st, int show_error); @@ -32,6 +32,7 @@ #include "commandline.h" #include "sdb_constants.h" #include "listener.h" +#include "auto_complete.h" #if SDB_TRACE SDB_MUTEX_DEFINE( D_lock ); @@ -176,6 +177,25 @@ void init_wakeup_select_func() { int main(int argc, char **argv) { +#ifndef OS_WINDOWS + if(argc > 2 && !strcmp("autocomplete", argv[1])) { + + int complete_checker = atoi(argv[2]); + + if(complete_checker == 0) { + fprintf(stderr, "error:zero_complete_cheker\n"); + return -1; + } + if(complete_checker == argc - 3) { + return auto_complete(argc - 4, argv + 3, 0); + } + else if(complete_checker == argc - 2) { + return auto_complete(argc - 3, argv + 3, 1); + } + fprintf(stderr, "error:wrong_complete_cheker\n"); + return -1; + } +#endif log_init(); sdb_sysdeps_init(); init_map(); diff --git a/src/sdb_client.c b/src/sdb_client.c index 74f22ff..ff57cb9 100644 --- a/src/sdb_client.c +++ b/src/sdb_client.c @@ -31,9 +31,12 @@ #include "sdb_client.h" #include "log.h" -static int switch_socket_transport(int fd, void** extra_args); +static int switch_socket_transport(int fd); static int __inline__ write_msg_size(int fd, int size, int host_fd); +char* target_serial = NULL; +transport_type target_ttype = kTransportAny; + void sendokmsg(int fd, const char *msg) { char buf[1024]; @@ -94,14 +97,11 @@ int send_service_with_length(int fd, const char* service, int host_fd) { return 0; } -static int switch_socket_transport(int fd, void** extra_args) +static int switch_socket_transport(int fd) { - char* serial = (char *)extra_args[0]; - transport_type ttype = *(transport_type*)extra_args[1]; - char service[64]; - get_host_prefix(service, sizeof service, ttype, serial, transport); + get_host_prefix(service, sizeof service, target_ttype, target_serial, transport); if(send_service_with_length(fd, service, 0) < 0) { sdb_close(fd); @@ -119,10 +119,10 @@ static int switch_socket_transport(int fd, void** extra_args) return 0; } -int sdk_launch_exist(void* extargv) { +int sdk_launch_exist() { const char* SDK_LAUNCH_QUERY = "shell:ls /usr/sbin/sdk_launch"; D("query the existence of sdk_launch\n"); - int fd = sdb_connect(SDK_LAUNCH_QUERY, extargv); + int fd = sdb_connect(SDK_LAUNCH_QUERY); if(fd < 0) { D("fail to query the sdbd version\n"); @@ -159,11 +159,11 @@ int sdk_launch_exist(void* extargv) { return 0; } -int sdb_higher_ver(int first, int middle, int last, void* extargv) { +int sdb_higher_ver(int first, int middle, int last) { const char* VERSION_QUERY = "shell:rpm -q sdbd"; D("query the sdbd version\n"); - int fd = sdb_connect(VERSION_QUERY, extargv); + int fd = sdb_connect(VERSION_QUERY); if(fd < 0) { D("fail to query the sdbd version\n"); @@ -321,7 +321,7 @@ int sdb_status(int fd, int host_fd) * If transport service, send transport prefix. Then, do the service. * If host service, do the service. does not have to get transport. */ -int _sdb_connect(const char *service, void** ext_args) +int _sdb_connect(const char *service) { int fd; @@ -331,16 +331,14 @@ int _sdb_connect(const char *service, void** ext_args) return 0; } - int server_port = *(int*)ext_args[2]; - - fd = sdb_host_connect("127.0.0.1", server_port, SOCK_STREAM); + fd = sdb_host_connect("127.0.0.1", DEFAULT_SDB_PORT, SOCK_STREAM); if(fd < 0) { D("error: cannot connect to daemon\n"); return -2; } //If service is not host, send transport_prefix - if (memcmp(service,"host",4) != 0 && switch_socket_transport(fd, ext_args)) { + if (memcmp(service,"host",4) != 0 && switch_socket_transport(fd)) { return -1; } @@ -390,11 +388,10 @@ static int __inline__ write_msg_size(int fd, int size, int host_fd) { * First, check the host version. * Then, send the service using _sdb_connect */ -int sdb_connect(const char *service, void** ext_args) +int sdb_connect(const char *service) { // check version before sending a sdb command - int fd = _sdb_connect("host:version", ext_args); - int server_port = *(int*)ext_args[2]; + int fd = _sdb_connect("host:version"); D("sdb_connect: service %s\n", service); @@ -444,7 +441,7 @@ int sdb_connect(const char *service, void** ext_args) if (cnt) { free_strings(tokens, cnt); } - int fd2 = _sdb_connect("host:kill", ext_args); + int fd2 = _sdb_connect("host:kill"); sdb_close(fd2); sdb_sleep_ms(2000); goto launch_server; @@ -452,7 +449,7 @@ int sdb_connect(const char *service, void** ext_args) } if(fd == -2) { - fprintf(stdout,"* daemon not running. starting it now on port %d *\n", server_port); + fprintf(stdout,"* daemon not running. starting it now on port %d *\n", DEFAULT_SDB_PORT); launch_server: if(launch_server()) { fprintf(stderr,"* failed to start daemon *\n"); @@ -462,7 +459,7 @@ launch_server: } } - fd = _sdb_connect(service, ext_args); + fd = _sdb_connect(service); if(fd == -2) { fprintf(stderr,"** daemon still not running\n"); } @@ -472,9 +469,9 @@ launch_server: } -int sdb_command(const char *service, void** extra_args) +int sdb_command(const char *service) { - int fd = sdb_connect(service, extra_args); + int fd = sdb_connect(service); if(fd < 0) { return -1; } @@ -487,12 +484,12 @@ int sdb_command(const char *service, void** extra_args) return 0; } -char *sdb_query(const char *service, void** extra_args) +char *sdb_query(const char *service) { char *tmp; D("sdb_query: %s\n", service); - int fd = sdb_connect(service, extra_args); + int fd = sdb_connect(service); if(fd < 0) { return 0; } diff --git a/src/sdb_client.h b/src/sdb_client.h index 31be98e..f8690a2 100644 --- a/src/sdb_client.h +++ b/src/sdb_client.h @@ -21,7 +21,10 @@ #include "common_modules.h" // debug launch pad is applied after sdbd 2.2.3 -#define SDB_HIGHER_THAN_2_2_3(extargv) sdk_launch_exist(extargv) +#define SDB_HIGHER_THAN_2_2_3() sdk_launch_exist() + +extern char* target_serial; +extern transport_type target_ttype; int send_service_with_length(int fd, const char* service, int host_fd); int sdb_status(int fd, int host_fd); @@ -30,20 +33,20 @@ int sdb_status(int fd, int host_fd); ** a valid fd for interacting with that service upon success ** or a negative number on failure */ -int sdb_connect(const char *service, void** ext_args); -int _sdb_connect(const char *service, void** ext_args); +int sdb_connect(const char *service); +int _sdb_connect(const char *service); /* connect to sdb, connect to the named service, return 0 if ** the connection succeeded AND the service returned OKAY */ -int sdb_command(const char *service, void** ext_args); +int sdb_command(const char *service); /* connect to sdb, connect to the named service, return ** a malloc'd string of its response upon success or NULL ** on failure. */ -char *sdb_query(const char *service, void** ext_args); +char *sdb_query(const char *service); /* return verbose error string from last operation */ const char *sdb_error(void); @@ -53,7 +56,7 @@ const char *sdb_error(void); * returns true, if target version is higher then {first}.{middle}.{last}. * else, returns false. */ -int sdb_higher_ver(int first, int middle, int last, void* extargv); +int sdb_higher_ver(int first, int middle, int last); /** * check /usr/sbin/sdk_launch exists in the target. @@ -61,7 +64,7 @@ int sdb_higher_ver(int first, int middle, int last, void* extargv); * returns true, if sdbd contains it. * else, returns false. */ -int sdk_launch_exist(void* extargv); +int sdk_launch_exist(); /* read a standard sdb status response (OKAY|FAIL) and ** return 0 in the event of OKAY, -1 in the event of FAIL diff --git a/src/sdb_model.c b/src/sdb_model.c index 203def1..6adc071 100644 --- a/src/sdb_model.c +++ b/src/sdb_model.c @@ -45,7 +45,7 @@ const COMMAND NULL_COMMAND = { //Create a command. A user should free cmdptr manually. void create_command(COMMAND** cmdptr, const char* name, const char** desc, int desc_size, const char* argdesc, - int (*Func)(int, char**, void**), int maxargs, int minargs) { + int (*Func)(int, char**), int maxargs, int minargs) { *cmdptr = (COMMAND*)malloc(sizeof(COMMAND)); (*cmdptr)->name = name; (*cmdptr)->desc = desc; @@ -170,7 +170,7 @@ int parse_opt(int argc, char** argv, LIST_NODE* opt_list, LIST_NODE** result_lis return pass_arg; } -int null_function (int argc, char** argv, void** extraarg) { +int null_function (int argc, char** argv) { fprintf(stderr, "unsupported command: %s\n", argv[0]); return -1; } diff --git a/src/sdb_model.h b/src/sdb_model.h index 60c889a..201aaef 100644 --- a/src/sdb_model.h +++ b/src/sdb_model.h @@ -38,7 +38,7 @@ struct command { int desc_size; const char** desc; const char* argdesc; - int (*Func)(int, char**, void**); + int (*Func)(int, char**); // -1 means no max limit. int maxargs; int minargs; @@ -64,9 +64,9 @@ typedef struct input_option INPUT_OPTION; const COMMAND NULL_COMMAND; -int null_function (int argc, char** argv, void** extraarg); +int null_function (int argc, char** argv); void create_command(COMMAND** cmdptr, const char* name, const char** desc, int desc_size, const char* argdesc, - int (*Func)(int, char**, void**), int maxargs, int minargs); + int (*Func)(int, char**), int maxargs, int minargs); void create_option(OPTION** optptr, const char* longopt, const char* shortopt, const char** desc, int desc_size, const char* argdesc, int hasarg); COMMAND* get_command(LIST_NODE* cmd_list, char* name); diff --git a/src/sockets.c b/src/sockets.c index 0f85310..62dbc4c 100755 --- a/src/sockets.c +++ b/src/sockets.c @@ -845,9 +845,16 @@ static int handle_host_request(char *service, SDB_SOCKET* socket) free(serial); return 0; } - // return a list of all connected devices + // return a list of all devices if (!strcmp(service, "devices")) { - list_transports(cmd_buf, cbuf_size); + list_targets(cmd_buf, cbuf_size, kTransportAny); + sendokmsg(socket->fd, cmd_buf); + return 0; + } + + // return a list of all remote emulator + if (!strcmp(service, "remote_emul")) { + list_targets(cmd_buf, cbuf_size, kTransportConnect); sendokmsg(socket->fd, cmd_buf); return 0; } diff --git a/src/strutils.c b/src/strutils.c index d3d5417..062f959 100755 --- a/src/strutils.c +++ b/src/strutils.c @@ -15,7 +15,7 @@ size_t tokenize(const char *str, const char *delim, char *tokens[], size_t max_t strncpy(tmp, str, PATH_MAX - 1); char *p = strtok(tmp, delim); if (max_tokens < 1 || max_tokens > MAX_TOKENS) { - max_tokens = 1; + max_tokens = MAX_TOKENS; } if (p != NULL) { @@ -114,3 +114,54 @@ size_t s_strnlen(const char *s, size_t maxlen) { } return len; } + +char* strlchr(const char*s, int chr) { + if(s == NULL) { + return NULL; + } + int len = strnlen(s, PATH_MAX); + int i = len - 1; + for(; i>-1; i--) { + if(s[i] == chr) { + fflush(stdout); + return (char*)(s + i); + } + } + + return NULL; +} + +char* trim(char *s) { + rtrim(s); + return ltrim(s); +} + +void rtrim(char* s) { + + int len = strnlen(s, PATH_MAX) - 1; + char* t = s + len; + + for(; len > -1; len--) { + if(*t != ' ') { + *(t+1) = '\0'; + break; + } + t--; + } +} + +char* ltrim(char *s) { + char* begin; + begin = s; + + while (*begin != '\0') { + if (*begin == ' ') + begin++; + else { + s = begin; + break; + } + } + + return s; +} diff --git a/src/strutils.h b/src/strutils.h index ef8cd3b..f5f2234 100644 --- a/src/strutils.h +++ b/src/strutils.h @@ -1,7 +1,7 @@ #ifndef _STRUTILS_H_ #define _STRUTILS_H_ -#define MAX_TOKENS 30 +#define MAX_TOKENS 500 size_t tokenize(const char *str, const char *delim, char *tokens[], size_t max_tokens); void free_strings(char **array, int n); @@ -9,5 +9,10 @@ int read_lines(const int fd, char* ptr, const unsigned int maxlen); int read_line(const int fd, char* ptr, const unsigned int maxlen); char *s_strncpy(char *dest, const char *source, size_t n); size_t s_strnlen(const char *s, size_t maxlen); +//get the last character of the string +char* strlchr(const char*s, int chr); +char* trim(char *s); +char* ltrim(char *s); +void rtrim(char *s); #endif diff --git a/src/transport.c b/src/transport.c index cc0fc8f..91c3535 100755 --- a/src/transport.c +++ b/src/transport.c @@ -314,7 +314,7 @@ int list_transports_msg(char* buffer, size_t bufferlen) char head[5]; int len; - len = list_transports(buffer+4, bufferlen-4); + len = list_targets(buffer+4, bufferlen-4, kTransportAny); snprintf(head, sizeof(head), "%04x", len); memcpy(buffer, head, 4); len += 4; @@ -570,8 +570,8 @@ exit: return result; } -int list_transports(char *buf, size_t bufsize) -{ +int list_targets(char* buf, size_t bufsize, transport_type ttype) { + char* p = buf; char* end = buf + bufsize; int len; @@ -583,23 +583,26 @@ int list_transports(char *buf, size_t bufsize) while(curptr != NULL) { TRANSPORT* t = curptr->data; curptr = curptr->next_ptr; - const char* serial = t->serial; - const char* devicename = (t->device_name == NULL) ? DEFAULT_DEVICENAME : t->device_name; /* tizen specific */ - if (!serial || !serial[0]) - serial = "????????????"; - // FIXME: what if each string length is longger than static length? - len = snprintf(p, end - p, "%-20s\t%-10s\t%s\n", serial, connection_state_name(t), devicename); - - if (p + len >= end) { - /* discard last line if buffer is too short */ - break; + if(ttype == kTransportAny || ttype == t->type) { + const char* serial = t->serial; + const char* devicename = (t->device_name == NULL) ? DEFAULT_DEVICENAME : t->device_name; /* tizen specific */ + if (!serial || !serial[0]) + serial = "????????????"; + // FIXME: what if each string length is longger than static length? + len = snprintf(p, end - p, "%-20s\t%-10s\t%s\n", serial, connection_state_name(t), devicename); + + if (p + len >= end) { + /* discard last line if buffer is too short */ + break; + } + p += len; } - p += len; } p[0] = 0; sdb_mutex_unlock(&transport_lock, "transport list_transports"); return p - buf; + } int register_device_con_transport(int s, const char *serial) { diff --git a/src/transport.h b/src/transport.h index eaebeb8..f588c7d 100755 --- a/src/transport.h +++ b/src/transport.h @@ -63,7 +63,7 @@ int register_device_con_transport(int s, const char *serial); void send_cmd(unsigned arg0, unsigned arg1, unsigned cmd, char* data, TRANSPORT* t); void close_usb_devices(); int list_transports_msg(char* buffer, size_t bufferlen); -int list_transports(char *buf, size_t bufsize); +int list_targets(char* buf, size_t bufsize, transport_type ttype); int local_connect(int port, const char *device_name); TRANSPORT *acquire_one_transport(transport_type ttype, const char* serial, char **error_out); void kick_transport( TRANSPORT* t ); |