|
1 | | -#include <stdio.h> |
| 1 | +/* |
| 2 | + * @file ssu_crontab.c |
| 3 | + * @brief ssu_crontab 프로그램 |
| 4 | + * @author 김병준 (kbj9704@gmail.com) |
| 5 | + */ |
| 6 | +#include "ssu_crontab.h" |
2 | 7 |
|
| 8 | +#define DEBUG |
| 9 | + |
| 10 | +/** |
| 11 | + * @brief ssu_crontab 메인 함수 |
| 12 | + */ |
3 | 13 | int main(void) |
4 | 14 | { |
5 | | - printf("Crontab!\n"); |
6 | | - return 0; |
| 15 | + FILE *fp; |
| 16 | + if (access(CRONTAB_FILE, F_OK) < 0) { |
| 17 | + if ((fp = fopen(CRONTAB_FILE, "w+")) < 0) |
| 18 | + fprintf(stderr, "main: fopen error for %s\n", CRONTAB_FILE); |
| 19 | + else |
| 20 | + fclose(fp); |
| 21 | + } |
| 22 | + |
| 23 | + prompt(); |
| 24 | + exit(0); // 정상 종료 |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @brief 프롬프트 메인 |
| 29 | + */ |
| 30 | +void prompt(void) // 프롬프트 메인 |
| 31 | +{ |
| 32 | + char command_buffer[MAX_BUFFER_SIZE]; // 명령행 버퍼 |
| 33 | + CommandToken command; // 명령행 토큰 구조체 |
| 34 | + |
| 35 | + while (true) { |
| 36 | + fputs("20162448>", stdout); // 프롬프트 출력 |
| 37 | + fgets(command_buffer, MAX_BUFFER_SIZE, stdin); // 명령행 입력 |
| 38 | + strcpy(command_buffer, ltrim(rtrim(command_buffer))); // 명령행 좌우 공백 제거 |
| 39 | + make_command_token(&command, command_buffer); // 명령행 토큰화 |
| 40 | + switch (get_command_type(command.argv[0])) { |
| 41 | + case ADD: |
| 42 | + break; |
| 43 | + |
| 44 | + case REMOVE: |
| 45 | + break; |
| 46 | + |
| 47 | + case EXIT: |
| 48 | + exit(0); |
| 49 | + break; |
| 50 | + |
| 51 | + case UNKNOWN: |
| 52 | + print_usage(); |
| 53 | + break; |
| 54 | + } |
| 55 | + free_command_token(&command); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * @brief 입력한 명령행을 토큰 구조체로 변환 |
| 61 | + * @param command 명령행 토큰 구조체 |
| 62 | + * @param command_buffer 명령행 문자열 |
| 63 | + */ |
| 64 | +void make_command_token(CommandToken *command, char *command_buffer) // 입력한 명령행을 토큰 구조체로 변환 |
| 65 | +{ |
| 66 | + char *tmp; |
| 67 | + |
| 68 | + command->argv = (char**)calloc(BUFFER_SIZE, sizeof(char*)); |
| 69 | + command->argc = 0; |
| 70 | + |
| 71 | + if ((tmp = strtok(command_buffer, " ")) == NULL) |
| 72 | + return; |
| 73 | +#ifdef DEBUG |
| 74 | + printf("make_command_token(): command->argv[%d] = %s\n", command->argc, tmp); |
| 75 | +#endif |
| 76 | + |
| 77 | + command->argv[command->argc] = (char *)calloc(BUFFER_SIZE, sizeof(char)); |
| 78 | + strcpy(command->argv[command->argc++], tmp); |
| 79 | + |
| 80 | + while ((tmp = strtok(NULL, " ")) != NULL) { |
| 81 | +#ifdef DEBUG |
| 82 | + printf("make_command_token(): command->argv[%d] = %s\n", command->argc, tmp); |
| 83 | +#endif |
| 84 | + command->argv[command->argc] = (char *)calloc(BUFFER_SIZE, sizeof(char)); // 명령행 인자 메모리 공간 할당 |
| 85 | + strcpy(command->argv[command->argc++], tmp); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * @brief 명령 타입 확인 및 번호 변환 |
| 91 | + * @param command 명령 문자열 |
| 92 | + * @return 명령 타입 번호 |
| 93 | + */ |
| 94 | +int get_command_type(char *command) // 명령 타입 확인 및 번호 변환 |
| 95 | +{ |
| 96 | +#ifdef DEBUG |
| 97 | + printf("get_command_type(): command = %s\n", command); |
| 98 | +#endif |
| 99 | + if (command == NULL) |
| 100 | + return false; |
| 101 | + else if (!strcmp(command, "add")) |
| 102 | + return ADD; |
| 103 | + else if (!strcmp(command, "remove")) |
| 104 | + return REMOVE; |
| 105 | + else if (!strcmp(command, "exit")) |
| 106 | + return EXIT; |
| 107 | + else |
| 108 | + return UNKNOWN; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * @brief 문자열 오른쪽 공백 제거 |
| 113 | + * @param _str 대상 문자열 |
| 114 | + * @return 공백이 제거된 문자열 |
| 115 | + */ |
| 116 | +char *rtrim(char *_str) // 문자열 오른쪽 공백 제거 |
| 117 | +{ |
| 118 | + char tmp[MAX_BUFFER_SIZE]; |
| 119 | + char *end; |
| 120 | + |
| 121 | + strcpy(tmp, _str); |
| 122 | + end = tmp + strlen(tmp) - 1; |
| 123 | + while (end != _str && isspace(*end)) |
| 124 | + --end; |
| 125 | + |
| 126 | + *(end + 1) = '0円'; |
| 127 | + _str = tmp; |
| 128 | + return _str; |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * @brief 문자열 왼쪽 공백 제거 |
| 133 | + * @param _str 대상 문자열 |
| 134 | + * @return 공백이 제거된 문자열 |
| 135 | + */ |
| 136 | +char *ltrim(char *_str) // 문자열 왼쪽 공백 제거 |
| 137 | +{ |
| 138 | + char *start = _str; |
| 139 | + |
| 140 | + while (*start != '0円' && isspace(*start)) |
| 141 | + ++start; |
| 142 | + _str = start; |
| 143 | + return _str; |
| 144 | +} |
| 145 | + |
| 146 | +void to_lower_case(char *str) // 문자열 소문자 변환 |
| 147 | +{ |
| 148 | + int i = 0; |
| 149 | + |
| 150 | + while (str[i]) { |
| 151 | + if (str[i] >= 'A' && str[i] <= 'Z'){ |
| 152 | + str[i] = str[i]+32; |
| 153 | + } |
| 154 | + i++; |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * @brief 명령행 구조체 초기화 |
| 160 | + * @param command 명령행 구조체 |
| 161 | + */ |
| 162 | +void free_command_token(CommandToken *command) // 명령행 구조체 초기화 |
| 163 | +{ |
| 164 | + int i; |
| 165 | + |
| 166 | + for (i = 0; i < command->argc; i++) |
| 167 | + free(command->argv[i]); |
| 168 | + free(command->argv); |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * @brief 사용법 출력 |
| 173 | + */ |
| 174 | +void print_usage(void) // 사용법 출력 |
| 175 | +{ |
| 176 | + printf("Usage: [COMMAND]\n"); |
| 177 | + printf("Command : \n"); |
| 178 | + printf(" ADD <PERIOD> <EXECUTE COMMAND> add commands to be excuted periodically in the reservation list\n"); |
| 179 | + printf(" REMOVE <COMMAND_NUMBER> remove the execution reservation of COMMAND_NUMBER from the reservation list\n"); |
7 | 180 | } |
0 commit comments