c-hglib
 All Data Structures Files Functions Variables Typedefs Macros
client.c File Reference
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include "client.h"
#include "utils.h"

Go to the source code of this file.

Macros

#define _GNU_SOURCE
#define HGPATH   "hg"

Functions

int read_header (hg_handle *handle)
 Reading the header from cmdsrv.
int read_hello (hg_handle *handle)
 Reading the hello msg from cmd server.
hg_handlehg_open (const char *path, char *encoding)
 Open the connection with the mercurial command server.
int hg_close (hg_handle **handle)
 Close the connection for the given handle.
char * cmd_prepare (char *const command[], size_t *cmd_size)
 Prepare the command for sending process.
int hg_rawcommand (hg_handle *handle, char *const command[], size_t cmd_size)
 Sending a command to the mercurial command server, through the given handle.
int hg_rawread (hg_handle *handle, char *buffer, size_t sizebuff)
 Reading some unparse data from the server.
int hg_rawwrite (hg_handle *handle, const char *buffer, size_t buff_size)
 Will write the buffer to server for the connection establish by the handle.
char hg_channel (hg_handle *handle)
 Reading the current channel.
hg_header hg_head (hg_handle *handle)
 Return the current header.
int hg_exitcode (hg_handle *handle)
 The exitcode for the current command.

Macro Definition Documentation

#define _GNU_SOURCE

Definition at line 1 of file client.c.

#define HGPATH   "hg"

Definition at line 16 of file client.c.

Function Documentation

int read_header ( hg_handle handle)

Reading the header from cmdsrv.

The function will read the header from the command server and will save it to the header parameter of the handle structure.

Parameters
handleThe handle of the connection, wherewith I want to communicate
Return values
0if succesfull
-1to indicate an error, with errno set appropriately

errno can be:

  • EINVAL - Invalid argument (handle it's set to a null pointer)
  • read(2) command errors

Definition at line 31 of file client.c.

int read_hello ( hg_handle handle)

Reading the hello msg from cmd server.

After the connection, the command server sends a hello message. The message contains the command server capabilities and the messages encoding.

Parameters
handleThe handle of the connection, wherewith I want to communicate
Return values
0if succesfull
-1to indicate an error, with errno set appropriately

errno can be:

  • EINVAL - Invalid argument (handle it's set to a null pointer)
  • read(2) command errors

Definition at line 67 of file client.c.

hg_handle* hg_open ( const char *  path,
char *  encoding 
)

Open the connection with the mercurial command server.

The handle structure will be allocated.

Parameters
pathThe path to the repository wherewith I want to create a connection NULL argument means the repository in which I am.
encodingWill set HGENCODING to the given encoding NULL argument means the default encoding. (UTF-8)
Return values
handle- A handle for this connection.
NULLIndicate an error, with errno set appropriately.

errno can be :

  • execl(2) command errors
  • dup2(2) command errors
  • fork(2) command errors
  • pipe(2) command errors
  • EFAULT - for a bad path address
  • EINVAL - for a bad encoding

Definition at line 103 of file client.c.

int hg_close ( hg_handle **  handle)

Close the connection for the given handle.

Erase the handle and free the memory

Parameters
handleThe handle of the connection that I want to close
Return values
0if successful
-1to indicate an error, with errno set appropriately.

errno can be:

  • EINVAL - Invalid argument ( handle it's set to a null pointer)
  • kill(2) command errors

Definition at line 159 of file client.c.

char* cmd_prepare ( char *const  command[],
size_t *  cmd_size 
)

Prepare the command for sending process.

Replace all the blank space with the '\0' character.

Parameters
commandan array that will contain the mercurial command
cmd_size- array size
Return values
stringrepresenting the command that will be send to cmdsrv
*cmd_sizewill be set on string size
char *command[] = {"tip", "-p"};
char *cmd_str = cmd_prepare(command, 2);
prinf("==%s==", cmd_str);
---> ==tip\0-p==

Definition at line 195 of file client.c.

int hg_rawcommand ( hg_handle handle,
char *const  command[],
size_t  cmd_size 
)

Sending a command to the mercurial command server, through the given handle.

Parameters
handleThe handle of the connection, wherewith I want to communicate
commandAn array of pointers to null-terminated strings that represent the argument list available to the new command.
cmd_sizeThe length of command array.
Return values
0if successful
-1to indicate an error, with errno set appropriately.

errno can be:

  • EINVAL - Invalid argument ( handle it's set to a null pointer)
  • write(2) command errors

Definition at line 224 of file client.c.

int hg_rawread ( hg_handle handle,
char *  buffer,
size_t  sizebuff 
)

Reading some unparse data from the server.

Will read just a 'line', the header that is received from server and the data that comes after the header

Parameters
handleThe handle of the connection, wherewith I want to communicate
bufferA character array where the read content will be stored.
sizebuffThe number of bytes to read.
Return values
Numberthe number of bytes that were read.
-1to indicate an error, with errno set appropriately.

errno can be:

  • EINVAL - Invalid argument (handle it's set to a null pointer)
  • read(2) command errors

Definition at line 259 of file client.c.

int hg_rawwrite ( hg_handle handle,
const char *  buffer,
size_t  sizebuff 
)

Will write the buffer to server for the connection establish by the handle.

This function will be used when one of the input channels will be received from the command server. ('I' or 'L' channels)

Parameters
handleThe handle of the connection, wherewith I want to communicate
bufferA null terminated character string of the content to write.
sizebuffThe number of bytes to write
Return values
Numberthe number of bytes that were written
-1to indicate an error, with errno set appropriately.

errno can be:

  • EINVAL - Invalid argument (handle it's set to a null pointer)
  • write(2) command errors

Definition at line 292 of file client.c.

char hg_channel ( hg_handle handle)

Reading the current channel.

Before you read or write data, you will want to know for what kind of data is server waiting, an input data or an output data. This function will return the current channel for the connection established by the handle.

Parameters
handleThe handle of the connection, wherewith I want to communicate
Return values
0if successful
-1to indicate an error, with errno set appropriately.

errno can be:

  • EINVAL - Invalid argument (handle it's set to a null pointer)

Definition at line 319 of file client.c.

hg_header hg_head ( hg_handle handle)

Return the current header.

Sometimes, the user needs the entire header. This is a way to do that.

Parameters
handleThe handle of the connection, wherewith I want to communicate
Return values
hg_headerthe header stucture for the given handle.
NULLto indicate an error, with errno set appropriately.

errno can be:

  • EINVAL - Invalid argument (handle it's set to a null pointer)

Definition at line 332 of file client.c.

int hg_exitcode ( hg_handle handle)

The exitcode for the current command.

The server tell use that he finished his action when the 'r' channel is send. This function will get the exitcode from the server, in as a measure to tell that the command was finished.

Parameters
handleThe handle of the connection, wherewith I want to communicate
Return values
exitcodeif successful
-1to indicate an error, with errno set appropriately.

errno can be:

  • EINVAL - Invalid argument (handle it's set to a null pointer)
  • read(2) command errors

Definition at line 340 of file client.c.