Last active
December 23, 2015 12:19
-
-
Save shuzo-kino/6634743 to your computer and use it in GitHub Desktop.
よくあるコマンド入力型の実行コード習作。structとtableを使うと比較的きれいに書ける。
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)にて確認。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#define TRUE 0 | |
#define FALSE -1 | |
static void start(char argc, char **argv); | |
static void hello(char argc, char **argv); | |
typedef struct | |
{ | |
char *cmd; | |
void (*func)(char argc, char **argv); | |
} cmd_t; | |
static cmd_t cmd_tbl[] = { | |
{"start", start}, | |
{"hello", hello}, | |
{NULL, NULL} //いわゆる番兵法 | |
}; | |
int main() { | |
char argc; | |
char **argv; | |
int i; | |
char str[10]; | |
printf("-------\n"); | |
printf(">> "); | |
fgets(str,sizeof(str),stdin); | |
str[strlen(str) - 1] = '\0'; //chomp! | |
for (i=0; cmd_tbl[i].cmd != NULL; i++) { | |
if (strcmp(str, cmd_tbl[i].cmd) == TRUE) { | |
//テーブル中にコマンドがあれば、実行. | |
cmd_tbl[i].func(argc, argv); | |
return 0; | |
} | |
} | |
printf("no cmd!"); | |
return 0; | |
} | |
void start(char argc, char **argv){ | |
printf("Welcome\n"); | |
} | |
void hello(char argc, char **argv){ | |
printf("Hello world\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment