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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
|
/* -*-C-*-
Client code to allow local and remote editing of files by XEmacs.
Copyright (C) 1989 Free Software Foundation, Inc.
Copyright (C) 1995 Sun Microsystems, Inc.
Copyright (C) 1997 Free Software Foundation, Inc.
This file is part of XEmacs.
XEmacs 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.
XEmacs 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 XEmacs; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Andy Norman (ange@hplb.hpl.hp.com), based on
'etc/emacsclient.c' from the GNU Emacs 18.52 distribution.
Please mail bugs and suggestions to the XEmacs maintainer.
*/
/* #### This file should be a windows-mode, not console-mode program under
Windows. (i.e. its entry point should be WinMain.) gnuattach functionality,
to the extent it's used at all, should be retrieved using a script that
calls the i.exe wrapper program, to obtain stdio handles.
#### For that matter, both the functionality of gnuclient and gnuserv
should be merged into XEmacs itself using a -remote arg, just like
Netscape and other modern programs.
--ben */
/*
* This file incorporates new features added by Bob Weiner <weiner@mot.com>,
* Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
* GNUATTACH support added by Ben Wing <wing@xemacs.org>.
* Please see the note at the end of the README file for details.
*
* (If gnuserv came bundled with your emacs, the README file is probably
* ../etc/gnuserv.README relative to the directory containing this file)
*/
#ifdef CYGWIN
extern void cygwin_conv_to_posix_path(const char *path, char *posix_path);
#endif
#include "gnuserv.h"
char gnuserv_version[] = "gnuclient version " GNUSERV_VERSION;
#include "getopt.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sysfile.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <signal.h>
#if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
!defined(INTERNET_DOMAIN_SOCKETS)
int
main (int argc, char *argv[])
{
fprintf (stderr, "Sorry, the Emacs server is only "
"supported on systems that have\n");
fprintf (stderr, "Unix Domain sockets, Internet Domain "
"sockets or System V IPC.\n");
exit (1);
} /* main */
#else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
static char cwd[MAXPATHLEN+2]; /* current working directory when calculated */
static char *cp = NULL; /* ptr into valid bit of cwd above */
static pid_t emacs_pid; /* Process id for emacs process */
void initialize_signals (void);
static void
tell_emacs_to_resume (int sig)
{
char buffer[GSERV_BUFSZ+1];
int s; /* socket / msqid to server */
int connect_type; /* CONN_UNIX, CONN_INTERNET, or
ONN_IPC */
/* Why is SYSV so retarded? */
/* We want emacs to realize that we are resuming */
#ifdef SIGCONT
signal(SIGCONT, tell_emacs_to_resume);
#endif
connect_type = make_connection (NULL, 0, &s);
sprintf(buffer,"(gnuserv-eval '(resume-pid-console %d))", (int)getpid());
send_string(s, buffer);
#ifdef SYSV_IPC
if (connect_type == (int) CONN_IPC)
disconnect_from_ipc_server (s, msgp, FALSE);
#else /* !SYSV_IPC */
if (connect_type != (int) CONN_IPC)
disconnect_from_server (s, FALSE);
#endif /* !SYSV_IPC */
}
static void
pass_signal_to_emacs (int sig)
{
if (kill (emacs_pid, sig) == -1)
{
fprintf (stderr, "gnuattach: Could not pass signal to emacs process\n");
exit (1);
}
initialize_signals ();
}
void
initialize_signals (void)
{
/* Set up signal handler to pass relevant signals to emacs process.
We used to send SIGSEGV, SIGBUS, SIGPIPE, SIGILL and others to
Emacs, but I think it's better not to. I can see no reason why
Emacs should SIGSEGV whenever gnuclient SIGSEGV-s, etc. */
signal (SIGQUIT, pass_signal_to_emacs);
signal (SIGINT, pass_signal_to_emacs);
#ifdef SIGWINCH
signal (SIGWINCH, pass_signal_to_emacs);
#endif
#ifdef SIGCONT
/* We want emacs to realize that we are resuming */
signal (SIGCONT, tell_emacs_to_resume);
#endif
}
/*
get_current_working_directory -- return the cwd.
*/
static char *
get_current_working_directory (void)
{
if (cp == NULL)
{ /* haven't calculated it yet */
#ifdef HAVE_GETCWD
if (getcwd (cwd,MAXPATHLEN) == NULL)
#else
if (getwd (cwd) == 0)
#endif /* HAVE_GETCWD */
{
perror (progname);
fprintf (stderr, "%s: unable to get current working directory\n",
progname);
exit (1);
} /* if */
/* on some systems, cwd can look like '@machine/' ... */
/* ignore everything before the first '/' */
for (cp = cwd; *cp && *cp != '/'; ++cp)
;
} /* if */
return cp;
} /* get_current_working_directory */
/*
filename_expand -- try to convert the given filename into a fully-qualified
pathname.
*/
static void
filename_expand (char *fullpath, char *filename)
/* fullpath - returned full pathname */
/* filename - filename to expand */
{
#ifdef CYGWIN
char cygwinFilename[MAXPATHLEN+1];
#endif
int len;
fullpath[0] = fullpath[MAXPATHLEN] = '\0';
#ifdef CYGWIN
/*
If we're in cygwin, just convert it and let the unix stuff handle it.
*/
cygwin_conv_to_posix_path(filename, cygwinFilename);
filename = cygwinFilename;
#endif
if (filename[0] && filename[0] == '/')
{
/* Absolute (unix-style) pathname. Do nothing */
strncat (fullpath, filename, MAXPATHLEN);
}
else
{
/* Assume relative Unix style path. Get the current directory
and prepend it. FIXME: need to fix the case of DOS paths like
"\foo", where we need to get the current drive. */
strncat (fullpath, get_current_working_directory (), MAXPATHLEN);
len = strlen (fullpath);
/* If no trailing slash, add one */
if (len <= 0 || (fullpath[len - 1] != '/' && len < MAXPATHLEN))
{
strcat (fullpath, "/");
len++;
}
/* Don't forget to add the filename! */
strncat (fullpath, filename, MAXPATHLEN - len);
}
} /* filename_expand */
/* Encase the string in quotes, escape all the backslashes and quotes
in string. */
static char *
clean_string (const char *s)
{
int i = 0;
char *p, *res;
{
const char *const_p;
for (const_p = s; *const_p; const_p++, i++)
{
if (*const_p == '\\' || *const_p == '\"')
++i;
else if (*const_p == '\004')
i += 3;
}
}
p = res = (char *) malloc (i + 2 + 1);
*p++ = '\"';
for (; *s; p++, s++)
{
switch (*s)
{
case '\\':
*p++ = '\\';
*p = '\\';
break;
case '\"':
*p++ = '\\';
*p = '\"';
break;
case '\004':
*p++ = '\\';
*p++ = 'C';
*p++ = '-';
*p = 'd';
break;
default:
*p = *s;
}
}
*p++ = '\"';
*p = '\0';
return res;
}
#define GET_ARGUMENT(var, desc) do { \
if (*(p + 1)) (var) = p + 1; \
else \
{ \
if (!argv[++i]) \
{ \
fprintf (stderr, "%s: `%s' must be followed by an argument\n", \
progname, desc); \
exit (1); \
} \
(var) = argv[i]; \
} \
over = 1; \
} while (0)
/* A strdup imitation. */
static char *
my_strdup (const char *s)
{
char *new_s = (char *) malloc (strlen (s) + 1);
if (new_s)
strcpy (new_s, s);
return new_s;
}
int
main (int argc, char *argv[])
{
int starting_line = 1; /* line to start editing at */
char command[MAXPATHLEN+50]; /* emacs command buffer */
char fullpath[MAXPATHLEN+1]; /* full pathname to file */
char *eval_form = NULL; /* form to evaluate with `-eval' */
char *eval_function = NULL; /* function to evaluate with `-f' */
char *load_library = NULL; /* library to load */
int quick = 0; /* quick edit, don't wait for user to
finish */
int batch = 0; /* batch mode */
int view = 0; /* view only. */
int nofiles = 0;
int errflg = 0; /* option error */
int s; /* socket / msqid to server */
int connect_type; /* CONN_UNIX, CONN_INTERNET, or
* CONN_IPC */
int suppress_windows_system = 0;
char *display = NULL;
#ifdef INTERNET_DOMAIN_SOCKETS
char *hostarg = NULL; /* remote hostname */
char *remotearg;
char thishost[HOSTNAMSZ]; /* this hostname */
char remotepath[MAXPATHLEN+1]; /* remote pathname */
int rflg = 0; /* pathname given on cmdline */
char *portarg;
unsigned short port = 0; /* port to server */
#endif /* INTERNET_DOMAIN_SOCKETS */
char *path; /* used indiscriminately */
#ifdef SYSV_IPC
struct msgbuf *msgp; /* message */
#endif /* SYSV_IPC */
char *tty = NULL;
char buffer[GSERV_BUFSZ + 1]; /* buffer to read pid */
char result[GSERV_BUFSZ + 1];
int i;
#ifdef INTERNET_DOMAIN_SOCKETS
memset (remotepath, 0, sizeof (remotepath));
#endif /* INTERNET_DOMAIN_SOCKETS */
progname = strrchr (argv[0], '/');
if (progname)
++progname;
else
progname = argv[0];
#ifdef USE_TMPDIR
tmpdir = getenv ("TMPDIR");
#endif
if (!tmpdir)
tmpdir = "/tmp";
display = getenv ("DISPLAY");
if (display)
display = my_strdup (display);
#ifndef HAVE_MS_WINDOWS
else
suppress_windows_system = 1;
#endif
for (i = 1; argv[i] && !errflg; i++)
{
if (*argv[i] != '-')
break;
else if (*argv[i] == '-'
&& (*(argv[i] + 1) == '\0'
|| (*(argv[i] + 1) == '-' && *(argv[i] + 2) == '\0')))
{
/* `-' or `--' */
++i;
break;
}
if (!strcmp (argv[i], "-batch") || !strcmp (argv[i], "--batch"))
batch = 1;
else if (!strcmp (argv[i], "-eval") || !strcmp (argv[i], "--eval"))
{
if (!argv[++i])
{
fprintf (stderr, "%s: `-eval' must be followed by an argument\n",
progname);
exit (1);
}
eval_form = argv[i];
}
else if (!strcmp (argv[i], "-display") || !strcmp (argv[i], "--display"))
{
suppress_windows_system = 0;
if (!argv[++i])
{
fprintf (stderr,
"%s: `-display' must be followed by an argument\n",
progname);
exit (1);
}
if (display)
free (display);
/* no need to strdup. */
display = argv[i];
}
else if (!strcmp (argv[i], "-nw"))
suppress_windows_system = 1;
else
{
/* Iterate over one-letter options. */
char *p;
int over = 0;
for (p = argv[i] + 1; *p && !over; p++)
{
switch (*p)
{
case 'q':
quick = 1;
break;
case 'v':
view = 1;
break;
case 'f':
GET_ARGUMENT (eval_function, "-f");
break;
case 'l':
GET_ARGUMENT (load_library, "-l");
break;
#ifdef INTERNET_DOMAIN_SOCKETS
case 'h':
GET_ARGUMENT (hostarg, "-h");
break;
case 'p':
GET_ARGUMENT (portarg, "-p");
port = atoi (portarg);
break;
case 'r':
GET_ARGUMENT (remotearg, "-r");
strncpy (remotepath, remotearg, MAXPATHLEN);
rflg = 1;
break;
#endif /* INTERNET_DOMAIN_SOCKETS */
default:
errflg = 1;
}
} /* for */
} /* else */
} /* for */
if (errflg)
{
fprintf (stderr,
#ifdef INTERNET_DOMAIN_SOCKETS
"Usage: %s [-nw] [-display display] [-q] [-v] [-l library]\n"
" [-batch] [-f function] [-eval form]\n"
" [-h host] [-p port] [-r remote-path] [[+line] file] ...\n",
#else /* !INTERNET_DOMAIN_SOCKETS */
"Usage: %s [-nw] [-q] [-v] [-l library] [-f function] [-eval form] "
"[[+line] path] ...\n",
#endif /* !INTERNET_DOMAIN_SOCKETS */
progname);
exit (1);
}
if (batch && argv[i])
{
fprintf (stderr, "%s: Cannot specify `-batch' with file names\n",
progname);
exit (1);
}
#if defined(INTERNET_DOMAIN_SOCKETS)
if (suppress_windows_system && hostarg)
{
fprintf (stderr, "%s: Remote editing is available only on X\n",
progname);
exit (1);
}
#endif
*result = '\0';
if (eval_function || eval_form || load_library)
{
#if defined(INTERNET_DOMAIN_SOCKETS)
connect_type = make_connection (hostarg, port, &s);
#else
connect_type = make_connection (NULL, 0, &s);
#endif
sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
send_string (s, command);
if (load_library)
{
send_string (s , "(load-library ");
send_string (s, clean_string(load_library));
send_string (s, ") ");
}
if (eval_form)
{
send_string (s, eval_form);
}
if (eval_function)
{
send_string (s, "(");
send_string (s, eval_function);
send_string (s, ")");
}
send_string (s, "))");
/* disconnect already sends EOT_STR */
#ifdef SYSV_IPC
if (connect_type == (int) CONN_IPC)
disconnect_from_ipc_server (s, msgp, batch && !quick);
#else /* !SYSV_IPC */
if (connect_type != (int) CONN_IPC)
disconnect_from_server (s, batch && !quick);
#endif /* !SYSV_IPC */
} /* eval_function || eval_form || load_library */
else if (batch)
{
/* no sexp on the command line, so read it from stdin */
int nb;
#if defined(INTERNET_DOMAIN_SOCKETS)
connect_type = make_connection (hostarg, port, &s);
#else
connect_type = make_connection (NULL, 0, &s);
#endif
sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
send_string (s, command);
while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ-1)) > 0)
{
buffer[nb] = '\0';
send_string(s, buffer);
}
send_string(s,"))");
/* disconnect already sends EOT_STR */
#ifdef SYSV_IPC
if (connect_type == (int) CONN_IPC)
disconnect_from_ipc_server (s, msgp, batch && !quick);
#else /* !SYSV_IPC */
if (connect_type != (int) CONN_IPC)
disconnect_from_server (s, batch && !quick);
#endif /* !SYSV_IPC */
}
if (!batch)
{
if (suppress_windows_system)
{
tty = ttyname (0);
if (!tty)
{
fprintf (stderr, "%s: Not connected to a tty", progname);
exit (1);
}
#if defined(INTERNET_DOMAIN_SOCKETS)
connect_type = make_connection (hostarg, port, &s);
#else
connect_type = make_connection (NULL, 0, &s);
#endif
send_string (s, "(gnuserv-eval '(emacs-pid))");
send_string (s, EOT_STR);
if (read_line (s, buffer) == 0)
{
fprintf (stderr, "%s: Could not establish Emacs process id\n",
progname);
exit (1);
}
/* Don't do disconnect_from_server because we have already read
data, and disconnect doesn't do anything else. */
#ifdef SYSV_IPC
if (connect_type == (int) CONN_IPC)
disconnect_from_ipc_server (s, msgp, FALSE);
#endif /* !SYSV_IPC */
emacs_pid = (pid_t)atol(buffer);
initialize_signals();
} /* suppress_windows_system */
#if defined(INTERNET_DOMAIN_SOCKETS)
connect_type = make_connection (hostarg, port, &s);
#else
connect_type = make_connection (NULL, 0, &s);
#endif
#ifdef INTERNET_DOMAIN_SOCKETS
if (connect_type == (int) CONN_INTERNET)
{
char *ptr;
gethostname (thishost, HOSTNAMSZ);
if (!rflg)
{ /* attempt to generate a path
* to this machine */
if ((ptr = getenv ("GNU_NODE")) != NULL)
/* user specified a path */
strncpy (remotepath, ptr, MAXPATHLEN);
}
#if 0 /* This is really bogus... re-enable it if you must have it! */
#if defined (__hpux)
else if (strcmp (thishost,hostarg))
{ /* try /net/thishost */
strcpy (remotepath, "/net/"); /* (this fails using internet
addresses) */
strcat (remotepath, thishost);
}
#endif
#endif
}
else
{ /* same machines, no need for path */
remotepath[0] = '\0'; /* default is the empty path */
}
#endif /* INTERNET_DOMAIN_SOCKETS */
#ifdef SYSV_IPC
if ((msgp = (struct msgbuf *)
malloc (sizeof *msgp + GSERV_BUFSZ)) == NULL)
{
fprintf (stderr, "%s: not enough memory for message buffer\n", progname);
exit (1);
} /* if */
msgp->mtext[0] = '\0'; /* ready for later strcats */
#endif /* SYSV_IPC */
if (suppress_windows_system)
{
char *term = getenv ("TERM");
if (!term)
{
fprintf (stderr, "%s: unknown terminal type\n", progname);
exit (1);
}
sprintf (command, "(gnuserv-edit-files '(tty %s %s %d) '(",
clean_string (tty), clean_string (term), (int)getpid ());
}
else /* !suppress_windows_system */
{
if (0)
;
#ifdef HAVE_X_WINDOWS
else if (display)
sprintf (command, "(gnuserv-edit-files '(x %s) '(",
clean_string (display));
#endif
#ifdef HAVE_GTK
else if (display)
strcpy (command, "(gnuserv-edit-files '(gtk nil) '(");
#endif
#ifdef HAVE_MS_WINDOWS
else
sprintf (command, "(gnuserv-edit-files '(mswindows nil) '(");
#endif
} /* !suppress_windows_system */
send_string (s, command);
if (!argv[i])
nofiles = 1;
for (; argv[i]; i++)
{
if (i < argc - 1 && *argv[i] == '+')
starting_line = atoi (argv[i++]);
else
starting_line = 1;
/* If the last argument is +something, treat it as a file. */
if (i == argc)
{
starting_line = 1;
--i;
}
filename_expand (fullpath, argv[i]);
#ifdef INTERNET_DOMAIN_SOCKETS
path = (char *) malloc (strlen (remotepath) + strlen (fullpath) + 1);
sprintf (path, "%s%s", remotepath, fullpath);
#else
path = my_strdup (fullpath);
#endif
sprintf (command, "(%d . %s)", starting_line, clean_string (path));
send_string (s, command);
free (path);
} /* for */
sprintf (command, ")%s%s",
(quick || (nofiles && !suppress_windows_system)) ? " 'quick" : "",
view ? " 'view" : "");
send_string (s, command);
send_string (s, ")");
#ifdef SYSV_IPC
if (connect_type == (int) CONN_IPC)
disconnect_from_ipc_server (s, msgp, FALSE);
#else /* !SYSV_IPC */
if (connect_type != (int) CONN_IPC)
disconnect_from_server (s, FALSE);
#endif /* !SYSV_IPC */
} /* not batch */
return 0;
} /* main */
#endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
|