Subversion Repositories NedoOS

Rev

Rev 2381 | Blame | Compare with Previous | Last modification | View Log | Download

  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 <graphic.h>
  10. #include <ctype.h>
  11. #include <math.h>
  12. //
  13. #define true 1
  14. #define false 0
  15. #define screenHeight 23
  16. #define screenWidth 80
  17.  
  18. unsigned int RBR_THR = 0xf8ef;
  19. unsigned int IER = 0xf9ef;
  20. unsigned int IIR_FCR = 0xfaef;
  21. unsigned int LCR = 0xfbef;
  22. unsigned int MCR = 0xfcef;
  23. unsigned int LSR = 0xfdef;
  24. unsigned int MSR = 0xfeef;
  25. unsigned int SR = 0xffef;
  26. unsigned int divider = 1;
  27. unsigned int comType = 0;
  28. unsigned int espType = 32;
  29. unsigned int espRetry = 5;
  30. unsigned long factor, timerok, count = 0;
  31. unsigned int magic = 16;
  32. unsigned char netDriver = 0;
  33. unsigned char curHost;
  34. unsigned long contLen;
  35. unsigned int httpErr;
  36.  
  37. unsigned char uVer[] = "0.4";
  38. unsigned char curPath[128];
  39. unsigned char cmd[256];
  40. unsigned char search[256];
  41. unsigned char crlf[2] = {13, 10};
  42. const unsigned char gotWiFi[] = "WIFI GOT IP";
  43. char hosts[3][32] = {"next.zxart.ee", "zxdb.remysharp.com", "hood.speccy.cz"};
  44. unsigned char userAgent1[] = " HTTP/1.1\r\nHost: ";
  45. unsigned char userAgent2[] = "\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; ZXDB)\r\n\r\n\0";
  46. unsigned char netbuf[4096];
  47. unsigned char buf[16384];
  48. struct sockaddr_in targetadr;
  49. struct readstructure readStruct;
  50. struct sockaddr_in dnsaddress;
  51.  
  52. struct window
  53. {
  54.         unsigned char x;
  55.         unsigned char y;
  56.         unsigned char w;
  57.         unsigned char h;
  58.         unsigned char text;
  59.         unsigned char back;
  60.         unsigned char tittle[80];
  61. } curWin;
  62.  
  63. struct time
  64. {
  65.         unsigned int hours;
  66.         unsigned int minutes;
  67.         unsigned char oldMinutes;
  68.  
  69. } clock;
  70.  
  71. struct linkStruct
  72. {
  73.         unsigned char host[128];
  74.         unsigned char path[512];
  75.         unsigned int port;
  76.         unsigned char hasName;
  77.         unsigned char fname[256];
  78. } link;
  79.  
  80. struct line
  81. {
  82.         unsigned long id;
  83.         unsigned char name[512];
  84.         unsigned char file[512];
  85.         unsigned char ext[5];
  86.         unsigned long size;
  87.         unsigned char option;
  88.         unsigned int year;
  89. } table[12];
  90.  
  91. struct limit
  92. {
  93.         int first;
  94.         int second;
  95.         int total;
  96.         int curline;
  97.         int curOpt;
  98.         int headLng;
  99.         int curPage;
  100. } limiter;
  101.  
  102. void spaces(unsigned char number)
  103. {
  104.         while (number > 0)
  105.         {
  106.                 putchar(' ');
  107.                 number--;
  108.         }
  109. }
  110.  
  111. void waitKey(void)
  112. {
  113.         do
  114.         {
  115.                 YIELD();
  116.         } while (OS_GETKEY() == 0);
  117. }
  118.  
  119. void clearStatus(void)
  120. {
  121.         OS_SETCOLOR(5);
  122.         OS_SETXY(0, 24);
  123.         spaces(79);
  124.         putchar('\r');
  125. }
  126.  
  127. void quit(void)
  128. {
  129.         OS_CLS(0);
  130.         OS_SETGFX(-1);
  131.         exit(0);
  132. }
  133.  
  134. void printTable(void)
  135. {
  136.         unsigned int cycle;
  137.  
  138.         for (cycle = 1; cycle < 256; cycle++)
  139.         {
  140.                 OS_SETCOLOR(7);
  141.                 printf("%03u:", cycle);
  142.                 OS_SETCOLOR(71);
  143.                 putchar(cycle);
  144.                 OS_SETCOLOR(7);
  145.                 printf(" ");
  146.                 if (cycle % 12 == 0)
  147.                 {
  148.                         printf("\r\n");
  149.                 }
  150.         }
  151. }
  152.  
  153. void delay(unsigned long counter)
  154. {
  155.         unsigned long start, finish;
  156.         counter = counter / 20;
  157.         if (counter < 1)
  158.         {
  159.                 counter = 1;
  160.         }
  161.         start = time();
  162.         finish = start + counter;
  163.  
  164.         while (start < finish)
  165.         {
  166.                 start = time();
  167.         }
  168. }
  169.  
  170. unsigned char delayLongKey(unsigned long counter)
  171. {
  172.         unsigned long start, finish, key;
  173.         counter = counter / 20;
  174.         if (counter < 1)
  175.         {
  176.                 counter = 1;
  177.         }
  178.         start = time();
  179.         finish = start + counter;
  180.  
  181.         while (start < finish)
  182.         {
  183.                 start = time();
  184.                 key = OS_GETKEY();
  185.                 if (key != 0)
  186.                 {
  187.                         return key;
  188.                 }
  189.                 YIELD();
  190.         }
  191.         return 32;
  192. }
  193.  
  194. ///////////////////////////
  195. #include <../common/esp-com.c>
  196. #include <../common/network.c>
  197. //////////////////////////
  198.  
  199. void clearNetbuf(void)
  200. {
  201.         unsigned int counter;
  202.         for (counter = 0; counter < sizeof(netbuf); counter++)
  203.         {
  204.                 netbuf[counter] = 0;
  205.         }
  206. }
  207.  
  208. int testOperation2(const char *process, int socket)
  209. {
  210.         if (socket < 0)
  211.         {
  212.                 printf("%s: [ERROR:", process);
  213.                 errorPrint(-socket);
  214.                 printf("]\r\n");
  215.                 YIELD();
  216.                 return -socket;
  217.         }
  218.         return 1;
  219. }
  220.  
  221. unsigned char saveBuf(unsigned char *fileNamePtr, unsigned char operation, unsigned int sizeOfBuf)
  222. {
  223.         FILE *fp2;
  224.  
  225.         switch (operation)
  226.         {
  227.         case 00:
  228.                 fp2 = OS_CREATEHANDLE(fileNamePtr, 0x80);
  229.                 if (((int)fp2) & 0xff)
  230.                 {
  231.                         clearStatus();
  232.                         printf("%s  creating error.", fileNamePtr);
  233.                         getchar();
  234.                         exit(0);
  235.                 }
  236.                 OS_CLOSEHANDLE(fp2);
  237.                 break;
  238.         case 01:
  239.                 fp2 = OS_OPENHANDLE(fileNamePtr, 0x80);
  240.                 if (((int)fp2) & 0xff)
  241.                 {
  242.                         clearStatus();
  243.                         printf("%s opening error.\r\n ", fileNamePtr);
  244.                         getchar();
  245.                         exit(0);
  246.                 }
  247.                 OS_SEEKHANDLE(fp2, OS_GETFILESIZE(fp2));
  248.                 OS_WRITEHANDLE(netbuf + limiter.headLng, fp2, sizeOfBuf);
  249.                 OS_CLOSEHANDLE(fp2);
  250.                 break;
  251.         case 02:
  252.                 OS_CLOSEHANDLE(fp2);
  253.                 break;
  254.         default:
  255.                 break;
  256.         }
  257.  
  258.         return 0;
  259. }
  260.  
  261. void drawClock(void)
  262. {
  263.         unsigned long dosTime;
  264.         dosTime = OS_GETTIME();
  265.         clock.hours = dosTime >> 11 & 31;        // 0b00011111
  266.         clock.minutes = (dosTime >> 5) & 63; // 0b00111111
  267.  
  268.         if (clock.minutes != clock.oldMinutes)
  269.         {
  270.                 clock.oldMinutes = clock.minutes;
  271.                 OS_SETCOLOR(103);
  272.                 OS_SETXY(73, 0);
  273.                 printf("[%02u:%02u]", clock.hours, clock.minutes);
  274.         }
  275. }
  276.  
  277. void drawPage(void)
  278. {
  279.         OS_SETCOLOR(103);
  280.         OS_SETXY(64, 0);
  281.         printf("[Page:%2d]", limiter.curPage);
  282. }
  283.  
  284. char readParamFromIni(void)
  285. {
  286.         FILE *fpini;
  287.         unsigned char *count1;
  288.         const char currentNetwork[] = "currentNetwork";
  289.         unsigned char curNet = 0;
  290.  
  291.         OS_GETPATH((unsigned int)&curPath);
  292.         OS_SETSYSDRV();
  293.         OS_CHDIR("/");
  294.         OS_CHDIR("ini");
  295.  
  296.         fpini = OS_OPENHANDLE("network.ini", 0x80);
  297.         if (((int)fpini) & 0xff)
  298.         {
  299.                 clearStatus();
  300.                 printf("network.ini not found.\r\n");
  301.                 getchar();
  302.                 return false;
  303.         }
  304.  
  305.         OS_READHANDLE(netbuf, fpini, sizeof(netbuf) - 1);
  306.         OS_CLOSEHANDLE(fpini);
  307.  
  308.         count1 = strstr(netbuf, currentNetwork);
  309.         if (count1 != NULL)
  310.         {
  311.                 sscanf(count1 + strlen(currentNetwork) + 1, "%u", &curNet);
  312.         }
  313.  
  314.         OS_CHDIR(curPath);
  315.         return curNet;
  316. }
  317.  
  318. void init(void)
  319. {
  320.         targetadr.family = AF_INET;
  321.         targetadr.porth = 00;
  322.         targetadr.portl = 80;
  323.         targetadr.b1 = 0;
  324.         targetadr.b2 = 0;
  325.         targetadr.b3 = 0;
  326.         targetadr.b4 = 0;
  327.         curHost = 0;
  328.         link.port = 80;
  329.         link.hasName = false;
  330.         limiter.curOpt = 1;
  331.         limiter.curPage = 0;
  332.         get_dns();
  333.         netDriver = readParamFromIni();
  334.         if (netDriver == 1)
  335.         {
  336.                 loadEspConfig();
  337.                 uart_init(divider);
  338.                 espReBoot();
  339.         }
  340.         OS_SETSYSDRV();
  341.         OS_MKDIR("../downloads");          // Create if not exist
  342.         OS_MKDIR("../downloads/zxdb"); // Create if not exist
  343.         OS_CHDIR("../downloads/zxdb");
  344.  
  345.         strcpy(link.host, hosts[curHost]);
  346.         clock.oldMinutes = 255;
  347. }
  348.  
  349. void errorBox(struct window w, const char *message)
  350. {
  351.         unsigned char wcount, tempx, tittleStart;
  352.  
  353.         w.h++;
  354.         OS_SETXY(w.x, w.y - 1);
  355.         BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  356.         OS_SETXY(w.x, w.y);
  357.         OS_SETCOLOR(w.text);
  358.         putchar(201);
  359.         for (wcount = 0; wcount < w.w; wcount++)
  360.         {
  361.                 putchar(205);
  362.         }
  363.         putchar(187);
  364.         OS_SETXY(w.x, w.y + w.h);
  365.         putchar(200);
  366.         for (wcount = 0; wcount < w.w; wcount++)
  367.         {
  368.                 putchar(205);
  369.         }
  370.         putchar(188);
  371.  
  372.         tempx = w.x + w.w + 1;
  373.         for (wcount = 1; wcount < w.h; wcount++)
  374.         {
  375.                 OS_SETXY(w.x, w.y + wcount);
  376.                 putchar(186);
  377.                 OS_SETXY(tempx, w.y + wcount);
  378.                 putchar(186);
  379.         }
  380.         tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  381.         OS_SETXY(tittleStart, w.y);
  382.         printf("[%s]", w.tittle);
  383.         OS_SETXY(w.x + 1, w.y + 1);
  384.         OS_SETCOLOR(w.back);
  385.         tittleStart = w.x + (w.w / 2) - (strlen(message) / 2);
  386.         OS_SETXY(tittleStart, w.y + 1);
  387.         printf("%s", message);
  388. }
  389.  
  390. void simpleBox(struct window w)
  391. {
  392.         unsigned char wcount, tempx;
  393.         w.h = w.h - 2;
  394.         OS_SETXY(w.x, w.y);
  395.         BDBOX(w.x, w.y + 1, w.w, w.h, w.back, 32);
  396.         w.w = w.w - 2;
  397.         OS_SETXY(w.x, w.y);
  398.         OS_SETCOLOR(w.text);
  399.         putchar(201);
  400.         for (wcount = 0; wcount < w.w; wcount++)
  401.         {
  402.                 putchar(205);
  403.         }
  404.         putchar(187);
  405.  
  406.         OS_SETXY(w.x, w.y + w.h);
  407.         putchar(200);
  408.         for (wcount = 0; wcount < w.w; wcount++)
  409.         {
  410.                 putchar(205);
  411.         }
  412.         putchar(188);
  413.         tempx = w.x + w.w + 1;
  414.         for (wcount = 1; wcount < w.h; wcount++)
  415.         {
  416.                 OS_SETXY(w.x, w.y + wcount);
  417.                 putchar(186);
  418.                 OS_SETXY(tempx, w.y + wcount);
  419.                 putchar(186);
  420.         }
  421. }
  422.  
  423. unsigned char inputBox(struct window w, const char *prefilled)
  424. {
  425.         unsigned char wcount, tempx, tittleStart;
  426.         unsigned char byte, counter;
  427.         w.h++;
  428.         OS_SETXY(w.x, w.y - 1);
  429.         BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  430.         OS_SETXY(w.x, w.y);
  431.         OS_SETCOLOR(w.text);
  432.         putchar(201);
  433.         for (wcount = 0; wcount < w.w; wcount++)
  434.         {
  435.                 putchar(205);
  436.         }
  437.         putchar(187);
  438.         OS_SETXY(w.x, w.y + w.h);
  439.         putchar(200);
  440.         for (wcount = 0; wcount < w.w; wcount++)
  441.         {
  442.                 putchar(205);
  443.         }
  444.         putchar(188);
  445.  
  446.         tempx = w.x + w.w + 1;
  447.         for (wcount = 1; wcount < w.h; wcount++)
  448.         {
  449.                 OS_SETXY(w.x, w.y + wcount);
  450.                 putchar(186);
  451.                 OS_SETXY(tempx, w.y + wcount);
  452.                 putchar(186);
  453.         }
  454.         tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  455.         OS_SETXY(tittleStart, w.y);
  456.         printf("[%s]", w.tittle);
  457.         OS_SETXY(w.x + 1, w.y + 1);
  458.         OS_SETCOLOR(w.back);
  459.         putchar(219);
  460.  
  461.         cmd[0] = 0;
  462.  
  463.         counter = strlen(prefilled);
  464.         if (counter != 0)
  465.         {
  466.                 strcpy(cmd, prefilled);
  467.                 goto skipKeys;
  468.         }
  469.  
  470.         do
  471.         {
  472.                 byte = OS_GETKEY();
  473.                 /*
  474.                 clearStatus();
  475.                 OS_SETCOLOR(103);
  476.                 printf("key = %u", byte);
  477.                 */
  478.                 if (byte != 0)
  479.                 {
  480.                         switch (byte)
  481.                         {
  482.                         case 0x08:
  483.                                 if (counter > 0)
  484.                                 {
  485.                                         counter--;
  486.                                         cmd[counter] = 0;
  487.                                 }
  488.                                 break;
  489.                         case 0x0d:
  490.  
  491.                                 if (counter == 0)
  492.                                 {
  493.                                         return false;
  494.                                 }
  495.                                 else
  496.                                 {
  497.                                         return true;
  498.                                 }
  499.  
  500.                         case 31:
  501.                                 break;
  502.                         case 250:
  503.                                 break;
  504.                         case 249:
  505.                                 break;
  506.                         case 248:
  507.                                 break;
  508.                         case 251: // Right
  509.                                 break;
  510.                         case 252: // Del
  511.                                 OS_SETXY(w.x + 1, w.y + 1);
  512.                                 spaces(counter + 1);
  513.                                 cmd[0] = 0;
  514.                                 counter = 0;
  515.                                 break;
  516.                         case 27:
  517.                                 cmd[0] = 0;
  518.                                 return false;
  519.  
  520.                         default:
  521.                                 if (counter < w.w - 1)
  522.                                 {
  523.                                         cmd[counter] = byte;
  524.                                         counter++;
  525.                                         cmd[counter] = 0;
  526.                                 }
  527.                                 break;
  528.                         }
  529.                 skipKeys:
  530.                         OS_SETXY(w.x + 1, w.y + 1);
  531.                         printf("%s", cmd);
  532.                         putchar(219);
  533.                         if (byte == 0x08)
  534.                         {
  535.                                 putchar(' ');
  536.                         }
  537.                 }
  538.                 YIELD();
  539.         } while (42);
  540.         return false;
  541. }
  542.  
  543. void sendReqdialog(void)
  544. {
  545.         curWin.w = 50;
  546.         curWin.x = 39 - curWin.w / 2;
  547.         curWin.y = 10;
  548.         curWin.h = 4;
  549.         curWin.text = 223;
  550.         curWin.back = 223;
  551.         simpleBox(curWin);
  552.         OS_SETXY(30, curWin.y + 1);
  553.         printf("Sending request...");
  554. }
  555.  
  556. void tittleDraw(void)
  557. {
  558.         clock.oldMinutes = 255;
  559.         OS_SETCOLOR(103);
  560.         OS_SETXY(0, 0);
  561.         spaces(79);
  562.         OS_SETXY(0, 0);
  563.         printf(" ZXDB downloader [%s]", uVer);
  564.         OS_SETXY(38 - strlen(link.host) / 2, 0);
  565.         printf("[");
  566.         OS_SETCOLOR(102);
  567.         printf("%s", link.host);
  568.         OS_SETCOLOR(103);
  569.         printf("]");
  570.         drawPage();
  571.         drawClock();
  572. }
  573. void drawSearch(void)
  574. {
  575.         OS_SETCOLOR(207);
  576.         OS_SETXY(38 - strlen(search) / 2, 1);
  577.         putchar('[');
  578.         OS_SETCOLOR(71);
  579.         printf("%s", search);
  580.         OS_SETCOLOR(curWin.text);
  581.         OS_SETCOLOR(207);
  582.         putchar(']');
  583. }
  584.  
  585. void mainWinDraw(void)
  586. {
  587.         tittleDraw();
  588.         OS_SETXY(0, 23);
  589.         OS_SETCOLOR(71);
  590.         printf("[");
  591.         OS_SETCOLOR(87);
  592.         printf("H");
  593.         OS_SETCOLOR(71);
  594.         printf("]change host       [");
  595.         OS_SETCOLOR(87);
  596.         printf("S");
  597.  
  598.         OS_SETCOLOR(71);
  599.         printf("]Search item        [");
  600.         OS_SETCOLOR(87);
  601.         printf("ENTER");
  602.         OS_SETCOLOR(71);
  603.         printf("]Download item       [");
  604.         OS_SETCOLOR(87);
  605.         printf("ESC");
  606.         OS_SETCOLOR(71);
  607.         printf("]Exit   ");
  608.  
  609.         curWin.x = 0;
  610.         curWin.y = 1;
  611.         curWin.w = 80;
  612.         curWin.h = 23;
  613.         curWin.text = 207;
  614.         curWin.back = 207;
  615.         simpleBox(curWin);
  616.         drawSearch();
  617.         clearStatus();
  618. }
  619.  
  620. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  621. {
  622.         unsigned int i, j;
  623.         unsigned int lenC, lenS;
  624.  
  625.         for (lenC = 0; c[lenC]; lenC++)
  626.                 ;
  627.         for (lenS = 0; s[lenS]; lenS++)
  628.                 ;
  629.  
  630.         for (i = startPos; i <= lenS - lenC; i++)
  631.         {
  632.                 for (j = 0; s[i + j] == c[j]; j++)
  633.                         ;
  634.  
  635.                 if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  636.                         return i;
  637.                 if (j == lenC)
  638.                         if (n - 1)
  639.                                 n--;
  640.                         else
  641.                                 return i;
  642.         }
  643.         return -1;
  644. }
  645.  
  646. void squeeze(char s[], int c)
  647. {
  648.         int i, j;
  649.  
  650.         for (i = j = 0; s[i] != '\0'; i++)
  651.                 if (s[i] != c)
  652.                         s[j++] = s[i];
  653.         s[j] = '\0';
  654. }
  655.  
  656. char *insert_string(const char *original, const char *to_insert, unsigned int position)
  657. {
  658.         unsigned int original_len = strlen(original);
  659.         unsigned int insert_len = strlen(to_insert);
  660.         unsigned int new_len = original_len + insert_len;
  661.  
  662.         char *new_string = (char *)malloc(new_len + 1); // +1 для \0
  663.         if (new_string == NULL)
  664.         {
  665.                 return NULL; // Обработка ошибки выделения памяти
  666.         }
  667.  
  668.         // Копирование части исходной строки до позиции вставки
  669.         strncpy(new_string, original, position);
  670.         new_string[position] = '\0';
  671.  
  672.         // Вставка строки
  673.         strcat(new_string, to_insert);
  674.  
  675.         // Добавление оставшейся части исходной строки
  676.         strcat(new_string, original + position);
  677.  
  678.         return new_string;
  679. }
  680.  
  681. int cutHeader(void)
  682. {
  683.         unsigned char *count1;
  684.         int counter;
  685.  
  686.         httpErr = httpError();
  687.         if (httpError() != 200)
  688.         {
  689.                 clearStatus();
  690.                 printf("HTTP response:[%u]", httpErr);
  691.                 return 0;
  692.         }
  693.         count1 = strstr(netbuf, "Content-Length:");
  694.         if (count1 == NULL)
  695.         {
  696.                 clearStatus();
  697.                 printf("contLen not found");
  698.                 contLen = 0;
  699.                 httpErr = 999; // bad kostil
  700.                 return 0;
  701.         }
  702.         contLen = atol(count1 + 15);
  703.         // printf("Content-Length: %lu \n\r", contLen);
  704.  
  705.         count1 = strstr(netbuf, "Content-Disposition: attachment; filename="); // 42
  706.                                                                                                                                                    // Content-Disposition: attachment; filename="WED_PRO.TRD"
  707.         if (count1 != NULL)
  708.         {
  709.                 strncpy(link.fname, count1 + 43, 64);
  710.                 strcat(link.fname, "\0");
  711.                 counter = 0;
  712.                 while (link.fname[counter] != '\"')
  713.                 {
  714.                         counter++;
  715.                 }
  716.                 link.fname[counter] = 0;
  717.                 link.hasName = true;
  718.         }
  719.         else
  720.         {
  721.                 strncpy(link.fname, table[limiter.curline].file, 57);
  722.                 strcat(link.fname, ".");
  723.                 strcat(link.fname, table[limiter.curline].ext);
  724.  
  725.                 counter = strlen(link.fname);
  726.                 while (counter != 0)
  727.                 {
  728.                         counter--;
  729.                         if (link.fname[counter] == '.' && table[limiter.curline].option > 1)
  730.                         {
  731.                                 const char *new_string;
  732.                                 char temp[65];
  733.                                 sprintf(temp, "-%02d", limiter.curOpt);
  734.                                 new_string = insert_string(link.fname, temp, counter);
  735.                                 strcpy(link.fname, new_string);
  736.                         }
  737.                 }
  738.                 link.hasName = false;
  739.         }
  740.  
  741.         count1 = strstr(netbuf, "\r\n\r\n");
  742.         if (count1 == NULL)
  743.         {
  744.                 clearStatus();
  745.                 printf("end of header not found\r\n");
  746.         }
  747.         else
  748.         {
  749.                 // printf("header %u bytes\r\n", ((unsigned int)count1 - (unsigned int)netbuf + 4));
  750.         }
  751.         return ((unsigned int)count1 - (unsigned int)netbuf + 4);
  752. }
  753.  
  754. void downDialog(void)
  755. {
  756.         unsigned int nameLong;
  757.  
  758.         mainWinDraw();
  759.  
  760.         nameLong = strlen(link.fname);
  761.         if (nameLong < 21)
  762.         {
  763.                 curWin.w = 23;
  764.         }
  765.         else
  766.         {
  767.                 curWin.w = nameLong + 4;
  768.         }
  769.         curWin.x = 39 - curWin.w / 2;
  770.         curWin.y = 10;
  771.         curWin.h = 4;
  772.         curWin.text = 223;
  773.         curWin.back = 223;
  774.         simpleBox(curWin);
  775.         OS_SETXY(38 - nameLong / 2, curWin.y);
  776.         printf("[%s]", link.fname);
  777. }
  778.  
  779. char getFileEsp(void)
  780. {
  781.         int todo;
  782.         unsigned char byte, firstPacket;
  783.         unsigned long downloaded = 0;
  784.         unsigned int fileSize1, down;
  785.         // const unsigned char sendOk[] = "SEND OK";
  786.         // unsigned int count;
  787.         sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%u", link.host, link.port);
  788.         sendcommand(cmd);
  789.  
  790.         do
  791.         {
  792.                 getAnswer3(); // CONNECT or ERROR or link is not valid
  793.  
  794.                 if (strstr(netbuf, "CONNECT") != NULL)
  795.                 {
  796.                         break;
  797.                 }
  798.                 else
  799.                 {
  800.                         if (strstr(netbuf, "ERROR") != NULL)
  801.                         {
  802.                                 return false;
  803.                         }
  804.                 }
  805.         } while (42); // Try until endo of the days recieve CONNECT or ERROR
  806.  
  807.         getAnswer3(); // OK
  808.  
  809.         sprintf(cmd, "AT+CIPSEND=%u", strlen(link.path) + 2);
  810.         sendcommand(cmd);
  811.         getAnswer3();
  812.  
  813.         do
  814.         {
  815.                 byte = uartReadBlock();
  816.         } while (byte != '>');
  817.  
  818.         // sendcommandNrn(link.path);
  819.         sendcommand(link.path);
  820.         /*
  821.                 count = 0;
  822.                 do
  823.                 {
  824.                         byte = uartReadBlock();
  825.                         if (byte == sendOk[count])
  826.                         {
  827.                                 count++;
  828.                                 // putchar(byte);
  829.                         }
  830.                         else
  831.                         {
  832.                                 count = 0;
  833.                         }
  834.                 } while (count < strlen(sendOk));
  835.  
  836.                 uartReadBlock(); // CR
  837.                 uartReadBlock(); // LF
  838.         */
  839.         firstPacket = true;
  840.         do
  841.         {
  842.                 unsigned char temp[64];
  843.                 limiter.headLng = 0;
  844.                 todo = recvHead();
  845.  
  846.                 if (todo == 0)
  847.                 {
  848.                         writeLog("Error parsing packet size, todo = 0", "getFileEsp     ");
  849.                         writeLog(netbuf, "getFileEsp     ");
  850.                         return false;
  851.                 }
  852.  
  853.                 if (!getdataEsp(todo))
  854.                 {
  855.                         OS_CLS(0);
  856.                         puts("[getdataEsp]Downloading timeout. Exit!");
  857.                         writeLog("Downloading timeout. Exit!", "getFileEsp     ");
  858.                         delayLongKey(5000);
  859.                         exit(0);
  860.                 }
  861.  
  862.                 if (firstPacket)
  863.                 {
  864.                         firstPacket = false;
  865.                         limiter.headLng = cutHeader();
  866.                         todo = todo - limiter.headLng;
  867.                         fileSize1 = contLen / 1024;
  868.  
  869.                         if (!link.hasName)
  870.                         {
  871.                                 curWin.w = 66;
  872.                                 curWin.x = 39 - curWin.w / 2;
  873.                                 curWin.y = 9;
  874.                                 curWin.h = 1;
  875.                                 curWin.text = 103;
  876.                                 curWin.back = 103;
  877.  
  878.                                 strcpy(curWin.tittle, "Введите имя файла");
  879.                                 if (inputBox(curWin, link.fname))
  880.                                 {
  881.                                         strncpy(link.fname, cmd, 64);
  882.                                         strcat(link.fname, "\0");
  883.                                 }
  884.                         }
  885.                         if (httpErr != 200)
  886.                         {
  887.                                 sendcommand("AT+CIPCLOSE");
  888.                                 getAnswer3(); // CLOSED
  889.                                 getAnswer3(); // OK
  890.                                 mainWinDraw();
  891.                                 return false;
  892.                         }
  893.                         downDialog();
  894.                         OS_DELETE(link.fname);
  895.                         saveBuf(link.fname, 00, 0);
  896.                 }
  897.  
  898.                 downloaded = downloaded + todo;
  899.                 down = downloaded / 1024;
  900.                 OS_SETCOLOR(223);
  901.                 sprintf(temp, "%4u  of %4u kb", down, fileSize1);
  902.                 OS_SETXY(38 - strlen(temp) / 2, 11);
  903.                 puts(temp);
  904.                 saveBuf(link.fname, 01, todo);
  905.                 drawClock();
  906.         } while (downloaded < contLen);
  907.         sendcommand("AT+CIPCLOSE");
  908.         getAnswer3(); // CLOSED
  909.         getAnswer3(); // OK
  910.         mainWinDraw();
  911.         return true;
  912. }
  913.  
  914. char getFileNet(void)
  915. {
  916.         int todo, socket;
  917.         char firstPacket;
  918.  
  919.         unsigned int fileSize1;
  920.         unsigned long downloaded = 0;
  921.         unsigned int down;
  922.  
  923.         socket = OpenSock(AF_INET, SOCK_STREAM);
  924.         if (testOperation2("OS_NETSOCKET", socket) != 1)
  925.         {
  926.                 getchar();
  927.                 quit();
  928.         }
  929.  
  930.         todo = netConnect(socket, 1);
  931.         if (testOperation2("OS_NETCONNECT", todo) != 1)
  932.         {
  933.                 getchar();
  934.                 quit();
  935.         }
  936.  
  937.         todo = tcpSend(socket, (unsigned int)&link.path, strlen(link.path), 1);
  938.         if (testOperation2("OS_WIZNETWRITE", todo) != 1)
  939.         {
  940.                 getchar();
  941.                 quit();
  942.         }
  943.  
  944.         firstPacket = true;
  945.         do
  946.         {
  947.                 unsigned char temp[64];
  948.                 limiter.headLng = 0;
  949.                 todo = tcpRead(socket, 1);
  950.                 testOperation("OS_WIZNETREAD", todo);
  951.                 if (todo == 0)
  952.                 {
  953.                         break;
  954.                 }
  955.                 if (firstPacket)
  956.                 {
  957.                         firstPacket = false;
  958.                         limiter.headLng = cutHeader();
  959.                         todo = todo - limiter.headLng;
  960.                         fileSize1 = contLen / 1024;
  961.  
  962.                         if (!link.hasName)
  963.                         {
  964.                                 curWin.w = 66;
  965.                                 curWin.x = 39 - curWin.w / 2;
  966.                                 curWin.y = 9;
  967.                                 curWin.h = 1;
  968.                                 curWin.text = 103;
  969.                                 curWin.back = 103;
  970.  
  971.                                 strcpy(curWin.tittle, "Введите имя файла");
  972.                                 if (inputBox(curWin, link.fname))
  973.                                 {
  974.                                         strncpy(link.fname, cmd, 64);
  975.                                         strcat(link.fname, "\0");
  976.                                 }
  977.                         }
  978.                         if (httpErr != 200)
  979.                         {
  980.                                 netShutDown(socket, 0);
  981.                                 mainWinDraw();
  982.                                 return false;
  983.                         }
  984.                         downDialog();
  985.                         saveBuf(link.fname, 00, 0);
  986.                 }
  987.                 downloaded = downloaded + todo;
  988.                 down = downloaded / 1024;
  989.                 OS_SETCOLOR(223);
  990.                 sprintf(temp, "%4u  of %4u kb", down, fileSize1);
  991.                 OS_SETXY(38 - strlen(temp) / 2, 11);
  992.                 puts(temp);
  993.                 saveBuf(link.fname, 01, todo);
  994.                 drawClock();
  995.         } while (downloaded < contLen);
  996.  
  997.         netShutDown(socket, 0);
  998.  
  999.         if (downloaded != contLen)
  1000.         {
  1001.                 puts("File download error!");
  1002.                 puts("File download error!");
  1003.                 puts("File download error!");
  1004.                 puts("File download error!");
  1005.                 waitKey();
  1006.         }
  1007.         mainWinDraw();
  1008.         return true;
  1009. }
  1010.  
  1011. char getFile(unsigned char number)
  1012. {
  1013.         int result = 0;
  1014.         unsigned char option = 1;
  1015.  
  1016.         for (option = 1; option <= table[number].option; option++)
  1017.         {
  1018.                 sprintf(link.path, "GET /get/%lu/%u%s%s%s", table[number].id, option, userAgent1, link.host, userAgent2);
  1019.                 limiter.curOpt = option;
  1020.                 switch (netDriver)
  1021.                 {
  1022.                 case 0:
  1023.                         result = getFileNet();
  1024.                         break;
  1025.                 case 1:
  1026.                         result = getFileEsp();
  1027.                         break;
  1028.                 default:
  1029.                         break;
  1030.                 }
  1031.         }
  1032.         return result;
  1033. }
  1034.  
  1035. char makeRequestEsp(void)
  1036. {
  1037.  
  1038.         int todo;
  1039.         unsigned char byte, firstPacket;
  1040.         unsigned long downloaded = 0;
  1041.         // unsigned int count;
  1042.         // const unsigned char sendOk[] = "SEND OK";
  1043.  
  1044.         sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%u", link.host, link.port);
  1045.         sendcommand(cmd);
  1046.  
  1047.         do
  1048.         {
  1049.                 getAnswer3(); // CONNECT or ERROR or link is not valid
  1050.  
  1051.                 if (strstr(netbuf, "CONNECT") != NULL)
  1052.                 {
  1053.                         break;
  1054.                 }
  1055.                 else
  1056.                 {
  1057.                         if (strstr(netbuf, "ERROR") != NULL)
  1058.                         {
  1059.                                 return false;
  1060.                         }
  1061.                 }
  1062.         } while (42); // Try until endo of the days recieve CONNECT or ERROR
  1063.  
  1064.         getAnswer3(); // OK
  1065.  
  1066.         sprintf(cmd, "AT+CIPSEND=%u", strlen(link.path) + 2);
  1067.         sendcommand(cmd);
  1068.         getAnswer3();
  1069.  
  1070.         do
  1071.         {
  1072.                 byte = uartReadBlock();
  1073.         } while (byte != '>');
  1074.  
  1075.         // sendcommandNrn(link.path);
  1076.         sendcommand(link.path);
  1077.         /*
  1078.                 count = 0;
  1079.                 do
  1080.                 {
  1081.                         byte = uartReadBlock();
  1082.                         if (byte == sendOk[count])
  1083.                         {
  1084.                                 count++;
  1085.                                 // putchar(byte);
  1086.                         }
  1087.                         else
  1088.                         {
  1089.                                 count = 0;
  1090.                         }
  1091.                 } while (count < strlen(sendOk));
  1092.  
  1093.                 uartReadBlock(); // CR
  1094.                 uartReadBlock(); // LF
  1095.         */
  1096.         firstPacket = true;
  1097.         do
  1098.         {
  1099.                 limiter.headLng = 0;
  1100.                 todo = recvHead();
  1101.  
  1102.                 if (todo == 0)
  1103.                 {
  1104.                         writeLog("Error parsing packet size, todo = 0", "makeRequestEsp ");
  1105.                         writeLog(netbuf, "makeRequestEsp ");
  1106.                         return false;
  1107.                 }
  1108.  
  1109.                 if (!getdataEsp(todo))
  1110.                 {
  1111.                         OS_CLS(0);
  1112.                         puts("[makeRequestEsp]Downloading timeout. Exit!");
  1113.                         writeLog("[makeRequestEsp]Downloading timeout. Exit!", "makeRequestEsp ");
  1114.                         delayLongKey(5000);
  1115.                         exit(0);
  1116.                 }
  1117.  
  1118.                 if (firstPacket)
  1119.                 {
  1120.                         firstPacket = false;
  1121.                         limiter.headLng = cutHeader();
  1122.                         todo = todo - limiter.headLng;
  1123.  
  1124.                         if (httpErr != 200)
  1125.                         {
  1126.                                 sendcommand("AT+CIPCLOSE");
  1127.                                 getAnswer3(); // CLOSED
  1128.                                 getAnswer3(); // OK
  1129.                                 return false;
  1130.                         }
  1131.                 }
  1132.                 if (downloaded + todo > sizeof(buf))
  1133.                 {
  1134.                         printf("dataBuffer overrun... %lu reached \n\r", downloaded + todo);
  1135.                         return false;
  1136.                 }
  1137.                 memcpy(buf + downloaded, netbuf + limiter.headLng, todo);
  1138.                 downloaded = downloaded + todo;
  1139.         } while (downloaded < contLen);
  1140.  
  1141.         sendcommand("AT+CIPCLOSE");
  1142.         getAnswer3(); // CLOSED
  1143.         getAnswer3(); // OK
  1144.         buf[downloaded + 1] = 0;
  1145.         return downloaded;
  1146. }
  1147.  
  1148. char makeRequestNet(void)
  1149. {
  1150.         int socket, todo;
  1151.         char firstPacket;
  1152.         unsigned long downloaded = 0;
  1153.  
  1154.         if (!dnsResolve(link.host))
  1155.         {
  1156.                 clearStatus();
  1157.                 printf("Ошибка определения адреса '%s'", link.host);
  1158.                 return false;
  1159.         }
  1160.  
  1161.         targetadr.porth = link.port >> 8;
  1162.         targetadr.portl = link.port;
  1163.         // clearStatus();
  1164.         // printf("Connecting to %u.%u.%u.%u:%u", targetadr.b1, targetadr.b2, targetadr.b3, targetadr.b4, targetadr.porth * 256 + targetadr.portl);
  1165.  
  1166.         socket = OpenSock(AF_INET, SOCK_STREAM);
  1167.         if (testOperation2("OS_NETSOCKET", socket) != 1)
  1168.         {
  1169.                 getchar();
  1170.                 quit();
  1171.         }
  1172.  
  1173.         todo = netConnect(socket, 1);
  1174.         if (testOperation2("OS_NETCONNECT", todo) != 1)
  1175.         {
  1176.                 getchar();
  1177.                 quit();
  1178.         }
  1179.         todo = tcpSend(socket, (unsigned int)&link.path, strlen(link.path), 1);
  1180.         if (testOperation2("OS_WIZNETWRITE", todo) != 1)
  1181.         {
  1182.                 getchar();
  1183.                 quit();
  1184.         }
  1185.         firstPacket = true;
  1186.         do
  1187.         {
  1188.                 limiter.headLng = 0;
  1189.                 todo = tcpRead(socket, 1);
  1190.                 testOperation("OS_WIZNETREAD", todo); // Quit if too many retries
  1191.  
  1192.                 if (firstPacket)
  1193.                 {
  1194.                         firstPacket = false;
  1195.                         limiter.headLng = cutHeader();
  1196.                         todo = todo - limiter.headLng;
  1197.  
  1198.                         if (httpErr != 200)
  1199.                         {
  1200.                                 netShutDown(socket, 0);
  1201.                                 return false;
  1202.                         }
  1203.                 }
  1204.  
  1205.                 if (downloaded + todo > sizeof(buf))
  1206.                 {
  1207.                         printf("dataBuffer overrun... %lu reached \n\r", downloaded + todo);
  1208.                         return false;
  1209.                 }
  1210.                 memcpy(buf + downloaded, netbuf + limiter.headLng, todo);
  1211.                 downloaded = downloaded + todo;
  1212.         } while (downloaded != contLen); // ref < лучше
  1213.  
  1214.         netShutDown(socket, 0);
  1215.         buf[downloaded + 1] = 0;
  1216.         return downloaded;
  1217. }
  1218.  
  1219. char makeRequest(const char *request)
  1220. {
  1221.         char result;
  1222.         unsigned int counter, len;
  1223.         char tempreq[256];
  1224.  
  1225.         sendReqdialog();
  1226.  
  1227.         strcpy(tempreq, request);
  1228.         len = strlen(tempreq);
  1229.         for (counter = 0; counter < len; counter++)
  1230.         {
  1231.                 if (tempreq[counter] == ' ' || tempreq[counter] == '-')
  1232.                 {
  1233.                         tempreq[counter] = '*';
  1234.                 }
  1235.         }
  1236.         sprintf(link.path, "GET /?s=%s&p=%d%s%s%s", tempreq, limiter.curPage, userAgent1, link.host, userAgent2);
  1237.  
  1238.         switch (netDriver)
  1239.         {
  1240.         case 0:
  1241.                 result = makeRequestNet();
  1242.                 break;
  1243.         case 1:
  1244.                 result = makeRequestEsp();
  1245.                 break;
  1246.  
  1247.         default:
  1248.                 return false;
  1249.         }
  1250.  
  1251.         return result;
  1252. }
  1253.  
  1254. int findLimiters(int n)
  1255. {
  1256.         n = pos(buf, "^", 1, n + 1);
  1257.         if (n == -1)
  1258.         {
  1259.                 return -2;
  1260.         }
  1261.         limiter.first = n + 1;
  1262.         n = pos(buf, "^", 1, n + 1);
  1263.         if (n == -1)
  1264.         {
  1265.                 return -2;
  1266.         }
  1267.         limiter.second = n - 1;
  1268.         return n;
  1269. }
  1270. void fillTable(void)
  1271. {
  1272.         int counter = 0;
  1273.         limiter.second = -1;
  1274.         limiter.total = 0;
  1275.         do
  1276.         {
  1277.                 if (findLimiters(limiter.second) != -2)
  1278.                 {
  1279.                         table[counter].id = atol(buf + limiter.first);
  1280.                         limiter.total++;
  1281.                 }
  1282.  
  1283.                 if (findLimiters(limiter.second) != -2)
  1284.                 {
  1285.                         strncpy(table[counter].name, buf + limiter.first, limiter.second - limiter.first + 1);
  1286.                         strcat(table[counter].name, "\0");
  1287.                         table[counter].name[limiter.second - limiter.first + 1] = 0;
  1288.                 }
  1289.                 if (findLimiters(limiter.second) != -2)
  1290.                 {
  1291.                         strncpy(table[counter].file, buf + limiter.first, limiter.second - limiter.first - 3);
  1292.                         strcat(table[counter].file, "\0");
  1293.                         table[counter].file[limiter.second - limiter.first - 3] = 0;
  1294.                         strncpy(table[counter].ext, buf + limiter.second - 2, 3);
  1295.                         strcat(table[counter].ext, "\0");
  1296.                 }
  1297.  
  1298.                 if (findLimiters(limiter.second) != -2)
  1299.                 {
  1300.                         table[counter].size = atol(buf + limiter.first);
  1301.                 }
  1302.  
  1303.                 if (findLimiters(limiter.second) != -2)
  1304.                 {
  1305.                         table[counter].option = atoi(buf + limiter.first);
  1306.                 }
  1307.  
  1308.                 if (findLimiters(limiter.second) != -2)
  1309.                 {
  1310.                         table[counter].year = atoi(buf + limiter.first);
  1311.                 }
  1312.  
  1313.                 limiter.second++;
  1314.                 counter++;
  1315.         } while (counter < 12);
  1316. }
  1317.  
  1318. void renderResult(char currentLine)
  1319. {
  1320.         int counter, line = 2;
  1321.  
  1322.         for (counter = 0; counter < limiter.total; counter++)
  1323.         {
  1324.                 // printf("[%lu] ", table[counter].id);
  1325.  
  1326.                 if (counter == currentLine)
  1327.                 {
  1328.                         OS_SETCOLOR(121);
  1329.                         OS_SETXY(2, line);
  1330.                         spaces(76);
  1331.                         OS_SETXY(2, line + 1);
  1332.                         spaces(76);
  1333.                 }
  1334.                 else
  1335.                 {
  1336.                         if (counter % 2 == 0)
  1337.                         {
  1338.                                 OS_SETCOLOR(206);
  1339.                         }
  1340.                         else
  1341.                         {
  1342.                                 OS_SETCOLOR(207);
  1343.                         }
  1344.                 }
  1345.                 OS_SETXY(2, line);
  1346.                 spaces(76);
  1347.                 OS_SETXY(2, line);
  1348.                 printf("%s ", table[counter].name);
  1349.                 OS_SETXY(48, line);
  1350.                 printf("%s  %6lu  %u  %4u", table[counter].ext, table[counter].size, table[counter].option, table[counter].year);
  1351.                 line++;
  1352.                 OS_SETXY(2, line);
  1353.                 spaces(76);
  1354.                 OS_SETXY(5, line);
  1355.                 printf("%s ", table[counter].file);
  1356.                 line++;
  1357.         }
  1358.         OS_SETCOLOR(206);
  1359.         for (counter = limiter.total; counter < 10; counter++)
  1360.         {
  1361.                 OS_SETXY(2, line);
  1362.                 spaces(76);
  1363.                 line++;
  1364.                 OS_SETXY(2, line);
  1365.                 spaces(76);
  1366.                 line++;
  1367.         }
  1368. }
  1369.  
  1370. char getKey(void)
  1371. {
  1372.         char key;
  1373.         key = OS_GETKEY();
  1374.         switch (key)
  1375.         {
  1376.         case 27: // escape - exit
  1377.                 OS_CLS(0);
  1378.                 OS_SETGFX(-1);
  1379.                 exit(0);
  1380.                 break;
  1381.         case 's':
  1382.         case 'ы':
  1383.         case 'Ы':
  1384.         case 'S':
  1385.         fuckingoto:
  1386.                 limiter.total = 0;
  1387.                 limiter.curline = 0;
  1388.                 curWin.w = 40;
  1389.                 curWin.x = 80 / 2 - curWin.w / 2 - 1;
  1390.                 curWin.y = 10;
  1391.                 curWin.h = 1;
  1392.                 curWin.text = 103;
  1393.                 curWin.back = 103;
  1394.                 strcpy(curWin.tittle, "Введите поисковый запрос");
  1395.  
  1396.                 if (inputBox(curWin, ""))
  1397.                 {
  1398.                         strcpy(search, cmd);
  1399.                         limiter.curPage = 0;
  1400.                         drawSearch();
  1401.                 }
  1402.                 else
  1403.                 {
  1404.                         mainWinDraw();
  1405.                         OS_SETXY(32, 11);
  1406.                         OS_SETCOLOR(206);
  1407.                         puts("No results found");
  1408.                         limiter.total = 0;
  1409.                         limiter.curline = 0;
  1410.                         break;
  1411.                 }
  1412.  
  1413.                 if (makeRequest(search) < 2)
  1414.                 {
  1415.                         mainWinDraw();
  1416.                         OS_SETXY(32, 11);
  1417.                         OS_SETCOLOR(206);
  1418.                         puts("No results found");
  1419.                         limiter.total = 0;
  1420.                         limiter.curline = 0;
  1421.                         return key;
  1422.                 }
  1423.  
  1424.                 OS_SETXY(1, 2);
  1425.                 OS_SETCOLOR(206);
  1426.                 fillTable();
  1427.                 break;
  1428.         case 'h':
  1429.         case 'H':
  1430.         case 'р':
  1431.         case 'Р':
  1432.                 limiter.total = 0;
  1433.                 limiter.curline = 0;
  1434.                 curHost++;
  1435.                 if (curHost > 1)
  1436.                 {
  1437.                         curHost = 0;
  1438.                 }
  1439.                 strcpy(link.host, hosts[curHost]);
  1440.                 mainWinDraw();
  1441.                 break;
  1442.         case 'q':
  1443.         case 'Q':
  1444.         case 'Й':
  1445.         case 'й':
  1446.         case 250:
  1447.                 if (limiter.total != 0)
  1448.                 {
  1449.                         if (limiter.curline < 1)
  1450.                         {
  1451.                                 limiter.curline = limiter.total - 1;
  1452.                         }
  1453.                         else
  1454.                         {
  1455.                                 limiter.curline--;
  1456.                         }
  1457.                 }
  1458.                 break;
  1459.         case 'a':
  1460.         case 'A':
  1461.         case 'ф':
  1462.         case 'Ф':
  1463.         case 249:
  1464.                 if (limiter.total != 0)
  1465.                 {
  1466.                         if (limiter.curline > limiter.total - 2)
  1467.                         {
  1468.                                 limiter.curline = 0;
  1469.                         }
  1470.                         else
  1471.                         {
  1472.                                 limiter.curline++;
  1473.                         }
  1474.                 }
  1475.                 break;
  1476.         case 248: // left
  1477.         case 'o':
  1478.  
  1479.                 if (limiter.curPage != 0 && limiter.total != 0)
  1480.                 {
  1481.                         limiter.curPage--;
  1482.                         mainWinDraw();
  1483.                         if (makeRequest(search) < 2)
  1484.                         {
  1485.                                 OS_SETXY(32, 11);
  1486.                                 OS_SETCOLOR(206);
  1487.                                 puts("No results found");
  1488.                                 limiter.total = 0;
  1489.                                 limiter.curline = 0;
  1490.                                 break;
  1491.                         }
  1492.                         // OS_SETXY(1, 2);
  1493.                         // OS_SETCOLOR(206);
  1494.                         fillTable();
  1495.                         limiter.curline = 0;
  1496.                 }
  1497.                 break;
  1498.         case 251: // right
  1499.         case 'p':
  1500.                 if (limiter.total != 0)
  1501.                 {
  1502.                         limiter.curPage++;
  1503.                         mainWinDraw();
  1504.                         if (makeRequest(search) < 2)
  1505.                         {
  1506.                                 limiter.curPage--;
  1507.                                 drawPage();
  1508.                                 makeRequest(search);
  1509.                         }
  1510.                         // OS_SETXY(1, 2);
  1511.                         // OS_SETCOLOR(206);
  1512.                         fillTable();
  1513.                         limiter.curline = 0;
  1514.                 }
  1515.                 break;
  1516.         case 13:
  1517.                 if (limiter.total != 0)
  1518.                 {
  1519.                         getFile(limiter.curline);
  1520.                 }
  1521.                 else
  1522.                 {
  1523.                         goto fuckingoto;
  1524.                 }
  1525.                 break;
  1526.         default:
  1527.                 break;
  1528.         }
  1529.  
  1530.         if (key != 0)
  1531.         {
  1532.                 renderResult(limiter.curline);
  1533.         }
  1534.         else
  1535.         {
  1536.                 YIELD();
  1537.         }
  1538.         return key;
  1539. }
  1540.  
  1541. C_task main(int argc, const char *argv[])
  1542. {
  1543.         OS_HIDEFROMPARENT();
  1544.         OS_SETGFX(0x86);
  1545.         OS_CLS(0);
  1546.         OS_SETSYSDRV();
  1547.         printf("[Build:%s  %s]", __DATE__, __TIME__);
  1548.         init();
  1549.         // printTable();
  1550.         //  waitKey();
  1551.         OS_CLS(0);
  1552.         mainWinDraw();
  1553.  
  1554.         do
  1555.         {
  1556.                 getKey();
  1557.         } while (42);
  1558. }
  1559.