| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943 |
- /* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch.
- Commands are spawned as separate processes for preemptive multitasking. */
- #include "io.h"
- #include "dbg.h"
- #include "kdbg.h"
- #include "kexec.h"
- #include "process.h"
- #include "terminal.h"
- #include "timer.h"
- extern unsigned char _binary_hello_elf_start[];
- extern unsigned char _binary_hello_elf_end[];
- extern void program_reboot_main(void);
- extern void program_poweroff_main(void);
- extern void program_ls_main(const char *path);
- extern void program_cat_main(const char *filename);
- extern void program_run_main(const char *filename);
- extern void program_save_main(const char *arg);
- extern void program_rm_main(const char *arg);
- extern void program_mkdir_main(const char *arg);
- extern void program_rmdir_main(const char *arg);
- extern void program_clock_main(void);
- extern void program_colors_main(void);
- extern void program_display_main(void);
- extern void program_cpuinfo_main(void);
- extern void program_mouseinfo_main(void);
- extern void program_serial_loop_main(const char *arg);
- extern void program_serial_recv_main(const char *arg);
- extern void program_run_serial_main(const char *arg);
- extern void program_run_embedded_main(const char *name);
- extern void program_lsusb_main(void);
- extern void program_storage_main(void);
- extern void program_mem_main(void);
- extern void program_format_main(const char *arg);
- extern void program_lspci_main(const char *filter);
- extern void program_peek_main(const char *arg);
- extern void program_poke_main(const char *arg);
- extern void program_netinfo_main(void);
- extern void program_netrecv_main(void);
- extern void program_netsend_main(void);
- extern void program_netip_main(const char *arg);
- extern void program_netlog_main(const char *arg);
- extern void program_netconsole_main(const char *arg);
- extern void program_dhcp_main(void);
- extern void program_netregs_main(void);
- extern void program_netloop_main(void);
- extern void program_netreset_main(void);
- extern void program_pong_main(void);
- extern void program_ping_main(const char *arg);
- extern void program_curl_main(const char *arg);
- extern void program_netdns_main(const char *arg);
- extern void program_host_main(const char *arg);
- /* Print the prompt (with the current directory) and set line_start. */
- static void term_print_prompt(struct term *t, int first)
- {
- if (!first)
- io_putchar('\n');
- io_putchar('[');
- io_putstr(t->cwd);
- io_putstr("] > ");
- t->line_start = io_get_cursor();
- }
- static int is_cmd_hello(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 5 > line_len)
- return 0;
- if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
- return 0;
- i += 5;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_reboot(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 6 > line_len)
- return 0;
- if (line[i] != 'r' || line[i+1] != 'e' || line[i+2] != 'b' || line[i+3] != 'o' || line[i+4] != 'o' || line[i+5] != 't')
- return 0;
- i += 6;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_poweroff(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 8 > line_len)
- return 0;
- if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'w' || line[i+3] != 'e' ||
- line[i+4] != 'r' || line[i+5] != 'o' || line[i+6] != 'f' || line[i+7] != 'f')
- return 0;
- i += 8;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_ls(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 2 > line_len)
- return 0;
- if (line[i] != 'l' || line[i+1] != 's')
- return 0;
- i += 2;
- if (i < line_len && line[i] != ' ')
- return 0; /* "ls" must be a whole word, so reject "lsusb" etc. */
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- path_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- path_buf[j++] = line[i++];
- path_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_cd(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 2 > line_len)
- return 0;
- if (line[i] != 'c' || line[i+1] != 'd')
- return 0;
- i += 2;
- if (i < line_len && line[i] != ' ')
- return 0; /* "cd" must be a whole word */
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- path_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- path_buf[j++] = line[i++];
- path_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_clock(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 5 > line_len)
- return 0;
- if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'o' ||
- line[i+3] != 'c' || line[i+4] != 'k')
- return 0;
- i += 5;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_colors(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 6 > line_len)
- return 0;
- if (line[i] != 'c' || line[i+1] != 'o' || line[i+2] != 'l' ||
- line[i+3] != 'o' || line[i+4] != 'r' || line[i+5] != 's')
- return 0;
- i += 6;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_display(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 7 > line_len)
- return 0;
- if (line[i] != 'd' || line[i+1] != 'i' || line[i+2] != 's' || line[i+3] != 'p' ||
- line[i+4] != 'l' || line[i+5] != 'a' || line[i+6] != 'y')
- return 0;
- i += 7;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_cpuinfo(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 7 > line_len)
- return 0;
- if (line[i] != 'c' || line[i+1] != 'p' || line[i+2] != 'u' || line[i+3] != 'i' ||
- line[i+4] != 'n' || line[i+5] != 'f' || line[i+6] != 'o')
- return 0;
- i += 7;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_mouseinfo(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 9 > line_len)
- return 0;
- if (line[i] != 'm' || line[i+1] != 'o' || line[i+2] != 'u' ||
- line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'i' ||
- line[i+6] != 'n' || line[i+7] != 'f' || line[i+8] != 'o')
- return 0;
- i += 9;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_lsusb(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 5 > line_len)
- return 0;
- if (line[i] != 'l' || line[i+1] != 's' || line[i+2] != 'u' ||
- line[i+3] != 's' || line[i+4] != 'b')
- return 0;
- i += 5;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_lspci(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 5 > line_len)
- return 0;
- if (line[i] != 'l' || line[i+1] != 's' || line[i+2] != 'p' ||
- line[i+3] != 'c' || line[i+4] != 'i')
- return 0;
- i += 5;
- if (i < line_len && line[i] != ' ')
- return 0; /* "lspci" must be a whole word */
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- arg_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_peek(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 4 > line_len)
- return 0;
- if (line[i] != 'p' || line[i+1] != 'e' || line[i+2] != 'e' ||
- line[i+3] != 'k')
- return 0;
- i += 4;
- if (i < line_len && line[i] != ' ')
- return 0; /* "peek" must be a whole word */
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- arg_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- /* poke <addr> <val>: write one dword to memory/MMIO. */
- static int is_cmd_poke(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 4 > line_len)
- return 0;
- if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'k' ||
- line[i+3] != 'e')
- return 0;
- i += 4;
- if (i < line_len && line[i] != ' ')
- return 0; /* "poke" must be a whole word */
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len)
- return 0; /* address and value required */
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_prog(const char *line, unsigned int line_len){
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 4 > line_len)
- return 0;
- if (line[i] != 'p' || line[i+1] != 'r' || line[i+2] != 'o' ||
- line[i+3] != 'g')
- return 0;
- i += 4;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_netinfo(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 7 > line_len)
- return 0;
- if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
- line[i+3] != 'i' || line[i+4] != 'n' || line[i+5] != 'f' ||
- line[i+6] != 'o')
- return 0;
- i += 7;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_netlog(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 6 > line_len)
- return 0;
- if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
- line[i+3] != 'l' || line[i+4] != 'o' || line[i+5] != 'g')
- return 0;
- i += 6;
- if (i < line_len && line[i] != ' ')
- return 0; /* "netlog" must be a whole word */
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- arg_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_netconsole(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 10 > line_len)
- return 0;
- if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
- line[i+3] != 'c' || line[i+4] != 'o' || line[i+5] != 'n' ||
- line[i+6] != 's' || line[i+7] != 'o' || line[i+8] != 'l' ||
- line[i+9] != 'e')
- return 0;
- i += 10;
- if (i < line_len && line[i] != ' ')
- return 0; /* "netconsole" must be a whole word */
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- arg_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_dhcp(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 4 > line_len)
- return 0;
- if (line[i] != 'd' || line[i+1] != 'h' || line[i+2] != 'c' ||
- line[i+3] != 'p')
- return 0;
- i += 4;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_netregs(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 7 > line_len)
- return 0;
- if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
- line[i+3] != 'r' || line[i+4] != 'e' || line[i+5] != 'g' ||
- line[i+6] != 's')
- return 0;
- i += 7;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_netloop(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 7 > line_len)
- return 0;
- if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
- line[i+3] != 'l' || line[i+4] != 'o' || line[i+5] != 'o' ||
- line[i+6] != 'p')
- return 0;
- i += 7;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_netreset(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 8 > line_len)
- return 0;
- if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
- line[i+3] != 'r' || line[i+4] != 'e' || line[i+5] != 's' ||
- line[i+6] != 'e' || line[i+7] != 't')
- return 0;
- i += 8;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_netrecv(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 7 > line_len)
- return 0;
- if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
- line[i+3] != 'r' || line[i+4] != 'e' || line[i+5] != 'c' ||
- line[i+6] != 'v')
- return 0;
- i += 7;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_netsend(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 7 > line_len)
- return 0;
- if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
- line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'n' ||
- line[i+6] != 'd')
- return 0;
- i += 7;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_netip(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 5 > line_len)
- return 0;
- if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
- line[i+3] != 'i' || line[i+4] != 'p')
- return 0;
- i += 5;
- if (i < line_len && line[i] != ' ')
- return 0; /* "netip" must be a whole word */
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- arg_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- /* One optional single-token argument (hostname or dotted IP). */
- static int is_cmd_ping(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 4 > line_len)
- return 0;
- if (line[i] != 'p' || line[i+1] != 'i' || line[i+2] != 'n' ||
- line[i+3] != 'g')
- return 0;
- i += 4;
- if (i < line_len && line[i] != ' ')
- return 0; /* "ping" must be a whole word */
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- arg_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- /* One optional single-token argument (URL, name or dotted IP). */
- static int is_cmd_curl(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 4 > line_len)
- return 0;
- if (line[i] != 'c' || line[i+1] != 'u' || line[i+2] != 'r' ||
- line[i+3] != 'l')
- return 0;
- i += 4;
- if (i < line_len && line[i] != ' ')
- return 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- arg_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_netdns(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 6 > line_len)
- return 0;
- if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
- line[i+3] != 'd' || line[i+4] != 'n' || line[i+5] != 's')
- return 0;
- i += 6;
- if (i < line_len && line[i] != ' ')
- return 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- arg_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- /* One optional single-token argument (DNS name). */
- static int is_cmd_host(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 4 > line_len)
- return 0;
- if (line[i] != 'h' || line[i+1] != 'o' || line[i+2] != 's' ||
- line[i+3] != 't')
- return 0;
- i += 4;
- if (i < line_len && line[i] != ' ')
- return 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- arg_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_pong(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 4 > line_len)
- return 0;
- if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'n' || line[i+3] != 'g')
- return 0;
- i += 4;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_space(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 5 > line_len)
- return 0;
- if (line[i] != 's' || line[i+1] != 'p' || line[i+2] != 'a' ||
- line[i+3] != 'c' || line[i+4] != 'e')
- return 0;
- i += 5;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- #define NET_UDP_ARG_MAX 128
- #define CAT_FNAME_MAX 64
- static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 3 > line_len)
- return 0;
- if (line[i] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
- return 0;
- i += 3;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len)
- return 0;
- j = 0;
- while (i < line_len && line[i] != ' ' && j < buf_size - 1)
- filename_buf[j++] = line[i++];
- filename_buf[j] = '\0';
- return (j > 0);
- }
- static int is_cmd_run(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 3 > line_len)
- return 0;
- if (line[i] != 'r' || line[i+1] != 'u' || line[i+2] != 'n')
- return 0;
- i += 3;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len)
- return 0;
- j = 0;
- while (i < line_len && line[i] != ' ' && j < buf_size - 1)
- filename_buf[j++] = line[i++];
- filename_buf[j] = '\0';
- return (j > 0);
- }
- #define SAVE_ARGS_MAX 128
- #define RM_FNAME_MAX 64
- #define LS_PATH_MAX 64
- #define FORMAT_DEV_MAX 16
- static int is_cmd_save(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 4 > line_len)
- return 0;
- if (line[i] != 's' || line[i+1] != 'a' || line[i+2] != 'v' || line[i+3] != 'e')
- return 0;
- i += 4;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len)
- return 0;
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- while (j > 0 && arg_buf[j - 1] == ' ')
- j--;
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len && j > 0);
- }
- static int is_cmd_rm(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 2 > line_len)
- return 0;
- if (line[i] != 'r' || line[i+1] != 'm')
- return 0;
- i += 2;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len)
- return 0;
- while (i < line_len && line[i] != ' ' && j < buf_size - 1)
- filename_buf[j++] = line[i++];
- filename_buf[j] = '\0';
- return (j > 0);
- }
- static int is_cmd_mkdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 5 > line_len)
- return 0;
- if (line[i] != 'm' || line[i+1] != 'k' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
- return 0;
- i += 5;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len)
- return 0;
- while (i < line_len && line[i] != ' ' && j < buf_size - 1)
- path_buf[j++] = line[i++];
- path_buf[j] = '\0';
- return (j > 0);
- }
- static int is_cmd_rmdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 5 > line_len)
- return 0;
- if (line[i] != 'r' || line[i+1] != 'm' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
- return 0;
- i += 5;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len)
- return 0;
- while (i < line_len && line[i] != ' ' && j < buf_size - 1)
- path_buf[j++] = line[i++];
- path_buf[j] = '\0';
- return (j > 0);
- }
- static int is_cmd_storage(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 7 > line_len)
- return 0;
- if (line[i] != 's' || line[i+1] != 't' || line[i+2] != 'o' ||
- line[i+3] != 'r' || line[i+4] != 'a' || line[i+5] != 'g' || line[i+6] != 'e')
- return 0;
- i += 7;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_meminfo(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 7 > line_len)
- return 0;
- if (line[i] != 'm' || line[i+1] != 'e' || line[i+2] != 'm' ||
- line[i+3] != 'i' || line[i+4] != 'n' || line[i+5] != 'f' ||
- line[i+6] != 'o')
- return 0;
- i += 7;
- if (i < line_len && line[i] != ' ')
- return 0; /* "meminfo" must be a whole word */
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_format(const char *line, unsigned int line_len, char *dev_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 6 > line_len)
- return 0;
- if (line[i] != 'f' || line[i+1] != 'o' || line[i+2] != 'r' ||
- line[i+3] != 'm' || line[i+4] != 'a' || line[i+5] != 't')
- return 0;
- i += 6;
- if (i < line_len && line[i] != ' ')
- return 0; /* "format" must be a whole word */
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len)
- return 0;
- j = 0;
- while (i < line_len && line[i] != ' ' && j < buf_size - 1)
- dev_buf[j++] = line[i++];
- dev_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len && j > 0);
- }
- /* Help listing: printed as two 32-column columns so it fits the 64-column
- terminal without scrolling. */
- static const char *const help_items[] = {
- "cd [path]", "ls [path]", "cat <path>", "save <path> <text>",
- "rm <path>", "mkdir <path>", "rmdir <path>", "hello", "reboot",
- "poweroff", "run <name>|<file>", "space", "prog", "clock", "colors",
- "display", "cpuinfo", "lsusb", "mouseinfo", "storage", "meminfo",
- "format <device>", "lspci [id]", "peek <addr> [len]", "poke <addr> <val>",
- "netinfo",
- "netrecv", "netsend", "netip [a.b.c.d]", "ping <host|a.b.c.d>",
- "netlog <text>", "netconsole [on|off|host <ip>]", "dhcp", "netregs",
- "netloop", "netreset", "pong", "ps", "kill <pid>", "kexec", "dbg",
- "serial test|recv", "clear", "curl <url>", "netdns [a.b.c.d]",
- "host <name>",
- };
- static void help_puts(const char *s)
- {
- while (*s)
- io_putchar(*s++);
- }
- static void help_item_pad(const char *s)
- {
- unsigned int n = 0;
- while (s[n]) {
- io_putchar(s[n]);
- n++;
- }
- while (n < 32) {
- io_putchar(' ');
- n++;
- }
- }
- static void help_print(void)
- {
- unsigned int nitems = sizeof(help_items) / sizeof(help_items[0]);
- unsigned int half = (nitems + 1) / 2;
- unsigned int k;
- for (k = 0; k < half; k++) {
- if (k + half < nitems) {
- help_item_pad(help_items[k]);
- help_puts(help_items[k + half]);
- } else {
- help_puts(help_items[k]);
- }
- io_putchar('\n');
- }
- }
- static int is_cmd_help(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 4 > line_len)
- return 0;
- if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'p')
- return 0;
- i += 4;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_clear(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 5 > line_len)
- return 0;
- if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'e' ||
- line[i+3] != 'a' || line[i+4] != 'r')
- return 0;
- i += 5;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_ps(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 2 > line_len)
- return 0;
- if (line[i] != 'p' || line[i+1] != 's')
- return 0;
- i += 2;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_kill(const char *line, unsigned int line_len, unsigned int *pid_out)
- {
- unsigned int i = 0, num = 0;
- int has_digit = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 4 > line_len)
- return 0;
- if (line[i] != 'k' || line[i+1] != 'i' || line[i+2] != 'l' || line[i+3] != 'l')
- return 0;
- i += 4;
- while (i < line_len && line[i] == ' ')
- i++;
- while (i < line_len && line[i] >= '0' && line[i] <= '9') {
- num = num * 10 + (unsigned int)(line[i] - '0');
- has_digit = 1;
- i++;
- }
- if (!has_digit)
- return 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i != line_len)
- return 0;
- *pid_out = num;
- return 1;
- }
- static int is_cmd_kexec(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 5 > line_len)
- return 0;
- if (line[i] != 'k' || line[i+1] != 'e' || line[i+2] != 'x' || line[i+3] != 'e' || line[i+4] != 'c')
- return 0;
- i += 5;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_dbg(const char *line, unsigned int line_len)
- {
- unsigned int i = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 3 > line_len)
- return 0;
- if (line[i] != 'd' || line[i+1] != 'b' || line[i+2] != 'g')
- return 0;
- i += 3;
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- static int is_cmd_serial(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
- {
- unsigned int i = 0, j = 0;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i + 6 > line_len)
- return 0;
- if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' ||
- line[i+3] != 'i' || line[i+4] != 'a' || line[i+5] != 'l')
- return 0;
- i += 6;
- while (i < line_len && line[i] == ' ')
- i++;
- if (i >= line_len) {
- if (buf_size > 0)
- arg_buf[0] = '\0';
- return 1;
- }
- while (i < line_len && j + 1 < buf_size)
- arg_buf[j++] = line[i++];
- while (j > 0 && arg_buf[j - 1] == ' ')
- j--;
- arg_buf[j] = '\0';
- while (i < line_len && line[i] == ' ')
- i++;
- return (i == line_len);
- }
- /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
- static void redraw_line_from(const char *line, unsigned int line_len,
- unsigned int line_pos, unsigned int line_start, unsigned int from)
- {
- unsigned int i;
- io_set_cursor(line_start + from);
- for (i = from; i < line_len; i++)
- io_putchar(line[i]);
- io_putchar(' ');
- io_set_cursor(line_start + line_pos);
- }
- /* Redraw entire line and clear through clear_through (so leftover chars are erased). */
- static void redraw_line_clear(const char *line, unsigned int line_len,
- unsigned int line_pos, unsigned int line_start, unsigned int clear_through)
- {
- unsigned int i;
- io_set_cursor(line_start);
- for (i = 0; i < line_len; i++)
- io_putchar(line[i]);
- for (i = line_len; i <= clear_through; i++)
- io_putchar(' ');
- io_set_cursor(line_start + line_pos);
- }
- /* Copy history entry (1 = newest) into t->line, redraw from start. */
- static void history_load_into_line(struct term *t, unsigned int browse)
- {
- unsigned int slot, len, old_len;
- const char *src;
- if (browse < 1 || browse > t->history_count)
- return;
- old_len = t->line_len;
- slot = (t->history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
- src = t->history[slot];
- for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
- t->line[len] = src[len];
- t->line[len] = '\0';
- t->line_len = len;
- t->line_pos = len;
- redraw_line_clear(t->line, len, len, t->line_start, old_len > len ? old_len : len);
- }
- /* ---- Tab completion ---- */
- /* Command names come from the help table ("ls [path]" -> "ls"). */
- static unsigned int help_name_len(const char *s)
- {
- unsigned int n = 0;
- while (s[n] && s[n] != ' ' && s[n] != '[' && s[n] != '<' && s[n] != '|')
- n++;
- return n;
- }
- static char lower_ch(char c)
- {
- return (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c;
- }
- static int prefix_ci(const char *s, const char *prefix, unsigned int plen)
- {
- unsigned int i;
- for (i = 0; i < plen; i++)
- if (lower_ch(s[i]) != lower_ch(prefix[i]))
- return 0;
- return 1;
- }
- /* "HELLO TXT" -> "hello.txt" (a completion-ready 8.3 display name). */
- static void tab_display_name(const char *n, char *out)
- {
- unsigned int bl = 0, el = 0, i, j = 0;
- for (i = 0; i < 8 && n[i] && n[i] != ' '; i++)
- bl = i + 1;
- for (i = 8; i < 11 && n[i] && n[i] != ' '; i++)
- el = i - 8 + 1;
- for (i = 0; i < bl; i++)
- out[j++] = lower_ch(n[i]);
- if (el) {
- out[j++] = '.';
- for (i = 0; i < el; i++)
- out[j++] = lower_ch(n[8 + i]);
- }
- out[j] = '\0';
- }
- /* Shared completion state (fat_list_dir takes no context pointer). */
- static char tab_prefix[16];
- static unsigned int tab_prefix_len;
- static unsigned int tab_count;
- static unsigned int tab_lcp_len;
- static char tab_lcp[16];
- static int tab_single_dir;
- static fat_fs_t *tab_fs;
- static unsigned int tab_dir_cluster;
- static unsigned int tab_col;
- static unsigned int tab_width;
- static void tab_collect_cb(const char *name, int is_dir, unsigned int size)
- {
- char disp[16];
- unsigned int i, n;
- (void)size;
- if (name[0] == '.') /* "." / ".." */
- return;
- tab_display_name(name, disp);
- if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
- return;
- n = 0;
- while (disp[n])
- n++;
- if (tab_count == 0) {
- for (i = 0; i < n; i++)
- tab_lcp[i] = disp[i];
- tab_lcp_len = n;
- tab_single_dir = is_dir;
- } else {
- i = 0;
- while (i < tab_lcp_len && i < n && tab_lcp[i] == disp[i])
- i++;
- tab_lcp_len = i;
- }
- tab_count++;
- }
- static void tab_print_one(const char *name, int is_dir)
- {
- unsigned int n = 0, w;
- while (name[n])
- n++;
- w = n + (is_dir ? 1 : 0);
- if (tab_col > 0 && tab_col + 2 + w > tab_width) {
- io_putchar('\n');
- tab_col = 0;
- }
- if (tab_col > 0) {
- io_putstr(" ");
- tab_col += 2;
- }
- io_putstr(name);
- if (is_dir)
- io_putchar('/');
- tab_col += w;
- }
- static void tab_print_cb(const char *name, int is_dir, unsigned int size)
- {
- char disp[16];
- (void)size;
- if (name[0] == '.')
- return;
- tab_display_name(name, disp);
- if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
- return;
- tab_print_one(disp, is_dir);
- }
- static int tab_scan_dir(const char *abs)
- {
- fat_fs_t *fs;
- const char *fs_path;
- if (vfs_resolve(abs, &fs, &fs_path) != 0)
- return 0;
- if (fat_resolve_dir(fs, fs_path, &tab_dir_cluster) != 0)
- return 0;
- tab_fs = fs;
- tab_count = 0;
- tab_lcp_len = 0;
- fat_list_dir(fs, tab_dir_cluster, tab_collect_cb);
- return (int)tab_count;
- }
- static int tab_scan_mounts(void)
- {
- unsigned int i;
- tab_count = 0;
- tab_lcp_len = 0;
- for (i = 0; i < vfs_mount_count; i++) {
- const char *name = vfs_mounts[i].name;
- unsigned int n = 0, j;
- while (name[n])
- n++;
- if (!prefix_ci(name, tab_prefix, tab_prefix_len))
- continue;
- if (tab_count == 0) {
- for (j = 0; j < n; j++)
- tab_lcp[j] = name[j];
- tab_lcp_len = n;
- } else {
- j = 0;
- while (j < tab_lcp_len && j < n && tab_lcp[j] == name[j])
- j++;
- tab_lcp_len = j;
- }
- tab_count++;
- }
- return (int)tab_count;
- }
- /* Replace line[start,end) with ins, keep the tail and redraw. */
- static void term_replace(struct term *t, unsigned int start, unsigned int end,
- const char *ins)
- {
- unsigned int inslen = 0, tail, i, old_len = t->line_len;
- while (ins[inslen])
- inslen++;
- tail = old_len - end;
- if (start + inslen + tail >= LINE_MAX)
- return;
- for (i = 0; i < tail; i++)
- t->line[start + inslen + i] = t->line[end + i];
- for (i = 0; i < inslen; i++)
- t->line[start + i] = ins[i];
- t->line_len = start + inslen + tail;
- t->line[t->line_len] = '\0';
- t->line_pos = start + inslen;
- redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start,
- old_len > t->line_len ? old_len : t->line_len);
- }
- /* List the ambiguous matches below the line, then redraw prompt + line. */
- static void term_tab_ambiguous(struct term *t, int mounts)
- {
- unsigned int i;
- io_putchar('\n');
- tab_col = 0;
- tab_width = t->screen.cols;
- if (mounts) {
- for (i = 0; i < vfs_mount_count; i++) {
- const char *name = vfs_mounts[i].name;
- if (prefix_ci(name, tab_prefix, tab_prefix_len))
- tab_print_one(name, 1);
- }
- } else {
- fat_list_dir(tab_fs, tab_dir_cluster, tab_print_cb);
- }
- io_putchar('\n');
- term_print_prompt(t, 1);
- redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start, t->line_len);
- }
- /* Complete the token at the cursor: command name in the first token, a VFS
- * path afterwards. One match completes, several extend to the common
- * prefix or are listed below the line. */
- static void term_complete(struct term *t)
- {
- char token[LINE_MAX + 1];
- char ins[LINE_MAX + 1];
- char dir[VFS_PATH_MAX];
- unsigned int start = t->line_pos, end, tl, i, j;
- while (start > 0 && t->line[start - 1] != ' ')
- start--;
- end = t->line_pos;
- while (end < t->line_len && t->line[end] != ' ')
- end++;
- tl = end - start;
- for (i = 0; i < tl; i++)
- token[i] = t->line[start + i];
- token[tl] = '\0';
- /* First token -> command name. */
- {
- int first = 1;
- for (i = 0; i < start; i++)
- if (t->line[i] != ' ') {
- first = 0;
- break;
- }
- if (first) {
- unsigned int nitems = sizeof(help_items) / sizeof(help_items[0]);
- unsigned int count = 0, lcp_len = 0, best = 0;
- char lcp[16];
- for (i = 0; i < nitems; i++) {
- unsigned int nl = help_name_len(help_items[i]);
- if (nl < tl || !prefix_ci(help_items[i], token, tl))
- continue;
- if (count == 0) {
- for (j = 0; j < nl; j++)
- lcp[j] = help_items[i][j];
- lcp_len = nl;
- best = i;
- } else {
- for (j = 0; j < lcp_len && j < nl &&
- lcp[j] == help_items[i][j]; j++)
- ;
- lcp_len = j;
- }
- count++;
- }
- if (count == 0)
- return;
- if (count == 1) {
- unsigned int nl = help_name_len(help_items[best]);
- for (j = 0; j < nl; j++)
- ins[j] = help_items[best][j];
- ins[nl] = ' ';
- ins[nl + 1] = '\0';
- term_replace(t, start, end, ins);
- return;
- }
- if (lcp_len > tl) {
- for (j = 0; j < lcp_len; j++)
- ins[j] = lcp[j];
- ins[lcp_len] = '\0';
- term_replace(t, start, end, ins);
- return;
- }
- io_putchar('\n');
- tab_col = 0;
- tab_width = t->screen.cols;
- for (i = 0; i < nitems; i++) {
- unsigned int nl = help_name_len(help_items[i]);
- char name[16];
- if (nl < tl || !prefix_ci(help_items[i], token, tl))
- continue;
- for (j = 0; j < nl && j < 15; j++)
- name[j] = help_items[i][j];
- name[j] = '\0';
- tab_print_one(name, 0);
- }
- io_putchar('\n');
- term_print_prompt(t, 1);
- redraw_line_clear(t->line, t->line_len, t->line_pos,
- t->line_start, t->line_len);
- return;
- }
- }
- /* Argument token -> VFS path. */
- {
- char dirpart[LINE_MAX + 1];
- const char *last = 0, *p;
- unsigned int dp, n;
- int mounts = 0;
- for (p = token; *p; p++)
- if (*p == '/')
- last = p;
- if (last) {
- dp = (unsigned int)(last - token) + 1;
- for (i = 0; i < dp; i++)
- dirpart[i] = token[i];
- dirpart[dp] = '\0';
- p = last + 1;
- } else {
- dp = 0;
- dirpart[0] = '\0';
- p = token;
- }
- {
- unsigned int k = 0;
- while (*p && k < 15)
- tab_prefix[k++] = *p++;
- tab_prefix[k] = '\0';
- tab_prefix_len = k;
- }
- if (dirpart[0] == '/' && dirpart[1] == '\0')
- mounts = 1;
- if (dirpart[0] == '/') {
- if (dp >= VFS_PATH_MAX)
- return;
- for (i = 0; dirpart[i]; i++)
- dir[i] = dirpart[i];
- dir[i] = '\0';
- } else {
- if (vfs_join_path(t->cwd, dirpart, dir, sizeof(dir)) != 0)
- return;
- }
- n = mounts ? (unsigned int)tab_scan_mounts()
- : (unsigned int)tab_scan_dir(dir);
- if (n == 0)
- return;
- if (dp + tab_lcp_len + 2 > LINE_MAX)
- return;
- if (n == 1) {
- for (i = 0; i < dp; i++)
- ins[i] = dirpart[i];
- for (i = 0; i < tab_lcp_len; i++)
- ins[dp + i] = tab_lcp[i];
- ins[dp + tab_lcp_len] = (mounts || tab_single_dir) ? '/' : ' ';
- ins[dp + tab_lcp_len + 1] = '\0';
- term_replace(t, start, end, ins);
- return;
- }
- if (tab_lcp_len > tab_prefix_len) {
- for (i = 0; i < dp; i++)
- ins[i] = dirpart[i];
- for (i = 0; i < tab_lcp_len; i++)
- ins[dp + i] = tab_lcp[i];
- ins[dp + tab_lcp_len] = '\0';
- term_replace(t, start, end, ins);
- return;
- }
- term_tab_ambiguous(t, mounts);
- }
- }
- /* Push current line to history if non-empty (and not duplicate of newest). */
- static void history_push(struct term *t, const char *line, unsigned int line_len)
- {
- unsigned int i, slot;
- int all_spaces = 1;
- if (line_len == 0)
- return;
- for (i = 0; i < line_len; i++) {
- if (line[i] != ' ') {
- all_spaces = 0;
- break;
- }
- }
- if (all_spaces)
- return;
- if (t->history_count > 0) {
- slot = (t->history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
- for (i = 0; i < line_len && i < LINE_MAX - 1 && t->history[slot][i]; i++) {
- if (t->history[slot][i] != line[i])
- break;
- }
- if (i == line_len && t->history[slot][i] == '\0')
- return; /* duplicate of newest */
- }
- slot = t->history_next;
- for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
- t->history[slot][i] = line[i];
- t->history[slot][i] = '\0';
- t->history_next = (t->history_next + 1) % HISTORY_SIZE;
- if (t->history_count < HISTORY_SIZE)
- t->history_count++;
- }
- /* Change directory: resolve `arg` (relative to t->cwd, or absolute) and
- * validate it is an existing directory. Updates t->cwd on success. */
- static void term_cd(struct term *t, const char *arg)
- {
- char abs[VFS_PATH_MAX];
- fat_fs_t *fs;
- const char *fs_path;
- unsigned int dir;
- int r;
- unsigned int i;
- if (arg[0] == '\0') {
- t->cwd[0] = '/';
- t->cwd[1] = '\0';
- return;
- }
- if (vfs_join_path(t->cwd, arg, abs, sizeof(abs)) != 0) {
- io_putstr("cd: path too long\n");
- return;
- }
- if (abs[0] == '/' && abs[1] == '\0') {
- t->cwd[0] = '/';
- t->cwd[1] = '\0';
- return;
- }
- if (vfs_resolve(abs, &fs, &fs_path) != 0) {
- io_putstr("cd: no such directory\n");
- return;
- }
- r = fat_resolve_dir(fs, fs_path, &dir);
- if (r == -2) {
- io_putstr("cd: disk error\n");
- return;
- }
- if (r != 0) {
- io_putstr("cd: no such directory\n");
- return;
- }
- for (i = 0; abs[i] && i < sizeof(t->cwd) - 1; i++)
- t->cwd[i] = abs[i];
- t->cwd[i] = '\0';
- }
- /* Resolve the first token (path) of `args` against t->cwd, rebuilding
- * "abs-path rest-of-args" into out. Used by `save`. */
- static void term_resolve_first(struct term *t, const char *args,
- char *out, unsigned int out_size)
- {
- char path[VFS_PATH_MAX];
- char abs[VFS_PATH_MAX];
- unsigned int i = 0, j = 0;
- while (args[i] == ' ')
- i++;
- while (args[i] && args[i] != ' ' && j < sizeof(path) - 1)
- path[j++] = args[i++];
- path[j] = '\0';
- if (vfs_join_path(t->cwd, path, abs, sizeof(abs)) != 0) {
- abs[0] = '/';
- abs[1] = '\0';
- }
- while (args[i] == ' ')
- i++;
- j = 0;
- {
- unsigned int k = 0;
- while (abs[k] && j + 1 < out_size)
- out[j++] = abs[k++];
- }
- if (args[i]) {
- if (j + 1 < out_size)
- out[j++] = ' ';
- while (args[i] && j + 1 < out_size)
- out[j++] = args[i++];
- }
- out[j] = '\0';
- }
- /* Spawn a foreground command process; the caller (term_run's wait loop)
- * waits for it to finish and handles Ctrl+C. */
- static void term_spawn(struct term *t, const char *name, void (*entry)(void))
- {
- int pid = process_create(name, entry);
- if (pid < 0)
- io_putstr("error: too many processes\n");
- else
- t->fg_pid = pid;
- }
- static void term_spawn_arg(struct term *t, const char *name,
- void (*entry)(const char *), const char *arg)
- {
- int pid = process_create_arg(name, entry, arg);
- if (pid < 0)
- io_putstr("error: too many processes\n");
- else
- t->fg_pid = pid;
- }
- void term_run(struct term *t)
- {
- t->line_len = 0;
- t->line_pos = 0;
- t->line[0] = '\0';
- t->history_browse = 0;
- t->fg_pid = -1;
- io_clear_screen();
- term_print_prompt(t, 1);
- for (;;) {
- if (t->fg_pid >= 0) {
- /* Waiting on a foreground child: poll keys for Ctrl+C
- * and watch for the child to exit. */
- int k = term_key_pop(t);
- if (k == IO_KEY_CTRL_C) {
- int killed = (process_kill((unsigned int)t->fg_pid) == 0);
- t->fg_pid = -1;
- if (killed)
- io_putstr("^C\n");
- term_print_prompt(t, 0);
- } else if (!process_is_alive((unsigned int)t->fg_pid)) {
- t->fg_pid = -1;
- term_print_prompt(t, 0);
- } else {
- process_sleep_ms(10);
- }
- continue;
- }
- {
- int k = term_key_pop(t);
- if (k) {
- kdbg_term_count++;
- kdbg_term_last = (unsigned int)k;
- kdbg_status();
- if (k == IO_KEY_CTRL_C) {
- /* Ctrl+C at the prompt: discard the line. */
- io_putstr("^C\n");
- t->line_len = 0;
- t->line_pos = 0;
- t->line[0] = '\0';
- term_print_prompt(t, 0);
- } else if (k == IO_KEY_LEFT) {
- if (t->line_pos > 0) {
- t->line_pos--;
- io_set_cursor(t->line_start + t->line_pos);
- }
- } else if (k == IO_KEY_RIGHT) {
- if (t->line_pos < t->line_len) {
- t->line_pos++;
- io_set_cursor(t->line_start + t->line_pos);
- }
- } else if (k == IO_KEY_UP) {
- if (t->history_count > 0) {
- if (t->history_browse < t->history_count)
- t->history_browse++;
- history_load_into_line(t, t->history_browse);
- }
- } else if (k == IO_KEY_DOWN) {
- if (t->history_browse > 0) {
- t->history_browse--;
- if (t->history_browse == 0) {
- unsigned int clear_len = t->line_len;
- t->line_len = 0;
- t->line_pos = 0;
- t->line[0] = '\0';
- redraw_line_clear(t->line, 0, 0, t->line_start, clear_len);
- } else {
- history_load_into_line(t, t->history_browse);
- }
- }
- } else if (k == '\b') {
- if (t->line_pos > 0) {
- unsigned int i;
- t->line_pos--;
- for (i = t->line_pos; i < t->line_len; i++)
- t->line[i] = t->line[i + 1];
- t->line_len--;
- t->line[t->line_len] = '\0';
- redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos);
- }
- } else if (k == '\t') {
- term_complete(t);
- } else if (k == '\n') {
- char cat_fname[CAT_FNAME_MAX];
- char save_args[SAVE_ARGS_MAX];
- char rm_fname[RM_FNAME_MAX];
- char ls_path[LS_PATH_MAX];
- char format_dev[FORMAT_DEV_MAX];
- char netip_arg[16];
- char ping_arg[64];
- char netlog_arg[NET_UDP_ARG_MAX];
- char netconsole_arg[NET_UDP_ARG_MAX];
- char curl_arg[128];
- char netdns_arg[32];
- char host_arg[64];
- char lspci_arg[16];
- char peek_arg[32];
- char resolved[VFS_PATH_MAX];
- char save_resolved[SAVE_ARGS_MAX + VFS_PATH_MAX];
- unsigned int kill_pid;
- int skip_prompt = 0;
- io_putchar('\n');
- history_push(t, t->line, t->line_len);
- t->history_browse = 0;
- if (is_cmd_hello(t->line, t->line_len)) {
- int pid = process_create_user("hello",
- _binary_hello_elf_start,
- (unsigned int)(_binary_hello_elf_end -
- _binary_hello_elf_start));
- if (pid < 0)
- io_putstr("error: too many processes\n");
- else
- t->fg_pid = pid;
- } else if (is_cmd_reboot(t->line, t->line_len)) {
- program_reboot_main();
- } else if (is_cmd_poweroff(t->line, t->line_len)) {
- program_poweroff_main();
- } else if (is_cmd_cd(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
- term_cd(t, ls_path);
- } else if (is_cmd_ls(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
- if (vfs_join_path(t->cwd, ls_path, resolved, sizeof(resolved)) == 0) {
- term_spawn_arg(t, "ls", program_ls_main, resolved);
- } else {
- io_putstr("ls: path too long\n");
- }
- } else if (is_cmd_lsusb(t->line, t->line_len)) {
- term_spawn(t, "lsusb", program_lsusb_main);
- } else if (is_cmd_lspci(t->line, t->line_len, lspci_arg, sizeof(lspci_arg))) {
- term_spawn_arg(t, "lspci", program_lspci_main, lspci_arg);
- } else if (is_cmd_peek(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
- term_spawn_arg(t, "peek", program_peek_main, peek_arg);
- } else if (is_cmd_poke(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
- term_spawn_arg(t, "poke", program_poke_main, peek_arg);
- } else if (is_cmd_netinfo(t->line, t->line_len)) {
- term_spawn(t, "netinfo", program_netinfo_main);
- } else if (is_cmd_netrecv(t->line, t->line_len)) {
- term_spawn(t, "netrecv", program_netrecv_main);
- } else if (is_cmd_netsend(t->line, t->line_len)) {
- term_spawn(t, "netsend", program_netsend_main);
- } else if (is_cmd_netip(t->line, t->line_len, netip_arg, sizeof(netip_arg))) {
- term_spawn_arg(t, "netip", program_netip_main, netip_arg);
- } else if (is_cmd_ping(t->line, t->line_len, ping_arg, sizeof(ping_arg))) {
- term_spawn_arg(t, "ping", program_ping_main, ping_arg);
- } else if (is_cmd_netlog(t->line, t->line_len, netlog_arg, sizeof(netlog_arg))) {
- term_spawn_arg(t, "netlog", program_netlog_main, netlog_arg);
- } else if (is_cmd_netconsole(t->line, t->line_len, netconsole_arg, sizeof(netconsole_arg))) {
- term_spawn_arg(t, "netconsole", program_netconsole_main, netconsole_arg);
- } else if (is_cmd_dhcp(t->line, t->line_len)) {
- term_spawn(t, "dhcp", program_dhcp_main);
- } else if (is_cmd_netregs(t->line, t->line_len)) {
- term_spawn(t, "netregs", program_netregs_main);
- } else if (is_cmd_netloop(t->line, t->line_len)) {
- term_spawn(t, "netloop", program_netloop_main);
- } else if (is_cmd_netreset(t->line, t->line_len)) {
- term_spawn(t, "netreset", program_netreset_main);
- } else if (is_cmd_netdns(t->line, t->line_len, netdns_arg, sizeof(netdns_arg))) {
- term_spawn_arg(t, "netdns", program_netdns_main, netdns_arg);
- } else if (is_cmd_curl(t->line, t->line_len, curl_arg, sizeof(curl_arg))) {
- term_spawn_arg(t, "curl", program_curl_main, curl_arg);
- } else if (is_cmd_host(t->line, t->line_len, host_arg, sizeof(host_arg))) {
- term_spawn_arg(t, "host", program_host_main, host_arg);
- } else if (is_cmd_pong(t->line, t->line_len)) {
- term_spawn(t, "pong", program_pong_main);
- } else if (is_cmd_storage(t->line, t->line_len)) {
- term_spawn(t, "storage", program_storage_main);
- } else if (is_cmd_meminfo(t->line, t->line_len)) {
- term_spawn(t, "meminfo", program_mem_main);
- } else if (is_cmd_clock(t->line, t->line_len)) {
- io_putstr("clock: not available in windowed mode yet\n");
- } else if (is_cmd_colors(t->line, t->line_len)) {
- io_putstr("colors: not available in windowed mode yet\n");
- } else if (is_cmd_cat(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
- if (vfs_join_path(t->cwd, cat_fname, resolved, sizeof(resolved)) == 0) {
- term_spawn_arg(t, "cat", program_cat_main, resolved);
- } else {
- io_putstr("cat: path too long\n");
- }
- } else if (is_cmd_run(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
- if (cat_fname[0] == 's' && cat_fname[1] == 'e' &&
- cat_fname[2] == 'r' && cat_fname[3] == 'i' &&
- cat_fname[4] == 'a' && cat_fname[5] == 'l' &&
- cat_fname[6] == '\0') {
- if (process_create_arg("run_serial", program_run_serial_main, cat_fname) < 0)
- io_putstr("error: too many processes\n");
- } else {
- io_putstr("run: not available in windowed mode yet\n");
- }
- } else if (is_cmd_save(t->line, t->line_len, save_args, SAVE_ARGS_MAX)) {
- term_resolve_first(t, save_args, save_resolved, sizeof(save_resolved));
- term_spawn_arg(t, "save", program_save_main, save_resolved);
- } else if (is_cmd_mkdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
- if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
- term_spawn_arg(t, "mkdir", program_mkdir_main, resolved);
- } else {
- io_putstr("mkdir: path too long\n");
- }
- } else if (is_cmd_rmdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
- if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
- term_spawn_arg(t, "rmdir", program_rmdir_main, resolved);
- } else {
- io_putstr("rmdir: path too long\n");
- }
- } else if (is_cmd_rm(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
- if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
- term_spawn_arg(t, "rm", program_rm_main, resolved);
- } else {
- io_putstr("rm: path too long\n");
- }
- } else if (is_cmd_space(t->line, t->line_len)) {
- term_spawn_arg(t, "space", program_run_embedded_main, "space");
- } else if (is_cmd_prog(t->line, t->line_len)) {
- term_spawn_arg(t, "prog", program_run_embedded_main, "prog");
- } else if (is_cmd_display(t->line, t->line_len)) {
- term_spawn(t, "display", program_display_main);
- } else if (is_cmd_cpuinfo(t->line, t->line_len)) {
- term_spawn(t, "cpuinfo", program_cpuinfo_main);
- } else if (is_cmd_mouseinfo(t->line, t->line_len)) {
- term_spawn(t, "mouseinfo", program_mouseinfo_main);
- } else if (is_cmd_format(t->line, t->line_len, format_dev, FORMAT_DEV_MAX)) {
- program_format_main(format_dev);
- } else if (is_cmd_ps(t->line, t->line_len)) {
- process_ps();
- } else if (is_cmd_kill(t->line, t->line_len, &kill_pid)) {
- if (process_kill(kill_pid) < 0)
- io_putstr("kill: no such process\n");
- else
- io_putstr("killed\n");
- } else if (is_cmd_kexec(t->line, t->line_len)) {
- if (kexec_busy) {
- io_putstr("kexec: already receiving\n");
- } else {
- kexec_busy = 1;
- if (process_create("kexec", program_kexec_main) < 0) {
- kexec_busy = 0;
- io_putstr("error: too many processes\n");
- }
- }
- } else if (is_cmd_dbg(t->line, t->line_len)) {
- program_dbg_main();
- } else if (is_cmd_help(t->line, t->line_len)) {
- help_print();
- } else if (is_cmd_clear(t->line, t->line_len)) {
- io_clear_screen();
- term_print_prompt(t, 1);
- skip_prompt = 1;
- } else {
- char serial_arg[8];
- if (is_cmd_serial(t->line, t->line_len, serial_arg, sizeof(serial_arg))) {
- if (serial_arg[0] == 't' && serial_arg[1] == 'e' &&
- serial_arg[2] == 's' && serial_arg[3] == 't' &&
- serial_arg[4] == '\0') {
- if (process_create_arg("serial_loop", program_serial_loop_main, serial_arg) < 0)
- io_putstr("error: too many processes\n");
- } else if (serial_arg[0] == 'r' && serial_arg[1] == 'e' &&
- serial_arg[2] == 'c' && serial_arg[3] == 'v' &&
- serial_arg[4] == '\0') {
- program_serial_recv_main(serial_arg);
- } else {
- io_putstr("serial: usage serial test|recv\n");
- }
- } else {
- unsigned int i = 0;
- while (i < t->line_len && t->line[i] == ' ')
- i++;
- if (i < t->line_len)
- io_putstr("command not found\n");
- }
- }
- t->line_len = 0;
- t->line_pos = 0;
- t->line[0] = '\0';
- if (!skip_prompt && t->fg_pid < 0) {
- term_print_prompt(t, 0);
- }
- } else {
- unsigned char u = (unsigned char)k;
- if (u >= 0x20 && u < 0x7F && t->line_len < LINE_MAX - 1) {
- unsigned int i;
- t->history_browse = 0;
- for (i = t->line_len; i > t->line_pos; i--)
- t->line[i] = t->line[i - 1];
- t->line[t->line_pos] = (char)k;
- t->line_len++;
- t->line[t->line_len] = '\0';
- t->line_pos++;
- redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos - 1);
- }
- }
- } else {
- process_sleep_ms(10);
- }
- }
- }
- }
- void term_entry(void *arg)
- {
- struct term *t = (struct term *)arg;
- process_set_screen(&t->screen);
- term_run(t);
- }
|