OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [netutil.c] - Diff between revs 354 and 362

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 354 Rev 362
Line 427... Line 427...
 
 
static void TelnetServer(IPSocket *socket)
static void TelnetServer(IPSocket *socket)
{
{
   uint8 buf[COMMAND_BUFFER_SIZE+4];
   uint8 buf[COMMAND_BUFFER_SIZE+4];
   char bufOut[32];
   char bufOut[32];
   int bytes, i, j, length, found;
   int bytes, i, j, k, length, found;
   char *ptr, *command = socket->userPtr;
   char *ptr, *command = socket->userPtr;
   char *argv[10];
   char *argv[10];
 
 
   if(socket->state > IP_TCP)
   if(socket->state > IP_TCP)
      return;
      return;
 
   for(;;)
 
   {
   bytes = IPRead(socket, buf, sizeof(buf)-1);
   bytes = IPRead(socket, buf, sizeof(buf)-1);
   if(command == NULL)
   if(command == NULL)
   {
   {
      socket->userPtr = command = (char*)malloc(COMMAND_BUFFER_SIZE);
      socket->userPtr = command = (char*)malloc(COMMAND_BUFFER_SIZE);
      if(command == NULL)
      if(command == NULL)
Line 455... Line 457...
      IPWrite(socket, buf, 6+23);
      IPWrite(socket, buf, 6+23);
      IPWriteFlush(socket);
      IPWriteFlush(socket);
      command[0] = 0;
      command[0] = 0;
      return;
      return;
   }
   }
 
      if(bytes == 0)
 
         return;
   socket->dontFlush = 0;
   socket->dontFlush = 0;
   buf[bytes] = 0;
   buf[bytes] = 0;
   length = (int)strlen(command);
   length = (int)strlen(command);
   for(j = 0; j < bytes; ++j)
   for(j = 0; j < bytes; ++j)
   {
   {
Line 568... Line 572...
         {
         {
            if(strcmp(command, TelnetFuncList[i].name) == 0 &&
            if(strcmp(command, TelnetFuncList[i].name) == 0 &&
               TelnetFuncList[i].func)
               TelnetFuncList[i].func)
            {
            {
               found = 1;
               found = 1;
 
                  for(k = 1; k < 10; ++k)
 
                  {
 
                     if(argv[k][0] == '>' && argv[k][1]) //stdout to file?
 
                     {
 
                        socket->fileOut = fopen(&argv[k][1], "a");
 
                        argv[k] = "";
 
                     }
 
                     if(argv[k][0] == '<' && argv[k][1]) //stdin from file?
 
                     {
 
                        socket->fileIn = fopen(&argv[k][1], "r");
 
                        argv[k] = "";
 
                     }
 
                  }
               TelnetFuncList[i].func(socket, argv);
               TelnetFuncList[i].func(socket, argv);
 
                  if(socket->fileOut)
 
                  {
 
                     fwrite("\r\n", 1, 2, socket->fileOut);
 
                     fclose(socket->fileOut);
 
                  }
 
                  socket->fileOut = NULL;
               break;
               break;
            }
            }
         }
         }
#ifdef DLL_SETUP
#ifdef DLL_SETUP
         if(found == 0)
         if(found == 0)
Line 592... Line 615...
            IPPrintf(socket, "\r\n-> ");
            IPPrintf(socket, "\r\n-> ");
      } //command entered
      } //command entered
   } //bytes
   } //bytes
   IPWriteFlush(socket);
   IPWriteFlush(socket);
}
}
 
}
 
 
 
 
void TelnetInit(TelnetFunc_t *funcList)
void TelnetInit(TelnetFunc_t *funcList)
{
{
   IPSocket *socket;
   IPSocket *socket;
Line 727... Line 751...
      if(width > 80)
      if(width > 80)
      {
      {
         IPPrintf(socket, "\n");
         IPPrintf(socket, "\n");
         width = bytes;
         width = bytes;
      }
      }
      IPWrite(socket, (uint8*)buf2, bytes);
      IPPrintf(socket, "%s", buf2);
   }
   }
   fclose(file);
   fclose(file);
}
}
 
 
 
 
Line 890... Line 914...
   fclose(file);
   fclose(file);
   IPPrintf(socket, "Created myfile.txt");
   IPPrintf(socket, "Created myfile.txt");
}
}
 
 
 
 
 
static void ConsoleUptime(IPSocket *socket, char *argv[])
 
{
 
   int days, hours, minutes, seconds;
 
   (void)argv;
 
   seconds = OS_ThreadTime() / 100;
 
   minutes = seconds / 60 % 60;
 
   hours = seconds / 3600 % 24;
 
   days = seconds / 3600 / 24;
 
   seconds %= 60;
 
   IPPrintf(socket, "%d days %2d:%2d:%2d\n", days, hours, minutes, seconds);
 
}
 
 
 
 
 
static void ConsoleDump(IPSocket *socket, char *argv[])
 
{
 
   FILE *fileIn;
 
   uint8 buf[16];
 
   int bytes, i, j;
 
 
 
   fileIn = fopen(argv[1], "r");
 
   if(fileIn == NULL)
 
      return;
 
   for(j = 0; j < 1024*1024*16; j += 16)
 
   {
 
      bytes = fread(buf, 1, 16, fileIn);
 
      if(bytes == 0)
 
         break;
 
      IPPrintf(socket, "%8x ", j);
 
      for(i = 0; i < bytes; ++i)
 
         IPPrintf(socket, "%2x ", buf[i]);
 
      for( ; i < 16; ++i)
 
         IPPrintf(socket, "   ");
 
      for(i = 0; i < bytes; ++i)
 
      {
 
         if(isprint(buf[i]))
 
            IPPrintf(socket, "%c", buf[i]);
 
         else
 
            IPPrintf(socket, ".");
 
      }
 
      IPPrintf(socket, "\n");
 
   }
 
   fclose(fileIn);
 
}
 
 
 
 
 
static void ConsoleGrep(IPSocket *socket, char *argv[])
 
{
 
   FILE *fileIn;
 
   char buf[200];
 
   int bytes;
 
   char *ptr, *ptrEnd;
 
 
 
   if(argv[1][0] == 0 || argv[2][0] == 0)
 
   {
 
      IPPrintf(socket, "Usage: grep pattern file\n");
 
      return;
 
   }
 
   fileIn = fopen(argv[2], "r");
 
   if(fileIn == NULL)
 
      return;
 
   bytes = 0;
 
   for(;;)
 
   {
 
      bytes += fread(buf + bytes, 1, sizeof(buf) - bytes - 1, fileIn);
 
      if(bytes == 0)
 
         break;
 
      buf[bytes] = 0;
 
      ptrEnd = strstr(buf, "\r");
 
      if(ptrEnd == NULL)
 
         ptrEnd = strstr(buf, "\n");
 
      if(ptrEnd)
 
      {
 
         *ptrEnd = 0;
 
         if(*++ptrEnd == '\n')
 
            ++ptrEnd;
 
      }
 
      ptr = strstr(buf, argv[1]);
 
      if(ptr)
 
         IPPrintf(socket, "%s\n", buf);
 
      if(ptrEnd)
 
      {
 
         bytes = strlen(ptrEnd);
 
         memcpy(buf, ptrEnd, bytes);
 
      }
 
      else
 
      {
 
         bytes = 0;
 
      }
 
   }
 
   fclose(fileIn);
 
}
 
 
 
 
#ifdef DLL_SETUP
#ifdef DLL_SETUP
#include "dll.h"
#include "dll.h"
 
 
static void ConsoleRun(IPSocket *socket, char *argv[])
static void ConsoleRun(IPSocket *socket, char *argv[])
{
{
   FILE *file;
   FILE *file;
   int bytes, i;
   int bytes, i;
   uint8 code[128];
   uint8 code[128];
   DllFunc funcPtr;
   DllFunc funcPtr;
   DllInfo info;
 
   int *bss;
 
   char *command, *ptr;
   char *command, *ptr;
 
 
   if(strcmp(argv[0], "run") == 0)
   if(strcmp(argv[0], "run") == 0)
      ++argv;
      ++argv;
   info.socket = socket;
 
   info.dllFuncList = DllFuncList;
 
   file = fopen(argv[0], "r");
   file = fopen(argv[0], "r");
   if(file == NULL)
   if(file == NULL)
   {
   {
      IPPrintf(socket, "Can't find %s", argv[0]);
      IPPrintf(socket, "Can't find %s", argv[0]);
      return;
      return;
   }
   }
 
 
   bytes = fread(code, 1, sizeof(code), file);
   bytes = fread(code, 1, sizeof(code), file);  //load first 128 bytes
 
   if(code[0] >= ' ')
 
   {
 
      socket->fileIn = file;       //script file
 
      fseek(file, 0, 0);
 
      return;
 
   }
 
 
   funcPtr = (DllFunc)code;
   funcPtr = (DllFunc)code;
   funcPtr(&info);     //call entry() to fill in info
   ptr = funcPtr(NULL);            //determine load address
 
 
   memcpy(info.entry, code, bytes);
   memcpy(ptr, code, bytes);       //copy to correct address
   bytes = fread(info.entry + bytes, 1, 1024*1024*8, file) + bytes;
   bytes += fread(ptr + bytes, 1, 1024*1024*8, file);
   fclose(file);
   fclose(file);
   printf("address=0x%x bytes=%d\n", (int)info.entry, bytes);
   printf("address=0x%x bytes=%d\n", (int)ptr, bytes);
   for(bss = info.bssStart; bss < info.bssEnd; ++bss)
   funcPtr = (DllFunc)ptr;
      *bss = 0;
   funcPtr = (DllFunc)funcPtr(DllFuncList);  //initialize DLL, find Start()
   *info.pDllF = DllFuncList;
 
 
 
   //Register new command
   //Register new command
   command = argv[0];
   command = argv[0];
   for(;;)
   for(;;)
   {
   {
Line 942... Line 1061...
      if(TelnetFuncList[i].name[0] == 0 ||
      if(TelnetFuncList[i].name[0] == 0 ||
         strcmp(TelnetFuncList[i].name, command) == 0)
         strcmp(TelnetFuncList[i].name, command) == 0)
      {
      {
         TelnetFuncList[i].name = (char*)malloc(40);
         TelnetFuncList[i].name = (char*)malloc(40);
         strcpy(TelnetFuncList[i].name, command);
         strcpy(TelnetFuncList[i].name, command);
         TelnetFuncList[i].func = (ConsoleFunc)info.startPtr;
         TelnetFuncList[i].func = (ConsoleFunc)funcPtr;
         break;
         break;
      }
      }
   }
   }
 
 
   socket->userFunc = socket->funcPtr;
   socket->userFunc = socket->funcPtr;
   info.startPtr(socket, argv);
   funcPtr(socket, argv);
}
}
 
 
 
 
typedef struct NameValue_t {
typedef struct NameValue_t {
   struct NameValue_t *next;
   struct NameValue_t *next;
Line 992... Line 1111...
#endif
#endif
 
 
static TelnetFunc_t MyFuncs[] = {
static TelnetFunc_t MyFuncs[] = {
   {"cat", ConsoleCat},
   {"cat", ConsoleCat},
   {"cp", ConsoleCp},
   {"cp", ConsoleCp},
 
   {"dump", ConsoleDump},
   {"exit", ConsoleExit},
   {"exit", ConsoleExit},
#ifdef INCLUDE_FLASH
#ifdef INCLUDE_FLASH
   {"flashErase", ConsoleFlashErase},
   {"flashErase", ConsoleFlashErase},
#endif
#endif
   {"ftp", ConsoleFtp},
   {"ftp", ConsoleFtp},
 
   {"grep", ConsoleGrep},
   {"help", ConsoleHelp},
   {"help", ConsoleHelp},
   {"ls", ConsoleLs},
   {"ls", ConsoleLs},
   {"math", ConsoleMath},
   {"math", ConsoleMath},
   {"mkdir", ConsoleMkdir},
   {"mkdir", ConsoleMkdir},
   {"mkfile", ConsoleMkfile},
   {"mkfile", ConsoleMkfile},
   {"ping", ConsolePing},
   {"ping", ConsolePing},
   {"rm", ConsoleRm},
   {"rm", ConsoleRm},
   {"tftp", ConsoleTftp},
   {"tftp", ConsoleTftp},
 
   {"uptime", ConsoleUptime},
#ifdef DLL_SETUP
#ifdef DLL_SETUP
   {"run", ConsoleRun},
   {"run", ConsoleRun},
#endif
#endif
#ifdef EDIT_FILE
#ifdef EDIT_FILE
   {"edit", EditFile},
   {"edit", EditFile},

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.