0

私の目標は、ユーザーに入力されたコマンドの履歴を表示させ(historyArray--done)、を入力して履歴内の任意のコマンドを再実行できるようにすることですhistory 1history 2ここで12はから印刷されたコマンドのリストの番号ですhistoryArrayhistory 1ユーザー入力の2番目のパラメーター()からインデックスを取得することができました。私の質問は、history N?から取得した特定のコマンドを実行する方法です。

したがって、たとえば:

     hshell> test [Enter]
     Command not found
     hshell> history 1
     Command not found

これが私の進歩です:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
    int i=0; int j=0; int k=0;
    int elementCounter = 0;
    char inputString[100];
    char *result=NULL;
    char delims[] = " ";
    char historyArray[30][20] = {0};
    char tokenArray[20][20] ;
    int tempIndex = 0;
    char hCommand[2][20]={0};

    do
    {
             j = 0;
             printf("hshell>");
             gets(inputString);

             strcpy (historyArray[k], inputString);
             k = (k+1) % 20;

            if (elementCounter <= 20)
             {         
              elementCounter++;                
             }

             if (elementCounter == 21)
             {
                k = 20;
                for (i=0; i<20; i++)
                {
                    strcpy(historyArray[i], historyArray[i+1]);
                }
                 strcpy (historyArray[19], inputString);                 
             }

             // Break the string into parts
             result = strtok(inputString, delims);


             while (result!=NULL)
             {
                  strcpy(tokenArray[j], result);
                   j++;
                  result= strtok(NULL, delims);                      
             }

              if (strcmp(tokenArray[0], "exit") == 0)
              {
                 return 0;
              }
              else if (strcmp(tokenArray[0], "history") ==  0)
              {
                   if (j>1)
                   { 
                      tempIndex = atoi(tokenArray[1]);
                     strcpy(hCommand,historyArray[tempIndex-1]);
                     puts(hCommand);
                    // tempIndex = atoi(tokenArray[j]);
                     //puts(tempIndex);
                   }
                   else
                   {
                       //print history array
                       for (i=0; i<elementCounter-1;i++)
                           printf("%i. %s\n", i+1, historyArray[i]);
                   }
              }
              else
              {
                  printf("Command not found\n");
              }



    }while (1);
}
  • hCommandから取得したコマンドを保存する場所ですhistoryArray
  • 私はWindowsマシンを使用しています。
4

2 に答える 2

1

実行したいコマンドの名前を取得したら、システムコールexecを実行することをお勧めします。execが、現在のプロセスイメージを実行しようとしているイメージに置き換えることを考慮に入れてください。そうでなければ、フォークに興味があるかもしれません。

EDIT#1次に、このAPIが必要だと思います。これらの関数のどれが私が最初に提供したものと同等であるか私はよく知らないことに注意してください。少し時間があれば、それを理解できますよね?:)

于 2013-02-26T15:15:48.007 に答える
0

stdlib.hの「system」関数を使用できます。

#include <stdlib.h>
int system(const char *command);

この関数は、windowsと*nixの両方に含まれています。fork電話や個別に心配する必要はありませんCreateProcess。これで面倒を見ることができます。詳細については、MSDNのドキュメントを参照してください。

コードには、次のように記述します。

system(hCommand);

コマンドが終了すると戻ります(同期呼び出しです)。

于 2013-02-26T15:34:24.677 に答える