lib/eggdrop/linemode.c File Reference

#include <eggdrop/eggdrop.h>
#include <unistd.h>

Go to the source code of this file.

Data Structures

struct  linemode_t

Defines

#define LINEMODE_LEVEL   SOCKBUF_LEVEL_TEXT_BUFFER

Functions

static int linemode_on_read (void *client_data, int idx, char *data, int len)
static int linemode_on_eof (void *client_data, int idx, int err, const char *errmsg)
static int linemode_on_delete (void *client_data, int idx)
int linemode_on (int idx)
 Turns linebuffering on an index on.
int linemode_off (int idx)
 Turns linebuffering on an index off.
int linemode_check (int idx)
 Checks if an index is in linebuffered mode.

Variables

static const char rcsid [] = "$Id: linemode.c,v 1.9 2006-11-14 14:51:23 sven Exp $"
static sockbuf_filter_t linemode_filter


Define Documentation

#define LINEMODE_LEVEL   SOCKBUF_LEVEL_TEXT_BUFFER

Definition at line 27 of file linemode.c.

Referenced by linemode_off(), linemode_on_eof(), and linemode_on_read().


Function Documentation

int linemode_check ( int  idx  ) 

Checks if an index is in linebuffered mode.

Parameters:
idx the index to check
Returns:
1 if linebuffered, 0 if not linebuffered, -1 on error

Definition at line 180 of file linemode.c.

References sockbuf_get_filter_data(), and sockbuf_isvalid().

Referenced by linemode_on(), and script_net_linemode().

00181 {
00182   if (!sockbuf_isvalid(idx)) return -1;
00183   return !sockbuf_get_filter_data(idx, &linemode_filter, 0);
00184 }

int linemode_off ( int  idx  ) 

Turns linebuffering on an index off.

If there if a line currently buffered it's send of immidiatly.

Parameters:
idx the index to turn off linebuffering for
Returns:
0 on success, -1 on error

Definition at line 152 of file linemode.c.

References linemode_t::data, linemode_t::len, LINEMODE_LEVEL, sockbuf_detach_filter(), and sockbuf_on_read().

Referenced by script_net_linemode().

00153 {
00154   int ret;
00155   linemode_t *old_data;
00156 
00157   ret = sockbuf_detach_filter(idx, &linemode_filter, &old_data);
00158   if (ret) return ret;
00159   if (old_data) {
00160     if (old_data->data) {
00161       if (old_data->len) {
00162         old_data->data[old_data->len] = 0;
00163         sockbuf_on_read(idx, LINEMODE_LEVEL, old_data->data, old_data->len);
00164         old_data->len = 0;
00165       }
00166       free(old_data->data);
00167     }
00168     free(old_data);
00169   }
00170   return(0);
00171 }

int linemode_on ( int  idx  ) 

Turns linebuffering on an index on.

Parameters:
idx the index to turn on linebuffering for
Returns:
0 on success, -1 on error

Definition at line 133 of file linemode.c.

References linemode_check(), and sockbuf_attach_filter().

Referenced by do_link(), got_chat_request(), http_reconnect(), irc_on_newclient(), script_net_linemode(), server_on_connect(), telnet_on_newclient(), and terminal_init().

00134 {
00135   linemode_t *old_data;
00136 
00137   if (linemode_check(idx)) return -1;
00138   old_data = calloc(1, sizeof(*old_data));
00139   sockbuf_attach_filter(idx, &linemode_filter, old_data);
00140   return(0);
00141 }

static int linemode_on_delete ( void *  client_data,
int  idx 
) [static]

Definition at line 109 of file linemode.c.

References linemode_t::data.

00110 {
00111   linemode_t *old_data = client_data;
00112   /* When the sockbuf is deleted, just kill the associated data. */
00113   if (old_data->data) free(old_data->data);
00114   free(old_data);
00115   return(0);
00116 }

static int linemode_on_eof ( void *  client_data,
int  idx,
int  err,
const char *  errmsg 
) [static]

Definition at line 95 of file linemode.c.

References linemode_t::data, linemode_t::len, LINEMODE_LEVEL, sockbuf_on_eof(), and sockbuf_on_read().

00096 {
00097   linemode_t *old_data = client_data;
00098   /* If there is any buffered data, do one more on->read callback. */
00099   if (old_data->len) {
00100     old_data->data[old_data->len] = 0;
00101     sockbuf_on_read(idx, LINEMODE_LEVEL, old_data->data, old_data->len);
00102     old_data->len = 0;
00103   }
00104 
00105   /* And now continue the eof event chain. */
00106   return sockbuf_on_eof(idx, LINEMODE_LEVEL, err, errmsg);
00107 }

static int linemode_on_read ( void *  client_data,
int  idx,
char *  data,
int  len 
) [static]

Definition at line 34 of file linemode.c.

References linemode_t::data, linemode_t::len, LINEMODE_LEVEL, sockbuf_isvalid(), and sockbuf_on_read().

00035 {
00036   linemode_t *old_data = client_data;
00037   char *line, *stop, *save;
00038   int linelen, savelen;
00039 
00040   while (1) {
00041     /* If there's a cr or lf, we have a line. */
00042     stop = memchr(data, '\n', len);
00043     if (!stop) {
00044       stop = memchr(data, '\r', len);
00045       if (!stop) {
00046         /* No line, save the whole thing. */
00047         save = data;
00048         savelen = len;
00049         break;
00050       }
00051     }
00052 
00053     /* Save the cursor position for the next iteration. */
00054     save = stop+1;
00055     savelen = len - (save - data);
00056 
00057     if (stop > data && *(stop-1) == '\r') stop--;
00058     linelen = stop - data;
00059     *stop = 0;
00060 
00061     /* If there is buffered data, concat it all. */
00062     if (old_data->len) {
00063       /* Expand the buffer. */
00064       old_data->data = realloc(old_data->data, old_data->len + linelen + 1);
00065       memcpy(old_data->data+old_data->len, data, linelen + 1);
00066 
00067       line = old_data->data;
00068       linelen += old_data->len;
00069 
00070       /* All the old data is used. */
00071       old_data->len = 0;
00072     }
00073     else {
00074       line = data;
00075     }
00076 
00077     sockbuf_on_read(idx, LINEMODE_LEVEL, line, linelen);
00078 
00079     if (!sockbuf_isvalid(idx)) return 0; /* We've been deleted */
00080 
00081     /* If we're out of data, we're done. */
00082     if (savelen <= 0) return(0);
00083     /* Otherwise, do this again to check for another line. */
00084     data = save;
00085     len = savelen;
00086   }
00087 
00088   /* No cr/lf, so we save the remaining data for next time. */
00089   old_data->data = realloc(old_data->data, savelen + old_data->len + 1);
00090   memcpy(old_data->data + old_data->len, save, savelen);
00091   old_data->len += savelen;
00092   return(0);
00093 }


Variable Documentation

Initial value:

 {
  "linemode",
  LINEMODE_LEVEL,
  NULL, linemode_on_eof, NULL,
  linemode_on_read, NULL, NULL,
  NULL, linemode_on_delete
}

Definition at line 118 of file linemode.c.

const char rcsid[] = "$Id: linemode.c,v 1.9 2006-11-14 14:51:23 sven Exp $" [static]

Definition at line 21 of file linemode.c.


Generated on Sun Nov 30 18:43:34 2008 for eggdrop1.9 by  doxygen 1.5.6