Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3299647

Browse files
committed
ssu_crontab prompt base complete
1 parent 905d198 commit 3299647

File tree

4 files changed

+239
-4
lines changed

4 files changed

+239
-4
lines changed

‎.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ssu_crontab
33
ssu_rsync
44

55
# Temporary files
6-
6+
ssu_crontab_file
77

88
# Prerequisites
99
*.d

‎common.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @file common.h
3+
* @brief 프로젝트에서 공통적으로 사용되는 라이브러리, 매크로 정의
4+
* @author 김병준 (kbj9704@gmail.com)
5+
*/
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <unistd.h>
9+
#include <stdbool.h>
10+
11+
#ifndef COMMON_H // Define guard
12+
#define COMMON_H
13+
14+
// 1. 시스템 라이브러리
15+
// 2. 확장 라이브러리
16+
// 3. 프로젝트 라이브러리
17+
18+
// 매크로
19+
#define BUFFER_SIZE 256
20+
#define MAX_BUFFER_SIZE 1024
21+
#endif // COMMON_H

‎ssu_crontab.c‎

Lines changed: 176 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,180 @@
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"
27

8+
#define DEBUG
9+
10+
/**
11+
* @brief ssu_crontab 메인 함수
12+
*/
313
int main(void)
414
{
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");
7180
}

‎ssu_crontab.h‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @file ssu_crontab.h
3+
* @brief ssu_crontab.c에서 사용되는 라이브러리, 매크로, 프로토타입 선언
4+
* @author 김병준 (kbj9704@gmail.com)
5+
*/
6+
#ifndef SSU_CRONTAB_H // Define Guard
7+
#define SSU_CRONTAB_H
8+
9+
#include <string.h>
10+
#include <ctype.h>
11+
#include "common.h"
12+
13+
/**
14+
* @brief 명령어 타입 번호
15+
*/
16+
#define ADD 1
17+
#define REMOVE 2
18+
#define EXIT 3
19+
#define UNKNOWN 4
20+
21+
/**
22+
* @brief 파일 이름
23+
*/
24+
#define CRONTAB_FILE "ssu_crontab_file"
25+
26+
typedef struct CommandTokenStruct // 프롬프트 명령행 토큰 구조체
27+
{
28+
int argc; // 인자 개수
29+
char **argv; // 인자 토큰
30+
} CommandToken;
31+
32+
void prompt(void); // 프롬프트 메인
33+
void make_command_token(CommandToken *command, char *command_buffer); // 입력한 명령행을 토큰 구조체로 변환
34+
int get_command_type(char *command); // 명령 타입 확인 및 번호 변환
35+
char *rtrim(char *_str); // 문자열 오른쪽 공백 제거
36+
char *ltrim(char *_str); // 문자열 왼쪽 공백 제거
37+
void to_lower_case(char *str); // 문자열 소문자 변환
38+
void free_command_token(CommandToken *command); // 토큰 구조체 메모리 해제
39+
void print_usage(void); // 사용법 출력
40+
41+
#endif // SSU_CRONTAB_H

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /