annotate arg.c @ 2:bf65d90b8dc8

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