📄 commandline.c
字号:
/* adb_connect() commands */ if(!strcmp(argv[0], "devices")) { char *tmp; snprintf(buf, sizeof buf, "host:%s", argv[0]); tmp = adb_query(buf); if(tmp) { printf("List of devices attached \n"); printf("%s\n", tmp); return 0; } else { return 1; } } if (!strcmp(argv[0], "emu")) { return adb_send_emulator_command(argc, argv); } if(!strcmp(argv[0], "shell")) { int r; int fd; if(argc < 2) { return interactive_shell(); } snprintf(buf, sizeof buf, "shell:%s", argv[1]); argc -= 2; argv += 2; while(argc-- > 0) { strcat(buf, " "); /* quote empty strings and strings with spaces */ quote = (**argv == 0 || strchr(*argv, ' ')); if (quote) strcat(buf, "\""); strcat(buf, *argv++); if (quote) strcat(buf, "\""); } for(;;) { fd = adb_connect(buf); if(fd >= 0) { read_and_dump(fd); adb_close(fd); r = 0; } else { fprintf(stderr,"error: %s\n", adb_error()); r = -1; } if(persist) { fprintf(stderr,"\n- waiting for device -\n"); adb_sleep_ms(1000); do_cmd(ttype, serial, "wait-for-device", 0); } else { return r; } } } if(!strcmp(argv[0], "debug")) { int fd = adb_connect("bootdebug:"); if(fd >= 0) { read_and_dump(fd); adb_close(fd); return 0; } fprintf(stderr,"error: %s\n", adb_error()); return 1; } if(!strcmp(argv[0], "bl")) { int fd; if(argc != 2) return usage(); snprintf(buf, sizeof buf, "bootloader:%s", argv[1]); fd = adb_connect(buf); if(fd >= 0) { read_and_dump(fd); adb_close(fd); return 0; } else { fprintf(stderr,"* command failed: %s *\n", adb_error()); } return 1; } if(!strcmp(argv[0], "kill-server")) { int fd; fd = _adb_connect("host:kill"); if(fd == -1) { fprintf(stderr,"* server not running *\n"); return 1; } return 0; } if(!strcmp(argv[0], "remount")) { int fd = adb_connect("remount:"); if(fd >= 0) { read_and_dump(fd); adb_close(fd); return 0; } fprintf(stderr,"error: %s\n", adb_error()); return 1; } /* adb_download() commands */ if(!strcmp(argv[0], "send")) { if(argc != 3) return usage(); snprintf(buf, sizeof buf, "bootloader:send:%s", argv[1]); if(adb_download(buf, argv[2], 1)) { return 1; } else { return 0; } } if(!strcmp(argv[0], "recover")) { if(argc != 2) return usage(); if(adb_download("recover", argv[1], 1)) { return 1; } else { return 0; } } if(!strcmp(argv[0], "bugreport")) { if (argc != 1) { return 1; } do_cmd(ttype, serial, "shell", "dumpstate", "-", 0); return 0; } /* adb_command() wrapper commands */ if(!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) { char* service = argv[0]; if (!strncmp(service, "wait-for-bootloader", strlen("wait-for-bootloader"))) { fprintf(stderr,"WAIT FOR BOOTLOADER\n"); } else if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) { if (ttype == kTransportUsb) { service = "wait-for-usb"; } else if (ttype == kTransportLocal) { service = "wait-for-local"; } else { service = "wait-for-any"; } } format_host_command(buf, sizeof buf, service, ttype, serial); if (adb_command(buf)) { D("failure: %s *\n",adb_error()); fprintf(stderr,"error: %s\n", adb_error()); return 1; } /* Allow a command to be run after wait-for-device, * e.g. 'adb wait-for-device shell'. */ if(argc > 1) { argc--; argv++; goto top; } return 0; } if(!strcmp(argv[0], "forward")) { if(argc != 3) return usage(); if (serial) { snprintf(buf, sizeof buf, "host-serial:%s:forward:%s;%s",serial,argv[1],argv[2]); } else { snprintf(buf, sizeof buf, "host:forward:%s;%s",argv[1],argv[2]); } if(adb_command(buf)) { fprintf(stderr,"error: %s\n", adb_error()); return 1; } return 0; } /* do_sync_*() commands */ if(!strcmp(argv[0], "ls")) { if(argc != 2) return usage(); return do_sync_ls(argv[1]); } if(!strcmp(argv[0], "push")) { if(argc != 3) return usage(); return do_sync_push(argv[1], argv[2], 0 /* no verify APK */); } if(!strcmp(argv[0], "pull")) { if(argc != 3) return usage(); return do_sync_pull(argv[1], argv[2]); } if(!strcmp(argv[0], "install")) { if (argc < 2) return usage(); return install_app(ttype, serial, argc, argv); } if(!strcmp(argv[0], "uninstall")) { if (argc < 2) return usage(); return uninstall_app(ttype, serial, argc, argv); } if(!strcmp(argv[0], "sync")) { char *srcarg, *android_srcpath, *data_srcpath; int ret; if(argc < 2) { /* No local path was specified. */ srcarg = NULL; } else if(argc == 2) { /* A local path or "android"/"data" arg was specified. */ srcarg = argv[1]; } else { return usage(); } ret = find_sync_dirs(srcarg, &android_srcpath, &data_srcpath); if(ret != 0) return usage(); if(android_srcpath != NULL) ret = do_sync_sync(android_srcpath, "/system"); if(ret == 0 && data_srcpath != NULL) ret = do_sync_sync(data_srcpath, "/data"); free(android_srcpath); free(data_srcpath); return ret; } /* passthrough commands */ if(!strcmp(argv[0],"get-state") || !strcmp(argv[0],"get-product") || !strcmp(argv[0],"get-serialno")) { char *tmp; format_host_command(buf, sizeof buf, argv[0], ttype, serial); tmp = adb_query(buf); if(tmp) { printf("%s\n", tmp); return 0; } else { return 1; } } /* other commands */ if(!strcmp(argv[0],"status-window")) { status_window(ttype, serial); return 0; } if(!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat")) { return logcat(ttype, serial, argc, argv); } if(!strcmp(argv[0],"ppp")) { return ppp(argc, argv); } if (!strcmp(argv[0], "start-server")) { return adb_connect("host:start-server"); } if (!strcmp(argv[0], "jdwp")) { int fd = adb_connect("jdwp"); if (fd >= 0) { read_and_dump(fd); adb_close(fd); return 0; } else { fprintf(stderr, "error: %s\n", adb_error()); return -1; } } /* "adb /?" is a common idiom under Windows */ if(!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) { help(); return 0; } if(!strcmp(argv[0], "version")) { version(stdout); return 0; } usage(); return 1;}static int do_cmd(transport_type ttype, char* serial, char *cmd, ...){ char *argv[16]; int argc; va_list ap; va_start(ap, cmd); argc = 0; if (serial) { argv[argc++] = "-s"; argv[argc++] = serial; } else if (ttype == kTransportUsb) { argv[argc++] = "-d"; } else if (ttype == kTransportLocal) { argv[argc++] = "-e"; } argv[argc++] = cmd; while((argv[argc] = va_arg(ap, char*)) != 0) argc++; va_end(ap);#if 0 int n; fprintf(stderr,"argc = %d\n",argc); for(n = 0; n < argc; n++) { fprintf(stderr,"argv[%d] = \"%s\"\n", n, argv[n]); }#endif return adb_commandline(argc, argv);}int find_sync_dirs(const char *srcarg, char **android_srcdir_out, char **data_srcdir_out){ char *android_srcdir, *data_srcdir; if(srcarg == NULL) { android_srcdir = product_file("system"); data_srcdir = product_file("data"); } else { /* srcarg may be "data", "system" or NULL. * if srcarg is NULL, then both data and system are synced */ if(strcmp(srcarg, "system") == 0) { android_srcdir = product_file("system"); data_srcdir = NULL; } else if(strcmp(srcarg, "data") == 0) { android_srcdir = NULL; data_srcdir = product_file("data"); } else { /* It's not "system" or "data". */ return 1; } } if(android_srcdir_out != NULL) *android_srcdir_out = android_srcdir; else free(android_srcdir); if(data_srcdir_out != NULL) *data_srcdir_out = data_srcdir; else free(data_srcdir); return 0;}static int pm_command(transport_type transport, char* serial, int argc, char** argv){ char buf[4096]; snprintf(buf, sizeof(buf), "shell:pm"); 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 uninstall_app(transport_type transport, char* serial, int argc, char** argv){ /* 'adb uninstall' takes the same arguments as 'pm uninstall' on device */ return pm_command(transport, serial, argc, argv);}static int delete_file(transport_type transport, char* serial, char* filename){ char buf[4096]; char* quoted; snprintf(buf, sizeof(buf), "shell:rm "); quoted = dupAndQuote(filename); strncat(buf, quoted, sizeof(buf)-1); free(quoted); send_shellcommand(transport, serial, buf); return 0;}int install_app(transport_type transport, char* serial, int argc, char** argv){ struct stat st; int err; const char *const WHERE = "/data/local/tmp/%s"; char to[PATH_MAX]; char* filename = argv[argc - 1]; const char* p; p = adb_dirstop(filename); if (p) { p++; snprintf(to, sizeof to, WHERE, p); } else { snprintf(to, sizeof to, WHERE, filename); } if (p[0] == '\0') { } err = stat(filename, &st); if (err != 0) { fprintf(stderr, "can't find '%s' to install\n", filename); return 1; } if (!S_ISREG(st.st_mode)) { fprintf(stderr, "can't install '%s' because it's not a file\n", filename); return 1; } if (!(err = do_sync_push(filename, to, 1 /* verify APK */))) { /* file in place; tell the Package Manager to install it */ argv[argc - 1] = to; /* destination name, not source location */ pm_command(transport, serial, argc, argv); delete_file(transport, serial, to); } return err;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -