Sat, 29 Apr 2000 15:35:45 +0200
trunk: changeset 12
Changed the copyright notice in headers
0 | 1 | /* |
2 | * libtu/util.c | |
3 | * | |
9 | 4 | * Copyright (c) Tuomo Valkonen 1999-2000. |
0 | 5 | * See the included file LICENSE for details. |
6 | */ | |
7 | ||
8 | #include <stdarg.h> | |
9 | #include <stdio.h> | |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
12 | ||
13 | #ifdef CONFIG_LOCALE | |
14 | #include <libintl.h> | |
15 | #endif | |
16 | ||
5 | 17 | #include <libtu/util.h> |
18 | #include <libtu/misc.h> | |
0 | 19 | |
20 | ||
21 | static const ProgInfo *proginfo=NULL; | |
22 | static const char *progname=NULL; | |
23 | ||
24 | static void common_opts(int *argc, char *argv[]); | |
25 | ||
26 | ||
27 | void libtu_init_argv0(const char *argv0, const ProgInfo *info) | |
28 | { | |
29 | progname=argv0; | |
30 | proginfo=info; | |
31 | ||
32 | #ifdef CONFIG_LOCALE | |
33 | textdomain(simple_basename(argv0)); | |
34 | #endif | |
35 | } | |
36 | ||
37 | ||
38 | void libtu_init(int *argc, char *argv[], const ProgInfo *info) | |
39 | { | |
40 | libtu_init_argv0(argv[0], info); | |
41 | ||
42 | common_opts(argc, argv); | |
43 | } | |
44 | ||
45 | ||
46 | const char *prog_execname() | |
47 | { | |
48 | return progname; | |
49 | } | |
50 | ||
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 | } |