?login_element?

Subversion Repositories NedoOS

Rev

Rev 2150 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <intrz80.h>
  4. #include <stdlib.h>
  5. #include <oscalls.h>
  6. #include <../common/terminal.c>
  7. #include <tcp.h>
  8. #include <osfs.h>
  9. #include <intrz80.h>
  10. #include <graphic.h>
  11. #include <ctype.h>
  12. #include <math.h>
  13.  
  14. #define true 1
  15. #define false 0
  16. #define screenHeight 23
  17. #define screenWidth 80
  18.  
  19. unsigned int RBR_THR = 0xf8ef;
  20. unsigned int IER = 0xf9ef;
  21. unsigned int IIR_FCR = 0xfaef;
  22. unsigned int LCR = 0xfbef;
  23. unsigned int MCR = 0xfcef;
  24. unsigned int LSR = 0xfdef;
  25. unsigned int MSR = 0xfeef;
  26. unsigned int SR = 0xffef;
  27. unsigned int divider = 1;
  28. unsigned char comType = 0;
  29. unsigned int espType = 32;
  30.  
  31. FILE *fp2;
  32.  
  33. unsigned char netDriver = 0;
  34.  
  35. unsigned char uVer[] = "00.75";
  36. unsigned char curPath[128];
  37. unsigned char cmd[128];
  38. unsigned int pageOffsets[128];
  39. unsigned long volumeOffsets[16];
  40.  
  41. unsigned char crlf[2] = {13, 10};
  42.  
  43. const unsigned char gotWiFi[] = "WIFI GOT IP";
  44.  
  45. struct sockaddr_in targetadr;
  46. struct readstructure readStruct;
  47. struct sockaddr_in dnsaddress;
  48.  
  49. struct linkStruct
  50. {
  51.         unsigned char type;
  52.         unsigned long size;
  53.         unsigned char nexType;
  54.         unsigned char path[512];
  55.         unsigned char host[64];
  56.         unsigned char prevHost[64];
  57.         unsigned int port;
  58. } link;
  59.  
  60. struct mouseStruct
  61. {
  62.         char lmb;
  63.         char rmb;
  64.         char mmb;
  65.         char wheel;
  66.         char prevWheel;
  67.         char mouseXpos;
  68.         char mouseYpos;
  69.         char cursXpos;
  70.         char cursYpos;
  71.         unsigned int prevMouseButtons;
  72.         char prevMouseMove;
  73.         char oldAtr;
  74. } mouse;
  75.  
  76. struct navigationStruct
  77. {
  78.         unsigned int page;
  79.         unsigned int volume;
  80.         unsigned int maxVolume;
  81.         unsigned int maxPage;
  82.         unsigned int linePage;
  83.         unsigned int lineSelect;
  84.         unsigned int lastLine;
  85.         unsigned int prevLineSelect;
  86.         unsigned int bufPos;
  87.         unsigned int nextBufPos;
  88.         unsigned int history;
  89.         unsigned int saveAs;
  90.         unsigned char fileName[128];
  91. } navi;
  92.  
  93. struct window
  94. {
  95.         unsigned char x;
  96.         unsigned char y;
  97.         unsigned char w;
  98.         unsigned char h;
  99.         unsigned char text;
  100.         unsigned char back;
  101.         unsigned char tittle[80];
  102. } curWin;
  103.  
  104. unsigned char nvext[1024];
  105.  
  106. unsigned char netbuf[32768];
  107.  
  108. void spaces(unsigned char number)
  109. {
  110.         while (number > 0)
  111.         {
  112.                 putchar(' ');
  113.                 number--;
  114.         }
  115. }
  116.  
  117. void clearStatus(void)
  118. {
  119.         OS_SETCOLOR(5);
  120.         OS_SETXY(0, 24);
  121.         spaces(79);
  122.         putchar('\r');
  123. }
  124.  
  125. void printTable(void)
  126. {
  127.         unsigned int cycle;
  128.  
  129.         for (cycle = 1; cycle < 256; cycle++)
  130.         {
  131.                 OS_SETCOLOR(7);
  132.                 printf("%03u:", cycle);
  133.                 OS_SETCOLOR(71);
  134.                 putchar(cycle);
  135.                 OS_SETCOLOR(7);
  136.                 printf(" ");
  137.                 if (cycle % 12 == 0)
  138.                 {
  139.                         printf("\r\n");
  140.                 }
  141.         }
  142. }
  143.  
  144. void delay(unsigned long counter)
  145. {
  146.         unsigned long start, finish;
  147.         counter = counter / 20;
  148.         if (counter < 1)
  149.         {
  150.                 counter = 1;
  151.         }
  152.         start = time();
  153.         finish = start + counter;
  154.  
  155.         while (start < finish)
  156.         {
  157.                 start = time();
  158.         }
  159. }
  160.  
  161. ///////////////////////////
  162. #include <../common/esp-com.c>
  163. #include <../common/network.c>
  164. //////////////////////////
  165.  
  166. unsigned char saveBuf(unsigned char *fileNamePtr, unsigned char operation, unsigned int sizeOfBuf)
  167. {
  168.         if (operation == 00)
  169.         {
  170.                 fp2 = OS_CREATEHANDLE(fileNamePtr, 0x80);
  171.                 if (((int)fp2) & 0xff)
  172.                 {
  173.                         clearStatus();
  174.                         printf("%s", fileNamePtr);
  175.                         printf(" creating error.");
  176.                         exit(0);
  177.                 }
  178.                 OS_CLOSEHANDLE(fp2);
  179.  
  180.                 fp2 = OS_OPENHANDLE(fileNamePtr, 0x80);
  181.                 if (((int)fp2) & 0xff)
  182.                 {
  183.                         clearStatus();
  184.                         printf("%s", fileNamePtr);
  185.                         printf(" opening error. ");
  186.  
  187.                         exit(0);
  188.                 }
  189.                 return 0;
  190.         }
  191.  
  192.         if (operation == 01)
  193.         {
  194.                 OS_WRITEHANDLE(netbuf, fp2, sizeOfBuf);
  195.                 return 0;
  196.         }
  197.  
  198.         if (operation == 02)
  199.         {
  200.                 OS_CLOSEHANDLE(fp2);
  201.                 return 0;
  202.         }
  203.         return 0;
  204. }
  205.  
  206. void drawClock(void)
  207. {
  208.         unsigned long dosTime;
  209.         unsigned int hours, minutes;
  210.         dosTime = OS_GETTIME();
  211.         hours = dosTime >> 11 & 31;        // 0b00011111
  212.         minutes = (dosTime >> 5) & 63; // 0b00111111
  213.  
  214.         OS_SETXY(73, 0);
  215.         printf("[%02u:%02u]", hours, minutes);
  216. }
  217.  
  218. void mainWinDraw(void)
  219. {
  220.         OS_SETCOLOR(207);
  221.         OS_SETXY(0, 0);
  222.         spaces(80);
  223.         OS_SETXY(0, 0);
  224.         printf("NedoGopher %s", uVer);
  225.  
  226.         OS_SETXY(39 - strlen(link.host) / 2, 0);
  227.         printf("%s", link.host);
  228.  
  229.         OS_SETXY(55, 0);
  230.  
  231.         if (netDriver)
  232.         {
  233.                 printf("[ESP-COM]");
  234.         }
  235.         else
  236.         {
  237.                 printf("[NEDONET]");
  238.         }
  239.         OS_SETXY(64, 0);
  240.         if (navi.saveAs)
  241.         {
  242.                 printf("[Save As]");
  243.         }
  244.         else
  245.         {
  246.                 printf("[Play It]");
  247.         }
  248.  
  249.         drawClock();
  250. }
  251.  
  252. void initMouse(void)
  253. {
  254.         unsigned long mouseRaw;
  255.         unsigned char mouseMove;
  256.         unsigned int mouseButtons;
  257.  
  258.         mouseRaw = OS_GETMOUSE();
  259.         mouseMove = mouseRaw >> 16;
  260.         mouseButtons = mouseRaw;
  261.  
  262.         mouse.wheel = (mouseButtons >> 4) & 15;
  263.         mouse.prevWheel = mouse.wheel;
  264. }
  265.  
  266. unsigned char getMouse(void)
  267. {
  268.         unsigned long mouseRaw;
  269.         unsigned char mouseMove;
  270.         unsigned int mouseButtons;
  271.  
  272.         mouseRaw = OS_GETMOUSE();
  273.         mouseMove = mouseRaw >> 16;
  274.         mouseButtons = mouseRaw;
  275.  
  276.         if (mouseMove != mouse.prevMouseMove)
  277.         {
  278.                 OS_SETXY(mouse.cursXpos, mouse.cursYpos);
  279.                 OS_PRATTR(mouse.oldAtr);
  280.                 mouse.prevMouseMove = mouseMove;
  281.  
  282.                 mouse.mouseXpos = mouseRaw >> 16;
  283.                 mouse.mouseYpos = mouseRaw >> 24;
  284.  
  285.                 mouse.cursXpos = mouse.mouseXpos / 3;
  286.                 mouse.cursYpos = 25 - mouse.mouseYpos / 10;
  287.  
  288.                 if (mouse.cursXpos > 79)
  289.                 {
  290.                         mouse.cursXpos = 79;
  291.                 }
  292.                 if (mouse.cursYpos > 25)
  293.                 {
  294.                         mouse.cursYpos = 25;
  295.                 }
  296.  
  297.                 OS_SETXY(mouse.cursXpos, mouse.cursYpos);
  298.                 mouse.oldAtr = OS_GETATTR();
  299.                 OS_PRATTR(215);
  300.         }
  301.  
  302.         if (mouseButtons != mouse.prevMouseButtons)
  303.         {
  304.  
  305.                 mouse.prevWheel = mouse.wheel;
  306.                 mouse.wheel = (mouseButtons >> 4) & 15;
  307.                 mouse.mmb = (mouseButtons >> 2) & 1;
  308.                 mouse.rmb = (mouseButtons >> 1) & 1;
  309.                 mouse.lmb = mouseButtons & 1;
  310.         }
  311.  
  312.         // clearStatus();
  313.         // printf("lmb:[%d] rmb:[%d] mmb:[%d] wheel:[%02d] X:[%03d] Y:[%03d] cursX:[%02d] cursY:[%02d]", mouse.lmb, mouse.rmb, mouse.mmb, mouse.wheel, mouse.mouseXpos, mouse.mouseYpos, mouse.cursXpos, mouse.cursYpos);
  314.  
  315.         OS_SETXY(mouse.cursXpos, mouse.cursYpos);
  316.  
  317.         return mouseButtons >> 8;
  318. }
  319.  
  320. unsigned char OS_SHELL(unsigned char *command)
  321. {
  322.         unsigned char fileName[] = "cmd.com";
  323.         unsigned char appCmd[128] = "cmd.com ";
  324.         unsigned int shellSize, loaded, loop, adr;
  325.         unsigned char pgbak;
  326.         union APP_PAGES shell_pg;
  327.         union APP_PAGES main_pg;
  328.         FILE *fp3;
  329.         main_pg.l = OS_GETMAINPAGES();
  330.         pgbak = main_pg.pgs.window_3;
  331.         OS_GETPATH((unsigned int)&curPath);
  332.         OS_SETSYSDRV();
  333.         strcat(appCmd, command);
  334.         fp3 = OS_OPENHANDLE(fileName, 0x80);
  335.         if (((int)fp3) & 0xff)
  336.         {
  337.                 clearStatus();
  338.                 printf("%s", fileName);
  339.                 printf(" not found.");
  340.                 do
  341.                 {
  342.                         YIELD();
  343.                 } while (_low_level_get() == 0);
  344.                 exit(0);
  345.         }
  346.  
  347.         shellSize = OS_GETFILESIZE(fp3);
  348.  
  349.         OS_CHDIR(curPath);
  350.  
  351.         OS_NEWAPP((unsigned int)&shell_pg);
  352.         shell_pg.l = OS_GETAPPMAINPAGES(shell_pg.pgs.pId);
  353.         SETPG32KHIGH(shell_pg.pgs.window_0);
  354.         memcpy((unsigned char *)(0xC080), (unsigned char *)(&appCmd), strlen(appCmd) + 1);
  355.  
  356.         loop = 0;
  357.         while (loop < shellSize)
  358.         {
  359.                 loaded = OS_READHANDLE(cmd, fp3, sizeof(cmd) - 1);
  360.                 adr = 0xC100 + loop;
  361.                 memcpy((unsigned char *)(adr), &cmd, loaded);
  362.                 loop = loop + loaded;
  363.         }
  364.         OS_CLOSEHANDLE(fp3);
  365.         SETPG32KHIGH(pgbak);
  366.         OS_RUNAPP(shell_pg.pgs.pId);
  367.         return shell_pg.pgs.pId;
  368. }
  369.  
  370. char loadPageFromDisk(unsigned char *filepath, unsigned int volume)
  371. {
  372.         unsigned int todo = 0;
  373.         unsigned long clean = 0, loaded = 0;
  374.         FILE *fp1;
  375.  
  376.         fp1 = OS_OPENHANDLE(filepath, 0x80);
  377.         if (((int)fp1) & 0xff)
  378.         {
  379.                 printf("%s opening error. ", filepath);
  380.                 return false;
  381.         }
  382.  
  383.         OS_SEEKHANDLE(fp1, volumeOffsets[volume]);
  384.         do
  385.         {
  386.                 if ((sizeof(netbuf) - loaded) < 513)
  387.                 {
  388.                         //clearStatus();
  389.                         //printf("Файл слишком большой, будет загружаться частями (%ld kb)...", link.size / 1024);
  390.                         break;
  391.                 }
  392.  
  393.                 todo = OS_READHANDLE(netbuf + loaded, fp1, 512);
  394.                 loaded = loaded + todo;
  395.  
  396.         } while (todo != 0 && errno == 0);
  397.         OS_CLOSEHANDLE(fp1);
  398.         netbuf[loaded + 1] = 0;
  399.  
  400.         if (todo == 0 && errno == 0)
  401.         {
  402.                 navi.maxVolume = volume;
  403.         }
  404.         volumeOffsets[volume + 1] = volumeOffsets[volume] + loaded;
  405.  
  406.         clean = loaded + 128;
  407.         do
  408.         {
  409.                 netbuf[loaded] = 0;
  410.                 loaded++;
  411.         } while (loaded < clean);
  412.  
  413.         return true;
  414. }
  415.  
  416. void loadNVext(void)
  417. {
  418.         FILE *nvf;
  419.         unsigned int nvextSize, loop, loaded;
  420.         OS_SETSYSDRV();
  421.         nvf = OS_OPENHANDLE("nv.ext", 0x80);
  422.         if (((int)nvf) & 0xff)
  423.         {
  424.                 clearStatus();
  425.                 printf("nv.ext not found.\r\n");
  426.                 exit(0);
  427.         }
  428.         nvextSize = OS_GETFILESIZE(nvf);
  429.  
  430.         loop = 0;
  431.         loaded = 0;
  432.         while (loop < nvextSize)
  433.         {
  434.                 loaded = OS_READHANDLE(nvext + loaded, nvf, sizeof(nvext) - 1);
  435.                 loop = loop + loaded;
  436.         }
  437.         OS_CLOSEHANDLE(nvf);
  438.         nvext[loop + 1] = 0;
  439. }
  440.  
  441. void init(void)
  442. {
  443.         targetadr.family = AF_INET;
  444.         targetadr.porth = 00;
  445.         targetadr.portl = 70;
  446.         targetadr.b1 = 0;
  447.         targetadr.b2 = 0;
  448.         targetadr.b3 = 0;
  449.         targetadr.b4 = 0;
  450.         mouse.oldAtr = 79;
  451.         navi.lineSelect = 1;
  452.         navi.prevLineSelect = 2;
  453.         navi.nextBufPos = 0;
  454.         navi.page = 0;
  455.         navi.maxPage = 32767;
  456.         navi.maxVolume = 32767;
  457.         navi.volume = 0;
  458.         volumeOffsets[0] = 0;
  459.         navi.saveAs = true;
  460.  
  461.         mouse.prevMouseButtons = 0;
  462.  
  463.         link.type = '1';
  464.         strcpy(link.host, "HOMEPAGE");
  465.         get_dns();
  466.         loadNVext();
  467.         loadEspConfig();
  468.         initMouse();
  469. }
  470.  
  471. void newPage(void)
  472. {
  473.         unsigned int counter = 0;
  474.         navi.page = 0;
  475.         navi.maxPage = 32767;
  476.         navi.maxVolume = 32767;
  477.         navi.linePage = 0;
  478.         navi.lineSelect = 1;
  479.         navi.prevLineSelect = 2;
  480.         navi.bufPos = 0;
  481.         navi.nextBufPos = 0;
  482.         volumeOffsets[0] = 0;
  483.         /*
  484.                 do
  485.                 {
  486.                         pageOffsets[counter] = 0;
  487.                         counter++;
  488.                 } while (counter < 127);
  489.         */
  490. }
  491.  
  492. void renderType(unsigned char linkType)
  493. {
  494.         OS_SETCOLOR(70);
  495.  
  496.         switch (linkType)
  497.         {
  498.         case 'i':
  499.                 putchar(' ');
  500.                 break;
  501.         case '0':
  502.                 putchar(21); // plain text
  503.                 putchar(' ');
  504.                 break;
  505.         case '1':
  506.                 putchar(16); // directory
  507.                 putchar(' ');
  508.                 break;
  509.         case '3':
  510.                 putchar(15); // error link
  511.                 putchar(' ');
  512.                 break;
  513.         case '5': // Dos zip
  514.                 putchar('Z');
  515.                 putchar(' ');
  516.                 break;
  517.         case '6': // uuencoded file
  518.                 putchar('Z');
  519.                 putchar(' ');
  520.                 break;
  521.         case '7': // search input
  522.                 putchar(253);
  523.                 putchar(' ');
  524.                 break;
  525.         case '8': // Telnet session
  526.                 putchar('T');
  527.                 putchar(' ');
  528.                 break;
  529.         case '9': // binary (pt3/scr)
  530.                 putchar(8);
  531.                 putchar(' ');
  532.                 break;
  533.         case 'g': // gif pic
  534.                 putchar(2);
  535.                 putchar(' ');
  536.                 break;
  537.         case 'I': // image
  538.                 putchar(2);
  539.                 putchar(' ');
  540.                 break;
  541.         case 's': // sound
  542.                 putchar(14);
  543.                 putchar(' ');
  544.                 break;
  545.         default:
  546.                 putchar(linkType);
  547.                 break;
  548.         }
  549.         OS_SETCOLOR(7);
  550. }
  551.  
  552. unsigned int renderPlain(unsigned int bufPos)
  553. {
  554.         unsigned int counter = 0, colCount = 0;
  555.         unsigned int byte = 0, flag = true;
  556.  
  557.         link.type = '0';
  558.  
  559.         OS_CLS(0);
  560.         mainWinDraw();
  561.         OS_SETCOLOR(7);
  562.         OS_SETXY(0, 1);
  563.  
  564.         counter = 0;
  565.         do
  566.         {
  567.                 byte = netbuf[bufPos];
  568.                 if (byte == 0)
  569.                 {
  570.                         navi.maxPage = navi.page;
  571.                         return bufPos;
  572.                 }
  573.  
  574.                 if (colCount == 80)
  575.                 {
  576.                         counter++;
  577.                         colCount = 0;
  578.                 }
  579.  
  580.                 if (byte == 0xd)
  581.                 {
  582.                         if (colCount != 80)
  583.                         {
  584.                                 putchar('\r');
  585.                                 flag = true;
  586.                         }
  587.                         else
  588.                         {
  589.                                 flag = false;
  590.                         }
  591.  
  592.                         bufPos++;
  593.                         colCount = 0;
  594.                         continue;
  595.                 }
  596.  
  597.                 if (byte == 0xa)
  598.                 {
  599.                         if (flag)
  600.                         {
  601.                                 putchar('\n');
  602.                         }
  603.  
  604.                         bufPos++;
  605.                         counter++;
  606.                         flag = true;
  607.                         continue;
  608.                 }
  609.  
  610.                 putchar(byte);
  611.  
  612.                 colCount++;
  613.                 bufPos++;
  614.  
  615.         } while (counter < screenHeight);
  616.         return bufPos;
  617. }
  618.  
  619. unsigned int renderPage(unsigned int bufPos)
  620. {
  621.         unsigned char counter = 0, colCount = 0;
  622.         unsigned char byte = 0;
  623.  
  624.         link.type = '1';
  625.  
  626.         OS_CLS(0);
  627.         mainWinDraw();
  628.         OS_SETXY(0, 1);
  629.  
  630.         byte = netbuf[bufPos];
  631.         renderType(byte);
  632.  
  633.         OS_SETCOLOR(7);
  634.         do
  635.         {
  636.                 while (42)
  637.                 {
  638.                         bufPos++;
  639.  
  640.                         byte = netbuf[bufPos];
  641.  
  642.                         if (byte == 9)
  643.                         {
  644.                                 putchar('\r');
  645.                                 putchar('\n');
  646.                                 break;
  647.                         }
  648.  
  649.                         if (byte == 0)
  650.                         {
  651.                                 navi.maxPage = navi.page;
  652.                                 return bufPos;
  653.                         }
  654.                         colCount++;
  655.                         if (colCount < 78)
  656.                         {
  657.                                 putchar(byte);
  658.                         }
  659.                 }
  660.                 while (42)
  661.                 {
  662.                         bufPos++;
  663.                         if (netbuf[bufPos] == 10)
  664.                         {
  665.                                 colCount = 0;
  666.                                 counter++;
  667.                                 bufPos++;
  668.                                 if (netbuf[bufPos] == '.')
  669.                                 {
  670.                                         navi.maxPage = navi.page;
  671.                                         navi.lastLine = counter;
  672.                                         return bufPos;
  673.                                 }
  674.                                 if (counter < screenHeight)
  675.                                 {
  676.                                         renderType(netbuf[bufPos]);
  677.                                 }
  678.                                 break;
  679.                         }
  680.                 }
  681.         } while (counter < screenHeight);
  682.         navi.lastLine = counter;
  683.         return bufPos;
  684. }
  685.  
  686. void reDraw(void)
  687. {
  688.         if (link.type == '0')
  689.         {
  690.                 navi.nextBufPos = renderPlain(pageOffsets[navi.page]);
  691.         }
  692.         else
  693.         {
  694.                 if (link.type == '1')
  695.                 {
  696.                         navi.nextBufPos = renderPage(pageOffsets[navi.page]);
  697.                 }
  698.         }
  699. }
  700.  
  701. void errorBox(struct window w, unsigned char *message)
  702. {
  703.         unsigned char wcount, tempx, tittleStart;
  704.  
  705.         w.h++;
  706.         OS_SETXY(w.x, w.y - 1);
  707.         BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  708.         OS_SETXY(w.x, w.y);
  709.         OS_SETCOLOR(w.text);
  710.         putchar(201);
  711.         for (wcount = 0; wcount < w.w; wcount++)
  712.         {
  713.                 putchar(205);
  714.         }
  715.         putchar(187);
  716.         OS_SETXY(w.x, w.y + w.h);
  717.         putchar(200);
  718.         for (wcount = 0; wcount < w.w; wcount++)
  719.         {
  720.                 putchar(205);
  721.         }
  722.         putchar(188);
  723.  
  724.         tempx = w.x + w.w + 1;
  725.         for (wcount = 1; wcount < w.h; wcount++)
  726.         {
  727.                 OS_SETXY(w.x, w.y + wcount);
  728.                 putchar(186);
  729.                 OS_SETXY(tempx, w.y + wcount);
  730.                 putchar(186);
  731.         }
  732.         tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  733.         OS_SETXY(tittleStart, w.y);
  734.         printf("[%s]", w.tittle);
  735.         OS_SETXY(w.x + 1, w.y + 1);
  736.         OS_SETCOLOR(w.back);
  737.         tittleStart = w.x + (w.w / 2) - (strlen(message) / 2);
  738.         OS_SETXY(tittleStart, w.y + 1);
  739.         printf("%s", message);
  740. }
  741.  
  742. unsigned char inputBox(struct window w, unsigned char *prefilled)
  743. {
  744.         unsigned char wcount, tempx, tittleStart;
  745.         unsigned char byte, counter;
  746.         w.h++;
  747.         OS_SETXY(w.x, w.y - 1);
  748.         BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  749.         OS_SETXY(w.x, w.y);
  750.         OS_SETCOLOR(w.text);
  751.         putchar(201);
  752.         for (wcount = 0; wcount < w.w; wcount++)
  753.         {
  754.                 putchar(205);
  755.         }
  756.         putchar(187);
  757.         OS_SETXY(w.x, w.y + w.h);
  758.         putchar(200);
  759.         for (wcount = 0; wcount < w.w; wcount++)
  760.         {
  761.                 putchar(205);
  762.         }
  763.         putchar(188);
  764.  
  765.         tempx = w.x + w.w + 1;
  766.         for (wcount = 1; wcount < w.h; wcount++)
  767.         {
  768.                 OS_SETXY(w.x, w.y + wcount);
  769.                 putchar(186);
  770.                 OS_SETXY(tempx, w.y + wcount);
  771.                 putchar(186);
  772.         }
  773.         tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  774.         OS_SETXY(tittleStart, w.y);
  775.         printf("[%s]", w.tittle);
  776.         OS_SETXY(w.x + 1, w.y + 1);
  777.         OS_SETCOLOR(w.back);
  778.         putchar(219);
  779.  
  780.         cmd[0] = 0;
  781.  
  782.         counter = strlen(prefilled);
  783.         if (counter != 0)
  784.         {
  785.                 strcpy(cmd, prefilled);
  786.                 goto skipKeys;
  787.         }
  788.  
  789.         do
  790.         {
  791.                 byte = OS_GETKEY();
  792.                 if (byte != 0)
  793.                 {
  794.                         switch (byte)
  795.                         {
  796.                         case 0x08:
  797.                                 if (counter > 0)
  798.                                 {
  799.                                         counter--;
  800.                                         cmd[counter] = 0;
  801.                                 }
  802.                                 break;
  803.                         case 0x0d:
  804.  
  805.                                 if (counter == 0)
  806.                                 {
  807.                                         return false;
  808.                                 }
  809.                                 else
  810.                                 {
  811.                                         return true;
  812.                                 }
  813.  
  814.                         case 31:
  815.                                 break;
  816.                         case 250:
  817.                                 break;
  818.                         case 249:
  819.                                 break;
  820.                         case 248:
  821.                                 break;
  822.                         case 251: // Right
  823.                                 break;
  824.                         case 252: // Del
  825.                                 OS_SETXY(w.x + 1, w.y + 1);
  826.                                 spaces(counter + 1);
  827.                                 cmd[0] = 0;
  828.                                 counter = 0;
  829.                                 break;
  830.                         case 27:
  831.                                 cmd[0] = 0;
  832.                                 return false;
  833.                         default:
  834.                                 if (counter < w.w - 1)
  835.                                 {
  836.                                         cmd[counter] = byte;
  837.                                         counter++;
  838.                                         cmd[counter] = 0;
  839.                                 }
  840.                                 break;
  841.                         }
  842.                 skipKeys:
  843.                         OS_SETXY(w.x + 1, w.y + 1);
  844.                         printf("%s", cmd);
  845.                         putchar(219);
  846.                         if (byte == 0x08)
  847.                         {
  848.                                 putchar(' ');
  849.                         }
  850.                 }
  851.                 YIELD();
  852.         } while (42);
  853.         return false;
  854. }
  855.  
  856. void errNoConnect(void)
  857. {
  858.         if (strcmp(link.host, "HOMEPAGE") == 0)
  859.         {
  860.                 return;
  861.         }
  862.         curWin.w = 50;
  863.         curWin.x = 80 / 2 - curWin.w / 2 - 1;
  864.         curWin.y = 10;
  865.         curWin.h = 1;
  866.         curWin.text = 215;
  867.         curWin.back = 215;
  868.         strcpy(curWin.tittle, "Ошибка открытия страницы");
  869.         strcpy(cmd, "Нет соединения с ");
  870.         strcat(cmd, link.host);
  871.         errorBox(curWin, cmd);
  872.         getchar();
  873. }
  874.  
  875. char getFileEsp(unsigned char *fileNamePtr)
  876. {
  877.         int todo;
  878.         unsigned char *count1;
  879.         unsigned char byte;
  880.         unsigned long downloaded = 0;
  881.         unsigned int count;
  882.         const unsigned char sendOk[] = "SEND OK";
  883.  
  884.         if ((strlen(link.path) == 1 && link.path[0] == '/') || strlen(link.path) == 0)
  885.         {
  886.                 strcpy(link.path, "\r\n");
  887.         }
  888.         else
  889.         {
  890.                 strcat(link.path, "\r\n");
  891.         }
  892.  
  893.         sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%u", link.host, link.port);
  894.         sendcommand(cmd);
  895.         do
  896.         {
  897.                 getAnswer2(); // CONNECT or ERROR or link is not valid
  898.                 count1 = strstr(netbuf, "CONNECT");
  899.                 if (strstr(netbuf, "ERROR") != NULL)
  900.                 {
  901.                         return false;
  902.                 }
  903.         } while (count1 == NULL);
  904.  
  905.         getAnswer2(); // OK
  906.  
  907.         sprintf(cmd, "AT+CIPSEND=%u", strlen(link.path)); // second CRLF in send command
  908.         sendcommand(cmd);
  909.         getAnswer2();
  910.  
  911.         do
  912.         {
  913.                 byte = uart_readBlock();
  914.         } while (byte != '>');
  915.  
  916.         sendcommandNrn(link.path);
  917.  
  918.         count = 0;
  919.         do
  920.         {
  921.                 byte = uart_readBlock();
  922.                 if (byte == sendOk[count])
  923.                 {
  924.                         count++;
  925.                         // putchar(byte);
  926.                 }
  927.                 else
  928.                 {
  929.                         count = 0;
  930.                 }
  931.         } while (count < strlen(sendOk));
  932.  
  933.         uart_readBlock(); // CR
  934.         uart_readBlock(); // LF
  935.  
  936.         saveBuf(fileNamePtr, 00, 0);
  937.         clearStatus();
  938.         do
  939.         {
  940.                 todo = recvHead();
  941.                 getdataEsp(todo);
  942.                 downloaded = downloaded + todo;
  943.                 printf("%lu kb \r", downloaded / 1024);
  944.                 saveBuf(fileNamePtr, 01, todo);
  945.         } while (todo != 0);
  946.         saveBuf(fileNamePtr, 02, 00);
  947.         // getAnswer2(); // OK
  948.         link.size = downloaded;
  949.         clearStatus();
  950.         if (downloaded == 0)
  951.         {
  952.                 return false;
  953.         }
  954.         return true;
  955. }
  956.  
  957. char getFile(unsigned char *fileNamePtr)
  958. {
  959.         int todo;
  960.         int socket;
  961.         unsigned long downloaded = 0;
  962.  
  963.         if (strcmp(link.host, "HOMEPAGE") == 0)
  964.         {
  965.                 return false;
  966.         }
  967.  
  968.         if (netDriver == 1)
  969.         {
  970.                 return getFileEsp(fileNamePtr);
  971.         }
  972.  
  973.         if (!dnsResolve(link.host))
  974.         {
  975.                 return false;
  976.         }
  977.  
  978.         targetadr.porth = 00;
  979.         targetadr.portl = link.port;
  980.  
  981.         // clearStatus();
  982.         // printf("File:%s", fileNamePtr);
  983.         // printf("\r\nAddress:%u.%u.%u.%u:%u\r\n", targetadr.b1, targetadr.b2, targetadr.b3, targetadr.b4, targetadr.porth * 256 + targetadr.portl);
  984.         // getchar();
  985.  
  986.         if ((strlen(link.path) == 1 && link.path[0] == '/') || strlen(link.path) == 0)
  987.         {
  988.                 strcpy(link.path, crlf);
  989.         }
  990.         else
  991.         {
  992.                 strcat(link.path, crlf);
  993.         }
  994.         socket = OpenSock(AF_INET, SOCK_STREAM);
  995.         if (socket < 0)
  996.         {
  997.                 return false;
  998.         }
  999.         todo = netConnect(socket, 1);
  1000.         if (socket < 0)
  1001.         {
  1002.                 return false;
  1003.         }
  1004.         todo = tcpSend(socket, (unsigned int)&link.path, strlen(link.path), 1);
  1005.         if (socket < 0)
  1006.         {
  1007.                 return false;
  1008.         }
  1009.         saveBuf(fileNamePtr, 00, 0);
  1010.         clearStatus();
  1011.         do
  1012.         {
  1013.                 todo = tcpRead(socket, 3);
  1014.                 if (todo < 1)
  1015.                 {
  1016.                         break;
  1017.                 }
  1018.  
  1019.                 downloaded = downloaded + todo;
  1020.                 printf("%lu kb   \r", downloaded / 1024);
  1021.                 saveBuf(fileNamePtr, 01, todo);
  1022.         } while (42);
  1023.         clearStatus();
  1024.         netShutDown(socket, 0);
  1025.         saveBuf(fileNamePtr, 02, 00);
  1026.         link.size = downloaded;
  1027.         if (downloaded == 0)
  1028.         {
  1029.                 return false;
  1030.         }
  1031.         return true;
  1032. }
  1033.  
  1034. unsigned char selectorProcessor(void)
  1035. {
  1036.         unsigned int startSearch = 0, lineSearch = 0, SelectedPos, counter1 = 0;
  1037.         unsigned char byte;
  1038.  
  1039.         if (link.type == '0' || navi.lineSelect > navi.lastLine) // Если текущая страница текстовая, нечего по ней тыкать или тыкнули ниже низа.
  1040.         {
  1041.                 // clearStatus();
  1042.                 // printf("Если текущая страница текстовая, нечего по ней тыкать или тыкнули ниже низа.");
  1043.                 return false;
  1044.         }
  1045.  
  1046.         startSearch = pageOffsets[navi.page];
  1047.  
  1048.         do
  1049.         {
  1050.                 byte = netbuf[startSearch + counter1];
  1051.  
  1052.                 if (byte == 0x0a)
  1053.                 {
  1054.                         lineSearch++;
  1055.                 }
  1056.                 counter1++;
  1057.  
  1058.         } while (lineSearch < navi.lineSelect - 1);
  1059.  
  1060.         if (counter1 == 1)
  1061.         {
  1062.                 counter1 = 0;
  1063.         }
  1064.         SelectedPos = startSearch + counter1;
  1065.  
  1066.         strcpy(link.prevHost, link.host);
  1067.         link.nexType = link.type;
  1068.         link.type = netbuf[SelectedPos];
  1069.  
  1070.         if (link.type == 'i' || link.type == '.' || link.type == 0)
  1071.         {
  1072.                 link.type = link.nexType;
  1073.                 return false;
  1074.         }
  1075.  
  1076.         counter1 = 1; // Пропускаем  заголовок селектора
  1077.         do
  1078.         {
  1079.                 byte = netbuf[SelectedPos + counter1];
  1080.                 counter1++;
  1081.         } while (byte != 9);
  1082.  
  1083.         SelectedPos = SelectedPos + counter1;
  1084.         counter1 = 0; // Извлекаем путь к селектору
  1085.  
  1086.         while (netbuf[SelectedPos + counter1] != 9)
  1087.         {
  1088.                 link.path[counter1] = netbuf[SelectedPos + counter1];
  1089.                 counter1++;
  1090.         }
  1091.         link.path[counter1] = 0;
  1092.  
  1093.         SelectedPos = SelectedPos + counter1 + 1;
  1094.         counter1 = 0; // Извлекаем хост селектора
  1095.         do
  1096.         {
  1097.                 link.host[counter1] = netbuf[SelectedPos + counter1];
  1098.                 counter1++;
  1099.         } while (netbuf[SelectedPos + counter1] != 9);
  1100.         link.host[counter1] = 0;
  1101.  
  1102.         SelectedPos = SelectedPos + counter1 + 1;
  1103.         link.port = atoi(netbuf + SelectedPos);
  1104.         return true;
  1105. }
  1106.  
  1107. void pusHistory(void)
  1108. {
  1109.         FILE *hf;
  1110.         unsigned char *historyBytes;
  1111.         unsigned char buf[1];
  1112.         unsigned int structSize, filePos, counter;
  1113.  
  1114.         if (link.type == '7')
  1115.         {
  1116.                 return;
  1117.         }
  1118.  
  1119.         navi.history++;
  1120.         structSize = sizeof(struct linkStruct);
  1121.         filePos = structSize * navi.history;
  1122.  
  1123.         OS_SETSYSDRV();
  1124.  
  1125.         hf = OS_CREATEHANDLE("browser/ng_hist.dat", 0x80);
  1126.         if (((int)hf) & 0xff)
  1127.         {
  1128.                 clearStatus();
  1129.                 printf("browser/ng_hist.dat creating error.");
  1130.                 exit(0);
  1131.         }
  1132.         OS_CLOSEHANDLE(hf);
  1133.  
  1134.         hf = OS_OPENHANDLE("browser/ng_hist.dat", 0x80);
  1135.         if (((int)hf) & 0xff)
  1136.         {
  1137.                 clearStatus();
  1138.                 printf("browser/ng_hist.dat opening error.");
  1139.                 exit(0);
  1140.         }
  1141.         OS_SEEKHANDLE(hf, filePos);
  1142.  
  1143.         historyBytes = (unsigned char *)&link;
  1144.  
  1145.         for (counter = 0; counter < structSize; counter++)
  1146.         {
  1147.                 buf[0] = historyBytes[counter];
  1148.                 OS_WRITEHANDLE(buf, hf, 1);
  1149.         }
  1150.         OS_CLOSEHANDLE(hf);
  1151. }
  1152.  
  1153. void popHistory(void)
  1154. {
  1155.         FILE *hf;
  1156.         unsigned int structSize, filePos;
  1157.         navi.history--;
  1158.         structSize = sizeof(struct linkStruct);
  1159.         filePos = structSize * navi.history;
  1160.         OS_SETSYSDRV();
  1161.         hf = OS_OPENHANDLE("browser/ng_hist.dat", 0x80);
  1162.         if (((int)hf) & 0xff)
  1163.         {
  1164.                 clearStatus();
  1165.                 printf("browser/ng_hist.dat opening error.");
  1166.                 exit(0);
  1167.         }
  1168.         OS_SEEKHANDLE(hf, filePos);
  1169.         OS_READHANDLE(netbuf, hf, structSize);
  1170.  
  1171.         memcpy(&link, netbuf, structSize);
  1172. }
  1173.  
  1174. char extractName(void)
  1175. {
  1176.         unsigned int counter, counter2 = 0, lng, byte, source;
  1177.         unsigned char ext2[128];
  1178.         unsigned char *count1;
  1179.  
  1180.         lng = strlen(link.path);
  1181.  
  1182.         for (counter = lng - 1; counter != 0; counter--)
  1183.         {
  1184.                 byte = link.path[counter];
  1185.                 if (byte == '/' || byte == ':')
  1186.                 {
  1187.                         break;
  1188.                 }
  1189.  
  1190.                 counter2++;
  1191.         }
  1192.         source = lng - counter2;
  1193.  
  1194.         for (counter = 0; counter < counter2; counter++)
  1195.         {
  1196.                 navi.fileName[counter] = link.path[source + counter];
  1197.         }
  1198.         navi.fileName[counter2] = 0;
  1199.  
  1200.         if (navi.saveAs)
  1201.         {
  1202.                 curWin.w = 61;
  1203.                 curWin.x = 80 / 2 - curWin.w / 2 - 1;
  1204.                 curWin.y = 10;
  1205.                 curWin.h = 1;
  1206.                 curWin.text = 103;
  1207.                 curWin.back = 103;
  1208.                 strcpy(curWin.tittle, "Введите имя файла");
  1209.  
  1210.                 lng = strlen(navi.fileName);
  1211.                 if (lng > 60)
  1212.                 {
  1213.                         lng = lng - 64 - 1;
  1214.                 }
  1215.                 else
  1216.                 {
  1217.                         lng = 0;
  1218.                 }
  1219.  
  1220.                 if (inputBox(curWin, navi.fileName + lng))
  1221.                 {
  1222.                         strcpy(navi.fileName, cmd);
  1223.                 }
  1224.                 else
  1225.                 {
  1226.                         return false;
  1227.                 }
  1228.         }
  1229.         else // Play It
  1230.         {
  1231.                 count1 = strstr(navi.fileName, ".");
  1232.                 if (count1 == NULL)
  1233.                 {
  1234.                         clearStatus();
  1235.                         printf("Ошибка определения типа файла, не найдено расширение. [%s]", navi.fileName);
  1236.                         getchar();
  1237.                         return false;
  1238.                 }
  1239.                 else
  1240.                 {
  1241.                         strcpy(ext2, count1 + 1);
  1242.                         strcpy(navi.fileName, "current.");
  1243.                         strcat(navi.fileName, ext2);
  1244.                 }
  1245.         }
  1246.         return true;
  1247. }
  1248.  
  1249. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  1250. {
  1251.         unsigned int i, j;
  1252.         unsigned int lenC, lenS;
  1253.  
  1254.         for (lenC = 0; c[lenC]; lenC++)
  1255.                 ;
  1256.         for (lenS = 0; s[lenS]; lenS++)
  1257.                 ;
  1258.  
  1259.         for (i = startPos; i <= lenS - lenC; i++)
  1260.         {
  1261.                 for (j = 0; s[i + j] == c[j]; j++)
  1262.                         ;
  1263.  
  1264.                 if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  1265.                         return i;
  1266.                 if (j == lenC)
  1267.                         if (n - 1)
  1268.                                 n--;
  1269.                         else
  1270.                                 return i;
  1271.         }
  1272.         return -1;
  1273. }
  1274.  
  1275. unsigned char mediaProcessorExt(void)
  1276. {
  1277.         // unsigned char ext[65];
  1278.         unsigned char extLow[4];
  1279.         unsigned char extUp[4];
  1280.         unsigned char byte;
  1281.         unsigned char *count1;
  1282.         unsigned int counter, counter2, next, curPosition;
  1283.         int n;
  1284.  
  1285.         count1 = strstr(navi.fileName, ".");
  1286.         if (count1 == NULL)
  1287.         {
  1288.                 clearStatus();
  1289.                 printf("Ошибка определения типа файла, не найдено расширение. [%s]", navi.fileName);
  1290.                 getchar();
  1291.         }
  1292.  
  1293.         counter = strlen(navi.fileName);
  1294.         do
  1295.         {
  1296.                 counter--;
  1297.                 if (navi.fileName[counter] == '.')
  1298.                 {
  1299.  
  1300.                         for (counter2 = 0; counter2 < 3; counter2++)
  1301.                         {
  1302.                                 extLow[counter2] = tolower(navi.fileName[counter + counter2 + 1]);
  1303.                                 extUp[counter2] = toupper(navi.fileName[counter + counter2 + 1]);
  1304.                         }
  1305.                         extUp[3] = 0;
  1306.                         extLow[3] = 0;
  1307.                         // printf("[%s]\r\n[%s]\r\n", extLow, extUp);
  1308.                         break;
  1309.                 }
  1310.  
  1311.         } while (counter != 0);
  1312.  
  1313.         next = 1;
  1314.         curPosition = 0;
  1315.         do
  1316.         {
  1317.                 n = -1;
  1318.                 n = pos(nvext, extLow, next, curPosition);
  1319.                 curPosition = n;
  1320.                 if (n == -1)
  1321.                 {
  1322.                         curPosition = 0;
  1323.                         n = pos(nvext, extUp, next, curPosition);
  1324.                         curPosition = n;
  1325.                         if (n == -1)
  1326.                         {
  1327.                                 clearStatus();
  1328.                                 printf("[ext]не найдено соответствие к расширению [%s][%s]", extLow, extUp);
  1329.                                 getchar();
  1330.                                 return false;
  1331.                         }
  1332.                 }
  1333.                 else
  1334.                 {
  1335.                         counter = 0;
  1336.                         do
  1337.                         {
  1338.                                 byte = nvext[n + counter];
  1339.                                 if (byte == 0x0d)
  1340.                                 {
  1341.                                         next++;
  1342.                                         break;
  1343.                                 }
  1344.  
  1345.                                 if (byte == ':')
  1346.                                 {
  1347.                                         counter++;
  1348.                                         counter2 = 0;
  1349.  
  1350.                                         while (nvext[n + counter] == ' ')
  1351.                                         {
  1352.                                                 counter++;
  1353.                                         }
  1354.  
  1355.                                         do
  1356.                                         {
  1357.                                                 byte = nvext[n + counter];
  1358.                                                 cmd[counter2] = byte;
  1359.                                                 counter++;
  1360.                                                 counter2++;
  1361.                                         } while (byte != 0x0d);
  1362.                                         cmd[counter2 - 1] = ' ';
  1363.                                         cmd[counter2] = 0;
  1364.                                         strcat(cmd, "current.");
  1365.                                         strcat(cmd, extLow);
  1366.                                         return true;
  1367.                                 }
  1368.                                 counter++;
  1369.                         } while (42);
  1370.                 }
  1371.         } while (42);
  1372.         return false;
  1373. }
  1374.  
  1375. void goHome(void)
  1376. {
  1377.         pusHistory();
  1378.         newPage();
  1379.         link.type = '1';
  1380.         strcpy(link.host, "HOMEPAGE");
  1381.         OS_SETSYSDRV();
  1382.         loadPageFromDisk("browser/nedogoph.gph", 0);
  1383.         navi.nextBufPos = renderPage(navi.nextBufPos);
  1384. }
  1385.  
  1386. void doLink(void)
  1387. {
  1388.  
  1389.         // clearStatus();
  1390.         // printf("[%c][%s][%d][%s]", link.type, link.host, link.port, link.path);
  1391.         // getchar();
  1392.  
  1393.         switch (link.type) // Тут уже новый элемент
  1394.         {
  1395.         case 'i':
  1396.                 link.type = link.nexType; // так-как мы остались на странице, восстановим тип, хотя можно просто ставить 1 (пока других нет)
  1397.                 return;
  1398.         case '0': // plain texts
  1399.                 if (getFile("browser/current.txt"))
  1400.                 {
  1401.                         newPage();
  1402.                         OS_SETSYSDRV();
  1403.                         loadPageFromDisk("browser/current.txt", 0);
  1404.                         navi.nextBufPos = renderPlain(navi.nextBufPos);
  1405.                 }
  1406.                 else
  1407.                 {
  1408.                         errNoConnect();
  1409.                         goHome();
  1410.                 }
  1411.                 return;
  1412.         case '1': // gopher page
  1413.                 if (getFile("browser/current.gph"))
  1414.                 {
  1415.                         newPage();
  1416.                         OS_SETSYSDRV();
  1417.                         loadPageFromDisk("browser/current.gph", 0);
  1418.                         navi.nextBufPos = renderPage(navi.nextBufPos);
  1419.                 }
  1420.                 else
  1421.                 {
  1422.  
  1423.                         errNoConnect();
  1424.                         goHome();
  1425.                 }
  1426.                 return;
  1427.         case '7': // search input
  1428.                 curWin.w = 40;
  1429.                 curWin.x = 80 / 2 - curWin.w / 2 - 1;
  1430.                 curWin.y = 10;
  1431.                 curWin.h = 1;
  1432.                 curWin.text = 95;
  1433.                 curWin.back = 95;
  1434.                 strcpy(curWin.tittle, "Введите поисковый запрос");
  1435.                 if (inputBox(curWin, ""))
  1436.                 {
  1437.                         strcat(link.path, "\t");
  1438.                         strcat(link.path, cmd);
  1439.                         if (getFile("browser/current.gph"))
  1440.                         {
  1441.                                 newPage();
  1442.                                 OS_SETSYSDRV();
  1443.                                 loadPageFromDisk("browser/current.gph", 0);
  1444.                                 navi.nextBufPos = renderPage(navi.nextBufPos);
  1445.                                 link.type = '1';
  1446.                                 pusHistory();
  1447.                         }
  1448.                         else
  1449.                         {
  1450.                                 reDraw();
  1451.                                 errNoConnect();
  1452.                                 goHome();
  1453.                         }
  1454.                 }
  1455.                 else
  1456.                 {
  1457.                         link.type = '1';
  1458.                         reDraw();
  1459.                         return;
  1460.                 }
  1461.                 return;
  1462.         case 'g': // gif pic
  1463.         case 'I': // image
  1464.         case 's': // sound
  1465.         case '9': // binary (pt3/scr)
  1466.         case '8':
  1467.         case '6':
  1468.         case '5':
  1469.         case '4':
  1470.                 if (!extractName())
  1471.                 {
  1472.                         link.type = '1';
  1473.                         reDraw();
  1474.                         return;
  1475.                 }
  1476.                 pusHistory();
  1477.                 OS_CHDIR("/");
  1478.                 OS_CHDIR("downloads");
  1479.                 if (getFile(navi.fileName))
  1480.                 {
  1481.                         popHistory();
  1482.                         OS_SETSYSDRV();
  1483.                         loadPageFromDisk("browser/current.gph", 0);
  1484.                         navi.nextBufPos = renderPage(pageOffsets[navi.page]);
  1485.                         if (!navi.saveAs)
  1486.                         {
  1487.                                 if (mediaProcessorExt())
  1488.                                 {
  1489.                                         OS_CHDIR("/");
  1490.                                         OS_CHDIR("downloads");
  1491.                                         clearStatus();
  1492.                                         printf("cmd:[%s]", cmd);
  1493.                                         OS_SHELL(cmd);
  1494.                                         OS_SETSYSDRV();
  1495.                                 }
  1496.                         }
  1497.                 }
  1498.                 else
  1499.                 {
  1500.                         popHistory();
  1501.                         errNoConnect();
  1502.                         goHome();
  1503.                 }
  1504.                 return;
  1505.         default:
  1506.                 clearStatus();
  1507.                 printf("Неизвестный селектор:[%d]lineselect[%d]linelast[%d]", link.type, navi.lineSelect, navi.lastLine);
  1508.                 link.type = link.nexType;
  1509.                 return;
  1510.         }
  1511. }
  1512.  
  1513. void activate(void)
  1514. {
  1515.         if (!selectorProcessor())
  1516.         {
  1517.                 return;
  1518.         }
  1519.  
  1520.         if (link.type == '0' || link.type == '1') //|| link.type == '7')
  1521.         {
  1522.                 pusHistory();
  1523.         }
  1524.         doLink();
  1525. }
  1526.  
  1527. void enterDomain(void)
  1528. {
  1529.         curWin.w = 40;
  1530.         curWin.x = 80 / 2 - curWin.w / 2 - 1;
  1531.         curWin.y = 10;
  1532.         curWin.h = 1;
  1533.         curWin.text = 207;
  1534.         curWin.back = 207;
  1535.         strcpy(curWin.tittle, "Введите адрес Gopher сервера");
  1536.  
  1537.         if (inputBox(curWin, ""))
  1538.         {
  1539.                 strcpy(link.prevHost, link.host);
  1540.                 link.type = '1';
  1541.                 strcpy(link.host, cmd);
  1542.                 strcpy(link.path, "/");
  1543.                 link.port = 70;
  1544.                 doLink();
  1545.         }
  1546.         else
  1547.         {
  1548.                 reDraw();
  1549.         }
  1550. }
  1551.  
  1552. void navigationPage(char keypress)
  1553. {
  1554.         unsigned char counter;
  1555.  
  1556.         switch (keypress)
  1557.         {
  1558.         case 250: // Up
  1559.                 navi.prevLineSelect = navi.lineSelect;
  1560.                 navi.lineSelect--;
  1561.  
  1562.                 if (navi.lineSelect < 1 && navi.page == 0)
  1563.                 {
  1564.                         navi.lineSelect = screenHeight;
  1565.                         break;
  1566.                 }
  1567.  
  1568.                 if (navi.page != 0 && navi.lineSelect == 0)
  1569.                 {
  1570.                         navi.page--;
  1571.                         navi.nextBufPos = pageOffsets[navi.page];
  1572.                         navi.nextBufPos = renderPage(navi.nextBufPos);
  1573.                         navi.lineSelect = screenHeight;
  1574.                 }
  1575.                 break;
  1576.         case 249: // down
  1577.                 navi.prevLineSelect = navi.lineSelect;
  1578.                 navi.lineSelect++;
  1579.                 if (navi.lineSelect > screenHeight && navi.page == navi.maxPage)
  1580.                 {
  1581.                         navi.lineSelect = 1;
  1582.                         break;
  1583.                 }
  1584.                 if (navi.page != navi.maxPage && navi.lineSelect > screenHeight)
  1585.                 {
  1586.                         navi.page++;
  1587.                         pageOffsets[navi.page] = navi.nextBufPos;
  1588.                         navi.nextBufPos = renderPage(navi.nextBufPos);
  1589.                         navi.lineSelect = 1;
  1590.                 }
  1591.                 break;
  1592.         case 248: // Left
  1593.                 if (navi.page == 0)
  1594.                 {
  1595.                         break;
  1596.                 }
  1597.                 navi.page--;
  1598.                 navi.nextBufPos = pageOffsets[navi.page];
  1599.                 navi.nextBufPos = renderPage(navi.nextBufPos);
  1600.                 navi.lineSelect = screenHeight;
  1601.                 break;
  1602.         case 251: // Right
  1603.                 if (navi.page == navi.maxPage)
  1604.                 {
  1605.                         break;
  1606.                 }
  1607.                 navi.page++;
  1608.                 pageOffsets[navi.page] = navi.nextBufPos;
  1609.  
  1610.                 navi.nextBufPos = renderPage(navi.nextBufPos);
  1611.                 navi.lineSelect = 1;
  1612.                 break;
  1613.         case 0x0d:
  1614.                 activate();
  1615.                 break;
  1616.         case 0x08: // BS
  1617.                 if (navi.history > 1)
  1618.                 {
  1619.                         popHistory();
  1620.                         doLink();
  1621.                 }
  1622.                 break;
  1623.         case 31: // screen redraw
  1624.                 renderPage(pageOffsets[navi.page]);
  1625.                 break;
  1626.         case 'h':
  1627.                 goHome();
  1628.                 break;
  1629.         case 'd':
  1630.                 enterDomain();
  1631.                 break;
  1632.         case 's':
  1633.                 navi.saveAs = !navi.saveAs;
  1634.                 mainWinDraw();
  1635.                 break;
  1636.         case 'i':
  1637.                 netDriver = !netDriver;
  1638.                 mainWinDraw();
  1639.                 if (netDriver)
  1640.                 {
  1641.                         uart_init(divider);
  1642.                         espReBoot();
  1643.                 }
  1644.  
  1645.                 break;
  1646.         }
  1647.  
  1648.         if (link.type == '1')
  1649.         {
  1650.                 for (counter = 1; counter < 79; counter++)
  1651.                 {
  1652.                         OS_SETXY(counter, navi.prevLineSelect);
  1653.                         OS_PRATTR(7);
  1654.                 }
  1655.  
  1656.                 if (mouse.cursYpos == navi.prevLineSelect)
  1657.                 {
  1658.                         OS_SETXY(mouse.cursXpos, mouse.cursYpos);
  1659.                         mouse.oldAtr = OS_GETATTR();
  1660.                 }
  1661.                 for (counter = 1; counter < 79; counter++)
  1662.                 {
  1663.                         OS_SETXY(counter, navi.lineSelect);
  1664.                         OS_PRATTR(15);
  1665.                 }
  1666.         }
  1667. }
  1668.  
  1669. void navigationPlain(char keypress)
  1670. {
  1671.         switch (keypress)
  1672.         {
  1673.         case 248: // Left
  1674.         case 250: // Up
  1675.  
  1676.                 if (navi.page == 0)
  1677.                 {
  1678.                         if (navi.volume == 0)
  1679.                         {
  1680.                                 break;
  1681.                         }
  1682.                         navi.volume--;
  1683.                         newPage();
  1684.                         OS_SETSYSDRV();
  1685.                         loadPageFromDisk("browser/current.txt", navi.volume);
  1686.                         navi.nextBufPos = renderPlain(navi.nextBufPos);
  1687.                         break;
  1688.                 }
  1689.                 navi.page--;
  1690.                 navi.nextBufPos = pageOffsets[navi.page];
  1691.                 navi.lineSelect = screenHeight;
  1692.                 navi.nextBufPos = renderPlain(navi.nextBufPos);
  1693.                 break;
  1694.         case 251: // Right
  1695.         case 249: // down
  1696.                 if (navi.page == navi.maxPage)
  1697.                 {
  1698.                         if (navi.volume == navi.maxVolume)
  1699.                         {
  1700.                                 break;
  1701.                         }
  1702.                         navi.volume++;
  1703.                         newPage();
  1704.                         OS_SETSYSDRV();
  1705.                         loadPageFromDisk("browser/current.txt", navi.volume);
  1706.                         navi.nextBufPos = renderPlain(navi.nextBufPos);
  1707.                         break;
  1708.                 }
  1709.                 navi.page++;
  1710.                 pageOffsets[navi.page] = navi.nextBufPos;
  1711.                 navi.lineSelect = 1;
  1712.                 navi.nextBufPos = renderPlain(navi.nextBufPos);
  1713.                 break;
  1714.         case 0x08: // BS
  1715.                 if (navi.history > 1)
  1716.                 {
  1717.                         popHistory();
  1718.                         doLink();
  1719.                 }
  1720.                 break;
  1721.         case 31: // screen redraw
  1722.                 renderPlain(pageOffsets[navi.page]);
  1723.                 break;
  1724.         case 'h':
  1725.                 goHome();
  1726.                 break;
  1727.         case 'd':
  1728.                 enterDomain();
  1729.                 break;
  1730.         case 's':
  1731.                 navi.saveAs = !navi.saveAs;
  1732.                 mainWinDraw();
  1733.                 break;
  1734.         case 'i':
  1735.                 netDriver = !netDriver;
  1736.                 mainWinDraw();
  1737.                 break;
  1738.         }
  1739.  
  1740.         if (mouse.cursYpos == navi.prevLineSelect)
  1741.         {
  1742.                 OS_SETXY(mouse.cursXpos, mouse.cursYpos);
  1743.                 mouse.oldAtr = OS_GETATTR();
  1744.         }
  1745. }
  1746.  
  1747. void navigation(unsigned char keypress)
  1748. {
  1749.  
  1750.         switch (link.type)
  1751.         {
  1752.         case '0':
  1753.                 navigationPlain(keypress);
  1754.                 break;
  1755.         case '1':
  1756.                 navigationPage(keypress);
  1757.                 break;
  1758.         }
  1759. }
  1760.  
  1761. C_task main(int argc, char *argv[])
  1762. {
  1763.         unsigned char keypress;
  1764.         int mouseScroll = 0;
  1765.         unsigned long start, finish;
  1766.         OS_HIDEFROMPARENT();
  1767.         OS_SETGFX(0x86);
  1768.         OS_CLS(0);
  1769.         OS_SETSYSDRV();
  1770.         init();
  1771.         // printTable();
  1772.         // getchar();
  1773.  
  1774.         goHome();
  1775.  
  1776.         start = 0;
  1777.         do
  1778.         {
  1779.                 keypress = getMouse();
  1780.                 if (mouse.lmb == 0)
  1781.                 {
  1782.                         if (mouse.cursYpos > 0 && mouse.cursYpos < screenHeight + 1)
  1783.                         {
  1784.                                 // strcpy(link.prevHost, link.host);
  1785.                                 navi.prevLineSelect = navi.lineSelect;
  1786.                                 navi.lineSelect = mouse.cursYpos;
  1787.                                 activate();
  1788.                                 OS_SETXY(mouse.cursXpos, mouse.cursYpos);
  1789.                                 OS_PRATTR(215);
  1790.                         }
  1791.                         else
  1792.                         {
  1793.                                 if (mouse.cursYpos == 0)
  1794.                                 {
  1795.                                         enterDomain();
  1796.                                 }
  1797.                         }
  1798.                 }
  1799.                 if (mouse.rmb == 0)
  1800.                 {
  1801.                         if (navi.history > 1)
  1802.                         {
  1803.                                 popHistory();
  1804.                                 doLink();
  1805.                         }
  1806.                 }
  1807.  
  1808.                 mouseScroll = mouse.prevWheel - mouse.wheel;
  1809.  
  1810.                 if (mouseScroll < -12)
  1811.                 {
  1812.                         mouseScroll = 1;
  1813.                 }
  1814.                 if (mouseScroll > 12)
  1815.                 {
  1816.                         mouseScroll = -1;
  1817.                 }
  1818.  
  1819.                 if (mouseScroll > 0)
  1820.                 {
  1821.                         // clearStatus();
  1822.                         // printf("UP prevWheel:[%2d] wheel:[%2d] mouseScroll:[%2d]", mouse.prevWheel, mouse.wheel, mouseScroll);
  1823.                         navigation(248); // Left
  1824.                 }
  1825.  
  1826.                 if (mouseScroll < 0)
  1827.                 {
  1828.                         // clearStatus();
  1829.                         // printf("DOWN prevWheel:[%2d] wheel:[%2d] mouseScroll:[%2d]", mouse.prevWheel, mouse.wheel, mouseScroll);
  1830.                         navigation(251); // Right
  1831.                 }
  1832.                 mouse.prevWheel = mouse.wheel;
  1833.  
  1834.                 if (keypress != 0)
  1835.                 {
  1836.                         navigation(keypress);
  1837.  
  1838.                         //      printf("keypress [%d]", keypress);
  1839.                 }
  1840.  
  1841.                 finish++;
  1842.                 if ((finish - start) > 50000)
  1843.                 {
  1844.                         mainWinDraw();
  1845.                         finish = 0;
  1846.                 }
  1847.                 YIELD();
  1848.         } while (keypress != 27);
  1849.         OS_DELETE("browser/current.gph");
  1850.         OS_DELETE("browser/current.txt");
  1851.         OS_DELETE("browser/ng_hist.dat");
  1852. }
  1853.