Пример #1
0
/*** Função que confere os comandos ***/
int comandos() {
    if(comando()) {
        if(match(PONTOVIRGULA)) {
            if(comandos()) {
                return 1;
            } else return 0;
        } else return 1;
    } else return 1;
}
Пример #2
0
/*** Função que confere um bloco ***/
int bloco() {
    if(match(BEGIN)) {
        if(comandos()) {
            if(match(END)) {
                return 1;
            } else return 0;
        } else return 0;
    } else return 0;
}
Пример #3
0
void EnviarChat() {
  char *label=GUI_getLabel(1);
  if (!label) return;

  if (label[0]=='/')
    comandos(label);
  else if (label[0]!='\0') {
    ListBox_Adicionar(4,0,0,0,7,"<%s> %s","tronix",label);

    Baloes_SetTexto(label);

    EditBox_SalvaHistorico(1);
  }

  GUI_setLabel(1,"\0");
}
Пример #4
0
main(int argc, char *argv[]) {
  /**DECLARACION DE VARIABLES*/
  int sockfd, *socket_hilo;
  struct sockaddr_in serveraddr;
  struct in_addr inaddr;
  struct hostent *host;
  char *server, *no_es_entero;
  int puerto = 0,ent=0;
  char *buffer = (char *)malloc(sizeof(char) * 256);
  char *h = "",*p = "",*n="",*a="",*IP="";
  FILE *nombre_Archivo;
  pthread_t hilo;

  /** Se verifica que la sintaxis sea la correcta */
  if (argc != 9){
    if (argc != 7){
      error("La sintaxis es: ./schat -h <host> -p <puerto> -n <nombre> -a <archivo>");
    } 
  }

  /**Esto es para verificar si esta ausente la -a, que pde o no ser
   pasada como parametro **/
  if (argc == 7){
     a = "-a";
     ent = 7;
  }else{
     ent = 9; 
  }  

  /** Se verifican los parametros pasados por la consola*/
  int i = 1;
  for (i;i < ent;i++){
    if (strcmp(argv[i],"-h") == 0){
      h = argv[i];
      IP = argv[i+1];
    }else if (strcmp(argv[i],"-p") == 0){
      p = argv[i];
      puerto = strtol(argv[i+1], &no_es_entero, 10);
	if (*no_es_entero != '\0') {
 	  error("El puerto ingresado %s es un ENTERO < 0");
	}
	if (puerto < 0) {
 	  error("El puerto ingresado %s es un ENTERO < 0");
	}
    }else if (strcmp(argv[i],"-n") == 0){
      n = argv[i];
      nombre = argv[i+1];
    }else if (strcmp(argv[i],"-a") == 0){
      a = argv[i];
      nombre_Archivo = fopen(argv[i+1],"r");
    }
    i++;
  }
  
 
  /**Se verifica que no falte ninguno de los parametros requeridos*/
  if ((strcmp(h,"-h") != 0) || (strcmp(p,"-p") != 0) || (strcmp(n,"-n") != 0) || (strcmp(a,"-a") != 0)){
    error("Falta la especificacion de alguno de los parametros <-a,-n,-p,-h>");
  }
  
  if (argc == 7){
     a = " ";  
  }

  /** Se abre el socket */
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0){
    perror("No se puedo abrir el socket");
    exit(0);
  }
    
  /** OBTENEMOS EL SERVIDOR */
  if (inet_aton(IP, &inaddr))
      host = gethostbyaddr((char *) &inaddr, sizeof(inaddr), AF_INET);
  else
      host = gethostbyname(IP);
  
  /**Si el servidor esta erroneo se imprime un mensaje de error*/
  if (!host) {
      error("Error con el host indicado");
    }
  

  /**Se obtiene la direccion del servidor. */
  bzero(&serveraddr, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_port = htons(puerto);
  bcopy((char *)host->h_addr,(char *)&serveraddr.sin_addr.s_addr,host->h_length);

  /**Se conecta al servidor */
    if (connect(sockfd, (struct sockaddr *) &serveraddr,sizeof(serveraddr)) < 0){
	  error("No Se puede establecer conexion con el servidor");
    }
    strcpy(buffer, nombre);
    /**Se envia el nombre de cliente al servidor para que verifique 
     * si hay algun otro usuario conectado con el mismo nombre*/
    if(send(sockfd, buffer, 32, 0) < 0){
	  error("Error al enviar informacion de inicio");
    }
    strcpy(buffer," ");
    /**Recibe la respuesta del servidor*/
    if (recv(sockfd, buffer, 1, 0) < 0){
	  error("Error al recibir nombre del cliente");
    }
    
    if (!strcmp("1",buffer)){
      printf("El usuario %s ya esta suscrito en el servidor\n", nombre);
      exit(0);
    }
    
  printf("###################################\n");
  printf("####### BIENVENIDOS A CCHAT #######\n");
  printf("###################################\n");
      
  /**hilo*/
  socket_hilo = malloc(1);
  *socket_hilo = sockfd;
  int salio = 1;
  /** Aqui crearemos un hilo para que el programa ejecute de manera
   * paralela la recepcion de informacion mediante el socket
   * mientras que se ejecuta la lectura del archivo y luego la
   * introduccion de comandos del cchat*/  
  if ((pthread_create(&hilo, NULL,conexion, (void *)socket_hilo)) < 0){
      error("ERROR en al crear nuevo hilo\n");
      exit(1);
  }
  /**Si no hay archivo evitamos realizar un llamado a la funcion
   * innecesariamente*/
  if (!(strcmp(a,"-a"))){
    if (nombre_Archivo == NULL) {
	printf("ERROR: Archivo NO encontrado\n");
    }else{
	salio = Leer_Archivo(nombre_Archivo,(void *)socket_hilo);
    } 
  }  
  /**Ejecucion de la introduccion de comandos*/
  comandos((void *)socket_hilo);
  free(socket_hilo);
  free(buffer);
  /**Se cierra el socket*/
  close(sockfd);
  exit(EXIT_SUCCESS);
}
Пример #5
0
/*** Função que confere um comando ***/
int comando() {
    if(match(PRINT)) {
        if(match(LPARENT)) {
            if(lista_argumentos()) {
                if(match(RPARENT)) {
                    return 1;
                } else return 0;
            } else return 0;
        } else return 0;
    } else if(match(IF)) {
        if(match(NOT)) return 1;
        if(expr_relacional()) {
            if(match(OR)||match(AND)) return 1;
            if(match(THEN)) {
                if(comando()) {
                    if(else_opc()) {
                        return 1;
                    } else return 0;
                } else return 0;
            } else return 0;
        } else if(match(LPARENT)) {
            if(expr_relacional()) {
                if(match(RPARENT)) {
                    if(match(OR)||match(AND)) return 1;
                    if(match(THEN)) {
                        if(comando()) {
                            if(else_opc()) {
                                return 1;
                            } else return 0;
                        } else return 0;
                    } else return 0;
                } else return 0;
            } else return 0;
        } else return 0;
    } else if(ident()) {
        if(match(ASSIGN)) {
            if(expr()) {
                if(match(PONTOVIRGULA)) {
                    if(comando()) {
                        if(comandos()) return 1;
                        return 1;
                    } else return 1;
                } else return 0;
            } else if(match(NUM)) {
                return 1;
            } else return 0;
        } else return 0;
    } else if(match(FOR)) {
        if(ident()) {
            if(match(ASSIGN)) {
                if(expr()) {
                    if(match(TO) || match(DOWNTO)) {
                        if(expr()) {
                            if(match(DO)) {
                                if(comando()) {
                                    if(comandos()) return 1;
                                    return 1;
                                } else return 0;
                            } else return 0;
                        } else return 0;
                    } else return 0;
                } else return 0;
            } else return 0;
        } else return 0;
    } else if(chamada_proc()) {
        return 1;
    } else if(bloco()) {
        return 1;
    } else if(match(WRITELN)||match(WRITE)) {
        if(match(LPARENT)) {
            if(literal()) {
                while(match(VIRGULA)) {
                    if(expr()) {
                        continue;
                    } else if(literal()) {
                        continue;
                    } else if(expr()) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
                if(match(RPARENT)) {
                    return 1;
                } else return 0;
            } else if(ident()) {
                while(match(VIRGULA)) {
                    if(ident()) {
                        continue;
                    } else if(literal()) {
                        continue;
                    } else if(expr()) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
                if(match(RPARENT)) {
                    return 1;
                } else return 0;
            } else if(match(RPARENT)) {
                return 1;
            } else return 0;
        } else return 0;
    } else if(match(READLN)) {
        if(match(LPARENT)) {
            if(lista_ident()) {
                if(match(RPARENT)) {
                    return 1;
                } else return 0;
            } else return 0;
        } else return 0;
    } else if(match(BREAK)||match(READKEY)) {
        if(match(PONTOVIRGULA)) {
            return 1;
        } else return 0;
    } else if(match(WHILE)) {
        if(match(LPARENT)) {
            if(expr_relacional()) {
                if(match(RPARENT)) {
                    if(match(DO)) {
                        if(comando()) {
                            if(comandos()) return 1;
                            return 1;
                        } else return 0;
                    } else return 0;
                } else return 0;
            } else return 0;
        } else if(expr_relacional()) {
            if(match(DO)) {
                if(comando()) {
                    return 1;
                } else return 0;
            } else return 0;
        } else return 0;
    } else return 1;
}
Пример #6
0
int main(){
	MSG msg;
	int res;
	char comando[150];
	
	pthread_t thread;
	pthread_attr_t attr;
	
	running = 1;
	int t1 = pthread_create(&thread, NULL, thread1, NULL);
	
        iniciaGerador();
       
        if(signal(SIGALRM,iniciaJogo) == SIG_ERR)
        {
            perror("\n[Erro] Não foi possivel configurar o sina SIGALRM \n");
            exit(EXIT_FAILURE);
        }
        fprintf(stderr, "\n[Servidor] Sinal SIGALRM configurado \n");
	
        
        //SERVIDOR
	//Verifica se servidor ja existe (ACCESS)        
        if (access(SERVER_FIFO, F_OK) == 0){
		perror("\n[Erro] Servidor ja existe!\n");
		exit(1);
	}
		
	//Cria fifo do servidor (MKFIFO)

	res = mkfifo(SERVER_FIFO, 0777);
	if (res == -1) {
		perror("\n[Erro] Erro no Mkfifo do servidor \n");
		exit(EXIT_FAILURE);
	}
	fprintf(stderr, "\n[Servidor] FIFO do servidor criado \n");
        
	//Passa o processo para background criando um processo filho e terminando o pai
	
	fork();	
	
	//Abre servidor (OPEN - O_RDWR)	
	fd_servidor = open(SERVER_FIFO, O_RDWR);  /* bloqueante */
	if (fd_servidor == -1) {
		perror("\n[Erro] Erro ao abrir o FIFO do servidor (RDWR/blocking) \n");
		exit(EXIT_FAILURE);
	}
	fprintf(stderr, "\n[Servidor] FIFO aberto para leitura! \n");

	
	while (1) { 

                //Lê mensagem
		res = read(fd_servidor, &msg, sizeof(msg));
		if (res < sizeof(msg)) 
                {
			fprintf(stderr, "\n[Erro] Recebido comando incompleto [bytes lidos: %d]", res);
		}
		else
			fprintf(stderr, "\n[Servidor] Comando -> %s \n", msg.comando);

                
                //Processo comandos
		comandos(&msg);
	} 

	running = 0;
	close(fd_servidor);
	unlink(SERVER_FIFO);
	return 0;
}