1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/* client.c - libraries for acting as a plugin.
*
* Copyright (C) 1998 Chris Lahey.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "client.h"
#include <glib.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
client_info empty_info = { NULL, NULL };
static gchar *
get_physical_block( gint length, gint fd )
{
gint bytes;
gchar *buffer, *start;
start = buffer = g_malloc0( length + 1 );
while( length > 0 )
{
do
{
bytes = read( fd, buffer, length );
} while ( ( bytes == -1 ) && ( ( errno==EAGAIN ) || ( errno==EINTR ) ) );
if ( bytes == -1 )
{
g_warning( "Plugin: Error reading from application\n" );
return NULL;
}
if ( bytes == 0 )
{
g_warning( "Plugin: Error EOF read?\n" );
return NULL;
}
length -= bytes;
buffer += bytes;
}
return start;
}
static void
send_physical_block( gchar *buffer, gint length, gint fd )
{
gint bytes;
while( length > 0 )
{
do
{
bytes = write( fd, buffer, length );
} while ( ( bytes == -1 ) && ( ( errno==EAGAIN ) || ( errno==EINTR ) ) );
if ( bytes == -1 )
{
g_warning( "Plugin: Error writing to application\n" );
return;
}
if ( bytes == 0 )
{
g_warning( "Plugin: Error EOF write?\n" );
return;
}
length -= bytes;
buffer += bytes;
}
return;
}
static void
sendcommand( gchar command, gint fd )
{
send_physical_block( &command, sizeof( command ), fd );
#ifdef DEBUG
printf( "From: %c\n", command );
#endif
}
static int
getnumber( int fd )
{
int number = 0;
int length = sizeof( int );
char *buff = (char *) &number;
buff += sizeof( int );
while( length -= read( fd, buff - length, length ) )
/* Empty statement */;
#ifdef DEBUG
printf( "To: %d\n", number );
#endif
return number;
}
static gchar *
getblock( int fd )
{
gchar *buffer = get_physical_block( getnumber( fd ), fd );
#ifdef DEBUG
printf( "To: %s\n", buffer );
#endif
return buffer;
}
static gboolean
getbool( int fd )
{
unsigned char number = 0;
if ( read( fd, &number, 1 ) )
{
#ifdef DEBUG
printf( "To: %s\n", number ? "TRUE" : "FALSE" );
#endif
return number;
}
else return TRUE;
}
static void
sendnumber( int fd, int number )
{
write( fd, &number, sizeof( number ) );
#ifdef DEBUG
printf( "From: %d\n", number );
#endif
}
static void
sendblock( int fd, gchar *buffer, gint length )
{
sendnumber( fd, length );
#ifdef DEBUG
printf( "From: %s\n", buffer );
#endif
send_physical_block( buffer, length, fd );
}
static int fd;
static int fdsend;
static int fddata;
gint client_init( gint *argc, gchar **argv[], client_info *info )
{
int context;
if( *argc < 5 || strcmp( (*argv)[1], "-go" ) )
{
printf( "Must be run as a plugin.\n" );
_exit(1);
}
fd = atoi( (*argv)[2] );
fdsend = atoi( (*argv)[3] );
fddata = atoi( (*argv)[4] );
context = getnumber( fd );
if( *argc > 5 && ! strcmp( (*argv)[5], "--query" ) )
{
if( info->menu_location || info->suggested_accelerator)
{
sendcommand( 'r', fdsend );
if( info->menu_location )
sendblock( fdsend, info->menu_location, strlen( info->menu_location ) );
else
sendblock( fdsend, "", 0 );
if( info->suggested_accelerator )
sendblock( fdsend, info->suggested_accelerator, strlen( info->suggested_accelerator ) );
else
sendblock( fdsend, "", 0 );
}
sendcommand( 'd', fdsend );
exit( 0 );
}
argv[4] = argv[0];
*argv += 4;
*argc -= 4;
return context;
}
gint client_document_current( gint context )
{
sendcommand( 'c', fdsend );
sendnumber( fdsend, context );
return getnumber( fddata );
}
gchar *client_document_filename( gint docid )
{
gchar *filename;
sendcommand( 'f', fdsend );
sendnumber( fdsend, docid );
filename = getblock( fddata );
return filename;
}
gint client_document_new( gint context, gchar *title )
{
sendcommand( 'n', fdsend );
sendnumber( fdsend, context );
sendblock( fdsend, title, strlen( title ) );
return getnumber( fddata );
}
gint client_document_open( gint context, gchar *title )
{
sendcommand( 'o', fdsend );
sendnumber( fdsend, context );
sendblock( fdsend, title, strlen( title ) );
return getnumber( fddata );
}
/* Returns true if document was actually closed. */
gboolean client_document_close( gint docid )
{
sendcommand( 'l', fdsend );
sendnumber( fdsend, docid );
return getbool( fddata );
}
void client_text_append( gint docid, gchar *buff, gint length )
{
sendcommand( 'a', fdsend );
sendnumber( fdsend, docid );
sendblock( fdsend, buff, length );
}
void client_text_insert( gint docid, gchar *buff, gint length, gint position )
{
sendcommand( 'i', fdsend );
sendnumber( fdsend, docid );
sendnumber( fdsend, position );
sendblock( fdsend, buff, length );
}
gchar *client_text_get( gint docid )
{
sendcommand( 'g', fdsend );
sendnumber( fdsend, docid );
return getblock( fddata );
}
void client_document_show( gint docid )
{
sendcommand( 's', fdsend );
sendnumber( fdsend, docid );
}
void client_finish( gint context )
{
sendcommand( 'd', fdsend );
}
/* Returns true if program actually quit. */
gboolean client_program_quit()
{
sendcommand( 'q', fdsend );
return getbool( fddata );
}
gint client_document_get_position( gint docid )
{
sendcommand( 'p', fdsend );
sendnumber( fdsend, docid );
return getnumber( fddata );
}
gchar *client_text_get_selection_text( gint docid )
{
sendcommand( 'e', fdsend );
sendcommand( 't', fdsend );
sendnumber( fdsend, docid );
return getblock( fddata );
}
selection_range client_document_get_selection_range( gint docid )
{
selection_range return_val;
sendcommand( 'e', fdsend );
sendcommand( 'r', fdsend );
sendnumber( fdsend, docid );
return_val.start = getnumber( fddata );
return_val.end = getnumber( fddata );
return return_val;
}
void client_text_set_selection_text( gint docid, gchar *buffer, gint length )
{
sendcommand( 'e', fdsend );
sendcommand( 's', fdsend );
sendnumber( fdsend, docid );
sendblock( fdsend, buffer, length );
}
void client_document_set_auto_indent( gint docid, gint auto_indent )
{
sendcommand( 't', fdsend );
sendcommand( 'i', fdsend );
sendnumber( fdsend, docid );
sendnumber( fdsend, auto_indent );
}
void client_document_set_status_bar( gint docid, gint status_bar )
{
sendcommand( 't', fdsend );
sendcommand( 'b', fdsend );
sendnumber( fdsend, docid );
sendnumber( fdsend, status_bar );
}
void client_document_set_word_wrap( gint docid, gint word_wrap )
{
sendcommand( 't', fdsend );
sendcommand( 'w', fdsend );
sendnumber( fdsend, docid );
sendnumber( fdsend, word_wrap );
}
void client_document_set_read_only( gint docid, gint read_only )
{
sendcommand( 't', fdsend );
sendcommand( 'r', fdsend );
sendnumber( fdsend, docid );
sendnumber( fdsend, read_only );
}
void client_document_set_split_screen( gint docid, gint split_screen )
{
sendcommand( 't', fdsend );
sendcommand( 't', fdsend );
sendnumber( fdsend, docid );
sendnumber( fdsend, split_screen );
}
void client_document_set_scroll_ball( gint docid, gint scroll_ball )
{
sendcommand( 't', fdsend );
sendcommand( 'r', fdsend );
sendnumber( fdsend, docid );
sendnumber( fdsend, scroll_ball );
}
|