From 4ae82c64d8f219666c2f8315a8a5ab9e30a1c1af Mon Sep 17 00:00:00 2001 From: Igor Pashev <pashev.igor@gmail.com> Date: Sat, 29 Oct 2022 15:37:18 +0200 Subject: WIP --- src/lib/XDither.c | 1 - src/lib/XShade.c | 1 - src/lib/XSpadFill.c | 1 - src/lib/bsdsignal.cxx | 1 - src/lib/cfuns-c.cxx | 8 +- src/lib/cursor.c | 144 -------- src/lib/cursor.cc | 143 ++++++++ src/lib/edin.c | 951 -------------------------------------------------- src/lib/edin.cc | 950 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib/emupty.c | 1 - src/lib/fnct_key.c | 345 ------------------ src/lib/fnct_key.cc | 344 ++++++++++++++++++ src/lib/halloc.c | 1 - src/lib/hash.c | 1 - src/lib/openpty.c | 168 --------- src/lib/openpty.cc | 171 +++++++++ src/lib/pixmap.c | 1 - src/lib/prt.c | 407 --------------------- src/lib/prt.cc | 406 +++++++++++++++++++++ src/lib/sockio-c.cxx | 4 +- src/lib/spadcolors.c | 1 - src/lib/util.c | 1 - src/lib/wct.c | 732 -------------------------------------- src/lib/wct.cc | 731 ++++++++++++++++++++++++++++++++++++++ 24 files changed, 2752 insertions(+), 2762 deletions(-) delete mode 100644 src/lib/cursor.c create mode 100644 src/lib/cursor.cc delete mode 100644 src/lib/edin.c create mode 100644 src/lib/edin.cc delete mode 100644 src/lib/fnct_key.c create mode 100644 src/lib/fnct_key.cc delete mode 100644 src/lib/openpty.c create mode 100644 src/lib/openpty.cc delete mode 100644 src/lib/prt.c create mode 100644 src/lib/prt.cc delete mode 100644 src/lib/wct.c create mode 100644 src/lib/wct.cc (limited to 'src/lib') diff --git a/src/lib/XDither.c b/src/lib/XDither.c index d766633d..aac711f8 100644 --- a/src/lib/XDither.c +++ b/src/lib/XDither.c @@ -33,7 +33,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "openaxiom-c-macros.h" #include <stdio.h> #include <stdlib.h> diff --git a/src/lib/XShade.c b/src/lib/XShade.c index 590f2d2f..a5851f69 100644 --- a/src/lib/XShade.c +++ b/src/lib/XShade.c @@ -33,7 +33,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "openaxiom-c-macros.h" #include <stdio.h> #include <stdlib.h> diff --git a/src/lib/XSpadFill.c b/src/lib/XSpadFill.c index 566c57ce..0b2212e5 100644 --- a/src/lib/XSpadFill.c +++ b/src/lib/XSpadFill.c @@ -58,7 +58,6 @@ #include <X11/Intrinsic.h> #include <X11/StringDefs.h> -#include "openaxiom-c-macros.h" #include "spadcolors.h" #include "XSpadFill.h" #include "XShade.h" diff --git a/src/lib/bsdsignal.cxx b/src/lib/bsdsignal.cxx index 07fc6b54..66e4dcd0 100644 --- a/src/lib/bsdsignal.cxx +++ b/src/lib/bsdsignal.cxx @@ -33,7 +33,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "openaxiom-c-macros.h" #include <signal.h> #include "bsdsignal.h" diff --git a/src/lib/cfuns-c.cxx b/src/lib/cfuns-c.cxx index 5dbcd6bc..5cb5f338 100644 --- a/src/lib/cfuns-c.cxx +++ b/src/lib/cfuns-c.cxx @@ -34,7 +34,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "openaxiom-c-macros.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include <errno.h> #include <limits.h> @@ -501,11 +503,11 @@ oa_acquire_temporary_pathname() { } return strdup(buf); #elif HAVE_DECL_MKTEMP - return mktemp(copy_c_str(std::string{ oa_get_tmpdir() } + "/oa-XXXXXX")); + return mktemp(strdup((std::string{ oa_get_tmpdir() } + "/oa-XXXXXX").c_str())); #elif HAVE_DECL_TEMPNAM return tempnam(oa_get_tmpdir(), "oa-"); #else - return copy_c_str("oa-" + std::to_string(random())); + return strdup(("oa-" + std::to_string(random())).c_str()); #endif } diff --git a/src/lib/cursor.c b/src/lib/cursor.c deleted file mode 100644 index 5413151d..00000000 --- a/src/lib/cursor.c +++ /dev/null @@ -1,144 +0,0 @@ -/* - Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. - All rights reserved. - Copyright (C) 2007-2010, Gabriel Dos Reis. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - - - Neither the name of The Numerical ALgorithms Group Ltd. nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER - OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#include <stdlib.h> -#include "openaxiom-c-macros.h" - -#include "cursor.h" - -/* - * This routine changes the shape of the cursor. it is a modified version of - * a program by SMWatt, called cursor.c. JMW 6/22/89 - */ - -/* this stuff can only be done on AIX <AND> the right terminal (aixterm,hft) */ -#if (defined(RIOSplatform) || defined(RTplatform)) && !defined(_AIX41) -#include "edible.h" -/* the HFT stuff requires ioctl's and termio's */ -#include <termio.h> -#include <stdio.h> -#include <fcntl.h> -#include <sys/hft.h> - -int -Cursor_shape(int shape) -{ - int hftfd; - char hftpath[16], s[100]; - int chno; - int i; - struct termio oldterm, newterm; - struct hftgetid hftgid; - char *termVal; - - termVal = oa_getenv("TERM"); - if (strcmp("hft", termVal) && strncmp("aixterm", termVal, 7)) - return; - - - - /* determine the desired shape */ - if (shape < 0 || shape > 5) { - fprintf(stderr, "%d - Invalid cursor number\n"); - return (-1); - } - /* change the shape */ - s[0] = 033; /* hf_intro.hf_esc */ - s[1] = '['; /* hf_intro.hf_lbr */ - s[2] = 'x'; /* hf_intro.hf_ex */ - s[3] = 0; /* hf_intro.hf_len[0] */ - s[4] = 0; /* hf_intro.hf_len[1] */ - s[5] = 0; /* hf_intro.hf_len[2] */ - s[6] = 10; /* hf_intro.hf_len[3] */ - s[7] = 2; /* hf_intro.hf_typehi */ - s[8] = 8; /* hf_intro.hf_typelo */ - s[9] = 2; /* hf_sublen */ - s[10] = 0; /* hf_subtype */ - s[11] = 0; /* hf_rsvd */ - s[12] = shape; /* hf_shape */ - - if (ioctl(0, HFTGETID, &hftgid) < 0) { - /* perror("ioctl: HFTGETID"); */ - chno = -1; - } - else - chno = hftgid.hf_chan; - if (chno == -1) { - /** try being moronic and just writing what I want to - standard output ****/ - - if (((ioctl(2, TCGETA, &oldterm)) == -1) || - ((ioctl(2, TCGETA, &newterm)) == -1)) { - perror("Getting termio"); - exit(0); - } - newterm.c_oflag = newterm.c_lflag = newterm.c_iflag = 0; - newterm.c_cc[0] = -1; - for (i = 1; i <= 5; i++) - newterm.c_cc[i] = 0; - if ((ioctl(2, TCSETAF, &newterm)) == -1) { - perror("Setting to raw mode"); - exit(0); - } - write(2, s, 13); - read(0, s, 1024); - if ((ioctl(2, TCSETAF, &oldterm)) == -1) { - perror("Resetting terminal"); - exit(0); - } - } - else { - /* open the currently active virtual terminal on the hft */ - sprintf(hftpath, "/dev/hft/%d", chno); - if ((hftfd = open(hftpath, O_RDWR)) == -1) { - perror("Could not open hft channel\n"); - exit(0); - } - write(hftfd, s, 13); - } -} -#else - -int -Cursor_shape(int shape) -{ - return shape; -} -#endif - - - - - diff --git a/src/lib/cursor.cc b/src/lib/cursor.cc new file mode 100644 index 00000000..af0b6603 --- /dev/null +++ b/src/lib/cursor.cc @@ -0,0 +1,143 @@ +/* + Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. + All rights reserved. + Copyright (C) 2007-2010, Gabriel Dos Reis. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + - Neither the name of The Numerical ALgorithms Group Ltd. nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <stdlib.h> + +#include "cursor.h" + +/* + * This routine changes the shape of the cursor. it is a modified version of + * a program by SMWatt, called cursor.c. JMW 6/22/89 + */ + +/* this stuff can only be done on AIX <AND> the right terminal (aixterm,hft) */ +#if (defined(RIOSplatform) || defined(RTplatform)) && !defined(_AIX41) +#include "edible.h" +/* the HFT stuff requires ioctl's and termio's */ +#include <termio.h> +#include <stdio.h> +#include <fcntl.h> +#include <sys/hft.h> + +int +Cursor_shape(int shape) +{ + int hftfd; + char hftpath[16], s[100]; + int chno; + int i; + struct termio oldterm, newterm; + struct hftgetid hftgid; + char *termVal; + + termVal = oa_getenv("TERM"); + if (strcmp("hft", termVal) && strncmp("aixterm", termVal, 7)) + return; + + + + /* determine the desired shape */ + if (shape < 0 || shape > 5) { + fprintf(stderr, "%d - Invalid cursor number\n"); + return (-1); + } + /* change the shape */ + s[0] = 033; /* hf_intro.hf_esc */ + s[1] = '['; /* hf_intro.hf_lbr */ + s[2] = 'x'; /* hf_intro.hf_ex */ + s[3] = 0; /* hf_intro.hf_len[0] */ + s[4] = 0; /* hf_intro.hf_len[1] */ + s[5] = 0; /* hf_intro.hf_len[2] */ + s[6] = 10; /* hf_intro.hf_len[3] */ + s[7] = 2; /* hf_intro.hf_typehi */ + s[8] = 8; /* hf_intro.hf_typelo */ + s[9] = 2; /* hf_sublen */ + s[10] = 0; /* hf_subtype */ + s[11] = 0; /* hf_rsvd */ + s[12] = shape; /* hf_shape */ + + if (ioctl(0, HFTGETID, &hftgid) < 0) { + /* perror("ioctl: HFTGETID"); */ + chno = -1; + } + else + chno = hftgid.hf_chan; + if (chno == -1) { + /** try being moronic and just writing what I want to + standard output ****/ + + if (((ioctl(2, TCGETA, &oldterm)) == -1) || + ((ioctl(2, TCGETA, &newterm)) == -1)) { + perror("Getting termio"); + exit(0); + } + newterm.c_oflag = newterm.c_lflag = newterm.c_iflag = 0; + newterm.c_cc[0] = -1; + for (i = 1; i <= 5; i++) + newterm.c_cc[i] = 0; + if ((ioctl(2, TCSETAF, &newterm)) == -1) { + perror("Setting to raw mode"); + exit(0); + } + write(2, s, 13); + read(0, s, 1024); + if ((ioctl(2, TCSETAF, &oldterm)) == -1) { + perror("Resetting terminal"); + exit(0); + } + } + else { + /* open the currently active virtual terminal on the hft */ + sprintf(hftpath, "/dev/hft/%d", chno); + if ((hftfd = open(hftpath, O_RDWR)) == -1) { + perror("Could not open hft channel\n"); + exit(0); + } + write(hftfd, s, 13); + } +} +#else + +int +Cursor_shape(int shape) +{ + return shape; +} +#endif + + + + + diff --git a/src/lib/edin.c b/src/lib/edin.c deleted file mode 100644 index 568bb77a..00000000 --- a/src/lib/edin.c +++ /dev/null @@ -1,951 +0,0 @@ -/* - Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. - All rights reserved. - Copyright (C) 2007-2010, Gabriel Dos Reis. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - - - Neither the name of The Numerical ALgorithms Group Ltd. nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER - OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -/* #define debug 1 */ - -#include <stdlib.h> -#include "openaxiom-c-macros.h" -#include <unistd.h> -#include <string.h> -#include <stdio.h> -#include <sys/types.h> - -#include "edible.h" - -#define HFT 0 -#define SUN 1 -#define DEC 2 -#define control_to_alpha(x) (x + ('A' - 0x01)) -#define alpha_to_control(x) (x - ('A' - 0x01)) - -int termId; -QueStruct *ring = NULL; -QueStruct *current = NULL; -int ring_size = 0; -int MAXRING = 64; -int prev_check = 10; -int curr_pntr; -int num_pntr; -int num_proc; -int had_tab; -int had_tab_last; -extern char buff[1024]; /* Buffers for collecting input and */ -extern int buff_flag[1024]; /* flags for whether buff chars - are printing or non-printing */ -int buff_pntr; /* present length of buff */ - - -#include "edin.h" -#include "prt.h" -#include "wct.h" -#include "cursor.h" -#include "fnct_key.h" - - - -void -init_reader(void) -{ - char *termVal; - - buff[50] = '\0'; /** initialize some stuff ***/ - init_flag(buff_flag, MAXLINE); - buff_pntr = curr_pntr = 0; - - had_tab = 0; - had_tab_last = 0; - termVal = (char *) getenv("TERM"); - if (!strcmp("sun", termVal)) - termId = SUN; - else if (!strcmp("xterm", termVal) || !strncmp("vt", termVal, 2)) - termId = DEC; - else if (!strcmp("hft", termVal) || !strncmp("aixterm", termVal, 7)) - termId = HFT; -} - - -void -do_reading(void) -{ - int ttt_read; - int done_completely; - - done_completely = 0; - num_proc = 0; - while (num_proc < num_read) { - if(in_buff[num_proc]== _ERASE) { - back_over_current_char(); - num_proc++; - } - else { - switch (in_buff[num_proc]) { - /* lets start checking for different types of chars */ - case _EOLN: - case _CR: - /* If I have read a complete line, so send it to the child */ - send_line_to_child(); - if (!PTY) - myputchar('\n'); - break; - - /* - * Use 0x7f as delete - */ - case _DEL: - /* Had a delete key */ - delete_current_char(); - break; - - case _CNTRL_W: - move_back_word(); - num_proc++; - break; - case _TAB: - had_tab = 1; - /* command completion stuff */ - num_proc++; - if (had_tab_last) - rescan_wct(); - else - find_wct(); - break; - case _BELL: - insert_buff_nonprinting(1); - putchar(_BELL); - fflush(stdout); - break; - case _ESC: - - /* - * get 2 characters more - */ - while (!(num_read - num_proc > 2)) { - ttt_read = read(0, - in_buff + num_read, - 2 - (num_read - num_proc) + 1); - if (ttt_read > 0) - num_read = num_read + ttt_read; - } - if (in_buff[num_proc + 1] == _LBRACK) { - - /* ESC [ */ - - switch (in_buff[num_proc + 2]) { - /* look for arrows */ - case _A: - /* up arrow */ - - /* - * The first thing I plan to do is get rid of the present - * input ** - */ - prev_buff(); - curr_pntr = buff_pntr; - num_proc = num_proc + 3; - break; - case _B: - /* down arrow */ - next_buff(); - curr_pntr = buff_pntr; - num_proc = num_proc + 3; - break; - case _C: - /* right arrow */ - move_ahead(); - num_proc = num_proc + 3; - break; - case _D: - /* left arrow */ - move_back(); - num_proc = num_proc + 3; - break; - - /* - * Use ^[[P as delete - */ - case _P: - /*** Had a delete key ****/ - delete_current_char(); - break; - case _H: - case 0: - move_home(); - num_proc += 3; - break; - case _M: - case _Z: - insert_buff_nonprinting(3); - done_completely = 1; - num_proc += 3; - break; - case _x: - num_proc = num_read; - break; - case _1: - case _2: - case _0: - - /* - * I have had a possible function key hit, look for the - * ones I want. check for ESC ] x ~ - */ - while (!(num_read - num_proc > 3)) { - ttt_read = read(0, - in_buff + num_read, - 3 - (num_read - num_proc) + 1); - if (ttt_read > 0) - num_read = num_read + ttt_read; - } - if (in_buff[num_proc + 3] == _twiddle) { - - /* - * treat ESC ] x ~ - */ - switch (in_buff[num_proc + 2]) { - case _2: - flip(INS_MODE); - if (INS_MODE) - Cursor_shape(5); - else - Cursor_shape(2); - reprint(curr_pntr); - num_proc += 4; - break; - default: - insert_buff_nonprinting(1); - break; - } - break; - } - /* check for ESC ] x y ~ */ - while (!(num_read - num_proc > 4)) { - ttt_read = read(0, - in_buff + num_read, - 4 - (num_read - num_proc) + 1); - if (ttt_read > 0) - num_read = num_read + ttt_read; - } - if (in_buff[num_proc + 4] == _twiddle) { - - /* - * treat ESC ] x y ~ - */ - insert_buff_nonprinting(1); - break; - } - - /* check for ESC ] x y z [q|z] */ - - while (!(num_read - num_proc > 5)) { - ttt_read = read(0, - in_buff + num_read, - 5 - (num_read - num_proc) + 1); - if (ttt_read > 0) - num_read = num_read + ttt_read; - } - if (insert_toggle(&in_buff[num_proc + 3])) { - flip(INS_MODE); - if (INS_MODE) - Cursor_shape(5); - else - Cursor_shape(2); - reprint(curr_pntr); - num_proc = num_proc + 6; - break; - } - else if (cntrl_end(&in_buff[num_proc + 3])) { - num_proc = num_proc + 6; - delete_to_end_of_line(); - break; - } - else if (back_word(&in_buff[num_proc + 3])) { - move_back_word(); - num_proc += 6; - break; - } - else if (fore_word(&in_buff[num_proc + 3])) { - move_fore_word(); - num_proc += 6; - break; - } - else if (end_key(&in_buff[num_proc + 3])) { - move_end(); - num_proc += 6; - break; - } - switch (in_buff[num_proc + 5]) { - case _q: - - /* - * IBM function keys - */ - { - char num[3]; - int key; - - num[0] = in_buff[num_proc + 3]; - num[1] = in_buff[num_proc + 4]; - num[2] = '\0'; - key = atoi(num); - if (key > 0 && key < 13) { - if (function_key[key].str != NULL) { - handle_function_key(key, contNum); - done_completely = 1; - } - else { - insert_buff_nonprinting(6); - done_completely = 1; - } - } - else { - insert_buff_nonprinting(6); - done_completely = 1; - } - break; - } - case _z: - - /* - * Sun function keys - */ - { - char num[3]; - int key; - - num[0] = in_buff[num_proc + 3]; - num[1] = in_buff[num_proc + 4]; - num[2] = '\0'; - key = atoi(num) - 23; - if (key > 0 && key < 13) { - if (function_key[key].str != NULL) { - handle_function_key(key, contNum); - done_completely = 1; - } - else { - insert_buff_nonprinting(6); - done_completely = 1; - } - } - else if (atoi(num) == 14) { - move_home(); - num_proc += 6; - done_completely = 1; - } - else if (atoi(num) == 20) { - move_end(); - num_proc += 6; - done_completely = 1; - } - else if (atoi(num) == 47) { - flip(INS_MODE); - if (INS_MODE) - Cursor_shape(5); - else - Cursor_shape(2); - reprint(curr_pntr); - num_proc = num_proc + 6; - done_completely = 1; - } - else { - insert_buff_nonprinting(6); - done_completely = 1; - } - - break; - } - - default: - insert_buff_nonprinting(1); - break; - } - default: - if (!done_completely) - insert_buff_nonprinting(1); - break; - } - } /* if */ - else { /* ESC w/o [ */ - insert_buff_nonprinting(1); - } - break; - - case _BKSPC: - back_over_current_char(); - num_proc++; - break; - default: - if (in_buff[num_proc] == _KILL) { - delete_line(); - num_proc++; - } - else { - if ((in_buff[num_proc] == _INTR) || (in_buff[num_proc] == _QUIT)) { - write(contNum, &in_buff[num_proc], num_read - num_proc); - if (!PTY) - write(contNum, "\n", 1); - num_proc++; - } - else { - if (in_buff[num_proc] == _EOF) { - insert_buff_nonprinting(1); - if (!PTY) - write(contNum, "\n", 1); - - /*comment out this bit - if (!buff_pntr) { - write(contNum, &in_buff[num_proc], 1); - if (!PTY) - write(contNum, "\n", 1); - } - else { - write(contNum, buff, buff_pntr); - } - */ - num_proc++; - } - else { - if (in_buff[num_proc] == _EOL) { - send_line_to_child(); - if (!PTY) - write(contNum, "\n", 1); - } - else { - if (in_buff[num_proc] == _ERASE) { - back_over_current_char(); - num_proc++; - } - else { - if (control_char(in_buff[num_proc])) - insert_buff_nonprinting(1); - else - insert_buff_printing(1); - } - } - } - } - } /* close the default case */ - break; - } /* switch */ - } /*else*/ - if (had_tab) { - had_tab_last = 1; - had_tab = 0; - } - else - had_tab_last = 0; - - } /* while */ -} - - - -void -send_line_to_child(void) -{ - static char converted_buffer[MAXLINE]; - int converted_num; - - /* Takes care of sending a line to the child, and resetting the - buffer for new input */ - - back_it_up(curr_pntr); - - /* start by putting the line into the command line ring ***/ - if (buff_pntr) - insert_queue(); - - /* finish the line and send it to the child **/ - buff[buff_pntr] = in_buff[num_proc]; - buff_flag[buff_pntr++] = 1; - buff[buff_pntr] = '\0'; - buff_flag[buff_pntr] = -1; - - /* - * Instead of actually writing the Line, I have to substitute in the - * actual characters recieved - */ - converted_num = - convert_buffer(converted_buffer, buff, buff_flag, buff_pntr); - write(contNum, converted_buffer, converted_num); - - /** reinitialize the buffer ***/ - init_flag(buff_flag, buff_pntr); - init_buff(buff, buff_pntr); - /** reinitialize my buffer pointers **/ - buff_pntr = curr_pntr = 0; - - /** reset the ring pointer **/ - current = NULL; - num_proc++; - return; -} - -int -convert_buffer(char *target, char *source,int * source_flag, int num) -{ - int i, j; - - /* - * Until I get something wierd, just keep copying - */ - for (i = 0, j = 0; i < num; i++, j++) { - switch (source[i]) { - case _CARROT: - if (source_flag[i] == 1) { - target[j] = source[i]; - } - else { - if (source[i + 1] == _LBRACK) { - target[j] = _ESC; - i++; - } - else if (source[i + 1] >= 'A' && source[i + 1] <= 'Z') { - target[j] = alpha_to_control(source[i + 1]); - i++; - } - } - break; - case '?': - default: - target[j] = source[i]; - } - } - return j; -} - - -void -insert_buff_printing(int amount) -{ - int count; - - /* This procedure takes the character at in_buff[num_proc] and adds - it to the buffer. It first checks to see if we should be inserting - or overwriting, and then does the appropriate thing */ - - if ((buff_pntr + amount) > 1023) { - putchar(_BELL); - fflush(stdout); - num_proc += amount; - } - else { - - if (INS_MODE) { - - forwardcopy(&buff[curr_pntr + amount], - &buff[curr_pntr], - buff_pntr - curr_pntr); - forwardflag_cpy(&buff_flag[curr_pntr + amount], - &buff_flag[curr_pntr], - buff_pntr - curr_pntr); - for (count = 0; count < amount; count++) { - buff[curr_pntr + count] = in_buff[num_proc + count]; - buff_flag[curr_pntr + count] = 1; - } - ins_print(curr_pntr, amount); - buff_pntr = buff_pntr + amount; - } - else { - for (count = 0; count < amount; count++) { - if (buff_flag[curr_pntr + count] == 2) { - myputchar(buff[curr_pntr + count]); - curr_pntr += count + 1; - delete_current_char(); - /** fix num_proc affected by delete **/ - num_proc -= 3; - curr_pntr -= count + 1; - myputchar(_BKSPC); - } - buff[curr_pntr + count] = in_buff[num_proc + count]; - buff_flag[curr_pntr + count] = 1; - } - myputchar(in_buff[num_proc]); - if (curr_pntr == buff_pntr) - buff_pntr++; - } - num_proc = num_proc + amount; - curr_pntr = curr_pntr + amount; - fflush(stdout); - } - return; - -} - -void -insert_buff_nonprinting(int amount) -{ - int count; - - /* This procedure takes the character at in_buff[num_proc] and adds - it to the buffer. It first checks to see if we should be inserting - or overwriting, and then does the appropriate thing */ - - /* it takes care of the special case, when I have an esc character */ - - if ((buff_pntr + amount) > 1023) { - myputchar(_BELL); - fflush(stdout); - num_proc += amount; - } - else { - if (INS_MODE) { - forwardcopy(&buff[curr_pntr + amount + 1], - &buff[curr_pntr], - buff_pntr - curr_pntr); - forwardflag_cpy(&buff_flag[curr_pntr + amount + 1], - &buff_flag[curr_pntr], - buff_pntr - curr_pntr); - /** now insert the special character **/ - switch (in_buff[num_proc]) { - case _ESC: - /** in this case I insert a '^[' into the string ***/ - buff[curr_pntr] = _CARROT; - buff_flag[curr_pntr] = 2; - buff[curr_pntr + 1] = _LBRACK; - buff_flag[curr_pntr + 1] = 0; - break; - default: - if (control_char(in_buff[num_proc])) { - buff[curr_pntr] = _CARROT; - buff_flag[curr_pntr] = 2; - buff[curr_pntr + 1] = control_to_alpha(in_buff[num_proc]); - buff_flag[curr_pntr + 1] = 0; - } - else { - /** What do I have ? **/ - buff[curr_pntr] = '?'; - buff_flag[curr_pntr] = 2; - buff[curr_pntr + 1] = in_buff[num_proc]; - buff_flag[curr_pntr] = 0; - break; - } - } - /** Now add the normal characters **/ - for (count = 1; count < amount; count++) { - buff[curr_pntr + count + 1] = in_buff[num_proc + count]; - buff_flag[curr_pntr + count + 1] = 1; - } - ins_print(curr_pntr, amount + 1); - buff_pntr = buff_pntr + amount + 1; - } - else { - /** I am in the overstrike mode **/ - switch (in_buff[num_proc]) { - case _ESC: - /** in this case I insert a '^[' into the string ***/ - buff[curr_pntr] = _CARROT; - buff_flag[curr_pntr] = 2; - buff[curr_pntr + 1] = _LBRACK; - buff_flag[curr_pntr + 1] = 0; - break; - default: - if (control_char(in_buff[num_proc])) { - buff[curr_pntr] = _CARROT; - buff_flag[curr_pntr] = 2; - buff[curr_pntr + 1] = control_to_alpha(in_buff[num_proc]); - buff_flag[curr_pntr + 1] = 0; - } - else { - /** What do I have ? **/ - buff[curr_pntr] = '?'; - buff_flag[curr_pntr] = 2; - buff[curr_pntr + 1] = in_buff[num_proc]; - buff_flag[curr_pntr] = 0; - break; - } - } - for (count = 1; count < amount; count++) { - if (buff_flag[curr_pntr + count] == 2) { - curr_pntr += count + 1; - delete_current_char(); - /** fix num. processed form delete **/ - num_proc -= 3; - curr_pntr -= count + 1; - } - buff[curr_pntr + count + 1] = in_buff[num_proc + count]; - buff_flag[curr_pntr + count + 1] = 1; - } - /** now print the characters I have put in **/ - printbuff(curr_pntr, amount + 1); - } - num_proc = num_proc + amount; - curr_pntr = curr_pntr + amount + 1; - if (curr_pntr > buff_pntr) - buff_pntr = curr_pntr; - } - return; - -} - -void -prev_buff(void) -{ - - /* - * If the current command ring is NULL, then I should NOT clear the - * current line. Thus my business is already done - */ - if (ring == NULL) - return; - clear_buff(); - init_buff(buff, buff_pntr); - init_flag(buff_flag, buff_pntr); - - if (current == NULL) { - if (ring == NULL) - return; - current = ring; - } - else - current = current->prev; - strcpy(buff, current->buff); - flagcpy(buff_flag, current->flags); - - /* first back up and blank the line */ - fflush(stdout); - printbuff(0, strlen(buff)); - curr_pntr = buff_pntr = strlen(buff); - fflush(stdout); - return ; -} - -void -next_buff(void) -{ - - /* - * If the current command ring is NULL, then I should NOT clear the - * current line. Thus my business is already done - */ - if (ring == NULL) - return; - clear_buff(); - init_buff(buff, buff_pntr); - init_flag(buff_flag, buff_pntr); - if (current == NULL) { - if (ring == NULL) - return; - current = ring->next; - } - else - current = current->next; - strcpy(buff, current->buff); - flagcpy(buff_flag, current->flags); - - /* first back up and blank the line **/ - fflush(stdout); - printbuff(0, strlen(buff)); - curr_pntr = buff_pntr = strlen(buff); - fflush(stdout); - return ; -} - - -void -forwardcopy(char *buff1,char * buff2,int num) -{ - int count; - - for (count = num; count >= 0; count--) - buff1[count] = buff2[count]; -} - - -void -forwardflag_cpy(int *buff1,int * buff2,int num) -{ - int count; - - for (count = num; count >= 0; count--) - buff1[count] = buff2[count]; -} - -void -strnmov(char *dest, const char *src, size_t n) -{ - while (n-- && (*dest++ = *src++)); -} - -void -strmov(char *dest, const char *src) -{ - while ((*dest++ = *src++)); -} - -void -flagcpy(int *s,int *t) -{ - while (*t >= 0) - *s++ = *t++; - *s = *t; -} - -void -flagncpy(int *s,int *t,int n) -{ - while (n-- > 0) - *s++ = *t++; -} - -void -insert_queue(void) -{ - QueStruct *trace; - QueStruct *new_q; - int c; - - if (!ECHOIT) - return; - if (ring != NULL && !strcmp(buff, ring->buff)) - return; - for (c = 0, trace = ring; trace != NULL && c < (prev_check - 1); - c++, trace = trace->prev) { - if (!strcmp(buff, trace->buff)) { - - /* - * throw this puppy at the end of the ring - */ - trace->next->prev = trace->prev; - trace->prev->next = trace->next; - trace->prev = ring; - trace->next = ring->next; - ring->next = trace; - trace->next->prev = trace; - ring = trace; - return; - } - } - - /* - * simply places the buff command into the front of the queue - */ - if (ring_size < MAXRING) { - new_q = (QueStruct *) malloc(sizeof(struct que_struct)); - if (new_q == NULL) { - fprintf(stderr, "Malloc Error: Ran out of memory\n"); - exit(-1); - } - if (ring_size == 0) { - ring = new_q; - ring->prev = ring->next = new_q; - } - else { - new_q->next = ring->next; - new_q->prev = ring; - ring->next = new_q; - new_q->next->prev = new_q; - ring = new_q; - } - ring_size++; - } - else - ring = ring->next; - - init_flag(ring->flags, MAXLINE); - init_buff(ring->buff, MAXLINE); - strcpy(ring->buff, buff); - flagncpy(ring->flags, buff_flag, buff_pntr); - (ring->buff)[buff_pntr] = '\0'; - (ring->flags)[buff_pntr] = -1; -} - - -void -init_flag(int *flags, int num) -{ - int i; - - for (i = 0; i < num; i++) - flags[i] = -1; -} - -void -init_buff(char *flags, int num) -{ - int i; - - for (i = 0; i < num; i++) - flags[i] = '\0'; -} - - -void -send_function_to_child(void) -{ - /* Takes care of sending a line to the child, and resetting the - buffer for new input */ - - back_it_up(curr_pntr); - /** start by putting the line into the command line ring ***/ - if (buff_pntr) - insert_queue(); - - /** finish the line and send it to the child **/ - buff[buff_pntr] = _EOLN; - - buff_flag[buff_pntr++] = 1; - buff[buff_pntr] = '\0'; - buff_flag[buff_pntr] = 0; - write(contNum, buff, buff_pntr); - - /** reinitialize the buffer ***/ - init_flag(buff_flag, buff_pntr); - init_buff(buff, buff_pntr); - /** reinitialize my buffer pointers **/ - buff_pntr = curr_pntr = 0; - - /** reset the ring pointer **/ - current = NULL; - - num_proc++; - return; -} - -void -send_buff_to_child(int chann) -{ - if (buff_pntr > 0) - write(chann, buff, buff_pntr); - num_proc += 6; - /** reinitialize the buffer ***/ - init_flag(buff_flag, buff_pntr); - init_buff(buff, buff_pntr); - /** reinitialize my buffer pointers **/ - buff_pntr = curr_pntr = 0; - /** reset the ring pointer **/ - current = NULL; - return; -} - diff --git a/src/lib/edin.cc b/src/lib/edin.cc new file mode 100644 index 00000000..7fee5176 --- /dev/null +++ b/src/lib/edin.cc @@ -0,0 +1,950 @@ +/* + Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. + All rights reserved. + Copyright (C) 2007-2010, Gabriel Dos Reis. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + - Neither the name of The Numerical ALgorithms Group Ltd. nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* #define debug 1 */ + +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <stdio.h> +#include <sys/types.h> + +#include "edible.h" + +#define HFT 0 +#define SUN 1 +#define DEC 2 +#define control_to_alpha(x) (x + ('A' - 0x01)) +#define alpha_to_control(x) (x - ('A' - 0x01)) + +int termId; +QueStruct *ring = NULL; +QueStruct *current = NULL; +int ring_size = 0; +int MAXRING = 64; +int prev_check = 10; +int curr_pntr; +int num_pntr; +int num_proc; +int had_tab; +int had_tab_last; +extern char buff[1024]; /* Buffers for collecting input and */ +extern int buff_flag[1024]; /* flags for whether buff chars + are printing or non-printing */ +int buff_pntr; /* present length of buff */ + + +#include "edin.h" +#include "prt.h" +#include "wct.h" +#include "cursor.h" +#include "fnct_key.h" + + + +void +init_reader(void) +{ + char *termVal; + + buff[50] = '\0'; /** initialize some stuff ***/ + init_flag(buff_flag, MAXLINE); + buff_pntr = curr_pntr = 0; + + had_tab = 0; + had_tab_last = 0; + termVal = (char *) getenv("TERM"); + if (!strcmp("sun", termVal)) + termId = SUN; + else if (!strcmp("xterm", termVal) || !strncmp("vt", termVal, 2)) + termId = DEC; + else if (!strcmp("hft", termVal) || !strncmp("aixterm", termVal, 7)) + termId = HFT; +} + + +void +do_reading(void) +{ + int ttt_read; + int done_completely; + + done_completely = 0; + num_proc = 0; + while (num_proc < num_read) { + if(in_buff[num_proc]== _ERASE) { + back_over_current_char(); + num_proc++; + } + else { + switch (in_buff[num_proc]) { + /* lets start checking for different types of chars */ + case _EOLN: + case _CR: + /* If I have read a complete line, so send it to the child */ + send_line_to_child(); + if (!PTY) + myputchar('\n'); + break; + + /* + * Use 0x7f as delete + */ + case _DEL: + /* Had a delete key */ + delete_current_char(); + break; + + case _CNTRL_W: + move_back_word(); + num_proc++; + break; + case _TAB: + had_tab = 1; + /* command completion stuff */ + num_proc++; + if (had_tab_last) + rescan_wct(); + else + find_wct(); + break; + case _BELL: + insert_buff_nonprinting(1); + putchar(_BELL); + fflush(stdout); + break; + case _ESC: + + /* + * get 2 characters more + */ + while (!(num_read - num_proc > 2)) { + ttt_read = read(0, + in_buff + num_read, + 2 - (num_read - num_proc) + 1); + if (ttt_read > 0) + num_read = num_read + ttt_read; + } + if (in_buff[num_proc + 1] == _LBRACK) { + + /* ESC [ */ + + switch (in_buff[num_proc + 2]) { + /* look for arrows */ + case _A: + /* up arrow */ + + /* + * The first thing I plan to do is get rid of the present + * input ** + */ + prev_buff(); + curr_pntr = buff_pntr; + num_proc = num_proc + 3; + break; + case _B: + /* down arrow */ + next_buff(); + curr_pntr = buff_pntr; + num_proc = num_proc + 3; + break; + case _C: + /* right arrow */ + move_ahead(); + num_proc = num_proc + 3; + break; + case _D: + /* left arrow */ + move_back(); + num_proc = num_proc + 3; + break; + + /* + * Use ^[[P as delete + */ + case _P: + /*** Had a delete key ****/ + delete_current_char(); + break; + case _H: + case 0: + move_home(); + num_proc += 3; + break; + case _M: + case _Z: + insert_buff_nonprinting(3); + done_completely = 1; + num_proc += 3; + break; + case _x: + num_proc = num_read; + break; + case _1: + case _2: + case _0: + + /* + * I have had a possible function key hit, look for the + * ones I want. check for ESC ] x ~ + */ + while (!(num_read - num_proc > 3)) { + ttt_read = read(0, + in_buff + num_read, + 3 - (num_read - num_proc) + 1); + if (ttt_read > 0) + num_read = num_read + ttt_read; + } + if (in_buff[num_proc + 3] == _twiddle) { + + /* + * treat ESC ] x ~ + */ + switch (in_buff[num_proc + 2]) { + case _2: + flip(INS_MODE); + if (INS_MODE) + Cursor_shape(5); + else + Cursor_shape(2); + reprint(curr_pntr); + num_proc += 4; + break; + default: + insert_buff_nonprinting(1); + break; + } + break; + } + /* check for ESC ] x y ~ */ + while (!(num_read - num_proc > 4)) { + ttt_read = read(0, + in_buff + num_read, + 4 - (num_read - num_proc) + 1); + if (ttt_read > 0) + num_read = num_read + ttt_read; + } + if (in_buff[num_proc + 4] == _twiddle) { + + /* + * treat ESC ] x y ~ + */ + insert_buff_nonprinting(1); + break; + } + + /* check for ESC ] x y z [q|z] */ + + while (!(num_read - num_proc > 5)) { + ttt_read = read(0, + in_buff + num_read, + 5 - (num_read - num_proc) + 1); + if (ttt_read > 0) + num_read = num_read + ttt_read; + } + if (insert_toggle(&in_buff[num_proc + 3])) { + flip(INS_MODE); + if (INS_MODE) + Cursor_shape(5); + else + Cursor_shape(2); + reprint(curr_pntr); + num_proc = num_proc + 6; + break; + } + else if (cntrl_end(&in_buff[num_proc + 3])) { + num_proc = num_proc + 6; + delete_to_end_of_line(); + break; + } + else if (back_word(&in_buff[num_proc + 3])) { + move_back_word(); + num_proc += 6; + break; + } + else if (fore_word(&in_buff[num_proc + 3])) { + move_fore_word(); + num_proc += 6; + break; + } + else if (end_key(&in_buff[num_proc + 3])) { + move_end(); + num_proc += 6; + break; + } + switch (in_buff[num_proc + 5]) { + case _q: + + /* + * IBM function keys + */ + { + char num[3]; + int key; + + num[0] = in_buff[num_proc + 3]; + num[1] = in_buff[num_proc + 4]; + num[2] = '\0'; + key = atoi(num); + if (key > 0 && key < 13) { + if (function_key[key].str != NULL) { + handle_function_key(key, contNum); + done_completely = 1; + } + else { + insert_buff_nonprinting(6); + done_completely = 1; + } + } + else { + insert_buff_nonprinting(6); + done_completely = 1; + } + break; + } + case _z: + + /* + * Sun function keys + */ + { + char num[3]; + int key; + + num[0] = in_buff[num_proc + 3]; + num[1] = in_buff[num_proc + 4]; + num[2] = '\0'; + key = atoi(num) - 23; + if (key > 0 && key < 13) { + if (function_key[key].str != NULL) { + handle_function_key(key, contNum); + done_completely = 1; + } + else { + insert_buff_nonprinting(6); + done_completely = 1; + } + } + else if (atoi(num) == 14) { + move_home(); + num_proc += 6; + done_completely = 1; + } + else if (atoi(num) == 20) { + move_end(); + num_proc += 6; + done_completely = 1; + } + else if (atoi(num) == 47) { + flip(INS_MODE); + if (INS_MODE) + Cursor_shape(5); + else + Cursor_shape(2); + reprint(curr_pntr); + num_proc = num_proc + 6; + done_completely = 1; + } + else { + insert_buff_nonprinting(6); + done_completely = 1; + } + + break; + } + + default: + insert_buff_nonprinting(1); + break; + } + default: + if (!done_completely) + insert_buff_nonprinting(1); + break; + } + } /* if */ + else { /* ESC w/o [ */ + insert_buff_nonprinting(1); + } + break; + + case _BKSPC: + back_over_current_char(); + num_proc++; + break; + default: + if (in_buff[num_proc] == _KILL) { + delete_line(); + num_proc++; + } + else { + if ((in_buff[num_proc] == _INTR) || (in_buff[num_proc] == _QUIT)) { + write(contNum, &in_buff[num_proc], num_read - num_proc); + if (!PTY) + write(contNum, "\n", 1); + num_proc++; + } + else { + if (in_buff[num_proc] == _EOF) { + insert_buff_nonprinting(1); + if (!PTY) + write(contNum, "\n", 1); + + /*comment out this bit + if (!buff_pntr) { + write(contNum, &in_buff[num_proc], 1); + if (!PTY) + write(contNum, "\n", 1); + } + else { + write(contNum, buff, buff_pntr); + } + */ + num_proc++; + } + else { + if (in_buff[num_proc] == _EOL) { + send_line_to_child(); + if (!PTY) + write(contNum, "\n", 1); + } + else { + if (in_buff[num_proc] == _ERASE) { + back_over_current_char(); + num_proc++; + } + else { + if (control_char(in_buff[num_proc])) + insert_buff_nonprinting(1); + else + insert_buff_printing(1); + } + } + } + } + } /* close the default case */ + break; + } /* switch */ + } /*else*/ + if (had_tab) { + had_tab_last = 1; + had_tab = 0; + } + else + had_tab_last = 0; + + } /* while */ +} + + + +void +send_line_to_child(void) +{ + static char converted_buffer[MAXLINE]; + int converted_num; + + /* Takes care of sending a line to the child, and resetting the + buffer for new input */ + + back_it_up(curr_pntr); + + /* start by putting the line into the command line ring ***/ + if (buff_pntr) + insert_queue(); + + /* finish the line and send it to the child **/ + buff[buff_pntr] = in_buff[num_proc]; + buff_flag[buff_pntr++] = 1; + buff[buff_pntr] = '\0'; + buff_flag[buff_pntr] = -1; + + /* + * Instead of actually writing the Line, I have to substitute in the + * actual characters recieved + */ + converted_num = + convert_buffer(converted_buffer, buff, buff_flag, buff_pntr); + write(contNum, converted_buffer, converted_num); + + /** reinitialize the buffer ***/ + init_flag(buff_flag, buff_pntr); + init_buff(buff, buff_pntr); + /** reinitialize my buffer pointers **/ + buff_pntr = curr_pntr = 0; + + /** reset the ring pointer **/ + current = NULL; + num_proc++; + return; +} + +int +convert_buffer(char *target, char *source,int * source_flag, int num) +{ + int i, j; + + /* + * Until I get something wierd, just keep copying + */ + for (i = 0, j = 0; i < num; i++, j++) { + switch (source[i]) { + case _CARROT: + if (source_flag[i] == 1) { + target[j] = source[i]; + } + else { + if (source[i + 1] == _LBRACK) { + target[j] = _ESC; + i++; + } + else if (source[i + 1] >= 'A' && source[i + 1] <= 'Z') { + target[j] = alpha_to_control(source[i + 1]); + i++; + } + } + break; + case '?': + default: + target[j] = source[i]; + } + } + return j; +} + + +void +insert_buff_printing(int amount) +{ + int count; + + /* This procedure takes the character at in_buff[num_proc] and adds + it to the buffer. It first checks to see if we should be inserting + or overwriting, and then does the appropriate thing */ + + if ((buff_pntr + amount) > 1023) { + putchar(_BELL); + fflush(stdout); + num_proc += amount; + } + else { + + if (INS_MODE) { + + forwardcopy(&buff[curr_pntr + amount], + &buff[curr_pntr], + buff_pntr - curr_pntr); + forwardflag_cpy(&buff_flag[curr_pntr + amount], + &buff_flag[curr_pntr], + buff_pntr - curr_pntr); + for (count = 0; count < amount; count++) { + buff[curr_pntr + count] = in_buff[num_proc + count]; + buff_flag[curr_pntr + count] = 1; + } + ins_print(curr_pntr, amount); + buff_pntr = buff_pntr + amount; + } + else { + for (count = 0; count < amount; count++) { + if (buff_flag[curr_pntr + count] == 2) { + myputchar(buff[curr_pntr + count]); + curr_pntr += count + 1; + delete_current_char(); + /** fix num_proc affected by delete **/ + num_proc -= 3; + curr_pntr -= count + 1; + myputchar(_BKSPC); + } + buff[curr_pntr + count] = in_buff[num_proc + count]; + buff_flag[curr_pntr + count] = 1; + } + myputchar(in_buff[num_proc]); + if (curr_pntr == buff_pntr) + buff_pntr++; + } + num_proc = num_proc + amount; + curr_pntr = curr_pntr + amount; + fflush(stdout); + } + return; + +} + +void +insert_buff_nonprinting(int amount) +{ + int count; + + /* This procedure takes the character at in_buff[num_proc] and adds + it to the buffer. It first checks to see if we should be inserting + or overwriting, and then does the appropriate thing */ + + /* it takes care of the special case, when I have an esc character */ + + if ((buff_pntr + amount) > 1023) { + myputchar(_BELL); + fflush(stdout); + num_proc += amount; + } + else { + if (INS_MODE) { + forwardcopy(&buff[curr_pntr + amount + 1], + &buff[curr_pntr], + buff_pntr - curr_pntr); + forwardflag_cpy(&buff_flag[curr_pntr + amount + 1], + &buff_flag[curr_pntr], + buff_pntr - curr_pntr); + /** now insert the special character **/ + switch (in_buff[num_proc]) { + case _ESC: + /** in this case I insert a '^[' into the string ***/ + buff[curr_pntr] = _CARROT; + buff_flag[curr_pntr] = 2; + buff[curr_pntr + 1] = _LBRACK; + buff_flag[curr_pntr + 1] = 0; + break; + default: + if (control_char(in_buff[num_proc])) { + buff[curr_pntr] = _CARROT; + buff_flag[curr_pntr] = 2; + buff[curr_pntr + 1] = control_to_alpha(in_buff[num_proc]); + buff_flag[curr_pntr + 1] = 0; + } + else { + /** What do I have ? **/ + buff[curr_pntr] = '?'; + buff_flag[curr_pntr] = 2; + buff[curr_pntr + 1] = in_buff[num_proc]; + buff_flag[curr_pntr] = 0; + break; + } + } + /** Now add the normal characters **/ + for (count = 1; count < amount; count++) { + buff[curr_pntr + count + 1] = in_buff[num_proc + count]; + buff_flag[curr_pntr + count + 1] = 1; + } + ins_print(curr_pntr, amount + 1); + buff_pntr = buff_pntr + amount + 1; + } + else { + /** I am in the overstrike mode **/ + switch (in_buff[num_proc]) { + case _ESC: + /** in this case I insert a '^[' into the string ***/ + buff[curr_pntr] = _CARROT; + buff_flag[curr_pntr] = 2; + buff[curr_pntr + 1] = _LBRACK; + buff_flag[curr_pntr + 1] = 0; + break; + default: + if (control_char(in_buff[num_proc])) { + buff[curr_pntr] = _CARROT; + buff_flag[curr_pntr] = 2; + buff[curr_pntr + 1] = control_to_alpha(in_buff[num_proc]); + buff_flag[curr_pntr + 1] = 0; + } + else { + /** What do I have ? **/ + buff[curr_pntr] = '?'; + buff_flag[curr_pntr] = 2; + buff[curr_pntr + 1] = in_buff[num_proc]; + buff_flag[curr_pntr] = 0; + break; + } + } + for (count = 1; count < amount; count++) { + if (buff_flag[curr_pntr + count] == 2) { + curr_pntr += count + 1; + delete_current_char(); + /** fix num. processed form delete **/ + num_proc -= 3; + curr_pntr -= count + 1; + } + buff[curr_pntr + count + 1] = in_buff[num_proc + count]; + buff_flag[curr_pntr + count + 1] = 1; + } + /** now print the characters I have put in **/ + printbuff(curr_pntr, amount + 1); + } + num_proc = num_proc + amount; + curr_pntr = curr_pntr + amount + 1; + if (curr_pntr > buff_pntr) + buff_pntr = curr_pntr; + } + return; + +} + +void +prev_buff(void) +{ + + /* + * If the current command ring is NULL, then I should NOT clear the + * current line. Thus my business is already done + */ + if (ring == NULL) + return; + clear_buff(); + init_buff(buff, buff_pntr); + init_flag(buff_flag, buff_pntr); + + if (current == NULL) { + if (ring == NULL) + return; + current = ring; + } + else + current = current->prev; + strcpy(buff, current->buff); + flagcpy(buff_flag, current->flags); + + /* first back up and blank the line */ + fflush(stdout); + printbuff(0, strlen(buff)); + curr_pntr = buff_pntr = strlen(buff); + fflush(stdout); + return ; +} + +void +next_buff(void) +{ + + /* + * If the current command ring is NULL, then I should NOT clear the + * current line. Thus my business is already done + */ + if (ring == NULL) + return; + clear_buff(); + init_buff(buff, buff_pntr); + init_flag(buff_flag, buff_pntr); + if (current == NULL) { + if (ring == NULL) + return; + current = ring->next; + } + else + current = current->next; + strcpy(buff, current->buff); + flagcpy(buff_flag, current->flags); + + /* first back up and blank the line **/ + fflush(stdout); + printbuff(0, strlen(buff)); + curr_pntr = buff_pntr = strlen(buff); + fflush(stdout); + return ; +} + + +void +forwardcopy(char *buff1,char * buff2,int num) +{ + int count; + + for (count = num; count >= 0; count--) + buff1[count] = buff2[count]; +} + + +void +forwardflag_cpy(int *buff1,int * buff2,int num) +{ + int count; + + for (count = num; count >= 0; count--) + buff1[count] = buff2[count]; +} + +void +strnmov(char *dest, const char *src, size_t n) +{ + while (n-- && (*dest++ = *src++)); +} + +void +strmov(char *dest, const char *src) +{ + while ((*dest++ = *src++)); +} + +void +flagcpy(int *s,int *t) +{ + while (*t >= 0) + *s++ = *t++; + *s = *t; +} + +void +flagncpy(int *s,int *t,int n) +{ + while (n-- > 0) + *s++ = *t++; +} + +void +insert_queue(void) +{ + QueStruct *trace; + QueStruct *new_q; + int c; + + if (!ECHOIT) + return; + if (ring != NULL && !strcmp(buff, ring->buff)) + return; + for (c = 0, trace = ring; trace != NULL && c < (prev_check - 1); + c++, trace = trace->prev) { + if (!strcmp(buff, trace->buff)) { + + /* + * throw this puppy at the end of the ring + */ + trace->next->prev = trace->prev; + trace->prev->next = trace->next; + trace->prev = ring; + trace->next = ring->next; + ring->next = trace; + trace->next->prev = trace; + ring = trace; + return; + } + } + + /* + * simply places the buff command into the front of the queue + */ + if (ring_size < MAXRING) { + new_q = (QueStruct *) malloc(sizeof(struct que_struct)); + if (new_q == NULL) { + fprintf(stderr, "Malloc Error: Ran out of memory\n"); + exit(-1); + } + if (ring_size == 0) { + ring = new_q; + ring->prev = ring->next = new_q; + } + else { + new_q->next = ring->next; + new_q->prev = ring; + ring->next = new_q; + new_q->next->prev = new_q; + ring = new_q; + } + ring_size++; + } + else + ring = ring->next; + + init_flag(ring->flags, MAXLINE); + init_buff(ring->buff, MAXLINE); + strcpy(ring->buff, buff); + flagncpy(ring->flags, buff_flag, buff_pntr); + (ring->buff)[buff_pntr] = '\0'; + (ring->flags)[buff_pntr] = -1; +} + + +void +init_flag(int *flags, int num) +{ + int i; + + for (i = 0; i < num; i++) + flags[i] = -1; +} + +void +init_buff(char *flags, int num) +{ + int i; + + for (i = 0; i < num; i++) + flags[i] = '\0'; +} + + +void +send_function_to_child(void) +{ + /* Takes care of sending a line to the child, and resetting the + buffer for new input */ + + back_it_up(curr_pntr); + /** start by putting the line into the command line ring ***/ + if (buff_pntr) + insert_queue(); + + /** finish the line and send it to the child **/ + buff[buff_pntr] = _EOLN; + + buff_flag[buff_pntr++] = 1; + buff[buff_pntr] = '\0'; + buff_flag[buff_pntr] = 0; + write(contNum, buff, buff_pntr); + + /** reinitialize the buffer ***/ + init_flag(buff_flag, buff_pntr); + init_buff(buff, buff_pntr); + /** reinitialize my buffer pointers **/ + buff_pntr = curr_pntr = 0; + + /** reset the ring pointer **/ + current = NULL; + + num_proc++; + return; +} + +void +send_buff_to_child(int chann) +{ + if (buff_pntr > 0) + write(chann, buff, buff_pntr); + num_proc += 6; + /** reinitialize the buffer ***/ + init_flag(buff_flag, buff_pntr); + init_buff(buff, buff_pntr); + /** reinitialize my buffer pointers **/ + buff_pntr = curr_pntr = 0; + /** reset the ring pointer **/ + current = NULL; + return; +} + diff --git a/src/lib/emupty.c b/src/lib/emupty.c index 7ddfbd2d..9b9848c9 100644 --- a/src/lib/emupty.c +++ b/src/lib/emupty.c @@ -31,7 +31,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "openaxiom-c-macros.h" /* Here is some code taken from Nick Simicich. It takes an escape sequence diff --git a/src/lib/fnct_key.c b/src/lib/fnct_key.c deleted file mode 100644 index f70bc430..00000000 --- a/src/lib/fnct_key.c +++ /dev/null @@ -1,345 +0,0 @@ -/* - Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. - All rights reserved. - Copyright (C) 2007-2013 Gabriel Dos Reis. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - - - Neither the name of The Numerical ALgorithms Group Ltd. nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER - OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#include "openaxiom-c-macros.h" -#include <unistd.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <fcntl.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <sys/wait.h> - -#include "cfuns.h" -#include "edible.h" -#include "bsdsignal.h" - - -#include "fnct_key.h" -#include "prt.h" -#include "edin.h" - -/* FIXME: Remove this in complete rewrite */ -using namespace OpenAxiom; - -/** Some constants for functio key defs ****/ -#define DELAYED 0 -#define IMMEDIATE 1 -#define SPECIAL 2 - - -/** Here is the structure for storing bound pf-keys ***/ -fkey function_key[13]; /** Strings which replace function - keys when a key is hit ***/ - -static const char *defaulteditor = "clefedit"; -char editorfilename[100]; - - - -/* - * The following function environment variable clef editor. The command - * should be the one that the user wishes to have execed - */ - -void -set_editor_key(void) -{ - int pid; - - sprintf(editorfilename, "/tmp/clef%d", pid = oa_getpid()); - - if (function_key[12].str == NULL) { - (function_key[12]).type = SPECIAL; - (function_key[12]).str = defaulteditor; - } -} - - - - -/*** This routine id used to find the users function key mappings. It - simply searches the users HOME directory for a file called ".clef". - If found it gets the key bindings from within - *****/ - -void -define_function_keys(void) -{ - char *HOME, path[1024], string[1024]; - int key; - int fd; - char type; - - /** lets initialize the key pointers **/ - for (key = 0; key < 13; key++) - (function_key[key]).str = NULL; - /** see if the user has a .clef file ***/ - HOME = oa_getenv("HOME"); - sprintf(path, "%s/.clef", HOME); - if ((fd = open(path, O_RDONLY)) == -1) { - return; - } - else { - /*** If so, then get the key bindings **/ - while ((key = get_key(fd, &type))) { - get_str(fd, string); - switch (type) { - case 'D': - if (key == 12) { - fprintf(stderr, - "Clef Error: PF12 can only be of type E in .clef\n"); - fprintf(stderr, "Line will be ignored\n"); - type = -1; - } - else { - (function_key[key]).type = DELAYED; - } - break; - case 'F': - if (key == 12) { - fprintf(stderr, - "Clef Error: PF12 can only be of type E in .clef\n"); - fprintf(stderr, "Line will be ignored\n"); - type = -1; - } - else { - (function_key[key]).type = IMMEDIATE; - } - break; - case 'E': - if (key != 12) { - fprintf(stderr, - "Clef Error: PF12 can only be of type E in .clef\n"); - fprintf(stderr, "Line will be ignored\n"); - type = -1; - } - else { - (function_key[key]).type = SPECIAL; - } - break; - } - if (type != -1) - (function_key[key]).str = strdup(string); - } - } - - /* - * Now set the editor function key - */ - set_editor_key(); -} - - -#define defof(c) ((c == 'F' || c == 'D' || c == 'E')?(1):(0)) - -int -get_key(int fd,char * ty) -{ - - /* - * Determines the key number being mapped, and whether it is immediate or - * delay. It reurns the key value, and modifies the parameter type - */ - char keynum[1024]; - int nr; - - nr = read(fd, keynum, 3); - if (nr != -1 && nr != 0) { - if (!defof(keynum[0])) { - return 0; - } - else { - *ty = keynum[0]; - keynum[3] = '\0'; - return (atoi(&keynum[1])); - } - } - else - return 0; -} - -int -get_str(int fd,char * string) -{ - /** Gets the key mapping being bound **/ - char c; - int count = 0; - char *trace = string; - - read(fd, &c, 1); - while (c == ' ') - read(fd, &c, 1); - while (c != '\n') { - count++; - *trace++ = c; - if (read(fd, &c, 1) == 0) - break; - } - *trace = '\0'; - return count; -} - -void -null_fnct(int sig) -{ - return; -} - -void -handle_function_key(int key,int chann) -{ - /** this procedure simply adds the string specified by the function key - to the buffer ****/ - int count, fd; - int amount = strlen(function_key[key].str); - int id; - - /*** This procedure takes the character at in_buff[num_proc] and adds - it to the buffer. It first checks to see if we should be inserting - or overwriting, and then does the appropriate thing *******/ - - switch ((function_key[key]).type) { - case IMMEDIATE: - if (INS_MODE) { - forwardcopy(&buff[curr_pntr + amount], - &buff[curr_pntr], - buff_pntr - curr_pntr); - forwardflag_cpy(&buff_flag[curr_pntr + amount], - &buff_flag[curr_pntr], - buff_pntr - curr_pntr); - for (count = 0; count < amount; count++) { - buff[curr_pntr + count] = (function_key[key].str)[count]; - buff_flag[curr_pntr + count] = '1'; - } - ins_print(curr_pntr, amount + 1); - buff_pntr = buff_pntr + amount; - } - else { - for (count = 0; count < amount; count++) { - buff[curr_pntr + count] = (function_key[key].str)[count]; - buff_flag[curr_pntr + count] = '1'; - myputchar((function_key[key].str)[count]); - } - } - num_proc = num_proc + 6; - curr_pntr = curr_pntr + amount; - buff_pntr = buff_pntr + amount; - send_function_to_child(); - break; - case DELAYED: - if (INS_MODE) { - forwardcopy(&buff[curr_pntr + amount], - &buff[curr_pntr], - buff_pntr - curr_pntr); - forwardflag_cpy(&buff_flag[curr_pntr + amount], - &buff_flag[curr_pntr], - buff_pntr - curr_pntr); - for (count = 0; count < amount; count++) { - buff[curr_pntr + count] = (function_key[key].str)[count]; - buff_flag[curr_pntr + count] = '1'; - } - ins_print(curr_pntr, amount + 1); - buff_pntr = buff_pntr + amount; - } - else { - for (count = 0; count < amount; count++) { - buff[curr_pntr + count] = (function_key[key].str)[count]; - buff_flag[curr_pntr + count] = '1'; - myputchar((function_key[key].str)[count]); - } - } - num_proc = num_proc + 6; - curr_pntr = curr_pntr + amount; - buff_pntr = buff_pntr + amount; - fflush(stdout); - break; - case SPECIAL: - /* fprintf(stderr, "Here I am \n"); */ - if (access(editorfilename, F_OK) < 0) { - fd = open(editorfilename, O_RDWR | O_CREAT, 0666); - write(fd, buff, buff_pntr); - back_up(buff_pntr); - close(fd); - } - else { - if (buff_pntr > 0) { - fd = open(editorfilename, O_RDWR | O_TRUNC); - write(fd, buff, buff_pntr); - back_up(buff_pntr); - close(fd); - } - } - bsdSignal(OPENAXIOM_SIGCHLD, null_fnct,RestartSystemCalls); - switch (id = fork()) { - case -1: - perror("Special key"); - break; - case 0: - execlp((function_key[12]).str, - (function_key[12]).str, - editorfilename, (char*) NULL); - perror("Returned from exec"); - exit(0); - - } - while (wait((int *) 0) < 0); - /** now I should read that file and send all it stuff thru the - reader *****/ - fd = open(editorfilename, O_RDWR); - if (fd == -1) { - perror("Opening temp file"); - exit(-1); - } - num_proc += 6; - - /** reinitialize the buffer ***/ - init_flag(buff_flag, buff_pntr); - init_buff(buff, buff_pntr); - /** reinitialize my buffer pointers **/ - buff_pntr = curr_pntr = 0; - /** reset the ring pointer **/ - current = NULL; - ECHOIT = 0; - while ((num_read = read(fd, in_buff, MAXLINE))) { - do_reading(); - } - close(fd); - break; - } - return; - -} diff --git a/src/lib/fnct_key.cc b/src/lib/fnct_key.cc new file mode 100644 index 00000000..af0d5c28 --- /dev/null +++ b/src/lib/fnct_key.cc @@ -0,0 +1,344 @@ +/* + Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. + All rights reserved. + Copyright (C) 2007-2013 Gabriel Dos Reis. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + - Neither the name of The Numerical ALgorithms Group Ltd. nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> + +#include "cfuns.h" +#include "edible.h" +#include "bsdsignal.h" + + +#include "fnct_key.h" +#include "prt.h" +#include "edin.h" + +/* FIXME: Remove this in complete rewrite */ +using namespace OpenAxiom; + +/** Some constants for functio key defs ****/ +#define DELAYED 0 +#define IMMEDIATE 1 +#define SPECIAL 2 + + +/** Here is the structure for storing bound pf-keys ***/ +fkey function_key[13]; /** Strings which replace function + keys when a key is hit ***/ + +static const char *defaulteditor = "clefedit"; +char editorfilename[100]; + + + +/* + * The following function environment variable clef editor. The command + * should be the one that the user wishes to have execed + */ + +void +set_editor_key(void) +{ + int pid; + + sprintf(editorfilename, "/tmp/clef%d", pid = oa_getpid()); + + if (function_key[12].str == NULL) { + (function_key[12]).type = SPECIAL; + (function_key[12]).str = defaulteditor; + } +} + + + + +/*** This routine id used to find the users function key mappings. It + simply searches the users HOME directory for a file called ".clef". + If found it gets the key bindings from within + *****/ + +void +define_function_keys(void) +{ + char *HOME, path[1024], string[1024]; + int key; + int fd; + char type; + + /** lets initialize the key pointers **/ + for (key = 0; key < 13; key++) + (function_key[key]).str = NULL; + /** see if the user has a .clef file ***/ + HOME = oa_getenv("HOME"); + sprintf(path, "%s/.clef", HOME); + if ((fd = open(path, O_RDONLY)) == -1) { + return; + } + else { + /*** If so, then get the key bindings **/ + while ((key = get_key(fd, &type))) { + get_str(fd, string); + switch (type) { + case 'D': + if (key == 12) { + fprintf(stderr, + "Clef Error: PF12 can only be of type E in .clef\n"); + fprintf(stderr, "Line will be ignored\n"); + type = -1; + } + else { + (function_key[key]).type = DELAYED; + } + break; + case 'F': + if (key == 12) { + fprintf(stderr, + "Clef Error: PF12 can only be of type E in .clef\n"); + fprintf(stderr, "Line will be ignored\n"); + type = -1; + } + else { + (function_key[key]).type = IMMEDIATE; + } + break; + case 'E': + if (key != 12) { + fprintf(stderr, + "Clef Error: PF12 can only be of type E in .clef\n"); + fprintf(stderr, "Line will be ignored\n"); + type = -1; + } + else { + (function_key[key]).type = SPECIAL; + } + break; + } + if (type != -1) + (function_key[key]).str = strdup(string); + } + } + + /* + * Now set the editor function key + */ + set_editor_key(); +} + + +#define defof(c) ((c == 'F' || c == 'D' || c == 'E')?(1):(0)) + +int +get_key(int fd,char * ty) +{ + + /* + * Determines the key number being mapped, and whether it is immediate or + * delay. It reurns the key value, and modifies the parameter type + */ + char keynum[1024]; + int nr; + + nr = read(fd, keynum, 3); + if (nr != -1 && nr != 0) { + if (!defof(keynum[0])) { + return 0; + } + else { + *ty = keynum[0]; + keynum[3] = '\0'; + return (atoi(&keynum[1])); + } + } + else + return 0; +} + +int +get_str(int fd,char * string) +{ + /** Gets the key mapping being bound **/ + char c; + int count = 0; + char *trace = string; + + read(fd, &c, 1); + while (c == ' ') + read(fd, &c, 1); + while (c != '\n') { + count++; + *trace++ = c; + if (read(fd, &c, 1) == 0) + break; + } + *trace = '\0'; + return count; +} + +void +null_fnct(int sig) +{ + return; +} + +void +handle_function_key(int key,int chann) +{ + /** this procedure simply adds the string specified by the function key + to the buffer ****/ + int count, fd; + int amount = strlen(function_key[key].str); + int id; + + /*** This procedure takes the character at in_buff[num_proc] and adds + it to the buffer. It first checks to see if we should be inserting + or overwriting, and then does the appropriate thing *******/ + + switch ((function_key[key]).type) { + case IMMEDIATE: + if (INS_MODE) { + forwardcopy(&buff[curr_pntr + amount], + &buff[curr_pntr], + buff_pntr - curr_pntr); + forwardflag_cpy(&buff_flag[curr_pntr + amount], + &buff_flag[curr_pntr], + buff_pntr - curr_pntr); + for (count = 0; count < amount; count++) { + buff[curr_pntr + count] = (function_key[key].str)[count]; + buff_flag[curr_pntr + count] = '1'; + } + ins_print(curr_pntr, amount + 1); + buff_pntr = buff_pntr + amount; + } + else { + for (count = 0; count < amount; count++) { + buff[curr_pntr + count] = (function_key[key].str)[count]; + buff_flag[curr_pntr + count] = '1'; + myputchar((function_key[key].str)[count]); + } + } + num_proc = num_proc + 6; + curr_pntr = curr_pntr + amount; + buff_pntr = buff_pntr + amount; + send_function_to_child(); + break; + case DELAYED: + if (INS_MODE) { + forwardcopy(&buff[curr_pntr + amount], + &buff[curr_pntr], + buff_pntr - curr_pntr); + forwardflag_cpy(&buff_flag[curr_pntr + amount], + &buff_flag[curr_pntr], + buff_pntr - curr_pntr); + for (count = 0; count < amount; count++) { + buff[curr_pntr + count] = (function_key[key].str)[count]; + buff_flag[curr_pntr + count] = '1'; + } + ins_print(curr_pntr, amount + 1); + buff_pntr = buff_pntr + amount; + } + else { + for (count = 0; count < amount; count++) { + buff[curr_pntr + count] = (function_key[key].str)[count]; + buff_flag[curr_pntr + count] = '1'; + myputchar((function_key[key].str)[count]); + } + } + num_proc = num_proc + 6; + curr_pntr = curr_pntr + amount; + buff_pntr = buff_pntr + amount; + fflush(stdout); + break; + case SPECIAL: + /* fprintf(stderr, "Here I am \n"); */ + if (access(editorfilename, F_OK) < 0) { + fd = open(editorfilename, O_RDWR | O_CREAT, 0666); + write(fd, buff, buff_pntr); + back_up(buff_pntr); + close(fd); + } + else { + if (buff_pntr > 0) { + fd = open(editorfilename, O_RDWR | O_TRUNC); + write(fd, buff, buff_pntr); + back_up(buff_pntr); + close(fd); + } + } + bsdSignal(OPENAXIOM_SIGCHLD, null_fnct,RestartSystemCalls); + switch (id = fork()) { + case -1: + perror("Special key"); + break; + case 0: + execlp((function_key[12]).str, + (function_key[12]).str, + editorfilename, (char*) NULL); + perror("Returned from exec"); + exit(0); + + } + while (wait((int *) 0) < 0); + /** now I should read that file and send all it stuff thru the + reader *****/ + fd = open(editorfilename, O_RDWR); + if (fd == -1) { + perror("Opening temp file"); + exit(-1); + } + num_proc += 6; + + /** reinitialize the buffer ***/ + init_flag(buff_flag, buff_pntr); + init_buff(buff, buff_pntr); + /** reinitialize my buffer pointers **/ + buff_pntr = curr_pntr = 0; + /** reset the ring pointer **/ + current = NULL; + ECHOIT = 0; + while ((num_read = read(fd, in_buff, MAXLINE))) { + do_reading(); + } + close(fd); + break; + } + return; + +} diff --git a/src/lib/halloc.c b/src/lib/halloc.c index 4b19c60a..fb43d6b3 100644 --- a/src/lib/halloc.c +++ b/src/lib/halloc.c @@ -31,7 +31,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "openaxiom-c-macros.h" /* memory allocation used by HyperDoc and addfile */ diff --git a/src/lib/hash.c b/src/lib/hash.c index 837eb8b1..7f727ecf 100644 --- a/src/lib/hash.c +++ b/src/lib/hash.c @@ -32,7 +32,6 @@ */ -#include "openaxiom-c-macros.h" #define _HASH_C #include "debug.h" diff --git a/src/lib/openpty.c b/src/lib/openpty.c deleted file mode 100644 index 5918aa7a..00000000 --- a/src/lib/openpty.c +++ /dev/null @@ -1,168 +0,0 @@ -/* - Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. - All rights reserved. - Copyright (C) 2007-2013, Gabriel Dos Reis. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - - - Neither the name of The Numerical ALgorithms Group Ltd. nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER - OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#include "openaxiom-c-macros.h" -#include <stdlib.h> -#include <unistd.h> -#include <stdio.h> -#include <fcntl.h> -#include <string.h> - -#ifdef HAVE_SYS_IOCTL_H -# include <sys/ioctl.h> -#endif -#ifdef HAVE_PTY_H -# include <pty.h> -#endif -#ifdef HAVE_UTIL_H -# include <util.h> -#endif -#ifdef HAVE_TERMIOS_H -# include <termios.h> -#endif -#ifdef HAVE_LIBUTIL_H -# include <sys/types.h> -# include <libutil.h> -#endif - - -#include "openpty.h" - -#if defined(SUNplatform) -static void -makeNextPtyNames(char *cont,char * serv) -{ - static int channelNo = 0; - static char group[] = "pqrstuvwxyzPQRST"; - static int groupNo = 0; - - sprintf(cont, "/dev/pty%c%x", group[groupNo], channelNo); - sprintf(serv, "/dev/tty%c%x", group[groupNo], channelNo); - channelNo++; /* try next */ - if (channelNo == 16) { /* move to new group */ - channelNo = 0; - groupNo++; - if (groupNo == 16) groupNo = 0; /* recycle */ - } -} -#endif - - -/* - * The main function is ptyopen. It simply opens up both sides of a - * pseudo-terminal. It uses and saves the pathnames for - * the devices which were actually opened. - * - * If it fails it simply exits the program. - * - * - * ptyopen(controller, server, controllerPath, serverPath) - * int *controller; The file descriptor for controller side of the pty - * int *server; The file descriptor for the server side - * char *serverPath; - * - * The path name vars should be declared of size 11 or more - */ - - -int -ptyopen(int *controller,int * server,char * serverPath) -{ -#if HAVE_DECL_OPENPTY - return openpty(controller,server, serverPath, NULL, NULL); -#elif defined(SUNplatform) - int looking = 1, i; - int oflag = O_RDWR; /* flag for opening the pty */ - char controllerPath[128]; - - for (i = 0; looking && i < 1000; i++) { - makeNextPtyNames(controllerPath, serverPath); - if (access(controllerPath, 6) != 0) continue; - *controller = open(controllerPath, oflag, 0); - if (*controller >= 0) { - *server = open(serverPath, oflag, 0); - if (*server > 0) - looking = 0; - else - close(*controller); - } - } - if (looking) { - fprintf(stderr, "Couldn't find a free pty.\n"); - exit(-1); - } - return (*controller); -#elif defined(SUN4OS5platform) -extern int grantpt(int); -extern int unlockpt(int); -extern char* ptsname(int); - int fdm,fds; - char *slavename; - - /* open master */ - if ((fdm = open("/dev/ptmx", O_RDWR)) < 0 ) - perror("ptyopen: Failed to open /dev/ptmx"); - else { - /* change permission ofslave */ - if (grantpt(fdm) < 0) - perror("ptyopen: Failed to grant access to slave device"); - /* unlock slave */ - if (unlockpt(fdm) < 0) - perror("ptyopen: Failed to unlock master/slave pair"); - /* get name of slave */ - if ((slavename = ptsname(fdm)) == NULL) - perror("ptyopen: Failed to get name of slave device"); - /* open slave */ - if ((fds = open(slavename, O_RDWR)) < 0 ) - perror("ptyopen: Failed to open slave"); - else { - /* push ptem */ - if (ioctl(fds, I_PUSH, "ptem") < 0) - perror("ptyopen: Failed to push ptem"); - /* push ldterm */ - if (ioctl(fds, I_PUSH, "ldterm") < 0) - perror("ptyopen: Failed to push idterm"); - strcpy(serverPath,slavename); - *controller=fdm; - *server=fds; - } - } - return(fdm); -#else -# error "don't know how to open a pty" -#endif -} - - diff --git a/src/lib/openpty.cc b/src/lib/openpty.cc new file mode 100644 index 00000000..4262832e --- /dev/null +++ b/src/lib/openpty.cc @@ -0,0 +1,171 @@ +/* + Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. + All rights reserved. + Copyright (C) 2007-2013, Gabriel Dos Reis. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + - Neither the name of The Numerical ALgorithms Group Ltd. nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <stdlib.h> +#include <unistd.h> +#include <stdio.h> +#include <fcntl.h> +#include <string.h> + +#ifdef HAVE_SYS_IOCTL_H +# include <sys/ioctl.h> +#endif +#ifdef HAVE_PTY_H +# include <pty.h> +#endif +#ifdef HAVE_UTIL_H +# include <util.h> +#endif +#ifdef HAVE_TERMIOS_H +# include <termios.h> +#endif +#ifdef HAVE_LIBUTIL_H +# include <sys/types.h> +# include <libutil.h> +#endif + + +#include "openpty.h" + +#if defined(SUNplatform) +static void +makeNextPtyNames(char *cont,char * serv) +{ + static int channelNo = 0; + static char group[] = "pqrstuvwxyzPQRST"; + static int groupNo = 0; + + sprintf(cont, "/dev/pty%c%x", group[groupNo], channelNo); + sprintf(serv, "/dev/tty%c%x", group[groupNo], channelNo); + channelNo++; /* try next */ + if (channelNo == 16) { /* move to new group */ + channelNo = 0; + groupNo++; + if (groupNo == 16) groupNo = 0; /* recycle */ + } +} +#endif + + +/* + * The main function is ptyopen. It simply opens up both sides of a + * pseudo-terminal. It uses and saves the pathnames for + * the devices which were actually opened. + * + * If it fails it simply exits the program. + * + * + * ptyopen(controller, server, controllerPath, serverPath) + * int *controller; The file descriptor for controller side of the pty + * int *server; The file descriptor for the server side + * char *serverPath; + * + * The path name vars should be declared of size 11 or more + */ + + +int +ptyopen(int *controller,int * server,char * serverPath) +{ +#if HAVE_DECL_OPENPTY + return openpty(controller,server, serverPath, NULL, NULL); +#elif defined(SUNplatform) + int looking = 1, i; + int oflag = O_RDWR; /* flag for opening the pty */ + char controllerPath[128]; + + for (i = 0; looking && i < 1000; i++) { + makeNextPtyNames(controllerPath, serverPath); + if (access(controllerPath, 6) != 0) continue; + *controller = open(controllerPath, oflag, 0); + if (*controller >= 0) { + *server = open(serverPath, oflag, 0); + if (*server > 0) + looking = 0; + else + close(*controller); + } + } + if (looking) { + fprintf(stderr, "Couldn't find a free pty.\n"); + exit(-1); + } + return (*controller); +#elif defined(SUN4OS5platform) +extern int grantpt(int); +extern int unlockpt(int); +extern char* ptsname(int); + int fdm,fds; + char *slavename; + + /* open master */ + if ((fdm = open("/dev/ptmx", O_RDWR)) < 0 ) + perror("ptyopen: Failed to open /dev/ptmx"); + else { + /* change permission ofslave */ + if (grantpt(fdm) < 0) + perror("ptyopen: Failed to grant access to slave device"); + /* unlock slave */ + if (unlockpt(fdm) < 0) + perror("ptyopen: Failed to unlock master/slave pair"); + /* get name of slave */ + if ((slavename = ptsname(fdm)) == NULL) + perror("ptyopen: Failed to get name of slave device"); + /* open slave */ + if ((fds = open(slavename, O_RDWR)) < 0 ) + perror("ptyopen: Failed to open slave"); + else { + /* push ptem */ + if (ioctl(fds, I_PUSH, "ptem") < 0) + perror("ptyopen: Failed to push ptem"); + /* push ldterm */ + if (ioctl(fds, I_PUSH, "ldterm") < 0) + perror("ptyopen: Failed to push idterm"); + strcpy(serverPath,slavename); + *controller=fdm; + *server=fds; + } + } + return(fdm); +#else +# error "don't know how to open a pty" +#endif +} + + diff --git a/src/lib/pixmap.c b/src/lib/pixmap.c index d0ad2097..da169dfb 100644 --- a/src/lib/pixmap.c +++ b/src/lib/pixmap.c @@ -33,7 +33,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "openaxiom-c-macros.h" #include <X11/Xlib.h> #include <X11/Xutil.h> diff --git a/src/lib/prt.c b/src/lib/prt.c deleted file mode 100644 index ee5b3af0..00000000 --- a/src/lib/prt.c +++ /dev/null @@ -1,407 +0,0 @@ -/* - Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - - - Neither the name of The Numerical ALgorithms Group Ltd. nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER - OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#include "openaxiom-c-macros.h" -#include <string.h> -#include <stdio.h> -#include <sys/types.h> -#include "edible.h" - -#include "prt.h" -#include "edin.h" - -void -myputchar(char c) -{ - if (ECHOIT) - putchar(c); - return; -} - -void -clear_buff(void) -{ - int count; - - /*** called when spadbuf gives me a line incase there is something already - on the line ****/ - if (buff_pntr > 0) { - /*** backup to the beginning of the line ***/ - for (count = curr_pntr; count > 0; count--) - myputchar(_BKSPC); - /** blank over the line ***/ - for (count = 0; count < buff_pntr; count++) { - myputchar(_BLANK); - } - /** back up again ***/ - for (count = buff_pntr; count > 0; count--) - myputchar(_BKSPC); - init_buff(buff, buff_pntr); - init_flag(buff_flag, buff_pntr); - curr_pntr = buff_pntr = 0; - } -} - - -void -move_end(void) -{ - - /** Moves cursor to the end of the line ***/ - if (curr_pntr == buff_pntr) { - putchar(_BELL); - } - else { - for (; curr_pntr < buff_pntr;) { - myputchar(buff[curr_pntr++]); - } - } - fflush(stdout); -} - -void -move_home(void) -{ - - /*** Moves the cursor to the front of the line ***/ - if (curr_pntr > 0) { - for (; curr_pntr > 0;) { - myputchar(_BKSPC); - curr_pntr--; - } - } - else { - putchar(_BELL); - } - fflush(stdout); - -} - -void -move_fore_word(void) -{ - /** move the cursor to the next blank space **/ - if (curr_pntr != buff_pntr) { - myputchar(buff[curr_pntr]); - curr_pntr++; - while (curr_pntr < buff_pntr && buff[curr_pntr] != ' ') { - myputchar(buff[curr_pntr]); - curr_pntr++; - } - } - else - putchar(_BELL); - fflush(stdout); - return; -} - -void -move_back_word(void) -{ - /*** moves the cursor to the last blank space ***/ - if (curr_pntr > 0) { - myputchar(_BKSPC); - curr_pntr--; - while (curr_pntr > 0 && buff[curr_pntr - 1] != ' ') { - myputchar(_BKSPC); - curr_pntr--; - } - - } - else - putchar(_BELL); - fflush(stdout); - return; -} - -void -delete_current_char(void) -{ - /** deletes the char currently above the current_pntr, if it can be **/ - if (curr_pntr != buff_pntr) { - if (buff_flag[curr_pntr] == 1 || buff_flag[curr_pntr] == 0) { - myputchar(_BLANK); - myputchar(_BKSPC); - strmov(&buff[curr_pntr], - &buff[curr_pntr + 1]); - flagcpy(&buff_flag[curr_pntr], - &buff_flag[curr_pntr + 1]); - buff_pntr--; - del_print(curr_pntr, 1); - } - else { - /** lets delete two of the little buggers **/ - myputchar(_BLANK); - myputchar(_BLANK); - myputchar(_BKSPC); - myputchar(_BKSPC); - strmov(&buff[curr_pntr], - &buff[curr_pntr + 2]); - flagcpy(&buff_flag[curr_pntr], - &buff_flag[curr_pntr + 2]); - buff_pntr -= 2; - del_print(curr_pntr, 2); - } - } - else { - putchar(_BELL); - fflush(stdout); - } - num_proc = num_proc + 3; -} - -void -delete_to_end_of_line(void) -{ - int count; - - /*** deletes from the curr_pntr to the end of line ***/ - - if (curr_pntr == buff_pntr) - return; /** There is nothing to do **/ - - /** blank over the end of the line ***/ - for (count = curr_pntr; count < buff_pntr; count++) { - myputchar(_BLANK); - } - /** back up again ***/ - for (count = buff_pntr; count > curr_pntr; count--) - myputchar(_BKSPC); - - buff_pntr = curr_pntr; - fflush(stdout); - return; - -} - -void -delete_line(void) -{ - int count; - - /*** deletes the entire line *****/ - - if (buff_pntr == 0) - return; /** There is nothing to do **/ - - /** first I have to back up to the beginning of the line ****/ - for (count = curr_pntr; count > 0; count--) - myputchar(_BKSPC); - - /** blank over the end of the line ***/ - for (count = 0; count < buff_pntr; count++) { - myputchar(_BLANK); - } - /** back up again ***/ - for (count = buff_pntr; count > 0; count--) - myputchar(_BKSPC); - - /* Also clear the buffer */ - init_buff(buff, buff_pntr); - init_flag(buff_flag, buff_pntr); - buff_pntr = curr_pntr = 0; - - fflush(stdout); - return; - -} - -void -printbuff(int start,int num) -{ - int trace; - - for (trace = start; trace < start + num; trace++) - if (buff[trace] != '\0') - myputchar(buff[trace]); - fflush(stdout); -} - -void -del_print(int start, int num) -{ - int count; - - /*** move the rest of the string ***/ - for (count = start; count < buff_pntr; count++) { - myputchar(buff[count]); - } - /** now blank out the number of chars we are supposed to ***/ - for (count = 0; count < num; count++) - myputchar(_BLANK); - /*** Now back up ***/ - for (count = buff_pntr + num; count > start; count--) - myputchar(_BKSPC); - fflush(stdout); -} - - -void -ins_print(int start,int num) -{ - int count; - - /** write the rest of the word ***/ - for (count = start; count < buff_pntr + num; count++) { - myputchar(buff[count]); - } - /** now back up to where we should be ***/ - for (count = buff_pntr; count > start; count--) - myputchar(_BKSPC); - fflush(stdout); -} - -void -reprint(int start) -{ - /** simply reprints a single character **/ - if (buff[start] == '\0') - myputchar(_BLANK); - else - myputchar(buff[start]); - myputchar(_BKSPC); - fflush(stdout); - return; -} - -void -back_up(int num_chars) -{ - int cnt; - - for (cnt = 0; cnt < num_chars; cnt++) - myputchar(_BKSPC); - for (cnt = 0; cnt < num_chars; cnt++) - myputchar(_BLANK); - for (cnt = 0; cnt < num_chars; cnt++) - myputchar(_BKSPC); - fflush(stdout); - -} - -void -back_it_up(int num_chars) -{ - int cnt; - - for (cnt = 0; cnt < num_chars; cnt++) - myputchar(_BKSPC); - fflush(stdout); -} - - -void -print_whole_buff(void) -{ - int trace; - - for (trace = 0; trace < buff_pntr; trace++) - if (buff[trace] != '\0') - myputchar(buff[trace]); - fflush(stdout); -} - -void -move_ahead(void) -{ - /*** simply moves the pointer ahead a single word ***/ - if (curr_pntr == buff_pntr) { - putchar(_BELL); - } - else { - if (buff_flag[curr_pntr] == 2) { - myputchar(buff[curr_pntr++]); - } - myputchar(buff[curr_pntr++]); - } - fflush(stdout); -} - -void -move_back(void) -{ - /** simply moves the cursor back one position **/ - if (curr_pntr == 0) { - putchar(_BELL); - } - else { - if (!buff_flag[curr_pntr - 1]) { - myputchar(_BKSPC); - curr_pntr--; - } - myputchar(_BKSPC); - curr_pntr--; - } - fflush(stdout); -} - -void -back_over_current_char(void) -{ - /*** simply backs over the character behind the cursor ***/ - if (curr_pntr == 0) { - putchar(_BELL); - } - else { - if (!buff_flag[curr_pntr - 1]) { - myputchar(_BKSPC); - myputchar(_BKSPC); - myputchar(_BLANK); - myputchar(_BLANK); - myputchar(_BKSPC); - myputchar(_BKSPC); - strmov(&buff[curr_pntr - 2], - &buff[curr_pntr]); - flagcpy(&buff_flag[curr_pntr - 2], - &buff_flag[curr_pntr]); - buff_pntr -= 2; - curr_pntr -= 2; - del_print(curr_pntr, 2); - } - else { - myputchar(_BKSPC); - myputchar(_BLANK); - myputchar(_BKSPC); - strmov(&buff[curr_pntr - 1], - &buff[curr_pntr]); - flagcpy(&buff_flag[curr_pntr - 1], - &buff_flag[curr_pntr]); - curr_pntr--; - buff_pntr--; - del_print(curr_pntr, 1); - } - } - fflush(stdout); - return; -} - diff --git a/src/lib/prt.cc b/src/lib/prt.cc new file mode 100644 index 00000000..831a199e --- /dev/null +++ b/src/lib/prt.cc @@ -0,0 +1,406 @@ +/* + Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + - Neither the name of The Numerical ALgorithms Group Ltd. nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <string.h> +#include <stdio.h> +#include <sys/types.h> +#include "edible.h" + +#include "prt.h" +#include "edin.h" + +void +myputchar(char c) +{ + if (ECHOIT) + putchar(c); + return; +} + +void +clear_buff(void) +{ + int count; + + /*** called when spadbuf gives me a line incase there is something already + on the line ****/ + if (buff_pntr > 0) { + /*** backup to the beginning of the line ***/ + for (count = curr_pntr; count > 0; count--) + myputchar(_BKSPC); + /** blank over the line ***/ + for (count = 0; count < buff_pntr; count++) { + myputchar(_BLANK); + } + /** back up again ***/ + for (count = buff_pntr; count > 0; count--) + myputchar(_BKSPC); + init_buff(buff, buff_pntr); + init_flag(buff_flag, buff_pntr); + curr_pntr = buff_pntr = 0; + } +} + + +void +move_end(void) +{ + + /** Moves cursor to the end of the line ***/ + if (curr_pntr == buff_pntr) { + putchar(_BELL); + } + else { + for (; curr_pntr < buff_pntr;) { + myputchar(buff[curr_pntr++]); + } + } + fflush(stdout); +} + +void +move_home(void) +{ + + /*** Moves the cursor to the front of the line ***/ + if (curr_pntr > 0) { + for (; curr_pntr > 0;) { + myputchar(_BKSPC); + curr_pntr--; + } + } + else { + putchar(_BELL); + } + fflush(stdout); + +} + +void +move_fore_word(void) +{ + /** move the cursor to the next blank space **/ + if (curr_pntr != buff_pntr) { + myputchar(buff[curr_pntr]); + curr_pntr++; + while (curr_pntr < buff_pntr && buff[curr_pntr] != ' ') { + myputchar(buff[curr_pntr]); + curr_pntr++; + } + } + else + putchar(_BELL); + fflush(stdout); + return; +} + +void +move_back_word(void) +{ + /*** moves the cursor to the last blank space ***/ + if (curr_pntr > 0) { + myputchar(_BKSPC); + curr_pntr--; + while (curr_pntr > 0 && buff[curr_pntr - 1] != ' ') { + myputchar(_BKSPC); + curr_pntr--; + } + + } + else + putchar(_BELL); + fflush(stdout); + return; +} + +void +delete_current_char(void) +{ + /** deletes the char currently above the current_pntr, if it can be **/ + if (curr_pntr != buff_pntr) { + if (buff_flag[curr_pntr] == 1 || buff_flag[curr_pntr] == 0) { + myputchar(_BLANK); + myputchar(_BKSPC); + strmov(&buff[curr_pntr], + &buff[curr_pntr + 1]); + flagcpy(&buff_flag[curr_pntr], + &buff_flag[curr_pntr + 1]); + buff_pntr--; + del_print(curr_pntr, 1); + } + else { + /** lets delete two of the little buggers **/ + myputchar(_BLANK); + myputchar(_BLANK); + myputchar(_BKSPC); + myputchar(_BKSPC); + strmov(&buff[curr_pntr], + &buff[curr_pntr + 2]); + flagcpy(&buff_flag[curr_pntr], + &buff_flag[curr_pntr + 2]); + buff_pntr -= 2; + del_print(curr_pntr, 2); + } + } + else { + putchar(_BELL); + fflush(stdout); + } + num_proc = num_proc + 3; +} + +void +delete_to_end_of_line(void) +{ + int count; + + /*** deletes from the curr_pntr to the end of line ***/ + + if (curr_pntr == buff_pntr) + return; /** There is nothing to do **/ + + /** blank over the end of the line ***/ + for (count = curr_pntr; count < buff_pntr; count++) { + myputchar(_BLANK); + } + /** back up again ***/ + for (count = buff_pntr; count > curr_pntr; count--) + myputchar(_BKSPC); + + buff_pntr = curr_pntr; + fflush(stdout); + return; + +} + +void +delete_line(void) +{ + int count; + + /*** deletes the entire line *****/ + + if (buff_pntr == 0) + return; /** There is nothing to do **/ + + /** first I have to back up to the beginning of the line ****/ + for (count = curr_pntr; count > 0; count--) + myputchar(_BKSPC); + + /** blank over the end of the line ***/ + for (count = 0; count < buff_pntr; count++) { + myputchar(_BLANK); + } + /** back up again ***/ + for (count = buff_pntr; count > 0; count--) + myputchar(_BKSPC); + + /* Also clear the buffer */ + init_buff(buff, buff_pntr); + init_flag(buff_flag, buff_pntr); + buff_pntr = curr_pntr = 0; + + fflush(stdout); + return; + +} + +void +printbuff(int start,int num) +{ + int trace; + + for (trace = start; trace < start + num; trace++) + if (buff[trace] != '\0') + myputchar(buff[trace]); + fflush(stdout); +} + +void +del_print(int start, int num) +{ + int count; + + /*** move the rest of the string ***/ + for (count = start; count < buff_pntr; count++) { + myputchar(buff[count]); + } + /** now blank out the number of chars we are supposed to ***/ + for (count = 0; count < num; count++) + myputchar(_BLANK); + /*** Now back up ***/ + for (count = buff_pntr + num; count > start; count--) + myputchar(_BKSPC); + fflush(stdout); +} + + +void +ins_print(int start,int num) +{ + int count; + + /** write the rest of the word ***/ + for (count = start; count < buff_pntr + num; count++) { + myputchar(buff[count]); + } + /** now back up to where we should be ***/ + for (count = buff_pntr; count > start; count--) + myputchar(_BKSPC); + fflush(stdout); +} + +void +reprint(int start) +{ + /** simply reprints a single character **/ + if (buff[start] == '\0') + myputchar(_BLANK); + else + myputchar(buff[start]); + myputchar(_BKSPC); + fflush(stdout); + return; +} + +void +back_up(int num_chars) +{ + int cnt; + + for (cnt = 0; cnt < num_chars; cnt++) + myputchar(_BKSPC); + for (cnt = 0; cnt < num_chars; cnt++) + myputchar(_BLANK); + for (cnt = 0; cnt < num_chars; cnt++) + myputchar(_BKSPC); + fflush(stdout); + +} + +void +back_it_up(int num_chars) +{ + int cnt; + + for (cnt = 0; cnt < num_chars; cnt++) + myputchar(_BKSPC); + fflush(stdout); +} + + +void +print_whole_buff(void) +{ + int trace; + + for (trace = 0; trace < buff_pntr; trace++) + if (buff[trace] != '\0') + myputchar(buff[trace]); + fflush(stdout); +} + +void +move_ahead(void) +{ + /*** simply moves the pointer ahead a single word ***/ + if (curr_pntr == buff_pntr) { + putchar(_BELL); + } + else { + if (buff_flag[curr_pntr] == 2) { + myputchar(buff[curr_pntr++]); + } + myputchar(buff[curr_pntr++]); + } + fflush(stdout); +} + +void +move_back(void) +{ + /** simply moves the cursor back one position **/ + if (curr_pntr == 0) { + putchar(_BELL); + } + else { + if (!buff_flag[curr_pntr - 1]) { + myputchar(_BKSPC); + curr_pntr--; + } + myputchar(_BKSPC); + curr_pntr--; + } + fflush(stdout); +} + +void +back_over_current_char(void) +{ + /*** simply backs over the character behind the cursor ***/ + if (curr_pntr == 0) { + putchar(_BELL); + } + else { + if (!buff_flag[curr_pntr - 1]) { + myputchar(_BKSPC); + myputchar(_BKSPC); + myputchar(_BLANK); + myputchar(_BLANK); + myputchar(_BKSPC); + myputchar(_BKSPC); + strmov(&buff[curr_pntr - 2], + &buff[curr_pntr]); + flagcpy(&buff_flag[curr_pntr - 2], + &buff_flag[curr_pntr]); + buff_pntr -= 2; + curr_pntr -= 2; + del_print(curr_pntr, 2); + } + else { + myputchar(_BKSPC); + myputchar(_BLANK); + myputchar(_BKSPC); + strmov(&buff[curr_pntr - 1], + &buff[curr_pntr]); + flagcpy(&buff_flag[curr_pntr - 1], + &buff_flag[curr_pntr]); + curr_pntr--; + buff_pntr--; + del_print(curr_pntr, 1); + } + } + fflush(stdout); + return; +} + diff --git a/src/lib/sockio-c.cxx b/src/lib/sockio-c.cxx index dda95ec3..c17c3398 100644 --- a/src/lib/sockio-c.cxx +++ b/src/lib/sockio-c.cxx @@ -70,9 +70,9 @@ namespace OpenAxiom { /* Note that the name AF_LOCAL is more portable than AF_UNIX, but MingW implementation and Windows documentation don't always agree. */ -#if HAVE_AF_LOCAL +#if HAVE_DECL_AF_LOCAL # define OPENAXIOM_AF_LOCAL AF_LOCAL -#elif HAVE_AF_UNIX +#elif HAVE_DECL_AF_UNIX # define OPENAXIOM_AF_LOCAL AF_UNIX #else # error "needs one of AF_LOCAL or AF_UNIX" diff --git a/src/lib/spadcolors.c b/src/lib/spadcolors.c index b8d784f2..8007b450 100644 --- a/src/lib/spadcolors.c +++ b/src/lib/spadcolors.c @@ -33,7 +33,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "openaxiom-c-macros.h" #include "spadcolors.h" #include <X11/Xlib.h> diff --git a/src/lib/util.c b/src/lib/util.c index 198cd3bf..264590ea 100644 --- a/src/lib/util.c +++ b/src/lib/util.c @@ -33,7 +33,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "openaxiom-c-macros.h" #include <unistd.h> #include <sys/types.h> #include <stdio.h> diff --git a/src/lib/wct.c b/src/lib/wct.c deleted file mode 100644 index 44510558..00000000 --- a/src/lib/wct.c +++ /dev/null @@ -1,732 +0,0 @@ -/* - Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. - All rights reserved. - Copyright (C) 2007-2011 Gabriel Dos Reis. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - - - Neither the name of The Numerical ALgorithms Group Ltd. nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER - OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -/* - * Word completion. - * - * - * Word completion is driven from a list of completion tables. Each table - * contains a list of words. - * - */ - -#include "openaxiom-c-macros.h" - -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <fcntl.h> -#include <time.h> -#include <ctype.h> -#include <sys/types.h> -#include <sys/stat.h> - -/* #define PINFO *//* A flag to suppress printing of the file info */ - -#define WCT /* A flag needed because ctype.h stole some - * of my constants */ -#include "edible.h" - -#define MAX_PREFIX 1024 -#define strneql(a,b,n) (*(a) == *(b) && !strncmp((a),(b),(n))) -#define Delimiter(c) (! isalnum(c) && c != '%' && c != '!' && c != '?' && c != '_') - - -#include "wct.h" -#include "prt.h" -#include "edin.h" - - - -static Wct *pwct = 0; -static Wix *pwix; -static Wix curr_wix; -static char curr_prefix[MAX_PREFIX]; -static Wct *pHeadwct; - -time_t -ftime(char *path) -{ - int rc; - struct stat stbuf; - - rc = stat(path, &stbuf); - if (rc == -1) - fatal("Cannot deterimine status of %s.", path); - - return stbuf.st_mtime; -} - -off_t -fsize(char *path) -{ - int rc; - struct stat stbuf; - - rc = stat(path, &stbuf); - if (rc == -1) - fatal("Cannot deterimine status of %s.", path); - - return stbuf.st_size; -} - - -/* - * Scan a word completion table for a particular word. - */ - - -Wix * -scanWct(Wct *pwct, char *prefix) -{ - long fmod; - int preflen, i, wc; - char **wv; - - preflen = strlen(prefix); - strncpy(curr_prefix, prefix, MAX_PREFIX); - - pHeadwct = pwct; - curr_wix.pwct = 0; - curr_wix.word = 0; - - for (; pwct; pwct = pwct->next) { - curr_wix.pwct = pwct; - - - - fmod = ftime(pwct->fname); - if (fmod > pwct->ftime) - reintern1Wct(pwct); - - wv = pwct->wordv; - wc = pwct->wordc; - for (i = 0; i < wc; i++) { - curr_wix.word = i; - if (strneql(wv[i], prefix, preflen)) - return &curr_wix; - } - } - return 0; -} - -Wix * -rescanWct(void) -{ - Wct *pwct, *start_pwct; - int preflen, start_word, i, wc; - char **wv, *prefix; - - start_pwct = curr_wix.pwct; - start_word = curr_wix.word; - - if (!start_pwct) return(0); - prefix = curr_prefix; - preflen = strlen(prefix); - - /* - * Finish the current structure. - */ - - pwct = start_pwct; - - curr_wix.pwct = pwct; - wv = pwct->wordv; - wc = pwct->wordc; - for (i = start_word + 1; i < wc; i++) { - curr_wix.word = i; - if (strneql(wv[i], prefix, preflen)) - return &curr_wix; - } - - /* - * Finish to the end of the list, doing whole structures. - */ - for (pwct = pwct->next; pwct; pwct = pwct->next) { - curr_wix.pwct = pwct; - - wv = pwct->wordv; - wc = pwct->wordc; - for (i = 0; i < wc; i++) { - curr_wix.word = i; - if (strneql(wv[i], prefix, preflen)) - return &curr_wix; - } - } - - /* - * Restart at beginning, doing whole structures. - */ - for (pwct = pHeadwct; pwct != start_pwct; pwct = pwct->next) { - curr_wix.pwct = pwct; - - wv = pwct->wordv; - wc = pwct->wordc; - for (i = 0; i < wc; i++) { - curr_wix.word = i; - if (strneql(wv[i], prefix, preflen)) - return &curr_wix; - } - } - - /* - * Do beginning half of the start structure. - */ - curr_wix.pwct = pwct; - wv = pwct->wordv; - wc = pwct->wordc; - for (i = 0; i <= start_word; i++) { - curr_wix.word = i; - if (strneql(wv[i], prefix, preflen)) - return &curr_wix; - } - - /* Not found? */ - return 0; -} - -/* - * Summarize a table. - */ -void -skimWct(Wct *pwct) -{ - while (pwct) { -#ifdef PINFO - skim1Wct(pwct); -#endif - pwct = pwct->next; - } -} - -void -skim1Wct(Wct *pwct) -{ -#define NHEAD 13 -#define NTAIL 7 - - int cc; - - printf("%-10s", pwct->fname); - printTime((long *)&(pwct->ftime)); - cc = skimString(pwct->fimage, pwct->fsize, NHEAD, NTAIL); - printf("%s", " " + (cc - (NHEAD + NTAIL))); - printf(" [%d w, %ld c]", pwct->wordc, (long)(pwct->fsize)); - printf("\n"); - -#ifdef SHOW_WORDS - { - int i; - char **wv; - - for (i = 1, wv = pwct->wordv; *wv; i++, wv++) { - printf("%5d: %s\n", i, *wv); - } - } -#endif -} - -void -printTime(long *clock) -{ - struct tm *tm; - - tm = localtime((time_t *)clock); - printf("%.2d/%.2d/%.2d %.2d:%.2d:%.2d ", - tm->tm_year, tm->tm_mon + 1, tm->tm_mday, - tm->tm_hour, tm->tm_min, tm->tm_sec); -} - -int -skimString(char *s, int slen,int nhead,int ntail) -{ - int spos, tlen, i, cc; - - cc = printf("\""); - for (spos = 0; spos < slen && cc <= nhead; spos++) - cc += prChar(s[spos]); - - /* Assume same tail has the same multi-character format ratio. */ - tlen = ntail * ((1.0 * spos) / nhead); - - if (spos + tlen >= slen) - for (; spos < slen; spos++) - cc += prChar(s[spos]); - else { - cc += printf("\"...\""); - for (i = slen - tlen; i < slen; i++) - cc += prChar(s[i]); - } - cc += printf("\""); - return cc; -} - -int -prChar(int c) -{ - if (c == '\n') - return printf("\\n"); - if (c == '\t') - return printf("\\t"); - if (c == '\b') - return printf("\\b"); - if (c == '"') - return printf("\\\""); - if (iscntrl(c)) - return printf("^%c", (c + 0100) % 0200); - if (isascii(c)) - return printf("%c", c); - - return printf("\\%d", c); -} - -Wct * -reread1Wct(Wct *pwct) -{ - int fd, rc; - - /* Check information about the file. */ - pwct->fsize = fsize(pwct->fname); - pwct->ftime = ftime(pwct->fname); - - /* Allocate space for file image */ - if (pwct->fimage) - free(pwct->fimage); - pwct->fimage = (char *) malloc(pwct->fsize + 1); - if (pwct->fimage == 0) - sfatal("Cannot allocate new table."); - pwct->fimage[pwct->fsize] = 0; - - /* Read file into buffer. */ - fd = open(pwct->fname, O_RDONLY); - if (fd == -1) - fatal("Cannot read file %s.", pwct->fname); - rc = read(fd, pwct->fimage, pwct->fsize); - if (rc != pwct->fsize) - fatal("Did not read all of file %s.", pwct->fname); - - return pwct; -} - -Wct * -read1Wct(char *fname) -{ - Wct *pwct; - - /* Allocate a new link for this file. */ - pwct = (Wct *) malloc(sizeof(Wct)); - if (pwct == 0) - sfatal("Cannot allocate new table."); - - pwct->fname = fname; - pwct->wordc = 0; - pwct->wordv = 0; - pwct->fimage = 0; - pwct->next = 0; - - return reread1Wct(pwct); -} - -Wct * -nconcWct(Wct *pwct,Wct * qwct) -{ - Wct *p0 = pwct; - - if (!p0) - return qwct; - - while (pwct->next) - pwct = pwct->next; - pwct->next = qwct; - - return p0; -} - -void -sortWct(Wct *pwct) -{ - while (pwct) { - sort1Wct(pwct); - pwct = pwct->next; - } -} - -void -sort1Wct(Wct *pwct) -{ - qsort((char *) pwct->wordv, pwct->wordc, - sizeof(*(pwct->wordv)), mystrcmp); -} - -int -mystrcmp(const void *s1,const void * s2) -{ - return strcmp(*(char **)s1, *(char **)s2); -} - -/* - * Break wct struct into words. - */ - -void -burstWct(Wct *pwct) -{ - while (pwct) { - burst1Wct(pwct); - pwct = pwct->next; - } -} - -void -burst1Wct(Wct *pwct) -{ - char *s, **wv; - int i, j, inword = 0; - - - for (s = pwct->fimage, i = 0; i < pwct->fsize; s++, i++) { - if (isspace(*s) || iscntrl(*s)) { - *s = 0; - inword = 0; - } - else if (!inword) { - inword = 1; - pwct->wordc++; - } - } - - if (pwct->wordv) - free(pwct->wordv); - pwct->wordv = (char **) calloc(pwct->wordc + 1, sizeof(char *)); - if (!pwct->wordv) - fatal("Could not make word list for %s.", pwct->fname); - - s = pwct->fimage; - i = 0; - for (wv = pwct->wordv, j = 0; j < pwct->wordc; wv++, j++) { - while (i < pwct->fsize && !s[i]) - i++; - *wv = s + i; - while (i < pwct->fsize && s[i]) - i++; - } - *wv = 0; -} - -Wct * -intern1Wct(char *fname) -{ - Wct *pwct; - - pwct = read1Wct(fname); - burst1Wct(pwct); - return pwct; -} - -void -reintern1Wct(Wct *pwct) -{ - reread1Wct(pwct); - burst1Wct(pwct); -} - -void -sfatal(const char *s) -{ - fatal("%s", s); -} - -void -fatal(const char* fmt, const char* s) -{ - static char fbuf[256]; - - sprintf(fbuf, fmt, s); - - perror(fbuf); - exit(1); -} - - - -/* load up the wct database */ -void -load_wct_file(char *fname) -{ - pwct = nconcWct(intern1Wct(fname), pwct); -} - -void -skim_wct(void) -{ - skimWct(pwct); -} - -/* - * This routine is called when a tab is hit. It simply takes the current - * buffer and tries to find a completion of the last word on the line in the - * data base. - */ - - -void -rescan_wct(void) -{ - int b = curr_pntr - 1; - int old_len; - int new_len; - int diff; - int i; - int ncs = 0; - - /* - * first thing I should do is find my way back to the beginning of the - * word - */ - while (b && !Delimiter(buff[b])) - b--; - if (Delimiter(buff[b])) - b++; - - old_len = curr_pntr - b; - - pwix = rescanWct(); - - if (!pwix) { - putchar(_BELL); - fflush(stdout); - } - else { - Wct *pwct = pwix->pwct; /* start replacing it */ - - new_len = strlen(pwct->wordv[pwix->word]); - if (new_len > old_len) { - - /* - * I have to just slide the characters forward a bit, stuff in - * the new characters, and then adjust curr_pntr - */ - diff = new_len - old_len; - if (curr_pntr != buff_pntr) { - forwardcopy(&buff[curr_pntr + diff], - &buff[curr_pntr], - buff_pntr - curr_pntr); - forwardflag_cpy(&buff_flag[curr_pntr + diff], - &buff_flag[curr_pntr], - buff_pntr - curr_pntr); - } - buff_pntr += diff; - ncs = curr_pntr + diff; - - /* Now insert the new word */ - for (i = 0; i < new_len; i++) - buff[b + i] = (pwct->wordv[pwix->word])[i]; - - /* move cursor to the beginning of the word */ - for (; curr_pntr != b; curr_pntr--) - putchar(_BKSPC); - - /** now print the characters on the rest of the line **/ - printbuff(curr_pntr, buff_pntr - curr_pntr); - - /* now move bcak the number of characters I want to */ - for (i = buff_pntr; i != ncs; i--) - putchar(_BKSPC); - - fflush(stdout); - - curr_pntr = ncs; - } - else if (new_len < old_len) { - /* this time I simply copy backwards and do the substituting */ - diff = old_len - new_len; - strnmov(&buff[curr_pntr - diff], - &buff[curr_pntr], - buff_pntr - curr_pntr); - flagncpy(&buff_flag[curr_pntr - diff], - &buff_flag[curr_pntr], - buff_pntr - curr_pntr); - buff_pntr -= diff; - ncs = curr_pntr - diff; - - /* Now insert the new word */ - for (i = 0; i < new_len; i++) - buff[b + i] = (pwct->wordv[pwix->word])[i]; - - /* move cursor to the beginning of the word */ - for (; curr_pntr != b; curr_pntr--) - putchar(_BKSPC); - - /** now print the characters on the rest of the line **/ - printbuff(b, buff_pntr - b); - - /* now blank out the characters out on the end of this line */ - for (i = 0; i < diff; i++) - myputchar(' '); - - /* now move back the number of characters I want to */ - for (i = buff_pntr + diff; i != ncs; i--) - putchar(_BKSPC); - - fflush(stdout); - - curr_pntr = ncs; - } - else { - diff = 0; - ncs = curr_pntr; - /* Now insert the new word */ - for (i = 0; i < new_len; i++) - buff[b + i] = (pwct->wordv[pwix->word])[i]; - - /* move cursor to the beginning of the word */ - for (; curr_pntr != b; curr_pntr--) - putchar(_BKSPC); - - /** now print the characters on the rest of the line **/ - printbuff(curr_pntr, buff_pntr - curr_pntr); - - /* now move back the number of characters I want to */ - for (i = buff_pntr; i != ncs; i--) - putchar(_BKSPC); - - fflush(stdout); - - curr_pntr = ncs; - } - } -} - -void -find_wct(void) -{ - - char search_buff[100]; - char *filler = search_buff; - int b = curr_pntr - 1; - int e = curr_pntr; - int ne = 0; - int st; - Wix *pwix; - int curr_len; - int new_len; - int diff; - int i; - - /* - * First thing I do is try and construct the string to be searched for. - * Basically I just start from the curr_pntr and search backward until I - * find a blank. Once I find a blank I copy forward until I get back to - * curr_pntr; - */ - if (!curr_pntr) { - putchar(_BELL); - return; - } - /* then get back to the first blank or back to the beginning */ - while (b && !Delimiter(buff[b])) - b--; - if (Delimiter(buff[b])) - b++; - - /* At the same time, let me find the end of the word */ - while (e < buff_pntr && !Delimiter(buff[e])) { - e++; - ne++; - } - - st = b; - curr_len = e - b; - - /* now simply copy the text forward */ - while (b < curr_pntr) - *filler++ = buff[b++]; - - *filler = '\0'; - - pwix = scanWct(pwct, search_buff); - - /* - * else pwix = rescanWct(); - */ - - if (!pwix) { - putchar(_BELL); - fflush(stdout); - } - else { - Wct *pwct = pwix->pwct; - - /* - * printf("Found %s in file %s\n", pwct->wordv[pwix->word], - * pwct->fname); - */ - /* copy them buffer into where it should be */ - new_len = strlen(pwct->wordv[pwix->word]); - diff = new_len - curr_len; - if (curr_pntr != buff_pntr) { - forwardcopy(&buff[curr_pntr + diff], - &buff[curr_pntr], - buff_pntr - curr_pntr); - forwardflag_cpy(&buff_flag[curr_pntr + diff], - &buff_flag[curr_pntr], - buff_pntr - curr_pntr); - } - buff_pntr += diff; - - - /* Now insert the new characters */ - for (i = new_len - diff; i < new_len; i++) - buff[st + i] = (pwct->wordv[pwix->word])[i]; - - /* Now move the cursor forward to the end of the word */ - for (i = 0; i < diff; i++) - putchar(buff[curr_pntr++]); - - /** now print the characters on the rest of the line **/ - printbuff(curr_pntr, buff_pntr - curr_pntr); - - /* now move bcak the number of characters I want to */ - for (i = buff_pntr; i != e + diff; i--) - putchar(_BKSPC); - - fflush(stdout); - - curr_pntr = diff + e; - - } - - -} diff --git a/src/lib/wct.cc b/src/lib/wct.cc new file mode 100644 index 00000000..bac8c894 --- /dev/null +++ b/src/lib/wct.cc @@ -0,0 +1,731 @@ +/* + Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. + All rights reserved. + Copyright (C) 2007-2011 Gabriel Dos Reis. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + - Neither the name of The Numerical ALgorithms Group Ltd. nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/* + * Word completion. + * + * + * Word completion is driven from a list of completion tables. Each table + * contains a list of words. + * + */ + + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <fcntl.h> +#include <time.h> +#include <ctype.h> +#include <sys/types.h> +#include <sys/stat.h> + +/* #define PINFO *//* A flag to suppress printing of the file info */ + +#define WCT /* A flag needed because ctype.h stole some + * of my constants */ +#include "edible.h" + +#define MAX_PREFIX 1024 +#define strneql(a,b,n) (*(a) == *(b) && !strncmp((a),(b),(n))) +#define Delimiter(c) (! isalnum(c) && c != '%' && c != '!' && c != '?' && c != '_') + + +#include "wct.h" +#include "prt.h" +#include "edin.h" + + + +static Wct *pwct = 0; +static Wix *pwix; +static Wix curr_wix; +static char curr_prefix[MAX_PREFIX]; +static Wct *pHeadwct; + +time_t +ftime(char *path) +{ + int rc; + struct stat stbuf; + + rc = stat(path, &stbuf); + if (rc == -1) + fatal("Cannot deterimine status of %s.", path); + + return stbuf.st_mtime; +} + +off_t +fsize(char *path) +{ + int rc; + struct stat stbuf; + + rc = stat(path, &stbuf); + if (rc == -1) + fatal("Cannot deterimine status of %s.", path); + + return stbuf.st_size; +} + + +/* + * Scan a word completion table for a particular word. + */ + + +Wix * +scanWct(Wct *pwct, char *prefix) +{ + long fmod; + int preflen, i, wc; + char **wv; + + preflen = strlen(prefix); + strncpy(curr_prefix, prefix, MAX_PREFIX); + + pHeadwct = pwct; + curr_wix.pwct = 0; + curr_wix.word = 0; + + for (; pwct; pwct = pwct->next) { + curr_wix.pwct = pwct; + + + + fmod = ftime(pwct->fname); + if (fmod > pwct->ftime) + reintern1Wct(pwct); + + wv = pwct->wordv; + wc = pwct->wordc; + for (i = 0; i < wc; i++) { + curr_wix.word = i; + if (strneql(wv[i], prefix, preflen)) + return &curr_wix; + } + } + return 0; +} + +Wix * +rescanWct(void) +{ + Wct *pwct, *start_pwct; + int preflen, start_word, i, wc; + char **wv, *prefix; + + start_pwct = curr_wix.pwct; + start_word = curr_wix.word; + + if (!start_pwct) return(0); + prefix = curr_prefix; + preflen = strlen(prefix); + + /* + * Finish the current structure. + */ + + pwct = start_pwct; + + curr_wix.pwct = pwct; + wv = pwct->wordv; + wc = pwct->wordc; + for (i = start_word + 1; i < wc; i++) { + curr_wix.word = i; + if (strneql(wv[i], prefix, preflen)) + return &curr_wix; + } + + /* + * Finish to the end of the list, doing whole structures. + */ + for (pwct = pwct->next; pwct; pwct = pwct->next) { + curr_wix.pwct = pwct; + + wv = pwct->wordv; + wc = pwct->wordc; + for (i = 0; i < wc; i++) { + curr_wix.word = i; + if (strneql(wv[i], prefix, preflen)) + return &curr_wix; + } + } + + /* + * Restart at beginning, doing whole structures. + */ + for (pwct = pHeadwct; pwct != start_pwct; pwct = pwct->next) { + curr_wix.pwct = pwct; + + wv = pwct->wordv; + wc = pwct->wordc; + for (i = 0; i < wc; i++) { + curr_wix.word = i; + if (strneql(wv[i], prefix, preflen)) + return &curr_wix; + } + } + + /* + * Do beginning half of the start structure. + */ + curr_wix.pwct = pwct; + wv = pwct->wordv; + wc = pwct->wordc; + for (i = 0; i <= start_word; i++) { + curr_wix.word = i; + if (strneql(wv[i], prefix, preflen)) + return &curr_wix; + } + + /* Not found? */ + return 0; +} + +/* + * Summarize a table. + */ +void +skimWct(Wct *pwct) +{ + while (pwct) { +#ifdef PINFO + skim1Wct(pwct); +#endif + pwct = pwct->next; + } +} + +void +skim1Wct(Wct *pwct) +{ +#define NHEAD 13 +#define NTAIL 7 + + int cc; + + printf("%-10s", pwct->fname); + printTime((long *)&(pwct->ftime)); + cc = skimString(pwct->fimage, pwct->fsize, NHEAD, NTAIL); + printf("%s", " " + (cc - (NHEAD + NTAIL))); + printf(" [%d w, %ld c]", pwct->wordc, (long)(pwct->fsize)); + printf("\n"); + +#ifdef SHOW_WORDS + { + int i; + char **wv; + + for (i = 1, wv = pwct->wordv; *wv; i++, wv++) { + printf("%5d: %s\n", i, *wv); + } + } +#endif +} + +void +printTime(long *clock) +{ + struct tm *tm; + + tm = localtime((time_t *)clock); + printf("%.2d/%.2d/%.2d %.2d:%.2d:%.2d ", + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); +} + +int +skimString(char *s, int slen,int nhead,int ntail) +{ + int spos, tlen, i, cc; + + cc = printf("\""); + for (spos = 0; spos < slen && cc <= nhead; spos++) + cc += prChar(s[spos]); + + /* Assume same tail has the same multi-character format ratio. */ + tlen = ntail * ((1.0 * spos) / nhead); + + if (spos + tlen >= slen) + for (; spos < slen; spos++) + cc += prChar(s[spos]); + else { + cc += printf("\"...\""); + for (i = slen - tlen; i < slen; i++) + cc += prChar(s[i]); + } + cc += printf("\""); + return cc; +} + +int +prChar(int c) +{ + if (c == '\n') + return printf("\\n"); + if (c == '\t') + return printf("\\t"); + if (c == '\b') + return printf("\\b"); + if (c == '"') + return printf("\\\""); + if (iscntrl(c)) + return printf("^%c", (c + 0100) % 0200); + if (isascii(c)) + return printf("%c", c); + + return printf("\\%d", c); +} + +Wct * +reread1Wct(Wct *pwct) +{ + int fd, rc; + + /* Check information about the file. */ + pwct->fsize = fsize(pwct->fname); + pwct->ftime = ftime(pwct->fname); + + /* Allocate space for file image */ + if (pwct->fimage) + free(pwct->fimage); + pwct->fimage = (char *) malloc(pwct->fsize + 1); + if (pwct->fimage == 0) + sfatal("Cannot allocate new table."); + pwct->fimage[pwct->fsize] = 0; + + /* Read file into buffer. */ + fd = open(pwct->fname, O_RDONLY); + if (fd == -1) + fatal("Cannot read file %s.", pwct->fname); + rc = read(fd, pwct->fimage, pwct->fsize); + if (rc != pwct->fsize) + fatal("Did not read all of file %s.", pwct->fname); + + return pwct; +} + +Wct * +read1Wct(char *fname) +{ + Wct *pwct; + + /* Allocate a new link for this file. */ + pwct = (Wct *) malloc(sizeof(Wct)); + if (pwct == 0) + sfatal("Cannot allocate new table."); + + pwct->fname = fname; + pwct->wordc = 0; + pwct->wordv = 0; + pwct->fimage = 0; + pwct->next = 0; + + return reread1Wct(pwct); +} + +Wct * +nconcWct(Wct *pwct,Wct * qwct) +{ + Wct *p0 = pwct; + + if (!p0) + return qwct; + + while (pwct->next) + pwct = pwct->next; + pwct->next = qwct; + + return p0; +} + +void +sortWct(Wct *pwct) +{ + while (pwct) { + sort1Wct(pwct); + pwct = pwct->next; + } +} + +void +sort1Wct(Wct *pwct) +{ + qsort((char *) pwct->wordv, pwct->wordc, + sizeof(*(pwct->wordv)), mystrcmp); +} + +int +mystrcmp(const void *s1,const void * s2) +{ + return strcmp(*(char **)s1, *(char **)s2); +} + +/* + * Break wct struct into words. + */ + +void +burstWct(Wct *pwct) +{ + while (pwct) { + burst1Wct(pwct); + pwct = pwct->next; + } +} + +void +burst1Wct(Wct *pwct) +{ + char *s, **wv; + int i, j, inword = 0; + + + for (s = pwct->fimage, i = 0; i < pwct->fsize; s++, i++) { + if (isspace(*s) || iscntrl(*s)) { + *s = 0; + inword = 0; + } + else if (!inword) { + inword = 1; + pwct->wordc++; + } + } + + if (pwct->wordv) + free(pwct->wordv); + pwct->wordv = (char **) calloc(pwct->wordc + 1, sizeof(char *)); + if (!pwct->wordv) + fatal("Could not make word list for %s.", pwct->fname); + + s = pwct->fimage; + i = 0; + for (wv = pwct->wordv, j = 0; j < pwct->wordc; wv++, j++) { + while (i < pwct->fsize && !s[i]) + i++; + *wv = s + i; + while (i < pwct->fsize && s[i]) + i++; + } + *wv = 0; +} + +Wct * +intern1Wct(char *fname) +{ + Wct *pwct; + + pwct = read1Wct(fname); + burst1Wct(pwct); + return pwct; +} + +void +reintern1Wct(Wct *pwct) +{ + reread1Wct(pwct); + burst1Wct(pwct); +} + +void +sfatal(const char *s) +{ + fatal("%s", s); +} + +void +fatal(const char* fmt, const char* s) +{ + static char fbuf[256]; + + sprintf(fbuf, fmt, s); + + perror(fbuf); + exit(1); +} + + + +/* load up the wct database */ +void +load_wct_file(char *fname) +{ + pwct = nconcWct(intern1Wct(fname), pwct); +} + +void +skim_wct(void) +{ + skimWct(pwct); +} + +/* + * This routine is called when a tab is hit. It simply takes the current + * buffer and tries to find a completion of the last word on the line in the + * data base. + */ + + +void +rescan_wct(void) +{ + int b = curr_pntr - 1; + int old_len; + int new_len; + int diff; + int i; + int ncs = 0; + + /* + * first thing I should do is find my way back to the beginning of the + * word + */ + while (b && !Delimiter(buff[b])) + b--; + if (Delimiter(buff[b])) + b++; + + old_len = curr_pntr - b; + + pwix = rescanWct(); + + if (!pwix) { + putchar(_BELL); + fflush(stdout); + } + else { + Wct *pwct = pwix->pwct; /* start replacing it */ + + new_len = strlen(pwct->wordv[pwix->word]); + if (new_len > old_len) { + + /* + * I have to just slide the characters forward a bit, stuff in + * the new characters, and then adjust curr_pntr + */ + diff = new_len - old_len; + if (curr_pntr != buff_pntr) { + forwardcopy(&buff[curr_pntr + diff], + &buff[curr_pntr], + buff_pntr - curr_pntr); + forwardflag_cpy(&buff_flag[curr_pntr + diff], + &buff_flag[curr_pntr], + buff_pntr - curr_pntr); + } + buff_pntr += diff; + ncs = curr_pntr + diff; + + /* Now insert the new word */ + for (i = 0; i < new_len; i++) + buff[b + i] = (pwct->wordv[pwix->word])[i]; + + /* move cursor to the beginning of the word */ + for (; curr_pntr != b; curr_pntr--) + putchar(_BKSPC); + + /** now print the characters on the rest of the line **/ + printbuff(curr_pntr, buff_pntr - curr_pntr); + + /* now move bcak the number of characters I want to */ + for (i = buff_pntr; i != ncs; i--) + putchar(_BKSPC); + + fflush(stdout); + + curr_pntr = ncs; + } + else if (new_len < old_len) { + /* this time I simply copy backwards and do the substituting */ + diff = old_len - new_len; + strnmov(&buff[curr_pntr - diff], + &buff[curr_pntr], + buff_pntr - curr_pntr); + flagncpy(&buff_flag[curr_pntr - diff], + &buff_flag[curr_pntr], + buff_pntr - curr_pntr); + buff_pntr -= diff; + ncs = curr_pntr - diff; + + /* Now insert the new word */ + for (i = 0; i < new_len; i++) + buff[b + i] = (pwct->wordv[pwix->word])[i]; + + /* move cursor to the beginning of the word */ + for (; curr_pntr != b; curr_pntr--) + putchar(_BKSPC); + + /** now print the characters on the rest of the line **/ + printbuff(b, buff_pntr - b); + + /* now blank out the characters out on the end of this line */ + for (i = 0; i < diff; i++) + myputchar(' '); + + /* now move back the number of characters I want to */ + for (i = buff_pntr + diff; i != ncs; i--) + putchar(_BKSPC); + + fflush(stdout); + + curr_pntr = ncs; + } + else { + diff = 0; + ncs = curr_pntr; + /* Now insert the new word */ + for (i = 0; i < new_len; i++) + buff[b + i] = (pwct->wordv[pwix->word])[i]; + + /* move cursor to the beginning of the word */ + for (; curr_pntr != b; curr_pntr--) + putchar(_BKSPC); + + /** now print the characters on the rest of the line **/ + printbuff(curr_pntr, buff_pntr - curr_pntr); + + /* now move back the number of characters I want to */ + for (i = buff_pntr; i != ncs; i--) + putchar(_BKSPC); + + fflush(stdout); + + curr_pntr = ncs; + } + } +} + +void +find_wct(void) +{ + + char search_buff[100]; + char *filler = search_buff; + int b = curr_pntr - 1; + int e = curr_pntr; + int ne = 0; + int st; + Wix *pwix; + int curr_len; + int new_len; + int diff; + int i; + + /* + * First thing I do is try and construct the string to be searched for. + * Basically I just start from the curr_pntr and search backward until I + * find a blank. Once I find a blank I copy forward until I get back to + * curr_pntr; + */ + if (!curr_pntr) { + putchar(_BELL); + return; + } + /* then get back to the first blank or back to the beginning */ + while (b && !Delimiter(buff[b])) + b--; + if (Delimiter(buff[b])) + b++; + + /* At the same time, let me find the end of the word */ + while (e < buff_pntr && !Delimiter(buff[e])) { + e++; + ne++; + } + + st = b; + curr_len = e - b; + + /* now simply copy the text forward */ + while (b < curr_pntr) + *filler++ = buff[b++]; + + *filler = '\0'; + + pwix = scanWct(pwct, search_buff); + + /* + * else pwix = rescanWct(); + */ + + if (!pwix) { + putchar(_BELL); + fflush(stdout); + } + else { + Wct *pwct = pwix->pwct; + + /* + * printf("Found %s in file %s\n", pwct->wordv[pwix->word], + * pwct->fname); + */ + /* copy them buffer into where it should be */ + new_len = strlen(pwct->wordv[pwix->word]); + diff = new_len - curr_len; + if (curr_pntr != buff_pntr) { + forwardcopy(&buff[curr_pntr + diff], + &buff[curr_pntr], + buff_pntr - curr_pntr); + forwardflag_cpy(&buff_flag[curr_pntr + diff], + &buff_flag[curr_pntr], + buff_pntr - curr_pntr); + } + buff_pntr += diff; + + + /* Now insert the new characters */ + for (i = new_len - diff; i < new_len; i++) + buff[st + i] = (pwct->wordv[pwix->word])[i]; + + /* Now move the cursor forward to the end of the word */ + for (i = 0; i < diff; i++) + putchar(buff[curr_pntr++]); + + /** now print the characters on the rest of the line **/ + printbuff(curr_pntr, buff_pntr - curr_pntr); + + /* now move bcak the number of characters I want to */ + for (i = buff_pntr; i != e + diff; i--) + putchar(_BKSPC); + + fflush(stdout); + + curr_pntr = diff + e; + + } + + +} -- cgit v1.2.3