File: MwUtils.c

package info (click to toggle)
mowitz 0.2.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,168 kB
  • ctags: 4,214
  • sloc: ansic: 30,869; sh: 9,051; makefile: 169
file content (531 lines) | stat: -rw-r--r-- 13,646 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
Copyright (C) 1996-2001  Ulric Eriksson <ulric@siag.nu>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the Licence, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "MwUtils.h"

/*
A set of functions to allocate and deallocate memory with a little
record keeping and error checking.
*/

/* ---
Allocate memory and check if anything was actually allocated.
If not, print an error message or do something else
*/

static void std_alloc_fail(void)
{
        fprintf(stderr, "Memory allocation failure\n");
        exit(EXIT_FAILURE);
}

static void (*alloc_fail)(void) = std_alloc_fail;
/* Also does a few checks that the library functions should already do */

static int paranoia = 1;

typedef struct malloc_node {
        void *p;                        /* allocated memory */
        struct malloc_node *next;       /* next in chain */
} malloc_node;

static malloc_node *nodes = NULL;

void MwMallocStats(void)
{
        malloc_node *m = nodes;
        while (m) {
                fprintf(stderr, "%p ", m->p);
                m = m->next;
        }
        fprintf(stderr, "\n");
}

static void remove_node(void *p)
{
        malloc_node *m, *n;

        if (!p) return;         /* deallocating NULL is fine */

        n = nodes;
        if (!n) {
                if (paranoia == 1) return;
                fprintf(stderr, "Deallocating %p when nothing allocated\n", p);
                if (paranoia == 2) return;
                alloc_fail();
        }

        if (n->p == p) {
                nodes = n->next;
                free(n);
                return;
        }

        for (m = n->next; m; m = m->next) {
                if (m->p == p) {
                        n->next = m->next;
                        free(m);
                        return;
                }
                n = m;
        }

        if (paranoia == 1) return;
        fprintf(stderr, "Deallocating %p which was not allocated\n", p);
        if (paranoia == 2) return;
        alloc_fail();
}

static void insert_node(void *p)
{
        malloc_node *n;

        if (!p) return;         /* we don't need to remember NULL */

        n = malloc(sizeof(malloc_node));
        if (n == NULL) alloc_fail();
        n->p = p;
        n->next = nodes;
        nodes = n;
}

/* ---
Set up a new failure handler and paranoia level.

        0 = don't check at all
        1 = try to fix errors without telling
        2 = warn about errors and try to fix them
        3 = warn about errors and fail
*/

void MwMallocInit(void (*new_fail)(void), int level)
{
        if (new_fail) alloc_fail = new_fail;
        else alloc_fail = std_alloc_fail;
        paranoia = level;
}

/* ---
Check for memory leaks.
*/

void MwMallocExit(void)
{
        malloc_node *n;

        if (paranoia == 0) return;

        for (n = nodes; n; n = n->next) {
                if (paranoia >= 2) {
                        fprintf(stderr, "Didn't deallocate %p\n", n->p);
			return;
		}
                if (paranoia == 3) alloc_fail();
        }
}

/* ---
Allocate memory with a bit of record keeping and error checking.
*/

void *MwMalloc(size_t size)
{
        void *p;

        p = malloc(size);
        if (p == NULL) alloc_fail();
        if (paranoia) insert_node(p);

        /* Courtesy of Youki Kadobayashi <youki-k@is.aist-nara.ac.jp> */
        memset(p, 1, size);

        return p;
}

/* ---
Reallocate memory.
*/

void *MwRealloc(void *ptr, size_t size)
{
        void *p;

        if (paranoia) remove_node(ptr);
        p = realloc(ptr, size);
        if (p == NULL) alloc_fail();
        if (paranoia) insert_node(p);
        return p;
}

/* ---
Allocate memory and initialize it to all zero.
*/

void *MwCalloc(size_t nmemb, size_t size)
{
        void *p;

        p = calloc(nmemb, size);
        if (p == NULL) alloc_fail();
        if (paranoia) insert_node(p);
        return p;
}

/* ---
Allocate a duplicate of a string.
*/

char *MwStrdup(const char *s)
{
        char *p;

        if (s) p = malloc(strlen(s)+1);
        else p = NULL;
        if (p == NULL) alloc_fail();
        else strcpy(p, s);
        if (paranoia) insert_node(p);
        return p;
}

/* ---
Free a block of memory.
*/

void MwFree(void *ptr)
{
        if (paranoia) remove_node(ptr);
        free(ptr);
}


/* ---
Strip trailing newline, if there is one
*/

void MwChomp(char *p)
{
	if ((p = strchr(p, '\n'))) *p = '\0';
}


/* translation stuff from Siag Office */

static struct dictstruct {
        char *key, *xl;
} *dict = NULL;

static long nw = 0;

/* ---
Replace \n with real newlines and \t with real tabs.
Other escaped characters are replaced with themselves.
*/

static void unescape(char *p)
{
        char *q = p;
        int c, state = 0;
        while ((c = *p++)) {
                switch (state) {
                case 0:
                        if (c == '\\') state = 1;
                        else *q++ = c;
                        break;
                case 1:
                        if (c == 'n') *q++ = '\n';
                        else if (c == 'r') *q++ = '\r';
                        else if (c == 'b') *q++ = '\b';
                        else if (c == 't') *q++ = '\t';
                        else *q++ = c;
                        state = 0;
                        break;
                }
        }
        *q = '\0';
}

static int compar(const void *p, const void *q)
{
        struct dictstruct *p1 = (struct dictstruct *)p;
        struct dictstruct *q1 = (struct dictstruct *)q;
        return strcmp(p1->key, q1->key);
}

/* ---
Load the message string dictionary from $(SIAGHOME)/common/dictionary.$(LANG)
*/

void MwLoadDictionary(char *fn)
{
        FILE *fp;
        char b[10000], *p, *q;

        /* convert everything after language code to upper case */
        p = strchr(fn, '_');
        if (p) {
                q = p;
                while (*q) {
                        *q = toupper(*q);
                        q++;
                }
        }

        if (p) {
                q = strchr(p, '.');
        } else {
                q = NULL;
        }

        fp = fopen(fn, "r");
        
        if (!fp && q) {
                /* try abridged file name */
                *q = '\0';
                fp = fopen(fn, "r");
        }
        if (!fp && p) {
                /* try abridged file name */
                *p = '\0';
                fp = fopen(fn, "r");
        }
        if (!fp) {
#ifdef DEBUG
                fprintf(stderr, "Can't open dictionary\n");
#endif
                return;
        }
        while (fgets(b, sizeof b, fp)) {
                if (b[0] == '#') continue;
                MwChomp(b);
                p = strchr(b, '\t');
                if (!p) continue;
                *p++ = 0;
                p += strspn(p, "\t");
                dict = MwRealloc(dict, (nw+1)*(sizeof dict[0]));
                unescape(b);
                unescape(p);
                dict[nw].key = MwStrdup(b);
                dict[nw++].xl = MwStrdup(p);
        }
        qsort(dict, nw, sizeof dict[0], compar);
}

/* ---
Translate a message string. If no match is found, the key is returned.
By using English keys, an application can keep working even when the
dictionary is incomplete.
990113: tree search through sorted list
*/

char *MwTranslate(char *key)
{
        long lower = 0, upper = nw-1;
        long i;
        int d;

        while (lower <= upper) {
                i = (lower+upper)/2;
                d = strcmp(key, dict[i].key);
                if (d == 0) return dict[i].xl;
                else if (d > 0) lower = i+1;
                else upper = i-1;
        }
        return key;
}

/* translation stuff done */

/* copy from q to p, quoting any character in b */                              
void MwQuotecpy(char *p, char *q, char *b)                                        
{                                                                               
        int c;                                                                  
                                                                                
        while ((c = *q++)) {                                                    
                if (strchr(b, c)) *p++ = '\\';                                  
                *p++ = c;                                                       
        }                                                                       
        *p = '\0';                                                              
}

pid_t MwSpawn(const char *command)
{
        char *argv[20], *p, cmd[1024];
        int argc = 0;
        pid_t pid;

        strncpy(cmd, command, sizeof cmd);

        for (p = strtok(cmd, " \t\r\n");
             p && argc < 20;
             p = strtok(NULL, " \t\r\n")) {
                argv[argc++] = p;
        }
        argv[argc] = NULL;
        pid = fork();
        if (!pid) {
                /* this is the child */

                /*close(2);*/   /* don't display the child's messages */

                execvp(argv[0], argv);
                exit(0);
        }
        return pid;
}

void MwHelp(char *p)
{
	char b[1024];

	sprintf(b, "netscape /usr/local/doc/Mowitz/%s", p);
	MwSpawn(b);
}

static struct {
        char *name;
        int value;
} cchar[] = {
        {"quot", '"'}, {"amp", '&'}, {"lt", '<'}, {"gt", '>'},
        {"nbsp", 160}, {"iexcl", 161}, {"cent", 162}, {"pound", 163},
        {"curren", 164}, {"yen", 165}, {"brvbar", 166}, {"sect", 167},
        {"uml", 168}, {"copy", 169}, {"ordf", 170}, {"laquo", 171},
        {"not", 172}, {"shy", 173}, {"reg", 174}, {"macr", 175},
        {"deg", 176}, {"plusmn", 177}, {"sup2", 178}, {"sup3", 179},
        {"acute", 180}, {"micro", 181}, {"para", 182}, {"middot", 183},
        {"cedil", 184}, {"sup1", 185}, {"ordm", 186}, {"raquo", 187},
        {"frac14", 188}, {"frac12", 189}, {"frac34", 190}, {"iquest", 191},
        {"Agrave", 192}, {"Aacute", 193}, {"Acirc", 194}, {"Atilde", 195},
        {"Auml", 196}, {"Aring", 197}, {"AElig", 198}, {"Ccedil", 199},
        {"Egrave", 200}, {"Eacute", 201}, {"Ecirc", 202}, {"Euml", 203},
        {"Igrave", 204}, {"Iacute", 205}, {"Icirc", 206}, {"Euml", 207},
        {"ETH", 208}, {"Ntilde", 209}, {"Ograve", 210}, {"Oacute", 211},
        {"Ocirc", 212}, {"Otilde", 213}, {"Ouml", 214}, {"times", 215},
        {"Oslash", 216}, {"Ugrave", 217}, {"Uacute", 218}, {"Ucirc", 219},
        {"Uuml", 220}, {"Yacute", 221}, {"THORN", 222}, {"szlig", 223},
        {"agrave", 224}, {"aacute", 225}, {"acirc", 226}, {"atilde", 227},
        {"auml", 228}, {"aring", 229}, {"aelig", 230}, {"ccedil", 231},
        {"egrave", 232}, {"eacute", 233}, {"ecirc", 234}, {"euml", 235},
        {"igrave", 236}, {"iacute", 237}, {"icirc", 238}, {"iuml", 239},
        {"eth", 240}, {"ntilde", 241}, {"ograve", 242}, {"oacute", 243},
        {"ocirc", 244}, {"otilde", 245}, {"ouml", 246}, {"divide", 247},
        {"slash", 248}, {"ugrave", 249}, {"uacute", 250}, {"ucirc", 251},
        {"uuml", 252}, {"yacute", 253}, {"thorn", 254}, {"yuml", 255},
        {NULL, 0}
};

/* convert Auml to  and #33 to ! */
int MwFromCchar(char *from)
{
        int i;

        if (from[0] == '#') {
                i = atoi(from+1);
                if (i >= ' ' && i <= 255) return i;
                return -1;
        }
        for (i = 0; cchar[i].name; i++) {
                if (!strcmp(cchar[i].name, from)) return cchar[i].value;
        }
        return -1;
}

/* convert  to &Auml; and so on */
void MwToCchar(char *to, int from)
{
        int i;

        for (i = 0; cchar[i].value; i++) {
                if (from == cchar[i].value) {
                        sprintf(to, "&%s;", cchar[i].name);
                        return;
                }
        }
        to[0] = from;
        to[1] = '\0';
}

/* ---
strcmp which is immune to libc silliness
*/
int MwStrcmp(const char *p, const char *q)
{
        int c;

        while (!(c = *p-*q) && *p) {
                p++;
                q++;
        }
        return c;
}

/* ---
Case insensitive compare
*/

int MwStrcasecmp(const char *p, const char *q)
{
        int c;

        while (!(c = toupper(*p)-toupper(*q)) && *p) {
                p++;
                q++;
        }
        return c;
}

/* ---
Case insensitive compare, length limited
*/

int MwStrncasecmp(const char *p, const char *q, size_t n)
{
        size_t i = 0;
        int c = 0;

        while ((i < n) && !(c = toupper(*p)-toupper(*q)) && *p) {
                p++;
                q++;
                i++;
        }
        return c;
}

int MwSnprintf(char *str, size_t n, const char *format, ...)
{
        int m;
        va_list ap;
        va_start(ap, format);
#ifdef HAVE_VSNPRINTF
        m = vsnprintf(str, n, format, ap);
#else
        m = vsprintf(str, format, ap);
#endif
        va_end(ap);
        return m;
}