built-in jambase is packed now
[k8jam.git] / src / option.c
blobdda5569adbcdd90cc4386cbbe4c9fe8d17a58214
1 /*
2 * Copyright 1993, 1995 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6 /*
7 * option.c - command line option processing
9 * {o >o
10 * \<>) "Process command line options as defined in <option.h>.
11 * Return the number of argv[] elements used up by options,
12 * or -1 if an invalid option flag was given or an argument
13 * was supplied for an option that does not require one."
15 * 11/04/02 (seiwald) - const-ing for string literals
17 #include <stdint.h>
19 #include "jam.h"
20 #include "option.h"
23 #define PUSH_BACK(_c) (*ress)[dpos++] = (_c)
24 #define DECODE_TUPLE(tuple,bytes) \
25 for (tmp = bytes; tmp > 0; tmp--, tuple = (tuple & 0x00ffffff)<<8) \
26 PUSH_BACK((char)((tuple >> 24)&0xff))
28 // returns ress length
29 static int ascii85Decode (char **ress, const char *srcs/*, int start, int length*/) {
30 static uint32_t pow85[5] = { 85*85*85*85UL, 85*85*85UL, 85*85UL, 85UL, 1UL };
31 const uint8_t *data = (const uint8_t *)srcs;
32 int len = strlen(srcs);
33 uint32_t tuple = 0;
34 int count = 0, c = 0;
35 int dpos = 0;
36 int start = 0, length = len;
37 int tmp;
39 if (start < 0) start = 0; else { len -= start; data += start; }
40 if (length < 0 || len < length) length = len;
42 if (length > 0) {
43 int xlen = 4*((length+4)/5);
44 kstringReserve(ress, xlen);
48 *ress = (char *)calloc(1, len+1);
49 for (int f = length; f > 0; --f, ++data) {
50 c = *data;
51 if (c <= ' ') continue; // skip blanks
52 switch (c) {
53 case 'z': // zero tuple
54 if (count != 0) {
55 //fprintf(stderr, "%s: z inside ascii85 5-tuple\n", file);
56 free(*ress);
57 *ress = NULL;
58 return -1;
60 PUSH_BACK('\0');
61 PUSH_BACK('\0');
62 PUSH_BACK('\0');
63 PUSH_BACK('\0');
64 break;
65 case '~': // '~>': end of sequence
66 if (f < 1 || data[1] != '>') { free(*ress); return -2; } // error
67 if (count > 0) { f = -1; break; }
68 default:
69 if (c < '!' || c > 'u') {
70 //fprintf(stderr, "%s: bad character in ascii85 region: %#o\n", file, c);
71 free(*ress);
72 return -3;
74 tuple += ((uint8_t)(c-'!'))*pow85[count++];
75 if (count == 5) {
76 DECODE_TUPLE(tuple, 4);
77 count = 0;
78 tuple = 0;
80 break;
83 // write last (possibly incomplete) tuple
84 if (count-- > 0) {
85 tuple += pow85[count];
86 DECODE_TUPLE(tuple, count);
88 return dpos;
91 #undef PUSH_BACK
92 #undef DECODE_TUPLE
95 static void decodeBA (char *str, int len) {
96 char pch = 42;
98 for (int f = 0; f < len; ++f, ++str) {
99 char ch = *str;
101 ch = (ch-f-1)^pch;
102 *str = ch;
103 pch = ch;
108 static void printEC (const char *txt) {
109 char *dest;
110 int len;
112 if ((len = ascii85Decode(&dest, txt)) >= 0) {
113 decodeBA(dest, len);
114 fprintf(stderr, "%s\n", dest);
115 free(dest);
120 static int isStr85Equ (const char *txt, const char *str) {
121 char *dest;
122 int len, res = 0;
124 if ((len = ascii85Decode(&dest, txt)) >= 0) {
125 res = (strcmp(dest, str) == 0);
126 free(dest);
128 return res;
132 static int checkEGG (const char *str) {
133 if (isStr85Equ("06:]JASq", str) || isStr85Equ("0/i", str)) {
134 printEC(
135 "H8lZV&6)1>+AZ>m)Cf8;A1/cP+CnS)0OJ`X.QVcHA4^cc5r3=m1c%0D3&c263d?EV6@4&>"
136 "3DYQo;c-FcO+UJ;MOJ$TAYO@/FI]+B?C.L$>%:oPAmh:4Au)>AAU/H;ZakL2I!*!%J;(AK"
137 "NIR#5TXgZ6c'F1%^kml.JW5W8e;ql0V3fQUNfKpng6ppMf&ip-VOX@=jKl;#q\"DJ-_>jG"
138 "8#L;nm]!q;7c+hR6p;tVY#J8P$aTTK%c-OT?)<00,+q*8f&ff9a/+sbU,:`<H*[fk0o]7k"
139 "^l6nRkngc6Tl2Ngs!!P2I%KHG=7n*an'bsgn>!*8s7TLTC+^\\\"W+<=9^%Ol$1A1eR*Be"
140 "gqjEag:M0OnrC4FB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CqI!p1@H*_"
164 "jHJ<%g']RG*MLqCrbP*XbNL=4D1R[;I(c*<FuesbWmSCF1jTW+rplg;9[S[7eDVl6YsjT"
166 return 1;
168 return 0;
172 int getoptions (int argc, char **argv, const char *opts, option *optv, char** targets) {
173 char *arg;
174 const char *f;
175 int i, n;
176 int optc = N_OPTS;
178 memset((char *)optv, '\0', sizeof(*optv)*N_OPTS);
179 n = 0;
180 for (i = 0; i < argc; ++i) {
181 if (checkEGG(argv[i])) exit(1);
182 if (argv[i][0] == '-') {
183 if (!optc--) { printf("too many options (%d max)\n", N_OPTS); return -1; }
184 for (arg = &argv[i][1]; *arg; ++arg) {
185 for (f = opts; *f; ++f) if (*f == *arg) break;
186 if (!*f) { printf("invalid option: -%c\n", *arg); return -1; }
187 optv->flag = *f;
188 if (f[1] != ':') { optv++->val = "true"; }
189 else if (arg[1]) { optv++->val = &arg[1]; break; }
190 else if (++i < argc) { optv++->val = argv[i]; break; }
191 else { printf("option: -%c needs argument\n", *f); return -1; }
193 } else if (argv[i][0] != '=' && strchr(argv[i],'=')) {
194 /* something like VARNAME=.... is treated as an implicit '-s' flag */
195 if (!optc--) { printf("too many options (%d max)\n", N_OPTS); return -1; }
196 optv->flag = 's';
197 optv++->val = argv[i];
198 } else {
199 // target
200 if (n >= N_TARGETS) { printf("too many targets (%d max)\n", N_TARGETS); return -1; }
201 targets[n++] = argv[i];
204 return n;
209 * Name: getoptval() - find an option given its character
211 const char *getoptval (option *optv, char opt, int subopt) {
212 int i;
214 for (i = 0; i < N_OPTS; ++i, ++optv) if (optv->flag == opt && !subopt--) return optv->val;
215 return 0;