editlib.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett/***********************************************************************
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder* *
e071fb22ea9923a2a4ff41184d80ca46b55ee932Till Mossakowski* This software is part of the ast package *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* Copyright (c) 1984-2011 AT&T Intellectual Property *
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu* and is licensed under the *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* Eclipse Public License, Version 1.0 *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* by AT&T Intellectual Property *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* A copy of the License is available at *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* http://www.eclipse.org/org/documents/epl-v10.html *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* Information and Software Systems Research *
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett* AT&T Research *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* Florham Park NJ *
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly* *
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder* David Korn <dgk@research.att.com> *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* Pat Sullivan *
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett* *
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder***********************************************************************/
05b3e12808da901dccd665715cb934462290d550Andy Gimblett/*
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett * Miscellaneous routine needed for standalone library for edit modes
aa0d5f8be9950e788884f7431cf4cb7bee74788cAndy Gimblett */
aa0d5f8be9950e788884f7431cf4cb7bee74788cAndy Gimblett
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly#define read ______read
020cdb5dad6b871aba61136a0e1567c00426de87Andy Gimblett
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder#include "io.h"
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder#include "terminal.h"
567db7182e691cce5816365d8c912d09ffe92f86Andy Gimblett#include "history.h"
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder#undef read
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder#include "edit.h"
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder#ifdef LDYNAMIC
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder# include "dlldefs.h"
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder#endif
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder#ifdef TIOCLBIC
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder# undef TIOCLBIC
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett# ifdef _sys_ioctl
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly# include <sys/ioctl.h>
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly# endif /* _sys_ioctl */
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly#endif /* TIOCLBIC */
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder#undef read
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder#ifdef __EXPORT__
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder# define extern __EXPORT__
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder#endif
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder#define e_create "cannot create"
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reillychar opt_flag = 0;
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reillychar ed_errbuf[IOBSIZE+1] = { 0 };
9f93b2a8b552789cd939d599504d39732672dc84Christian Maederstruct fileblk *io_ftable[NFILE+USERIO] = { 0 };
9f93b2a8b552789cd939d599504d39732672dc84Christian Maederstatic struct fileblk outfile = { ed_errbuf, ed_errbuf, ed_errbuf+IOBSIZE, 2, IOWRT};
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reillystatic int editfd;
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reillystatic int output = 0;
9f93b2a8b552789cd939d599504d39732672dc84Christian Maederstatic char beenhere;
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder#if 0 /* almost always false. Makes a good block comment */
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder **********************************************************************
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett * There are two scenarios here.
53f89daf88665d3ea96d871110a5c0d9d8326bd2Andy Gimblett *
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett * 1) We ALWAYS want ksh-editing on the terminal input.
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett * 2) We SOMETIMES want ksh-editing on the terminal input.
53f89daf88665d3ea96d871110a5c0d9d8326bd2Andy Gimblett *
53f89daf88665d3ea96d871110a5c0d9d8326bd2Andy Gimblett * If you are running with case 1, then just call read() in the usual
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * manner and be happy.
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett *
c052e3ee4a53ee3a2da829aa142fd596ef6c9e3dAndy Gimblett * If you want to only have line-editing sometimes, and the very first
c052e3ee4a53ee3a2da829aa142fd596ef6c9e3dAndy Gimblett * read() is not one of those times, then you must perform the
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett * initialization first, turn OFF editing, perform your read(), and
c052e3ee4a53ee3a2da829aa142fd596ef6c9e3dAndy Gimblett * so forth. An example of this is in a curses application in which
a09bfcbcb0fba5663fca1968aa82daebf2e092c4Andy Gimblett * single characters are read in RAW mode and acted upon immediately.
a181b88611e09ffc9701a5f1022002cc0bc0c584Andy Gimblett * Line editing here will not work.
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett *
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett * Do the following:
a09bfcbcb0fba5663fca1968aa82daebf2e092c4Andy Gimblett * edit_Init();
05b3e12808da901dccd665715cb934462290d550Andy Gimblett * SAVopt_flag = set_edit(0,0); /* fd=0 set to NO editing */
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett * read(0,buffer,1); /* do your single char read */
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett * set_edit(0,SAVopt_flag); /* restore editing on fd=0 */
61051521e4d82769a47f23aecb5fb477de47d534Andy Gimblett **********************************************************************
61051521e4d82769a47f23aecb5fb477de47d534Andy Gimblett#endif /* 0 */
61051521e4d82769a47f23aecb5fb477de47d534Andy Gimblett
61051521e4d82769a47f23aecb5fb477de47d534Andy Gimblettint edit_Init()
61051521e4d82769a47f23aecb5fb477de47d534Andy Gimblett{
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett register char *sp;
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett if(!beenhere)
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett {
1c7c4d95775a8ad5f7373e5cf0bad86f8301c56cAndy Gimblett beenhere = 1;
b22c258cca179a5ffe777b64b32e10687c5f6b2cAndy Gimblett hist_open();
b22c258cca179a5ffe777b64b32e10687c5f6b2cAndy Gimblett if(!(sp = getenv("VISUAL")))
b22c258cca179a5ffe777b64b32e10687c5f6b2cAndy Gimblett sp = getenv("EDITOR");
b22c258cca179a5ffe777b64b32e10687c5f6b2cAndy Gimblett if(sp)
8528886a04f14abe0ddf80f50c853cc25bc821cdAndy Gimblett {
876bd2c70a93981cc80f8376284616bce4a0fefcChristian Maeder if(strrchr(sp,'/'))
aa0d5f8be9950e788884f7431cf4cb7bee74788cAndy Gimblett sp = strrchr(sp,'/')+1;
aa0d5f8be9950e788884f7431cf4cb7bee74788cAndy Gimblett if(strcmp(sp,"vi") == 0)
aa0d5f8be9950e788884f7431cf4cb7bee74788cAndy Gimblett opt_flag = EDITVI;
aa0d5f8be9950e788884f7431cf4cb7bee74788cAndy Gimblett else if(strcmp(sp,"emacs")==0)
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett opt_flag = EMACS;
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett else if(strcmp(sp,"gmacs")==0)
197888c54795ec1e79e77289b7e20436a6db74c0Andy Gimblett opt_flag = GMACS;
197888c54795ec1e79e77289b7e20436a6db74c0Andy Gimblett }
197888c54795ec1e79e77289b7e20436a6db74c0Andy Gimblett }
a1f6118e7ce7f8892fc4299e316630ec74083f0aAndy Gimblett return(1);
a1f6118e7ce7f8892fc4299e316630ec74083f0aAndy Gimblett}
a1f6118e7ce7f8892fc4299e316630ec74083f0aAndy Gimblett/*
a1f6118e7ce7f8892fc4299e316630ec74083f0aAndy Gimblett * read routine with edit modes
a1f6118e7ce7f8892fc4299e316630ec74083f0aAndy Gimblett */
042fd01d46834d3fecb5ac109ff905c5eb034376Liam O'Reilly
a1f6118e7ce7f8892fc4299e316630ec74083f0aAndy Gimblett#ifdef LDYNAMIC
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett#ifdef __STDC__
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimbletttypedef ssize_t (*Read_f)(int, void*, size_t);
05b3e12808da901dccd665715cb934462290d550Andy Gimblett#else
05b3e12808da901dccd665715cb934462290d550Andy Gimbletttypedef ssize_t (*Read_f)();
05b3e12808da901dccd665715cb934462290d550Andy Gimblett#endif
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblettssize_t rEAd(fd,buf,n)
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblettint fd;
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblettvoid *buf;
c679188b6762edb198e353f724e77c74aa64a7e4Andy Gimblettsize_t n;
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett{
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly ssize_t r;
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett static Read_f readfn;
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett static int here;
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly static int loop;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly static void* dll;
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett static char msg[] = "ie: cannot intercept read() system call\n";
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett /*
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett * on some systems read() is globally bound by the
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett * runtime linker which may get us into a loop if
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett * another library intercepts read() between us and
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett * the system library
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett * on these systems the (dllnext)() call bypasses
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett * any intermediate libraries and gets directly to
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett * the system library
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett */
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett if (here++)
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett {
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett if (loop++)
20ed727452613e36c0a95ddabf7ecc81cf941ed2Andy Gimblett return -1;
edc768ab3a40d51bf18761330cfc4b4d460c0822Andy Gimblett if (!(dll = (dllnext)(RTLD_LAZY)))
0a83f8dcd5598436966584b858313eb5efd95d5bLiam O'Reilly goto bad;
0a83f8dcd5598436966584b858313eb5efd95d5bLiam O'Reilly readfn = 0;
0a83f8dcd5598436966584b858313eb5efd95d5bLiam O'Reilly }
0a83f8dcd5598436966584b858313eb5efd95d5bLiam O'Reilly if (!readfn)
0a83f8dcd5598436966584b858313eb5efd95d5bLiam O'Reilly {
0a83f8dcd5598436966584b858313eb5efd95d5bLiam O'Reilly if (!dll && !(dll = dllnext(RTLD_LAZY)))
0a83f8dcd5598436966584b858313eb5efd95d5bLiam O'Reilly goto bad;
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly if (!(readfn = (Read_f)dlsym(dll, "_read")) && !(readfn = (Read_f)dlsym(dll, "read")))
2f35e5f6757968746dbab385be21fcae52378a3fLiam O'Reilly goto bad;
2f35e5f6757968746dbab385be21fcae52378a3fLiam O'Reilly }
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly r = (*readfn)(fd, buf, n);
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reilly here--;
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reilly return r;
d9c1248c7972dfdafbacb1b73b2eb965eac9ef42Liam O'Reilly bad:
7371f8fe3a9a286a74ea30a3cd18e7740f67d537Andy Gimblett write(2, msg, sizeof(msg) - 1);
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly return -1;
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly}
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly#endif /* LDYNAMIC */
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblettextern ssize_t read(fd,buf,n)
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblettint fd;
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblettvoid *buf;
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblettsize_t n;
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett{
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett register int r, flag;
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett char *buff = (char*)buf;
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett if(fd==editfd && !beenhere)
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett edit_Init();
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett flag = (fd==editfd?opt_flag&EDITMASK:0);
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett switch(flag)
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett {
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett case EMACS:
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett case GMACS:
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett tty_set(-1);
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett r = emacs_read(fd,buff,n);
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett break;
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett case VIRAW:
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett case EDITVI:
c679188b6762edb198e353f724e77c74aa64a7e4Andy Gimblett tty_set(-1);
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett r = vi_read(fd,buff,n);
c679188b6762edb198e353f724e77c74aa64a7e4Andy Gimblett break;
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett default:
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett {
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly#ifdef SYSCALL
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett r = syscall(3,fd,buff,n);
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett#else
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett r = rEAd(fd,buff,n);
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett#endif /* SYSCALL */
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett }
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett }
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett if(fd==editfd && hist_ptr && (opt_flag&NOHIST)==0 && r>0)
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett {
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett /* write and flush history */
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett int c = buff[r];
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett buff[r] = 0;
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett hist_eof();
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett p_setout(hist_ptr->fixfd);
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett p_str(buff,0);
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett hist_flush();
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett buff[r] = c;
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett }
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett return(r);
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett}
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett#ifndef __EXPORT__
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblettextern ssize_t _read(fd,buf,n)
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblettint fd;
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblettvoid *buf;
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblettsize_t n;
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett{
06dd4e7c29f33f6122a910719e3bd9062256e397Andy Gimblett return(read(fd,buf,n));
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett}
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblett
da955132262baab309a50fdffe228c9efe68251dCui Jianextern ssize_t __read(fd,buf,n)
afc52bfaabee38c4d55cee9f35b1a0028ba3854aAndy Gimblettint fd;
7371f8fe3a9a286a74ea30a3cd18e7740f67d537Andy Gimblettvoid *buf;
7371f8fe3a9a286a74ea30a3cd18e7740f67d537Andy Gimblettsize_t n;
7371f8fe3a9a286a74ea30a3cd18e7740f67d537Andy Gimblett{
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly return(read(fd,buf,n));
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly}
7371f8fe3a9a286a74ea30a3cd18e7740f67d537Andy Gimblett
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reillyextern ssize_t _libc_read(fd,buf,n)
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reillyint fd;
2bb060537a37352251aa04d8dc09aa53aad5d4bfLiam O'Reillyvoid *buf;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reillysize_t n;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly{
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly return(read(fd,buf,n));
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly}
2bb060537a37352251aa04d8dc09aa53aad5d4bfLiam O'Reilly
576a4ca6de740c90afd448607c2323477139de24Liam O'Reillyextern ssize_t __libc_read(fd,buf,n)
576a4ca6de740c90afd448607c2323477139de24Liam O'Reillyint fd;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reillyvoid *buf;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reillysize_t n;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly{
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly return(read(fd,buf,n));
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly}
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly#endif
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly/*
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly * enable edit mode <mode> on file number <fd>
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly * the NOHIST bit can also be set to avoid writing the history file
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly * <fd> cannot be file two
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly */
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly
576a4ca6de740c90afd448607c2323477139de24Liam O'Reillyint set_edit(fd,mode)
576a4ca6de740c90afd448607c2323477139de24Liam O'Reillyint fd;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reillyint mode;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly{
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly int retval = opt_flag;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly opt_flag = mode;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly if(fd==2)
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly return(-1);
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly editfd = fd;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly return(retval);
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly}
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly/*
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly * flush the output queue and reset the output stream
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly */
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly
576a4ca6de740c90afd448607c2323477139de24Liam O'Reillyvoid p_setout(fd)
576a4ca6de740c90afd448607c2323477139de24Liam O'Reillyregister int fd;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly{
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly register struct fileblk *fp;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly if(!io_ftable[fd])
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly io_ftable[fd] = &outfile;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly fp = io_ftable[fd];
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly fp->last = fp->base + IOBSIZE;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly fp->flag &= ~(IOREAD|IOERR|IOEOF);
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly if(output==fd)
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly return;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly if(io_ftable[fd]==io_ftable[output])
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly p_flush();
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly output = fd;
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly}
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly
576a4ca6de740c90afd448607c2323477139de24Liam O'Reilly/*
7371f8fe3a9a286a74ea30a3cd18e7740f67d537Andy Gimblett * flush the output if necessary and null terminate the buffer
7371f8fe3a9a286a74ea30a3cd18e7740f67d537Andy Gimblett */
7371f8fe3a9a286a74ea30a3cd18e7740f67d537Andy Gimblett
7371f8fe3a9a286a74ea30a3cd18e7740f67d537Andy Gimblettvoid p_flush()
7371f8fe3a9a286a74ea30a3cd18e7740f67d537Andy Gimblett{
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly register struct fileblk *fp = io_ftable[output];
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly register int count;
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly if(fp && (count=fp->ptr-fp->base))
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly {
if(write(output,fp->base,count) < 0)
fp->flag |= IOERR;
/* leave previous buffer as a null terminated string */
*fp->ptr = 0;
fp->ptr = fp->base;
}
}
/*
* print a given character
*/
void p_char(c)
register int c;
{
register struct fileblk *fp = io_ftable[output];
if(fp->ptr >= fp->last)
p_flush();
*fp->ptr++ = c;
}
/*
* print a string optionally followed by a character
*/
void p_str(string,c)
register char *string;
int c;
{
register struct fileblk *fp = io_ftable[output];
register int cc;
while(1)
{
if((cc= *string)==0)
cc = c,c = 0;
else
string++;
if(cc==0)
break;
if(fp->ptr >= fp->last)
p_flush();
*fp->ptr++ = cc;
}
}
/*
* copy string a to string b and return pointer to end of string b
*/
char *ed_movstr(a,b)
register const char *a;
register char *b;
{
while(*b++ = *a++);
return(--b);
}
/*
* print and error message and exit
*/
void ed_failed(name,message)
char *name,*message;
{
p_setout(ERRIO);
p_str(name,' ');
p_char(':');
p_char(' ');
p_str(message,'\n');
exit(2);
}
/*
* print a prompt
*/
void pr_prompt(string)
register char *string;
{
register int c;
register char *dp = editb.e_prbuff;
#ifdef TIOCLBIC
int mode;
mode = LFLUSHO;
ioctl(ERRIO,TIOCLBIC,&mode);
#endif /* TIOCLBIC */
p_setout(ERRIO);
while(c= *string++)
{
if(dp < editb.e_prbuff+PRSIZE)
*dp++ = c;
p_char(c);
}
*dp = 0;
if (!(opt_flag&EDITMASK))
p_flush();
}