Mercurial > hg > xemacs-beta
annotate lib-src/gnuslib.c @ 4952:19a72041c5ed
Mule-izing, various fixes related to char * arguments
-------------------- ChangeLog entries follow: --------------------
modules/ChangeLog addition:
2010-01-26 Ben Wing <ben@xemacs.org>
* postgresql/postgresql.c:
* postgresql/postgresql.c (CHECK_LIVE_CONNECTION):
* postgresql/postgresql.c (print_pgresult):
* postgresql/postgresql.c (Fpq_conn_defaults):
* postgresql/postgresql.c (Fpq_connectdb):
* postgresql/postgresql.c (Fpq_connect_start):
* postgresql/postgresql.c (Fpq_result_status):
* postgresql/postgresql.c (Fpq_res_status):
Mule-ize large parts of it.
2010-01-26 Ben Wing <ben@xemacs.org>
* ldap/eldap.c (print_ldap):
* ldap/eldap.c (allocate_ldap):
Use write_ascstring().
src/ChangeLog addition:
2010-01-26 Ben Wing <ben@xemacs.org>
* alloc.c:
* alloc.c (build_ascstring):
* alloc.c (build_msg_cistring):
* alloc.c (staticpro_1):
* alloc.c (staticpro_name):
* alloc.c (staticpro_nodump_1):
* alloc.c (staticpro_nodump_name):
* alloc.c (unstaticpro_nodump_1):
* alloc.c (mcpro_1):
* alloc.c (mcpro_name):
* alloc.c (object_memory_usage_stats):
* alloc.c (common_init_alloc_early):
* alloc.c (init_alloc_once_early):
* buffer.c (print_buffer):
* buffer.c (vars_of_buffer):
* buffer.c (common_init_complex_vars_of_buffer):
* buffer.c (init_initial_directory):
* bytecode.c (invalid_byte_code):
* bytecode.c (print_compiled_function):
* bytecode.c (mark_compiled_function):
* chartab.c (print_table_entry):
* chartab.c (print_char_table):
* config.h.in:
* console-gtk.c:
* console-gtk.c (gtk_device_to_console_connection):
* console-gtk.c (gtk_semi_canonicalize_console_connection):
* console-gtk.c (gtk_canonicalize_console_connection):
* console-gtk.c (gtk_semi_canonicalize_device_connection):
* console-gtk.c (gtk_canonicalize_device_connection):
* console-stream.c (stream_init_frame_1):
* console-stream.c (vars_of_console_stream):
* console-stream.c (init_console_stream):
* console-x.c (x_semi_canonicalize_console_connection):
* console-x.c (x_semi_canonicalize_device_connection):
* console-x.c (x_canonicalize_device_connection):
* console-x.h:
* data.c (eq_with_ebola_notice):
* data.c (Fsubr_interactive):
* data.c (Fnumber_to_string):
* data.c (digit_to_number):
* device-gtk.c (gtk_init_device):
* device-msw.c (print_devmode):
* device-x.c (x_event_name):
* dialog-msw.c (handle_directory_dialog_box):
* dialog-msw.c (handle_file_dialog_box):
* dialog-msw.c (vars_of_dialog_mswindows):
* doc.c (weird_doc):
* doc.c (Fsnarf_documentation):
* doc.c (vars_of_doc):
* dumper.c (pdump):
* dynarr.c:
* dynarr.c (Dynarr_realloc):
* editfns.c (Fuser_real_login_name):
* editfns.c (get_home_directory):
* elhash.c (print_hash_table_data):
* elhash.c (print_hash_table):
* emacs.c (main_1):
* emacs.c (vars_of_emacs):
* emodules.c:
* emodules.c (_emodules_list):
* emodules.c (Fload_module):
* emodules.c (Funload_module):
* emodules.c (Flist_modules):
* emodules.c (find_make_module):
* emodules.c (attempt_module_delete):
* emodules.c (emodules_load):
* emodules.c (emodules_doc_subr):
* emodules.c (emodules_doc_sym):
* emodules.c (syms_of_module):
* emodules.c (vars_of_module):
* emodules.h:
* eval.c (print_subr):
* eval.c (signal_call_debugger):
* eval.c (build_error_data):
* eval.c (signal_error):
* eval.c (maybe_signal_error):
* eval.c (signal_continuable_error):
* eval.c (maybe_signal_continuable_error):
* eval.c (signal_error_2):
* eval.c (maybe_signal_error_2):
* eval.c (signal_continuable_error_2):
* eval.c (maybe_signal_continuable_error_2):
* eval.c (signal_ferror):
* eval.c (maybe_signal_ferror):
* eval.c (signal_continuable_ferror):
* eval.c (maybe_signal_continuable_ferror):
* eval.c (signal_ferror_with_frob):
* eval.c (maybe_signal_ferror_with_frob):
* eval.c (signal_continuable_ferror_with_frob):
* eval.c (maybe_signal_continuable_ferror_with_frob):
* eval.c (syntax_error):
* eval.c (syntax_error_2):
* eval.c (maybe_syntax_error):
* eval.c (sferror):
* eval.c (sferror_2):
* eval.c (maybe_sferror):
* eval.c (invalid_argument):
* eval.c (invalid_argument_2):
* eval.c (maybe_invalid_argument):
* eval.c (invalid_constant):
* eval.c (invalid_constant_2):
* eval.c (maybe_invalid_constant):
* eval.c (invalid_operation):
* eval.c (invalid_operation_2):
* eval.c (maybe_invalid_operation):
* eval.c (invalid_change):
* eval.c (invalid_change_2):
* eval.c (maybe_invalid_change):
* eval.c (invalid_state):
* eval.c (invalid_state_2):
* eval.c (maybe_invalid_state):
* eval.c (wtaerror):
* eval.c (stack_overflow):
* eval.c (out_of_memory):
* eval.c (print_multiple_value):
* eval.c (issue_call_trapping_problems_warning):
* eval.c (backtrace_specials):
* eval.c (backtrace_unevalled_args):
* eval.c (Fbacktrace):
* eval.c (warn_when_safe):
* event-Xt.c (modwarn):
* event-Xt.c (modbarf):
* event-Xt.c (check_modifier):
* event-Xt.c (store_modifier):
* event-Xt.c (emacs_Xt_format_magic_event):
* event-Xt.c (describe_event):
* event-gtk.c (dragndrop_data_received):
* event-gtk.c (store_modifier):
* event-gtk.c (gtk_reset_modifier_mapping):
* event-msw.c (dde_eval_string):
* event-msw.c (Fdde_alloc_advise_item):
* event-msw.c (mswindows_dde_callback):
* event-msw.c (FROB):
* event-msw.c (emacs_mswindows_format_magic_event):
* event-stream.c (external_debugging_print_event):
* event-stream.c (execute_help_form):
* event-stream.c (vars_of_event_stream):
* events.c (print_event_1):
* events.c (print_event):
* events.c (event_equal):
* extents.c (print_extent_1):
* extents.c (print_extent):
* extents.c (vars_of_extents):
* faces.c (print_face):
* faces.c (complex_vars_of_faces):
* file-coding.c:
* file-coding.c (print_coding_system):
* file-coding.c (print_coding_system_in_print_method):
* file-coding.c (default_query_method):
* file-coding.c (find_coding_system):
* file-coding.c (make_coding_system_1):
* file-coding.c (chain_print):
* file-coding.c (undecided_print):
* file-coding.c (gzip_print):
* file-coding.c (vars_of_file_coding):
* file-coding.c (complex_vars_of_file_coding):
* fileio.c:
* fileio.c (report_file_type_error):
* fileio.c (report_error_with_errno):
* fileio.c (report_file_error):
* fileio.c (barf_or_query_if_file_exists):
* fileio.c (vars_of_fileio):
* floatfns.c (matherr):
* fns.c (print_bit_vector):
* fns.c (Fmapconcat):
* fns.c (add_suffix_to_symbol):
* fns.c (add_prefix_to_symbol):
* frame-gtk.c:
* frame-gtk.c (Fgtk_window_id):
* frame-x.c (def):
* frame-x.c (x_cde_transfer_callback):
* frame.c:
* frame.c (Fmake_frame):
* gc.c (show_gc_cursor_and_message):
* gc.c (vars_of_gc):
* glyphs-eimage.c (png_instantiate):
* glyphs-eimage.c (tiff_instantiate):
* glyphs-gtk.c (gtk_print_image_instance):
* glyphs-msw.c (mswindows_print_image_instance):
* glyphs-x.c (x_print_image_instance):
* glyphs-x.c (update_widget_face):
* glyphs.c (make_string_from_file):
* glyphs.c (print_image_instance):
* glyphs.c (signal_image_error):
* glyphs.c (signal_image_error_2):
* glyphs.c (signal_double_image_error):
* glyphs.c (signal_double_image_error_2):
* glyphs.c (xbm_mask_file_munging):
* glyphs.c (pixmap_to_lisp_data):
* glyphs.h:
* gui.c (gui_item_display_flush_left):
* hpplay.c (player_error_internal):
* hpplay.c (myHandler):
* intl-win32.c:
* intl-win32.c (langcode_to_lang):
* intl-win32.c (sublangcode_to_lang):
* intl-win32.c (Fmswindows_get_locale_info):
* intl-win32.c (lcid_to_locale_mule_or_no):
* intl-win32.c (mswindows_multibyte_to_unicode_print):
* intl-win32.c (complex_vars_of_intl_win32):
* keymap.c:
* keymap.c (print_keymap):
* keymap.c (ensure_meta_prefix_char_keymapp):
* keymap.c (Fkey_description):
* keymap.c (Ftext_char_description):
* lisp.h:
* lisp.h (struct):
* lisp.h (DECLARE_INLINE_HEADER):
* lread.c (Fload_internal):
* lread.c (locate_file):
* lread.c (read_escape):
* lread.c (read_raw_string):
* lread.c (read1):
* lread.c (read_list):
* lread.c (read_compiled_function):
* lread.c (init_lread):
* lrecord.h:
* marker.c (print_marker):
* marker.c (marker_equal):
* menubar-msw.c (displayable_menu_item):
* menubar-x.c (command_builder_operate_menu_accelerator):
* menubar.c (vars_of_menubar):
* minibuf.c (reinit_complex_vars_of_minibuf):
* minibuf.c (complex_vars_of_minibuf):
* mule-charset.c (Fmake_charset):
* mule-charset.c (complex_vars_of_mule_charset):
* mule-coding.c (iso2022_print):
* mule-coding.c (fixed_width_query):
* number.c (bignum_print):
* number.c (ratio_print):
* number.c (bigfloat_print):
* number.c (bigfloat_finalize):
* objects-msw.c:
* objects-msw.c (mswindows_color_to_string):
* objects-msw.c (mswindows_color_list):
* objects-tty.c:
* objects-tty.c (tty_font_list):
* objects-tty.c (tty_find_charset_font):
* objects-xlike-inc.c (xft_find_charset_font):
* objects-xlike-inc.c (endif):
* print.c:
* print.c (write_istring):
* print.c (write_ascstring):
* print.c (Fterpri):
* print.c (Fprint):
* print.c (print_error_message):
* print.c (print_vector_internal):
* print.c (print_cons):
* print.c (print_string):
* print.c (printing_unreadable_object):
* print.c (print_internal):
* print.c (print_float):
* print.c (print_symbol):
* process-nt.c (mswindows_report_winsock_error):
* process-nt.c (nt_canonicalize_host_name):
* process-unix.c (unix_canonicalize_host_name):
* process.c (print_process):
* process.c (report_process_error):
* process.c (report_network_error):
* process.c (make_process_internal):
* process.c (Fstart_process_internal):
* process.c (status_message):
* process.c (putenv_internal):
* process.c (vars_of_process):
* process.h:
* profile.c (vars_of_profile):
* rangetab.c (print_range_table):
* realpath.c (vars_of_realpath):
* redisplay.c (vars_of_redisplay):
* search.c (wordify):
* search.c (Freplace_match):
* sheap.c (sheap_adjust_h):
* sound.c (report_sound_error):
* sound.c (Fplay_sound_file):
* specifier.c (print_specifier):
* symbols.c (Fsubr_name):
* symbols.c (do_symval_forwarding):
* symbols.c (set_default_buffer_slot_variable):
* symbols.c (set_default_console_slot_variable):
* symbols.c (store_symval_forwarding):
* symbols.c (default_value):
* symbols.c (defsymbol_massage_name_1):
* symbols.c (defsymbol_massage_name_nodump):
* symbols.c (defsymbol_massage_name):
* symbols.c (defsymbol_massage_multiword_predicate_nodump):
* symbols.c (defsymbol_massage_multiword_predicate):
* symbols.c (defsymbol_nodump):
* symbols.c (defsymbol):
* symbols.c (defkeyword):
* symbols.c (defkeyword_massage_name):
* symbols.c (check_module_subr):
* symbols.c (deferror_1):
* symbols.c (deferror):
* symbols.c (deferror_massage_name):
* symbols.c (deferror_massage_name_and_message):
* symbols.c (defvar_magic):
* symeval.h:
* symeval.h (DEFVAR_SYMVAL_FWD):
* sysdep.c:
* sysdep.c (init_system_name):
* sysdll.c:
* sysdll.c (MAYBE_PREPEND_UNDERSCORE):
* sysdll.c (dll_function):
* sysdll.c (dll_variable):
* sysdll.c (dll_error):
* sysdll.c (dll_open):
* sysdll.c (dll_close):
* sysdll.c (image_for_address):
* sysdll.c (my_find_image):
* sysdll.c (search_linked_libs):
* sysdll.h:
* sysfile.h:
* sysfile.h (DEFAULT_DIRECTORY_FALLBACK):
* syswindows.h:
* tests.c (DFC_CHECK_LENGTH):
* tests.c (DFC_CHECK_CONTENT):
* tests.c (Ftest_hash_tables):
* text.c (vars_of_text):
* text.h:
* tooltalk.c (tt_opnum_string):
* tooltalk.c (tt_message_arg_ival_string):
* tooltalk.c (Ftooltalk_default_procid):
* tooltalk.c (Ftooltalk_default_session):
* tooltalk.c (init_tooltalk):
* tooltalk.c (vars_of_tooltalk):
* ui-gtk.c (Fdll_load):
* ui-gtk.c (type_to_marshaller_type):
* ui-gtk.c (Fgtk_import_function_internal):
* ui-gtk.c (emacs_gtk_object_printer):
* ui-gtk.c (emacs_gtk_boxed_printer):
* unicode.c (unicode_to_ichar):
* unicode.c (unicode_print):
* unicode.c (unicode_query):
* unicode.c (vars_of_unicode):
* unicode.c (complex_vars_of_unicode):
* win32.c:
* win32.c (mswindows_report_process_error):
* window.c (print_window):
* xemacs.def.in.in:
BASIC IDEA: Further fixing up uses of char * and CIbyte *
to reflect their actual semantics; Mule-izing some code;
redoing of the not-yet-working code to handle message translation.
Clean up code to handle message-translation (not yet working).
Create separate versions of build_msg_string() for working with
Ibyte *, CIbyte *, and Ascbyte * arguments. Assert that Ascbyte *
arguments are pure-ASCII. Make build_msg_string() be the same
as build_msg_ascstring(). Create same three versions of GETTEXT()
and DEFER_GETTEXT(). Also create build_defer_string() and
variants for the equivalent of DEFER_GETTEXT() when building a
string. Remove old CGETTEXT(). Clean up code where GETTEXT(),
DEFER_GETTEXT(), build_msg_string(), etc. was being called and
introduce some new calls to build_msg_string(), etc. Remove
GETTEXT() from calls to weird_doc() -- we assume that the
message snarfer knows about weird_doc(). Remove uses of
DEFER_GETTEXT() from error messages in sysdep.c and instead use
special comments /* @@@begin-snarf@@@ */ and /* @@@end-snarf@@@ */
that the message snarfer presumably knows about.
Create build_ascstring() and use it in many instances in place
of build_string(). The purpose of having Ascbyte * variants is
to make the code more self-documenting in terms of what sort of
semantics is expected for char * strings. In fact in the process
of looking for uses of build_string(), much improperly Mule-ized
was discovered.
Mule-ize a lot of code as described in previous paragraph,
e.g. in sysdep.c.
Make the error functions take Ascbyte * strings and fix up a
couple of places where non-pure-ASCII strings were being passed in
(file-coding.c, mule-coding.c, unicode.c). (It's debatable whether
we really need to make the error functions work this way. It
helps catch places where code is written in a way that message
translation won't work, but we may well never implement message
translation.)
Make staticpro() and friends take Ascbyte * strings instead of
raw char * strings. Create a const_Ascbyte_ptr dynarr type
to describe what's held by staticpro_names[] and friends,
create pdump descriptions for const_Ascbyte_ptr dynarrs, and
use them in place of specially-crafted staticpro descriptions.
Mule-ize certain other functions (e.g. x_event_name) by correcting
raw use of char * to Ascbyte *, Rawbyte * or another such type,
and raw use of char[] buffers to another type (usually Ascbyte[]).
Change many uses of write_c_string() to write_msg_string(),
write_ascstring(), etc.
Mule-ize emodules.c, emodules.h, sysdll.h.
Fix some un-Mule-ized code in intl-win32.c.
A comment in event-Xt.c and the limitations of the message
snarfer (make-msgfile or whatever) is presumably incorrect --
it should be smart enough to handle function calls spread over
more than one line. Clean up code in event-Xt.c that was
written awkwardly for this reason.
In config.h.in, instead of NEED_ERROR_CHECK_TYPES_INLINES,
create a more general XEMACS_DEFS_NEEDS_INLINE_DECLS to
indicate when inlined functions need to be declared in
xemacs.defs.in.in, and make use of it in xemacs.defs.in.in.
We need to do this because postgresql.c now calls qxestrdup(),
which is an inline function.
Make nconc2() and other such functions MODULE_API and put
them in xemacs.defs.in.in since postgresql.c now uses them.
Clean up indentation in lread.c and a few other places.
In text.h, document ASSERT_ASCTEXT_ASCII() and
ASSERT_ASCTEXT_ASCII_LEN(), group together the stand-in
encodings and add some more for DLL symbols, function and
variable names, etc.
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Tue, 26 Jan 2010 23:22:30 -0600 |
parents | 2161ac78b41e |
children | e6508b64ee08 061f4f90f874 |
rev | line source |
---|---|
428 | 1 /* -*-C-*- |
613 | 2 Common library code for the XEmacs server and client. |
428 | 3 |
613 | 4 This file is part of XEmacs. |
428 | 5 |
6 Copying is permitted under those conditions described by the GNU | |
7 General Public License. | |
8 | |
9 Copyright (C) 1989 Free Software Foundation, Inc. | |
10 | |
11 Author: Andy Norman (ange@hplb.hpl.hp.com), based on | |
12 'etc/server.c' and 'etc/emacsclient.c' from the 18.52 GNU | |
13 Emacs distribution. | |
14 | |
15 Please mail bugs and suggestions to the author at the above address. | |
16 */ | |
17 | |
18 /* HISTORY | |
19 * 11-Nov-1990 bristor@simba | |
20 * Added EOT stuff. | |
21 */ | |
22 | |
23 /* | |
24 * This file incorporates new features added by Bob Weiner <weiner@mot.com>, | |
25 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>. | |
26 * Please see the note at the end of the README file for details. | |
27 * | |
28 * (If gnuserv came bundled with your emacs, the README file is probably | |
29 * ../etc/gnuserv.README relative to the directory containing this file) | |
30 */ | |
31 | |
32 #if 0 | |
33 static char rcsid [] = "!Header: gnuslib.c,v 2.4 95/02/16 11:57:37 arup alpha !"; | |
34 #endif | |
35 | |
36 #include "gnuserv.h" | |
37 #include <errno.h> | |
38 | |
39 #ifdef SYSV_IPC | |
40 static int connect_to_ipc_server (void); | |
41 #endif | |
42 #ifdef UNIX_DOMAIN_SOCKETS | |
43 static int connect_to_unix_server (void); | |
44 #endif | |
45 #ifdef INTERNET_DOMAIN_SOCKETS | |
458 | 46 static int connect_to_internet_server (char *serverhost, unsigned short port); |
428 | 47 #endif |
48 | |
49 /* On some systems, e.g. DGUX, inet_addr returns a 'struct in_addr'. */ | |
50 #ifdef HAVE_BROKEN_INET_ADDR | |
51 # define IN_ADDR struct in_addr | |
52 # define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1) | |
53 #else | |
54 # if (LONGBITS > 32) | |
55 # define IN_ADDR unsigned int | |
56 # else | |
57 # define IN_ADDR unsigned long | |
58 # endif | |
59 # define NUMERIC_ADDR_ERROR (numeric_addr == (IN_ADDR) -1) | |
60 #endif | |
61 | |
62 #include <stdlib.h> | |
63 #include <stdio.h> | |
64 #include <sys/types.h> | |
65 #include <sys/stat.h> | |
66 #ifdef HAVE_UNISTD_H | |
67 #include <unistd.h> | |
68 #endif /* HAVE_UNISTD_H */ | |
69 #ifdef HAVE_STRING_H | |
70 #include <string.h> | |
71 #endif /* HAVE_STRING_H */ | |
72 | |
73 #include <arpa/inet.h> | |
74 | |
75 char *tmpdir = NULL; | |
76 | |
77 char *progname = NULL; | |
78 | |
440 | 79 int |
80 make_connection (char *hostarg, int portarg, int *s) | |
428 | 81 { |
82 #ifdef INTERNET_DOMAIN_SOCKETS | |
83 char *ptr; | |
84 if (hostarg == NULL) | |
85 hostarg = getenv("GNU_HOST"); | |
86 if (portarg == 0 && (ptr=getenv("GNU_PORT")) != NULL) | |
87 portarg = atoi(ptr); | |
88 #endif | |
89 | |
90 if (hostarg != NULL) { | |
91 /* hostname was given explicitly, via cmd line arg or GNU_HOST, | |
92 * so obey it. */ | |
93 #ifdef UNIX_DOMAIN_SOCKETS | |
94 if (!strcmp(hostarg, "unix")) { | |
95 *s = connect_to_unix_server(); | |
96 return (int) CONN_UNIX; | |
97 } | |
98 #endif /* UNIX_DOMAIN_SOCKETS */ | |
99 #ifdef INTERNET_DOMAIN_SOCKETS | |
100 *s = connect_to_internet_server(hostarg, portarg); | |
101 return (int) CONN_INTERNET; | |
102 #endif | |
103 #ifdef SYSV_IPC | |
104 return -1; /* hostarg should always be NULL for SYSV_IPC */ | |
105 #endif | |
106 } else { | |
107 /* no hostname given. Use unix-domain/sysv-ipc, or | |
108 * internet-domain connection to local host if they're not available. */ | |
109 #if defined(UNIX_DOMAIN_SOCKETS) | |
110 *s = connect_to_unix_server(); | |
111 return (int) CONN_UNIX; | |
112 #elif defined(SYSV_IPC) | |
113 *s = connect_to_ipc_server(); | |
114 return (int) CONN_IPC; | |
115 #elif defined(INTERNET_DOMAIN_SOCKETS) | |
116 { | |
117 char localhost[HOSTNAMSZ]; | |
118 gethostname(localhost,HOSTNAMSZ); /* use this host by default */ | |
119 *s = connect_to_internet_server(localhost, portarg); | |
120 return (int) CONN_INTERNET; | |
121 } | |
122 #endif /* IPC type */ | |
123 } | |
124 } | |
125 | |
126 #ifdef SYSV_IPC | |
127 /* | |
128 connect_to_ipc_server -- establish connection with server process via SYSV IPC | |
129 Returns msqid for server if successful. | |
130 */ | |
440 | 131 static int |
132 connect_to_ipc_server (void) | |
428 | 133 { |
134 int s; /* connected msqid */ | |
135 key_t key; /* message key */ | |
136 char buf[GSERV_BUFSZ+1]; /* buffer for filename */ | |
137 | |
138 sprintf(buf,"%s/gsrv%d",tmpdir,(int)geteuid()); | |
139 creat(buf,0600); | |
140 if ((key = ftok(buf,1)) == -1) { | |
141 perror(progname); | |
142 fprintf(stderr, "%s: unable to get ipc key from %s\n", | |
143 progname, buf); | |
144 exit(1); | |
145 } | |
146 | |
147 if ((s = msgget(key,0600)) == -1) { | |
148 perror(progname); | |
149 fprintf(stderr,"%s: unable to access msg queue\n",progname); | |
150 exit(1); | |
151 }; /* if */ | |
152 | |
153 return(s); | |
154 | |
155 } /* connect_to_ipc_server */ | |
156 | |
157 | |
158 /* | |
159 disconnect_from_ipc_server -- inform the server that sending has finished, | |
160 and wait for its reply. | |
161 */ | |
440 | 162 void |
163 disconnect_from_ipc_server (int s, struct msgbuf *msgp, int echo) | |
428 | 164 { |
165 int len; /* length of received message */ | |
166 | |
167 send_string(s,EOT_STR); /* EOT terminates this message */ | |
168 msgp->mtype = 1; | |
169 | |
170 if(msgsnd(s,msgp,strlen(msgp->mtext)+1,0) < 0) { | |
171 perror(progname); | |
172 fprintf(stderr,"%s: unable to send message to server\n",progname); | |
173 exit(1); | |
174 }; /* if */ | |
175 | |
176 if((len = msgrcv(s,msgp,GSERV_BUFSZ,getpid(),0)) < 0) { | |
177 perror(progname); | |
178 fprintf(stderr,"%s: unable to receive message from server\n",progname); | |
179 exit(1); | |
180 }; /* if */ | |
181 | |
182 if (echo) { | |
183 msgp->mtext[len] = '\0'; /* string terminate message */ | |
184 fputs(msgp->mtext, stdout); | |
185 if (msgp->mtext[len-1] != '\n') putchar ('\n'); | |
186 }; /* if */ | |
187 | |
188 } /* disconnect_from_ipc_server */ | |
189 #endif /* SYSV_IPC */ | |
190 | |
191 | |
192 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS) | |
193 /* | |
194 send_string -- send string to socket. | |
195 */ | |
440 | 196 void |
442 | 197 send_string (int s, const char *msg) |
428 | 198 { |
199 #if 0 | |
200 if (send(s,msg,strlen(msg),0) < 0) { | |
201 perror(progname); | |
202 fprintf(stderr,"%s: unable to send\n",progname); | |
203 exit(1); | |
204 }; /* if */ | |
205 #else | |
206 int len, left=strlen(msg); | |
207 while (left > 0) { | |
208 if ((len=write(s,msg,min2(left,GSERV_BUFSZ))) < 0) { | |
209 /* XEmacs addition: robertl@arnet.com */ | |
210 if (errno == EPIPE) { | |
211 return ; | |
212 } | |
213 perror(progname); | |
214 fprintf(stderr,"%s: unable to send\n",progname); | |
215 exit(1); | |
216 }; /* if */ | |
217 left -= len; | |
218 msg += len; | |
219 }; /* while */ | |
220 #endif | |
221 } /* send_string */ | |
222 | |
223 /* | |
224 read_line -- read a \n terminated line from a socket | |
225 */ | |
440 | 226 int |
227 read_line (int s, char *dest) | |
428 | 228 { |
229 int length; | |
230 int offset=0; | |
231 char buffer[GSERV_BUFSZ+1]; | |
232 | |
233 while ((length=read(s,buffer+offset,1)>0) && buffer[offset]!='\n' | |
234 && buffer[offset] != EOT_CHR) { | |
235 offset += length; | |
236 if (offset >= GSERV_BUFSZ) | |
237 break; | |
238 } | |
239 buffer[offset] = '\0'; | |
240 strcpy(dest,buffer); | |
241 return 1; | |
242 } /* read_line */ | |
243 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */ | |
244 | |
245 | |
246 #ifdef UNIX_DOMAIN_SOCKETS | |
247 /* | |
248 connect_to_unix_server -- establish connection with server process via a unix- | |
249 domain socket. Returns socket descriptor for server | |
250 if successful. | |
251 */ | |
440 | 252 static int |
253 connect_to_unix_server (void) | |
428 | 254 { |
255 int s; /* connected socket descriptor */ | |
256 struct sockaddr_un server; /* for unix connections */ | |
257 | |
258 if ((s = socket(AF_UNIX,SOCK_STREAM,0)) < 0) { | |
259 perror(progname); | |
260 fprintf(stderr,"%s: unable to create socket\n",progname); | |
261 exit(1); | |
262 }; /* if */ | |
263 | |
264 server.sun_family = AF_UNIX; | |
265 #ifdef HIDE_UNIX_SOCKET | |
266 sprintf(server.sun_path,"%s/gsrvdir%d/gsrv",tmpdir,(int)geteuid()); | |
267 #else /* HIDE_UNIX_SOCKET */ | |
268 sprintf(server.sun_path,"%s/gsrv%d",tmpdir,(int)geteuid()); | |
269 #endif /* HIDE_UNIX_SOCKET */ | |
270 if (connect(s,(struct sockaddr *)&server,strlen(server.sun_path)+2) < 0) { | |
271 perror(progname); | |
272 fprintf(stderr,"%s: unable to connect to local\n",progname); | |
273 exit(1); | |
274 }; /* if */ | |
275 | |
276 return(s); | |
277 | |
278 } /* connect_to_unix_server */ | |
279 #endif /* UNIX_DOMAIN_SOCKETS */ | |
280 | |
281 | |
282 #ifdef INTERNET_DOMAIN_SOCKETS | |
283 /* | |
284 internet_addr -- return the internet addr of the hostname or | |
285 internet address passed. Return -1 on error. | |
286 */ | |
440 | 287 int |
288 internet_addr (char *host) | |
428 | 289 { |
290 struct hostent *hp; /* pointer to host info for remote host */ | |
291 IN_ADDR numeric_addr; /* host address */ | |
292 | |
293 numeric_addr = inet_addr(host); | |
294 if (!NUMERIC_ADDR_ERROR) | |
295 return numeric_addr; | |
296 else if ((hp = gethostbyname(host)) != NULL) | |
297 return ((struct in_addr *)(hp->h_addr))->s_addr; | |
298 else | |
299 return -1; | |
300 | |
301 } /* internet_addr */ | |
302 | |
303 #ifdef AUTH_MAGIC_COOKIE | |
304 # include <X11/X.h> | |
305 # include <X11/Xauth.h> | |
306 | |
307 static Xauth *server_xauth = NULL; | |
308 #endif | |
309 | |
310 /* | |
311 connect_to_internet_server -- establish connection with server process via | |
312 an internet domain socket. Returns socket | |
313 descriptor for server if successful. | |
314 */ | |
440 | 315 static int |
458 | 316 connect_to_internet_server (char *serverhost, unsigned short port) |
428 | 317 { |
318 int s; /* connected socket descriptor */ | |
319 struct servent *sp; /* pointer to service information */ | |
320 struct sockaddr_in peeraddr_in; /* for peer socket address */ | |
321 char buf[512]; /* temporary buffer */ | |
322 | |
323 /* clear out address structures */ | |
324 memset((char *)&peeraddr_in,0,sizeof(struct sockaddr_in)); | |
325 | |
326 /* Set up the peer address to which we will connect. */ | |
327 peeraddr_in.sin_family = AF_INET; | |
328 | |
329 /* look up the server host's internet address */ | |
647 | 330 if ((peeraddr_in.sin_addr.s_addr = internet_addr (serverhost)) == |
331 (unsigned int) -1) | |
332 { | |
333 fprintf (stderr, "%s: unable to find %s in /etc/hosts or from YP\n", | |
334 progname, serverhost); | |
335 exit(1); | |
336 } | |
428 | 337 |
338 if (port == 0) { | |
339 if ((sp = getservbyname ("gnuserv","tcp")) == NULL) | |
340 peeraddr_in.sin_port = htons(DEFAULT_PORT+getuid()); | |
341 else | |
342 peeraddr_in.sin_port = sp->s_port; | |
343 } /* if */ | |
344 else | |
345 peeraddr_in.sin_port = htons(port); | |
346 | |
347 /* Create the socket. */ | |
348 if ((s = socket (AF_INET,SOCK_STREAM, 0))== -1) { | |
349 perror(progname); | |
350 fprintf(stderr,"%s: unable to create socket\n",progname); | |
351 exit(1); | |
352 }; /* if */ | |
353 | |
354 /* Try to connect to the remote server at the address | |
355 * which was just built into peeraddr. | |
356 */ | |
357 if (connect(s, (struct sockaddr *)&peeraddr_in, | |
358 sizeof(struct sockaddr_in)) == -1) { | |
359 perror(progname); | |
360 fprintf(stderr, "%s: unable to connect to remote\n",progname); | |
361 exit(1); | |
362 }; /* if */ | |
363 | |
364 #ifdef AUTH_MAGIC_COOKIE | |
365 | |
366 /* send credentials using MIT-MAGIC-COOKIE-1 protocol */ | |
367 | |
368 server_xauth = | |
369 XauGetAuthByAddr(FamilyInternet, | |
370 sizeof(peeraddr_in.sin_addr.s_addr), | |
371 (char *) &peeraddr_in.sin_addr.s_addr, | |
372 strlen(MCOOKIE_SCREEN), MCOOKIE_SCREEN, | |
373 strlen(MCOOKIE_X_NAME), MCOOKIE_X_NAME); | |
374 | |
375 if (server_xauth && server_xauth->data) { | |
376 sprintf(buf, "%s\n%d\n", MCOOKIE_NAME, server_xauth->data_length); | |
377 write (s, buf, strlen(buf)); | |
378 write (s, server_xauth->data, server_xauth->data_length); | |
379 | |
380 return (s); | |
381 } | |
382 | |
383 #endif /* AUTH_MAGIC_COOKIE */ | |
384 | |
385 sprintf (buf, "%s\n", DEFAUTH_NAME); | |
386 write (s, buf, strlen(buf)); | |
387 | |
388 return(s); | |
389 | |
390 } /* connect_to_internet_server */ | |
391 #endif /* INTERNET_DOMAIN_SOCKETS */ | |
392 | |
393 | |
394 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS) | |
395 /* | |
396 disconnect_from_server -- inform the server that sending has finished, and wait for | |
397 its reply. | |
398 */ | |
440 | 399 void |
400 disconnect_from_server (int s, int echo) | |
428 | 401 { |
402 #if 0 | |
403 char buffer[REPLYSIZ+1]; | |
404 #else | |
405 char buffer[GSERV_BUFSZ+1]; | |
406 #endif | |
407 int add_newline = 1; | |
408 int length; | |
409 | |
410 send_string(s,EOT_STR); /* make sure server gets string */ | |
411 | |
3556 | 412 #ifndef _SCO_DS |
428 | 413 /* |
3556 | 414 * There used to be a comment here complaining about ancient Linux |
415 * versions. It is no longer relevant. I don't know why _SCO_DS is | |
416 * verboten here, as the original comment did not say. | |
428 | 417 */ |
418 | |
419 if (shutdown(s,1) == -1) { | |
420 perror(progname); | |
421 fprintf(stderr, "%s: unable to shutdown socket\n",progname); | |
422 exit(1); | |
423 }; /* if */ | |
424 #endif | |
425 | |
426 #if 0 | |
427 while((length = recv(s,buffer,REPLYSIZ,0)) > 0) { | |
428 buffer[length] = '\0'; | |
429 if (echo) fputs(buffer,stdout); | |
430 add_newline = (buffer[length-1] != '\n'); | |
431 }; /* while */ | |
432 #else | |
433 while ((length = read(s,buffer,GSERV_BUFSZ)) > 0 || | |
434 (length == -1 && errno == EINTR)) { | |
3556 | 435 if (length > 0) { |
428 | 436 buffer[length] = '\0'; |
437 if (echo) { | |
438 fputs(buffer,stdout); | |
439 add_newline = (buffer[length-1] != '\n'); | |
440 }; /* if */ | |
441 }; /* if */ | |
442 }; /* while */ | |
443 #endif | |
444 | |
445 if (echo && add_newline) putchar('\n'); | |
446 | |
447 if(length < 0) { | |
448 perror(progname); | |
449 fprintf(stderr,"%s: unable to read the reply from the server\n",progname); | |
450 exit(1); | |
451 }; /* if */ | |
452 | |
453 } /* disconnect_from_server */ | |
454 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */ |