📄 halcmd.c
字号:
"HAL:%d: ERROR: HAL is locked, loading of programs is not permitted\n", linenumber); return HAL_PERM; } /* check for options (-w, -i, and/or -r) */ wait_flag = 0; ignore_flag = 0; prog_name = NULL; while ( **args == '-' ) { /* this argument contains option(s) */ cp1 = *args; cp1++; while ( *cp1 != '\0' ) { if ( *cp1 == 'w' ) { wait_flag = 1; } else if ( *cp1 == 'i' ) { ignore_flag = 1; } else { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: unknown loadusr option '-%c'\n", linenumber, *cp1); return HAL_INVAL; } cp1++; } /* move to next arg */ args++; } /* get program name */ prog_name = *args++; /* need to find path to a program matching "prog_name" */ prog_path[0] = '\0'; if ( prog_path[0] == '\0' ) { /* try the name by itself */ strncpy (prog_path, prog_name, MAX_CMD_LEN); rtapi_print_msg(RTAPI_MSG_DBG, "Trying '%s'\n", prog_path); if ( stat(prog_path, &stat_buf) != 0 ) { /* no luck, clear prog_path to indicate failure */ prog_path[0] = '\0'; } } if ( prog_path[0] == '\0' ) { /* no luck yet, try the emc2/bin directory where the halcmd executable is located */ n = readlink("/proc/self/exe", prog_path, MAX_CMD_LEN-10); if ( n > 0 ) { prog_path[n] = '\0'; /* have path to executabie, find last '/' */ cp2 = ""; cp1 = prog_path; while ( *cp1 != '\0' ) { if ( *cp1 == '/' ) { cp2 = cp1; } cp1++; } if ( *cp2 == '/' ) { /* chop "halcmd" from end of path */ *(++cp2) = '\0'; /* append the program name */ strncat(prog_path, prog_name, MAX_CMD_LEN-strlen(prog_path)); /* and try it */ rtapi_print_msg(RTAPI_MSG_DBG, "Trying '%s'\n", prog_path); if ( stat(prog_path, &stat_buf) != 0 ) { /* no luck, clear prog_path to indicate failure */ prog_path[0] = '\0'; } } } } if ( prog_path[0] == '\0' ) { /* no luck yet, try the user's PATH */ envpath = getenv("PATH"); if ( envpath != NULL ) { while ( *envpath != '\0' ) { /* copy a single directory from the PATH env variable */ n = 0; while ( (*envpath != ':') && (*envpath != '\0') && (n < MAX_CMD_LEN)) { prog_path[n++] = *envpath++; } /* append '/' and program name */ if ( n < MAX_CMD_LEN ) { prog_path[n++] = '/'; } cp1 = prog_name; while ((*cp1 != '\0') && ( n < MAX_CMD_LEN)) { prog_path[n++] = *cp1++; } prog_path[n] = '\0'; rtapi_print_msg(RTAPI_MSG_DBG, "Trying '%s'\n", prog_path); if ( stat(prog_path, &stat_buf) != 0 ) { /* no luck, clear prog_path to indicate failure */ prog_path[0] = '\0'; /* and get ready to try the next directory */ if ( *envpath == ':' ) { envpath++; } } else { /* success, break out of loop */ break; } } } } if ( prog_path[0] == '\0' ) { /* still can't find a program to run */ rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: Can't find program '%s'\n", linenumber, prog_name); return -1; } /* now we need to fork, and then exec the program.... */ /* disconnect from the HAL shmem area before forking */ hal_exit(comp_id); comp_id = 0; /* now the fork() */ pid = fork(); if ( pid < 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: loadusr fork() failed\n", linenumber); /* reconnect to the HAL shmem area */ comp_id = hal_init(comp_name); if (comp_id < 0) { fprintf(stderr, "halcmd: hal_init() failed after fork\n" ); exit(-1); } return -1; } if ( pid == 0 ) { /* this is the child process - prepare to exec() the program */ argv[0] = prog_path; /* loop thru remaining arguments */ n = 0; m = 1; while ( args[n][0] != '\0' ) { argv[m++] = args[n++]; } /* add a NULL to terminate the argv array */ argv[m] = NULL; /* print debugging info if "very verbose" (-V) */ rtapi_print_msg(RTAPI_MSG_DBG, "%s ", argv[0] ); n = 1; while ( argv[n] != NULL ) { rtapi_print_msg(RTAPI_MSG_DBG, "%s ", argv[n++] ); } rtapi_print_msg(RTAPI_MSG_DBG, "\n" ); /* call execv() to invoke the program */ execv(prog_path, argv); /* should never get here */ rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: execv(%s) failed\n", linenumber, prog_path ); exit(1); } /* this is the parent process, reconnect to the HAL shmem area */ comp_id = hal_init(comp_name); if (comp_id < 0) { fprintf(stderr, "halcmd: hal_init() failed after loadusr\n" ); exit(-1); } if ( wait_flag ) { /* wait for child process to complete */ retval = waitpid ( pid, &status, 0 ); /* check result of waitpid() */ if ( retval < 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: waitpid(%d) failed\n", linenumber, pid); return -1; } if ( WIFEXITED(status) == 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: program '%s' did not exit normally\n", linenumber, prog_name ); return -1; } if ( ignore_flag == 0 ) { retval = WEXITSTATUS(status); if ( retval != 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: program '%s' failed, returned %d\n", linenumber, prog_name, retval ); return -1; } } /* print success message */ rtapi_print_msg(RTAPI_MSG_INFO, "Program '%s' finished\n", prog_name); } else { /* print success message */ rtapi_print_msg(RTAPI_MSG_INFO, "Program '%s' started\n", prog_name); } return 0;}static void print_comp_info(char *pattern){ int next, len; hal_comp_t *comp; if (scriptmode == 0) { rtapi_print("Loaded HAL Components:\n"); rtapi_print("ID Type Name\n"); } rtapi_mutex_get(&(hal_data->mutex)); len = strlen(pattern); next = hal_data->comp_list_ptr; while (next != 0) { comp = SHMPTR(next); if ( strncmp(pattern, comp->name, len) == 0 ) { rtapi_print("%02d %s %s\n", comp->comp_id, (comp->type ? "RT " : "User"), comp->name); } next = comp->next_ptr; } rtapi_mutex_give(&(hal_data->mutex)); rtapi_print("\n");}static void print_pin_info(char *pattern){ int next, len; hal_pin_t *pin; hal_comp_t *comp; hal_sig_t *sig; void *dptr; if (scriptmode == 0) { rtapi_print("Component Pins:\n"); rtapi_print("Owner Type Dir Value Name\n"); } rtapi_mutex_get(&(hal_data->mutex)); len = strlen(pattern); next = hal_data->pin_list_ptr; while (next != 0) { pin = SHMPTR(next); if ( strncmp(pattern, pin->name, len) == 0 ) { comp = SHMPTR(pin->owner_ptr); if (pin->signal != 0) { sig = SHMPTR(pin->signal); dptr = SHMPTR(sig->data_ptr); } else { sig = 0; dptr = &(pin->dummysig); } if (scriptmode == 0) { rtapi_print(" %02d %s %s %s %s", comp->comp_id, data_type((int) pin->type), data_dir((int) pin->dir), data_value((int) pin->type, dptr), pin->name); } else { rtapi_print("%s %s %s %s %s", comp->name, data_type((int) pin->type), data_dir((int) pin->dir), data_value2((int) pin->type, dptr), pin->name); } if (sig == 0) { rtapi_print("\n"); } else { rtapi_print(" %s %s\n", data_arrow1((int) pin->dir), sig->name); } } next = pin->next_ptr; } rtapi_mutex_give(&(hal_data->mutex)); rtapi_print("\n");}static void print_sig_info(char *pattern){ int next, len; hal_sig_t *sig; void *dptr; hal_pin_t *pin; if (scriptmode != 0) { print_script_sig_info(pattern); return; } rtapi_print("Signals:\n"); rtapi_print("Type Value Name\n"); rtapi_mutex_get(&(hal_data->mutex)); len = strlen(pattern); next = hal_data->sig_list_ptr; while (next != 0) { sig = SHMPTR(next); if ( strncmp(pattern, sig->name, len) == 0 ) { dptr = SHMPTR(sig->data_ptr); rtapi_print("%s %s %s\n", data_type((int) sig->type), data_value((int) sig->type, dptr), sig->name); /* look for pin(s) linked to this signal */ pin = halpr_find_pin_by_sig(sig, 0); while (pin != 0) { rtapi_print(" %s %s\n", data_arrow2((int) pin->dir), pin->name); pin = halpr_find_pin_by_sig(sig, pin); } } next = sig->next_ptr; } rtapi_mutex_give(&(hal_data->mutex)); rtapi_print("\n");}static void print_script_sig_info(char *pattern){ int next, len; hal_sig_t *sig; void *dptr; hal_pin_t *pin; if (scriptmode == 0) { return; } rtapi_mutex_get(&(hal_data->mutex)); len = strlen(pattern); next = hal_data->sig_list_ptr; while (next != 0) { sig = SHMPTR(next); if ( strncmp(pattern, sig->name, len) == 0 ) { dptr = SHMPTR(sig->data_ptr); rtapi_print("%s %s %s", data_type((int) sig->type), data_value2((int) sig->type, dptr), sig->name); /* look for pin(s) linked to this signal */ pin = halpr_find_pin_by_sig(sig, 0); while (pin != 0) { rtapi_print(" %s %s", data_arrow2((int) pin->dir), pin->name); pin = halpr_find_pin_by_sig(sig, pin); } rtapi_print("\n"); } next = sig->next_ptr; } rtapi_mutex_give(&(hal_data->mutex)); rtapi_print("\n");}static void print_param_info(char *pattern){ int next, len; hal_param_t *param; hal_comp_t *comp; if (scriptmode == 0) { rtapi_print("Parameters:\n"); rtapi_print("Owner Type Dir Value Name\n"); } rtapi_mutex_get(&(hal_data->mutex)); len = strlen(pattern); next = hal_data->param_list_ptr; while (next != 0) { param = SHMPTR(next); if ( strncmp(pattern, param->name, len) == 0 ) { comp = SHMPTR(param->owner_ptr); if (scriptmode == 0) { rtapi_print(" %02d %s %s %s %s\n", comp->comp_id, data_type((int) param->type), data_dir((int) param->dir), data_value((int) param->type, SHMPTR(param->data_ptr)), param->name); } else { rtapi_print("%s %s %s %s %s\n", comp->name, data_type((int) param->type), data_dir((int) param->dir), data_value2((int) param->type, SHMPTR(param->data_ptr)), param->name); } } next = param->next_ptr; } rtapi_mutex_give(&(hal_data->mutex)); rtapi_print("\n");}static void print_funct_info(char *pattern){ int next, len; hal_funct_t *fptr; hal_comp_t *comp; if (scriptmode == 0) { rtapi_print("Exported Functions:\n"); rtapi_print("Owner CodeAddr Arg FP Users Name\n"); } rtapi_mutex_get(&(hal_data->mutex)); len = strlen(pattern); next = hal_data->funct_list_ptr; while (next != 0) { fptr = SHMPTR(next); if ( strncmp(pattern, fptr->name, len) == 0 ) { comp = SHMPTR(fptr->owner_ptr); if (scriptmode == 0) { rtapi_print(" %02d %08X %08X %s %3d %s\n", comp->comp_id, fptr->funct, fptr->arg, (fptr->uses_fp ? "YES" : "NO "), fptr->users, fptr->name); } else { rtapi_print("%s %08X %08X %s %3d %s\n", comp->name, fptr->funct, fptr->arg, (fptr->uses_fp ? "YES" : "NO "), fptr->users, fptr->name); } } next = fptr->next_ptr; } rtapi_mutex_give(&(hal_data->mutex)); rtapi_print("\n");}static void print_thread_info(char *pattern){ int next_thread, len, n; hal_thread_t *tptr; hal_list_t *list_root, *list_entry; hal_funct_entry_t *fentry; hal_funct_t *funct; if (scriptmode == 0) { rtapi_print("Realtime Threads:\n"); rtapi_print(" Period FP Name (Time, Max-Time)\n"); } rtapi_mutex_get(&(hal_data->mutex)); len = strlen(pattern); next_thread = hal_data->thread_list_ptr; while (next_thread != 0) { tptr = SHMPTR(next_thread); if ( strncmp(pattern, tptr->name, len) == 0 ) { /* note that the scriptmode format string has no \n */ // TODO FIXME add thread runtime and max runtime to this print rtapi_print(((scriptmode == 0) ? "%11d %s %s ( %d, %d )\n" : "%d %s %s %d %d"), tptr->period, (tptr->uses_fp ? "YES" : "NO "), tptr->name, tptr->runtime, tptr->maxtime); list_root = &(tptr->funct_list); list_entry = list_next(list_root); n = 1; while (list_entry != list_root) { /* print the function info */ fentry = (hal_funct_entry_t *) list_entry; funct = SHMPTR(fentry-
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -