comparison lib-src/gnuclient.c @ 153:25f70ba0133c r20-3b3

Import from CVS: tag r20-3b3
author cvs
date Mon, 13 Aug 2007 09:38:25 +0200
parents 59463afc5666
children 85ec50267440
comparison
equal deleted inserted replaced
152:4c132ee2d62b 153:25f70ba0133c
54 #ifdef HAVE_UNISTD_H 54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h> 55 #include <unistd.h>
56 #endif /* HAVE_UNISTD_H */ 56 #endif /* HAVE_UNISTD_H */
57 #include <signal.h> 57 #include <signal.h>
58 58
59 int read_line(int, char *);
59 60
60 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \ 61 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
61 !defined(INTERNET_DOMAIN_SOCKETS) 62 !defined(INTERNET_DOMAIN_SOCKETS)
62 int 63 int
63 main (int argc, char *argv[]) 64 main (int argc, char *argv[])
89 /* We want emacs to realize that we are resuming */ 90 /* We want emacs to realize that we are resuming */
90 signal(SIGCONT, tell_emacs_to_resume); 91 signal(SIGCONT, tell_emacs_to_resume);
91 92
92 connect_type = make_connection (NULL, (u_short) 0, &s); 93 connect_type = make_connection (NULL, (u_short) 0, &s);
93 94
94 sprintf(buffer,"(gnuserv-eval '(resume-pid-console %d))", getpid()); 95 sprintf(buffer,"(gnuserv-eval '(resume-pid-console %d))", (int)getpid());
95 send_string(s, buffer); 96 send_string(s, buffer);
96 97
97 #ifdef SYSV_IPC 98 #ifdef SYSV_IPC
98 if (connect_type == (int) CONN_IPC) 99 if (connect_type == (int) CONN_IPC)
99 disconnect_from_ipc_server (s, msgp, FALSE); 100 disconnect_from_ipc_server (s, msgp, FALSE);
250 (var) = argv[i]; \ 251 (var) = argv[i]; \
251 } \ 252 } \
252 over = 1; \ 253 over = 1; \
253 } while (0) 254 } while (0)
254 255
256 /* A strdup immitation. */
257 static char *
258 my_strdup (CONST char *s)
259 {
260 char *new = malloc (strlen (s) + 1);
261 if (new)
262 strcpy (new, s);
263 return new;
264 }
255 265
256 int 266 int
257 main (int argc, char *argv[]) 267 main (int argc, char *argv[])
258 { 268 {
259 int starting_line = 1; /* line to start editing at */ 269 int starting_line = 1; /* line to start editing at */
266 finish */ 276 finish */
267 int batch = 0; /* batch mode */ 277 int batch = 0; /* batch mode */
268 int view = 0; /* view only. */ 278 int view = 0; /* view only. */
269 int nofiles = 0; 279 int nofiles = 0;
270 int errflg = 0; /* option error */ 280 int errflg = 0; /* option error */
271 int c; /* char from getopt */
272 int s; /* socket / msqid to server */ 281 int s; /* socket / msqid to server */
273 int connect_type; /* CONN_UNIX, CONN_INTERNET, or 282 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
274 * CONN_IPC */ 283 * CONN_IPC */
275 int suppress_windows_system = 0; 284 int suppress_windows_system = 0;
276 char *display; 285 char *display = NULL;
277 #ifdef INTERNET_DOMAIN_SOCKETS 286 #ifdef INTERNET_DOMAIN_SOCKETS
278 char *hostarg = NULL; /* remote hostname */ 287 char *hostarg = NULL; /* remote hostname */
279 char *remotearg; 288 char *remotearg;
280 char thishost[HOSTNAMSZ]; /* this hostname */ 289 char thishost[HOSTNAMSZ]; /* this hostname */
281 char remotepath[MAXPATHLEN+1]; /* remote pathname */ 290 char remotepath[MAXPATHLEN+1]; /* remote pathname */
303 progname = argv[0]; 312 progname = argv[0];
304 313
305 display = getenv ("DISPLAY"); 314 display = getenv ("DISPLAY");
306 if (!display) 315 if (!display)
307 suppress_windows_system = 1; 316 suppress_windows_system = 1;
317 else
318 display = my_strdup (display);
308 319
309 for (i = 1; argv[i] && !errflg; i++) 320 for (i = 1; argv[i] && !errflg; i++)
310 { 321 {
311 if (*argv[i] != '-') 322 if (*argv[i] != '-')
312 break; 323 break;
313 if (!strcmp (argv[i], "-batch")) 324 else if (*argv[i] == '-'
325 && (*(argv[i] + 1) == '\0'
326 || (*(argv[i] + 1) == '-' && *(argv[i] + 2) == '\0')))
327 {
328 /* `-' or `--' */
329 ++i;
330 break;
331 }
332
333 if (!strcmp (argv[i], "-batch") || !strcmp (argv[i], "--batch"))
314 batch = 1; 334 batch = 1;
315 else if (!strcmp (argv[i], "-eval")) 335 else if (!strcmp (argv[i], "-eval") || !strcmp (argv[i], "--eval"))
316 { 336 {
317 if (!argv[++i]) 337 if (!argv[++i])
318 { 338 {
319 fprintf (stderr, "%s: `-eval' must be followed by an argument\n", 339 fprintf (stderr, "%s: `-eval' must be followed by an argument\n",
320 progname); 340 progname);
321 exit (1); 341 exit (1);
322 } 342 }
323 eval_form = argv[i]; 343 eval_form = argv[i];
324 } 344 }
325 else if (!strcmp (argv[i], "-display")) 345 else if (!strcmp (argv[i], "-display") || !strcmp (argv[i], "--display"))
326 { 346 {
327 suppress_windows_system = 0; 347 suppress_windows_system = 0;
328 if (!argv[++i]) 348 if (!argv[++i])
329 { 349 {
330 fprintf (stderr, "%s: `-display' must be followed by an argument\n", 350 fprintf (stderr,
351 "%s: `-display' must be followed by an argument\n",
331 progname); 352 progname);
332 exit (1); 353 exit (1);
333 } 354 }
355 if (display)
356 free (display);
357 /* no need to strdup. */
334 display = argv[i]; 358 display = argv[i];
335 } 359 }
336 else if (!strcmp (argv[i], "-nw")) 360 else if (!strcmp (argv[i], "-nw"))
337 suppress_windows_system = 1; 361 suppress_windows_system = 1;
338 else 362 else
394 { 418 {
395 fprintf (stderr, "%s: Cannot specify `-batch' with file names\n", 419 fprintf (stderr, "%s: Cannot specify `-batch' with file names\n",
396 progname); 420 progname);
397 exit (1); 421 exit (1);
398 } 422 }
423 if (suppress_windows_system && hostarg)
424 {
425 fprintf (stderr, "%s: Remote editing is available only on X\n",
426 progname);
427 exit (1);
428 }
429
399 *result = '\0'; 430 *result = '\0';
400 if (eval_function || eval_form || load_library) 431 if (eval_function || eval_form || load_library)
401 { 432 {
402 #if defined(INTERNET_DOMAIN_SOCKETS) 433 #if defined(INTERNET_DOMAIN_SOCKETS)
403 connect_type = make_connection (hostarg, port, &s); 434 connect_type = make_connection (hostarg, port, &s);
444 if (!tty) 475 if (!tty)
445 { 476 {
446 fprintf (stderr, "%s: Not connected to a tty", progname); 477 fprintf (stderr, "%s: Not connected to a tty", progname);
447 exit (1); 478 exit (1);
448 } 479 }
449 }
450
451 #if defined(INTERNET_DOMAIN_SOCKETS) 480 #if defined(INTERNET_DOMAIN_SOCKETS)
452 connect_type = make_connection (hostarg, port, &s); 481 connect_type = make_connection (hostarg, port, &s);
453 #else 482 #else
454 connect_type = make_connection (NULL, (u_short) 0, &s); 483 connect_type = make_connection (NULL, (u_short) 0, &s);
455 #endif 484 #endif
456 485 send_string (s, "(gnuserv-eval '(emacs-pid))");
457 send_string (s, "(gnuserv-eval '(emacs-pid))"); 486 send_string (s, EOT_STR);
458 send_string (s, EOT_STR); 487
459 488 if (read_line (s, buffer) == 0)
460 if (read_line (s, buffer) == 0) 489 {
461 { 490 fprintf (stderr, "%s: Could not establish Emacs procces id\n",
462 fprintf (stderr, "%s: Could not establish emacs procces id\n", 491 progname);
463 progname); 492 exit (1);
464 exit (1); 493 }
465 }
466 /* Don't do disconnect_from_server becasue we have already read 494 /* Don't do disconnect_from_server becasue we have already read
467 data, and disconnect doesn't do anything else. */ 495 data, and disconnect doesn't do anything else. */
468 #ifndef INTERNET_DOMAIN_SOCKETS 496 #ifndef INTERNET_DOMAIN_SOCKETS
469 if (connect_type == (int) CONN_IPC) 497 if (connect_type == (int) CONN_IPC)
470 disconnect_from_ipc_server (s, msgp, FALSE); 498 disconnect_from_ipc_server (s, msgp, FALSE);
471 #endif /* !SYSV_IPC */ 499 #endif /* !SYSV_IPC */
472 500
473 emacs_pid = (pid_t)atol(buffer); 501 emacs_pid = (pid_t)atol(buffer);
474 initialize_signals(); 502 initialize_signals();
503 } /* suppress_windows_system */
475 504
476 #if defined(INTERNET_DOMAIN_SOCKETS) 505 #if defined(INTERNET_DOMAIN_SOCKETS)
477 connect_type = make_connection (hostarg, port, &s); 506 connect_type = make_connection (hostarg, port, &s);
478 #else 507 #else
479 connect_type = make_connection (NULL, (u_short) 0, &s); 508 connect_type = make_connection (NULL, (u_short) 0, &s);
526 { 555 {
527 fprintf (stderr, "%s: unknown terminal type\n", progname); 556 fprintf (stderr, "%s: unknown terminal type\n", progname);
528 exit (1); 557 exit (1);
529 } 558 }
530 sprintf (command, "(gnuserv-edit-files '(tty %s %s %d) '(", 559 sprintf (command, "(gnuserv-edit-files '(tty %s %s %d) '(",
531 clean_string (tty), clean_string (term), getpid ()); 560 clean_string (tty), clean_string (term), (int)getpid ());
532 } 561 }
533 else /* !suppress_windows_system */ 562 else /* !suppress_windows_system */
534 { 563 {
535 sprintf (command, "(gnuserv-edit-files '(x %s) '(", 564 sprintf (command, "(gnuserv-edit-files '(x %s) '(",
536 clean_string (display)); 565 clean_string (display));
555 filename_expand (fullpath, argv[i]); 584 filename_expand (fullpath, argv[i]);
556 #ifdef INTERNET_DOMAIN_SOCKETS 585 #ifdef INTERNET_DOMAIN_SOCKETS
557 path = malloc (strlen (remotepath) + strlen (fullpath) + 1); 586 path = malloc (strlen (remotepath) + strlen (fullpath) + 1);
558 sprintf (path, "%s%s", remotepath, fullpath); 587 sprintf (path, "%s%s", remotepath, fullpath);
559 #else 588 #else
560 path = malloc (strlen (fullpath)); 589 path = my_strdup (fullpath);
561 strcpy (path, fullpath);
562 #endif 590 #endif
563 sprintf (command, "(%d . %s)", starting_line, clean_string (path)); 591 sprintf (command, "(%d . %s)", starting_line, clean_string (path));
564 send_string (s, command); 592 send_string (s, command);
565 free (path); 593 free (path);
566 } /* for */ 594 } /* for */