int main(int argc, char *argv[]) { int rv = -1; const char *device = NULL; const char *scriptname = NULL; // wait for socket if not there right away? int dowait = 0; terminal_init(); int i=1; while(i<argc && argv[i][0]=='-' && argv[i][1] != 0) { switch(argv[i][1]) { case '?': assert_single_char(argv[i]); usage(EXIT_SUCCESS); /* usage() exits already */ break; case 'd': assert_single_char(argv[i]); if (i < argc-1) { i++; device = argv[i]; log_info("main: device = %s\n", device); } else { log_error("-d requires <device> parameter\n"); exit(1); } break; case 'v': set_verbose(); break; case 't': trace = 1; break; case 'w': dowait = 1; break; default: log_error("Unknown command line option %s\n", argv[i]); usage(1); break; } i++; } // next parameter is the script name if (i >= argc) { log_error("Script name parameter missing!\n"); return -1; } scriptname = argv[i]; i++; registry_t *script = load_script_from_file(scriptname); if (script != NULL) { int sockfd = socket_open(device, dowait); if (sockfd >= 0) { rv = execute_script(sockfd, script); } } return rv; }
int main(int argc, const char *argv[]) { const char *socket = NULL; mem_init(); atexit(mem_exit); terminal_init(); // -------------------------------------------- // parse the parameters // Check -v (verbose) first to enable log_debug() // when processing other options for (int i=1; i < argc; i++) if (!strcmp("-v", argv[i])) set_verbose(1); int p = 1; while (p < argc && argv[p][0]=='-') { switch(argv[p][1]) { case 0: // single '-' option ends parameter processing p++; goto endpars; case 'v': assert_single_char(argv[p]); // verbose is already checked above set_verbose(1); break; case '?': assert_single_char(argv[p]); usage(EXIT_SUCCESS); /* usage() exits already */ break; case 'T': assert_single_char(argv[p]); if (p < argc-2) { p++; socket = argv[p]; log_info("main: tools socket = %s\n", socket); } else { log_error("-T requires <socket name> parameter\n"); exit(EXIT_RESPAWN_NEVER); } break; default: log_error("Unknown command line option %s\n", argv[p]); usage(EXIT_RESPAWN_NEVER); break; } p++; } endpars: // -------------------------------------------- // open the socket if (socket == NULL) { const char *home = os_get_home_dir(); socket = malloc_path(home, ".xdtools"); } int sockfd = socket_open(socket, 0); if (sockfd < 0) { log_errno("Could not open socket %s\n", socket); mem_free(socket); exit(1); } mem_free(socket); // -------------------------------------------- // find our command either as ending part of the name of the binary ... const cmdtab_t *cmd = NULL; int l = strlen(argv[0]); for (int i = 0; i < numcmds; i++) { int cl = strlen(cmdtab[i].name); if ((cl <= l) && !strcmp(cmdtab[i].name, &argv[0][l-cl])) { cmd = &cmdtab[i]; break; } } // ... or as command line parameter if (p < argc) { l = strlen(argv[p]); if (cmd == NULL) { for (int i = 0; i < numcmds; i++) { int cl = strlen(cmdtab[i].name); if ((cl <= l) && !strcmp(cmdtab[i].name, argv[p])) { cmd = &cmdtab[i]; p++; break; } } } } if (cmd == NULL) { log_error("Could not identify any of the commands!\n"); usage(1); } int rv = cmd->func(sockfd, argc-p, argv+p); close(sockfd); return rv; }