0
|
1 // optionの解析ルーチン作成
|
|
2 // long option, 例えば--helpなどは使えるように
|
|
3 // long optionは--から始まるものを有効
|
|
4 // strcmpは使わない
|
|
5 // c fileはincludeしない
|
|
6
|
|
7 extern int printf(char*,...);
|
|
8
|
|
9 char* outfile;
|
|
10 char* infile;
|
|
11 int help;
|
|
12
|
|
13 #define LONG_OPTION_NUM 2
|
|
14 int match[LONG_OPTION_NUM]; // matching table
|
|
15 char* optstr[LONG_OPTION_NUM]={ // option string
|
|
16 "help",
|
|
17 "file",
|
|
18 };
|
|
19 int score[LONG_OPTION_NUM]={4,4}; // length of option string
|
|
20
|
|
21 #ifdef DEBUG
|
|
22 extern void breakp(void *,void *);
|
|
23 #define BREAKP(ret,env) breakp(ret,env)
|
|
24 #else
|
|
25 #define BREAKP(ret,env)
|
|
26 #endif
|
|
27
|
|
28 // evaluates matching table
|
|
29 code
|
|
30 opt5(int j, int i, int argc, char** argv, (*exit0)(int), void* env)
|
|
31 {
|
|
32 if(j<LONG_OPTION_NUM){
|
|
33 printf("match(%d): %d\n", j, match[j]);
|
|
34 if(match[j]==score[j]){
|
|
35 printf("detected opt: ");
|
|
36 if(j==0){ // help
|
|
37 help=1;
|
|
38 printf("help\n");
|
|
39 goto exit0(0), env;
|
|
40 }else if(j==1){ // file
|
|
41 printf("file\n");
|
|
42 printf("file: %s %s\n", outfile, argv[i]);
|
|
43 if(outfile){
|
|
44 goto exit0(1), env;
|
|
45 }else{
|
|
46 outfile=argv[i];
|
|
47 i++;
|
|
48 goto opt0(i, argc, argv, exit0, env);
|
|
49 }
|
|
50 }else{
|
|
51 printf("unknown\n");
|
|
52 goto exit0(1), env;
|
|
53 }
|
|
54 }
|
|
55 j++;
|
|
56 goto opt5(j, i, argc, argv, exit0, env);
|
|
57 }else{
|
|
58 goto exit0(1), env;
|
|
59 }
|
|
60 }
|
|
61
|
|
62 // matching test
|
|
63 code
|
|
64 opt4(int k, int j, char* p, int i, int argc, char** argv, (*exit0)(int), void* env)
|
|
65 {
|
|
66 char* o;
|
|
67 if(*p!='\0'){
|
|
68 o=optstr[j];
|
|
69 if(j<LONG_OPTION_NUM){
|
|
70 if(*p==o[k]){
|
|
71 match[j]++;
|
|
72 }
|
|
73 j++;
|
|
74 }else{
|
|
75 k++;
|
|
76 p++;
|
|
77 j=0;
|
|
78 }
|
|
79 goto opt4(k, j, p, i, argc, argv, exit0, env);
|
|
80 }
|
|
81 goto opt5(0, i, argc, argv, exit0, env);
|
|
82 }
|
|
83
|
|
84 // initialize matching table
|
|
85 code
|
|
86 opt3(int j, char* p, int i, int argc, char** argv, (*exit0)(int), void* env)
|
|
87 {
|
|
88 if(j<LONG_OPTION_NUM){
|
|
89 match[j]=0;
|
|
90 j++;
|
|
91 goto opt3(j, p, i, argc, argv, exit0, env);
|
|
92 }else{
|
|
93 goto opt4(0, 0, p, i, argc, argv, exit0, env);
|
|
94 }
|
|
95 }
|
|
96
|
|
97 // evaluates one hyphen options such like -f or -h.
|
|
98 code
|
|
99 opt2(char* p, int i, int argc, char** argv, (*exit0)(int), void* env)
|
|
100 {
|
|
101 if(*p=='-'){
|
|
102 p++;
|
|
103 goto opt3(0, p, i, argc, argv, exit0, env);
|
|
104 } else if(*(p+1)=='\0'){
|
|
105 if(*p=='h'){
|
|
106 help=1;
|
|
107 goto exit0(0), env;
|
|
108 }else if(*p=='f'){
|
|
109 if(argv[i]){
|
|
110 if(outfile){
|
|
111 goto exit0(1), env;
|
|
112 }else{
|
|
113 outfile=argv[i];
|
|
114 i++;
|
|
115 goto opt0(i, argc, argv, exit0, env);
|
|
116 }
|
|
117 }else{
|
|
118 goto exit0(1), env;
|
|
119 }
|
|
120 }
|
|
121 }
|
|
122 goto exit0(1), env;
|
|
123 }
|
|
124
|
|
125 // sets 'infile'
|
|
126 code
|
|
127 opt1(char* p, int i, int argc, char** argv, (*exit0)(int), void* env)
|
|
128 {
|
|
129 if(infile){
|
|
130 printf("infile exists: infile=%s %s\n", infile, p);
|
|
131 goto exit0(1), env;
|
|
132 }else{
|
|
133 infile=p;
|
|
134 goto opt0(i, argc, argv, exit0, env);
|
|
135 }
|
|
136 }
|
|
137
|
|
138 code
|
|
139 opt0(int i, int argc, char** argv, (*exit0)(int), void* env)
|
|
140 {
|
|
141 char* p;
|
|
142 BREAKP(exit0,env);
|
|
143 if(i<argc){
|
|
144 p=argv[i];
|
|
145 i++;
|
|
146 if(*p=='-'){
|
|
147 p++;
|
|
148 goto opt2(p, i, argc, argv, exit0, env);
|
|
149 }else{
|
|
150 goto opt1(p, i, argc, argv, exit0, env);
|
|
151 }
|
|
152 goto opt0(i, argc, argv, exit0, env);
|
|
153 }else{
|
|
154 goto exit0(0), env;
|
|
155 }
|
|
156 }
|
|
157
|
|
158 int
|
|
159 option(int argc, char** argv)
|
|
160 {
|
|
161 BREAKP(return, environment);
|
|
162 goto opt0(1, argc, argv, return, environment);
|
|
163 return 0;
|
|
164 }
|
|
165
|
|
166 // prints usage
|
|
167 void
|
|
168 usage()
|
|
169 {
|
|
170 printf("Usage:porogram <option> <infile>\n");
|
|
171 printf("-h: help\n");
|
|
172 printf("-f: outfile\n");
|
|
173 }
|
|
174
|
|
175 #ifndef DEBUG
|
|
176 int
|
|
177 main(int argc,char** argv)
|
|
178 {
|
|
179 if(option(argc,argv)){
|
|
180 printf("Invalid option\n");
|
|
181 usage();
|
|
182 return 1;
|
|
183 }
|
|
184 if(help){
|
|
185 usage();
|
|
186 return 0;
|
|
187 }
|
|
188 if(infile){
|
|
189 printf("infile: %s\n", infile);
|
|
190 } else{
|
|
191 printf("Invalid usage\n");
|
|
192 usage();
|
|
193 return 1;
|
|
194 }
|
|
195 if(outfile){
|
|
196 printf("outfile: %s\n", outfile);
|
|
197 }
|
|
198 return 0;
|
|
199 }
|
|
200 #endif // DEBUG
|