예제 #1
0
파일: comandos.c 프로젝트: Goostavo/shii
//Le a primeira coluna de dados, e processa o comando correspondente
//retorna a flag para finalizacao do shiii
int process(char com_matrix[10][64][1024],       //Matriz que guarda os comandos de forma legivel
            int* n_command,                     //numero de comandos
            int* pipe,                          //Localizacao do pipe
            int* bkgnd,                         //Flag de execucao em background
            char* diretorio)                    //Diretorio corrente
{
    int conta_comando = 0;
    while (conta_comando <= *n_command)
    {
        //Comando do bash CD
        if (!strcmp(com_matrix[conta_comando][0],"cd"))
        {
            command_cd(com_matrix[conta_comando][1],diretorio);
        }
        else if (!strcmp(com_matrix[conta_comando][0],"pwd"))
        {
            command_pwd();
        }
        else if (!strcmp(com_matrix[conta_comando][0],"wait"))
        {
            command_wait();
        }
        else if (!strcmp(com_matrix[conta_comando][0],"exit"))
        {
            printf("Adeus!\n");
            return 1;               //Finaliza o processamento prematuramente
        }
        else if (!strcmp(com_matrix[conta_comando][0],"history"))
        {
            hist_show();
        }
        else if (com_matrix[conta_comando][0][0]=='!')//Hist_recall
        {
            //Deve conter apenas ! e um inteiro
            if (com_matrix[conta_comando][0][2]!='\0')
            {
                printf("Erro: Evento desconhecido!\n");
            }
            else
            {
                hist_recall(((int)com_matrix[conta_comando][0][1])-48,diretorio);
            }
        }
        else
        {
            executa_aplicativo(com_matrix,&conta_comando,bkgnd,pipe);
        }
        conta_comando++;
    }//Fim while
    return 0;                       //retorna ok para continuar a shii
}
/**
 * \brief Set command implementation
 */
int	command_set(MountPrx mount, RaDec radec) {
    mount->GotoRaDec(radec);
    return command_wait(mount, await_completion);
}
/**
 * \brief main function
 */
int	main(int argc, char *argv[]) {
    debug_set_ident("snowmount");
    CommunicatorSingleton	communicator(argc, argv);

    int	c;
    int	longindex;
    astro::ServerName	servername;
    putenv("POSIXLY_CORRECT=1");
    while (EOF != (c = getopt_long(argc, argv, "dhc:fw", longopts,
                                   &longindex)))
        switch (c) {
        case 'd':
            debuglevel = LOG_DEBUG;
            break;
        case 'f':
            decimal = true;
            break;
        case 'h':
            usage(argv[0]);
            return EXIT_SUCCESS;
        case 'w':
            await_completion = true;
            break;
        default:
            throw std::runtime_error("unknown option");
        }

    // next comes the command
    if (argc <= optind) {
        throw std::runtime_error("command missing");
    }
    std::string	command(argv[optind++]);

    // handle the help command
    if (command == "help") {
        return command_help(argv[0]);
    }

    servername = astro::ServerName(command);

    // next argument must be the command
    if (argc <= optind) {
        throw std::runtime_error("command missing");
    }
    command = std::string(argv[optind++]);

    if (command == "help") {
        return command_help(argv[0]);
    }

    // we need a remote device proxy for all other commands
    Ice::CommunicatorPtr	ic = CommunicatorSingleton::get();
    Ice::ObjectPrx	base = ic->stringToProxy(servername.connect("Devices"));
    DevicesPrx	devices = DevicesPrx::checkedCast(base);

    // handle the list command
    if (command == "list") {
        return command_list(devices);
    }

    // for the other commands we need the mount name
    if (argc <= optind) {
        throw std::runtime_error("no mount name");
    }
    std::string	mountname(argv[optind++]);

    // get a proxy for the mount
    MountPrx	mount = devices->getMount(mountname);

    // get command
    if (command == "get") {
        return command_get(mount);
    }
    if (command == "cancel") {
        return command_cancel(mount);
    }
    if (command == "wait") {
        return command_wait(mount, true);
    }

    // two more arguments are angles
    if (command == "set") {
        if (argc < (optind + 2)) {
            throw std::runtime_error("missing angle arguments");
        }
        RaDec	radec;
        astro::Angle	ra
            = astro::Angle::hms_to_angle(argv[optind++]);
        radec.ra = ra.hours();
        astro::Angle	dec
            = astro::Angle::dms_to_angle(argv[optind++]);
        radec.dec = dec.degrees();
        return command_set(mount, radec);
    }

    // if we get here, then an unknown command was given
    throw std::runtime_error("unknown command");
}
/**
 * \brief Cancel command implementation
 */
int	command_cancel(MountPrx mount) {
    mount->cancel();
    return command_wait(mount, await_completion);
}