Ejemplo n.º 1
0
static int init() {
    int result = 0;
    if(refcount == 0) {
        result = halcmd_startup(0);
        atexit(shutdown);
    }
    if(result == 0) {
        refcount ++;
    }
    return result;
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
    int c, fd;
    int keep_going, retval, errorcount;
    int filemode = 0;
    char *filename = NULL;
    FILE *srcfile = NULL;
    char raw_buf[MAX_CMD_LEN+1];
    int linenumber = 1;
    char *cf=NULL, *cw=NULL, *cl=NULL;

    if (argc < 2) {
        /* no args specified, print help */
        print_help_general(0);
        exit(0);
    }
    /* set default level of output - 'quiet' */
    rtapi_set_msg_level(RTAPI_MSG_ERR);
    /* set default for other options */
    keep_going = 0;
    /* start parsing the command line, options first */
    while(1) {
        c = getopt(argc, argv, "+RCfi:kqQsvVhe");
        if(c == -1) break;
        switch(c) {
        case 'R':
            /* force an unlock of the HAL mutex - to be used after a segfault in a hal program */
            if (release_HAL_mutex() < 0) {
                printf("HALCMD: Release Mutex failed!\n");
                return 1;
            }
            return 0;
            break;
        case 'h':
            /* -h = help */
            if (argc > optind) { /* there are more arguments */
                do_help_cmd(argv[optind]);
            } else {
                print_help_general(1);
            }
            return 0;
            break;
        case 'k':
            /* -k = keep going */
            keep_going = 1;
            break;
        case 'q':
            /* -q = quiet (default) */
            rtapi_set_msg_level(RTAPI_MSG_ERR);
            break;
        case 'Q':
            /* -Q = very quiet */
            rtapi_set_msg_level(RTAPI_MSG_NONE);
            break;
        case 's':
            /* script friendly mode */
            scriptmode = 1;
            break;
        case 'v':
            /* -v = verbose */
            rtapi_set_msg_level(RTAPI_MSG_INFO);
            break;
        case 'V':
            /* -V = very verbose */
            rtapi_set_msg_level(RTAPI_MSG_ALL);
            break;
        case 'e':
            echo_mode = 1;
            break;
        case 'f':
            filemode = 1;
            break;
        case 'C':
            cl = getenv("COMP_LINE");
            cw = getenv("COMP_POINT");
            if (!cl || !cw) exit(0);
            c = atoi(cw)-strlen(argv[0])-1;
            if (c<0) c=0;
            cl += strlen(argv[0])+1;
            if (c>0 && cl[c]=='\0') c--;    // if at end of line, back up one char
            cf=&cl[c];
            {
                int n;
                for (n=c; n>0; n--, cf--) {
                    if (isspace(*cf) || *cf == '=' || *cf == '<' || *cf == '>') {
                        cf++;
                        break;
                    }
                }
                halcmd_startup(1);
                propose_completion(cl, cf, n);
            }
            if (comp_id >= 0) halcmd_shutdown();
            exit(0);
            break;
#ifndef NO_INI
        case 'i':
            /* -i = allow reading 'setp' values from an ini file */
            if (halcmd_inifile == NULL) {
                /* it's the first -i (ignore repeats) */
                /* there is a following arg, and it's not an option */
                filename = optarg;
                halcmd_inifile = fopen(filename, "r");
                if (halcmd_inifile == NULL) {
                    fprintf(stderr,
                            "Could not open ini file '%s'\n",
                            filename);
                    exit(-1);
                }
                /* make sure file is closed on exec() */
                fd = fileno(halcmd_inifile);
                fcntl(fd, F_SETFD, FD_CLOEXEC);
            }
            break;
#endif /* NO_INI */
        case '?':
            /* option not in getopt() list
               getopt already printed an error message */
            exit(-1);
            break;
        default:
            /* option in getopt list but not in switch statement - bug */
            printf("Unimplemented option '-%c'\n", c);
            exit(-1);
            break;
        }
    }
    if(filemode) {
        /* it's the first -f (ignore repeats) */
        if (argc > optind) {
            /* there is a following arg, and it's not an option */
            filename = argv[optind++];
            srcfile = fopen(filename, "r");
            halcmd_set_filename(filename);
            if (srcfile == NULL) {
                fprintf(stderr,
                        "Could not open command file '%s'\n",
                        filename);
                exit(-1);
            }
            /* make sure file is closed on exec() */
            fd = fileno(srcfile);
            fcntl(fd, F_SETFD, FD_CLOEXEC);
        } else {
            halcmd_set_filename("<stdin>");
            /* no filename followed -f option, use stdin */
            srcfile = stdin;
            prompt_mode = 1;
        }
    }

    if ( halcmd_startup(0) != 0 ) return 1;

    errorcount = 0;
    /* HAL init is OK, let's process the command(s) */
    if (srcfile == NULL) {
#ifndef NO_INI
        if(halcmd_inifile) {
            fprintf(stderr, "-i may only be used together with -f\n");
            errorcount++;
        }
#endif
        if(errorcount == 0 && argc > optind) {
            halcmd_set_filename("<commandline>");
            halcmd_set_linenumber(0);
            retval = halcmd_parse_cmd(&argv[optind]);
            if (retval != 0) {
                errorcount++;
            }
        }
    } else {
        /* read command line(s) from 'srcfile' */
        while (get_input(srcfile, raw_buf, MAX_CMD_LEN)) {
            char *tokens[MAX_TOK+1];
            halcmd_set_linenumber(linenumber++);
            /* remove comments, do var substitution, and tokenise */
            retval = halcmd_preprocess_line(raw_buf, tokens);
            if(echo_mode) {
                halcmd_echo("%s\n", raw_buf);
            }
            if (retval == 0) {
                /* the "quit" command is not handled by parse_line() */
                if ( ( strcasecmp(tokens[0],"quit") == 0 ) ||
                        ( strcasecmp(tokens[0],"exit") == 0 ) ) {
                    break;
                }
                /* process command */
                retval = halcmd_parse_cmd(tokens);
            }
            /* did a signal happen while we were busy? */
            if ( halcmd_done ) {
                /* treat it as an error */
                errorcount++;
                /* exit from loop */
                break;
            }
            if ( retval != 0 ) {
                errorcount++;
            }
            if (( errorcount > 0 ) && ( keep_going == 0 )) {
                /* exit from loop */
                break;
            }
        }
    }
    /* all done */
    halcmd_shutdown();
    if ( errorcount > 0 ) {
        return 1;
    } else {
        return 0;
    }

}