📄 commandline.c
字号:
snprintf(buffer, buflen, "host-serial:%s:%s", serial, command); } else { const char* prefix = "host"; if (ttype == kTransportUsb) prefix = "host-usb"; else if (ttype == kTransportLocal) prefix = "host-local"; snprintf(buffer, buflen, "%s:%s", prefix, command); }}static void status_window(transport_type ttype, const char* serial){ char command[4096]; char *state = 0; char *laststate = 0; /* silence stderr */#ifdef _WIN32 /* XXX: TODO */#else int fd; fd = unix_open("/dev/null", O_WRONLY); dup2(fd, 2); adb_close(fd);#endif format_host_command(command, sizeof command, "get-state", ttype, serial); for(;;) { adb_sleep_ms(250); if(state) { free(state); state = 0; } state = adb_query(command); if(state) { if(laststate && !strcmp(state,laststate)){ continue; } else { if(laststate) free(laststate); laststate = strdup(state); } } printf("%c[2J%c[2H", 27, 27); printf("Android Debug Bridge\n"); printf("State: %s\n", state ? state : "offline"); fflush(stdout); }}/** duplicate string and quote all \ " ( ) chars */static char *dupAndQuote(const char *s){ const char *ts; size_t alloc_len; char *ret; char *dest; ts = s; alloc_len = 0; for( ;*ts != '\0'; ts++) { alloc_len++; if (*ts == '"' || *ts == '\\') { alloc_len++; } } ret = (char *)malloc(alloc_len + 1); ts = s; dest = ret; for ( ;*ts != '\0'; ts++) { if (*ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') { *dest++ = '\\'; } *dest++ = *ts; } *dest++ = '\0'; return ret;}/** * Run ppp in "notty" mode against a resource listed as the first parameter * eg: * * ppp dev:/dev/omap_csmi_tty0 <ppp options> * */int ppp(int argc, char **argv){#ifdef HAVE_WIN32_PROC fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]); return -1;#else char *adb_service_name; pid_t pid; int fd; if (argc < 2) { fprintf(stderr, "usage: adb %s <adb service name> [ppp opts]\n", argv[0]); return 1; } adb_service_name = argv[1]; fd = adb_connect(adb_service_name); if(fd < 0) { fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n", adb_service_name, adb_error()); return 1; } pid = fork(); if (pid < 0) { perror("from fork()"); return 1; } else if (pid == 0) { int err; int i; const char **ppp_args; // copy args ppp_args = (const char **) alloca(sizeof(char *) * argc + 1); ppp_args[0] = "pppd"; for (i = 2 ; i < argc ; i++) { //argv[2] and beyond become ppp_args[1] and beyond ppp_args[i - 1] = argv[i]; } ppp_args[i-1] = NULL; // child side dup2(fd, STDIN_FILENO); dup2(fd, STDOUT_FILENO); adb_close(STDERR_FILENO); adb_close(fd); err = execvp("pppd", (char * const *)ppp_args); if (err < 0) { perror("execing pppd"); } exit(-1); } else { // parent side adb_close(fd); return 0; }#endif /* !HAVE_WIN32_PROC */}static int send_shellcommand(transport_type transport, char* serial, char* buf){ int fd, ret; for(;;) { fd = adb_connect(buf); if(fd >= 0) break; fprintf(stderr,"- waiting for device -\n"); adb_sleep_ms(1000); do_cmd(transport, serial, "wait-for-device", 0); } read_and_dump(fd); ret = adb_close(fd); if (ret) perror("close"); return ret;}static int logcat(transport_type transport, char* serial, int argc, char **argv){ char buf[4096]; char *log_tags; char *quoted_log_tags; log_tags = getenv("ANDROID_LOG_TAGS"); quoted_log_tags = dupAndQuote(log_tags == NULL ? "" : log_tags); snprintf(buf, sizeof(buf), "shell:export ANDROID_LOG_TAGS=\"\%s\" ; exec logcat", quoted_log_tags); free(quoted_log_tags); argc -= 1; argv += 1; while(argc-- > 0) { char *quoted; quoted = dupAndQuote (*argv++); strncat(buf, " ", sizeof(buf)-1); strncat(buf, quoted, sizeof(buf)-1); free(quoted); } send_shellcommand(transport, serial, buf); return 0;}int adb_download_data(const char *what, const void* data, int sz, unsigned progress){ char service[4096]; snprintf(service, sizeof service, "bootloader:flash:%s", what); return adb_download_buffer(service, data, sz, 1);}#define SENTINEL_FILE "config" OS_PATH_SEPARATOR_STR "envsetup.make"static int top_works(const char *top){ if (top != NULL && adb_is_absolute_host_path(top)) { char path_buf[PATH_MAX]; snprintf(path_buf, sizeof(path_buf), "%s" OS_PATH_SEPARATOR_STR SENTINEL_FILE, top); return access(path_buf, F_OK) == 0; } return 0;}static char *find_top_from(const char *indir, char path_buf[PATH_MAX]){ strcpy(path_buf, indir); while (1) { if (top_works(path_buf)) { return path_buf; } char *s = adb_dirstop(path_buf); if (s != NULL) { *s = '\0'; } else { path_buf[0] = '\0'; return NULL; } }}static char *find_top(char path_buf[PATH_MAX]){ char *top = getenv("ANDROID_BUILD_TOP"); if (top != NULL && top[0] != '\0') { if (!top_works(top)) { fprintf(stderr, "adb: bad ANDROID_BUILD_TOP value \"%s\"\n", top); return NULL; } } else { top = getenv("TOP"); if (top != NULL && top[0] != '\0') { if (!top_works(top)) { fprintf(stderr, "adb: bad TOP value \"%s\"\n", top); return NULL; } } else { top = NULL; } } if (top != NULL) { /* The environment pointed to a top directory that works. */ strcpy(path_buf, top); return path_buf; } /* The environment didn't help. Walk up the tree from the CWD * to see if we can find the top. */ char dir[PATH_MAX]; top = find_top_from(getcwd(dir, sizeof(dir)), path_buf); if (top == NULL) { /* If the CWD isn't under a good-looking top, see if the * executable is. */ get_my_path(dir); top = find_top_from(dir, path_buf); } return top;}/* <hint> may be: * - A simple product name * e.g., "sooner"TODO: debug? sooner-debug, sooner:debug? * - A relative path from the CWD to the ANDROID_PRODUCT_OUT dir * e.g., "out/target/product/sooner" * - An absolute path to the PRODUCT_OUT dir * e.g., "/src/device/out/target/product/sooner" * * Given <hint>, try to construct an absolute path to the * ANDROID_PRODUCT_OUT dir. */static const char *find_product_out_path(const char *hint){ static char path_buf[PATH_MAX]; if (hint == NULL || hint[0] == '\0') { return NULL; } /* If it's already absolute, don't bother doing any work. */ if (adb_is_absolute_host_path(hint)) { strcpy(path_buf, hint); return path_buf; } /* If there are any slashes in it, assume it's a relative path; * make it absolute. */ if (adb_dirstart(hint) != NULL) { if (getcwd(path_buf, sizeof(path_buf)) == NULL) { fprintf(stderr, "adb: Couldn't get CWD: %s\n", strerror(errno)); return NULL; } if (strlen(path_buf) + 1 + strlen(hint) >= sizeof(path_buf)) { fprintf(stderr, "adb: Couldn't assemble path\n"); return NULL; } strcat(path_buf, OS_PATH_SEPARATOR_STR); strcat(path_buf, hint); return path_buf; } /* It's a string without any slashes. Try to do something with it. * * Try to find the root of the build tree, and build a PRODUCT_OUT * path from there. */ char top_buf[PATH_MAX]; const char *top = find_top(top_buf); if (top == NULL) { fprintf(stderr, "adb: Couldn't find top of build tree\n"); return NULL; }//TODO: if we have a way to indicate debug, look in out/debug/target/... snprintf(path_buf, sizeof(path_buf), "%s" OS_PATH_SEPARATOR_STR "out" OS_PATH_SEPARATOR_STR "target" OS_PATH_SEPARATOR_STR "product" OS_PATH_SEPARATOR_STR "%s", top_buf, hint); if (access(path_buf, F_OK) < 0) { fprintf(stderr, "adb: Couldn't find a product dir " "based on \"-p %s\"; \"%s\" doesn't exist\n", hint, path_buf); return NULL; } return path_buf;}int adb_commandline(int argc, char **argv){ char buf[4096]; int no_daemon = 0; int is_daemon = 0; int persist = 0; int r; int quote; transport_type ttype = kTransportAny; char* serial = NULL; /* If defined, this should be an absolute path to * the directory containing all of the various system images * for a particular product. If not defined, and the adb * command requires this information, then the user must * specify the path using "-p". */ gProductOutPath = getenv("ANDROID_PRODUCT_OUT"); if (gProductOutPath == NULL || gProductOutPath[0] == '\0') { gProductOutPath = NULL; } // TODO: also try TARGET_PRODUCT as a hint /* modifiers and flags */ while(argc > 0) { if(!strcmp(argv[0],"nodaemon")) { no_daemon = 1; } else if (!strcmp(argv[0], "fork-server")) { /* this is a special flag used only when the ADB client launches the ADB Server */ is_daemon = 1; } else if(!strcmp(argv[0],"persist")) { persist = 1; } else if(!strncmp(argv[0], "-p", 2)) { const char *product = NULL; if (argv[0][2] == '\0') { if (argc < 2) return usage(); product = argv[1]; argc--; argv++; } else { product = argv[1] + 2; } gProductOutPath = find_product_out_path(product); if (gProductOutPath == NULL) { fprintf(stderr, "adb: could not resolve \"-p %s\"\n", product); return usage(); } } else if (argv[0][0]=='-' && argv[0][1]=='s') { if (isdigit(argv[0][2])) { serial = argv[0] + 2; } else { if(argc < 2) return usage(); serial = argv[1]; argc--; argv++; } } else if (!strcmp(argv[0],"-d")) { ttype = kTransportUsb; } else if (!strcmp(argv[0],"-e")) { ttype = kTransportLocal; } else { /* out of recognized modifiers and flags */ break; } argc--; argv++; } adb_set_transport(ttype, serial); if ((argc > 0) && (!strcmp(argv[0],"server"))) { if (no_daemon || is_daemon) { r = adb_main(is_daemon); } else { r = launch_server(); } if(r) { fprintf(stderr,"* could not start server *\n"); } return r; }top: if(argc == 0) { return usage(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -