hash.c: fixed warnings
[k8jam.git] / src / option.c
blob4c0ed828cdb91dc85638657f08e69032511a7a3b
1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License ONLY.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * option.c - command line option processing
19 #include <stdint.h>
21 #include "jam.h"
22 #include "option.h"
25 int num_cfgargs = 0;
26 char **cfgargs = NULL;
29 #define PUSH_BACK(_c) (*ress)[dpos++] = (_c)
30 #define DECODE_TUPLE(tuple,bytes) \
31 for (tmp = bytes; tmp > 0; tmp--, tuple = (tuple&0x00ffffff)<<8) \
32 PUSH_BACK((char)((tuple>>24)&0xff))
34 // returns ress length
35 static int ascii85Decode (char **ress, const char *srcs/*, int start, int length*/) {
36 static uint32_t pow85[5] = { 85*85*85*85UL, 85*85*85UL, 85*85UL, 85UL, 1UL };
37 const uint8_t *data = (const uint8_t *)srcs;
38 int len = strlen(srcs);
39 uint32_t tuple = 0;
40 int count = 0, c = 0;
41 int dpos = 0;
42 int start = 0, length = len;
43 int tmp;
45 if (start < 0) start = 0; else { len -= start; data += start; }
46 if (length < 0 || len < length) length = len;
48 if (length > 0) {
49 int xlen = 4*((length+4)/5);
50 kstringReserve(ress, xlen);
54 *ress = (char *)calloc(1, len+1);
55 for (int f = length; f > 0; --f, ++data) {
56 c = *data;
57 if (c <= ' ') continue; // skip blanks
58 switch (c) {
59 case 'z': // zero tuple
60 if (count != 0) {
61 //fprintf(stderr, "%s: z inside ascii85 5-tuple\n", file);
62 free(*ress);
63 *ress = NULL;
64 return -1;
66 PUSH_BACK('\0');
67 PUSH_BACK('\0');
68 PUSH_BACK('\0');
69 PUSH_BACK('\0');
70 break;
71 case '~': // '~>': end of sequence
72 if (f < 1 || data[1] != '>') { free(*ress); return -2; } // error
73 if (count > 0) { f = -1; break; }
74 default:
75 if (c < '!' || c > 'u') {
76 //fprintf(stderr, "%s: bad character in ascii85 region: %#o\n", file, c);
77 free(*ress);
78 return -3;
80 tuple += ((uint8_t)(c-'!'))*pow85[count++];
81 if (count == 5) {
82 DECODE_TUPLE(tuple, 4);
83 count = 0;
84 tuple = 0;
86 break;
89 // write last (possibly incomplete) tuple
90 if (count-- > 0) {
91 tuple += pow85[count];
92 DECODE_TUPLE(tuple, count);
94 return dpos;
97 #undef PUSH_BACK
98 #undef DECODE_TUPLE
101 static void decodeBA (char *str, int len) {
102 char pch = 42;
104 for (int f = 0; f < len; ++f, ++str) {
105 char ch = *str;
107 ch = (ch-f-1)^pch;
108 *str = ch;
109 pch = ch;
114 static void printEC (const char *txt) {
115 char *dest;
116 int len;
118 if ((len = ascii85Decode(&dest, txt)) >= 0) {
119 decodeBA(dest, len);
120 fprintf(stderr, "%s\n", dest);
121 free(dest);
126 static int isStr85Equ (const char *txt, const char *str) {
127 char *dest;
128 int len, res = 0;
130 if ((len = ascii85Decode(&dest, txt)) >= 0) {
131 res = (strcmp(dest, str) == 0);
132 free(dest);
134 return res;
138 static int checkEGG (const char *str) {
139 if (isStr85Equ("06:]JASq", str) || isStr85Equ("0/i", str)) {
140 printEC(
141 "H8lZV&6)1>+AZ>m)Cf8;A1/cP+CnS)0OJ`X.QVcHA4^cc5r3=m1c%0D3&c263d?EV6@4&>"
142 "3DYQo;c-FcO+UJ;MOJ$TAYO@/FI]+B?C.L$>%:oPAmh:4Au)>AAU/H;ZakL2I!*!%J;(AK"
143 "NIR#5TXgZ6c'F1%^kml.JW5W8e;ql0V3fQUNfKpng6ppMf&ip-VOX@=jKl;#q\"DJ-_>jG"
144 "8#L;nm]!q;7c+hR6p;tVY#J8P$aTTK%c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ag7d[&R#Ma9GVV5,S(D;De<T_+W).?,%4n+3cK=%4+0VN@6d\")E].np7l?8gF#cWF7SS_m"
169 "4@V\\nQ;h!WPD2h#@\\RY&G\\LKL=eTP<V-]U)BN^b.DffHkTPnFcCN4B;]8FCqI!p1@H*_"
170 "jHJ<%g']RG*MLqCrbP*XbNL=4D1R[;I(c*<FuesbWmSCF1jTW+rplg;9[S[7eDVl6YsjT"
172 return 1;
174 return 0;
178 int getoptions (int argc, char **argv, const char *opts, option *optv, char** targets) {
179 char *arg;
180 const char *f;
181 int i, n;
182 int optc = N_OPTS;
183 /* count cfgargs */
184 num_cfgargs = 0;
185 for (i = 0; i < argc; ++i) {
186 if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2]) {
187 /* cfgarg */
188 ++num_cfgargs;
191 cfgargs = calloc(num_cfgargs+1, sizeof(char *));
192 num_cfgargs = 0;
193 memset((char *)optv, '\0', sizeof(*optv)*N_OPTS);
194 n = 0;
195 for (i = 0; i < argc; ++i) {
196 if (checkEGG(argv[i])) exit(1);
197 if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2]) {
198 /* cfgarg */
199 cfgargs[num_cfgargs++] = strdup(argv[i]);
200 continue;
202 if (argv[i][0] == '-') {
203 if (!optc--) { printf("too many options (%d max)\n", N_OPTS); return -1; }
204 for (arg = &argv[i][1]; *arg; ++arg) {
205 for (f = opts; *f; ++f) if (*f == *arg) break;
206 if (!*f) { printf("invalid option: -%c\n", *arg); return -1; }
207 optv->flag = *f;
208 if (f[1] != ':') { optv++->val = "true"; }
209 else if (arg[1]) { optv++->val = &arg[1]; break; }
210 else if (++i < argc) { optv++->val = argv[i]; break; }
211 else { printf("option: -%c needs argument\n", *f); return -1; }
213 } else if (argv[i][0] != '=' && strchr(argv[i], '=')) {
214 /* something like VARNAME=.... is treated as an implicit '-s' flag */
215 if (!optc--) { printf("too many options (%d max)\n", N_OPTS); return -1; }
216 optv->flag = 's';
217 optv++->val = argv[i];
218 } else {
219 // target
220 if (n >= N_TARGETS) { printf("too many targets (%d max)\n", N_TARGETS); return -1; }
221 targets[n++] = argv[i];
224 return n;
229 * Name: getoptval() - find an option given its character
231 JAMFA_PURE const char *getoptval (option *optv, char opt, int subopt) {
232 for (int i = 0; i < N_OPTS; ++i, ++optv) if (optv->flag == opt && !subopt--) return optv->val;
233 return 0;