Asterisk - The Open Source Telephony Project GIT-master-4f2b068
Loading...
Searching...
No Matches
Functions
editline_compat.c File Reference
#include "asterisk.h"
#include <histedit.h>
#include "editline_compat.h"
Include dependency graph for editline_compat.c:

Go to the source code of this file.

Functions

int editline_read_char (EditLine *el, wchar_t *cp)
 
static int read_char (EditLine *el, wchar_t *cp)
 

Function Documentation

◆ editline_read_char()

int editline_read_char ( EditLine *  el,
wchar_t *  cp 
)

Definition at line 168 of file editline_compat.c.

169{
170 return read_char(el, cp);
171}
static EditLine * el
Definition asterisk.c:348
static int read_char(EditLine *el, wchar_t *cp)

References el, and read_char().

Referenced by ast_el_read_char().

◆ read_char()

static int read_char ( EditLine *  el,
wchar_t *  cp 
)
static

Definition at line 71 of file editline_compat.c.

72{
73 ssize_t num_read;
74#ifdef EDITLINE_ORIG
75 /* Removed for Asterisk. FIXIO is set when the EL_SAFEREAD
76 flag is set on the EditLine handle, which we do not do
77 so `tried` will always be 1 */
78 int tried = (el->el_flags & FIXIO) == 0;
79#endif
80 char cbuf[MB_LEN_MAX];
81 size_t cbp = 0;
82#ifdef EDITLINE_ORIG
83 /* Removed for Asterisk. This is only needed for FIXIO,
84 as above. */
85 int save_errno = errno;
86#endif
87
88 again:
89#ifdef EDITLINE_ORIG
90 /* Removed for Asterisk. libedit only sets up signal handlers
91 for SIGCONT and SIGWINCH if the EL_SIGNAL flag is set on
92 the EditLine handle, which we do not do.
93
94 Additionally, we don't have access to the internals of `el`
95 to get the file descriptor to read from, but we know that
96 we will always be reading from stdin, so hardcode it */
97 el->el_signal->sig_no = 0;
98 while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
99 int e = errno;
100 switch (el->el_signal->sig_no) {
101 case SIGCONT:
102 el_wset(el, EL_REFRESH);
103 /*FALLTHROUGH*/
104 case SIGWINCH:
105 sig_set(el);
106 goto again;
107 default:
108 break;
109 }
110 if (!tried && read__fixio(el->el_infd, e) == 0) {
111 errno = save_errno;
112 tried = 1;
113 } else {
114 errno = e;
115 *cp = L'\0';
116 return -1;
117 }
118 }
119#else
120 while ((num_read = read(STDIN_FILENO, cbuf + cbp, (size_t)1)) == -1) {
121 *cp = L'\0';
122 return -1;
123 }
124#endif
125
126 /* Test for EOF */
127 if (num_read == 0) {
128 *cp = L'\0';
129 return 0;
130 }
131
132 for (;;) {
133 mbstate_t mbs;
134
135 ++cbp;
136 /* This only works because UTF8 is stateless. */
137 memset(&mbs, 0, sizeof(mbs));
138 switch (mbrtowc(cp, cbuf, cbp, &mbs)) {
139 case (size_t)-1:
140 if (cbp > 1) {
141 /*
142 * Invalid sequence, discard all bytes
143 * except the last one.
144 */
145 cbuf[0] = cbuf[cbp - 1];
146 cbp = 0;
147 break;
148 } else {
149 /* Invalid byte, discard it. */
150 cbp = 0;
151 goto again;
152 }
153 case (size_t)-2:
154 if (cbp >= MB_LEN_MAX) {
155 errno = EILSEQ;
156 *cp = L'\0';
157 return -1;
158 }
159 /* Incomplete sequence, read another byte. */
160 goto again;
161 default:
162 /* Valid character, process it. */
163 return 1;
164 }
165 }
166}
int errno

References el, and errno.

Referenced by editline_read_char().