16 |
16 |
17 #include <libtu/util.h> |
17 #include <libtu/util.h> |
18 #include <libtu/misc.h> |
18 #include <libtu/misc.h> |
19 |
19 |
20 |
20 |
21 static const ProgInfo *proginfo=NULL; |
|
22 static const char *progname=NULL; |
21 static const char *progname=NULL; |
23 |
22 |
24 static void common_opts(int *argc, char *argv[]); |
|
25 |
23 |
26 |
24 void libtu_init(const char *argv0) |
27 void libtu_init_argv0(const char *argv0, const ProgInfo *info) |
|
28 { |
25 { |
29 progname=argv0; |
26 progname=argv0; |
30 proginfo=info; |
|
31 |
27 |
32 #ifdef CONFIG_LOCALE |
28 #ifdef CONFIG_LOCALE |
33 textdomain(simple_basename(argv0)); |
29 textdomain(simple_basename(argv0)); |
34 #endif |
30 #endif |
35 } |
31 } |
36 |
32 |
37 |
33 |
38 void libtu_init(int *argc, char *argv[], const ProgInfo *info) |
34 void libtu_init_copt(int argc, char *const argv[], |
|
35 const OptParserCommonInfo *cinfo) |
39 { |
36 { |
40 libtu_init_argv0(argv[0], info); |
37 int opt; |
41 |
38 |
42 common_opts(argc, argv); |
39 libtu_init(argv[0]); |
|
40 |
|
41 optparser_init(argc, argv, OPTP_DEFAULT, NULL, cinfo); |
|
42 |
|
43 while((opt=optparser_get_opt())){ |
|
44 switch(opt){ |
|
45 default: |
|
46 optparser_print_error(); |
|
47 exit(EXIT_FAILURE); |
|
48 } |
|
49 } |
43 } |
50 } |
44 |
51 |
45 |
52 |
46 const char *prog_execname() |
53 const char *prog_execname() |
47 { |
54 { |
48 return progname; |
55 return progname; |
49 } |
56 } |
50 |
57 |
51 |
|
52 const ProgInfo *prog_info() |
|
53 { |
|
54 return proginfo; |
|
55 } |
|
56 |
|
57 |
|
58 const char *prog_name() |
|
59 { |
|
60 return proginfo ? proginfo->name : NULL; |
|
61 } |
|
62 |
|
63 |
|
64 const char *prog_version() |
|
65 { |
|
66 return proginfo ? proginfo->version : NULL; |
|
67 } |
|
68 |
|
69 |
|
70 const char *prog_authors() |
|
71 { |
|
72 return proginfo ? proginfo->authors : NULL; |
|
73 } |
|
74 |
|
75 |
|
76 const char *prog_license() |
|
77 { |
|
78 return proginfo ? proginfo->license : NULL; |
|
79 } |
|
80 |
|
81 |
|
82 const char *prog_usage() |
|
83 { |
|
84 return proginfo ? proginfo->usage : NULL; |
|
85 } |
|
86 |
|
87 |
|
88 /* */ |
|
89 |
|
90 |
|
91 static char *usages[][2]={ |
|
92 {"--help", "Show this help\n"}, |
|
93 {"--version", "Show program version\n"}, |
|
94 {"--authors", "Show program authors\n"}, |
|
95 {"--license", "Show program license\n"}, |
|
96 {"--proginfo", "Show program info\n"}, |
|
97 {NULL,} |
|
98 }; |
|
99 |
|
100 |
|
101 static void common_usage(size_t start, size_t len) |
|
102 { |
|
103 size_t l; |
|
104 int i; |
|
105 |
|
106 for(i=0; usages[i][0]!=NULL; i++){ |
|
107 |
|
108 for(l=0; l<start; l++){ |
|
109 putc(' ', stdout); |
|
110 }; |
|
111 |
|
112 l=strlen(usages[i][0]); |
|
113 writef(stdout, usages[i][0], l); |
|
114 |
|
115 do{ |
|
116 putc(' ', stdout); |
|
117 }while(++l<len-start); |
|
118 |
|
119 writef(stdout, usages[i][1], strlen(usages[i][1])); |
|
120 } |
|
121 } |
|
122 |
|
123 |
|
124 static void show_usage(const char *p) |
|
125 { |
|
126 const char *tmp; |
|
127 size_t len; |
|
128 size_t start; |
|
129 |
|
130 do{ |
|
131 tmp=strchr(p, '\n'); |
|
132 |
|
133 if(tmp==NULL){ |
|
134 len=strlen(p); |
|
135 if(len==0) |
|
136 break; |
|
137 }else{ |
|
138 len=tmp-p; |
|
139 } |
|
140 |
|
141 start=strspn(p, " "); |
|
142 |
|
143 if(p[start]=='$' && p[start+1]=='u'){ |
|
144 tmp=prog_execname(); |
|
145 if(start!=0) |
|
146 writef(stdout, p, start); |
|
147 printf(TR("Usage: %s"), tmp); |
|
148 writef(stdout, p+start+2, len-start-2); |
|
149 putc('\n', stdout); |
|
150 }else if(p[start]=='$' && p[start+1]=='c'){ |
|
151 common_usage(start, len); |
|
152 }else{ |
|
153 writef(stdout, p, len); |
|
154 putc('\n', stdout); |
|
155 } |
|
156 |
|
157 p+=len+1; |
|
158 }while(*(p-1)!='\0'); |
|
159 |
|
160 } |
|
161 |
|
162 |
|
163 static void common_opts(int *argcp, char *argv[]) |
|
164 { |
|
165 int argc=*argcp; |
|
166 const char *p, *p2; |
|
167 |
|
168 argc--; |
|
169 argv++; |
|
170 |
|
171 for(; argc>0; argc--, argv++){ |
|
172 if(strcmp(*argv, "--help")==0){ |
|
173 p=prog_usage(); |
|
174 if(p==NULL){ |
|
175 printf(TR("Usage: %s [options]\n" |
|
176 "Where options are:\n\n"), prog_execname()); |
|
177 common_usage(5, 20); |
|
178 printf("\n"); |
|
179 }else{ |
|
180 show_usage(p); |
|
181 } |
|
182 }else if(strcmp(*argv, "--version")==0){ |
|
183 p=prog_version(); |
|
184 if(p==NULL) |
|
185 printf(TR("No version available\n")); |
|
186 else |
|
187 printf("%s\n", p); |
|
188 }else if(strcmp(*argv, "--authors")==0){ |
|
189 p=prog_authors(); |
|
190 if(p==NULL) |
|
191 printf(TR("No author(s) info available\n")); |
|
192 else |
|
193 printf("%s\n", p); |
|
194 }else if(strcmp(*argv, "--license")==0){ |
|
195 p=prog_license(); |
|
196 if(p==NULL) |
|
197 printf(TR("No license available\n")); |
|
198 else |
|
199 printf("%s", TR(p)); |
|
200 }else if(strcmp(*argv, "--proginfo")==0){ |
|
201 p2=prog_version(); |
|
202 p=prog_name(); |
|
203 |
|
204 if(p==NULL){ |
|
205 p=prog_execname(); |
|
206 |
|
207 if(p==NULL){ |
|
208 printf(TR("No name available\n")); |
|
209 break; |
|
210 } |
|
211 } |
|
212 |
|
213 if(p2) |
|
214 printf("%s v%s\n", p, p2); |
|
215 else |
|
216 printf("%s\n", p); |
|
217 }else if(strcmp(*argv, "--")){ |
|
218 break; |
|
219 }else{ |
|
220 continue; |
|
221 } |
|
222 |
|
223 exit(EXIT_SUCCESS); |
|
224 } |
|
225 } |
|