Initial import (again)
[srvx.git] / src / tools.c
1 /* tools.c - miscellaneous utility functions
2  * Copyright 2000-2004 srvx Development Team
3  *
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; either version 2 of the License, or
7  * (at your option) any later version.  Important limitations are
8  * listed in the COPYING file that accompanies this software.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, email srvx-maintainers@srvx.net.
17  */
18
19 #include "log.h"
20 #include "nickserv.h"
21 #include "recdb.h"
22
23 #ifdef HAVE_NETDB_H
24 #include <netdb.h>
25 #endif
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
28 #endif
29 #ifdef HAVE_ARPA_INET_H
30 #include <arpa/inet.h>
31 #endif
32
33 #define NUMNICKLOG 6
34 #define NUMNICKBASE (1 << NUMNICKLOG)
35 #define NUMNICKMASK (NUMNICKBASE - 1)
36
37 /* Yes, P10's encoding here is almost-but-not-quite MIME Base64.  Yay
38  * for gratuitous incompatibilities. */
39 static const char convert2y[256] = {
40   'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
41   'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
42   'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
43   'w','x','y','z','0','1','2','3','4','5','6','7','8','9','[',']'
44 };
45
46 static const unsigned char convert2n[256] = {
47    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50   52,53,54,55,56,57,58,59,60,61, 0, 0, 0, 0, 0, 0, 
51    0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
52   15,16,17,18,19,20,21,22,23,24,25,62, 0,63, 0, 0,
53    0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
54   41,42,43,44,45,46,47,48,49,50,51, 0, 0, 0, 0, 0
55 };
56
57 unsigned long int
58 base64toint(const char* s, int count)
59 {
60     unsigned int i = 0;
61     while (*s && count) {
62         i = (i << NUMNICKLOG) + convert2n[(unsigned char)*s++];
63         count--;
64     }
65     return i;
66 }
67
68 const char* inttobase64(char* buf, unsigned int v, unsigned int count)
69 {
70   buf[count] = '\0';
71   while (count > 0) {
72       buf[--count] = convert2y[(unsigned char)(v & NUMNICKMASK)];
73       v >>= NUMNICKLOG;
74   }
75   return buf;
76 }
77
78 static char irc_tolower[256];
79 #undef tolower
80 #define tolower(X) irc_tolower[(unsigned char)(X)]
81
82 int
83 irccasecmp(const char *stra, const char *strb) {
84     while (*stra && (tolower(*stra) == tolower(*strb)))
85         stra++, strb++;
86     return tolower(*stra) - tolower(*strb);
87 }
88
89 int
90 ircncasecmp(const char *stra, const char *strb, unsigned int len) {
91     len--;
92     while (*stra && (tolower(*stra) == tolower(*strb)) && len)
93         stra++, strb++, len--;
94     return tolower(*stra) - tolower(*strb);
95 }
96
97 const char *
98 irccasestr(const char *haystack, const char *needle) {
99     unsigned int hay_len = strlen(haystack), needle_len = strlen(needle), pos;
100     if (hay_len < needle_len)
101         return NULL;
102     for (pos=0; pos<hay_len+1-needle_len; ++pos) {
103         if ((tolower(haystack[pos]) == tolower(*needle))
104             && !ircncasecmp(haystack+pos, needle, needle_len))
105             return haystack+pos;
106     }
107     return NULL;
108 }
109
110 int
111 split_line(char *line, int irc_colon, int argv_size, char *argv[])
112 {
113     int argc = 0;
114     int n;
115     while (*line && (argc < argv_size)) {
116         while (*line == ' ') *line++ = 0;
117         if (*line == ':' && irc_colon && argc > 0) {
118             /* the rest is a single parameter */
119             argv[argc++] = line + 1;
120             break;
121         }
122         if (!*line)
123             break;
124         argv[argc++] = line;
125         if (argc >= argv_size)
126             break;
127         while (*line != ' ' && *line) line++;
128     }
129 #ifdef NDEBUG
130     n = 0;
131 #else
132     for (n=argc; n<argv_size; n++) {
133         argv[n] = (char*)0xFEEDBEEF;
134     }
135 #endif
136     return argc;
137 }
138
139 /* This is ircu's mmatch() function, from match.c. */
140 int mmatch(const char *old_mask, const char *new_mask)
141 {
142   register const char *m = old_mask;
143   register const char *n = new_mask;
144   const char *ma = m;
145   const char *na = n;
146   int wild = 0;
147   int mq = 0, nq = 0;
148
149   while (1)
150   {
151     if (*m == '*')
152     {
153       while (*m == '*')
154         m++;
155       wild = 1;
156       ma = m;
157       na = n;
158     }
159
160     if (!*m)
161     {
162       if (!*n)
163         return 0;
164       for (m--; (m > old_mask) && (*m == '?'); m--)
165         ;
166       if ((*m == '*') && (m > old_mask) && (m[-1] != '\\'))
167         return 0;
168       if (!wild)
169         return 1;
170       m = ma;
171
172       /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
173       if ((*na == '\\') && ((na[1] == '*') || (na[1] == '?')))
174         ++na;
175
176       n = ++na;
177     }
178     else if (!*n)
179     {
180       while (*m == '*')
181         m++;
182       return (*m != 0);
183     }
184     if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?')))
185     {
186       m++;
187       mq = 1;
188     }
189     else
190       mq = 0;
191
192     /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
193     if ((*n == '\\') && ((n[1] == '*') || (n[1] == '?')))
194     {
195       n++;
196       nq = 1;
197     }
198     else
199       nq = 0;
200
201 /*
202  * This `if' has been changed compared to match() to do the following:
203  * Match when:
204  *   old (m)         new (n)         boolean expression
205  *    *               any             (*m == '*' && !mq) ||
206  *    ?               any except '*'  (*m == '?' && !mq && (*n != '*' || nq)) ||
207  * any except * or ?  same as m       (!((*m == '*' || *m == '?') && !mq) &&
208  *                                      toLower(*m) == toLower(*n) &&
209  *                                        !((mq && !nq) || (!mq && nq)))
210  *
211  * Here `any' also includes \* and \? !
212  *
213  * After reworking the boolean expressions, we get:
214  * (Optimized to use boolean shortcircuits, with most frequently occuring
215  *  cases upfront (which took 2 hours!)).
216  */
217     if ((*m == '*' && !mq) ||
218         ((!mq || nq) && tolower(*m) == tolower(*n)) ||
219         (*m == '?' && !mq && (*n != '*' || nq)))
220     {
221       if (*m)
222         m++;
223       if (*n)
224         n++;
225     }
226     else
227     {
228       if (!wild)
229         return 1;
230       m = ma;
231
232       /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
233       if ((*na == '\\') && ((na[1] == '*') || (na[1] == '?')))
234         ++na;
235
236       n = ++na;
237     }
238   }
239 }
240
241 int
242 match_ircglob(const char *text, const char *glob)
243 {
244     unsigned int star_p, q_cnt;
245     while (1) {
246         switch (*glob) {
247         case 0:
248             return !*text;
249         case '\\':
250             glob++;
251             /* intentionally not tolower(...) so people can force
252              * capitalization, or we can overload \ in the future */
253             if (*text++ != *glob++) return 0;
254             break;
255         case '*':
256         case '?':
257             star_p = q_cnt = 0;
258             do {
259                 if (*glob == '*') star_p = 1;
260                 else if (*glob == '?') q_cnt++;
261                 else break;
262                 glob++;
263             } while (1);
264             while (q_cnt) { if (!*text++) return 0; q_cnt--; }
265             if (star_p) {
266                 /* if this is the last glob character, it will match any text */
267                 if (!*glob) return 1;
268                 /* Thanks to the loop above, we know that the next
269                  * character is a normal character.  So just look for
270                  * the right character.
271                  */
272                 for (; *text; text++) {
273                     if ((tolower(*text) == tolower(*glob))
274                         && match_ircglob(text+1, glob+1)) {
275                         return 1;
276                     }
277                 }
278                 return 0;
279             }
280             /* if !star_p, fall through to normal character case,
281              * first checking to see if ?s carried us to the end */
282             if (!*glob && !*text) return 1;
283         default:
284             if (!*text) return 0;
285             while (*text && *glob && *glob != '*' && *glob != '?' && *glob != '\\') {
286                 if (tolower(*text++) != tolower(*glob++)) return 0;
287             }
288         }
289     }
290 }
291
292 extern const char *hidden_host_suffix;
293
294 int
295 user_matches_glob(struct userNode *user, const char *orig_glob, int include_nick)
296 {
297     char *glob, *marker;
298
299     /* Make a writable copy of the glob */
300     glob = alloca(strlen(orig_glob)+1);
301     strcpy(glob, orig_glob);
302     /* Check the nick, if it's present */
303     if (include_nick) {
304         if (!(marker = strchr(glob, '!'))) {
305             log_module(MAIN_LOG, LOG_ERROR, "user_matches_glob(\"%s\", \"%s\", %d) called, and glob doesn't include a '!'", user->nick, orig_glob, include_nick);
306             return 0;
307         }
308         *marker = 0;
309         if (!match_ircglob(user->nick, glob)) return 0;
310         glob = marker + 1;
311     }
312     /* Check the ident */
313     if (!(marker = strchr(glob, '@'))) {
314         log_module(MAIN_LOG, LOG_ERROR, "user_matches_glob(\"%s\", \"%s\", %d) called, and glob doesn't include an '@'", user->nick, orig_glob, include_nick);
315         return 0;
316     }
317     *marker = 0;
318     if (!match_ircglob(user->ident, glob))
319         return 0;
320     glob = marker + 1;
321     /* Now check the host part */
322     if (isdigit(*glob) && !glob[strspn(glob, "0123456789./*?")]) {
323         /* Looks like an IP-based mask */
324         return match_ircglob(inet_ntoa(user->ip), glob);
325     } else {
326         /* The host part of the mask isn't IP-based */
327         if (hidden_host_suffix && user->handle_info) {
328             char hidden_host[HOSTLEN+1];
329             snprintf(hidden_host, sizeof(hidden_host), "%s.%s", user->handle_info->handle, hidden_host_suffix);
330             if (match_ircglob(hidden_host, glob))
331                 return 1;
332         }
333         return match_ircglob(user->hostname, glob);
334     }
335 }
336
337 int
338 is_ircmask(const char *text)
339 {
340     while (*text && (isalnum((char)*text) || strchr("-_[]|\\`^{}?*", *text))) text++;
341     if (*text++ != '!') return 0;
342     while (*text && *text != '@' && !isspace((char)*text)) text++;
343     if (*text++ != '@') return 0;
344     while (*text && !isspace((char)*text)) text++;
345     return !*text;
346 }
347
348 int
349 is_gline(const char *text)
350 {
351     if (*text == '@') return 0;
352     text += strcspn(text, "@!% \t\r\n");
353     if (*text++ != '@') return 0;
354     if (!*text) return 0;
355     while (*text && (isalnum((char)*text) || strchr(".-?*", *text))) text++;
356     return !*text;
357 }
358
359 int
360 split_ircmask(char *text, char **nick, char **ident, char **host)
361 {
362     char *start;
363
364     start = text;
365     while (isalnum((char)*text) || strchr("=[]\\`^{}?*", *text)) text++;
366     if (*text != '!' || ((text - start) > NICKLEN)) return 0;
367     *text = 0;
368     if (nick) *nick = start;
369
370     start = ++text;
371     while (*text && *text != '@' && !isspace((char)*text)) text++;
372     if (*text != '@' || ((text - start) > USERLEN)) return 0;
373     *text = 0;
374     if (ident) *ident = start;
375     
376     start = ++text;
377     while (*text && (isalnum((char)*text) || strchr(".-?*", *text))) text++;
378     if (host) *host = start;
379     return !*text && ((text - start) <= HOSTLEN) && nick && ident && host;
380 }
381
382 char *
383 sanitize_ircmask(char *input)
384 {
385     unsigned int length, flag;
386     char *mask, *start, *output;
387
388     /* Sanitize everything in place; input *must* be a valid
389        hostmask. */
390     output = input;
391     flag = 0;
392
393     /* The nick is truncated at the end. */
394     length = 0;
395     mask = input;
396     while(*input++ != '!')
397     {
398         length++;
399     }
400     if(length > NICKLEN)
401     {
402         mask += NICKLEN;
403         *mask++ = '!';
404
405         /* This flag is used to indicate following parts should
406            be shifted. */
407         flag = 1;
408     }
409     else
410     {
411         mask = input;
412     }
413
414     /* The ident and host must be truncated at the beginning and
415        replaced with a '*' to be compatible with ircu. */
416     length = 0;
417     start = input;
418     while(*input++ != '@')
419     {
420         length++;
421     }
422     if(length > USERLEN || flag)
423     {
424         if(length > USERLEN)
425         {
426             start = input - USERLEN;
427             *mask++ = '*';
428         }
429         while(*start != '@')
430         {
431             *mask++ = *start++;
432         }
433         *mask++ = '@';
434
435         flag = 1;
436     }
437     else
438     {
439         mask = input;
440     }
441
442     length = 0;
443     start = input;
444     while(*input++)
445     {
446         length++;
447     }
448     if(length > HOSTLEN || flag)
449     {
450         if(length > HOSTLEN)
451         {
452             start = input - HOSTLEN;
453             *mask++ = '*';
454         }
455         while(*start)
456         {
457             *mask++ = *start++;
458         }
459         *mask = '\0';
460     }
461
462     return output;
463 }
464
465 static long
466 TypeLength(char type)
467 {
468     switch (type) {
469     case 'y': return 365*24*60*60;
470     case 'M': return 31*24*60*60;
471     case 'w': return 7*24*60*60;
472     case 'd': return 24*60*60;
473     case 'h': return 60*60;
474     case 'm': return 60;
475     case 's': return 1;
476     default: return 0;
477     }
478 }
479
480 unsigned long
481 ParseInterval(const char *interval)
482 {
483     unsigned long seconds = 0;
484     int partial = 0;
485     char c;
486
487     /* process the string, resetting the count if we find a unit character */
488     while ((c = *interval++)) {
489         if (isdigit((int)c)) {
490             partial = partial*10 + c - '0';
491         } else {
492             seconds += TypeLength(c) * partial;
493             partial = 0;
494         }
495     }
496     /* assume the last chunk is seconds (the normal case) */
497     return seconds + partial;
498 }
499
500 static long
501 GetSizeMultiplier(char type)
502 {
503     switch (type) {
504     case 'G': case 'g': return 1024*1024*1024;
505     case 'M': case 'm': return 1024*1024;
506     case 'K': case 'k': return 1024;
507     case 'B': case 'b': return 1;
508     default: return 0;
509     }
510 }
511
512 unsigned long
513 ParseVolume(const char *volume)
514 {
515     unsigned long accum = 0, partial = 0;
516     char c;
517     while ((c = *volume++)) {
518         if (isdigit((int)c)) {
519             partial = partial*10 + c - '0';
520         } else {
521             accum += GetSizeMultiplier(c) * partial;
522             partial = 0;
523         }
524     }
525     return accum + partial;
526 }
527
528 int
529 parse_ipmask(const char *str, struct in_addr *addr, unsigned long *mask)
530 {
531     int accum, pos;
532     unsigned long t_a, t_m;
533
534     t_a = t_m = pos = 0;
535     if (addr) addr->s_addr = htonl(t_a);
536     if (mask) *mask = t_m;
537     while (*str) {
538         if (!isdigit(*str)) return 0;
539         accum = 0;
540         do {
541             accum = (accum * 10) + *str++ - '0';
542         } while (isdigit(*str));
543         if (accum > 255) return 0;
544         t_a = (t_a << 8) | accum;
545         t_m = (t_m << 8) | 255;
546         pos += 8;
547         if (*str == '.') {
548             str++;
549             while (*str == '*') {
550                 str++;
551                 if (*str == '.') {
552                     t_a <<= 8;
553                     t_m <<= 8;
554                     pos += 8;
555                     str++;
556                 } else if (*str == 0) {
557                     t_a <<= 32 - pos;
558                     t_m <<= 32 - pos;
559                     pos = 32;
560                     goto out;
561                 } else {
562                     return 0;
563                 }
564             }
565         } else if (*str == '/') {
566             int start = pos;
567             accum = 0;
568             do {
569                 accum = (accum * 10) + *str++ - '0';
570             } while (isdigit(*str));
571             while (pos < start+accum && pos < 32) {
572                 t_a = (t_a << 1) | 0;
573                 t_m = (t_m << 1) | 1;
574                 pos++;
575             }
576             if (pos != start+accum) return 0;
577         } else if (*str == 0) {
578             break;
579         } else {
580             return 0;
581         }
582     }
583 out:
584     if (pos != 32) return 0;
585     if (addr) addr->s_addr = htonl(t_a);
586     if (mask) *mask = t_m;
587     return 1;
588 }
589
590 char *
591 unsplit_string(char *set[], unsigned int max, char *dest)
592 {
593     static char unsplit_buffer[MAXLEN*2];
594     unsigned int ii, jj, pos;
595
596     if (!dest) dest = unsplit_buffer;
597     for (ii=pos=0; ii<max; ii++) {
598         for (jj=0; set[ii][jj]; jj++) {
599             dest[pos++] = set[ii][jj];
600         }
601         dest[pos++] = ' ';
602     }
603     dest[--pos] = 0;
604     return dest;
605 }
606
607 char *
608 intervalString2(char *output, time_t interval, int brief)
609 {
610     static const struct {
611         const char *name;
612         long length;
613     } unit[] = {
614         { "year", 365 * 24 * 60 * 60 },
615         { "week",   7 * 24 * 60 * 60 },
616         { "day",        24 * 60 * 60 },
617         { "hour",            60 * 60 },
618         { "minute",               60 },
619         { "second",                1 }
620     };
621     unsigned int type, words, pos, count;
622
623     if(!interval)
624     {
625         strcpy(output, brief ? "0s" : "0 seconds");
626         return output;
627     }
628
629     for (type = 0, words = pos = 0;
630          interval && (words < 2) && (type < ArrayLength(unit));
631          type++) {
632         if (interval < unit[type].length)
633             continue;
634         count = interval / unit[type].length;
635         interval = interval % unit[type].length;
636
637         if (brief)
638             pos += sprintf(output + pos, "%d%c", count, unit[type].name[0]);
639         else if (words == 1)
640             pos += sprintf(output + pos, " and %d %s", count, unit[type].name);
641         else
642             pos += sprintf(output + pos, "%d %s", count, unit[type].name);
643         if (count != 1)
644             output[pos++] = 's';
645         words++;
646     }
647
648     output[pos] = 0;
649     return output;
650 }
651
652 int
653 getipbyname(const char *name, unsigned long *ip)
654 {
655     struct hostent *he = gethostbyname(name);
656     if (he) {
657         if (he->h_addrtype != AF_INET)
658             return 0;
659         memcpy(ip, he->h_addr_list[0], sizeof(*ip));
660         return 1;
661     } else {
662         return 0;
663     }
664 }
665
666 DEFINE_LIST(string_buffer, char)
667
668 void
669 string_buffer_append_substring(struct string_buffer *buf, const char *tail, unsigned int len)
670 {
671     while (buf->used + len >= buf->size) {
672         if (!buf->size)
673             buf->size = 16;
674         else
675             buf->size <<= 1;
676         buf->list = realloc(buf->list, buf->size*sizeof(buf->list[0]));
677     }
678     memcpy(buf->list + buf->used, tail, len+1);
679     buf->used += len;
680 }
681
682 void
683 string_buffer_append_string(struct string_buffer *buf, const char *tail)
684 {
685     string_buffer_append_substring(buf, tail, strlen(tail));
686 }
687
688 void
689 string_buffer_append_vprintf(struct string_buffer *buf, const char *fmt, va_list args)
690 {
691     va_list working;
692     unsigned int len;
693     int ret;
694
695     VA_COPY(working, args);
696     len = strlen(fmt);
697     if (!buf->list || ((buf->used + buf->size) < len)) {
698         buf->size = buf->used + len;
699         buf->list = realloc(buf->list, buf->size);
700     }
701     ret = vsnprintf(buf->list + buf->used, buf->size - buf->used, fmt, working);
702     if (ret <= 0) {
703         /* pre-C99 behavior; double buffer size until it is big enough */
704         va_end(working);
705         VA_COPY(working, args);
706         while ((ret = vsnprintf(buf->list + buf->used, buf->size, fmt, working)) == -1) {
707             buf->size += len;
708             buf->list = realloc(buf->list, buf->size);
709             va_end(working);
710             VA_COPY(working, args);
711         }
712         buf->used += ret;
713     } else if (buf->used + ret < buf->size) {
714         /* no need to increase allocation size */
715         buf->used += ret;
716     } else {
717         /* now we know exactly how much space we need */
718         if (buf->size <= buf->used + ret) {
719             buf->size = buf->used + ret + 1;
720             buf->list = realloc(buf->list, buf->size);
721         }
722         va_end(working);
723         VA_COPY(working, args);
724         buf->used += vsnprintf(buf->list + buf->used, buf->size, fmt, working);
725     }
726     va_end(working);
727     va_end(args);
728 }
729
730 void string_buffer_append_printf(struct string_buffer *buf, const char *fmt, ...)
731 {
732     va_list args;
733     va_start(args, fmt);
734     string_buffer_append_vprintf(buf, fmt, args);
735 }
736
737 void
738 string_buffer_replace(struct string_buffer *buf, unsigned int from, unsigned int len, const char *repl)
739 {
740     unsigned int repl_len = strlen(repl);
741     if (from > buf->used) return;
742     if (len + from > buf->used) len = buf->used - from;
743     buf->used = buf->used + repl_len - len;
744     if (buf->size <= buf->used) {
745         while (buf->used >= buf->size) {
746             buf->size <<= 1;
747         }
748         buf->list = realloc(buf->list, buf->size*sizeof(buf->list[0]));
749     }
750     memmove(buf->list+from+repl_len, buf->list+from+len, strlen(buf->list+from+len));
751     strcpy(buf->list+from, repl);
752 }
753
754 struct string_list str_tab;
755
756 const char *
757 strtab(unsigned int ii) {
758     if (ii > 65536) return NULL;
759     if (ii > str_tab.size) {
760         unsigned int old_size = str_tab.size;
761         while (ii >= str_tab.size) str_tab.size <<= 1;
762         str_tab.list = realloc(str_tab.list, str_tab.size*sizeof(str_tab.list[0]));
763         memset(str_tab.list+old_size, 0, (str_tab.size-old_size)*sizeof(str_tab.list[0]));
764     }
765     if (!str_tab.list[ii]) {
766         str_tab.list[ii] = malloc(12);
767         sprintf(str_tab.list[ii], "%u", ii);
768     }
769     return str_tab.list[ii];
770 }
771
772 void
773 tools_init(void)
774 {
775     unsigned int upr, lwr;
776     for (lwr=0; lwr<256; ++lwr) tolower(lwr) = lwr;
777     for (upr='A', lwr='a'; lwr <= 'z'; ++upr, ++lwr) tolower(upr) = lwr;
778 #ifdef WITH_PROTOCOL_P10
779     for (upr='[', lwr='{'; lwr <= '~'; ++upr, ++lwr) tolower(upr) = lwr;
780     for (upr=0xc0, lwr=0xe0; lwr <= 0xf6; ++upr, ++lwr) tolower(upr) = lwr;
781     for (upr=0xd8, lwr=0xf8; lwr <= 0xfe; ++upr, ++lwr) tolower(upr) = lwr;
782 #endif
783     str_tab.size = 1001;
784     str_tab.list = calloc(str_tab.size, sizeof(str_tab.list[0]));
785 }
786
787 void
788 tools_cleanup(void)
789 {
790     unsigned int ii;
791     for (ii=0; ii<str_tab.size; ++ii) {
792         if (str_tab.list[ii]) free(str_tab.list[ii]);
793     }
794     free(str_tab.list);
795 }