12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930 |
- #include "busybox.h"
- #include "NUM_APPLETS.h"
- #include "unicode.h"
- #ifndef _POSIX_VDISABLE
- # define _POSIX_VDISABLE '\0'
- #endif
- #ifdef TEST
- # define ENABLE_FEATURE_EDITING 0
- # define ENABLE_FEATURE_TAB_COMPLETION 0
- # define ENABLE_FEATURE_USERNAME_COMPLETION 0
- #endif
- #if ENABLE_FEATURE_EDITING
- #define ENABLE_USERNAME_OR_HOMEDIR \
- (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
- #define IF_USERNAME_OR_HOMEDIR(...)
- #if ENABLE_USERNAME_OR_HOMEDIR
- # undef IF_USERNAME_OR_HOMEDIR
- # define IF_USERNAME_OR_HOMEDIR(...) __VA_ARGS__
- #endif
- #undef CHAR_T
- #if ENABLE_UNICODE_SUPPORT
- # define BB_NUL ((wchar_t)0)
- # define CHAR_T wchar_t
- static bool BB_isspace(CHAR_T c) { return ((unsigned)c < 256 && isspace(c)); }
- # if ENABLE_FEATURE_EDITING_VI
- static bool BB_isalnum_or_underscore(CHAR_T c) {
- return ((unsigned)c < 256 && isalnum(c)) || c == '_';
- }
- # endif
- static bool BB_ispunct(CHAR_T c) { return ((unsigned)c < 256 && ispunct(c)); }
- # undef isspace
- # undef isalnum
- # undef ispunct
- # undef isprint
- # define isspace isspace_must_not_be_used
- # define isalnum isalnum_must_not_be_used
- # define ispunct ispunct_must_not_be_used
- # define isprint isprint_must_not_be_used
- #else
- # define BB_NUL '\0'
- # define CHAR_T char
- # define BB_isspace(c) isspace(c)
- # if ENABLE_FEATURE_EDITING_VI
- static bool BB_isalnum_or_underscore(CHAR_T c)
- {
- return ((unsigned)c < 256 && isalnum(c)) || c == '_';
- }
- # endif
- # define BB_ispunct(c) ispunct(c)
- #endif
- #if ENABLE_UNICODE_PRESERVE_BROKEN
- # define unicode_mark_raw_byte(wc) ((wc) | 0x20000000)
- # define unicode_is_raw_byte(wc) ((wc) & 0x20000000)
- #else
- # define unicode_is_raw_byte(wc) 0
- #endif
- #define ESC "\033"
- #define SEQ_CLEAR_TILL_END_OF_SCREEN ESC"[J"
- enum {
- MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
- ? CONFIG_FEATURE_EDITING_MAX_LEN
- : 0x7ff0
- };
- #if ENABLE_USERNAME_OR_HOMEDIR
- static const char null_str[] ALIGN1 = "";
- #endif
- struct lineedit_statics {
- line_input_t *state;
- unsigned cmdedit_termw;
- unsigned cmdedit_x;
- unsigned cmdedit_y;
- unsigned cmdedit_prmt_len;
- unsigned cursor;
- int command_len;
-
- int maxsize;
- CHAR_T *command_ps;
- const char *cmdedit_prompt;
- const char *prompt_last_line;
- #if ENABLE_USERNAME_OR_HOMEDIR
- char *user_buf;
- char *home_pwd_buf;
- #endif
- #if ENABLE_FEATURE_TAB_COMPLETION
- char **matches;
- unsigned num_matches;
- #endif
- unsigned SIGWINCH_saved;
- volatile unsigned SIGWINCH_count;
- volatile smallint ok_to_redraw;
- #if ENABLE_FEATURE_EDITING_VI
- # define DELBUFSIZ 128
- smallint newdelflag;
- CHAR_T *delptr;
- CHAR_T delbuf[DELBUFSIZ];
- #endif
- #if ENABLE_FEATURE_EDITING_ASK_TERMINAL
- smallint sent_ESC_br6n;
- #endif
-
- struct sigaction SIGWINCH_handler;
- };
- extern struct lineedit_statics *const lineedit_ptr_to_statics;
- #define S (*lineedit_ptr_to_statics)
- #define state (S.state )
- #define cmdedit_termw (S.cmdedit_termw )
- #define cmdedit_x (S.cmdedit_x )
- #define cmdedit_y (S.cmdedit_y )
- #define cmdedit_prmt_len (S.cmdedit_prmt_len)
- #define cursor (S.cursor )
- #define command_len (S.command_len )
- #define command_ps (S.command_ps )
- #define cmdedit_prompt (S.cmdedit_prompt )
- #define prompt_last_line (S.prompt_last_line)
- #define user_buf (S.user_buf )
- #define home_pwd_buf (S.home_pwd_buf )
- #define matches (S.matches )
- #define num_matches (S.num_matches )
- #define delptr (S.delptr )
- #define newdelflag (S.newdelflag )
- #define delbuf (S.delbuf )
- #define INIT_S() do { \
- (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
- barrier(); \
- cmdedit_termw = 80; \
- IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \
- IF_FEATURE_EDITING_VI(delptr = delbuf;) \
- } while (0)
- static void deinit_S(void)
- {
- #if ENABLE_FEATURE_EDITING_FANCY_PROMPT
-
- free((char*)cmdedit_prompt);
- #endif
- #if ENABLE_USERNAME_OR_HOMEDIR
- free(user_buf);
- if (home_pwd_buf != null_str)
- free(home_pwd_buf);
- #endif
- free(lineedit_ptr_to_statics);
- }
- #define DEINIT_S() deinit_S()
- #if ENABLE_UNICODE_SUPPORT
- static size_t load_string(const char *src)
- {
- if (unicode_status == UNICODE_ON) {
- ssize_t len = mbstowcs(command_ps, src, S.maxsize - 1);
- if (len < 0)
- len = 0;
- command_ps[len] = BB_NUL;
- return len;
- } else {
- unsigned i = 0;
- while (src[i] && i < S.maxsize - 1) {
- command_ps[i] = src[i];
- i++;
- }
- command_ps[i] = BB_NUL;
- return i;
- }
- }
- static unsigned save_string(char *dst, unsigned maxsize)
- {
- if (unicode_status == UNICODE_ON) {
- # if !ENABLE_UNICODE_PRESERVE_BROKEN
- ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
- if (len < 0)
- len = 0;
- dst[len] = '\0';
- return len;
- # else
- unsigned dstpos = 0;
- unsigned srcpos = 0;
- maxsize--;
- while (dstpos < maxsize) {
- wchar_t wc;
- int n = srcpos;
-
- while ((wc = command_ps[srcpos]) != BB_NUL
- && !unicode_is_raw_byte(wc)
- ) {
- srcpos++;
- }
- command_ps[srcpos] = BB_NUL;
- n = wcstombs(dst + dstpos, command_ps + n, maxsize - dstpos);
- if (n < 0)
- break;
- dstpos += n;
- if (wc == BB_NUL)
- break;
-
- command_ps[srcpos] = wc;
- srcpos++;
- if (dstpos == maxsize)
- break;
- dst[dstpos++] = (char) wc;
- }
- dst[dstpos] = '\0';
- return dstpos;
- # endif
- } else {
- unsigned i = 0;
- while ((dst[i] = command_ps[i]) != 0)
- i++;
- return i;
- }
- }
- static void BB_PUTCHAR(wchar_t c)
- {
- if (unicode_status == UNICODE_ON) {
- char buf[MB_CUR_MAX + 1];
- mbstate_t mbst = { 0 };
- ssize_t len = wcrtomb(buf, c, &mbst);
- if (len > 0) {
- buf[len] = '\0';
- fputs(buf, stdout);
- }
- } else {
-
- putchar(c);
- }
- }
- # if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
- static wchar_t adjust_width_and_validate_wc(unsigned *width_adj, wchar_t wc)
- # else
- static wchar_t adjust_width_and_validate_wc(wchar_t wc)
- # define adjust_width_and_validate_wc(width_adj, wc) \
- ((*(width_adj))++, adjust_width_and_validate_wc(wc))
- # endif
- {
- int w = 1;
- if (unicode_status == UNICODE_ON) {
- if (wc > CONFIG_LAST_SUPPORTED_WCHAR) {
-
- goto subst;
- }
- w = wcwidth(wc);
- if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0)
- || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
- || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
- ) {
- subst:
- w = 1;
- wc = CONFIG_SUBST_WCHAR;
- }
- }
- # if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
- *width_adj += w;
- #endif
- return wc;
- }
- #else
- static size_t load_string(const char *src)
- {
- safe_strncpy(command_ps, src, S.maxsize);
- return strlen(command_ps);
- }
- # if ENABLE_FEATURE_TAB_COMPLETION
- static void save_string(char *dst, unsigned maxsize)
- {
- safe_strncpy(dst, command_ps, maxsize);
- }
- # endif
- # define BB_PUTCHAR(c) bb_putchar(c)
- int adjust_width_and_validate_wc(unsigned *width_adj, int wc);
- #endif
- #define HACK_FOR_WRONG_WIDTH 1
- static void put_cur_glyph_and_inc_cursor(void)
- {
- CHAR_T c = command_ps[cursor];
- unsigned width = 0;
- int ofs_to_right;
- if (c == BB_NUL) {
-
- c = ' ';
- } else {
-
- cursor++;
- if (unicode_status == UNICODE_ON) {
- IF_UNICODE_WIDE_WCHARS(width = cmdedit_x;)
- c = adjust_width_and_validate_wc(&cmdedit_x, c);
- IF_UNICODE_WIDE_WCHARS(width = cmdedit_x - width;)
- } else {
- cmdedit_x++;
- }
- }
- ofs_to_right = cmdedit_x - cmdedit_termw;
- if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right <= 0) {
-
- BB_PUTCHAR(c);
- }
- if (ofs_to_right >= 0) {
-
- #if HACK_FOR_WRONG_WIDTH
-
- puts("\r");
- #else
-
-
- c = command_ps[cursor];
- if (c == BB_NUL)
- c = ' ';
- BB_PUTCHAR(c);
- bb_putchar('\b');
- #endif
- cmdedit_y++;
- if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right == 0) {
- width = 0;
- } else {
-
- BB_PUTCHAR(c);
- }
- cmdedit_x = width;
- }
- }
- static void put_till_end_and_adv_cursor(void)
- {
- while (cursor < command_len)
- put_cur_glyph_and_inc_cursor();
- }
- static void goto_new_line(void)
- {
- put_till_end_and_adv_cursor();
- if (cmdedit_x != 0)
- bb_putchar('\n');
- }
- static void beep(void)
- {
- bb_putchar('\007');
- }
- static void put_prompt_custom(bool is_full)
- {
- fputs((is_full ? cmdedit_prompt : prompt_last_line), stdout);
- cursor = 0;
- cmdedit_y = cmdedit_prmt_len / cmdedit_termw;
- cmdedit_x = cmdedit_prmt_len % cmdedit_termw;
- }
- #define put_prompt_last_line() put_prompt_custom(0)
- #define put_prompt() put_prompt_custom(1)
- static void input_backward(unsigned num)
- {
- if (num > cursor)
- num = cursor;
- if (num == 0)
- return;
- cursor -= num;
- if ((ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS)
- && unicode_status == UNICODE_ON
- ) {
-
- int n = num;
- num = 0;
- while (--n >= 0)
- adjust_width_and_validate_wc(&num, command_ps[cursor + n]);
- if (num == 0)
- return;
- }
- if (cmdedit_x >= num) {
- cmdedit_x -= num;
- if (num <= 4) {
-
- do {
- bb_putchar('\b');
- } while (--num);
- return;
- }
- printf(ESC"[%uD", num);
- return;
- }
-
- if (ENABLE_UNICODE_WIDE_WCHARS) {
-
- unsigned sv_cursor;
-
- printf("\r" ESC"[%uA", cmdedit_y);
- cmdedit_y = 0;
- sv_cursor = cursor;
- put_prompt_last_line();
- while (cursor < sv_cursor)
- put_cur_glyph_and_inc_cursor();
- } else {
- int lines_up;
-
- num -= cmdedit_x;
-
- lines_up = 1 + (num - 1) / cmdedit_termw;
- cmdedit_x = (cmdedit_termw * cmdedit_y - num) % cmdedit_termw;
- cmdedit_y -= lines_up;
-
- printf("\r" ESC"[%uA", lines_up);
-
- if (cmdedit_x)
- printf(ESC"[%uC", cmdedit_x);
- }
- }
- static void draw_custom(int y, int back_cursor, bool is_full)
- {
- if (y > 0)
- printf(ESC"[%uA", y);
- bb_putchar('\r');
- put_prompt_custom(is_full);
- put_till_end_and_adv_cursor();
- printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
- input_backward(back_cursor);
- }
- #define redraw(y, back_cursor) draw_custom((y), (back_cursor), 0)
- #define draw_full(back_cursor) draw_custom(0, (back_cursor), 1)
- #if !ENABLE_FEATURE_EDITING_VI
- static void input_delete(void)
- #define input_delete(save) input_delete()
- #else
- static void input_delete(int save)
- #endif
- {
- int j = cursor;
- if (j == (int)command_len)
- return;
- #if ENABLE_FEATURE_EDITING_VI
- if (save) {
- if (newdelflag) {
- delptr = delbuf;
- newdelflag = 0;
- }
- if ((delptr - delbuf) < DELBUFSIZ)
- *delptr++ = command_ps[j];
- }
- #endif
- memmove(command_ps + j, command_ps + j + 1,
-
- (command_len - j) * sizeof(command_ps[0]));
- command_len--;
- put_till_end_and_adv_cursor();
-
- printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
- input_backward(cursor - j);
- }
- #if ENABLE_FEATURE_EDITING_VI
- static void put(void)
- {
- int ocursor;
- int j = delptr - delbuf;
- if (j == 0)
- return;
- ocursor = cursor;
-
- memmove(command_ps + cursor + j, command_ps + cursor,
- (command_len - cursor + 1) * sizeof(command_ps[0]));
- memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0]));
- command_len += j;
- put_till_end_and_adv_cursor();
- input_backward(cursor - ocursor - j + 1);
- }
- #endif
- static void input_backspace(void)
- {
- if (cursor > 0) {
- input_backward(1);
- input_delete(0);
- }
- }
- static void input_forward(void)
- {
- if (cursor < command_len)
- put_cur_glyph_and_inc_cursor();
- }
- #if ENABLE_FEATURE_TAB_COMPLETION
- static void free_tab_completion_data(void)
- {
- if (matches) {
- while (num_matches)
- free(matches[--num_matches]);
- free(matches);
- matches = NULL;
- }
- }
- static void add_match(char *matched)
- {
- unsigned char *p = (unsigned char*)matched;
- while (*p) {
-
- if (*p < ' '
- || (!ENABLE_UNICODE_SUPPORT && *p >= 0x7f)
- || (ENABLE_UNICODE_SUPPORT && *p == 0x7f)
- ) {
- free(matched);
- return;
- }
- p++;
- }
- matches = xrealloc_vector(matches, 4, num_matches);
- matches[num_matches] = matched;
- num_matches++;
- }
- # if ENABLE_FEATURE_USERNAME_COMPLETION
- static char *username_path_completion(char *ud)
- {
- struct passwd *entry;
- char *tilde_name = ud;
- char *home = NULL;
- ud++;
- if (*ud == '/') {
- home = home_pwd_buf;
- } else {
-
- ud = strchr(ud, '/');
- *ud = '\0';
- entry = getpwnam(tilde_name + 1);
- *ud = '/';
- if (entry)
- home = entry->pw_dir;
- }
- if (home) {
- ud = concat_path_file(home, ud);
- free(tilde_name);
- tilde_name = ud;
- }
- return tilde_name;
- }
- static NOINLINE unsigned complete_username(const char *ud)
- {
- struct passwd *pw;
- unsigned userlen;
- ud++;
- userlen = strlen(ud);
- setpwent();
- while ((pw = getpwent()) != NULL) {
-
- if ( is_prefixed_with(pw->pw_name, ud)) {
- add_match(xasprintf("~%s/", pw->pw_name));
- }
- }
- endpwent();
- return 1 + userlen;
- }
- # endif
- enum {
- FIND_EXE_ONLY = 0,
- FIND_DIR_ONLY = 1,
- FIND_FILE_ONLY = 2,
- };
- static int path_parse(char ***p)
- {
- int npth;
- const char *pth;
- char *tmp;
- char **res;
- if (state->flags & WITH_PATH_LOOKUP)
- pth = state->path_lookup;
- else
- pth = getenv("PATH");
-
- if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
- return 1;
- tmp = (char*)pth;
- npth = 1;
- while (1) {
- tmp = strchr(tmp, ':');
- if (!tmp)
- break;
- tmp++;
- if (*tmp == '\0')
- break;
- npth++;
- }
- *p = res = xmalloc(npth * sizeof(res[0]));
- res[0] = tmp = xstrdup(pth);
- npth = 1;
- while (1) {
- tmp = strchr(tmp, ':');
- if (!tmp)
- break;
- *tmp++ = '\0';
- if (*tmp == '\0')
- break;
- res[npth++] = tmp;
- }
- return npth;
- }
- static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
- {
- char *path1[1];
- char **paths = path1;
- int npaths;
- int i;
- unsigned pf_len;
- const char *pfind;
- char *dirbuf = NULL;
- npaths = 1;
- path1[0] = (char*)".";
- pfind = strrchr(command, '/');
- if (!pfind) {
- if (type == FIND_EXE_ONLY)
- npaths = path_parse(&paths);
- pfind = command;
- } else {
-
- pfind++;
-
- dirbuf = xstrndup(command, pfind - command);
- # if ENABLE_FEATURE_USERNAME_COMPLETION
- if (dirbuf[0] == '~')
- dirbuf = username_path_completion(dirbuf);
- # endif
- path1[0] = dirbuf;
- }
- pf_len = strlen(pfind);
- # if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1
- if (type == FIND_EXE_ONLY && !dirbuf) {
- const char *p = applet_names;
- while (*p) {
- if (strncmp(pfind, p, pf_len) == 0)
- add_match(xstrdup(p));
- while (*p++ != '\0')
- continue;
- }
- }
- # endif
- for (i = 0; i < npaths; i++) {
- DIR *dir;
- struct dirent *next;
- struct stat st;
- char *found;
- dir = opendir(paths[i]);
- if (!dir)
- continue;
- while ((next = readdir(dir)) != NULL) {
- unsigned len;
- const char *name_found = next->d_name;
-
- if (!pfind[0] && DOT_OR_DOTDOT(name_found))
- continue;
-
- if (!is_prefixed_with(name_found, pfind))
- continue;
- found = concat_path_file(paths[i], name_found);
-
- if (stat(found, &st) && lstat(found, &st))
- goto cont;
-
- len = strlen(name_found);
- found = xrealloc(found, len + 2);
- strcpy(found, name_found);
- if (S_ISDIR(st.st_mode)) {
-
- found[len] = '/';
- found[len + 1] = '\0';
- } else {
-
- if (type == FIND_DIR_ONLY)
- goto cont;
- }
-
- add_match(found);
- continue;
- cont:
- free(found);
- }
- closedir(dir);
- }
- if (paths != path1) {
- free(paths[0]);
- free(paths);
- }
- free(dirbuf);
- return pf_len;
- }
- #define dbg_bmp 0
- #define QUOT (UCHAR_MAX+1)
- static void remove_chunk(int16_t *int_buf, int beg, int end)
- {
-
- if (beg == end)
- return;
- while ((int_buf[beg] = int_buf[end]) != 0)
- beg++, end++;
- if (dbg_bmp) {
- int i;
- for (i = 0; int_buf[i]; i++)
- bb_putchar((unsigned char)int_buf[i]);
- bb_putchar('\n');
- }
- }
- static NOINLINE int build_match_prefix(char *match_buf)
- {
- int i, j;
- int command_mode;
- int16_t *int_buf = (int16_t*)match_buf;
- if (dbg_bmp) printf("\n%s\n", match_buf);
-
- i = strlen(match_buf);
- do {
- int_buf[i] = (unsigned char)match_buf[i];
- i--;
- } while (i >= 0);
-
- for (i = 0; int_buf[i]; i++) {
- if (int_buf[i] == '\\') {
- remove_chunk(int_buf, i, i + 1);
- int_buf[i] |= QUOT;
- }
- }
-
- {
- int in_quote = 0;
- i = 0;
- while (int_buf[i]) {
- int cur = int_buf[i];
- if (!cur)
- break;
- if (cur == '\'' || cur == '"') {
- if (!in_quote || (cur == in_quote)) {
- in_quote ^= cur;
- remove_chunk(int_buf, i, i + 1);
- continue;
- }
- }
- if (in_quote)
- int_buf[i] = cur | QUOT;
- i++;
- }
- }
-
- for (i = 0; int_buf[i]; i++) {
- int cur = int_buf[i];
- if (cur == ';' || cur == '&' || cur == '|') {
- int prev = i ? int_buf[i - 1] : 0;
- if (cur == '&' && (prev == '>' || prev == '<')) {
- continue;
- } else if (cur == '|' && prev == '>') {
- continue;
- }
- remove_chunk(int_buf, 0, i + 1 + (cur == int_buf[i + 1]));
- i = -1;
- }
- }
-
- for (i = 0; int_buf[i]; i++) {
- if (int_buf[i] == '`') {
- for (j = i + 1; int_buf[j]; j++) {
- if (int_buf[j] == '`') {
-
- remove_chunk(int_buf, i, j);
- goto next;
- }
- }
-
- remove_chunk(int_buf, 0, i + 1);
- break;
- next: ;
- }
- }
-
- for (i = 0; int_buf[i]; i++) {
- if (int_buf[i] == '(' || int_buf[i] == '{') {
- remove_chunk(int_buf, 0, i + 1);
- i = -1;
- }
- }
-
- for (i = 0; int_buf[i]; i++)
- if (int_buf[i] != ' ')
- break;
- remove_chunk(int_buf, 0, i);
-
- command_mode = FIND_EXE_ONLY;
- for (i = 0; int_buf[i]; i++) {
- if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
- if (int_buf[i] == ' '
- && command_mode == FIND_EXE_ONLY
- && (char)int_buf[0] == 'c'
- && (char)int_buf[1] == 'd'
- && i == 2
- ) {
- command_mode = FIND_DIR_ONLY;
- } else {
- command_mode = FIND_FILE_ONLY;
- break;
- }
- }
- }
- if (dbg_bmp) printf("command_mode(0:exe/1:dir/2:file):%d\n", command_mode);
-
- for (i = 0; int_buf[i]; i++)
- continue;
- for (--i; i >= 0; i--) {
- int cur = int_buf[i];
- if (cur == ' ' || cur == '<' || cur == '>' || cur == '|' || cur == '&') {
- remove_chunk(int_buf, 0, i + 1);
- break;
- }
- }
-
- i = 0;
- while ((match_buf[i] = int_buf[i]) != '\0')
- i++;
- if (dbg_bmp) printf("final match_buf:'%s'\n", match_buf);
- return command_mode;
- }
- static void showfiles(void)
- {
- int ncols, row;
- int column_width = 0;
- int nfiles = num_matches;
- int nrows = nfiles;
- int l;
-
- for (row = 0; row < nrows; row++) {
- l = unicode_strwidth(matches[row]);
- if (column_width < l)
- column_width = l;
- }
- column_width += 2;
- ncols = cmdedit_termw / column_width;
- if (ncols > 1) {
- nrows /= ncols;
- if (nfiles % ncols)
- nrows++;
- } else {
- ncols = 1;
- }
- for (row = 0; row < nrows; row++) {
- int n = row;
- int nc;
- for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
- printf("%s%-*s", matches[n],
- (int)(column_width - unicode_strwidth(matches[n])), ""
- );
- }
- if (ENABLE_UNICODE_SUPPORT)
- puts(printable_string(NULL, matches[n]));
- else
- puts(matches[n]);
- }
- }
- static const char *is_special_char(char c)
- {
- return strchr(" `\"#$%^&*()=+{}[]:;'|\\<>", c);
- }
- static char *quote_special_chars(char *found)
- {
- int l = 0;
- char *s = xzalloc((strlen(found) + 1) * 2);
- while (*found) {
- if (is_special_char(*found))
- s[l++] = '\\';
- s[l++] = *found++;
- }
-
- return s;
- }
- static NOINLINE void input_tab(smallint *lastWasTab)
- {
- char *chosen_match;
- char *match_buf;
- size_t len_found;
-
- unsigned match_pfx_len = match_pfx_len;
- int find_type;
- # if ENABLE_UNICODE_SUPPORT
-
- int cursor_mb;
- # endif
- if (!(state->flags & TAB_COMPLETION))
- return;
- if (*lastWasTab) {
-
- if (num_matches > 0) {
-
- int sav_cursor = cursor;
- goto_new_line();
- showfiles();
- draw_full(command_len - sav_cursor);
- }
- return;
- }
- *lastWasTab = 1;
- chosen_match = NULL;
-
- match_buf = xmalloc(MAX_LINELEN * sizeof(int16_t));
- # if !ENABLE_UNICODE_SUPPORT
- save_string(match_buf, cursor + 1);
- # else
- {
- CHAR_T wc = command_ps[cursor];
- command_ps[cursor] = BB_NUL;
- save_string(match_buf, MAX_LINELEN);
- command_ps[cursor] = wc;
- cursor_mb = strlen(match_buf);
- }
- # endif
- find_type = build_match_prefix(match_buf);
-
- free_tab_completion_data();
- # if ENABLE_FEATURE_USERNAME_COMPLETION
-
- if (state->flags & USERNAME_COMPLETION)
- if (match_buf[0] == '~' && strchr(match_buf, '/') == NULL)
- match_pfx_len = complete_username(match_buf);
- # endif
-
- if (!matches)
- match_pfx_len = complete_cmd_dir_file(match_buf, find_type);
-
- {
- const char *e = match_buf + strlen(match_buf);
- const char *s = e - match_pfx_len;
- while (s < e)
- if (is_special_char(*s++))
- match_pfx_len++;
- }
-
- if (matches) {
- unsigned i, n = 0;
- qsort_string_vector(matches, num_matches);
- for (i = 0; i < num_matches - 1; ++i) {
-
- if (strcmp(matches[i], matches[i+1]) == 0) {
- free(matches[i]);
-
- } else {
- matches[n++] = matches[i];
- }
-
- }
- matches[n++] = matches[i];
- num_matches = n;
- }
-
- if (num_matches != 1) {
- char *cp;
- beep();
- if (!matches)
- goto ret;
-
- chosen_match = xstrdup(matches[0]);
- for (cp = chosen_match; *cp; cp++) {
- unsigned n;
- for (n = 1; n < num_matches; n++) {
- if (matches[n][cp - chosen_match] != *cp) {
- goto stop;
- }
- }
- }
- stop:
- if (cp == chosen_match) {
- goto ret;
- }
- *cp = '\0';
- cp = quote_special_chars(chosen_match);
- free(chosen_match);
- chosen_match = cp;
- len_found = strlen(chosen_match);
- } else {
-
- *lastWasTab = 0;
- chosen_match = quote_special_chars(matches[0]);
- len_found = strlen(chosen_match);
- if (chosen_match[len_found-1] != '/') {
- chosen_match[len_found] = ' ';
- chosen_match[++len_found] = '\0';
- }
- }
- # if !ENABLE_UNICODE_SUPPORT
-
-
-
-
- if ((int)(len_found - match_pfx_len + command_len) < S.maxsize) {
- int pos;
-
- strcpy(match_buf, &command_ps[cursor]);
-
- sprintf(&command_ps[cursor], "%s%s", chosen_match + match_pfx_len, match_buf);
- command_len = strlen(command_ps);
-
- pos = cursor + len_found - match_pfx_len;
-
- redraw(cmdedit_y, command_len - pos);
- }
- # else
- {
-
- char *command = match_buf + MAX_LINELEN;
- int len = save_string(command, MAX_LINELEN);
-
-
- if ((int)(len_found - match_pfx_len + len) < MAX_LINELEN) {
- int pos;
-
- strcpy(match_buf, &command[cursor_mb]);
-
- strcpy(&command[cursor_mb], chosen_match + match_pfx_len);
- len = load_string(command);
-
- sprintf(&command[cursor_mb], "%s%s", chosen_match + match_pfx_len, match_buf);
- command_len = load_string(command);
-
-
- pos = command_len - len;
- redraw(cmdedit_y, pos >= 0 ? pos : 0);
- }
- }
- # endif
- ret:
- free(chosen_match);
- free(match_buf);
- }
- #endif
- line_input_t* FAST_FUNC new_line_input_t(int flags)
- {
- line_input_t *n = xzalloc(sizeof(*n));
- n->flags = flags;
- n->timeout = -1;
- #if MAX_HISTORY > 0
- n->max_history = MAX_HISTORY;
- #endif
- return n;
- }
- #if MAX_HISTORY > 0
- unsigned FAST_FUNC size_from_HISTFILESIZE(const char *hp)
- {
- int size = MAX_HISTORY;
- if (hp) {
- size = atoi(hp);
- if (size <= 0)
- return 1;
- if (size > MAX_HISTORY)
- return MAX_HISTORY;
- }
- return size;
- }
- static void save_command_ps_at_cur_history(void)
- {
- if (command_ps[0] != BB_NUL) {
- int cur = state->cur_history;
- free(state->history[cur]);
- # if ENABLE_UNICODE_SUPPORT
- {
- char tbuf[MAX_LINELEN];
- save_string(tbuf, sizeof(tbuf));
- state->history[cur] = xstrdup(tbuf);
- }
- # else
- state->history[cur] = xstrdup(command_ps);
- # endif
- }
- }
- static int get_previous_history(void)
- {
- if ((state->flags & DO_HISTORY) && state->cur_history) {
- save_command_ps_at_cur_history();
- state->cur_history--;
- return 1;
- }
- beep();
- return 0;
- }
- static int get_next_history(void)
- {
- if (state->flags & DO_HISTORY) {
- if (state->cur_history < state->cnt_history) {
- save_command_ps_at_cur_history();
- return ++state->cur_history;
- }
- }
- beep();
- return 0;
- }
- void FAST_FUNC show_history(const line_input_t *st)
- {
- int i;
- if (!st)
- return;
- for (i = 0; i < st->cnt_history; i++)
- printf("%4d %s\n", i, st->history[i]);
- }
- # if ENABLE_FEATURE_EDITING_SAVEHISTORY
- static void free_line_input_t(line_input_t *n)
- {
- int i = n->cnt_history;
- while (i > 0)
- free(n->history[--i]);
- free(n);
- }
- static void load_history(line_input_t *st_parm)
- {
- char *temp_h[MAX_HISTORY];
- char *line;
- FILE *fp;
- unsigned idx, i, line_len;
-
- fp = fopen_for_read(st_parm->hist_file);
- if (fp) {
-
- for (idx = st_parm->cnt_history; idx > 0;) {
- idx--;
- free(st_parm->history[idx]);
- st_parm->history[idx] = NULL;
- }
-
- memset(temp_h, 0, sizeof(temp_h));
- idx = 0;
- st_parm->cnt_history_in_file = 0;
- while ((line = xmalloc_fgetline(fp)) != NULL) {
- if (line[0] == '\0') {
- free(line);
- continue;
- }
- free(temp_h[idx]);
- temp_h[idx] = line;
- st_parm->cnt_history_in_file++;
- idx++;
- if (idx == st_parm->max_history)
- idx = 0;
- }
- fclose(fp);
-
- if (st_parm->cnt_history_in_file) {
- while (temp_h[idx] == NULL) {
- idx++;
- if (idx == st_parm->max_history)
- idx = 0;
- }
- }
-
- for (i = 0; i < st_parm->max_history;) {
- line = temp_h[idx];
- if (!line)
- break;
- idx++;
- if (idx == st_parm->max_history)
- idx = 0;
- line_len = strlen(line);
- if (line_len >= MAX_LINELEN)
- line[MAX_LINELEN-1] = '\0';
- st_parm->history[i++] = line;
- }
- st_parm->cnt_history = i;
- if (ENABLE_FEATURE_EDITING_SAVE_ON_EXIT)
- st_parm->cnt_history_in_file = i;
- }
- }
- # if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
- void save_history(line_input_t *st)
- {
- FILE *fp;
- if (!st->hist_file)
- return;
- if (st->cnt_history <= st->cnt_history_in_file)
- return;
- fp = fopen(st->hist_file, "a");
- if (fp) {
- int i, fd;
- char *new_name;
- line_input_t *st_temp;
- for (i = st->cnt_history_in_file; i < st->cnt_history; i++)
- fprintf(fp, "%s\n", st->history[i]);
- fclose(fp);
-
- st_temp = new_line_input_t(st->flags);
- st_temp->hist_file = st->hist_file;
- st_temp->max_history = st->max_history;
- load_history(st_temp);
-
- new_name = xasprintf("%s.%u.new", st->hist_file, (int) getpid());
- fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
- if (fd >= 0) {
- fp = xfdopen_for_write(fd);
- for (i = 0; i < st_temp->cnt_history; i++)
- fprintf(fp, "%s\n", st_temp->history[i]);
- fclose(fp);
- if (rename(new_name, st->hist_file) == 0)
- st->cnt_history_in_file = st_temp->cnt_history;
- }
- free(new_name);
- free_line_input_t(st_temp);
- }
- }
- # else
- static void save_history(char *str)
- {
- int fd;
- int len, len2;
- if (!state->hist_file)
- return;
- fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0600);
- if (fd < 0)
- return;
- xlseek(fd, 0, SEEK_END);
- len = strlen(str);
- str[len] = '\n';
- len2 = full_write(fd, str, len + 1);
- str[len] = '\0';
- close(fd);
- if (len2 != len + 1)
- return;
-
- state->cnt_history_in_file++;
- if (state->cnt_history_in_file > state->max_history * 4) {
- char *new_name;
- line_input_t *st_temp;
-
- st_temp = new_line_input_t(state->flags);
- st_temp->hist_file = state->hist_file;
- st_temp->max_history = state->max_history;
- load_history(st_temp);
-
- new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
- fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
- if (fd >= 0) {
- FILE *fp;
- int i;
- fp = xfdopen_for_write(fd);
- for (i = 0; i < st_temp->cnt_history; i++)
- fprintf(fp, "%s\n", st_temp->history[i]);
- fclose(fp);
- if (rename(new_name, state->hist_file) == 0)
- state->cnt_history_in_file = st_temp->cnt_history;
- }
- free(new_name);
- free_line_input_t(st_temp);
- }
- }
- # endif
- # else
- # define load_history(a) ((void)0)
- # define save_history(a) ((void)0)
- # endif
- static void remember_in_history(char *str)
- {
- int i;
- if (!(state->flags & DO_HISTORY))
- return;
- if (str[0] == '\0')
- return;
- i = state->cnt_history;
-
- if (i && strcmp(state->history[i-1], str) == 0)
- return;
- free(state->history[state->max_history]);
- state->history[state->max_history] = NULL;
-
-
- if (i >= state->max_history) {
- free(state->history[0]);
- for (i = 0; i < state->max_history-1; i++)
- state->history[i] = state->history[i+1];
-
- # if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
- if (state->cnt_history_in_file)
- state->cnt_history_in_file--;
- # endif
- }
-
- state->history[i++] = xstrdup(str);
-
- state->cur_history = i;
- state->cnt_history = i;
- # if ENABLE_FEATURE_EDITING_SAVEHISTORY && !ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
- save_history(str);
- # endif
- }
- #else
- # define remember_in_history(a) ((void)0)
- #endif
- #if ENABLE_FEATURE_EDITING_VI
- static void
- vi_Word_motion(int eat)
- {
- CHAR_T *command = command_ps;
- while (cursor < command_len && !BB_isspace(command[cursor]))
- input_forward();
- if (eat) while (cursor < command_len && BB_isspace(command[cursor]))
- input_forward();
- }
- static void
- vi_word_motion(int eat)
- {
- CHAR_T *command = command_ps;
- if (BB_isalnum_or_underscore(command[cursor])) {
- while (cursor < command_len
- && (BB_isalnum_or_underscore(command[cursor+1]))
- ) {
- input_forward();
- }
- } else if (BB_ispunct(command[cursor])) {
- while (cursor < command_len && BB_ispunct(command[cursor+1]))
- input_forward();
- }
- if (cursor < command_len)
- input_forward();
- if (eat) {
- while (cursor < command_len && BB_isspace(command[cursor]))
- input_forward();
- }
- }
- static void
- vi_End_motion(void)
- {
- CHAR_T *command = command_ps;
- input_forward();
- while (cursor < command_len && BB_isspace(command[cursor]))
- input_forward();
- while (cursor < command_len-1 && !BB_isspace(command[cursor+1]))
- input_forward();
- }
- static void
- vi_end_motion(void)
- {
- CHAR_T *command = command_ps;
- if (cursor >= command_len-1)
- return;
- input_forward();
- while (cursor < command_len-1 && BB_isspace(command[cursor]))
- input_forward();
- if (cursor >= command_len-1)
- return;
- if (BB_isalnum_or_underscore(command[cursor])) {
- while (cursor < command_len-1
- && (BB_isalnum_or_underscore(command[cursor+1]))
- ) {
- input_forward();
- }
- } else if (BB_ispunct(command[cursor])) {
- while (cursor < command_len-1 && BB_ispunct(command[cursor+1]))
- input_forward();
- }
- }
- static void
- vi_Back_motion(void)
- {
- CHAR_T *command = command_ps;
- while (cursor > 0 && BB_isspace(command[cursor-1]))
- input_backward(1);
- while (cursor > 0 && !BB_isspace(command[cursor-1]))
- input_backward(1);
- }
- static void
- vi_back_motion(void)
- {
- CHAR_T *command = command_ps;
- if (cursor <= 0)
- return;
- input_backward(1);
- while (cursor > 0 && BB_isspace(command[cursor]))
- input_backward(1);
- if (cursor <= 0)
- return;
- if (BB_isalnum_or_underscore(command[cursor])) {
- while (cursor > 0
- && (BB_isalnum_or_underscore(command[cursor-1]))
- ) {
- input_backward(1);
- }
- } else if (BB_ispunct(command[cursor])) {
- while (cursor > 0 && BB_ispunct(command[cursor-1]))
- input_backward(1);
- }
- }
- #endif
- static void ctrl_left(void)
- {
- CHAR_T *command = command_ps;
- while (1) {
- CHAR_T c;
- input_backward(1);
- if (cursor == 0)
- break;
- c = command[cursor];
- if (c != ' ' && !BB_ispunct(c)) {
-
- while (1) {
- c = command[cursor - 1];
- if (c == ' ' || BB_ispunct(c))
- break;
- input_backward(1);
- if (cursor == 0)
- break;
- }
- break;
- }
- }
- }
- static void ctrl_right(void)
- {
- CHAR_T *command = command_ps;
- while (1) {
- CHAR_T c;
- c = command[cursor];
- if (c == BB_NUL)
- break;
- if (c != ' ' && !BB_ispunct(c)) {
-
- while (1) {
- input_forward();
- c = command[cursor];
- if (c == BB_NUL || c == ' ' || BB_ispunct(c))
- break;
- }
- break;
- }
- input_forward();
- }
- }
- #if ENABLE_FEATURE_EDITING_ASK_TERMINAL
- static void ask_terminal(void)
- {
-
-
- struct pollfd pfd;
- pfd.fd = STDIN_FILENO;
- pfd.events = POLLIN;
- if (safe_poll(&pfd, 1, 0) == 0) {
- S.sent_ESC_br6n = 1;
- fputs(ESC"[6n", stdout);
- fflush_all();
- }
- }
- #else
- #define ask_terminal() ((void)0)
- #endif
- #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
- static void parse_and_put_prompt(const char *prmt_ptr)
- {
- const char *p;
- cmdedit_prompt = prompt_last_line = prmt_ptr;
- p = strrchr(prmt_ptr, '\n');
- if (p)
- prompt_last_line = p + 1;
- cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
- put_prompt();
- }
- #else
- static void parse_and_put_prompt(const char *prmt_ptr)
- {
- int prmt_size = 0;
- char *prmt_mem_ptr = xzalloc(1);
- # if ENABLE_USERNAME_OR_HOMEDIR
- char *cwd_buf = NULL;
- # endif
- char flg_not_length = '[';
- char cbuf[2];
-
- cbuf[1] = '\0';
- while (*prmt_ptr) {
- char timebuf[sizeof("HH:MM:SS")];
- char *free_me = NULL;
- char *pbuf;
- char c;
- pbuf = cbuf;
- c = *prmt_ptr++;
- if (c == '\\') {
- const char *cp;
- int l;
- cp = prmt_ptr;
- c = *cp;
- if (c != 't')
- c = bb_process_escape_sequence(&prmt_ptr);
- if (prmt_ptr == cp) {
- if (*cp == '\0')
- break;
- c = *prmt_ptr++;
- switch (c) {
- # if ENABLE_USERNAME_OR_HOMEDIR
- case 'u':
- pbuf = user_buf ? user_buf : (char*)"";
- break;
- # endif
- case 'H':
- case 'h':
- pbuf = free_me = safe_gethostname();
- if (c == 'h')
- strchrnul(pbuf, '.')[0] = '\0';
- break;
- case '$':
- c = (geteuid() == 0 ? '#' : '$');
- break;
- case 'T':
- case '@':
- case 'A':
- case 't':
-
- strftime_HHMMSS(timebuf, sizeof(timebuf), NULL)[-3] = '\0';
- pbuf = timebuf;
- break;
- # if ENABLE_USERNAME_OR_HOMEDIR
- case 'w':
- case 'W':
- if (!cwd_buf) {
- cwd_buf = xrealloc_getcwd_or_warn(NULL);
- if (!cwd_buf)
- cwd_buf = (char *)bb_msg_unknown;
- else if (home_pwd_buf[0]) {
- char *after_home_user;
-
- after_home_user = is_prefixed_with(cwd_buf, home_pwd_buf);
- if (after_home_user
- && (*after_home_user == '/' || *after_home_user == '\0')
- ) {
- cwd_buf[0] = '~';
- overlapping_strcpy(cwd_buf + 1, after_home_user);
- }
- }
- }
- pbuf = cwd_buf;
- if (c == 'w')
- break;
- cp = strrchr(pbuf, '/');
- if (cp)
- pbuf = (char*)cp + 1;
- break;
- # endif
- case 'x': case 'X': {
- char buf2[4];
- for (l = 0; l < 3;) {
- unsigned h;
- buf2[l++] = *prmt_ptr;
- buf2[l] = '\0';
- h = strtoul(buf2, &pbuf, 16);
- if (h > UCHAR_MAX || (pbuf - buf2) < l) {
- buf2[--l] = '\0';
- break;
- }
- prmt_ptr++;
- }
- c = (char)strtoul(buf2, NULL, 16);
- if (c == 0)
- c = '?';
- pbuf = cbuf;
- break;
- }
- case '[': case ']':
- if (c == flg_not_length) {
-
- flg_not_length ^= 6;
- continue;
- }
- break;
- }
- }
- }
- cbuf[0] = c;
- {
- int n = strlen(pbuf);
- prmt_size += n;
- if (c == '\n')
- cmdedit_prmt_len = 0;
- else if (flg_not_length != ']') {
- #if 0
- cmdedit_prmt_len += unicode_strwidth(pbuf);
- #else
- cmdedit_prmt_len += n;
- #endif
- }
- }
- prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_size+1), pbuf);
- free(free_me);
- }
- # if ENABLE_USERNAME_OR_HOMEDIR
- if (cwd_buf != (char *)bb_msg_unknown)
- free(cwd_buf);
- # endif
-
- cmdedit_prompt = prompt_last_line = prmt_mem_ptr;
- prmt_ptr = strrchr(cmdedit_prompt, '\n');
- if (prmt_ptr)
- prompt_last_line = prmt_ptr + 1;
- put_prompt();
- }
- #endif
- static void cmdedit_setwidth(void)
- {
- int new_y;
- cmdedit_termw = get_terminal_width(STDIN_FILENO);
-
- new_y = (cursor + cmdedit_prmt_len) / cmdedit_termw;
-
- redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
- }
- static void win_changed(int nsig UNUSED_PARAM)
- {
- if (S.ok_to_redraw) {
-
- int sv_errno = errno;
- cmdedit_setwidth();
- fflush_all();
- errno = sv_errno;
- } else {
-
- S.SIGWINCH_count++;
- }
- }
- static int lineedit_read_key(char *read_key_buffer, int timeout)
- {
- int64_t ic;
- #if ENABLE_UNICODE_SUPPORT
- char unicode_buf[MB_CUR_MAX + 1];
- int unicode_idx = 0;
- #endif
- fflush_all();
- while (1) {
-
- S.ok_to_redraw = 1;
- ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
- S.ok_to_redraw = 0;
- if (errno) {
- #if ENABLE_UNICODE_SUPPORT
- if (errno == EAGAIN && unicode_idx != 0)
- goto pushback;
- #endif
- break;
- }
- #if ENABLE_FEATURE_EDITING_ASK_TERMINAL
- if ((int32_t)ic == KEYCODE_CURSOR_POS
- && S.sent_ESC_br6n
- ) {
- S.sent_ESC_br6n = 0;
- if (cursor == 0) {
- int col = ((ic >> 32) & 0x7fff) - 1;
-
- if ((int)(col - cmdedit_prmt_len) > 0) {
-
- cmdedit_x += (col - cmdedit_prmt_len);
- while (cmdedit_x >= cmdedit_termw) {
- cmdedit_x -= cmdedit_termw;
- cmdedit_y++;
- }
- }
- }
- continue;
- }
- #endif
- #if ENABLE_UNICODE_SUPPORT
- if (unicode_status == UNICODE_ON) {
- wchar_t wc;
- if ((int32_t)ic < 0)
- break;
-
- unicode_buf[unicode_idx++] = ic;
- unicode_buf[unicode_idx] = '\0';
- if (mbstowcs(&wc, unicode_buf, 1) != 1) {
-
- if (unicode_idx < MB_CUR_MAX) {
- timeout = 50;
- continue;
- }
- pushback:
-
- read_key_ungets(read_key_buffer, unicode_buf + 1, unicode_idx - 1);
- # if !ENABLE_UNICODE_PRESERVE_BROKEN
- ic = CONFIG_SUBST_WCHAR;
- # else
- ic = unicode_mark_raw_byte(unicode_buf[0]);
- # endif
- } else {
-
- ic = wc;
- }
- }
- #endif
- break;
- }
- return ic;
- }
- #if ENABLE_UNICODE_BIDI_SUPPORT
- static int isrtl_str(void)
- {
- int idx = cursor;
- while (idx < command_len && unicode_bidi_is_neutral_wchar(command_ps[idx]))
- idx++;
- return unicode_bidi_isrtl(command_ps[idx]);
- }
- #else
- # define isrtl_str() 0
- #endif
- #define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel)
- #undef CTRL
- #define CTRL(a) ((a) & ~0x40)
- enum {
- VI_CMDMODE_BIT = 0x40000000,
-
- };
- #if ENABLE_FEATURE_REVERSE_SEARCH
- static int32_t reverse_i_search(int timeout)
- {
- char match_buf[128];
- char read_key_buffer[KEYCODE_BUFFER_SIZE];
- const char *matched_history_line;
- const char *saved_prompt;
- unsigned saved_prmt_len;
- int32_t ic;
- matched_history_line = NULL;
- read_key_buffer[0] = 0;
- match_buf[0] = '\0';
-
- saved_prompt = prompt_last_line;
- saved_prmt_len = cmdedit_prmt_len;
- goto set_prompt;
- while (1) {
- int h;
- unsigned match_buf_len = strlen(match_buf);
- ic = lineedit_read_key(read_key_buffer, timeout);
- switch (ic) {
- case CTRL('R'):
- break;
- case '\b':
- case '\x7f':
-
- if (unicode_status == UNICODE_ON) {
- while (match_buf_len != 0) {
- uint8_t c = match_buf[--match_buf_len];
- if ((c & 0xc0) != 0x80)
- break;
- }
- } else {
- if (match_buf_len != 0)
- match_buf_len--;
- }
- match_buf[match_buf_len] = '\0';
- break;
- default:
- if (ic < ' '
- || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
- || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
- ) {
- goto ret;
- }
-
- #if ENABLE_UNICODE_SUPPORT
- if (unicode_status == UNICODE_ON) {
- mbstate_t mbstate = { 0 };
- char buf[MB_CUR_MAX + 1];
- int len = wcrtomb(buf, ic, &mbstate);
- if (len > 0) {
- buf[len] = '\0';
- if (match_buf_len + len < sizeof(match_buf))
- strcpy(match_buf + match_buf_len, buf);
- }
- } else
- #endif
- if (match_buf_len < sizeof(match_buf) - 1) {
- match_buf[match_buf_len] = ic;
- match_buf[match_buf_len + 1] = '\0';
- }
- break;
- }
-
- h = state->cur_history;
- if (ic == CTRL('R'))
- h--;
- while (h >= 0) {
- if (state->history[h]) {
- char *match = strstr(state->history[h], match_buf);
- if (match) {
- state->cur_history = h;
- matched_history_line = state->history[h];
- command_len = load_string(matched_history_line);
- cursor = match - matched_history_line;
- free((char*)prompt_last_line);
- set_prompt:
- prompt_last_line = xasprintf("(reverse-i-search)'%s': ", match_buf);
- cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
- goto do_redraw;
- }
- }
- h--;
- }
-
- match_buf[match_buf_len] = '\0';
- beep();
- continue;
- do_redraw:
- redraw(cmdedit_y, command_len - cursor);
- }
- ret:
- if (matched_history_line)
- command_len = load_string(matched_history_line);
- free((char*)prompt_last_line);
- prompt_last_line = saved_prompt;
- cmdedit_prmt_len = saved_prmt_len;
- redraw(cmdedit_y, command_len - cursor);
- return ic;
- }
- #endif
- int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize)
- {
- int len, n;
- int timeout;
- #if ENABLE_FEATURE_TAB_COMPLETION
- smallint lastWasTab = 0;
- #endif
- smallint break_out = 0;
- #if ENABLE_FEATURE_EDITING_VI
- smallint vi_cmdmode = 0;
- #endif
- struct termios initial_settings;
- struct termios new_settings;
- char read_key_buffer[KEYCODE_BUFFER_SIZE];
- INIT_S();
- n = get_termios_and_make_raw(STDIN_FILENO, &new_settings, &initial_settings, 0
- | TERMIOS_CLEAR_ISIG
- );
- if (n != 0 || (initial_settings.c_lflag & (ECHO|ICANON)) == ICANON) {
-
- parse_and_put_prompt(prompt);
- fflush_all();
- if (fgets(command, maxsize, stdin) == NULL)
- len = -1;
- else
- len = strlen(command);
- DEINIT_S();
- return len;
- }
- init_unicode();
- if (maxsize > MAX_LINELEN)
- maxsize = MAX_LINELEN;
- S.maxsize = maxsize;
- timeout = -1;
-
- state = (line_input_t*) &const_int_0;
- if (st) {
- state = st;
- timeout = st->timeout;
- }
- #if MAX_HISTORY > 0
- # if ENABLE_FEATURE_EDITING_SAVEHISTORY
- if (state->hist_file)
- if (state->cnt_history == 0)
- load_history(state);
- # endif
- if (state->flags & DO_HISTORY)
- state->cur_history = state->cnt_history;
- #endif
-
- cmdedit_y = 0;
- command_len = 0;
- #if ENABLE_UNICODE_SUPPORT
- command_ps = xzalloc(maxsize * sizeof(command_ps[0]));
- #else
- command_ps = command;
- command[0] = '\0';
- #endif
- #define command command_must_not_be_used
- tcsetattr_stdin_TCSANOW(&new_settings);
- #if ENABLE_USERNAME_OR_HOMEDIR
- {
- struct passwd *entry;
- entry = getpwuid(geteuid());
- if (entry) {
- user_buf = xstrdup(entry->pw_name);
- home_pwd_buf = xstrdup(entry->pw_dir);
- }
- }
- #endif
- #if 0
- for (i = 0; i <= state->max_history; i++)
- bb_error_msg("history[%d]:'%s'", i, state->history[i]);
- bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
- #endif
-
- cmdedit_termw = get_terminal_width(STDIN_FILENO);
-
- parse_and_put_prompt(prompt);
- ask_terminal();
-
- S.SIGWINCH_handler.sa_handler = win_changed;
- S.SIGWINCH_handler.sa_flags = SA_RESTART;
- sigaction(SIGWINCH, &S.SIGWINCH_handler, &S.SIGWINCH_handler);
- read_key_buffer[0] = 0;
- while (1) {
-
- int32_t ic, ic_raw;
- unsigned count;
- count = S.SIGWINCH_count;
- if (S.SIGWINCH_saved != count) {
- S.SIGWINCH_saved = count;
- cmdedit_setwidth();
- }
- ic = ic_raw = lineedit_read_key(read_key_buffer, timeout);
- #if ENABLE_FEATURE_REVERSE_SEARCH
- again:
- #endif
- #if ENABLE_FEATURE_EDITING_VI
- newdelflag = 1;
- if (vi_cmdmode) {
-
- ic |= VI_CMDMODE_BIT;
- }
- #endif
- switch (ic) {
- case '\n':
- case '\r':
- vi_case('\n'|VI_CMDMODE_BIT:)
- vi_case('\r'|VI_CMDMODE_BIT:)
-
- goto_new_line();
- break_out = 1;
- break;
- case CTRL('A'):
- vi_case('0'|VI_CMDMODE_BIT:)
-
- input_backward(cursor);
- break;
- case CTRL('B'):
- vi_case('h'|VI_CMDMODE_BIT:)
- vi_case('\b'|VI_CMDMODE_BIT:)
- vi_case('\x7f'|VI_CMDMODE_BIT:)
- input_backward(1);
- break;
- case CTRL('E'):
- vi_case('$'|VI_CMDMODE_BIT:)
-
- put_till_end_and_adv_cursor();
- break;
- case CTRL('F'):
- vi_case('l'|VI_CMDMODE_BIT:)
- vi_case(' '|VI_CMDMODE_BIT:)
- input_forward();
- break;
- case '\b':
- case '\x7f':
- if (!isrtl_str())
- input_backspace();
- else
- input_delete(0);
- break;
- case KEYCODE_DELETE:
- if (!isrtl_str())
- input_delete(0);
- else
- input_backspace();
- break;
- #if ENABLE_FEATURE_TAB_COMPLETION
- case '\t':
- input_tab(&lastWasTab);
- break;
- #endif
- case CTRL('K'):
-
- command_ps[cursor] = BB_NUL;
- command_len = cursor;
- printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
- break;
- case CTRL('L'):
- vi_case(CTRL('L')|VI_CMDMODE_BIT:)
-
-
- printf(ESC"[H" ESC"[J");
- draw_full(command_len - cursor);
- break;
- #if MAX_HISTORY > 0
- case CTRL('N'):
- vi_case(CTRL('N')|VI_CMDMODE_BIT:)
- vi_case('j'|VI_CMDMODE_BIT:)
-
- if (get_next_history())
- goto rewrite_line;
- break;
- case CTRL('P'):
- vi_case(CTRL('P')|VI_CMDMODE_BIT:)
- vi_case('k'|VI_CMDMODE_BIT:)
-
- if (get_previous_history())
- goto rewrite_line;
- break;
- #endif
- case CTRL('U'):
- vi_case(CTRL('U')|VI_CMDMODE_BIT:)
-
- if (cursor) {
- command_len -= cursor;
- memmove(command_ps, command_ps + cursor,
- (command_len + 1) * sizeof(command_ps[0]));
- redraw(cmdedit_y, command_len);
- }
- break;
- case CTRL('W'):
- vi_case(CTRL('W')|VI_CMDMODE_BIT:)
-
- while (cursor > 0 && BB_isspace(command_ps[cursor-1]))
- input_backspace();
- while (cursor > 0 && !BB_isspace(command_ps[cursor-1]))
- input_backspace();
- break;
- case KEYCODE_ALT_D: {
-
- int nc, sc = cursor;
- ctrl_right();
- nc = cursor - sc;
- input_backward(nc);
- while (--nc >= 0)
- input_delete(1);
- break;
- }
- case KEYCODE_ALT_BACKSPACE: {
-
- int sc = cursor;
- ctrl_left();
- while (sc-- > cursor)
- input_delete(1);
- break;
- }
- #if ENABLE_FEATURE_REVERSE_SEARCH
- case CTRL('R'):
- ic = ic_raw = reverse_i_search(timeout);
- goto again;
- #endif
- #if ENABLE_FEATURE_EDITING_VI
- case 'i'|VI_CMDMODE_BIT:
- vi_cmdmode = 0;
- break;
- case 'I'|VI_CMDMODE_BIT:
- input_backward(cursor);
- vi_cmdmode = 0;
- break;
- case 'a'|VI_CMDMODE_BIT:
- input_forward();
- vi_cmdmode = 0;
- break;
- case 'A'|VI_CMDMODE_BIT:
- put_till_end_and_adv_cursor();
- vi_cmdmode = 0;
- break;
- case 'x'|VI_CMDMODE_BIT:
- input_delete(1);
- break;
- case 'X'|VI_CMDMODE_BIT:
- if (cursor > 0) {
- input_backward(1);
- input_delete(1);
- }
- break;
- case 'W'|VI_CMDMODE_BIT:
- vi_Word_motion(1);
- break;
- case 'w'|VI_CMDMODE_BIT:
- vi_word_motion(1);
- break;
- case 'E'|VI_CMDMODE_BIT:
- vi_End_motion();
- break;
- case 'e'|VI_CMDMODE_BIT:
- vi_end_motion();
- break;
- case 'B'|VI_CMDMODE_BIT:
- vi_Back_motion();
- break;
- case 'b'|VI_CMDMODE_BIT:
- vi_back_motion();
- break;
- case 'C'|VI_CMDMODE_BIT:
- vi_cmdmode = 0;
-
- case 'D'|VI_CMDMODE_BIT:
- goto clear_to_eol;
- case 'c'|VI_CMDMODE_BIT:
- vi_cmdmode = 0;
-
- case 'd'|VI_CMDMODE_BIT: {
- int nc, sc;
- ic = lineedit_read_key(read_key_buffer, timeout);
- if (errno)
- goto return_error_indicator;
- if (ic == ic_raw) {
- input_backward(cursor);
- goto clear_to_eol;
- break;
- }
- sc = cursor;
- switch (ic) {
- case 'w':
- case 'W':
- case 'e':
- case 'E':
- switch (ic) {
- case 'w':
- vi_word_motion(vi_cmdmode);
- break;
- case 'W':
- vi_Word_motion(vi_cmdmode);
- break;
- case 'e':
- vi_end_motion();
- input_forward();
- break;
- case 'E':
- vi_End_motion();
- input_forward();
- break;
- }
- nc = cursor;
- input_backward(cursor - sc);
- while (nc-- > cursor)
- input_delete(1);
- break;
- case 'b':
- case 'B':
- if (ic == 'b')
- vi_back_motion();
- else
- vi_Back_motion();
- while (sc-- > cursor)
- input_delete(1);
- break;
- case ' ':
- input_delete(1);
- break;
- case '$':
- clear_to_eol:
- while (cursor < command_len)
- input_delete(1);
- break;
- }
- break;
- }
- case 'p'|VI_CMDMODE_BIT:
- input_forward();
-
- case 'P'|VI_CMDMODE_BIT:
- put();
- break;
- case 'r'|VI_CMDMODE_BIT:
- ic = lineedit_read_key(read_key_buffer, timeout);
- if (errno)
- goto return_error_indicator;
- if (ic < ' ' || ic > 255) {
- beep();
- } else {
- command_ps[cursor] = ic;
- bb_putchar(ic);
- bb_putchar('\b');
- }
- break;
- case '\x1b':
- if (state->flags & VI_MODE) {
-
- vi_cmdmode = 1;
- input_backward(1);
- }
- break;
- #endif
- #if MAX_HISTORY > 0
- case KEYCODE_UP:
- if (get_previous_history())
- goto rewrite_line;
- beep();
- break;
- case KEYCODE_DOWN:
- if (!get_next_history())
- break;
- rewrite_line:
-
-
- command_len = load_string(state->history[state->cur_history] ?
- state->history[state->cur_history] : "");
-
- redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
- break;
- #endif
- case KEYCODE_RIGHT:
- input_forward();
- break;
- case KEYCODE_LEFT:
- input_backward(1);
- break;
- case KEYCODE_CTRL_LEFT:
- case KEYCODE_ALT_LEFT:
- ctrl_left();
- break;
- case KEYCODE_CTRL_RIGHT:
- case KEYCODE_ALT_RIGHT:
- ctrl_right();
- break;
- case KEYCODE_HOME:
- input_backward(cursor);
- break;
- case KEYCODE_END:
- put_till_end_and_adv_cursor();
- break;
- default:
- if (initial_settings.c_cc[VINTR] != 0
- && ic_raw == initial_settings.c_cc[VINTR]
- ) {
-
- command_len = 0;
- break_out = -1;
- break;
- }
- if (initial_settings.c_cc[VEOF] != 0
- && ic_raw == initial_settings.c_cc[VEOF]
- ) {
-
- if (command_len == 0) {
- errno = 0;
- case -1:
- IF_FEATURE_EDITING_VI(return_error_indicator:)
- break_out = command_len = -1;
- break;
- }
- input_delete(0);
- break;
- }
- if (ic < ' '
- || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
- || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
- ) {
-
- break;
- }
- if ((int)command_len >= (maxsize - 2)) {
-
- break;
- }
- command_len++;
- if (cursor == (command_len - 1)) {
-
- command_ps[cursor] = ic;
- command_ps[cursor + 1] = BB_NUL;
- put_cur_glyph_and_inc_cursor();
- if (unicode_bidi_isrtl(ic))
- input_backward(1);
- } else {
-
- int sc = cursor;
- memmove(command_ps + sc + 1, command_ps + sc,
- (command_len - sc) * sizeof(command_ps[0]));
- command_ps[sc] = ic;
-
- if (!isrtl_str())
- sc++;
- put_till_end_and_adv_cursor();
-
- input_backward(cursor - sc);
- }
- break;
- }
- if (break_out)
- break;
- #if ENABLE_FEATURE_TAB_COMPLETION
- if (ic_raw != '\t')
- lastWasTab = 0;
- #endif
- }
- #if ENABLE_FEATURE_EDITING_ASK_TERMINAL
- if (S.sent_ESC_br6n) {
-
- usleep(20*1000);
- }
- #endif
- #undef command
- #if ENABLE_UNICODE_SUPPORT
- command[0] = '\0';
- if (command_len > 0)
- command_len = save_string(command, maxsize - 1);
- free(command_ps);
- #endif
- if (command_len > 0) {
- remember_in_history(command);
- }
- if (break_out > 0) {
- command[command_len++] = '\n';
- command[command_len] = '\0';
- }
- #if ENABLE_FEATURE_TAB_COMPLETION
- free_tab_completion_data();
- #endif
-
- tcsetattr_stdin_TCSANOW(&initial_settings);
-
- sigaction_set(SIGWINCH, &S.SIGWINCH_handler);
- fflush_all();
- len = command_len;
- DEINIT_S();
- return len;
- }
- #else
- #undef read_line_input
- int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
- {
- fputs(prompt, stdout);
- fflush_all();
- if (!fgets(command, maxsize, stdin))
- return -1;
- return strlen(command);
- }
- #endif
- #ifdef TEST
- #include <locale.h>
- const char *applet_name = "debug stuff usage";
- int main(int argc, char **argv)
- {
- char buff[MAX_LINELEN];
- char *prompt =
- #if ENABLE_FEATURE_EDITING_FANCY_PROMPT
- "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
- "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
- "\\!\\[\\e[36;1m\\]\\$ \\[\\E[m\\]";
- #else
- "% ";
- #endif
- while (1) {
- int l;
- l = read_line_input(prompt, buff);
- if (l <= 0 || buff[l-1] != '\n')
- break;
- buff[l-1] = '\0';
- printf("*** read_line_input() returned line =%s=\n", buff);
- }
- printf("*** read_line_input() detect ^D\n");
- return 0;
- }
- #endif
|