Subversion Repositories Kolibri OS

Rev

Rev 3245 | Rev 4015 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. /// ===========================================================
  3.  
  4. void command_clear()
  5. {
  6. for (;CMD_POS;CMD_POS--)
  7.         printf("%c %c", 8, 8);
  8. CMD[0]='\0';
  9. }
  10.  
  11. /// ===========================================================
  12.  
  13. void command_history_add(char command[])
  14. {
  15.  
  16. if (    (0 != strcmp( CMD_HISTORY[0], CMD)) &&
  17.         (0 != strcmp( CMD_HISTORY[1], CMD)) &&
  18.         (0 != strcmp( CMD_HISTORY[2], CMD)) &&
  19.         (0 != strcmp( CMD_HISTORY[3], CMD)) &&
  20.         (0 != strcmp( CMD_HISTORY[4], CMD)) &&
  21.         (0 != strcmp( CMD_HISTORY[5], CMD)) &&
  22.         (0 != strcmp( CMD_HISTORY[6], CMD)) &&
  23.         (0 != strcmp( CMD_HISTORY[7], CMD)) &&
  24.         (0 != strcmp( CMD_HISTORY[8], CMD)) &&
  25.         (0 != strcmp( CMD_HISTORY[9], CMD)) )
  26.  
  27.         {
  28.         strcpy(CMD_HISTORY[9], CMD_HISTORY[8]);
  29.         strcpy(CMD_HISTORY[8], CMD_HISTORY[7]);
  30.         strcpy(CMD_HISTORY[7], CMD_HISTORY[6]);
  31.         strcpy(CMD_HISTORY[6], CMD_HISTORY[5]);
  32.         strcpy(CMD_HISTORY[5], CMD_HISTORY[4]);
  33.         strcpy(CMD_HISTORY[4], CMD_HISTORY[3]);
  34.         strcpy(CMD_HISTORY[3], CMD_HISTORY[2]);
  35.         strcpy(CMD_HISTORY[2], CMD_HISTORY[1]);
  36.         strcpy(CMD_HISTORY[1], CMD_HISTORY[0]);
  37.  
  38.         strcpy(CMD_HISTORY[0], CMD);
  39.        
  40.         if (CMD_HISTORY_NUM_REAL < CMD_HISTORY_NUM)
  41.                 CMD_HISTORY_NUM_REAL++;
  42.        
  43.         }
  44.  
  45. }
  46.  
  47. /// ===========================================================
  48.  
  49. void command_get()
  50. {
  51. unsigned key;
  52. //unsigned pos = 0;
  53. int hist;
  54.  
  55. CMD_POS = 0;
  56. CMD_NUM = 0;
  57.  
  58. for (;;)
  59.         {
  60.         key = getch();
  61.         if ( 0 != (key & 0xff) )
  62.                 {
  63.                 key &= 0xff;
  64.                 switch (key)
  65.                         {
  66.                         case 27: // ESC
  67.                                 command_clear();
  68.                                 break;
  69.  
  70.                         case 13: // ENTER
  71.                                 CMD[CMD_POS] = '\0';
  72.                                 printf("\n\r");
  73.  
  74.                                 command_history_add(CMD);
  75.                                 return;
  76.  
  77.                         case 8: // BACKSPACE
  78.                                 if (CMD_POS > 0)
  79.                                         {
  80.                                         printf ("%c %c", 8, 8);
  81.                                         CMD_POS--;
  82.                                         }
  83.                                 break;
  84.  
  85.                         case 9: // TAB
  86.                                 break;
  87.  
  88.                         default:
  89.                                 if (CMD_POS < 255)
  90.                                         {
  91.                                        
  92.                                         if ( kol_key_control() & 0x40 ) // ¥á«¨ ¢ª«îçñ­ CapsLock
  93.                                                 if ( (kol_key_control() & 1) || (kol_key_control() & 2)) // ¥á«¨ ­ ¦ âë è¨äâë
  94.                                                         key = tolower(key);
  95.                                                 else
  96.                                                         key = toupper(key);
  97.  
  98.                                         CMD[CMD_POS] = key;
  99.                                         CMD_POS++;
  100.                                         printf("%c", key);
  101.                                         }
  102.                                         break;
  103.                         };
  104.                 }
  105.         else    // ®¡à ¡®âª  à áè¨à¥­­ëå ª« ¢¨è
  106.                 {
  107.                 key = (key>>8)&0xff;
  108. //              printf ("%d\n\r", key);
  109.  
  110.                 switch (key)
  111.                         {
  112.  
  113.                         case 80: // Down
  114.                                 for (hist = 0; hist < CMD_HISTORY_NUM; hist++)
  115.                                         {
  116.                                         command_clear();
  117.  
  118.                                         if (CMD_NUM < CMD_HISTORY_NUM-1)
  119.                                                 CMD_NUM++;
  120.                                         else
  121.                                                 CMD_NUM = 0;
  122.                                
  123.                                         printf( CMD_HISTORY[CMD_NUM] );
  124.                                         strcpy(CMD, CMD_HISTORY[CMD_NUM]);
  125.                                         if ((CMD_POS = strlen(CMD)) != 0)
  126.                                                 break;
  127.                                         }
  128.  
  129.                                 break;
  130.  
  131.                         case 72: // Up
  132.                                 for (hist = 0; hist < CMD_HISTORY_NUM; hist++)
  133.                                         {
  134.                                         command_clear();
  135.  
  136.                                         if (CMD_NUM > 0)
  137.                                                 CMD_NUM--;
  138.                                         else
  139.                                                 CMD_NUM = CMD_HISTORY_NUM-1;
  140.  
  141.                                         printf( CMD_HISTORY[CMD_NUM] );
  142.                                         strcpy(CMD, CMD_HISTORY[CMD_NUM]);
  143.                                         if ((CMD_POS = strlen(CMD)) != 0)
  144.                                                 break;
  145.                                         }
  146.                                 break;
  147.  
  148.                         case 0: // console window closed
  149.                                 cmd_exit(NULL);
  150.  
  151.                         };
  152.                 }
  153.                
  154.         }
  155. }
  156.  
  157.  
  158.  
  159. /// ===========================================================
  160.  
  161. int command_get_cmd(char cmd[])
  162. {
  163. unsigned i, len;
  164. int quote = 0;
  165.  
  166. if (CMD[0]=='"')
  167.    quote = 1;
  168.  
  169. if (quote == 0)
  170.    {
  171.    for (i=0;;i++)
  172.         {
  173.         cmd[i] = CMD[i];
  174.         if (0 == cmd[i])
  175.                 {
  176.                 i = -2;
  177.                 break;
  178.                 }
  179.         if ( iswhite(cmd[i]) )
  180.                 {
  181.                 cmd[i] = '\0';
  182.                 break;
  183.                 }
  184.         }
  185.    return i+1;
  186.    }
  187.    else
  188.    {
  189.    len = 0;
  190.    for (i=1;;i++)
  191.         {
  192.         cmd[len] = CMD[i];
  193.         if (0 == cmd[len])
  194.                 {
  195.                 len = -2;
  196.                 break;
  197.                 }
  198.         if ( cmd[len] == '"' )
  199.                 {
  200.                 cmd[len] = '\0';
  201.                 break;
  202.                 }
  203.         len++;
  204.         }
  205.    trim(cmd);
  206.    return len+2;
  207.    }
  208. }
  209.  
  210. /// ===========================================================
  211.  
  212. typedef int (*handler1_t)(char* arg);
  213.  
  214. /// ===========================================================
  215.  
  216. void command_execute()
  217. {
  218. char cmd[256];
  219. char args[256];
  220. unsigned arg;
  221. int i;
  222. int result;
  223.  
  224. trim(CMD);
  225. arg = command_get_cmd(cmd);
  226.  
  227. if ( !strlen(cmd) )
  228.         return;
  229.  
  230. strcpy(args, CMD+arg);
  231. trim(args);
  232.  
  233. for (i = 0; i < NUM_OF_CMD; i++)
  234.         {
  235.         if (!strcmp(cmd, COMMANDS[i].name))
  236.                 {
  237.                 result = ((handler1_t)COMMANDS[i].handler)(args);
  238.                 if (result != TRUE)
  239.                    {
  240.                    #if LANG_ENG
  241.                        printf("  Error!\n\r");
  242.                    #elif LANG_RUS
  243.                        printf("  Žè¨¡ª !\n\r");
  244.                    #endif
  245.                     }
  246.                 return;
  247.                 }
  248.         }
  249.  
  250.  
  251. if ( -1 != alias_search(CMD) )
  252.         {
  253.         strcpy(CMD, ALIASES+64*1024+256*alias_search(CMD));
  254.         command_execute();
  255.         return;
  256.         }
  257.  
  258. executable_run(cmd, args);
  259.  
  260. }
  261.  
  262. /// ===========================================================
  263.  
  264.