From 92a4d9bee04476d1f28f76c130fb0faf2eda4829 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Sun, 2 Jul 2006 07:08:28 +0000 Subject: [PATCH] Removed pointless memcpy() calls; eliminated some redundant code. --- x11/pdcx11.c | 13 ++--- x11/process/x11.c | 108 ++++++++++++---------------------------- x11/process/x11.h | 3 +- x11/process/x11common.c | 7 ++- x11/process/x11curses.c | 69 +++++++------------------ 5 files changed, 64 insertions(+), 136 deletions(-) diff --git a/x11/pdcx11.c b/x11/pdcx11.c index 905c48c0..3250633b 100644 --- a/x11/pdcx11.c +++ b/x11/pdcx11.c @@ -20,7 +20,7 @@ #include #include -RCSID("$Id: pdcx11.c,v 1.66 2006/07/01 18:06:43 wmcbrine Exp $"); +RCSID("$Id: pdcx11.c,v 1.67 2006/07/02 07:08:27 wmcbrine Exp $"); AppData app_data; @@ -2362,24 +2362,19 @@ unsigned long XCurses_get_key_modifiers(void) int XCursesSendKeyToCurses(unsigned long key, MOUSE_STATUS *ms) { - char buf[100]; /* enough for MOUSE_STATUS */ - PDC_LOG(("%s:XCursesSendKeyToCurses() - called: sending %d\n", XCLOGMSG, key)); - memcpy(buf, (char *)&key, sizeof(unsigned long)); - - if (write_socket(key_sock, buf, sizeof(unsigned long)) < 0) + if (write_socket(key_sock, (char *)&key, sizeof(unsigned long)) < 0) XCursesExitXCursesProcess(1, SIGKILL, "exiting from XCursesSendKeyToCurses"); if (ms != NULL) { - memcpy(buf, (char *)&Mouse_status, sizeof(MOUSE_STATUS)); - MOUSE_LOG(("%s:writing mouse stuff\n", XCLOGMSG)); - if (write_socket(key_sock, buf, sizeof(MOUSE_STATUS)) < 0) + if (write_socket(key_sock, (char *)&Mouse_status, + sizeof(MOUSE_STATUS)) < 0) XCursesExitXCursesProcess(1, SIGKILL, "exiting from XCursesSendKeyToCurses"); } diff --git a/x11/process/x11.c b/x11/process/x11.c index b8de3844..eb80b4cc 100644 --- a/x11/process/x11.c +++ b/x11/process/x11.c @@ -19,7 +19,7 @@ #include -RCSID("$Id: x11.c,v 1.45 2006/07/01 18:06:43 wmcbrine Exp $"); +RCSID("$Id: x11.c,v 1.46 2006/07/02 07:08:27 wmcbrine Exp $"); extern AppData app_data; @@ -132,6 +132,13 @@ int XCursesRefreshScreen(void) return 0; } +static void XCursesContinue(void) +{ + if (write_display_socket_int(CURSES_CONTINUE) < 0) + XCursesExitXCursesProcess(4, SIGKILL, + "exiting from XCursesProcessRequestsFromCurses"); +} + void XCursesProcessRequestsFromCurses(XtPointer client_data, int *fid, XtInputId *id) { @@ -157,8 +164,8 @@ void XCursesProcessRequestsFromCurses(XtPointer client_data, int *fid, if ((s = select(FD_SETSIZE, (FD_SET_CAST)&readfds, NULL, NULL, &socket_timeout)) < 0) - XCursesExitXCursesProcess(2, SIGKILL, - "exiting from XCursesProcessRequestsFromCurses - select failed"); + XCursesExitXCursesProcess(2, SIGKILL, "exiting from " + "XCursesProcessRequestsFromCurses - select failed"); if (s == 0) /* no requests pending - should never happen!*/ return; @@ -171,14 +178,13 @@ void XCursesProcessRequestsFromCurses(XtPointer client_data, int *fid, PDC_LOG(("%s:XCursesProcessRequestsFromCurses() - " "before read_socket()\n", XCLOGMSG)); - if (read_socket(display_sock, buf, sizeof(int)) < 0) - XCursesExitXCursesProcess(3, SIGKILL, - "exiting from XCursesProcessRequestsFromCurses - first read"); + if (read_socket(display_sock, (char *)&num_cols, sizeof(int)) < 0) + XCursesExitXCursesProcess(3, SIGKILL, "exiting from " + "XCursesProcessRequestsFromCurses - first read"); PDC_LOG(("%s:XCursesProcessRequestsFromCurses() - " "after read_socket()\n", XCLOGMSG)); - memcpy((char *)&num_cols, buf, sizeof(int)); after_first_curses_request = True; switch(num_cols) @@ -218,12 +224,7 @@ void XCursesProcessRequestsFromCurses(XtPointer client_data, int *fid, XCursesDisplayCursor(SP->cursrow, SP->curscol, SP->cursrow, SP->curscol); #endif - old_x = CURSES_CONTINUE; - memcpy(buf, (char *)&old_x, sizeof(int)); - - if (write_socket(display_sock, buf, sizeof(int)) < 0) - XCursesExitXCursesProcess(4, SIGKILL, - "exiting from XCursesProcessRequestsFromCurses"); + XCursesContinue(); break; /* request from curses to confirm completion of display */ @@ -235,12 +236,7 @@ void XCursesProcessRequestsFromCurses(XtPointer client_data, int *fid, XCursesDisplayCursor(SP->cursrow, SP->curscol, SP->cursrow, SP->curscol); - old_x = CURSES_CONTINUE; - memcpy(buf, (char *)&old_x, sizeof(int)); - - if (write_socket(display_sock, buf, sizeof(int)) < 0) - XCursesExitXCursesProcess(4, SIGKILL, - "exiting from XCursesProcessRequestsFromCurses"); + XCursesContinue(); break; case CURSES_REFRESH_SCROLLBAR: @@ -306,13 +302,11 @@ void XCursesProcessRequestsFromCurses(XtPointer client_data, int *fid, case CURSES_TITLE: XC_LOG(("CURSES_TITLE received from child\n")); - if (read_socket(display_sock, buf, sizeof(int)) < 0) + if (read_socket(display_sock, (char *)&pos, sizeof(int)) < 0) XCursesExitXCursesProcess(5, SIGKILL, "exiting from CURSES_TITLE " "XCursesProcessRequestsFromCurses"); - memcpy((char *)&pos, buf, sizeof(int)); - if (read_socket(display_sock, title, pos) < 0) XCursesExitXCursesProcess(5, SIGKILL, "exiting from CURSES_TITLE " @@ -366,23 +360,14 @@ void XCursesProcessRequestsFromCurses(XtPointer client_data, int *fid, memset(Xcurscr, 0, SP->XcurscrSize); atrtab = (unsigned char *)(Xcurscr + XCURSCR_ATRTAB_OFF); memcpy(atrtab, save_atrtab, sizeof(save_atrtab)); - - old_x = CURSES_CONTINUE; - memcpy(buf, (char *)&old_x, sizeof(int)); - if (write_socket(display_sock, buf, sizeof(int)) < 0) - XCursesExitXCursesProcess(4, SIGKILL, - "exiting from XCursesProcessRequestsFromCurses"); + XCursesContinue(); break; case CURSES_GET_SELECTION: XC_LOG(("CURSES_GET_SELECTION received from child\n")); - old_x = CURSES_CONTINUE; - memcpy(buf, (char *)&old_x, sizeof(int)); - if (write_socket(display_sock, buf, sizeof(int)) < 0) - XCursesExitXCursesProcess(4, SIGKILL, - "exiting from XCursesProcessRequestsFromCurses"); + XCursesContinue(); XtGetSelectionValue(topLevel, XA_PRIMARY, XA_STRING, XCursesRequestorCallbackForGetSelection, @@ -393,12 +378,11 @@ void XCursesProcessRequestsFromCurses(XtPointer client_data, int *fid, case CURSES_SET_SELECTION: XC_LOG(("CURSES_SET_SELECTION received from child\n")); - if (read_socket(display_sock, buf, sizeof(long)) < 0) - XCursesExitXCursesProcess(5, SIGKILL, - "exiting from CURSES_TITLE " - "XCursesProcessRequestsFromCurses"); - - memcpy((char *)&length, buf, sizeof(long)); + if (read_socket(display_sock, (char *)&length, + sizeof(long)) < 0) + XCursesExitXCursesProcess(5, SIGKILL, + "exiting from CURSES_TITLE " + "XCursesProcessRequestsFromCurses"); if (length > (long)tmpsel_length) { @@ -412,11 +396,7 @@ void XCursesProcessRequestsFromCurses(XtPointer client_data, int *fid, if (!tmpsel) { - old_x = PDC_CLIP_MEMORY_ERROR; - - memcpy(buf, (char *)&old_x, sizeof(int)); - - if (write_socket(display_sock, buf, sizeof(int)) < 0) + if (write_display_socket_int(PDC_CLIP_MEMORY_ERROR) < 0) XCursesExitXCursesProcess(4, SIGKILL, "exiting from XCursesProcessRequestsFromCurses"); break; @@ -443,21 +423,16 @@ void XCursesProcessRequestsFromCurses(XtPointer client_data, int *fid, old_x = PDC_CLIP_SUCCESS; SelectionOff(); - memcpy(buf, (char *)&old_x, sizeof(int)); - if (write_socket(display_sock, buf, sizeof(int)) < 0) + if (write_display_socket_int(old_x) < 0) XCursesExitXCursesProcess(4, SIGKILL, "exiting from XCursesProcessRequestsFromCurses"); break; case CURSES_CLEAR_SELECTION: XC_LOG(("CURSES_CLEAR_SELECTION received from child\n")); - old_x = CURSES_CONTINUE; - memcpy(buf, (char *)&old_x, sizeof(int)); - if (write_socket(display_sock, buf, sizeof(int)) < 0) - XCursesExitXCursesProcess(4, SIGKILL, - "exiting from XCursesProcessRequestsFromCurses"); + XCursesContinue(); SelectionOff(); break; @@ -476,8 +451,7 @@ int XCursesSetupX(const char *display_name, int argc, char *argv[]) int italic_font_valid; XColor pointerforecolor, pointerbackcolor; XrmValue rmfrom, rmto; - char wait_buf[5]; - int wait_value = 0, i = 0; + int i = 0; int minwidth, minheight; PDC_LOG(("%s:XCursesSetupX called\n", XCLOGMSG)); @@ -741,9 +715,7 @@ int XCursesSetupX(const char *display_name, int argc, char *argv[]) that when the curses process makes a request, the Xcurses process can service the request.*/ - wait_value = CURSES_CHILD; - memcpy(wait_buf, (char *)&wait_value, sizeof(int)); - write_socket(display_sock, wait_buf, sizeof(int)); + write_display_socket_int(CURSES_CHILD); XtRealizeWidget(topLevel); @@ -897,7 +869,6 @@ int XCursesSetupX(const char *display_name, int argc, char *argv[]) RETSIGTYPE XCursesSignalHandler(int signo) { - char buf[10]; int flag = CURSES_EXIT; PDC_LOG(("%s:XCursesSignalHandler() - called: %d\n", XCLOGMSG, signo)); @@ -936,9 +907,7 @@ RETSIGTYPE XCursesSignalHandler(int signo) /* Send a CURSES_EXIT to myself */ - memcpy(buf, (char *)&flag, sizeof(int)); - - if (write_socket(exit_sock, buf, sizeof(int)) < 0) + if (write_socket(exit_sock, (char *)&flag, sizeof(int)) < 0) XCursesExitXCursesProcess(7, signo, "exiting from XCursesSignalHandler"); } @@ -948,19 +917,12 @@ void XCursesRequestorCallbackForGetSelection(Widget w, XtPointer data, XtPointer value, unsigned long *length, int *format) { - int rc; - char buf[12]; /* big enough for 2 integers */ - char *string = (char *)value; - PDC_LOG(("%s:XCursesRequestorCallbackForSelection() - called\n", XCLOGMSG)); if ((value == NULL) && (*length == 0)) { - rc = PDC_CLIP_EMPTY; - memcpy(buf, (char *)&rc, sizeof(int)); - - if (write_socket(display_sock, buf, sizeof(int)) < 0) + if (write_display_socket_int(PDC_CLIP_EMPTY) < 0) XCursesExitXCursesProcess(4, SIGKILL, "exiting from XCursesRequestorCallbackForGetSelection"); return; @@ -969,19 +931,15 @@ void XCursesRequestorCallbackForGetSelection(Widget w, XtPointer data, /* Here all is OK, send PDC_CLIP_SUCCESS, then length, then contents */ - rc = PDC_CLIP_SUCCESS; - memcpy(buf, (char *)&rc, sizeof(int)); - if (write_socket(display_sock, buf, sizeof(int)) < 0) + if (write_display_socket_int(PDC_CLIP_SUCCESS) < 0) XCursesExitXCursesProcess(4, SIGKILL, "exiting from XCursesRequestorCallbackForGetSelection"); - rc = *(length); - memcpy(buf, (char *)&rc, sizeof(int)); - if (write_socket(display_sock, buf, sizeof(int)) < 0) + if (write_display_socket_int((int)(*length)) < 0) XCursesExitXCursesProcess(4, SIGKILL, "exiting from XCursesRequestorCallbackForGetSelection"); - if (write_socket(display_sock, string, *length) < 0) + if (write_socket(display_sock, (char *)value, *length) < 0) XCursesExitXCursesProcess(4, SIGKILL, "exiting from XCursesRequestorCallbackForGetSelection"); } diff --git a/x11/process/x11.h b/x11/process/x11.h index 026fa20f..52e4aaef 100644 --- a/x11/process/x11.h +++ b/x11/process/x11.h @@ -1,4 +1,4 @@ -/* $Id: x11.h,v 1.8 2006/03/29 20:06:41 wmcbrine Exp $ */ +/* $Id: x11.h,v 1.9 2006/07/02 07:08:28 wmcbrine Exp $ */ void dummy_function(void); void get_GC(Display *, Window, GC *, XFontStruct *, int, int, bool); @@ -34,6 +34,7 @@ void SelectionSet(void); int write_socket(int, const char *, int); int read_socket(int, char *, int); +int write_display_socket_int(int); int XCursesSetupX(const char *display_name, int argc, char *argv[]); RETSIGTYPE XCursesSigwinchHandler(int signo); diff --git a/x11/process/x11common.c b/x11/process/x11common.c index c52cd947..06d700bd 100644 --- a/x11/process/x11common.c +++ b/x11/process/x11common.c @@ -17,7 +17,7 @@ #include "pdcx11.h" -RCSID("$Id: x11common.c,v 1.15 2006/04/13 17:19:44 wmcbrine Exp $"); +RCSID("$Id: x11common.c,v 1.16 2006/07/02 07:08:28 wmcbrine Exp $"); /* Variables specific to process port */ @@ -105,3 +105,8 @@ int read_socket(int sock_num, char *buf, int len) start = rc; } } + +int write_display_socket_int(int x) +{ + return write_socket(display_sock, (char *)&x, sizeof(int)); +} diff --git a/x11/process/x11curses.c b/x11/process/x11curses.c index 07539d8d..e7f957eb 100644 --- a/x11/process/x11curses.c +++ b/x11/process/x11curses.c @@ -22,7 +22,7 @@ #include -RCSID("$Id: x11curses.c,v 1.31 2006/04/15 16:55:28 wmcbrine Exp $"); +RCSID("$Id: x11curses.c,v 1.32 2006/07/02 07:08:28 wmcbrine Exp $"); extern AppData app_data; @@ -126,16 +126,9 @@ void XCurses_set_title(const char *title) int XCurses_refresh_scrollbar(void) { - char buf[30]; - int idx; - PDC_LOG(("%s:XCurses_refresh_scrollbar() - called\n", XCLOGMSG)); - idx = CURSES_REFRESH_SCROLLBAR; - memcpy(buf, (char *)&idx, sizeof(int)); - idx = sizeof(int); - - if (write_socket(display_sock, buf, idx) < 0) + if (write_display_socket_int(CURSES_REFRESH_SCROLLBAR) < 0) XCursesExitCursesProcess(1, "exiting from XCurses_refresh_scrollbar"); @@ -146,28 +139,25 @@ int XCurses_rawgetch(void) { unsigned long newkey = 0; int key = 0; - char buf[100]; /* big enough for MOUSE_STATUS struct */ PDC_LOG(("%s:XCurses_rawgetch() - called\n", XCLOGMSG)); while (1) { - if (read_socket(key_sock, buf, sizeof(unsigned long)) < 0) - XCursesExitCursesProcess(2, - "exiting from XCurses_rawchar"); + if (read_socket(key_sock, (char *)&newkey, + sizeof(unsigned long)) < 0) + XCursesExitCursesProcess(2, + "exiting from XCurses_rawchar"); - memcpy((char *)&newkey, buf, sizeof(unsigned long)); pdc_key_modifier = (newkey >> 24) & 0xFF; key = (int)(newkey & 0x00FFFFFF); if (key == KEY_MOUSE) { - if (read_socket(key_sock, buf, sizeof(MOUSE_STATUS)) < 0) - XCursesExitCursesProcess(2, - "exiting from XCurses_rawchar"); - - memcpy((char *)&Trapped_Mouse_status, buf, - sizeof(MOUSE_STATUS)); + if (read_socket(key_sock, (char *)&Trapped_Mouse_status, + sizeof(MOUSE_STATUS)) < 0) + XCursesExitCursesProcess(2, + "exiting from XCurses_rawchar"); /* Check if the mouse has been clicked on a slk area. If the return value is > 0 (indicating the label number), @@ -235,15 +225,11 @@ bool XCurses_kbhit(void) int XCursesInstruct(int flag) { - char buf[10]; - PDC_LOG(("%s:XCursesInstruct() - called flag %d\n", XCLOGMSG, flag)); /* Send a request to X */ - memcpy(buf, (char *)&flag, sizeof(int)); - - if (write_socket(display_sock, buf, sizeof(int)) < 0) + if (write_display_socket_int(flag) < 0) XCursesExitCursesProcess(4, "exiting from XCursesInstruct"); return OK; @@ -252,7 +238,6 @@ int XCursesInstruct(int flag) int XCursesInstructAndWait(int flag) { int result; - char buf[10]; PDC_LOG(("%s:XCursesInstructAndWait() - called\n", XCLOGMSG)); @@ -262,12 +247,10 @@ int XCursesInstructAndWait(int flag) /* wait for X to say the refresh has occurred*/ - if (read_socket(display_sock, buf, sizeof(int)) < 0) + if (read_socket(display_sock, (char *)&result, sizeof(int)) < 0) XCursesExitCursesProcess(5, "exiting from XCursesInstructAndWait"); - memcpy((char *)&result, buf, sizeof(int)); - if (result != CURSES_CONTINUE) XCursesExitCursesProcess(6, "exiting from " "XCursesInstructAndWait - synchronization error"); @@ -302,7 +285,6 @@ int XCurses_transform_line(const chtype *ch, int row, static int XCursesSetupCurses(void) { - char wait_buf[5]; int wait_value; PDC_LOG(("%s:XCursesSetupCurses called\n", XCLOGMSG)); @@ -316,8 +298,7 @@ static int XCursesSetupCurses(void) FD_ZERO(&readfds); FD_ZERO(&writefds); - read_socket(display_sock, wait_buf, sizeof(int)); - memcpy((char *)&wait_value, wait_buf, sizeof(int)); + read_socket(display_sock, (char *)&wait_value, sizeof(int)); if (wait_value != CURSES_CHILD) return ERR; @@ -441,26 +422,21 @@ int XCurses_getclipboard(char **contents, long *length) { int result = 0; int len; - char buf[12]; PDC_LOG(("%s:XCurses_getclipboard() - called\n", XCLOGMSG)); XCursesInstructAndWait(CURSES_GET_SELECTION); - if (read_socket(display_sock, buf, sizeof(int)) < 0) + if (read_socket(display_sock, (char *)&result, sizeof(int)) < 0) XCursesExitCursesProcess(5, "exiting from XCurses_getclipboard"); - memcpy((char *)&result, buf, sizeof(int)); - if (result == PDC_CLIP_SUCCESS) { - if (read_socket(display_sock, buf, sizeof(int)) < 0) + if (read_socket(display_sock, (char *)&len, sizeof(int)) < 0) XCursesExitCursesProcess(5, "exiting from XCurses_getclipboard"); - memcpy((char *)&len, buf, sizeof(int)); - if (len != 0) { *contents = (char *)malloc(len + 1); @@ -483,15 +459,12 @@ int XCurses_getclipboard(char **contents, long *length) int XCurses_setclipboard(const char *contents, long length) { int rc; - char buf[12]; /* big enough for 2 integers */ - long len = length; PDC_LOG(("%s:XCurses_setclipboard() - called\n", XCLOGMSG)); XCursesInstruct(CURSES_SET_SELECTION); - memcpy(buf, (char *)&len, sizeof(long)); - if (write_socket(display_sock, buf, sizeof(long)) < 0) + if (write_socket(display_sock, (char *)&length, sizeof(long)) < 0) XCursesExitCursesProcess(5, "exiting from XCurses_setclipboard"); @@ -501,36 +474,32 @@ int XCurses_setclipboard(const char *contents, long length) /* Wait for X to do its stuff. Now expect return code. */ - if (read_socket(display_sock, buf, sizeof(int)) < 0) + if (read_socket(display_sock, (char *)&rc, sizeof(int)) < 0) XCursesExitCursesProcess(5, "exiting from XCurses_setclipboard"); - memcpy((char *)&rc, buf, sizeof(int)); return rc; } int XCurses_clearclipboard(void) { int rc; - char buf[12]; /* big enough for 2 integers */ long len = 0; PDC_LOG(("%s:XCurses_clearclipboard() - called\n", XCLOGMSG)); XCursesInstruct(CURSES_CLEAR_SELECTION); - memcpy(buf, (char *)&len, sizeof(long)); - if (write_socket(display_sock, buf, sizeof(long)) < 0) + if (write_socket(display_sock, (char *)&len, sizeof(long)) < 0) XCursesExitCursesProcess(5, "exiting from XCurses_setclipboard"); /* Wait for X to do its stuff. Now expect return code. */ - if (read_socket(display_sock, buf, sizeof(int)) < 0) + if (read_socket(display_sock, (char *)&rc, sizeof(int)) < 0) XCursesExitCursesProcess(5, "exiting from XCurses_clearclipboard"); - memcpy((char *)&rc, buf, sizeof(int)); return rc; }