示例#1
0
文件: tcllib.c 项目: Nazg-Gul/fm
/**
 *
 * Initialize Tcl embed library
 *
 * @return TCL_OK if successful, TCL_ERROR otherwise
 */
int
tcllib_init (void)
{
  /* NOTE: Keep order, config.tcl be loaded last */
  static wchar_t *important_files[] = { L"associations.tcl",
                                        L"config.tcl" };
  /* static wchar_t *unimportant_files[] = { L"custom.tcl" }; */
  int i, j, count;
  wchar_t **list;

  interpreter = Tcl_CreateInterp();
  if (interpreter == NULL)
    {
      return TCL_ERROR;
    }

  if (tcllib_init_commands (interpreter) != TCL_OK)
    {
      return TCL_ERROR;
    }

  for  (i = 0; i < sizeof (important_files) / sizeof (important_files[0]); ++i)
    {
      count = get_shared_files (important_files[i], NULL, &list);

      if (count == 0)
        {
          wchar_t msg[1024];
          swprintf (msg, BUF_LEN (msg),
                    _(L"Configuration file \"%ls\" not found"),
                    important_files[i]);
          MESSAGE_ERROR (msg);
          return TCL_ERROR;
        }

      for (j = 0; j < count; ++j)
        {
          if (tcllib_load_file (list[j]) != TCL_OK)
            {
              TCL_RUNTIME_ERROR;

              /* Free memory used by unseen items */
              while (j < count)
                {
                  SAFE_FREE (list[j]);
                  ++j;
                }
              SAFE_FREE (list);

              return TCL_ERROR;
            }
          SAFE_FREE (list[j]);
        }

      SAFE_FREE (list);
    }

  hook_register (L"open-file-hook", _file_associations_hook, 1);

  return TCL_OK;
}
示例#2
0
文件: scf_client.c 项目: plineo/scf
void do_connect(char *hostname)
{
    int ack;
    filestruct files;
     
    if (srv_socket > 0)
    {
	printf("Already connected to server. Logout first.\n");
	return;
    }

    if (hostname == NULL)
    {
        printf("Enter a valid hostname\n");
        return;
    }

    server = gethostbyname(hostname);

    if (server == NULL)
     {
        //printf("%s: No such host!\n", hostname);
	perror(hostname);
        return;
     }
            
    srv_socket = socket(AF_INET, SOCK_STREAM, 0);

    if (srv_socket < 0)
    {
        perror("ERROR opening socket");
        return;
    }

     bzero((char *) &serv_addr, sizeof(serv_addr));
     serv_addr.sin_family = AF_INET;
     bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
     serv_addr.sin_port = htons(PORT);

     printf("Trying %s (%s).....\n", hostname, inet_ntoa(serv_addr.sin_addr));

     //Conectar con el servidor
     if (connect(srv_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
     {
        perror(hostname);
	srv_socket = close(srv_socket);
        return;
     }

    //Iniciar protocolo de conexion con servidor SCF
    if (send_ec_pkg(srv_socket, my_ident, my_port) == -1)
    {
	srv_socket = close(srv_socket);
        return;
    }

    ack = recv_ack_err(srv_socket);
    
    /* Error en el canal */
    if (ack == -1)
    {
	srv_socket = close(srv_socket);
        return;
    }
    
    /* Si el servidor rechaza nuestro paquete EC, optamos por abandonar */
    if (ack == 1)
    {
	send_fc_pkg(srv_socket);
	if (recv_ack_err(srv_socket) == 0)
	    srv_socket = close(srv_socket);
        return;
    }
	
    //El servidor acepta nuestro EC, ahora le enviamos la lista de ficheros
    get_shared_files(&files);
    send_default_pkg(srv_socket, files.lfiles, files.files, LF);
    ack = recv_ack_err(srv_socket);
    
    if (ack == 0) 
	printf("Connected to SCF Server %s\n", hostname);
    else
    {
	send_fc_pkg(srv_socket);
	if (recv_ack_err(srv_socket) == 0)
	    srv_socket = close(srv_socket);
    }
}