Print this page
make: translate using gettext, rather than the unmaintainable catgets
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/make/lib/mksh/misc.cc
+++ new/usr/src/cmd/make/lib/mksh/misc.cc
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26
27 27 /*
28 28 * misc.cc
29 29 *
30 30 * This file contains various unclassified routines. Some main groups:
31 31 * getname
32 32 * Memory allocation
33 33 * String handling
34 34 * Property handling
35 35 * Error message handling
↓ open down ↓ |
35 lines elided |
↑ open up ↑ |
36 36 * Make internal state dumping
37 37 * main routine support
38 38 */
39 39
40 40 /*
41 41 * Included files
42 42 */
43 43 #include <bsd/bsd.h> /* bsd_signal() */
44 44 #include <mksh/i18n.h> /* get_char_semantics_value() */
45 45 #include <mksh/misc.h>
46 -#include <mksdmsi18n/mksdmsi18n.h>
47 46 #include <stdarg.h> /* va_list, va_start(), va_end() */
48 47 #include <stdlib.h> /* mbstowcs() */
49 48 #include <sys/signal.h> /* SIG_DFL */
50 49 #include <sys/wait.h> /* wait() */
51 50
52 51 #include <string.h> /* strerror() */
52 +#include <libintl.h>
53 53
54 54
55 55 /*
56 56 * Defined macros
57 57 */
58 58
59 59 /*
60 60 * typedefs & structs
61 61 */
62 62
63 63 /*
64 64 * Static variables
65 65 */
66 66 extern "C" {
67 67 void (*sigivalue)(int) = SIG_DFL;
68 68 void (*sigqvalue)(int) = SIG_DFL;
69 69 void (*sigtvalue)(int) = SIG_DFL;
70 70 void (*sighvalue)(int) = SIG_DFL;
71 71 }
72 72
73 73 long getname_bytes_count = 0;
74 74 long getname_names_count = 0;
75 75 long getname_struct_count = 0;
76 76
77 77 long freename_bytes_count = 0;
78 78 long freename_names_count = 0;
79 79 long freename_struct_count = 0;
80 80
81 81 long expandstring_count = 0;
82 82 long getwstring_count = 0;
83 83
84 84 /*
85 85 * File table of contents
86 86 */
87 87 static void expand_string(register String string, register int length);
88 88
89 89 #define FATAL_ERROR_MSG_SIZE 200
90 90
91 91 /*
92 92 * getmem(size)
93 93 *
94 94 * malloc() version that checks the returned value.
95 95 *
96 96 * Return value:
97 97 * The memory chunk we allocated
98 98 *
99 99 * Parameters:
↓ open down ↓ |
37 lines elided |
↑ open up ↑ |
100 100 * size The size of the chunk we need
101 101 *
102 102 * Global variables used:
103 103 */
104 104 char *
105 105 getmem(register int size)
106 106 {
107 107 register char *result = (char *) malloc((unsigned) size);
108 108 if (result == NULL) {
109 109 char buf[FATAL_ERROR_MSG_SIZE];
110 - sprintf(buf, NOCATGETS("*** Error: malloc(%d) failed: %s\n"), size, strerror(errno));
111 - strcat(buf, catgets(libmksdmsi18n_catd, 1, 126, "mksh: Fatal error: Out of memory\n"));
110 + sprintf(buf, "*** Error: malloc(%d) failed: %s\n", size, strerror(errno));
111 + strcat(buf, gettext("mksh: Fatal error: Out of memory\n"));
112 112 fputs(buf, stderr);
113 113 exit_status = 1;
114 114 exit(1);
115 115 }
116 116 return result;
117 117 }
118 118
119 119 /*
120 120 * retmem(p)
121 121 *
122 122 * Cover funtion for free() to make it possible to insert advises.
123 123 *
124 124 * Parameters:
125 125 * p The memory block to free
126 126 *
127 127 * Global variables used:
128 128 */
129 129 void
130 130 retmem(wchar_t *p)
131 131 {
132 132 (void) free((char *) p);
133 133 }
134 134
135 135 void
136 136 retmem_mb(caddr_t p)
137 137 {
138 138 (void) free(p);
139 139 }
140 140
141 141 /*
142 142 * getname_fn(name, len, dont_enter)
143 143 *
144 144 * Hash a name string to the corresponding nameblock.
145 145 *
146 146 * Return value:
147 147 * The Name block for the string
148 148 *
149 149 * Parameters:
150 150 * name The string we want to internalize
151 151 * len The length of that string
152 152 * dont_enter Don't enter the name if it does not exist
153 153 *
154 154 * Global variables used:
155 155 * funny The vector of semantic tags for characters
156 156 * hashtab The hashtable used for the nametable
157 157 */
158 158 Name
159 159 getname_fn(wchar_t *name, register int len, register Boolean dont_enter, register Boolean * foundp)
160 160 {
161 161 register int length;
162 162 register wchar_t *cap = name;
163 163 register Name np;
164 164 static Name_rec empty_Name;
165 165 char *tmp_mbs_buffer = NULL;
166 166 char *mbs_name = mbs_buffer;
167 167
168 168 /*
169 169 * First figure out how long the string is.
170 170 * If the len argument is -1 we count the chars here.
171 171 */
172 172 if (len == FIND_LENGTH) {
173 173 length = wslen(name);
174 174 } else {
175 175 length = len;
176 176 }
177 177
178 178 Wstring ws;
179 179 ws.init(name, length);
180 180 if (length >= MAXPATHLEN) {
181 181 mbs_name = tmp_mbs_buffer = getmem((length * MB_LEN_MAX) + 1);
182 182 }
183 183 (void) wcstombs(mbs_name, ws.get_string(), (length * MB_LEN_MAX) + 1);
184 184
185 185 /* Look for the string */
186 186 if (dont_enter || (foundp != 0)) {
187 187 np = hashtab.lookup(mbs_name);
188 188 if (foundp != 0) {
189 189 *foundp = (np != 0) ? true : false;
190 190 }
191 191 if ((np != 0) || dont_enter) {
192 192 if(tmp_mbs_buffer != NULL) {
193 193 retmem_mb(tmp_mbs_buffer);
194 194 }
195 195 return np;
196 196 } else {
197 197 np = ALLOC(Name);
198 198 }
199 199 } else {
200 200 Boolean found;
201 201 np = hashtab.insert(mbs_name, found);
202 202 if (found) {
203 203 if(tmp_mbs_buffer != NULL) {
204 204 retmem_mb(tmp_mbs_buffer);
205 205 }
206 206 return np;
207 207 }
208 208 }
209 209 getname_struct_count += sizeof(struct _Name);
210 210 *np = empty_Name;
211 211
212 212 np->string_mb = strdup(mbs_name);
213 213 if(tmp_mbs_buffer != NULL) {
214 214 retmem_mb(tmp_mbs_buffer);
215 215 mbs_name = tmp_mbs_buffer = NULL;
216 216 }
217 217 getname_bytes_count += strlen(np->string_mb) + 1;
218 218 /* Fill in the new Name */
219 219 np->stat.time = file_no_time;
220 220 np->hash.length = length;
221 221 /* Scan the namestring to classify it */
222 222 for (cap = name, len = 0; --length >= 0;) {
223 223 len |= get_char_semantics_value(*cap++);
224 224 }
225 225 np->dollar = BOOLEAN((len & (int) dollar_sem) != 0);
226 226 np->meta = BOOLEAN((len & (int) meta_sem) != 0);
227 227 np->percent = BOOLEAN((len & (int) percent_sem) != 0);
228 228 np->wildcard = BOOLEAN((len & (int) wildcard_sem) != 0);
229 229 np->colon = BOOLEAN((len & (int) colon_sem) != 0);
230 230 np->parenleft = BOOLEAN((len & (int) parenleft_sem) != 0);
231 231 getname_names_count++;
232 232 return np;
233 233 }
234 234
235 235 void
236 236 store_name(Name name)
237 237 {
238 238 hashtab.insert(name);
239 239 }
240 240
241 241 void
242 242 free_name(Name name)
243 243 {
244 244 freename_names_count++;
245 245 freename_struct_count += sizeof(struct _Name);
246 246 freename_bytes_count += strlen(name->string_mb) + 1;
247 247 retmem_mb(name->string_mb);
248 248 for (Property next, p = name->prop; p != NULL; p = next) {
249 249 next = p->next;
250 250 free(p);
251 251 }
252 252 free(name);
253 253 }
254 254
255 255 /*
256 256 * enable_interrupt(handler)
257 257 *
258 258 * This routine sets a new interrupt handler for the signals make
259 259 * wants to deal with.
260 260 *
261 261 * Parameters:
262 262 * handler The function installed as interrupt handler
263 263 *
264 264 * Static variables used:
265 265 * sigivalue The original signal handler
266 266 * sigqvalue The original signal handler
267 267 * sigtvalue The original signal handler
268 268 * sighvalue The original signal handler
269 269 */
270 270 void
271 271 enable_interrupt(register void (*handler) (int))
272 272 {
273 273 if (sigivalue != SIG_IGN) {
274 274 (void) bsd_signal(SIGINT, (SIG_PF) handler);
275 275 }
276 276 if (sigqvalue != SIG_IGN) {
277 277 (void) bsd_signal(SIGQUIT, (SIG_PF) handler);
278 278 }
279 279 if (sigtvalue != SIG_IGN) {
280 280 (void) bsd_signal(SIGTERM, (SIG_PF) handler);
281 281 }
282 282 if (sighvalue != SIG_IGN) {
283 283 (void) bsd_signal(SIGHUP, (SIG_PF) handler);
284 284 }
285 285 }
286 286
287 287 /*
288 288 * setup_char_semantics()
289 289 *
290 290 * Load the vector char_semantics[] with lexical markers
291 291 *
292 292 * Parameters:
293 293 *
294 294 * Global variables used:
295 295 * char_semantics The vector of character semantics that we set
296 296 */
297 297 void
298 298 setup_char_semantics(void)
299 299 {
300 300 const char *s;
301 301 wchar_t wc_buffer[1];
302 302 int entry;
303 303
304 304 if (svr4) {
305 305 s = "@-";
306 306 } else {
307 307 s = "=@-?!+";
308 308 }
309 309 for (s; MBTOWC(wc_buffer, s); s++) {
310 310 entry = get_char_semantics_entry(*wc_buffer);
311 311 char_semantics[entry] |= (int) command_prefix_sem;
312 312 }
313 313 char_semantics[dollar_char_entry] |= (int) dollar_sem;
314 314 for (s = "#|=^();&<>*?[]:$`'\"\\\n"; MBTOWC(wc_buffer, s); s++) {
315 315 entry = get_char_semantics_entry(*wc_buffer);
316 316 char_semantics[entry] |= (int) meta_sem;
317 317 }
318 318 char_semantics[percent_char_entry] |= (int) percent_sem;
319 319 for (s = "@*<%?^"; MBTOWC(wc_buffer, s); s++) {
320 320 entry = get_char_semantics_entry(*wc_buffer);
321 321 char_semantics[entry] |= (int) special_macro_sem;
322 322 }
323 323 for (s = "?[*"; MBTOWC(wc_buffer, s); s++) {
324 324 entry = get_char_semantics_entry(*wc_buffer);
325 325 char_semantics[entry] |= (int) wildcard_sem;
326 326 }
327 327 char_semantics[colon_char_entry] |= (int) colon_sem;
328 328 char_semantics[parenleft_char_entry] |= (int) parenleft_sem;
329 329 }
330 330
331 331 /*
332 332 * errmsg(errnum)
333 333 *
334 334 * Return the error message for a system call error
335 335 *
336 336 * Return value:
337 337 * An error message string
338 338 *
339 339 * Parameters:
340 340 * errnum The number of the error we want to describe
341 341 *
342 342 * Global variables used:
343 343 * sys_errlist A vector of error messages
344 344 * sys_nerr The size of sys_errlist
↓ open down ↓ |
223 lines elided |
↑ open up ↑ |
345 345 */
346 346 char *
347 347 errmsg(int errnum)
348 348 {
349 349
350 350 extern int sys_nerr;
351 351 char *errbuf;
352 352
353 353 if ((errnum < 0) || (errnum > sys_nerr)) {
354 354 errbuf = getmem(6+1+11+1);
355 - (void) sprintf(errbuf, catgets(libmksdmsi18n_catd, 1, 127, "Error %d"), errnum);
355 + (void) sprintf(errbuf, gettext("Error %d"), errnum);
356 356 return errbuf;
357 357 } else {
358 358 return strerror(errnum);
359 359
360 360 }
361 361 }
362 362
363 363 static char static_buf[MAXPATHLEN*3];
364 364
365 365 /*
366 366 * fatal_mksh(format, args...)
367 367 *
368 368 * Print a message and die
369 369 *
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
370 370 * Parameters:
371 371 * format printf type format string
372 372 * args Arguments to match the format
373 373 */
374 374 /*VARARGS*/
375 375 void
376 376 fatal_mksh(const char *message, ...)
377 377 {
378 378 va_list args;
379 379 char *buf = static_buf;
380 - char *mksh_fat_err = catgets(libmksdmsi18n_catd, 1, 128, "mksh: Fatal error: ");
381 - char *cur_wrk_dir = catgets(libmksdmsi18n_catd, 1, 129, "Current working directory: ");
380 + char *mksh_fat_err = gettext("mksh: Fatal error: ");
381 + char *cur_wrk_dir = gettext("Current working directory: ");
382 382 int mksh_fat_err_len = strlen(mksh_fat_err);
383 383
384 384 va_start(args, message);
385 385 (void) fflush(stdout);
386 386 (void) strcpy(buf, mksh_fat_err);
387 387 size_t buf_len = vsnprintf(static_buf + mksh_fat_err_len,
388 388 sizeof(static_buf) - mksh_fat_err_len,
389 389 message, args)
390 390 + mksh_fat_err_len
391 391 + strlen(cur_wrk_dir)
392 392 + strlen(get_current_path_mksh())
393 393 + 3; // "\n\n"
394 394 va_end(args);
395 395 if (buf_len >= sizeof(static_buf)) {
396 396 buf = getmem(buf_len);
397 397 (void) strcpy(buf, mksh_fat_err);
398 398 va_start(args, message);
399 399 (void) vsprintf(buf + mksh_fat_err_len, message, args);
400 400 va_end(args);
401 401 }
402 402 (void) strcat(buf, "\n");
403 403 /*
404 404 if (report_pwd) {
405 405 */
406 406 if (1) {
407 407 (void) strcat(buf, cur_wrk_dir);
408 408 (void) strcat(buf, get_current_path_mksh());
409 409 (void) strcat(buf, "\n");
410 410 }
411 411 (void) fputs(buf, stderr);
412 412 (void) fflush(stderr);
413 413 if (buf != static_buf) {
414 414 retmem_mb(buf);
415 415 }
416 416 exit_status = 1;
417 417 exit(1);
418 418 }
419 419
420 420 /*
421 421 * fatal_reader_mksh(format, args...)
422 422 *
423 423 * Parameters:
424 424 * format printf style format string
425 425 * args arguments to match the format
426 426 */
427 427 /*VARARGS*/
428 428 void
429 429 fatal_reader_mksh(const char * pattern, ...)
↓ open down ↓ |
38 lines elided |
↑ open up ↑ |
430 430 {
431 431 va_list args;
432 432 char message[1000];
433 433
434 434 va_start(args, pattern);
435 435 /*
436 436 if (file_being_read != NULL) {
437 437 WCSTOMBS(mbs_buffer, file_being_read);
438 438 if (line_number != 0) {
439 439 (void) sprintf(message,
440 - catgets(libmksdmsi18n_catd, 1, 130, "%s, line %d: %s"),
440 + gettext("%s, line %d: %s"),
441 441 mbs_buffer,
442 442 line_number,
443 443 pattern);
444 444 } else {
445 445 (void) sprintf(message,
446 446 "%s: %s",
447 447 mbs_buffer,
448 448 pattern);
449 449 }
450 450 pattern = message;
451 451 }
452 452 */
453 453
454 454 (void) fflush(stdout);
455 - (void) fprintf(stderr, catgets(libmksdmsi18n_catd, 1, 131, "mksh: Fatal error in reader: "));
455 + (void) fprintf(stderr, gettext("mksh: Fatal error in reader: "));
456 456 (void) vfprintf(stderr, pattern, args);
457 457 (void) fprintf(stderr, "\n");
458 458 va_end(args);
459 459
460 460 /*
461 461 if (temp_file_name != NULL) {
462 462 (void) fprintf(stderr,
463 - catgets(libmksdmsi18n_catd, 1, 132, "mksh: Temp-file %s not removed\n"),
463 + gettext("mksh: Temp-file %s not removed\n"),
464 464 temp_file_name->string_mb);
465 465 temp_file_name = NULL;
466 466 }
467 467 */
468 468
469 469 /*
470 470 if (report_pwd) {
471 471 */
472 472 if (1) {
473 473 (void) fprintf(stderr,
474 - catgets(libmksdmsi18n_catd, 1, 133, "Current working directory %s\n"),
474 + gettext("Current working directory %s\n"),
475 475 get_current_path_mksh());
476 476 }
477 477 (void) fflush(stderr);
478 478 exit_status = 1;
479 479 exit(1);
480 480 }
481 481
482 482 /*
483 483 * warning_mksh(format, args...)
484 484 *
485 485 * Print a message and continue.
486 486 *
487 487 * Parameters:
488 488 * format printf type format string
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
489 489 * args Arguments to match the format
490 490 */
491 491 /*VARARGS*/
492 492 void
493 493 warning_mksh(char * message, ...)
494 494 {
495 495 va_list args;
496 496
497 497 va_start(args, message);
498 498 (void) fflush(stdout);
499 - (void) fprintf(stderr, catgets(libmksdmsi18n_catd, 1, 134, "mksh: Warning: "));
499 + (void) fprintf(stderr, gettext("mksh: Warning: "));
500 500 (void) vfprintf(stderr, message, args);
501 501 (void) fprintf(stderr, "\n");
502 502 va_end(args);
503 503 /*
504 504 if (report_pwd) {
505 505 */
506 506 if (1) {
507 507 (void) fprintf(stderr,
508 - catgets(libmksdmsi18n_catd, 1, 135, "Current working directory %s\n"),
508 + gettext("Current working directory %s\n"),
509 509 get_current_path_mksh());
510 510 }
511 511 (void) fflush(stderr);
512 512 }
513 513
514 514 /*
515 515 * get_current_path_mksh()
516 516 *
517 517 * Stuff current_path with the current path if it isnt there already.
518 518 *
519 519 * Parameters:
520 520 *
521 521 * Global variables used:
522 522 */
523 523 char *
524 524 get_current_path_mksh(void)
525 525 {
526 526 char pwd[(MAXPATHLEN * MB_LEN_MAX)];
527 527 static char *current_path;
528 528
529 529 if (current_path == NULL) {
530 530 getcwd(pwd, sizeof(pwd));
531 531 if (pwd[0] == (int) nul_char) {
532 532 pwd[0] = (int) slash_char;
533 533 pwd[1] = (int) nul_char;
534 534 }
535 535 current_path = strdup(pwd);
536 536 }
537 537 return current_path;
538 538 }
539 539
540 540 /*
541 541 * append_prop(target, type)
542 542 *
543 543 * Create a new property and append it to the property list of a Name.
544 544 *
545 545 * Return value:
546 546 * A new property block for the target
547 547 *
548 548 * Parameters:
549 549 * target The target that wants a new property
550 550 * type The type of property being requested
551 551 *
552 552 * Global variables used:
553 553 */
554 554 Property
555 555 append_prop(register Name target, register Property_id type)
556 556 {
557 557 register Property *insert = &target->prop;
558 558 register Property prop = *insert;
559 559 register int size;
560 560
561 561 switch (type) {
562 562 case conditional_prop:
563 563 size = sizeof (struct Conditional);
564 564 break;
565 565 case line_prop:
566 566 size = sizeof (struct Line);
567 567 break;
568 568 case macro_prop:
569 569 size = sizeof (struct _Macro);
570 570 break;
571 571 case makefile_prop:
572 572 size = sizeof (struct Makefile);
573 573 break;
574 574 case member_prop:
575 575 size = sizeof (struct Member);
576 576 break;
577 577 case recursive_prop:
578 578 size = sizeof (struct Recursive);
579 579 break;
580 580 case sccs_prop:
581 581 size = sizeof (struct Sccs);
582 582 break;
583 583 case suffix_prop:
584 584 size = sizeof (struct Suffix);
585 585 break;
586 586 case target_prop:
587 587 size = sizeof (struct Target);
588 588 break;
589 589 case time_prop:
590 590 size = sizeof (struct STime);
591 591 break;
592 592 case vpath_alias_prop:
593 593 size = sizeof (struct Vpath_alias);
594 594 break;
↓ open down ↓ |
76 lines elided |
↑ open up ↑ |
595 595 case long_member_name_prop:
596 596 size = sizeof (struct Long_member_name);
597 597 break;
598 598 case macro_append_prop:
599 599 size = sizeof (struct _Macro_appendix);
600 600 break;
601 601 case env_mem_prop:
602 602 size = sizeof (struct _Env_mem);
603 603 break;
604 604 default:
605 - fatal_mksh(catgets(libmksdmsi18n_catd, 1, 136, "Internal error. Unknown prop type %d"), type);
605 + fatal_mksh(gettext("Internal error. Unknown prop type %d"), type);
606 606 }
607 607 for (; prop != NULL; insert = &prop->next, prop = *insert);
608 608 size += PROPERTY_HEAD_SIZE;
609 609 *insert = prop = (Property) getmem(size);
610 610 memset((char *) prop, 0, size);
611 611 prop->type = type;
612 612 prop->next = NULL;
613 613 return prop;
614 614 }
615 615
616 616 /*
617 617 * maybe_append_prop(target, type)
618 618 *
619 619 * Append a property to the Name if none of this type exists
620 620 * else return the one already there
621 621 *
622 622 * Return value:
623 623 * A property of the requested type for the target
624 624 *
625 625 * Parameters:
626 626 * target The target that wants a new property
627 627 * type The type of property being requested
628 628 *
629 629 * Global variables used:
630 630 */
631 631 Property
632 632 maybe_append_prop(register Name target, register Property_id type)
633 633 {
634 634 register Property prop;
635 635
636 636 if ((prop = get_prop(target->prop, type)) != NULL) {
637 637 return prop;
638 638 }
639 639 return append_prop(target, type);
640 640 }
641 641
642 642 /*
643 643 * get_prop(start, type)
644 644 *
645 645 * Scan the property list of a Name to find the next property
646 646 * of a given type.
647 647 *
648 648 * Return value:
649 649 * The first property of the type, if any left
650 650 *
651 651 * Parameters:
652 652 * start The first property block to check for type
653 653 * type The type of property block we need
654 654 *
655 655 * Global variables used:
656 656 */
657 657 Property
658 658 get_prop(register Property start, register Property_id type)
659 659 {
660 660 for (; start != NULL; start = start->next) {
661 661 if (start->type == type) {
662 662 return start;
663 663 }
664 664 }
665 665 return NULL;
666 666 }
667 667
668 668 /*
669 669 * append_string(from, to, length)
670 670 *
671 671 * Append a C string to a make string expanding it if nessecary
672 672 *
673 673 * Parameters:
674 674 * from The source (C style) string
675 675 * to The destination (make style) string
676 676 * length The length of the from string
677 677 *
678 678 * Global variables used:
679 679 */
680 680 void
681 681 append_string(register wchar_t *from, register String to, register int length)
682 682 {
683 683 if (length == FIND_LENGTH) {
684 684 length = wslen(from);
685 685 }
686 686 if (to->buffer.start == NULL) {
687 687 expand_string(to, 32 + length);
688 688 }
689 689 if (to->buffer.end - to->text.p <= length) {
690 690 expand_string(to,
691 691 (to->buffer.end - to->buffer.start) * 2 +
692 692 length);
693 693 }
694 694 if (length > 0) {
695 695 (void) wsncpy(to->text.p, from, length);
696 696 to->text.p += length;
697 697 }
698 698 *(to->text.p) = (int) nul_char;
699 699 }
700 700
701 701 wchar_t * get_wstring(char *from) {
702 702 if(from == NULL) {
703 703 return NULL;
704 704 }
705 705 getwstring_count++;
706 706 wchar_t * wcbuf = ALLOC_WC(strlen(from) + 1);
707 707 mbstowcs(wcbuf, from, strlen(from)+1);
708 708 return wcbuf;
709 709 }
710 710
711 711 void
712 712 append_string(register char *from, register String to, register int length)
713 713 {
714 714 if (length == FIND_LENGTH) {
715 715 length = strlen(from);
716 716 }
717 717 if (to->buffer.start == NULL) {
718 718 expand_string(to, 32 + length);
719 719 }
720 720 if (to->buffer.end - to->text.p <= length) {
721 721 expand_string(to,
722 722 (to->buffer.end - to->buffer.start) * 2 +
723 723 length);
724 724 }
725 725 if (length > 0) {
726 726 (void) mbstowcs(to->text.p, from, length);
727 727 to->text.p += length;
728 728 }
729 729 *(to->text.p) = (int) nul_char;
730 730 }
731 731
732 732 /*
733 733 * expand_string(string, length)
734 734 *
735 735 * Allocate more memory for strings that run out of space.
736 736 *
737 737 * Parameters:
738 738 * string The make style string we want to expand
739 739 * length The new length we need
740 740 *
741 741 * Global variables used:
742 742 */
743 743 static void
744 744 expand_string(register String string, register int length)
745 745 {
746 746 register wchar_t *p;
747 747
748 748 if (string->buffer.start == NULL) {
749 749 /* For strings that have no memory allocated */
750 750 string->buffer.start =
751 751 string->text.p =
752 752 string->text.end =
753 753 ALLOC_WC(length);
754 754 string->buffer.end = string->buffer.start + length;
755 755 string->text.p[0] = (int) nul_char;
756 756 string->free_after_use = true;
757 757 expandstring_count++;
758 758 return;
759 759 }
760 760 if (string->buffer.end - string->buffer.start >= length) {
761 761 /* If we really don't need more memory. */
762 762 return;
763 763 }
764 764 /*
765 765 * Get more memory, copy the string and free the old buffer if
766 766 * it is was malloc()'ed.
767 767 */
768 768 expandstring_count++;
769 769 p = ALLOC_WC(length);
770 770 (void) wscpy(p, string->buffer.start);
771 771 string->text.p = p + (string->text.p - string->buffer.start);
772 772 string->text.end = p + (string->text.end - string->buffer.start);
773 773 string->buffer.end = p + length;
774 774 if (string->free_after_use) {
775 775 retmem(string->buffer.start);
776 776 }
777 777 string->buffer.start = p;
778 778 string->free_after_use = true;
779 779 }
780 780
781 781 /*
782 782 * append_char(from, to)
783 783 *
784 784 * Append one char to a make string expanding it if nessecary
785 785 *
786 786 * Parameters:
787 787 * from Single character to append to string
788 788 * to The destination (make style) string
789 789 *
790 790 * Global variables used:
791 791 */
792 792 void
793 793 append_char(wchar_t from, register String to)
794 794 {
795 795 if (to->buffer.start == NULL) {
796 796 expand_string(to, 32);
797 797 }
798 798 if (to->buffer.end - to->text.p <= 2) {
799 799 expand_string(to, to->buffer.end - to->buffer.start + 32);
800 800 }
801 801 *(to->text.p)++ = from;
802 802 *(to->text.p) = (int) nul_char;
803 803 }
804 804
805 805 /*
806 806 * handle_interrupt_mksh()
807 807 *
808 808 * This is where C-C traps are caught.
809 809 */
810 810 void
811 811 handle_interrupt_mksh(int)
812 812 {
813 813 (void) fflush(stdout);
814 814 /* Make sure the processes running under us terminate first. */
815 815 if (childPid > 0) {
816 816 kill(childPid, SIGTERM);
817 817 childPid = -1;
818 818 }
819 819 while (wait((int *) NULL) != -1);
820 820 exit_status = 2;
821 821 exit(2);
822 822 }
823 823
824 824 /*
825 825 * setup_interrupt()
826 826 *
827 827 * This routine saves the original interrupt handler pointers
828 828 *
829 829 * Parameters:
830 830 *
831 831 * Static variables used:
832 832 * sigivalue The original signal handler
833 833 * sigqvalue The original signal handler
834 834 * sigtvalue The original signal handler
835 835 * sighvalue The original signal handler
836 836 */
837 837 void
838 838 setup_interrupt(register void (*handler) (int))
839 839 {
840 840 sigivalue = bsd_signal(SIGINT, SIG_IGN);
841 841 sigqvalue = bsd_signal(SIGQUIT, SIG_IGN);
↓ open down ↓ |
226 lines elided |
↑ open up ↑ |
842 842 sigtvalue = bsd_signal(SIGTERM, SIG_IGN);
843 843 sighvalue = bsd_signal(SIGHUP, SIG_IGN);
844 844 enable_interrupt(handler);
845 845 }
846 846
847 847
848 848 void
849 849 mbstowcs_with_check(wchar_t *pwcs, const char *s, size_t n)
850 850 {
851 851 if(mbstowcs(pwcs, s, n) == -1) {
852 - fatal_mksh(catgets(libmksdmsi18n_catd, 1, 143, "The string `%s' is not valid in current locale"), s);
852 + fatal_mksh(gettext("The string `%s' is not valid in current locale"), s);
853 853 }
854 854 }
855 855
856 856
857 857
858 858 Wstring::Wstring()
859 859 {
860 860 INIT_STRING_FROM_STACK(string, string_buf);
861 861 }
862 862
863 863 Wstring::Wstring(struct _Name * name)
864 864 {
865 865 INIT_STRING_FROM_STACK(string, string_buf);
866 866 append_string(name->string_mb, &string, name->hash.length);
867 867 }
868 868
869 869 Wstring::~Wstring()
870 870 {
871 871 if(string.free_after_use) {
872 872 retmem(string.buffer.start);
873 873 }
874 874 }
875 875
876 876 void
877 877 Wstring::init(struct _Name * name)
878 878 {
879 879 if(string.free_after_use) {
880 880 retmem(string.buffer.start);
881 881 }
882 882 INIT_STRING_FROM_STACK(string, string_buf);
883 883 append_string(name->string_mb, &string, name->hash.length);
884 884 }
885 885
886 886 void
887 887 Wstring::init(wchar_t * name, unsigned length)
888 888 {
889 889 INIT_STRING_FROM_STACK(string, string_buf);
890 890 append_string(name, &string, length);
891 891 string.buffer.start[length] = 0;
892 892 }
893 893
894 894 Boolean
895 895 Wstring::equaln(wchar_t * str, unsigned length)
896 896 {
897 897 return (Boolean)IS_WEQUALN(string.buffer.start, str, length);
898 898 }
899 899
900 900 Boolean
901 901 Wstring::equaln(Wstring * str, unsigned length)
902 902 {
903 903 return (Boolean)IS_WEQUALN(string.buffer.start, str->string.buffer.start, length);
904 904 }
905 905
906 906 Boolean
907 907 Wstring::equal(wchar_t * str, unsigned off, unsigned length)
908 908 {
909 909 return (Boolean)IS_WEQUALN(string.buffer.start + off, str, length);
910 910 }
911 911
912 912 Boolean
913 913 Wstring::equal(wchar_t * str, unsigned off)
914 914 {
915 915 return (Boolean)IS_WEQUAL(string.buffer.start + off, str);
916 916 }
917 917
918 918 Boolean
919 919 Wstring::equal(wchar_t * str)
920 920 {
921 921 return equal(str, 0);
922 922 }
923 923
924 924 Boolean
925 925 Wstring::equal(Wstring * str, unsigned off, unsigned length)
926 926 {
927 927 return (Boolean)IS_WEQUALN(string.buffer.start + off, str->string.buffer.start, length);
928 928 }
929 929
930 930 Boolean
931 931 Wstring::equal(Wstring * str)
932 932 {
933 933 return equal(str, 0);
934 934 }
935 935
936 936 Boolean
937 937 Wstring::equal(Wstring * str, unsigned off)
938 938 {
939 939 return (Boolean)IS_WEQUAL(string.buffer.start + off, str->string.buffer.start);
940 940 }
941 941
942 942 void
943 943 Wstring::append_to_str(struct _String * str, unsigned off, unsigned length)
944 944 {
945 945 append_string(string.buffer.start + off, str, length);
946 946 }
947 947
948 948 Name
949 949 Name_set::lookup(const char *key)
950 950 {
951 951 for (entry *node = root; node != 0;) {
952 952 int res = strcmp(key, node->name->string_mb);
953 953 if (res < 0) {
954 954 node = node->left;
955 955 } else if (res > 0) {
956 956 node = node->right;
957 957 } else {
958 958 return node->name;
959 959 }
960 960 }
961 961 return 0;
962 962 }
963 963
964 964 Name
965 965 Name_set::insert(const char *key, Boolean &found)
966 966 {
967 967 Name name = 0;
968 968
969 969 if (root != 0) {
970 970 for (entry *node = root; name == 0;) {
971 971 int res = strcmp(key, node->name->string_mb);
972 972 if (res < 0) {
973 973 if (node->left != 0) {
974 974 node = node->left;
975 975 } else {
976 976 found = false;
977 977 name = ALLOC(Name);
978 978
979 979 node->left = new entry(name, node);
980 980 rebalance(node);
981 981 }
982 982 } else if (res > 0) {
983 983 if (node->right != 0) {
984 984 node = node->right;
985 985 } else {
986 986 found = false;
987 987 name = ALLOC(Name);
988 988
989 989 node->right = new entry(name, node);
990 990 rebalance(node);
991 991 }
992 992 } else {
993 993 found = true;
994 994 name = node->name;
995 995 }
996 996 }
997 997 } else {
998 998 found = false;
999 999 name = ALLOC(Name);
1000 1000
1001 1001 root = new entry(name, 0);
1002 1002 }
1003 1003 return name;
1004 1004 }
1005 1005
1006 1006 void
1007 1007 Name_set::insert(Name name) {
1008 1008 if (root != 0) {
1009 1009 for (entry *node = root;;) {
1010 1010 int res = strcmp(name->string_mb, node->name->string_mb);
1011 1011 if (res < 0) {
1012 1012 if (node->left != 0) {
1013 1013 node = node->left;
1014 1014 } else {
1015 1015 node->left = new entry(name, node);
1016 1016 rebalance(node);
1017 1017 break;
1018 1018 }
1019 1019 } else if (res > 0) {
1020 1020 if (node->right != 0) {
1021 1021 node = node->right;
1022 1022 } else {
1023 1023 node->right = new entry(name, node);
1024 1024 rebalance(node);
1025 1025 break;
1026 1026 }
1027 1027 } else {
1028 1028 // should be an error: inserting already existing name
1029 1029 break;
1030 1030 }
1031 1031 }
1032 1032 } else {
1033 1033 root = new entry(name, 0);
1034 1034 }
1035 1035 }
1036 1036
1037 1037 void
1038 1038 Name_set::rebalance(Name_set::entry *node) {
1039 1039 for (; node != 0; node = node->parent) {
1040 1040 entry *right = node->right;
1041 1041 entry *left = node->left;
1042 1042
1043 1043 unsigned rdepth = (right != 0) ? right->depth : 0;
1044 1044 unsigned ldepth = (left != 0) ? left->depth : 0;
1045 1045
1046 1046 if (ldepth > rdepth + 1) {
1047 1047 if ((node->left = left->right) != 0) {
1048 1048 left->right->parent = node;
1049 1049 }
1050 1050 if ((left->parent = node->parent) != 0) {
1051 1051 if (node == node->parent->right) {
1052 1052 node->parent->right = left;
1053 1053 } else {
1054 1054 node->parent->left = left;
1055 1055 }
1056 1056 } else {
1057 1057 root = left;
1058 1058 }
1059 1059 left->right = node;
1060 1060 node->parent = left;
1061 1061
1062 1062 node->setup_depth();
1063 1063 node = left;
1064 1064 } else if (rdepth > ldepth + 1) {
1065 1065 if ((node->right = right->left) != 0) {
1066 1066 right->left->parent = node;
1067 1067 }
1068 1068 if ((right->parent = node->parent) != 0) {
1069 1069 if (node == node->parent->right) {
1070 1070 node->parent->right = right;
1071 1071 } else {
1072 1072 node->parent->left = right;
1073 1073 }
1074 1074 } else {
1075 1075 root = right;
1076 1076 }
1077 1077 right->left = node;
1078 1078 node->parent = right;
1079 1079
1080 1080 node->setup_depth();
1081 1081 node = right;
1082 1082 }
1083 1083 node->setup_depth();
1084 1084 }
1085 1085 }
1086 1086
1087 1087 Name_set::iterator
1088 1088 Name_set::begin() const {
1089 1089 for (entry *node = root; node != 0; node = node->left) {
1090 1090 if (node->left == 0) {
1091 1091 return iterator(node);
1092 1092 }
1093 1093 }
1094 1094 return iterator();
1095 1095 }
1096 1096
1097 1097 Name_set::iterator&
1098 1098 Name_set::iterator::operator++() {
1099 1099 if (node != 0) {
1100 1100 if (node->right != 0) {
1101 1101 node = node->right;
1102 1102 while (node->left != 0) {
1103 1103 node = node->left;
1104 1104 }
1105 1105 } else {
1106 1106 while ((node->parent != 0) && (node->parent->right == node)) {
1107 1107 node = node->parent;
1108 1108 }
1109 1109 node = node->parent;
1110 1110 }
1111 1111 }
1112 1112 return *this;
1113 1113 }
↓ open down ↓ |
251 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX