/**************************************************************************** * @(#) Ndless - cli.c Command Line Interface * * Copyright (C) 2010 by ANNEHEIM Geoffrey and ARMAND Olivier * Contact: geoffrey.anneheim@gmail.com / olivier.calc@gmail.com * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. * * RCSID $Id$ ****************************************************************************/ #include #include "term.h" // screen: 30 rows, 40 columns for 8x8 font static char strbuf[40]; static char path[0x104]; void printhelp(void){ char *helpstr[13] = { " | exit - quits Transfer\n", "? | help - shows this help\n", "cls - clearscreen\n", "dir [path]- directory listing\n", "md - create a directory \n", "rd - delete a directory\n", "cwd - show current working directory\n", "cd - change directory\n", "del - delete file\n", "rf - read file \n", "wf -writes a file\n", "io - toggle screen/serial user I/O\n", "" }; drawstring(helpstr[0]); drawstring(helpstr[1]); drawstring(helpstr[2]); drawstring(helpstr[3]); drawstring(helpstr[4]); drawstring(helpstr[5]); drawstring(helpstr[6]); drawstring(helpstr[7]); drawstring(helpstr[8]); drawstring(helpstr[9]); drawstring(helpstr[10]); drawstring(helpstr[11]); } void readfile(char *fname){ FILE *ifile; int i; ifile = fopen(fname,"rb"); if(ifile == NULL){ drawstring("Error open file\n"); return; } i = fread((void*)0x11800000,1,0x100000,ifile); fclose(ifile); sprintf(strbuf,"Read 0x%X bytes to 11800000\n",i); drawstring(strbuf); } void writefile(char *fname, int size){ FILE *ofile; int i; ofile = fopen(fname,"wb"); if(ofile == NULL){ drawstring("Error open file\n"); return; } i = fwrite((void*)0x11800000,1,size,ofile); fclose(ofile); sprintf(strbuf,"Wrote 0x%X bytes from 11800000\n",i); drawstring(strbuf); } void dir(char *path){ DSTAT st; char buf[64]; //scratch buffer if (NU_Get_First((struct dstat*)&st,path)){ drawstring("Failed to find first file\n"); NU_Done((struct dstat*)&st); return ; } // List all the files in the directory with given search pattern do{ if (st.fattribute & 0x10) // found a directory { sprintf(buf," %s\n", st.lfname ); drawstring( buf ); } else // found a file { sprintf(buf,"%10ld %s\n", (long int)st.fsize, st.lfname ); drawstring( buf ); } } while (NU_Get_Next((struct dstat*)&st) == 0); // fill in next find , 0 == no error NU_Done((struct dstat*)&st); // done - free up resources } int parseinput(void){ int arg1, arg2, scanned; char cmd[40], fmt[40]; memset(strbuf,0x00,sizeof(strbuf)); gets(strbuf); if(strstr(strbuf,"io") == strbuf ){ if (setIOmode(-1) == 0){ setIOmode(1); drawstring("serial I/O on\n"); ShowCursor(); }else{ setIOmode(0); drawstring("Nspire screen/kbd I/O on\n"); ShowCursor(); } return -2; } if(strstr(strbuf,"exit") || strstr(strbuf,"\x1b") ){ return 0; } if(strstr(strbuf,"help") || strbuf[0] == '?'){ printhelp(); ShowCursor(); return 1; } if(strstr(strbuf,"cls") == strbuf ) { clearscreen(); return 5; } if(strstr(strbuf,"rf ") == strbuf ) { scanned = sscanf(strbuf,"rf %s",path ); if(scanned == 1){ readfile(path); ShowCursor(); } return 8; } if(strstr(strbuf,"wf ") == strbuf ) { scanned = sscanf(strbuf,"wf %s %x",path, &arg1 ); if(scanned == 2){ writefile(path, arg1); ShowCursor(); } return 9; } if(strstr(strbuf,"dir") == strbuf ) { scanned = sscanf(strbuf,"dir %s",path ); if(scanned == 1){ dir(path); ShowCursor(); } else { dir("*.*"); ShowCursor(); } return 10; } if(strstr(strbuf,"md ") == strbuf ) { scanned = sscanf(strbuf,"md %s",path ); if(scanned == 1){ mkdir(path, 0777); ShowCursor(); } return 11; } if(strstr(strbuf,"rd ") == strbuf ) { scanned = sscanf(strbuf,"rd %s",path ); if(scanned == 1){ rmdir(path); ShowCursor(); } return 12; } if(strstr(strbuf,"cwd") == strbuf ) { getcwd(path,0x101); strcat(path,"\n"); drawstring(path); ShowCursor(); return 13; } if(strstr(strbuf,"cd ") == strbuf ) { scanned = sscanf(strbuf,"cd %s",path ); set_current_path(path); ShowCursor(); return 14; } if(strstr(strbuf,"del ") == strbuf ) { scanned = sscanf(strbuf,"del %s",path ); if(scanned == 1){ unlink(path); ShowCursor(); return 15; } } if(strstr(strbuf,"puts") == strbuf ) { // use sprintf's format to send any char "\x??..." scanned = sscanf(strbuf,"puts %s",fmt ); sprintf(cmd,fmt); log_rs232_v(cmd); ShowCursor(); return 16; } // not in the list or syntax error sprintf(cmd,"Command not found: %s\n",strbuf); drawstring(cmd); ShowCursor(); return -1; }