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