src/emu/inptport.c
| r7335 | r7336 | |
| 98 | 98 | #include "profiler.h" |
| 99 | 99 | #include "ui.h" |
| 100 | 100 | #include "uiinput.h" |
| 101 | #include "debug/debugcon.h" |
| 101 | 102 | |
| 102 | 103 | #include <ctype.h> |
| 103 | 104 | #include <time.h> |
| 104 | 105 | |
| 105 | | #ifdef MESS |
| 106 | | #include "uimess.h" |
| 107 | | #endif /* MESS */ |
| 108 | | |
| 109 | | |
| 110 | 106 | /* temporary: set this to 1 to enable the originally defined behavior that |
| 111 | 107 | a field specified via PORT_MODIFY which intersects a previously-defined |
| 112 | 108 | field completely wipes out the previous definition */ |
| r7335 | r7336 | |
| 130 | 126 | #define JOYDIR_LEFT_BIT (1 << JOYDIR_LEFT) |
| 131 | 127 | #define JOYDIR_RIGHT_BIT (1 << JOYDIR_RIGHT) |
| 132 | 128 | |
| 129 | #define NUM_SIMUL_KEYS (UCHAR_SHIFT_END - UCHAR_SHIFT_BEGIN + 1) |
| 130 | #define LOG_INPUTX 0 |
| 131 | #define SPACE_COUNT 3 |
| 132 | #define INVALID_CHAR '?' |
| 133 | #define IP_NAME_DEFAULT NULL |
| 133 | 134 | |
| 134 | 135 | |
| 135 | 136 | /*************************************************************************** |
| r7335 | r7336 | |
| 268 | 269 | UINT32 playback_accumulated_frames;/* accumulated frames during playback */ |
| 269 | 270 | }; |
| 270 | 271 | |
| 272 | typedef const input_port_config *input_port_config_c; |
| 273 | typedef const input_field_config *input_field_config_c; |
| 271 | 274 | |
| 272 | 275 | |
| 276 | typedef struct _inputx_code inputx_code; |
| 277 | struct _inputx_code |
| 278 | { |
| 279 | unicode_char ch; |
| 280 | input_field_config_c field[NUM_SIMUL_KEYS]; |
| 281 | }; |
| 282 | |
| 283 | typedef struct _key_buffer key_buffer; |
| 284 | struct _key_buffer |
| 285 | { |
| 286 | int begin_pos; |
| 287 | int end_pos; |
| 288 | unsigned int status_keydown : 1; |
| 289 | unicode_char buffer[4096]; |
| 290 | }; |
| 291 | |
| 292 | typedef struct _char_info char_info; |
| 293 | struct _char_info |
| 294 | { |
| 295 | unicode_char ch; |
| 296 | const char *name; |
| 297 | const char *alternate; /* alternative string, in UTF-8 */ |
| 298 | }; |
| 299 | |
| 273 | 300 | /*************************************************************************** |
| 274 | 301 | MACROS |
| 275 | 302 | ***************************************************************************/ |
| r7335 | r7336 | |
| 291 | 318 | static const char *const seqtypestrings[] = { "standard", "decrement", "increment" }; |
| 292 | 319 | |
| 293 | 320 | |
| 321 | static const char_info charinfo[] = |
| 322 | { |
| 323 | { 0x0008, "Backspace", NULL }, /* Backspace */ |
| 324 | { 0x0009, "Tab", " " }, /* Tab */ |
| 325 | { 0x000c, "Clear", NULL }, /* Clear */ |
| 326 | { 0x000d, "Enter", NULL }, /* Enter */ |
| 327 | { 0x001a, "Esc", NULL }, /* Esc */ |
| 328 | { 0x0020, "Space", " " }, /* Space */ |
| 329 | { 0x0061, NULL, "A" }, /* a */ |
| 330 | { 0x0062, NULL, "B" }, /* b */ |
| 331 | { 0x0063, NULL, "C" }, /* c */ |
| 332 | { 0x0064, NULL, "D" }, /* d */ |
| 333 | { 0x0065, NULL, "E" }, /* e */ |
| 334 | { 0x0066, NULL, "F" }, /* f */ |
| 335 | { 0x0067, NULL, "G" }, /* g */ |
| 336 | { 0x0068, NULL, "H" }, /* h */ |
| 337 | { 0x0069, NULL, "I" }, /* i */ |
| 338 | { 0x006a, NULL, "J" }, /* j */ |
| 339 | { 0x006b, NULL, "K" }, /* k */ |
| 340 | { 0x006c, NULL, "L" }, /* l */ |
| 341 | { 0x006d, NULL, "M" }, /* m */ |
| 342 | { 0x006e, NULL, "N" }, /* n */ |
| 343 | { 0x006f, NULL, "O" }, /* o */ |
| 344 | { 0x0070, NULL, "P" }, /* p */ |
| 345 | { 0x0071, NULL, "Q" }, /* q */ |
| 346 | { 0x0072, NULL, "R" }, /* r */ |
| 347 | { 0x0073, NULL, "S" }, /* s */ |
| 348 | { 0x0074, NULL, "T" }, /* t */ |
| 349 | { 0x0075, NULL, "U" }, /* u */ |
| 350 | { 0x0076, NULL, "V" }, /* v */ |
| 351 | { 0x0077, NULL, "W" }, /* w */ |
| 352 | { 0x0078, NULL, "X" }, /* x */ |
| 353 | { 0x0079, NULL, "Y" }, /* y */ |
| 354 | { 0x007a, NULL, "Z" }, /* z */ |
| 355 | { 0x00a0, NULL, " " }, /* non breaking space */ |
| 356 | { 0x00a1, NULL, "!" }, /* inverted exclaimation mark */ |
| 357 | { 0x00a6, NULL, "|" }, /* broken bar */ |
| 358 | { 0x00a9, NULL, "(c)" }, /* copyright sign */ |
| 359 | { 0x00ab, NULL, "<<" }, /* left pointing double angle */ |
| 360 | { 0x00ae, NULL, "(r)" }, /* registered sign */ |
| 361 | { 0x00bb, NULL, ">>" }, /* right pointing double angle */ |
| 362 | { 0x00bc, NULL, "1/4" }, /* vulgar fraction one quarter */ |
| 363 | { 0x00bd, NULL, "1/2" }, /* vulgar fraction one half */ |
| 364 | { 0x00be, NULL, "3/4" }, /* vulgar fraction three quarters */ |
| 365 | { 0x00bf, NULL, "?" }, /* inverted question mark */ |
| 366 | { 0x00c0, NULL, "A" }, /* 'A' grave */ |
| 367 | { 0x00c1, NULL, "A" }, /* 'A' acute */ |
| 368 | { 0x00c2, NULL, "A" }, /* 'A' circumflex */ |
| 369 | { 0x00c3, NULL, "A" }, /* 'A' tilde */ |
| 370 | { 0x00c4, NULL, "A" }, /* 'A' diaeresis */ |
| 371 | { 0x00c5, NULL, "A" }, /* 'A' ring above */ |
| 372 | { 0x00c6, NULL, "AE" }, /* 'AE' ligature */ |
| 373 | { 0x00c7, NULL, "C" }, /* 'C' cedilla */ |
| 374 | { 0x00c8, NULL, "E" }, /* 'E' grave */ |
| 375 | { 0x00c9, NULL, "E" }, /* 'E' acute */ |
| 376 | { 0x00ca, NULL, "E" }, /* 'E' circumflex */ |
| 377 | { 0x00cb, NULL, "E" }, /* 'E' diaeresis */ |
| 378 | { 0x00cc, NULL, "I" }, /* 'I' grave */ |
| 379 | { 0x00cd, NULL, "I" }, /* 'I' acute */ |
| 380 | { 0x00ce, NULL, "I" }, /* 'I' circumflex */ |
| 381 | { 0x00cf, NULL, "I" }, /* 'I' diaeresis */ |
| 382 | { 0x00d0, NULL, "D" }, /* 'ETH' */ |
| 383 | { 0x00d1, NULL, "N" }, /* 'N' tilde */ |
| 384 | { 0x00d2, NULL, "O" }, /* 'O' grave */ |
| 385 | { 0x00d3, NULL, "O" }, /* 'O' acute */ |
| 386 | { 0x00d4, NULL, "O" }, /* 'O' circumflex */ |
| 387 | { 0x00d5, NULL, "O" }, /* 'O' tilde */ |
| 388 | { 0x00d6, NULL, "O" }, /* 'O' diaeresis */ |
| 389 | { 0x00d7, NULL, "X" }, /* multiplication sign */ |
| 390 | { 0x00d8, NULL, "O" }, /* 'O' stroke */ |
| 391 | { 0x00d9, NULL, "U" }, /* 'U' grave */ |
| 392 | { 0x00da, NULL, "U" }, /* 'U' acute */ |
| 393 | { 0x00db, NULL, "U" }, /* 'U' circumflex */ |
| 394 | { 0x00dc, NULL, "U" }, /* 'U' diaeresis */ |
| 395 | { 0x00dd, NULL, "Y" }, /* 'Y' acute */ |
| 396 | { 0x00df, NULL, "SS" }, /* sharp S */ |
| 397 | { 0x00e0, NULL, "a" }, /* 'a' grave */ |
| 398 | { 0x00e1, NULL, "a" }, /* 'a' acute */ |
| 399 | { 0x00e2, NULL, "a" }, /* 'a' circumflex */ |
| 400 | { 0x00e3, NULL, "a" }, /* 'a' tilde */ |
| 401 | { 0x00e4, NULL, "a" }, /* 'a' diaeresis */ |
| 402 | { 0x00e5, NULL, "a" }, /* 'a' ring above */ |
| 403 | { 0x00e6, NULL, "ae" }, /* 'ae' ligature */ |
| 404 | { 0x00e7, NULL, "c" }, /* 'c' cedilla */ |
| 405 | { 0x00e8, NULL, "e" }, /* 'e' grave */ |
| 406 | { 0x00e9, NULL, "e" }, /* 'e' acute */ |
| 407 | { 0x00ea, NULL, "e" }, /* 'e' circumflex */ |
| 408 | { 0x00eb, NULL, "e" }, /* 'e' diaeresis */ |
| 409 | { 0x00ec, NULL, "i" }, /* 'i' grave */ |
| 410 | { 0x00ed, NULL, "i" }, /* 'i' acute */ |
| 411 | { 0x00ee, NULL, "i" }, /* 'i' circumflex */ |
| 412 | { 0x00ef, NULL, "i" }, /* 'i' diaeresis */ |
| 413 | { 0x00f0, NULL, "d" }, /* 'eth' */ |
| 414 | { 0x00f1, NULL, "n" }, /* 'n' tilde */ |
| 415 | { 0x00f2, NULL, "o" }, /* 'o' grave */ |
| 416 | { 0x00f3, NULL, "o" }, /* 'o' acute */ |
| 417 | { 0x00f4, NULL, "o" }, /* 'o' circumflex */ |
| 418 | { 0x00f5, NULL, "o" }, /* 'o' tilde */ |
| 419 | { 0x00f6, NULL, "o" }, /* 'o' diaeresis */ |
| 420 | { 0x00f8, NULL, "o" }, /* 'o' stroke */ |
| 421 | { 0x00f9, NULL, "u" }, /* 'u' grave */ |
| 422 | { 0x00fa, NULL, "u" }, /* 'u' acute */ |
| 423 | { 0x00fb, NULL, "u" }, /* 'u' circumflex */ |
| 424 | { 0x00fc, NULL, "u" }, /* 'u' diaeresis */ |
| 425 | { 0x00fd, NULL, "y" }, /* 'y' acute */ |
| 426 | { 0x00ff, NULL, "y" }, /* 'y' diaeresis */ |
| 427 | { 0x2010, NULL, "-" }, /* hyphen */ |
| 428 | { 0x2011, NULL, "-" }, /* non-breaking hyphen */ |
| 429 | { 0x2012, NULL, "-" }, /* figure dash */ |
| 430 | { 0x2013, NULL, "-" }, /* en dash */ |
| 431 | { 0x2014, NULL, "-" }, /* em dash */ |
| 432 | { 0x2015, NULL, "-" }, /* horizontal dash */ |
| 433 | { 0x2018, NULL, "\'" }, /* left single quotation mark */ |
| 434 | { 0x2019, NULL, "\'" }, /* right single quotation mark */ |
| 435 | { 0x201a, NULL, "\'" }, /* single low quotation mark */ |
| 436 | { 0x201b, NULL, "\'" }, /* single high reversed quotation mark */ |
| 437 | { 0x201c, NULL, "\"" }, /* left double quotation mark */ |
| 438 | { 0x201d, NULL, "\"" }, /* right double quotation mark */ |
| 439 | { 0x201e, NULL, "\"" }, /* double low quotation mark */ |
| 440 | { 0x201f, NULL, "\"" }, /* double high reversed quotation mark */ |
| 441 | { 0x2024, NULL, "." }, /* one dot leader */ |
| 442 | { 0x2025, NULL, ".." }, /* two dot leader */ |
| 443 | { 0x2026, NULL, "..." }, /* horizontal ellipsis */ |
| 444 | { 0x2047, NULL, "??" }, /* double question mark */ |
| 445 | { 0x2048, NULL, "?!" }, /* question exclamation mark */ |
| 446 | { 0x2049, NULL, "!?" }, /* exclamation question mark */ |
| 447 | { 0xff01, NULL, "!" }, /* fullwidth exclamation point */ |
| 448 | { 0xff02, NULL, "\"" }, /* fullwidth quotation mark */ |
| 449 | { 0xff03, NULL, "#" }, /* fullwidth number sign */ |
| 450 | { 0xff04, NULL, "$" }, /* fullwidth dollar sign */ |
| 451 | { 0xff05, NULL, "%" }, /* fullwidth percent sign */ |
| 452 | { 0xff06, NULL, "&" }, /* fullwidth ampersand */ |
| 453 | { 0xff07, NULL, "\'" }, /* fullwidth apostrophe */ |
| 454 | { 0xff08, NULL, "(" }, /* fullwidth left parenthesis */ |
| 455 | { 0xff09, NULL, ")" }, /* fullwidth right parenthesis */ |
| 456 | { 0xff0a, NULL, "*" }, /* fullwidth asterisk */ |
| 457 | { 0xff0b, NULL, "+" }, /* fullwidth plus */ |
| 458 | { 0xff0c, NULL, "," }, /* fullwidth comma */ |
| 459 | { 0xff0d, NULL, "-" }, /* fullwidth minus */ |
| 460 | { 0xff0e, NULL, "." }, /* fullwidth period */ |
| 461 | { 0xff0f, NULL, "/" }, /* fullwidth slash */ |
| 462 | { 0xff10, NULL, "0" }, /* fullwidth zero */ |
| 463 | { 0xff11, NULL, "1" }, /* fullwidth one */ |
| 464 | { 0xff12, NULL, "2" }, /* fullwidth two */ |
| 465 | { 0xff13, NULL, "3" }, /* fullwidth three */ |
| 466 | { 0xff14, NULL, "4" }, /* fullwidth four */ |
| 467 | { 0xff15, NULL, "5" }, /* fullwidth five */ |
| 468 | { 0xff16, NULL, "6" }, /* fullwidth six */ |
| 469 | { 0xff17, NULL, "7" }, /* fullwidth seven */ |
| 470 | { 0xff18, NULL, "8" }, /* fullwidth eight */ |
| 471 | { 0xff19, NULL, "9" }, /* fullwidth nine */ |
| 472 | { 0xff1a, NULL, ":" }, /* fullwidth colon */ |
| 473 | { 0xff1b, NULL, ";" }, /* fullwidth semicolon */ |
| 474 | { 0xff1c, NULL, "<" }, /* fullwidth less than sign */ |
| 475 | { 0xff1d, NULL, "=" }, /* fullwidth equals sign */ |
| 476 | { 0xff1e, NULL, ">" }, /* fullwidth greater than sign */ |
| 477 | { 0xff1f, NULL, "?" }, /* fullwidth question mark */ |
| 478 | { 0xff20, NULL, "@" }, /* fullwidth at sign */ |
| 479 | { 0xff21, NULL, "A" }, /* fullwidth 'A' */ |
| 480 | { 0xff22, NULL, "B" }, /* fullwidth 'B' */ |
| 481 | { 0xff23, NULL, "C" }, /* fullwidth 'C' */ |
| 482 | { 0xff24, NULL, "D" }, /* fullwidth 'D' */ |
| 483 | { 0xff25, NULL, "E" }, /* fullwidth 'E' */ |
| 484 | { 0xff26, NULL, "F" }, /* fullwidth 'F' */ |
| 485 | { 0xff27, NULL, "G" }, /* fullwidth 'G' */ |
| 486 | { 0xff28, NULL, "H" }, /* fullwidth 'H' */ |
| 487 | { 0xff29, NULL, "I" }, /* fullwidth 'I' */ |
| 488 | { 0xff2a, NULL, "J" }, /* fullwidth 'J' */ |
| 489 | { 0xff2b, NULL, "K" }, /* fullwidth 'K' */ |
| 490 | { 0xff2c, NULL, "L" }, /* fullwidth 'L' */ |
| 491 | { 0xff2d, NULL, "M" }, /* fullwidth 'M' */ |
| 492 | { 0xff2e, NULL, "N" }, /* fullwidth 'N' */ |
| 493 | { 0xff2f, NULL, "O" }, /* fullwidth 'O' */ |
| 494 | { 0xff30, NULL, "P" }, /* fullwidth 'P' */ |
| 495 | { 0xff31, NULL, "Q" }, /* fullwidth 'Q' */ |
| 496 | { 0xff32, NULL, "R" }, /* fullwidth 'R' */ |
| 497 | { 0xff33, NULL, "S" }, /* fullwidth 'S' */ |
| 498 | { 0xff34, NULL, "T" }, /* fullwidth 'T' */ |
| 499 | { 0xff35, NULL, "U" }, /* fullwidth 'U' */ |
| 500 | { 0xff36, NULL, "V" }, /* fullwidth 'V' */ |
| 501 | { 0xff37, NULL, "W" }, /* fullwidth 'W' */ |
| 502 | { 0xff38, NULL, "X" }, /* fullwidth 'X' */ |
| 503 | { 0xff39, NULL, "Y" }, /* fullwidth 'Y' */ |
| 504 | { 0xff3a, NULL, "Z" }, /* fullwidth 'Z' */ |
| 505 | { 0xff3b, NULL, "[" }, /* fullwidth left bracket */ |
| 506 | { 0xff3c, NULL, "\\" }, /* fullwidth backslash */ |
| 507 | { 0xff3d, NULL, "]" }, /* fullwidth right bracket */ |
| 508 | { 0xff3e, NULL, "^" }, /* fullwidth caret */ |
| 509 | { 0xff3f, NULL, "_" }, /* fullwidth underscore */ |
| 510 | { 0xff40, NULL, "`" }, /* fullwidth backquote */ |
| 511 | { 0xff41, NULL, "a" }, /* fullwidth 'a' */ |
| 512 | { 0xff42, NULL, "b" }, /* fullwidth 'b' */ |
| 513 | { 0xff43, NULL, "c" }, /* fullwidth 'c' */ |
| 514 | { 0xff44, NULL, "d" }, /* fullwidth 'd' */ |
| 515 | { 0xff45, NULL, "e" }, /* fullwidth 'e' */ |
| 516 | { 0xff46, NULL, "f" }, /* fullwidth 'f' */ |
| 517 | { 0xff47, NULL, "g" }, /* fullwidth 'g' */ |
| 518 | { 0xff48, NULL, "h" }, /* fullwidth 'h' */ |
| 519 | { 0xff49, NULL, "i" }, /* fullwidth 'i' */ |
| 520 | { 0xff4a, NULL, "j" }, /* fullwidth 'j' */ |
| 521 | { 0xff4b, NULL, "k" }, /* fullwidth 'k' */ |
| 522 | { 0xff4c, NULL, "l" }, /* fullwidth 'l' */ |
| 523 | { 0xff4d, NULL, "m" }, /* fullwidth 'm' */ |
| 524 | { 0xff4e, NULL, "n" }, /* fullwidth 'n' */ |
| 525 | { 0xff4f, NULL, "o" }, /* fullwidth 'o' */ |
| 526 | { 0xff50, NULL, "p" }, /* fullwidth 'p' */ |
| 527 | { 0xff51, NULL, "q" }, /* fullwidth 'q' */ |
| 528 | { 0xff52, NULL, "r" }, /* fullwidth 'r' */ |
| 529 | { 0xff53, NULL, "s" }, /* fullwidth 's' */ |
| 530 | { 0xff54, NULL, "t" }, /* fullwidth 't' */ |
| 531 | { 0xff55, NULL, "u" }, /* fullwidth 'u' */ |
| 532 | { 0xff56, NULL, "v" }, /* fullwidth 'v' */ |
| 533 | { 0xff57, NULL, "w" }, /* fullwidth 'w' */ |
| 534 | { 0xff58, NULL, "x" }, /* fullwidth 'x' */ |
| 535 | { 0xff59, NULL, "y" }, /* fullwidth 'y' */ |
| 536 | { 0xff5a, NULL, "z" }, /* fullwidth 'z' */ |
| 537 | { 0xff5b, NULL, "{" }, /* fullwidth left brace */ |
| 538 | { 0xff5c, NULL, "|" }, /* fullwidth vertical bar */ |
| 539 | { 0xff5d, NULL, "}" }, /* fullwidth right brace */ |
| 540 | { 0xff5e, NULL, "~" }, /* fullwidth tilde */ |
| 541 | { 0xff5f, NULL, "((" }, /* fullwidth double left parenthesis */ |
| 542 | { 0xff60, NULL, "))" }, /* fullwidth double right parenthesis */ |
| 543 | { 0xffe0, NULL, "\xC2\xA2" }, /* fullwidth cent sign */ |
| 544 | { 0xffe1, NULL, "\xC2\xA3" }, /* fullwidth pound sign */ |
| 545 | { 0xffe4, NULL, "\xC2\xA4" }, /* fullwidth broken bar */ |
| 546 | { 0xffe5, NULL, "\xC2\xA5" }, /* fullwidth yen sign */ |
| 547 | { 0xffe6, NULL, "\xE2\x82\xA9" }, /* fullwidth won sign */ |
| 548 | { 0xffe9, NULL, "\xE2\x86\x90" }, /* fullwidth left arrow */ |
| 549 | { 0xffea, NULL, "\xE2\x86\x91" }, /* fullwidth up arrow */ |
| 550 | { 0xffeb, NULL, "\xE2\x86\x92" }, /* fullwidth right arrow */ |
| 551 | { 0xffec, NULL, "\xE2\x86\x93" }, /* fullwidth down arrow */ |
| 552 | { 0xffed, NULL, "\xE2\x96\xAA" }, /* fullwidth solid box */ |
| 553 | { 0xffee, NULL, "\xE2\x97\xA6" }, /* fullwidth open circle */ |
| 554 | { UCHAR_SHIFT_1, "Shift", NULL }, /* Shift key */ |
| 555 | { UCHAR_SHIFT_2, "Ctrl", NULL }, /* Ctrl key */ |
| 556 | { UCHAR_MAMEKEY(F1), "F1", NULL }, /* F1 function key */ |
| 557 | { UCHAR_MAMEKEY(F2), "F2", NULL }, /* F2 function key */ |
| 558 | { UCHAR_MAMEKEY(F3), "F3", NULL }, /* F3 function key */ |
| 559 | { UCHAR_MAMEKEY(F4), "F4", NULL }, /* F4 function key */ |
| 560 | { UCHAR_MAMEKEY(F5), "F5", NULL }, /* F5 function key */ |
| 561 | { UCHAR_MAMEKEY(F6), "F6", NULL }, /* F6 function key */ |
| 562 | { UCHAR_MAMEKEY(F7), "F7", NULL }, /* F7 function key */ |
| 563 | { UCHAR_MAMEKEY(F8), "F8", NULL }, /* F8 function key */ |
| 564 | { UCHAR_MAMEKEY(F9), "F9", NULL }, /* F9 function key */ |
| 565 | { UCHAR_MAMEKEY(F10), "F10", NULL }, /* F10 function key */ |
| 566 | { UCHAR_MAMEKEY(F11), "F11", NULL }, /* F11 function key */ |
| 567 | { UCHAR_MAMEKEY(F12), "F12", NULL }, /* F12 function key */ |
| 568 | { UCHAR_MAMEKEY(F13), "F13", NULL }, /* F13 function key */ |
| 569 | { UCHAR_MAMEKEY(F14), "F14", NULL }, /* F14 function key */ |
| 570 | { UCHAR_MAMEKEY(F15), "F15", NULL }, /* F15 function key */ |
| 571 | { UCHAR_MAMEKEY(ESC), "Esc", "\033" }, /* Esc key */ |
| 572 | { UCHAR_MAMEKEY(INSERT), "Insert", NULL }, /* Insert key */ |
| 573 | { UCHAR_MAMEKEY(DEL), "Delete", "\010" }, /* Delete key */ |
| 574 | { UCHAR_MAMEKEY(HOME), "Home", "\014" }, /* Home key */ |
| 575 | { UCHAR_MAMEKEY(END), "End", NULL }, /* End key */ |
| 576 | { UCHAR_MAMEKEY(PGUP), "Page Up", NULL }, /* Page Up key */ |
| 577 | { UCHAR_MAMEKEY(PGDN), "Page Down", NULL }, /* Page Down key */ |
| 578 | { UCHAR_MAMEKEY(LEFT), "Cursor Left", NULL }, /* Cursor Left */ |
| 579 | { UCHAR_MAMEKEY(RIGHT), "Cursor Right", NULL }, /* Cursor Right */ |
| 580 | { UCHAR_MAMEKEY(UP), "Cursor Up", NULL }, /* Cursor Up */ |
| 581 | { UCHAR_MAMEKEY(DOWN), "Cursor Down", NULL }, /* Cursor Down */ |
| 582 | { UCHAR_MAMEKEY(0_PAD), "Keypad 0", NULL }, /* 0 on the numeric keypad */ |
| 583 | { UCHAR_MAMEKEY(1_PAD), "Keypad 1", NULL }, /* 1 on the numeric keypad */ |
| 584 | { UCHAR_MAMEKEY(2_PAD), "Keypad 2", NULL }, /* 2 on the numeric keypad */ |
| 585 | { UCHAR_MAMEKEY(3_PAD), "Keypad 3", NULL }, /* 3 on the numeric keypad */ |
| 586 | { UCHAR_MAMEKEY(4_PAD), "Keypad 4", NULL }, /* 4 on the numeric keypad */ |
| 587 | { UCHAR_MAMEKEY(5_PAD), "Keypad 5", NULL }, /* 5 on the numeric keypad */ |
| 588 | { UCHAR_MAMEKEY(6_PAD), "Keypad 6", NULL }, /* 6 on the numeric keypad */ |
| 589 | { UCHAR_MAMEKEY(7_PAD), "Keypad 7", NULL }, /* 7 on the numeric keypad */ |
| 590 | { UCHAR_MAMEKEY(8_PAD), "Keypad 8", NULL }, /* 8 on the numeric keypad */ |
| 591 | { UCHAR_MAMEKEY(9_PAD), "Keypad 9", NULL }, /* 9 on the numeric keypad */ |
| 592 | { UCHAR_MAMEKEY(SLASH_PAD), "Keypad /", NULL }, /* / on the numeric keypad */ |
| 593 | { UCHAR_MAMEKEY(ASTERISK), "Keypad *", NULL }, /* * on the numeric keypad */ |
| 594 | { UCHAR_MAMEKEY(MINUS_PAD), "Keypad -", NULL }, /* - on the numeric Keypad */ |
| 595 | { UCHAR_MAMEKEY(PLUS_PAD), "Keypad +", NULL }, /* + on the numeric Keypad */ |
| 596 | { UCHAR_MAMEKEY(DEL_PAD), "Keypad .", NULL }, /* . on the numeric keypad */ |
| 597 | { UCHAR_MAMEKEY(ENTER_PAD), "Keypad Enter", NULL }, /* Enter on the numeric keypad */ |
| 598 | { UCHAR_MAMEKEY(PRTSCR), "Print Screen", NULL }, /* Print Screen key */ |
| 599 | { UCHAR_MAMEKEY(PAUSE), "Pause", NULL }, /* Pause key */ |
| 600 | { UCHAR_MAMEKEY(LSHIFT), "Left Shift", NULL }, /* Left Shift key */ |
| 601 | { UCHAR_MAMEKEY(RSHIFT), "Right Shift", NULL }, /* Right Shift key */ |
| 602 | { UCHAR_MAMEKEY(LCONTROL), "Left Ctrl", NULL }, /* Left Control key */ |
| 603 | { UCHAR_MAMEKEY(RCONTROL), "Right Ctrl", NULL }, /* Right Control key */ |
| 604 | { UCHAR_MAMEKEY(LALT), "Left Alt", NULL }, /* Left Alt key */ |
| 605 | { UCHAR_MAMEKEY(RALT), "Right Alt", NULL }, /* Right Alt key */ |
| 606 | { UCHAR_MAMEKEY(SCRLOCK), "Scroll Lock", NULL }, /* Scroll Lock key */ |
| 607 | { UCHAR_MAMEKEY(NUMLOCK), "Num Lock", NULL }, /* Num Lock key */ |
| 608 | { UCHAR_MAMEKEY(CAPSLOCK), "Caps Lock", NULL }, /* Caps Lock key */ |
| 609 | { UCHAR_MAMEKEY(LWIN), "Left Win", NULL }, /* Left Win key */ |
| 610 | { UCHAR_MAMEKEY(RWIN), "Right Win", NULL }, /* Right Win key */ |
| 611 | { UCHAR_MAMEKEY(MENU), "Menu", NULL }, /* Menu key */ |
| 612 | { UCHAR_MAMEKEY(CANCEL), "Break", NULL } /* Break/Pause key */ |
| 613 | }; |
| 294 | 614 | |
| 615 | static inputx_code *codes; |
| 616 | static key_buffer *keybuffer; |
| 617 | static emu_timer *inputx_timer; |
| 618 | static int (*queue_chars)(const unicode_char *text, size_t text_len); |
| 619 | static int (*accept_char)(unicode_char ch); |
| 620 | static int (*charqueue_empty)(void); |
| 621 | static attotime current_rate; |
| 622 | |
| 623 | static TIMER_CALLBACK(inputx_timerproc); |
| 624 | |
| 625 | |
| 626 | /* Debugging commands and handlers. */ |
| 627 | static void execute_input(running_machine *machine, int ref, int params, const char *param[]); |
| 628 | static void execute_dumpkbd(running_machine *machine, int ref, int params, const char *param[]); |
| 629 | |
| 295 | 630 | /*************************************************************************** |
| 296 | 631 | COMMON SHARED STRINGS |
| 297 | 632 | ***************************************************************************/ |
| r7335 | r7336 | |
| 1563 | 1898 | } |
| 1564 | 1899 | } |
| 1565 | 1900 | |
| 1901 | /*------------------------------------------------- |
| 1902 | get_keyboard_code - accesses a particular |
| 1903 | keyboard code |
| 1904 | -------------------------------------------------*/ |
| 1566 | 1905 | |
| 1906 | static unicode_char get_keyboard_code(const input_field_config *field, int i) |
| 1907 | { |
| 1908 | unicode_char ch = field->chars[i]; |
| 1909 | |
| 1910 | /* special hack to allow for PORT_CODE('\xA3') */ |
| 1911 | if ((ch >= 0xFFFFFF80) && (ch <= 0xFFFFFFFF)) |
| 1912 | ch &= 0xFF; |
| 1913 | return ch; |
| 1914 | } |
| 1915 | |
| 1567 | 1916 | /*------------------------------------------------- |
| 1917 | get_keyboard_key_name - builds the name of |
| 1918 | a key based on natural keyboard characters |
| 1919 | -------------------------------------------------*/ |
| 1920 | |
| 1921 | static astring *get_keyboard_key_name(const input_field_config *field) |
| 1922 | { |
| 1923 | astring *result = astring_alloc(); |
| 1924 | int i; |
| 1925 | unicode_char ch; |
| 1926 | |
| 1927 | |
| 1928 | /* loop through each character on the field*/ |
| 1929 | for (i = 0; i < ARRAY_LENGTH(field->chars) && (field->chars[i] != '\0'); i++) |
| 1930 | { |
| 1931 | ch = get_keyboard_code(field, i); |
| 1932 | astring_printf(result, "%s%-*s ", astring_c(result), MAX(SPACE_COUNT - 1, 0), inputx_key_name(ch)); |
| 1933 | } |
| 1934 | |
| 1935 | /* trim extra spaces */ |
| 1936 | astring_trimspace(result); |
| 1937 | |
| 1938 | /* special case */ |
| 1939 | if (astring_len(result) == 0) |
| 1940 | astring_cpyc(result, "Unnamed Key"); |
| 1941 | |
| 1942 | return result; |
| 1943 | } |
| 1944 | |
| 1945 | /*------------------------------------------------- |
| 1568 | 1946 | init_port_state - initialize the live port |
| 1569 | 1947 | states based on the tokens |
| 1570 | 1948 | -------------------------------------------------*/ |
| r7335 | r7336 | |
| 1639 | 2017 | writedevicetail = &(*writedevicetail)->next; |
| 1640 | 2018 | } |
| 1641 | 2019 | |
| 1642 | | #ifdef MESS |
| 1643 | | /* MESS-specific code to name keyboard key names */ |
| 2020 | /* Name keyboard key names */ |
| 1644 | 2021 | if ((field->type == IPT_KEYBOARD) && (field->name == NULL)) |
| 1645 | 2022 | { |
| 1646 | | astring *name = mess_get_keyboard_key_name(field); |
| 2023 | astring *name = get_keyboard_key_name(field); |
| 1647 | 2024 | if (name != NULL) |
| 1648 | 2025 | { |
| 1649 | 2026 | field->state->name = auto_strdup(machine, astring_c(name)); |
| 1650 | 2027 | astring_free(name); |
| 1651 | 2028 | } |
| 1652 | 2029 | } |
| 1653 | | #endif /* MESS */ |
| 1654 | 2030 | } |
| 1655 | 2031 | } |
| 1656 | 2032 | |
| r7335 | r7336 | |
| 1952 | 2328 | frame_update(machine); |
| 1953 | 2329 | } |
| 1954 | 2330 | |
| 2331 | static key_buffer *get_buffer(running_machine *machine) |
| 2332 | { |
| 2333 | assert(inputx_can_post(machine)); |
| 2334 | return (key_buffer *) keybuffer; |
| 2335 | } |
| 1955 | 2336 | |
| 2337 | |
| 2338 | |
| 2339 | static const inputx_code *find_code(unicode_char ch) |
| 2340 | { |
| 2341 | int i; |
| 2342 | |
| 2343 | assert(codes); |
| 2344 | for (i = 0; codes[i].ch; i++) |
| 2345 | { |
| 2346 | if (codes[i].ch == ch) |
| 2347 | return &codes[i]; |
| 2348 | } |
| 2349 | return NULL; |
| 2350 | } |
| 2351 | |
| 1956 | 2352 | /*------------------------------------------------- |
| 2353 | input_port_update_hook - hook function |
| 2354 | called from core to allow for natural keyboard |
| 2355 | -------------------------------------------------*/ |
| 2356 | |
| 2357 | static void input_port_update_hook(running_machine *machine, const input_port_config *port, input_port_value *digital) |
| 2358 | { |
| 2359 | const key_buffer *keybuf; |
| 2360 | const inputx_code *code; |
| 2361 | unicode_char ch; |
| 2362 | int i; |
| 2363 | UINT32 value; |
| 2364 | |
| 2365 | if (inputx_can_post(machine)) |
| 2366 | { |
| 2367 | keybuf = get_buffer(machine); |
| 2368 | |
| 2369 | /* is the key down right now? */ |
| 2370 | if (keybuf->status_keydown && (keybuf->begin_pos != keybuf->end_pos)) |
| 2371 | { |
| 2372 | /* identify the character that is down right now, and its component codes */ |
| 2373 | ch = keybuf->buffer[keybuf->begin_pos]; |
| 2374 | code = find_code(ch); |
| 2375 | |
| 2376 | /* loop through this character's component codes */ |
| 2377 | if (code != NULL) |
| 2378 | { |
| 2379 | for (i = 0; i < ARRAY_LENGTH(code->field) && (code->field[i] != NULL); i++) |
| 2380 | { |
| 2381 | if (code->field[i]->port == port) |
| 2382 | { |
| 2383 | value = code->field[i]->mask; |
| 2384 | *digital |= value; |
| 2385 | } |
| 2386 | } |
| 2387 | } |
| 2388 | } |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | |
| 2393 | /*------------------------------------------------- |
| 1957 | 2394 | frame_update - core logic for per-frame input |
| 1958 | 2395 | port updating |
| 1959 | 2396 | -------------------------------------------------*/ |
| r7335 | r7336 | |
| 2024 | 2461 | port->state->digital |= field->mask; |
| 2025 | 2462 | } |
| 2026 | 2463 | |
| 2027 | | #ifdef MESS |
| 2028 | 2464 | /* hook for MESS's natural keyboard support */ |
| 2029 | | mess_input_port_update_hook(machine, port, &port->state->digital); |
| 2030 | | #endif /* MESS */ |
| 2031 | | |
| 2465 | input_port_update_hook(machine, port, &port->state->digital); |
| 2466 | |
| 2032 | 2467 | /* handle playback/record */ |
| 2033 | 2468 | playback_port(port); |
| 2034 | 2469 | record_port(port); |
| r7335 | r7336 | |
| 2299 | 2734 | changed = TRUE; |
| 2300 | 2735 | } |
| 2301 | 2736 | |
| 2302 | | #ifdef MESS |
| 2303 | | /* (MESS-specific) check for disabled keyboard */ |
| 2304 | | if (field->type == IPT_KEYBOARD && ui_mess_keyboard_disabled(field->port->machine)) |
| 2737 | if (field->type == IPT_KEYBOARD && ui_get_use_natural_keyboard(field->port->machine)) |
| 2305 | 2738 | return FALSE; |
| 2306 | | #endif /* MESS */ |
| 2307 | 2739 | |
| 2308 | 2740 | /* if this is a switch-down event, handle impulse and toggle */ |
| 2309 | 2741 | if (changed && curstate) |
| r7335 | r7336 | |
| 4225 | 4657 | } |
| 4226 | 4658 | return have_keyboard; |
| 4227 | 4659 | } |
| 4660 | |
| 4661 | /*************************************************************************** |
| 4662 | MISCELLANEOUS |
| 4663 | ***************************************************************************/ |
| 4664 | |
| 4665 | /*------------------------------------------------- |
| 4666 | find_charinfo - looks up information about a |
| 4667 | particular character |
| 4668 | -------------------------------------------------*/ |
| 4669 | |
| 4670 | static const char_info *find_charinfo(unicode_char target_char) |
| 4671 | { |
| 4672 | int low = 0; |
| 4673 | int high = ARRAY_LENGTH(charinfo); |
| 4674 | int i; |
| 4675 | unicode_char ch; |
| 4676 | |
| 4677 | /* perform a simple binary search to find the proper alternate */ |
| 4678 | while(high > low) |
| 4679 | { |
| 4680 | i = (high + low) / 2; |
| 4681 | ch = charinfo[i].ch; |
| 4682 | if (ch < target_char) |
| 4683 | low = i + 1; |
| 4684 | else if (ch > target_char) |
| 4685 | high = i; |
| 4686 | else |
| 4687 | return &charinfo[i]; |
| 4688 | } |
| 4689 | return NULL; |
| 4690 | } |
| 4691 | |
| 4692 | |
| 4693 | |
| 4694 | /*************************************************************************** |
| 4695 | CODE ASSEMBLING |
| 4696 | ***************************************************************************/ |
| 4697 | |
| 4698 | /*------------------------------------------------- |
| 4699 | code_point_string - obtain a string representation of a |
| 4700 | given code; used for logging and debugging |
| 4701 | -------------------------------------------------*/ |
| 4702 | |
| 4703 | static const char *code_point_string(running_machine *machine, unicode_char ch) |
| 4704 | { |
| 4705 | static char buf[16]; |
| 4706 | const char *result = buf; |
| 4707 | |
| 4708 | switch(ch) |
| 4709 | { |
| 4710 | /* check some magic values */ |
| 4711 | case '\0': strcpy(buf, "\\0"); break; |
| 4712 | case '\r': strcpy(buf, "\\r"); break; |
| 4713 | case '\n': strcpy(buf, "\\n"); break; |
| 4714 | case '\t': strcpy(buf, "\\t"); break; |
| 4715 | |
| 4716 | default: |
| 4717 | if ((ch >= 32) && (ch < 128)) |
| 4718 | { |
| 4719 | /* seven bit ASCII is easy */ |
| 4720 | buf[0] = (char) ch; |
| 4721 | buf[1] = '\0'; |
| 4722 | } |
| 4723 | else if (ch >= UCHAR_MAMEKEY_BEGIN) |
| 4724 | { |
| 4725 | /* try to obtain a codename with input_code_name(); this can result in an empty string */ |
| 4726 | astring astr; |
| 4727 | input_code_name(machine, astr, (input_code) ch - UCHAR_MAMEKEY_BEGIN); |
| 4728 | snprintf(buf, ARRAY_LENGTH(buf), "%s", astr.cstr()); |
| 4729 | } |
| 4730 | else |
| 4731 | { |
| 4732 | /* empty string; resolve later */ |
| 4733 | buf[0] = '\0'; |
| 4734 | } |
| 4735 | |
| 4736 | /* did we fail to resolve? if so, we have a last resort */ |
| 4737 | if (buf[0] == '\0') |
| 4738 | snprintf(buf, ARRAY_LENGTH(buf), "U+%04X", (unsigned) ch); |
| 4739 | break; |
| 4740 | } |
| 4741 | return result; |
| 4742 | } |
| 4743 | |
| 4744 | |
| 4745 | /*------------------------------------------------- |
| 4746 | scan_keys - scans through input ports and |
| 4747 | sets up natural keyboard input mapping |
| 4748 | -------------------------------------------------*/ |
| 4749 | |
| 4750 | static int scan_keys(running_machine *machine, const input_port_config *portconfig, inputx_code *codes, input_port_config_c *ports, input_field_config_c *shift_ports, int keys, int shift) |
| 4751 | { |
| 4752 | int code_count = 0; |
| 4753 | const input_port_config *port; |
| 4754 | const input_field_config *field; |
| 4755 | unicode_char code; |
| 4756 | |
| 4757 | assert(keys < NUM_SIMUL_KEYS); |
| 4758 | |
| 4759 | for (port = portconfig; port != NULL; port = port->next) |
| 4760 | { |
| 4761 | for (field = port->fieldlist; field != NULL; field = field->next) |
| 4762 | { |
| 4763 | if (field->type == IPT_KEYBOARD) |
| 4764 | { |
| 4765 | code = get_keyboard_code(field, shift); |
| 4766 | if (code != 0) |
| 4767 | { |
| 4768 | /* is this a shifter key? */ |
| 4769 | if ((code >= UCHAR_SHIFT_BEGIN) && (code <= UCHAR_SHIFT_END)) |
| 4770 | { |
| 4771 | shift_ports[keys] = field; |
| 4772 | code_count += scan_keys(machine, |
| 4773 | portconfig, |
| 4774 | codes ? &codes[code_count] : NULL, |
| 4775 | ports, |
| 4776 | shift_ports, |
| 4777 | keys+1, |
| 4778 | code - UCHAR_SHIFT_1 + 1); |
| 4779 | } |
| 4780 | else |
| 4781 | { |
| 4782 | /* not a shifter key; record normally */ |
| 4783 | if (codes) |
| 4784 | { |
| 4785 | /* if we have a destination, record the codes used here */ |
| 4786 | memcpy((void *) codes[code_count].field, shift_ports, sizeof(shift_ports[0]) * keys); |
| 4787 | codes[code_count].ch = code; |
| 4788 | codes[code_count].field[keys] = field; |
| 4789 | } |
| 4790 | |
| 4791 | /* increment the count */ |
| 4792 | code_count++; |
| 4793 | |
| 4794 | if (LOG_INPUTX) |
| 4795 | logerror("inputx: code=%i (%s) port=%p field->name='%s'\n", (int) code, code_point_string(machine, code), port, field->name); |
| 4796 | } |
| 4797 | } |
| 4798 | } |
| 4799 | } |
| 4800 | } |
| 4801 | return code_count; |
| 4802 | } |
| 4803 | |
| 4804 | |
| 4805 | |
| 4806 | /*------------------------------------------------- |
| 4807 | build_codes - given an input port table, create |
| 4808 | a input code table useful for mapping unicode |
| 4809 | chars |
| 4810 | -------------------------------------------------*/ |
| 4811 | |
| 4812 | static inputx_code *build_codes(running_machine *machine, const input_port_config *portconfig) |
| 4813 | { |
| 4814 | inputx_code *codes = NULL; |
| 4815 | const input_port_config *ports[NUM_SIMUL_KEYS]; |
| 4816 | const input_field_config *fields[NUM_SIMUL_KEYS]; |
| 4817 | int code_count; |
| 4818 | |
| 4819 | /* first count the number of codes */ |
| 4820 | code_count = scan_keys(machine, portconfig, NULL, ports, fields, 0, 0); |
| 4821 | if (code_count > 0) |
| 4822 | { |
| 4823 | /* allocate the codes */ |
| 4824 | codes = auto_alloc_array_clear(machine, inputx_code, code_count + 1); |
| 4825 | |
| 4826 | /* and populate them */ |
| 4827 | scan_keys(machine, portconfig, codes, ports, fields, 0, 0); |
| 4828 | } |
| 4829 | return codes; |
| 4830 | } |
| 4831 | |
| 4832 | |
| 4833 | |
| 4834 | /*************************************************************************** |
| 4835 | VALIDITY CHECKS |
| 4836 | ***************************************************************************/ |
| 4837 | |
| 4838 | /*------------------------------------------------- |
| 4839 | validate_natural_keyboard_statics - |
| 4840 | validates natural keyboard static data |
| 4841 | -------------------------------------------------*/ |
| 4842 | |
| 4843 | int validate_natural_keyboard_statics(void) |
| 4844 | { |
| 4845 | int i; |
| 4846 | int error = FALSE; |
| 4847 | unicode_char last_char = 0; |
| 4848 | const char_info *ci; |
| 4849 | |
| 4850 | /* check to make sure that charinfo is in order */ |
| 4851 | for (i = 0; i < ARRAY_LENGTH(charinfo); i++) |
| 4852 | { |
| 4853 | if (last_char >= charinfo[i].ch) |
| 4854 | { |
| 4855 | mame_printf_error("inputx: charinfo is out of order; 0x%08x should be higher than 0x%08x\n", charinfo[i].ch, last_char); |
| 4856 | error = TRUE; |
| 4857 | } |
| 4858 | last_char = charinfo[i].ch; |
| 4859 | } |
| 4860 | |
| 4861 | /* check to make sure that I can look up everything on alternate_charmap */ |
| 4862 | for (i = 0; i < ARRAY_LENGTH(charinfo); i++) |
| 4863 | { |
| 4864 | ci = find_charinfo(charinfo[i].ch); |
| 4865 | if (ci != &charinfo[i]) |
| 4866 | { |
| 4867 | mame_printf_error("inputx: expected find_charinfo(0x%08x) to work properly\n", charinfo[i].ch); |
| 4868 | error = TRUE; |
| 4869 | } |
| 4870 | } |
| 4871 | return error; |
| 4872 | } |
| 4873 | |
| 4874 | |
| 4875 | |
| 4876 | /*************************************************************************** |
| 4877 | CORE IMPLEMENTATION |
| 4878 | ***************************************************************************/ |
| 4879 | |
| 4880 | static void clear_keybuffer(running_machine *machine) |
| 4881 | { |
| 4882 | keybuffer = NULL; |
| 4883 | queue_chars = NULL; |
| 4884 | codes = NULL; |
| 4885 | } |
| 4886 | |
| 4887 | |
| 4888 | |
| 4889 | static void setup_keybuffer(running_machine *machine) |
| 4890 | { |
| 4891 | inputx_timer = timer_alloc(machine, inputx_timerproc, NULL); |
| 4892 | keybuffer = auto_alloc_clear(machine, key_buffer); |
| 4893 | add_exit_callback(machine, clear_keybuffer); |
| 4894 | } |
| 4895 | |
| 4896 | |
| 4897 | |
| 4898 | void inputx_init(running_machine *machine) |
| 4899 | { |
| 4900 | codes = NULL; |
| 4901 | inputx_timer = NULL; |
| 4902 | queue_chars = NULL; |
| 4903 | accept_char = NULL; |
| 4904 | charqueue_empty = NULL; |
| 4905 | keybuffer = NULL; |
| 4906 | |
| 4907 | if (machine->debug_flags & DEBUG_FLAG_ENABLED) |
| 4908 | { |
| 4909 | debug_console_register_command(machine, "input", CMDFLAG_NONE, 0, 1, 1, execute_input); |
| 4910 | debug_console_register_command(machine, "dumpkbd", CMDFLAG_NONE, 0, 0, 1, execute_dumpkbd); |
| 4911 | } |
| 4912 | |
| 4913 | /* posting keys directly only makes sense for a computer */ |
| 4914 | if (input_machine_have_keyboard(machine)) |
| 4915 | { |
| 4916 | codes = build_codes(machine, machine->portlist.first()); |
| 4917 | setup_keybuffer(machine); |
| 4918 | } |
| 4919 | } |
| 4920 | |
| 4921 | |
| 4922 | |
| 4923 | void inputx_setup_natural_keyboard( |
| 4924 | int (*queue_chars_)(const unicode_char *text, size_t text_len), |
| 4925 | int (*accept_char_)(unicode_char ch), |
| 4926 | int (*charqueue_empty_)(void)) |
| 4927 | { |
| 4928 | queue_chars = queue_chars_; |
| 4929 | accept_char = accept_char_; |
| 4930 | charqueue_empty = charqueue_empty_; |
| 4931 | } |
| 4932 | |
| 4933 | int inputx_can_post(running_machine *machine) |
| 4934 | { |
| 4935 | return queue_chars || codes; |
| 4936 | } |
| 4937 | |
| 4938 | |
| 4939 | static int can_post_key_directly(unicode_char ch) |
| 4940 | { |
| 4941 | int rc = FALSE; |
| 4942 | const inputx_code *code; |
| 4943 | |
| 4944 | if (queue_chars) |
| 4945 | { |
| 4946 | rc = accept_char ? accept_char(ch) : TRUE; |
| 4947 | } |
| 4948 | else |
| 4949 | { |
| 4950 | code = find_code(ch); |
| 4951 | if (code) |
| 4952 | rc = code->field[0] != NULL; |
| 4953 | } |
| 4954 | return rc; |
| 4955 | } |
| 4956 | |
| 4957 | |
| 4958 | |
| 4959 | static int can_post_key_alternate(unicode_char ch) |
| 4960 | { |
| 4961 | const char *s; |
| 4962 | const char_info *ci; |
| 4963 | unicode_char uchar; |
| 4964 | int rc; |
| 4965 | |
| 4966 | ci = find_charinfo(ch); |
| 4967 | s = ci ? ci->alternate : NULL; |
| 4968 | if (!s) |
| 4969 | return 0; |
| 4970 | |
| 4971 | while(*s) |
| 4972 | { |
| 4973 | rc = uchar_from_utf8(&uchar, s, strlen(s)); |
| 4974 | if (rc <= 0) |
| 4975 | return 0; |
| 4976 | if (!can_post_key_directly(uchar)) |
| 4977 | return 0; |
| 4978 | s += rc; |
| 4979 | } |
| 4980 | return 1; |
| 4981 | } |
| 4982 | |
| 4983 | |
| 4984 | |
| 4985 | int inputx_can_post_key(running_machine *machine, unicode_char ch) |
| 4986 | { |
| 4987 | return inputx_can_post(machine) && (can_post_key_directly(ch) || can_post_key_alternate(ch)); |
| 4988 | } |
| 4989 | |
| 4990 | |
| 4991 | |
| 4992 | static attotime choose_delay(unicode_char ch) |
| 4993 | { |
| 4994 | attoseconds_t delay = 0; |
| 4995 | |
| 4996 | if (attotime_compare(current_rate, attotime_zero) != 0) |
| 4997 | return current_rate; |
| 4998 | |
| 4999 | if (queue_chars) |
| 5000 | { |
| 5001 | /* systems with queue_chars can afford a much smaller delay */ |
| 5002 | delay = DOUBLE_TO_ATTOSECONDS(0.01); |
| 5003 | } |
| 5004 | else |
| 5005 | { |
| 5006 | switch(ch) { |
| 5007 | case '\r': |
| 5008 | delay = DOUBLE_TO_ATTOSECONDS(0.2); |
| 5009 | break; |
| 5010 | |
| 5011 | default: |
| 5012 | delay = DOUBLE_TO_ATTOSECONDS(0.05); |
| 5013 | break; |
| 5014 | } |
| 5015 | } |
| 5016 | return attotime_make(0, delay); |
| 5017 | } |
| 5018 | |
| 5019 | |
| 5020 | |
| 5021 | static void internal_post_key(running_machine *machine, unicode_char ch) |
| 5022 | { |
| 5023 | key_buffer *keybuf; |
| 5024 | |
| 5025 | keybuf = get_buffer(machine); |
| 5026 | |
| 5027 | /* need to start up the timer? */ |
| 5028 | if (keybuf->begin_pos == keybuf->end_pos) |
| 5029 | { |
| 5030 | timer_adjust_oneshot(inputx_timer, choose_delay(ch), 0); |
| 5031 | keybuf->status_keydown = 0; |
| 5032 | } |
| 5033 | |
| 5034 | keybuf->buffer[keybuf->end_pos++] = ch; |
| 5035 | keybuf->end_pos %= ARRAY_LENGTH(keybuf->buffer); |
| 5036 | } |
| 5037 | |
| 5038 | |
| 5039 | |
| 5040 | static int buffer_full(running_machine *machine) |
| 5041 | { |
| 5042 | key_buffer *keybuf; |
| 5043 | keybuf = get_buffer(machine); |
| 5044 | return ((keybuf->end_pos + 1) % ARRAY_LENGTH(keybuf->buffer)) == keybuf->begin_pos; |
| 5045 | } |
| 5046 | |
| 5047 | |
| 5048 | |
| 5049 | void inputx_postn_rate(running_machine *machine, const unicode_char *text, size_t text_len, attotime rate) |
| 5050 | { |
| 5051 | int last_cr = 0; |
| 5052 | unicode_char ch; |
| 5053 | const char *s; |
| 5054 | const char_info *ci; |
| 5055 | const inputx_code *code; |
| 5056 | |
| 5057 | current_rate = rate; |
| 5058 | |
| 5059 | if (inputx_can_post(machine)) |
| 5060 | { |
| 5061 | while((text_len > 0) && !buffer_full(machine)) |
| 5062 | { |
| 5063 | ch = *(text++); |
| 5064 | text_len--; |
| 5065 | |
| 5066 | /* change all eolns to '\r' */ |
| 5067 | if ((ch != '\n') || !last_cr) |
| 5068 | { |
| 5069 | if (ch == '\n') |
| 5070 | ch = '\r'; |
| 5071 | else |
| 5072 | last_cr = (ch == '\r'); |
| 5073 | |
| 5074 | if (LOG_INPUTX) |
| 5075 | { |
| 5076 | code = find_code(ch); |
| 5077 | logerror("inputx_postn(): code=%i (%s) field->name='%s'\n", (int) ch, code_point_string(machine, ch), (code && code->field[0]) ? code->field[0]->name : "<null>"); |
| 5078 | } |
| 5079 | |
| 5080 | if (can_post_key_directly(ch)) |
| 5081 | { |
| 5082 | /* we can post this key in the queue directly */ |
| 5083 | internal_post_key(machine, ch); |
| 5084 | } |
| 5085 | else if (can_post_key_alternate(ch)) |
| 5086 | { |
| 5087 | /* we can post this key with an alternate representation */ |
| 5088 | ci = find_charinfo(ch); |
| 5089 | assert(ci && ci->alternate); |
| 5090 | s = ci->alternate; |
| 5091 | while(*s) |
| 5092 | { |
| 5093 | s += uchar_from_utf8(&ch, s, strlen(s)); |
| 5094 | internal_post_key(machine, ch); |
| 5095 | } |
| 5096 | } |
| 5097 | } |
| 5098 | else |
| 5099 | { |
| 5100 | last_cr = 0; |
| 5101 | } |
| 5102 | } |
| 5103 | } |
| 5104 | } |
| 5105 | |
| 5106 | |
| 5107 | |
| 5108 | static TIMER_CALLBACK(inputx_timerproc) |
| 5109 | { |
| 5110 | key_buffer *keybuf; |
| 5111 | attotime delay; |
| 5112 | |
| 5113 | keybuf = get_buffer(machine); |
| 5114 | |
| 5115 | if (queue_chars) |
| 5116 | { |
| 5117 | /* the driver has a queue_chars handler */ |
| 5118 | while((keybuf->begin_pos != keybuf->end_pos) && queue_chars(&keybuf->buffer[keybuf->begin_pos], 1)) |
| 5119 | { |
| 5120 | keybuf->begin_pos++; |
| 5121 | keybuf->begin_pos %= ARRAY_LENGTH(keybuf->buffer); |
| 5122 | |
| 5123 | if (attotime_compare(current_rate, attotime_zero) != 0) |
| 5124 | break; |
| 5125 | } |
| 5126 | } |
| 5127 | else |
| 5128 | { |
| 5129 | /* the driver does not have a queue_chars handler */ |
| 5130 | if (keybuf->status_keydown) |
| 5131 | { |
| 5132 | keybuf->status_keydown = FALSE; |
| 5133 | keybuf->begin_pos++; |
| 5134 | keybuf->begin_pos %= ARRAY_LENGTH(keybuf->buffer); |
| 5135 | } |
| 5136 | else |
| 5137 | { |
| 5138 | keybuf->status_keydown = TRUE; |
| 5139 | } |
| 5140 | } |
| 5141 | |
| 5142 | /* need to make sure timerproc is called again if buffer not empty */ |
| 5143 | if (keybuf->begin_pos != keybuf->end_pos) |
| 5144 | { |
| 5145 | delay = choose_delay(keybuf->buffer[keybuf->begin_pos]); |
| 5146 | timer_adjust_oneshot(inputx_timer, delay, 0); |
| 5147 | } |
| 5148 | } |
| 5149 | |
| 5150 | /*------------------------------------------------- |
| 5151 | inputx_key_name - returns the name of a |
| 5152 | specific key |
| 5153 | -------------------------------------------------*/ |
| 5154 | |
| 5155 | const char *inputx_key_name(unicode_char ch) |
| 5156 | { |
| 5157 | static char buf[UTF8_CHAR_MAX + 1]; |
| 5158 | const char_info *ci; |
| 5159 | const char *result; |
| 5160 | int pos; |
| 5161 | |
| 5162 | ci = find_charinfo(ch); |
| 5163 | result = ci ? ci->name : NULL; |
| 5164 | |
| 5165 | if (ci && ci->name) |
| 5166 | { |
| 5167 | result = ci->name; |
| 5168 | } |
| 5169 | else |
| 5170 | { |
| 5171 | if ((ch > 0x7F) || isprint(ch)) |
| 5172 | { |
| 5173 | pos = utf8_from_uchar(buf, ARRAY_LENGTH(buf), ch); |
| 5174 | buf[pos] = '\0'; |
| 5175 | result = buf; |
| 5176 | } |
| 5177 | else |
| 5178 | result = "???"; |
| 5179 | } |
| 5180 | return result; |
| 5181 | } |
| 5182 | |
| 5183 | |
| 5184 | |
| 5185 | /* --------------------------------------------------------------------- */ |
| 5186 | |
| 5187 | int inputx_is_posting(running_machine *machine) |
| 5188 | { |
| 5189 | const key_buffer *keybuf; |
| 5190 | keybuf = get_buffer(machine); |
| 5191 | return (keybuf->begin_pos != keybuf->end_pos) || (charqueue_empty && !charqueue_empty()); |
| 5192 | } |
| 5193 | |
| 5194 | |
| 5195 | |
| 5196 | /*************************************************************************** |
| 5197 | |
| 5198 | Coded input |
| 5199 | |
| 5200 | ***************************************************************************/ |
| 5201 | |
| 5202 | void inputx_postn_coded_rate(running_machine *machine, const char *text, size_t text_len, attotime rate) |
| 5203 | { |
| 5204 | size_t i, j, key_len, increment; |
| 5205 | unicode_char ch; |
| 5206 | |
| 5207 | static const struct |
| 5208 | { |
| 5209 | const char *key; |
| 5210 | unicode_char code; |
| 5211 | } codes[] = |
| 5212 | { |
| 5213 | { "BACKSPACE", 8 }, |
| 5214 | { "BS", 8 }, |
| 5215 | { "BKSP", 8 }, |
| 5216 | { "DEL", UCHAR_MAMEKEY(DEL) }, |
| 5217 | { "DELETE", UCHAR_MAMEKEY(DEL) }, |
| 5218 | { "END", UCHAR_MAMEKEY(END) }, |
| 5219 | { "ENTER", 13 }, |
| 5220 | { "ESC", '\033' }, |
| 5221 | { "HOME", UCHAR_MAMEKEY(HOME) }, |
| 5222 | { "INS", UCHAR_MAMEKEY(INSERT) }, |
| 5223 | { "INSERT", UCHAR_MAMEKEY(INSERT) }, |
| 5224 | { "PGDN", UCHAR_MAMEKEY(PGDN) }, |
| 5225 | { "PGUP", UCHAR_MAMEKEY(PGUP) }, |
| 5226 | { "SPACE", 32 }, |
| 5227 | { "TAB", 9 }, |
| 5228 | { "F1", UCHAR_MAMEKEY(F1) }, |
| 5229 | { "F2", UCHAR_MAMEKEY(F2) }, |
| 5230 | { "F3", UCHAR_MAMEKEY(F3) }, |
| 5231 | { "F4", UCHAR_MAMEKEY(F4) }, |
| 5232 | { "F5", UCHAR_MAMEKEY(F5) }, |
| 5233 | { "F6", UCHAR_MAMEKEY(F6) }, |
| 5234 | { "F7", UCHAR_MAMEKEY(F7) }, |
| 5235 | { "F8", UCHAR_MAMEKEY(F8) }, |
| 5236 | { "F9", UCHAR_MAMEKEY(F9) }, |
| 5237 | { "F10", UCHAR_MAMEKEY(F10) }, |
| 5238 | { "F11", UCHAR_MAMEKEY(F11) }, |
| 5239 | { "F12", UCHAR_MAMEKEY(F12) }, |
| 5240 | { "QUOTE", '\"' } |
| 5241 | }; |
| 5242 | |
| 5243 | i = 0; |
| 5244 | while(i < text_len) |
| 5245 | { |
| 5246 | ch = text[i]; |
| 5247 | increment = 1; |
| 5248 | |
| 5249 | if (ch == '{') |
| 5250 | { |
| 5251 | for (j = 0; j < ARRAY_LENGTH(codes); j++) |
| 5252 | { |
| 5253 | key_len = strlen(codes[j].key); |
| 5254 | if (i + key_len + 2 <= text_len) |
| 5255 | { |
| 5256 | if (!memcmp(codes[j].key, &text[i + 1], key_len) && (text[i + key_len + 1] == '}')) |
| 5257 | { |
| 5258 | ch = codes[j].code; |
| 5259 | increment = key_len + 2; |
| 5260 | } |
| 5261 | } |
| 5262 | } |
| 5263 | } |
| 5264 | |
| 5265 | if (ch) |
| 5266 | inputx_postc_rate(machine, ch, rate); |
| 5267 | i += increment; |
| 5268 | } |
| 5269 | } |
| 5270 | |
| 5271 | |
| 5272 | |
| 5273 | /*************************************************************************** |
| 5274 | |
| 5275 | Alternative calls |
| 5276 | |
| 5277 | ***************************************************************************/ |
| 5278 | |
| 5279 | void inputx_postn(running_machine *machine, const unicode_char *text, size_t text_len) |
| 5280 | { |
| 5281 | inputx_postn_rate(machine, text, text_len, attotime_make(0, 0)); |
| 5282 | } |
| 5283 | |
| 5284 | |
| 5285 | |
| 5286 | void inputx_post_rate(running_machine *machine, const unicode_char *text, attotime rate) |
| 5287 | { |
| 5288 | size_t len = 0; |
| 5289 | while(text[len]) |
| 5290 | len++; |
| 5291 | inputx_postn_rate(machine, text, len, rate); |
| 5292 | } |
| 5293 | |
| 5294 | |
| 5295 | |
| 5296 | void inputx_post(running_machine *machine, const unicode_char *text) |
| 5297 | { |
| 5298 | inputx_post_rate(machine, text, attotime_make(0, 0)); |
| 5299 | } |
| 5300 | |
| 5301 | |
| 5302 | |
| 5303 | void inputx_postc_rate(running_machine *machine, unicode_char ch, attotime rate) |
| 5304 | { |
| 5305 | inputx_postn_rate(machine, &ch, 1, rate); |
| 5306 | } |
| 5307 | |
| 5308 | |
| 5309 | |
| 5310 | void inputx_postc(running_machine *machine, unicode_char ch) |
| 5311 | { |
| 5312 | inputx_postc_rate(machine, ch, attotime_make(0, 0)); |
| 5313 | } |
| 5314 | |
| 5315 | |
| 5316 | |
| 5317 | void inputx_postn_utf16_rate(running_machine *machine, const utf16_char *text, size_t text_len, attotime rate) |
| 5318 | { |
| 5319 | size_t len = 0; |
| 5320 | unicode_char c; |
| 5321 | utf16_char w1, w2; |
| 5322 | unicode_char buf[256]; |
| 5323 | |
| 5324 | while(text_len > 0) |
| 5325 | { |
| 5326 | if (len == ARRAY_LENGTH(buf)) |
| 5327 | { |
| 5328 | inputx_postn(machine, buf, len); |
| 5329 | len = 0; |
| 5330 | } |
| 5331 | |
| 5332 | w1 = *(text++); |
| 5333 | text_len--; |
| 5334 | |
| 5335 | if ((w1 >= 0xd800) && (w1 <= 0xdfff)) |
| 5336 | { |
| 5337 | if (w1 <= 0xDBFF) |
| 5338 | { |
| 5339 | w2 = 0; |
| 5340 | if (text_len > 0) |
| 5341 | { |
| 5342 | w2 = *(text++); |
| 5343 | text_len--; |
| 5344 | } |
| 5345 | if ((w2 >= 0xdc00) && (w2 <= 0xdfff)) |
| 5346 | { |
| 5347 | c = w1 & 0x03ff; |
| 5348 | c <<= 10; |
| 5349 | c |= w2 & 0x03ff; |
| 5350 | } |
| 5351 | else |
| 5352 | { |
| 5353 | c = INVALID_CHAR; |
| 5354 | } |
| 5355 | } |
| 5356 | else |
| 5357 | { |
| 5358 | c = INVALID_CHAR; |
| 5359 | } |
| 5360 | } |
| 5361 | else |
| 5362 | { |
| 5363 | c = w1; |
| 5364 | } |
| 5365 | buf[len++] = c; |
| 5366 | } |
| 5367 | inputx_postn_rate(machine, buf, len, rate); |
| 5368 | } |
| 5369 | |
| 5370 | |
| 5371 | |
| 5372 | void inputx_postn_utf16(running_machine *machine, const utf16_char *text, size_t text_len) |
| 5373 | { |
| 5374 | inputx_postn_utf16_rate(machine, text, text_len, attotime_make(0, 0)); |
| 5375 | } |
| 5376 | |
| 5377 | |
| 5378 | |
| 5379 | void inputx_post_utf16_rate(running_machine *machine, const utf16_char *text, attotime rate) |
| 5380 | { |
| 5381 | size_t len = 0; |
| 5382 | while(text[len]) |
| 5383 | len++; |
| 5384 | inputx_postn_utf16_rate(machine, text, len, rate); |
| 5385 | } |
| 5386 | |
| 5387 | |
| 5388 | |
| 5389 | void inputx_post_utf16(running_machine *machine, const utf16_char *text) |
| 5390 | { |
| 5391 | inputx_post_utf16_rate(machine, text, attotime_make(0, 0)); |
| 5392 | } |
| 5393 | |
| 5394 | |
| 5395 | |
| 5396 | void inputx_postn_utf8_rate(running_machine *machine, const char *text, size_t text_len, attotime rate) |
| 5397 | { |
| 5398 | size_t len = 0; |
| 5399 | unicode_char buf[256]; |
| 5400 | unicode_char c; |
| 5401 | int rc; |
| 5402 | |
| 5403 | while(text_len > 0) |
| 5404 | { |
| 5405 | if (len == ARRAY_LENGTH(buf)) |
| 5406 | { |
| 5407 | inputx_postn(machine, buf, len); |
| 5408 | len = 0; |
| 5409 | } |
| 5410 | |
| 5411 | rc = uchar_from_utf8(&c, text, text_len); |
| 5412 | if (rc < 0) |
| 5413 | { |
| 5414 | rc = 1; |
| 5415 | c = INVALID_CHAR; |
| 5416 | } |
| 5417 | text += rc; |
| 5418 | text_len -= rc; |
| 5419 | buf[len++] = c; |
| 5420 | } |
| 5421 | inputx_postn_rate(machine, buf, len, rate); |
| 5422 | } |
| 5423 | |
| 5424 | |
| 5425 | |
| 5426 | void inputx_postn_utf8(running_machine *machine, const char *text, size_t text_len) |
| 5427 | { |
| 5428 | inputx_postn_utf8_rate(machine, text, text_len, attotime_make(0, 0)); |
| 5429 | } |
| 5430 | |
| 5431 | |
| 5432 | |
| 5433 | void inputx_post_utf8_rate(running_machine *machine, const char *text, attotime rate) |
| 5434 | { |
| 5435 | inputx_postn_utf8_rate(machine, text, strlen(text), rate); |
| 5436 | } |
| 5437 | |
| 5438 | |
| 5439 | |
| 5440 | void inputx_post_utf8(running_machine *machine, const char *text) |
| 5441 | { |
| 5442 | inputx_post_utf8_rate(machine, text, attotime_make(0, 0)); |
| 5443 | } |
| 5444 | |
| 5445 | |
| 5446 | |
| 5447 | void inputx_post_coded(running_machine *machine, const char *text) |
| 5448 | { |
| 5449 | inputx_postn_coded(machine, text, strlen(text)); |
| 5450 | } |
| 5451 | |
| 5452 | |
| 5453 | |
| 5454 | void inputx_post_coded_rate(running_machine *machine, const char *text, attotime rate) |
| 5455 | { |
| 5456 | inputx_postn_coded_rate(machine, text, strlen(text), rate); |
| 5457 | } |
| 5458 | |
| 5459 | |
| 5460 | |
| 5461 | void inputx_postn_coded(running_machine *machine, const char *text, size_t text_len) |
| 5462 | { |
| 5463 | inputx_postn_coded_rate(machine, text, text_len, attotime_make(0, 0)); |
| 5464 | } |
| 5465 | |
| 5466 | |
| 5467 | |
| 5468 | /*************************************************************************** |
| 5469 | |
| 5470 | Other stuff |
| 5471 | |
| 5472 | This stuff is here more out of convienience than anything else |
| 5473 | ***************************************************************************/ |
| 5474 | |
| 5475 | int input_classify_port(const input_field_config *field) |
| 5476 | { |
| 5477 | int result; |
| 5478 | |
| 5479 | if (field->category && (field->type != IPT_CATEGORY)) |
| 5480 | return INPUT_CLASS_CATEGORIZED; |
| 5481 | |
| 5482 | switch(field->type) |
| 5483 | { |
| 5484 | case IPT_JOYSTICK_UP: |
| 5485 | case IPT_JOYSTICK_DOWN: |
| 5486 | case IPT_JOYSTICK_LEFT: |
| 5487 | case IPT_JOYSTICK_RIGHT: |
| 5488 | case IPT_JOYSTICKLEFT_UP: |
| 5489 | case IPT_JOYSTICKLEFT_DOWN: |
| 5490 | case IPT_JOYSTICKLEFT_LEFT: |
| 5491 | case IPT_JOYSTICKLEFT_RIGHT: |
| 5492 | case IPT_JOYSTICKRIGHT_UP: |
| 5493 | case IPT_JOYSTICKRIGHT_DOWN: |
| 5494 | case IPT_JOYSTICKRIGHT_LEFT: |
| 5495 | case IPT_JOYSTICKRIGHT_RIGHT: |
| 5496 | case IPT_BUTTON1: |
| 5497 | case IPT_BUTTON2: |
| 5498 | case IPT_BUTTON3: |
| 5499 | case IPT_BUTTON4: |
| 5500 | case IPT_BUTTON5: |
| 5501 | case IPT_BUTTON6: |
| 5502 | case IPT_BUTTON7: |
| 5503 | case IPT_BUTTON8: |
| 5504 | case IPT_BUTTON9: |
| 5505 | case IPT_BUTTON10: |
| 5506 | case IPT_AD_STICK_X: |
| 5507 | case IPT_AD_STICK_Y: |
| 5508 | case IPT_AD_STICK_Z: |
| 5509 | case IPT_TRACKBALL_X: |
| 5510 | case IPT_TRACKBALL_Y: |
| 5511 | case IPT_LIGHTGUN_X: |
| 5512 | case IPT_LIGHTGUN_Y: |
| 5513 | case IPT_MOUSE_X: |
| 5514 | case IPT_MOUSE_Y: |
| 5515 | case IPT_START: |
| 5516 | case IPT_SELECT: |
| 5517 | result = INPUT_CLASS_CONTROLLER; |
| 5518 | break; |
| 5519 | |
| 5520 | case IPT_KEYBOARD: |
| 5521 | result = INPUT_CLASS_KEYBOARD; |
| 5522 | break; |
| 5523 | |
| 5524 | case IPT_CONFIG: |
| 5525 | result = INPUT_CLASS_CONFIG; |
| 5526 | break; |
| 5527 | |
| 5528 | case IPT_DIPSWITCH: |
| 5529 | result = INPUT_CLASS_DIPSWITCH; |
| 5530 | break; |
| 5531 | |
| 5532 | case 0: |
| 5533 | if (field->name && (field->name != (const char *) -1)) |
| 5534 | result = INPUT_CLASS_MISC; |
| 5535 | else |
| 5536 | result = INPUT_CLASS_INTERNAL; |
| 5537 | break; |
| 5538 | |
| 5539 | default: |
| 5540 | result = INPUT_CLASS_INTERNAL; |
| 5541 | break; |
| 5542 | } |
| 5543 | return result; |
| 5544 | } |
| 5545 | |
| 5546 | |
| 5547 | |
| 5548 | int input_player_number(const input_field_config *port) |
| 5549 | { |
| 5550 | return port->player; |
| 5551 | } |
| 5552 | |
| 5553 | |
| 5554 | |
| 5555 | /*------------------------------------------------- |
| 5556 | input_has_input_class - checks to see if a |
| 5557 | particular input class is present |
| 5558 | -------------------------------------------------*/ |
| 5559 | |
| 5560 | int input_has_input_class(running_machine *machine, int inputclass) |
| 5561 | { |
| 5562 | const input_port_config *port; |
| 5563 | const input_field_config *field; |
| 5564 | |
| 5565 | for (port = machine->portlist.first(); port != NULL; port = port->next) |
| 5566 | { |
| 5567 | for (field = port->fieldlist; field != NULL; field = field->next) |
| 5568 | { |
| 5569 | if (input_classify_port(field) == inputclass) |
| 5570 | return TRUE; |
| 5571 | } |
| 5572 | } |
| 5573 | return FALSE; |
| 5574 | } |
| 5575 | |
| 5576 | |
| 5577 | |
| 5578 | /*------------------------------------------------- |
| 5579 | input_count_players - counts the number of |
| 5580 | active players |
| 5581 | -------------------------------------------------*/ |
| 5582 | |
| 5583 | int input_count_players(running_machine *machine) |
| 5584 | { |
| 5585 | const input_port_config *port; |
| 5586 | const input_field_config *field; |
| 5587 | int joystick_count; |
| 5588 | |
| 5589 | joystick_count = 0; |
| 5590 | for (port = machine->portlist.first(); port != NULL; port = port->next) |
| 5591 | { |
| 5592 | for (field = port->fieldlist; field != NULL; field = field->next) |
| 5593 | { |
| 5594 | if (input_classify_port(field) == INPUT_CLASS_CONTROLLER) |
| 5595 | { |
| 5596 | if (joystick_count <= field->player + 1) |
| 5597 | joystick_count = field->player + 1; |
| 5598 | } |
| 5599 | } |
| 5600 | } |
| 5601 | return joystick_count; |
| 5602 | } |
| 5603 | |
| 5604 | |
| 5605 | |
| 5606 | /*------------------------------------------------- |
| 5607 | input_category_active - checks to see if a |
| 5608 | specific category is active |
| 5609 | -------------------------------------------------*/ |
| 5610 | |
| 5611 | int input_category_active(running_machine *machine, int category) |
| 5612 | { |
| 5613 | const input_port_config *port; |
| 5614 | const input_field_config *field = NULL; |
| 5615 | const input_setting_config *setting; |
| 5616 | input_field_user_settings settings; |
| 5617 | |
| 5618 | assert(category >= 1); |
| 5619 | |
| 5620 | /* loop through the input ports */ |
| 5621 | for (port = machine->portlist.first(); port != NULL; port = port->next) |
| 5622 | { |
| 5623 | for (field = port->fieldlist; field != NULL; field = field->next) |
| 5624 | { |
| 5625 | /* is this field a category? */ |
| 5626 | if (field->type == IPT_CATEGORY) |
| 5627 | { |
| 5628 | /* get the settings value */ |
| 5629 | input_field_get_user_settings(field, &settings); |
| 5630 | |
| 5631 | for (setting = field->settinglist; setting != NULL; setting = setting->next) |
| 5632 | { |
| 5633 | /* is this the category we want? if so, is this settings value correct? */ |
| 5634 | if ((setting->category == category) && (settings.value == setting->value)) |
| 5635 | return TRUE; |
| 5636 | } |
| 5637 | } |
| 5638 | } |
| 5639 | } |
| 5640 | return FALSE; |
| 5641 | } |
| 5642 | |
| 5643 | |
| 5644 | |
| 5645 | /*************************************************************************** |
| 5646 | DEBUGGER SUPPORT |
| 5647 | ***************************************************************************/ |
| 5648 | |
| 5649 | /*------------------------------------------------- |
| 5650 | execute_input - debugger command to enter |
| 5651 | natural keyboard input |
| 5652 | -------------------------------------------------*/ |
| 5653 | |
| 5654 | static void execute_input(running_machine *machine, int ref, int params, const char *param[]) |
| 5655 | { |
| 5656 | inputx_post_coded(machine, param[0]); |
| 5657 | } |
| 5658 | |
| 5659 | |
| 5660 | |
| 5661 | /*------------------------------------------------- |
| 5662 | execute_dumpkbd - debugger command to natural |
| 5663 | keyboard codes |
| 5664 | -------------------------------------------------*/ |
| 5665 | |
| 5666 | static void execute_dumpkbd(running_machine *machine, int ref, int params, const char *param[]) |
| 5667 | { |
| 5668 | const char *filename; |
| 5669 | FILE *file = NULL; |
| 5670 | const inputx_code *code; |
| 5671 | char buffer[512]; |
| 5672 | size_t pos; |
| 5673 | int i, j; |
| 5674 | size_t left_column_width = 24; |
| 5675 | |
| 5676 | /* was there a file specified? */ |
| 5677 | filename = (params > 0) ? param[0] : NULL; |
| 5678 | if (filename != NULL) |
| 5679 | { |
| 5680 | /* if so, open it */ |
| 5681 | file = fopen(filename, "w"); |
| 5682 | if (file == NULL) |
| 5683 | { |
| 5684 | debug_console_printf(machine, "Cannot open \"%s\"\n", filename); |
| 5685 | return; |
| 5686 | } |
| 5687 | } |
| 5688 | |
| 5689 | if ((codes != NULL) && (codes[0].ch != 0)) |
| 5690 | { |
| 5691 | /* loop through all codes */ |
| 5692 | for (i = 0; codes[i].ch; i++) |
| 5693 | { |
| 5694 | code = &codes[i]; |
| 5695 | pos = 0; |
| 5696 | |
| 5697 | /* describe the character code */ |
| 5698 | pos += snprintf(&buffer[pos], ARRAY_LENGTH(buffer) - pos, "%08X (%s) ", |
| 5699 | code->ch, |
| 5700 | code_point_string(machine, code->ch)); |
| 5701 | |
| 5702 | /* pad with spaces */ |
| 5703 | while(pos < left_column_width) |
| 5704 | buffer[pos++] = ' '; |
| 5705 | buffer[pos] = '\0'; |
| 5706 | |
| 5707 | /* identify the keys used */ |
| 5708 | for (j = 0; j < ARRAY_LENGTH(code->field) && (code->field[j] != NULL); j++) |
| 5709 | { |
| 5710 | pos += snprintf(&buffer[pos], ARRAY_LENGTH(buffer) - pos, "%s'%s'", |
| 5711 | (j > 0) ? ", " : "", |
| 5712 | code->field[j]->name); |
| 5713 | } |
| 5714 | |
| 5715 | /* and output it as appropriate */ |
| 5716 | if (file != NULL) |
| 5717 | fprintf(file, "%s\n", buffer); |
| 5718 | else |
| 5719 | debug_console_printf(machine, "%s\n", buffer); |
| 5720 | } |
| 5721 | } |
| 5722 | else |
| 5723 | { |
| 5724 | debug_console_printf(machine, "No natural keyboard support\n"); |
| 5725 | } |
| 5726 | |
| 5727 | /* cleanup */ |
| 5728 | if (file != NULL) |
| 5729 | fclose(file); |
| 5730 | |
| 5731 | } |
src/mess/inputx.c
| r7335 | r7336 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | inputx.c |
| 4 | | |
| 5 | | Secondary input related functions for MESS specific functionality |
| 6 | | |
| 7 | | *********************************************************************/ |
| 8 | | |
| 9 | | #include <ctype.h> |
| 10 | | #include <assert.h> |
| 11 | | #include <wctype.h> |
| 12 | | #include "emu.h" |
| 13 | | #include "inputx.h" |
| 14 | | |
| 15 | | #include "debug/debugcon.h" |
| 16 | | |
| 17 | | |
| 18 | | |
| 19 | | /*************************************************************************** |
| 20 | | CONSTANTS |
| 21 | | ***************************************************************************/ |
| 22 | | |
| 23 | | #define NUM_SIMUL_KEYS (UCHAR_SHIFT_END - UCHAR_SHIFT_BEGIN + 1) |
| 24 | | #define LOG_INPUTX 0 |
| 25 | | #define SPACE_COUNT 3 |
| 26 | | #define INVALID_CHAR '?' |
| 27 | | #define IP_NAME_DEFAULT NULL |
| 28 | | |
| 29 | | |
| 30 | | |
| 31 | | /*************************************************************************** |
| 32 | | TYPE DEFINITIONS |
| 33 | | ***************************************************************************/ |
| 34 | | |
| 35 | | typedef const input_port_config *input_port_config_c; |
| 36 | | typedef const input_field_config *input_field_config_c; |
| 37 | | |
| 38 | | |
| 39 | | typedef struct _mess_input_code mess_input_code; |
| 40 | | struct _mess_input_code |
| 41 | | { |
| 42 | | unicode_char ch; |
| 43 | | input_field_config_c field[NUM_SIMUL_KEYS]; |
| 44 | | }; |
| 45 | | |
| 46 | | typedef struct _key_buffer key_buffer; |
| 47 | | struct _key_buffer |
| 48 | | { |
| 49 | | int begin_pos; |
| 50 | | int end_pos; |
| 51 | | unsigned int status_keydown : 1; |
| 52 | | unicode_char buffer[4096]; |
| 53 | | }; |
| 54 | | |
| 55 | | typedef struct _char_info char_info; |
| 56 | | struct _char_info |
| 57 | | { |
| 58 | | unicode_char ch; |
| 59 | | const char *name; |
| 60 | | const char *alternate; /* alternative string, in UTF-8 */ |
| 61 | | }; |
| 62 | | |
| 63 | | |
| 64 | | |
| 65 | | /*************************************************************************** |
| 66 | | GLOBAL VARIABLES |
| 67 | | ***************************************************************************/ |
| 68 | | |
| 69 | | static const char_info charinfo[] = |
| 70 | | { |
| 71 | | { 0x0008, "Backspace", NULL }, /* Backspace */ |
| 72 | | { 0x0009, "Tab", " " }, /* Tab */ |
| 73 | | { 0x000c, "Clear", NULL }, /* Clear */ |
| 74 | | { 0x000d, "Enter", NULL }, /* Enter */ |
| 75 | | { 0x001a, "Esc", NULL }, /* Esc */ |
| 76 | | { 0x0020, "Space", " " }, /* Space */ |
| 77 | | { 0x0061, NULL, "A" }, /* a */ |
| 78 | | { 0x0062, NULL, "B" }, /* b */ |
| 79 | | { 0x0063, NULL, "C" }, /* c */ |
| 80 | | { 0x0064, NULL, "D" }, /* d */ |
| 81 | | { 0x0065, NULL, "E" }, /* e */ |
| 82 | | { 0x0066, NULL, "F" }, /* f */ |
| 83 | | { 0x0067, NULL, "G" }, /* g */ |
| 84 | | { 0x0068, NULL, "H" }, /* h */ |
| 85 | | { 0x0069, NULL, "I" }, /* i */ |
| 86 | | { 0x006a, NULL, "J" }, /* j */ |
| 87 | | { 0x006b, NULL, "K" }, /* k */ |
| 88 | | { 0x006c, NULL, "L" }, /* l */ |
| 89 | | { 0x006d, NULL, "M" }, /* m */ |
| 90 | | { 0x006e, NULL, "N" }, /* n */ |
| 91 | | { 0x006f, NULL, "O" }, /* o */ |
| 92 | | { 0x0070, NULL, "P" }, /* p */ |
| 93 | | { 0x0071, NULL, "Q" }, /* q */ |
| 94 | | { 0x0072, NULL, "R" }, /* r */ |
| 95 | | { 0x0073, NULL, "S" }, /* s */ |
| 96 | | { 0x0074, NULL, "T" }, /* t */ |
| 97 | | { 0x0075, NULL, "U" }, /* u */ |
| 98 | | { 0x0076, NULL, "V" }, /* v */ |
| 99 | | { 0x0077, NULL, "W" }, /* w */ |
| 100 | | { 0x0078, NULL, "X" }, /* x */ |
| 101 | | { 0x0079, NULL, "Y" }, /* y */ |
| 102 | | { 0x007a, NULL, "Z" }, /* z */ |
| 103 | | { 0x00a0, NULL, " " }, /* non breaking space */ |
| 104 | | { 0x00a1, NULL, "!" }, /* inverted exclaimation mark */ |
| 105 | | { 0x00a6, NULL, "|" }, /* broken bar */ |
| 106 | | { 0x00a9, NULL, "(c)" }, /* copyright sign */ |
| 107 | | { 0x00ab, NULL, "<<" }, /* left pointing double angle */ |
| 108 | | { 0x00ae, NULL, "(r)" }, /* registered sign */ |
| 109 | | { 0x00bb, NULL, ">>" }, /* right pointing double angle */ |
| 110 | | { 0x00bc, NULL, "1/4" }, /* vulgar fraction one quarter */ |
| 111 | | { 0x00bd, NULL, "1/2" }, /* vulgar fraction one half */ |
| 112 | | { 0x00be, NULL, "3/4" }, /* vulgar fraction three quarters */ |
| 113 | | { 0x00bf, NULL, "?" }, /* inverted question mark */ |
| 114 | | { 0x00c0, NULL, "A" }, /* 'A' grave */ |
| 115 | | { 0x00c1, NULL, "A" }, /* 'A' acute */ |
| 116 | | { 0x00c2, NULL, "A" }, /* 'A' circumflex */ |
| 117 | | { 0x00c3, NULL, "A" }, /* 'A' tilde */ |
| 118 | | { 0x00c4, NULL, "A" }, /* 'A' diaeresis */ |
| 119 | | { 0x00c5, NULL, "A" }, /* 'A' ring above */ |
| 120 | | { 0x00c6, NULL, "AE" }, /* 'AE' ligature */ |
| 121 | | { 0x00c7, NULL, "C" }, /* 'C' cedilla */ |
| 122 | | { 0x00c8, NULL, "E" }, /* 'E' grave */ |
| 123 | | { 0x00c9, NULL, "E" }, /* 'E' acute */ |
| 124 | | { 0x00ca, NULL, "E" }, /* 'E' circumflex */ |
| 125 | | { 0x00cb, NULL, "E" }, /* 'E' diaeresis */ |
| 126 | | { 0x00cc, NULL, "I" }, /* 'I' grave */ |
| 127 | | { 0x00cd, NULL, "I" }, /* 'I' acute */ |
| 128 | | { 0x00ce, NULL, "I" }, /* 'I' circumflex */ |
| 129 | | { 0x00cf, NULL, "I" }, /* 'I' diaeresis */ |
| 130 | | { 0x00d0, NULL, "D" }, /* 'ETH' */ |
| 131 | | { 0x00d1, NULL, "N" }, /* 'N' tilde */ |
| 132 | | { 0x00d2, NULL, "O" }, /* 'O' grave */ |
| 133 | | { 0x00d3, NULL, "O" }, /* 'O' acute */ |
| 134 | | { 0x00d4, NULL, "O" }, /* 'O' circumflex */ |
| 135 | | { 0x00d5, NULL, "O" }, /* 'O' tilde */ |
| 136 | | { 0x00d6, NULL, "O" }, /* 'O' diaeresis */ |
| 137 | | { 0x00d7, NULL, "X" }, /* multiplication sign */ |
| 138 | | { 0x00d8, NULL, "O" }, /* 'O' stroke */ |
| 139 | | { 0x00d9, NULL, "U" }, /* 'U' grave */ |
| 140 | | { 0x00da, NULL, "U" }, /* 'U' acute */ |
| 141 | | { 0x00db, NULL, "U" }, /* 'U' circumflex */ |
| 142 | | { 0x00dc, NULL, "U" }, /* 'U' diaeresis */ |
| 143 | | { 0x00dd, NULL, "Y" }, /* 'Y' acute */ |
| 144 | | { 0x00df, NULL, "SS" }, /* sharp S */ |
| 145 | | { 0x00e0, NULL, "a" }, /* 'a' grave */ |
| 146 | | { 0x00e1, NULL, "a" }, /* 'a' acute */ |
| 147 | | { 0x00e2, NULL, "a" }, /* 'a' circumflex */ |
| 148 | | { 0x00e3, NULL, "a" }, /* 'a' tilde */ |
| 149 | | { 0x00e4, NULL, "a" }, /* 'a' diaeresis */ |
| 150 | | { 0x00e5, NULL, "a" }, /* 'a' ring above */ |
| 151 | | { 0x00e6, NULL, "ae" }, /* 'ae' ligature */ |
| 152 | | { 0x00e7, NULL, "c" }, /* 'c' cedilla */ |
| 153 | | { 0x00e8, NULL, "e" }, /* 'e' grave */ |
| 154 | | { 0x00e9, NULL, "e" }, /* 'e' acute */ |
| 155 | | { 0x00ea, NULL, "e" }, /* 'e' circumflex */ |
| 156 | | { 0x00eb, NULL, "e" }, /* 'e' diaeresis */ |
| 157 | | { 0x00ec, NULL, "i" }, /* 'i' grave */ |
| 158 | | { 0x00ed, NULL, "i" }, /* 'i' acute */ |
| 159 | | { 0x00ee, NULL, "i" }, /* 'i' circumflex */ |
| 160 | | { 0x00ef, NULL, "i" }, /* 'i' diaeresis */ |
| 161 | | { 0x00f0, NULL, "d" }, /* 'eth' */ |
| 162 | | { 0x00f1, NULL, "n" }, /* 'n' tilde */ |
| 163 | | { 0x00f2, NULL, "o" }, /* 'o' grave */ |
| 164 | | { 0x00f3, NULL, "o" }, /* 'o' acute */ |
| 165 | | { 0x00f4, NULL, "o" }, /* 'o' circumflex */ |
| 166 | | { 0x00f5, NULL, "o" }, /* 'o' tilde */ |
| 167 | | { 0x00f6, NULL, "o" }, /* 'o' diaeresis */ |
| 168 | | { 0x00f8, NULL, "o" }, /* 'o' stroke */ |
| 169 | | { 0x00f9, NULL, "u" }, /* 'u' grave */ |
| 170 | | { 0x00fa, NULL, "u" }, /* 'u' acute */ |
| 171 | | { 0x00fb, NULL, "u" }, /* 'u' circumflex */ |
| 172 | | { 0x00fc, NULL, "u" }, /* 'u' diaeresis */ |
| 173 | | { 0x00fd, NULL, "y" }, /* 'y' acute */ |
| 174 | | { 0x00ff, NULL, "y" }, /* 'y' diaeresis */ |
| 175 | | { 0x2010, NULL, "-" }, /* hyphen */ |
| 176 | | { 0x2011, NULL, "-" }, /* non-breaking hyphen */ |
| 177 | | { 0x2012, NULL, "-" }, /* figure dash */ |
| 178 | | { 0x2013, NULL, "-" }, /* en dash */ |
| 179 | | { 0x2014, NULL, "-" }, /* em dash */ |
| 180 | | { 0x2015, NULL, "-" }, /* horizontal dash */ |
| 181 | | { 0x2018, NULL, "\'" }, /* left single quotation mark */ |
| 182 | | { 0x2019, NULL, "\'" }, /* right single quotation mark */ |
| 183 | | { 0x201a, NULL, "\'" }, /* single low quotation mark */ |
| 184 | | { 0x201b, NULL, "\'" }, /* single high reversed quotation mark */ |
| 185 | | { 0x201c, NULL, "\"" }, /* left double quotation mark */ |
| 186 | | { 0x201d, NULL, "\"" }, /* right double quotation mark */ |
| 187 | | { 0x201e, NULL, "\"" }, /* double low quotation mark */ |
| 188 | | { 0x201f, NULL, "\"" }, /* double high reversed quotation mark */ |
| 189 | | { 0x2024, NULL, "." }, /* one dot leader */ |
| 190 | | { 0x2025, NULL, ".." }, /* two dot leader */ |
| 191 | | { 0x2026, NULL, "..." }, /* horizontal ellipsis */ |
| 192 | | { 0x2047, NULL, "??" }, /* double question mark */ |
| 193 | | { 0x2048, NULL, "?!" }, /* question exclamation mark */ |
| 194 | | { 0x2049, NULL, "!?" }, /* exclamation question mark */ |
| 195 | | { 0xff01, NULL, "!" }, /* fullwidth exclamation point */ |
| 196 | | { 0xff02, NULL, "\"" }, /* fullwidth quotation mark */ |
| 197 | | { 0xff03, NULL, "#" }, /* fullwidth number sign */ |
| 198 | | { 0xff04, NULL, "$" }, /* fullwidth dollar sign */ |
| 199 | | { 0xff05, NULL, "%" }, /* fullwidth percent sign */ |
| 200 | | { 0xff06, NULL, "&" }, /* fullwidth ampersand */ |
| 201 | | { 0xff07, NULL, "\'" }, /* fullwidth apostrophe */ |
| 202 | | { 0xff08, NULL, "(" }, /* fullwidth left parenthesis */ |
| 203 | | { 0xff09, NULL, ")" }, /* fullwidth right parenthesis */ |
| 204 | | { 0xff0a, NULL, "*" }, /* fullwidth asterisk */ |
| 205 | | { 0xff0b, NULL, "+" }, /* fullwidth plus */ |
| 206 | | { 0xff0c, NULL, "," }, /* fullwidth comma */ |
| 207 | | { 0xff0d, NULL, "-" }, /* fullwidth minus */ |
| 208 | | { 0xff0e, NULL, "." }, /* fullwidth period */ |
| 209 | | { 0xff0f, NULL, "/" }, /* fullwidth slash */ |
| 210 | | { 0xff10, NULL, "0" }, /* fullwidth zero */ |
| 211 | | { 0xff11, NULL, "1" }, /* fullwidth one */ |
| 212 | | { 0xff12, NULL, "2" }, /* fullwidth two */ |
| 213 | | { 0xff13, NULL, "3" }, /* fullwidth three */ |
| 214 | | { 0xff14, NULL, "4" }, /* fullwidth four */ |
| 215 | | { 0xff15, NULL, "5" }, /* fullwidth five */ |
| 216 | | { 0xff16, NULL, "6" }, /* fullwidth six */ |
| 217 | | { 0xff17, NULL, "7" }, /* fullwidth seven */ |
| 218 | | { 0xff18, NULL, "8" }, /* fullwidth eight */ |
| 219 | | { 0xff19, NULL, "9" }, /* fullwidth nine */ |
| 220 | | { 0xff1a, NULL, ":" }, /* fullwidth colon */ |
| 221 | | { 0xff1b, NULL, ";" }, /* fullwidth semicolon */ |
| 222 | | { 0xff1c, NULL, "<" }, /* fullwidth less than sign */ |
| 223 | | { 0xff1d, NULL, "=" }, /* fullwidth equals sign */ |
| 224 | | { 0xff1e, NULL, ">" }, /* fullwidth greater than sign */ |
| 225 | | { 0xff1f, NULL, "?" }, /* fullwidth question mark */ |
| 226 | | { 0xff20, NULL, "@" }, /* fullwidth at sign */ |
| 227 | | { 0xff21, NULL, "A" }, /* fullwidth 'A' */ |
| 228 | | { 0xff22, NULL, "B" }, /* fullwidth 'B' */ |
| 229 | | { 0xff23, NULL, "C" }, /* fullwidth 'C' */ |
| 230 | | { 0xff24, NULL, "D" }, /* fullwidth 'D' */ |
| 231 | | { 0xff25, NULL, "E" }, /* fullwidth 'E' */ |
| 232 | | { 0xff26, NULL, "F" }, /* fullwidth 'F' */ |
| 233 | | { 0xff27, NULL, "G" }, /* fullwidth 'G' */ |
| 234 | | { 0xff28, NULL, "H" }, /* fullwidth 'H' */ |
| 235 | | { 0xff29, NULL, "I" }, /* fullwidth 'I' */ |
| 236 | | { 0xff2a, NULL, "J" }, /* fullwidth 'J' */ |
| 237 | | { 0xff2b, NULL, "K" }, /* fullwidth 'K' */ |
| 238 | | { 0xff2c, NULL, "L" }, /* fullwidth 'L' */ |
| 239 | | { 0xff2d, NULL, "M" }, /* fullwidth 'M' */ |
| 240 | | { 0xff2e, NULL, "N" }, /* fullwidth 'N' */ |
| 241 | | { 0xff2f, NULL, "O" }, /* fullwidth 'O' */ |
| 242 | | { 0xff30, NULL, "P" }, /* fullwidth 'P' */ |
| 243 | | { 0xff31, NULL, "Q" }, /* fullwidth 'Q' */ |
| 244 | | { 0xff32, NULL, "R" }, /* fullwidth 'R' */ |
| 245 | | { 0xff33, NULL, "S" }, /* fullwidth 'S' */ |
| 246 | | { 0xff34, NULL, "T" }, /* fullwidth 'T' */ |
| 247 | | { 0xff35, NULL, "U" }, /* fullwidth 'U' */ |
| 248 | | { 0xff36, NULL, "V" }, /* fullwidth 'V' */ |
| 249 | | { 0xff37, NULL, "W" }, /* fullwidth 'W' */ |
| 250 | | { 0xff38, NULL, "X" }, /* fullwidth 'X' */ |
| 251 | | { 0xff39, NULL, "Y" }, /* fullwidth 'Y' */ |
| 252 | | { 0xff3a, NULL, "Z" }, /* fullwidth 'Z' */ |
| 253 | | { 0xff3b, NULL, "[" }, /* fullwidth left bracket */ |
| 254 | | { 0xff3c, NULL, "\\" }, /* fullwidth backslash */ |
| 255 | | { 0xff3d, NULL, "]" }, /* fullwidth right bracket */ |
| 256 | | { 0xff3e, NULL, "^" }, /* fullwidth caret */ |
| 257 | | { 0xff3f, NULL, "_" }, /* fullwidth underscore */ |
| 258 | | { 0xff40, NULL, "`" }, /* fullwidth backquote */ |
| 259 | | { 0xff41, NULL, "a" }, /* fullwidth 'a' */ |
| 260 | | { 0xff42, NULL, "b" }, /* fullwidth 'b' */ |
| 261 | | { 0xff43, NULL, "c" }, /* fullwidth 'c' */ |
| 262 | | { 0xff44, NULL, "d" }, /* fullwidth 'd' */ |
| 263 | | { 0xff45, NULL, "e" }, /* fullwidth 'e' */ |
| 264 | | { 0xff46, NULL, "f" }, /* fullwidth 'f' */ |
| 265 | | { 0xff47, NULL, "g" }, /* fullwidth 'g' */ |
| 266 | | { 0xff48, NULL, "h" }, /* fullwidth 'h' */ |
| 267 | | { 0xff49, NULL, "i" }, /* fullwidth 'i' */ |
| 268 | | { 0xff4a, NULL, "j" }, /* fullwidth 'j' */ |
| 269 | | { 0xff4b, NULL, "k" }, /* fullwidth 'k' */ |
| 270 | | { 0xff4c, NULL, "l" }, /* fullwidth 'l' */ |
| 271 | | { 0xff4d, NULL, "m" }, /* fullwidth 'm' */ |
| 272 | | { 0xff4e, NULL, "n" }, /* fullwidth 'n' */ |
| 273 | | { 0xff4f, NULL, "o" }, /* fullwidth 'o' */ |
| 274 | | { 0xff50, NULL, "p" }, /* fullwidth 'p' */ |
| 275 | | { 0xff51, NULL, "q" }, /* fullwidth 'q' */ |
| 276 | | { 0xff52, NULL, "r" }, /* fullwidth 'r' */ |
| 277 | | { 0xff53, NULL, "s" }, /* fullwidth 's' */ |
| 278 | | { 0xff54, NULL, "t" }, /* fullwidth 't' */ |
| 279 | | { 0xff55, NULL, "u" }, /* fullwidth 'u' */ |
| 280 | | { 0xff56, NULL, "v" }, /* fullwidth 'v' */ |
| 281 | | { 0xff57, NULL, "w" }, /* fullwidth 'w' */ |
| 282 | | { 0xff58, NULL, "x" }, /* fullwidth 'x' */ |
| 283 | | { 0xff59, NULL, "y" }, /* fullwidth 'y' */ |
| 284 | | { 0xff5a, NULL, "z" }, /* fullwidth 'z' */ |
| 285 | | { 0xff5b, NULL, "{" }, /* fullwidth left brace */ |
| 286 | | { 0xff5c, NULL, "|" }, /* fullwidth vertical bar */ |
| 287 | | { 0xff5d, NULL, "}" }, /* fullwidth right brace */ |
| 288 | | { 0xff5e, NULL, "~" }, /* fullwidth tilde */ |
| 289 | | { 0xff5f, NULL, "((" }, /* fullwidth double left parenthesis */ |
| 290 | | { 0xff60, NULL, "))" }, /* fullwidth double right parenthesis */ |
| 291 | | { 0xffe0, NULL, "\xC2\xA2" }, /* fullwidth cent sign */ |
| 292 | | { 0xffe1, NULL, "\xC2\xA3" }, /* fullwidth pound sign */ |
| 293 | | { 0xffe4, NULL, "\xC2\xA4" }, /* fullwidth broken bar */ |
| 294 | | { 0xffe5, NULL, "\xC2\xA5" }, /* fullwidth yen sign */ |
| 295 | | { 0xffe6, NULL, "\xE2\x82\xA9" }, /* fullwidth won sign */ |
| 296 | | { 0xffe9, NULL, "\xE2\x86\x90" }, /* fullwidth left arrow */ |
| 297 | | { 0xffea, NULL, "\xE2\x86\x91" }, /* fullwidth up arrow */ |
| 298 | | { 0xffeb, NULL, "\xE2\x86\x92" }, /* fullwidth right arrow */ |
| 299 | | { 0xffec, NULL, "\xE2\x86\x93" }, /* fullwidth down arrow */ |
| 300 | | { 0xffed, NULL, "\xE2\x96\xAA" }, /* fullwidth solid box */ |
| 301 | | { 0xffee, NULL, "\xE2\x97\xA6" }, /* fullwidth open circle */ |
| 302 | | { UCHAR_SHIFT_1, "Shift", NULL }, /* Shift key */ |
| 303 | | { UCHAR_SHIFT_2, "Ctrl", NULL }, /* Ctrl key */ |
| 304 | | { UCHAR_MAMEKEY(F1), "F1", NULL }, /* F1 function key */ |
| 305 | | { UCHAR_MAMEKEY(F2), "F2", NULL }, /* F2 function key */ |
| 306 | | { UCHAR_MAMEKEY(F3), "F3", NULL }, /* F3 function key */ |
| 307 | | { UCHAR_MAMEKEY(F4), "F4", NULL }, /* F4 function key */ |
| 308 | | { UCHAR_MAMEKEY(F5), "F5", NULL }, /* F5 function key */ |
| 309 | | { UCHAR_MAMEKEY(F6), "F6", NULL }, /* F6 function key */ |
| 310 | | { UCHAR_MAMEKEY(F7), "F7", NULL }, /* F7 function key */ |
| 311 | | { UCHAR_MAMEKEY(F8), "F8", NULL }, /* F8 function key */ |
| 312 | | { UCHAR_MAMEKEY(F9), "F9", NULL }, /* F9 function key */ |
| 313 | | { UCHAR_MAMEKEY(F10), "F10", NULL }, /* F10 function key */ |
| 314 | | { UCHAR_MAMEKEY(F11), "F11", NULL }, /* F11 function key */ |
| 315 | | { UCHAR_MAMEKEY(F12), "F12", NULL }, /* F12 function key */ |
| 316 | | { UCHAR_MAMEKEY(F13), "F13", NULL }, /* F13 function key */ |
| 317 | | { UCHAR_MAMEKEY(F14), "F14", NULL }, /* F14 function key */ |
| 318 | | { UCHAR_MAMEKEY(F15), "F15", NULL }, /* F15 function key */ |
| 319 | | { UCHAR_MAMEKEY(ESC), "Esc", "\033" }, /* Esc key */ |
| 320 | | { UCHAR_MAMEKEY(INSERT), "Insert", NULL }, /* Insert key */ |
| 321 | | { UCHAR_MAMEKEY(DEL), "Delete", "\010" }, /* Delete key */ |
| 322 | | { UCHAR_MAMEKEY(HOME), "Home", "\014" }, /* Home key */ |
| 323 | | { UCHAR_MAMEKEY(END), "End", NULL }, /* End key */ |
| 324 | | { UCHAR_MAMEKEY(PGUP), "Page Up", NULL }, /* Page Up key */ |
| 325 | | { UCHAR_MAMEKEY(PGDN), "Page Down", NULL }, /* Page Down key */ |
| 326 | | { UCHAR_MAMEKEY(LEFT), "Cursor Left", NULL }, /* Cursor Left */ |
| 327 | | { UCHAR_MAMEKEY(RIGHT), "Cursor Right", NULL }, /* Cursor Right */ |
| 328 | | { UCHAR_MAMEKEY(UP), "Cursor Up", NULL }, /* Cursor Up */ |
| 329 | | { UCHAR_MAMEKEY(DOWN), "Cursor Down", NULL }, /* Cursor Down */ |
| 330 | | { UCHAR_MAMEKEY(0_PAD), "Keypad 0", NULL }, /* 0 on the numeric keypad */ |
| 331 | | { UCHAR_MAMEKEY(1_PAD), "Keypad 1", NULL }, /* 1 on the numeric keypad */ |
| 332 | | { UCHAR_MAMEKEY(2_PAD), "Keypad 2", NULL }, /* 2 on the numeric keypad */ |
| 333 | | { UCHAR_MAMEKEY(3_PAD), "Keypad 3", NULL }, /* 3 on the numeric keypad */ |
| 334 | | { UCHAR_MAMEKEY(4_PAD), "Keypad 4", NULL }, /* 4 on the numeric keypad */ |
| 335 | | { UCHAR_MAMEKEY(5_PAD), "Keypad 5", NULL }, /* 5 on the numeric keypad */ |
| 336 | | { UCHAR_MAMEKEY(6_PAD), "Keypad 6", NULL }, /* 6 on the numeric keypad */ |
| 337 | | { UCHAR_MAMEKEY(7_PAD), "Keypad 7", NULL }, /* 7 on the numeric keypad */ |
| 338 | | { UCHAR_MAMEKEY(8_PAD), "Keypad 8", NULL }, /* 8 on the numeric keypad */ |
| 339 | | { UCHAR_MAMEKEY(9_PAD), "Keypad 9", NULL }, /* 9 on the numeric keypad */ |
| 340 | | { UCHAR_MAMEKEY(SLASH_PAD), "Keypad /", NULL }, /* / on the numeric keypad */ |
| 341 | | { UCHAR_MAMEKEY(ASTERISK), "Keypad *", NULL }, /* * on the numeric keypad */ |
| 342 | | { UCHAR_MAMEKEY(MINUS_PAD), "Keypad -", NULL }, /* - on the numeric Keypad */ |
| 343 | | { UCHAR_MAMEKEY(PLUS_PAD), "Keypad +", NULL }, /* + on the numeric Keypad */ |
| 344 | | { UCHAR_MAMEKEY(DEL_PAD), "Keypad .", NULL }, /* . on the numeric keypad */ |
| 345 | | { UCHAR_MAMEKEY(ENTER_PAD), "Keypad Enter", NULL }, /* Enter on the numeric keypad */ |
| 346 | | { UCHAR_MAMEKEY(PRTSCR), "Print Screen", NULL }, /* Print Screen key */ |
| 347 | | { UCHAR_MAMEKEY(PAUSE), "Pause", NULL }, /* Pause key */ |
| 348 | | { UCHAR_MAMEKEY(LSHIFT), "Left Shift", NULL }, /* Left Shift key */ |
| 349 | | { UCHAR_MAMEKEY(RSHIFT), "Right Shift", NULL }, /* Right Shift key */ |
| 350 | | { UCHAR_MAMEKEY(LCONTROL), "Left Ctrl", NULL }, /* Left Control key */ |
| 351 | | { UCHAR_MAMEKEY(RCONTROL), "Right Ctrl", NULL }, /* Right Control key */ |
| 352 | | { UCHAR_MAMEKEY(LALT), "Left Alt", NULL }, /* Left Alt key */ |
| 353 | | { UCHAR_MAMEKEY(RALT), "Right Alt", NULL }, /* Right Alt key */ |
| 354 | | { UCHAR_MAMEKEY(SCRLOCK), "Scroll Lock", NULL }, /* Scroll Lock key */ |
| 355 | | { UCHAR_MAMEKEY(NUMLOCK), "Num Lock", NULL }, /* Num Lock key */ |
| 356 | | { UCHAR_MAMEKEY(CAPSLOCK), "Caps Lock", NULL }, /* Caps Lock key */ |
| 357 | | { UCHAR_MAMEKEY(LWIN), "Left Win", NULL }, /* Left Win key */ |
| 358 | | { UCHAR_MAMEKEY(RWIN), "Right Win", NULL }, /* Right Win key */ |
| 359 | | { UCHAR_MAMEKEY(MENU), "Menu", NULL }, /* Menu key */ |
| 360 | | { UCHAR_MAMEKEY(CANCEL), "Break", NULL } /* Break/Pause key */ |
| 361 | | }; |
| 362 | | |
| 363 | | |
| 364 | | |
| 365 | | |
| 366 | | /*************************************************************************** |
| 367 | | MISCELLANEOUS |
| 368 | | ***************************************************************************/ |
| 369 | | |
| 370 | | /*------------------------------------------------- |
| 371 | | find_charinfo - looks up information about a |
| 372 | | particular character |
| 373 | | -------------------------------------------------*/ |
| 374 | | |
| 375 | | static const char_info *find_charinfo(unicode_char target_char) |
| 376 | | { |
| 377 | | int low = 0; |
| 378 | | int high = ARRAY_LENGTH(charinfo); |
| 379 | | int i; |
| 380 | | unicode_char ch; |
| 381 | | |
| 382 | | /* perform a simple binary search to find the proper alternate */ |
| 383 | | while(high > low) |
| 384 | | { |
| 385 | | i = (high + low) / 2; |
| 386 | | ch = charinfo[i].ch; |
| 387 | | if (ch < target_char) |
| 388 | | low = i + 1; |
| 389 | | else if (ch > target_char) |
| 390 | | high = i; |
| 391 | | else |
| 392 | | return &charinfo[i]; |
| 393 | | } |
| 394 | | return NULL; |
| 395 | | } |
| 396 | | |
| 397 | | |
| 398 | | |
| 399 | | /*************************************************************************** |
| 400 | | CODE ASSEMBLING |
| 401 | | ***************************************************************************/ |
| 402 | | |
| 403 | | /*------------------------------------------------- |
| 404 | | code_point_string - obtain a string representation of a |
| 405 | | given code; used for logging and debugging |
| 406 | | -------------------------------------------------*/ |
| 407 | | |
| 408 | | static const char *code_point_string(running_machine *machine, unicode_char ch) |
| 409 | | { |
| 410 | | static char buf[16]; |
| 411 | | const char *result = buf; |
| 412 | | |
| 413 | | switch(ch) |
| 414 | | { |
| 415 | | /* check some magic values */ |
| 416 | | case '\0': strcpy(buf, "\\0"); break; |
| 417 | | case '\r': strcpy(buf, "\\r"); break; |
| 418 | | case '\n': strcpy(buf, "\\n"); break; |
| 419 | | case '\t': strcpy(buf, "\\t"); break; |
| 420 | | |
| 421 | | default: |
| 422 | | if ((ch >= 32) && (ch < 128)) |
| 423 | | { |
| 424 | | /* seven bit ASCII is easy */ |
| 425 | | buf[0] = (char) ch; |
| 426 | | buf[1] = '\0'; |
| 427 | | } |
| 428 | | else if (ch >= UCHAR_MAMEKEY_BEGIN) |
| 429 | | { |
| 430 | | /* try to obtain a codename with input_code_name(); this can result in an empty string */ |
| 431 | | astring astr; |
| 432 | | input_code_name(machine, astr, (input_code) ch - UCHAR_MAMEKEY_BEGIN); |
| 433 | | snprintf(buf, ARRAY_LENGTH(buf), "%s", astr.cstr()); |
| 434 | | } |
| 435 | | else |
| 436 | | { |
| 437 | | /* empty string; resolve later */ |
| 438 | | buf[0] = '\0'; |
| 439 | | } |
| 440 | | |
| 441 | | /* did we fail to resolve? if so, we have a last resort */ |
| 442 | | if (buf[0] == '\0') |
| 443 | | snprintf(buf, ARRAY_LENGTH(buf), "U+%04X", (unsigned) ch); |
| 444 | | break; |
| 445 | | } |
| 446 | | return result; |
| 447 | | } |
| 448 | | |
| 449 | | |
| 450 | | |
| 451 | | /*------------------------------------------------- |
| 452 | | get_keyboard_code - accesses a particular |
| 453 | | keyboard code |
| 454 | | -------------------------------------------------*/ |
| 455 | | |
| 456 | | static unicode_char get_keyboard_code(const input_field_config *field, int i) |
| 457 | | { |
| 458 | | unicode_char ch = field->chars[i]; |
| 459 | | |
| 460 | | /* special hack to allow for PORT_CODE('\xA3') */ |
| 461 | | if ((ch >= 0xFFFFFF80) && (ch <= 0xFFFFFFFF)) |
| 462 | | ch &= 0xFF; |
| 463 | | return ch; |
| 464 | | } |
| 465 | | |
| 466 | | |
| 467 | | |
| 468 | | /*------------------------------------------------- |
| 469 | | scan_keys - scans through input ports and |
| 470 | | sets up natural keyboard input mapping |
| 471 | | -------------------------------------------------*/ |
| 472 | | |
| 473 | | static int scan_keys(running_machine *machine, const input_port_config *portconfig, mess_input_code *codes, input_port_config_c *ports, input_field_config_c *shift_ports, int keys, int shift) |
| 474 | | { |
| 475 | | int code_count = 0; |
| 476 | | const input_port_config *port; |
| 477 | | const input_field_config *field; |
| 478 | | unicode_char code; |
| 479 | | |
| 480 | | assert(keys < NUM_SIMUL_KEYS); |
| 481 | | |
| 482 | | for (port = portconfig; port != NULL; port = port->next) |
| 483 | | { |
| 484 | | for (field = port->fieldlist; field != NULL; field = field->next) |
| 485 | | { |
| 486 | | if (field->type == IPT_KEYBOARD) |
| 487 | | { |
| 488 | | code = get_keyboard_code(field, shift); |
| 489 | | if (code != 0) |
| 490 | | { |
| 491 | | /* is this a shifter key? */ |
| 492 | | if ((code >= UCHAR_SHIFT_BEGIN) && (code <= UCHAR_SHIFT_END)) |
| 493 | | { |
| 494 | | shift_ports[keys] = field; |
| 495 | | code_count += scan_keys(machine, |
| 496 | | portconfig, |
| 497 | | codes ? &codes[code_count] : NULL, |
| 498 | | ports, |
| 499 | | shift_ports, |
| 500 | | keys+1, |
| 501 | | code - UCHAR_SHIFT_1 + 1); |
| 502 | | } |
| 503 | | else |
| 504 | | { |
| 505 | | /* not a shifter key; record normally */ |
| 506 | | if (codes) |
| 507 | | { |
| 508 | | /* if we have a destination, record the codes used here */ |
| 509 | | memcpy((void *) codes[code_count].field, shift_ports, sizeof(shift_ports[0]) * keys); |
| 510 | | codes[code_count].ch = code; |
| 511 | | codes[code_count].field[keys] = field; |
| 512 | | } |
| 513 | | |
| 514 | | /* increment the count */ |
| 515 | | code_count++; |
| 516 | | |
| 517 | | if (LOG_INPUTX) |
| 518 | | logerror("inputx: code=%i (%s) port=%p field->name='%s'\n", (int) code, code_point_string(machine, code), port, field->name); |
| 519 | | } |
| 520 | | } |
| 521 | | } |
| 522 | | } |
| 523 | | } |
| 524 | | return code_count; |
| 525 | | } |
| 526 | | |
| 527 | | |
| 528 | | |
| 529 | | /*------------------------------------------------- |
| 530 | | build_codes - given an input port table, create |
| 531 | | a input code table useful for mapping unicode |
| 532 | | chars |
| 533 | | -------------------------------------------------*/ |
| 534 | | |
| 535 | | static mess_input_code *build_codes(running_machine *machine, const input_port_config *portconfig) |
| 536 | | { |
| 537 | | mess_input_code *codes = NULL; |
| 538 | | const input_port_config *ports[NUM_SIMUL_KEYS]; |
| 539 | | const input_field_config *fields[NUM_SIMUL_KEYS]; |
| 540 | | int code_count; |
| 541 | | |
| 542 | | /* first count the number of codes */ |
| 543 | | code_count = scan_keys(machine, portconfig, NULL, ports, fields, 0, 0); |
| 544 | | if (code_count > 0) |
| 545 | | { |
| 546 | | /* allocate the codes */ |
| 547 | | codes = auto_alloc_array_clear(machine, mess_input_code, code_count + 1); |
| 548 | | |
| 549 | | /* and populate them */ |
| 550 | | scan_keys(machine, portconfig, codes, ports, fields, 0, 0); |
| 551 | | } |
| 552 | | return codes; |
| 553 | | } |
| 554 | | |
| 555 | | |
| 556 | | |
| 557 | | /*************************************************************************** |
| 558 | | VALIDITY CHECKS |
| 559 | | ***************************************************************************/ |
| 560 | | |
| 561 | | /*------------------------------------------------- |
| 562 | | mess_validate_natural_keyboard_statics - |
| 563 | | validates natural keyboard static data |
| 564 | | -------------------------------------------------*/ |
| 565 | | |
| 566 | | int mess_validate_natural_keyboard_statics(void) |
| 567 | | { |
| 568 | | int i; |
| 569 | | int error = FALSE; |
| 570 | | unicode_char last_char = 0; |
| 571 | | const char_info *ci; |
| 572 | | |
| 573 | | /* check to make sure that charinfo is in order */ |
| 574 | | for (i = 0; i < ARRAY_LENGTH(charinfo); i++) |
| 575 | | { |
| 576 | | if (last_char >= charinfo[i].ch) |
| 577 | | { |
| 578 | | mame_printf_error("inputx: charinfo is out of order; 0x%08x should be higher than 0x%08x\n", charinfo[i].ch, last_char); |
| 579 | | error = TRUE; |
| 580 | | } |
| 581 | | last_char = charinfo[i].ch; |
| 582 | | } |
| 583 | | |
| 584 | | /* check to make sure that I can look up everything on alternate_charmap */ |
| 585 | | for (i = 0; i < ARRAY_LENGTH(charinfo); i++) |
| 586 | | { |
| 587 | | ci = find_charinfo(charinfo[i].ch); |
| 588 | | if (ci != &charinfo[i]) |
| 589 | | { |
| 590 | | mame_printf_error("inputx: expected find_charinfo(0x%08x) to work properly\n", charinfo[i].ch); |
| 591 | | error = TRUE; |
| 592 | | } |
| 593 | | } |
| 594 | | return error; |
| 595 | | } |
| 596 | | |
| 597 | | |
| 598 | | |
| 599 | | /*************************************************************************** |
| 600 | | CORE IMPLEMENTATION |
| 601 | | ***************************************************************************/ |
| 602 | | |
| 603 | | static mess_input_code *codes; |
| 604 | | static key_buffer *keybuffer; |
| 605 | | static emu_timer *inputx_timer; |
| 606 | | static int (*queue_chars)(const unicode_char *text, size_t text_len); |
| 607 | | static int (*accept_char)(unicode_char ch); |
| 608 | | static int (*charqueue_empty)(void); |
| 609 | | static attotime current_rate; |
| 610 | | |
| 611 | | static TIMER_CALLBACK(inputx_timerproc); |
| 612 | | |
| 613 | | |
| 614 | | /* Debugging commands and handlers. */ |
| 615 | | static void execute_input(running_machine *machine, int ref, int params, const char *param[]); |
| 616 | | static void execute_dumpkbd(running_machine *machine, int ref, int params, const char *param[]); |
| 617 | | |
| 618 | | |
| 619 | | |
| 620 | | static void clear_keybuffer(running_machine *machine) |
| 621 | | { |
| 622 | | keybuffer = NULL; |
| 623 | | queue_chars = NULL; |
| 624 | | codes = NULL; |
| 625 | | } |
| 626 | | |
| 627 | | |
| 628 | | |
| 629 | | static void setup_keybuffer(running_machine *machine) |
| 630 | | { |
| 631 | | inputx_timer = timer_alloc(machine, inputx_timerproc, NULL); |
| 632 | | keybuffer = auto_alloc_clear(machine, key_buffer); |
| 633 | | add_exit_callback(machine, clear_keybuffer); |
| 634 | | } |
| 635 | | |
| 636 | | |
| 637 | | |
| 638 | | void inputx_init(running_machine *machine) |
| 639 | | { |
| 640 | | codes = NULL; |
| 641 | | inputx_timer = NULL; |
| 642 | | queue_chars = NULL; |
| 643 | | accept_char = NULL; |
| 644 | | charqueue_empty = NULL; |
| 645 | | keybuffer = NULL; |
| 646 | | |
| 647 | | if (machine->debug_flags & DEBUG_FLAG_ENABLED) |
| 648 | | { |
| 649 | | debug_console_register_command(machine, "input", CMDFLAG_NONE, 0, 1, 1, execute_input); |
| 650 | | debug_console_register_command(machine, "dumpkbd", CMDFLAG_NONE, 0, 0, 1, execute_dumpkbd); |
| 651 | | } |
| 652 | | |
| 653 | | /* posting keys directly only makes sense for a computer */ |
| 654 | | if (input_machine_have_keyboard(machine)) |
| 655 | | { |
| 656 | | codes = build_codes(machine, machine->portlist.first()); |
| 657 | | setup_keybuffer(machine); |
| 658 | | } |
| 659 | | } |
| 660 | | |
| 661 | | |
| 662 | | |
| 663 | | void inputx_setup_natural_keyboard( |
| 664 | | int (*queue_chars_)(const unicode_char *text, size_t text_len), |
| 665 | | int (*accept_char_)(unicode_char ch), |
| 666 | | int (*charqueue_empty_)(void)) |
| 667 | | { |
| 668 | | queue_chars = queue_chars_; |
| 669 | | accept_char = accept_char_; |
| 670 | | charqueue_empty = charqueue_empty_; |
| 671 | | } |
| 672 | | |
| 673 | | int inputx_can_post(running_machine *machine) |
| 674 | | { |
| 675 | | return queue_chars || codes; |
| 676 | | } |
| 677 | | |
| 678 | | static key_buffer *get_buffer(running_machine *machine) |
| 679 | | { |
| 680 | | assert(inputx_can_post(machine)); |
| 681 | | return (key_buffer *) keybuffer; |
| 682 | | } |
| 683 | | |
| 684 | | |
| 685 | | |
| 686 | | static const mess_input_code *find_code(unicode_char ch) |
| 687 | | { |
| 688 | | int i; |
| 689 | | |
| 690 | | assert(codes); |
| 691 | | for (i = 0; codes[i].ch; i++) |
| 692 | | { |
| 693 | | if (codes[i].ch == ch) |
| 694 | | return &codes[i]; |
| 695 | | } |
| 696 | | return NULL; |
| 697 | | } |
| 698 | | |
| 699 | | |
| 700 | | |
| 701 | | static int can_post_key_directly(unicode_char ch) |
| 702 | | { |
| 703 | | int rc = FALSE; |
| 704 | | const mess_input_code *code; |
| 705 | | |
| 706 | | if (queue_chars) |
| 707 | | { |
| 708 | | rc = accept_char ? accept_char(ch) : TRUE; |
| 709 | | } |
| 710 | | else |
| 711 | | { |
| 712 | | code = find_code(ch); |
| 713 | | if (code) |
| 714 | | rc = code->field[0] != NULL; |
| 715 | | } |
| 716 | | return rc; |
| 717 | | } |
| 718 | | |
| 719 | | |
| 720 | | |
| 721 | | static int can_post_key_alternate(unicode_char ch) |
| 722 | | { |
| 723 | | const char *s; |
| 724 | | const char_info *ci; |
| 725 | | unicode_char uchar; |
| 726 | | int rc; |
| 727 | | |
| 728 | | ci = find_charinfo(ch); |
| 729 | | s = ci ? ci->alternate : NULL; |
| 730 | | if (!s) |
| 731 | | return 0; |
| 732 | | |
| 733 | | while(*s) |
| 734 | | { |
| 735 | | rc = uchar_from_utf8(&uchar, s, strlen(s)); |
| 736 | | if (rc <= 0) |
| 737 | | return 0; |
| 738 | | if (!can_post_key_directly(uchar)) |
| 739 | | return 0; |
| 740 | | s += rc; |
| 741 | | } |
| 742 | | return 1; |
| 743 | | } |
| 744 | | |
| 745 | | |
| 746 | | |
| 747 | | int inputx_can_post_key(running_machine *machine, unicode_char ch) |
| 748 | | { |
| 749 | | return inputx_can_post(machine) && (can_post_key_directly(ch) || can_post_key_alternate(ch)); |
| 750 | | } |
| 751 | | |
| 752 | | |
| 753 | | |
| 754 | | static attotime choose_delay(unicode_char ch) |
| 755 | | { |
| 756 | | attoseconds_t delay = 0; |
| 757 | | |
| 758 | | if (attotime_compare(current_rate, attotime_zero) != 0) |
| 759 | | return current_rate; |
| 760 | | |
| 761 | | if (queue_chars) |
| 762 | | { |
| 763 | | /* systems with queue_chars can afford a much smaller delay */ |
| 764 | | delay = DOUBLE_TO_ATTOSECONDS(0.01); |
| 765 | | } |
| 766 | | else |
| 767 | | { |
| 768 | | switch(ch) { |
| 769 | | case '\r': |
| 770 | | delay = DOUBLE_TO_ATTOSECONDS(0.2); |
| 771 | | break; |
| 772 | | |
| 773 | | default: |
| 774 | | delay = DOUBLE_TO_ATTOSECONDS(0.05); |
| 775 | | break; |
| 776 | | } |
| 777 | | } |
| 778 | | return attotime_make(0, delay); |
| 779 | | } |
| 780 | | |
| 781 | | |
| 782 | | |
| 783 | | static void internal_post_key(running_machine *machine, unicode_char ch) |
| 784 | | { |
| 785 | | key_buffer *keybuf; |
| 786 | | |
| 787 | | keybuf = get_buffer(machine); |
| 788 | | |
| 789 | | /* need to start up the timer? */ |
| 790 | | if (keybuf->begin_pos == keybuf->end_pos) |
| 791 | | { |
| 792 | | timer_adjust_oneshot(inputx_timer, choose_delay(ch), 0); |
| 793 | | keybuf->status_keydown = 0; |
| 794 | | } |
| 795 | | |
| 796 | | keybuf->buffer[keybuf->end_pos++] = ch; |
| 797 | | keybuf->end_pos %= ARRAY_LENGTH(keybuf->buffer); |
| 798 | | } |
| 799 | | |
| 800 | | |
| 801 | | |
| 802 | | static int buffer_full(running_machine *machine) |
| 803 | | { |
| 804 | | key_buffer *keybuf; |
| 805 | | keybuf = get_buffer(machine); |
| 806 | | return ((keybuf->end_pos + 1) % ARRAY_LENGTH(keybuf->buffer)) == keybuf->begin_pos; |
| 807 | | } |
| 808 | | |
| 809 | | |
| 810 | | |
| 811 | | void inputx_postn_rate(running_machine *machine, const unicode_char *text, size_t text_len, attotime rate) |
| 812 | | { |
| 813 | | int last_cr = 0; |
| 814 | | unicode_char ch; |
| 815 | | const char *s; |
| 816 | | const char_info *ci; |
| 817 | | const mess_input_code *code; |
| 818 | | |
| 819 | | current_rate = rate; |
| 820 | | |
| 821 | | if (inputx_can_post(machine)) |
| 822 | | { |
| 823 | | while((text_len > 0) && !buffer_full(machine)) |
| 824 | | { |
| 825 | | ch = *(text++); |
| 826 | | text_len--; |
| 827 | | |
| 828 | | /* change all eolns to '\r' */ |
| 829 | | if ((ch != '\n') || !last_cr) |
| 830 | | { |
| 831 | | if (ch == '\n') |
| 832 | | ch = '\r'; |
| 833 | | else |
| 834 | | last_cr = (ch == '\r'); |
| 835 | | |
| 836 | | if (LOG_INPUTX) |
| 837 | | { |
| 838 | | code = find_code(ch); |
| 839 | | logerror("inputx_postn(): code=%i (%s) field->name='%s'\n", (int) ch, code_point_string(machine, ch), (code && code->field[0]) ? code->field[0]->name : "<null>"); |
| 840 | | } |
| 841 | | |
| 842 | | if (can_post_key_directly(ch)) |
| 843 | | { |
| 844 | | /* we can post this key in the queue directly */ |
| 845 | | internal_post_key(machine, ch); |
| 846 | | } |
| 847 | | else if (can_post_key_alternate(ch)) |
| 848 | | { |
| 849 | | /* we can post this key with an alternate representation */ |
| 850 | | ci = find_charinfo(ch); |
| 851 | | assert(ci && ci->alternate); |
| 852 | | s = ci->alternate; |
| 853 | | while(*s) |
| 854 | | { |
| 855 | | s += uchar_from_utf8(&ch, s, strlen(s)); |
| 856 | | internal_post_key(machine, ch); |
| 857 | | } |
| 858 | | } |
| 859 | | } |
| 860 | | else |
| 861 | | { |
| 862 | | last_cr = 0; |
| 863 | | } |
| 864 | | } |
| 865 | | } |
| 866 | | } |
| 867 | | |
| 868 | | |
| 869 | | |
| 870 | | static TIMER_CALLBACK(inputx_timerproc) |
| 871 | | { |
| 872 | | key_buffer *keybuf; |
| 873 | | attotime delay; |
| 874 | | |
| 875 | | keybuf = get_buffer(machine); |
| 876 | | |
| 877 | | if (queue_chars) |
| 878 | | { |
| 879 | | /* the driver has a queue_chars handler */ |
| 880 | | while((keybuf->begin_pos != keybuf->end_pos) && queue_chars(&keybuf->buffer[keybuf->begin_pos], 1)) |
| 881 | | { |
| 882 | | keybuf->begin_pos++; |
| 883 | | keybuf->begin_pos %= ARRAY_LENGTH(keybuf->buffer); |
| 884 | | |
| 885 | | if (attotime_compare(current_rate, attotime_zero) != 0) |
| 886 | | break; |
| 887 | | } |
| 888 | | } |
| 889 | | else |
| 890 | | { |
| 891 | | /* the driver does not have a queue_chars handler */ |
| 892 | | if (keybuf->status_keydown) |
| 893 | | { |
| 894 | | keybuf->status_keydown = FALSE; |
| 895 | | keybuf->begin_pos++; |
| 896 | | keybuf->begin_pos %= ARRAY_LENGTH(keybuf->buffer); |
| 897 | | } |
| 898 | | else |
| 899 | | { |
| 900 | | keybuf->status_keydown = TRUE; |
| 901 | | } |
| 902 | | } |
| 903 | | |
| 904 | | /* need to make sure timerproc is called again if buffer not empty */ |
| 905 | | if (keybuf->begin_pos != keybuf->end_pos) |
| 906 | | { |
| 907 | | delay = choose_delay(keybuf->buffer[keybuf->begin_pos]); |
| 908 | | timer_adjust_oneshot(inputx_timer, delay, 0); |
| 909 | | } |
| 910 | | } |
| 911 | | |
| 912 | | |
| 913 | | |
| 914 | | /*------------------------------------------------- |
| 915 | | mess_input_port_update_hook - hook function |
| 916 | | called from core to allow for natural keyboard |
| 917 | | -------------------------------------------------*/ |
| 918 | | |
| 919 | | void mess_input_port_update_hook(running_machine *machine, const input_port_config *port, input_port_value *digital) |
| 920 | | { |
| 921 | | const key_buffer *keybuf; |
| 922 | | const mess_input_code *code; |
| 923 | | unicode_char ch; |
| 924 | | int i; |
| 925 | | UINT32 value; |
| 926 | | |
| 927 | | if (inputx_can_post(machine)) |
| 928 | | { |
| 929 | | keybuf = get_buffer(machine); |
| 930 | | |
| 931 | | /* is the key down right now? */ |
| 932 | | if (keybuf->status_keydown && (keybuf->begin_pos != keybuf->end_pos)) |
| 933 | | { |
| 934 | | /* identify the character that is down right now, and its component codes */ |
| 935 | | ch = keybuf->buffer[keybuf->begin_pos]; |
| 936 | | code = find_code(ch); |
| 937 | | |
| 938 | | /* loop through this character's component codes */ |
| 939 | | if (code != NULL) |
| 940 | | { |
| 941 | | for (i = 0; i < ARRAY_LENGTH(code->field) && (code->field[i] != NULL); i++) |
| 942 | | { |
| 943 | | if (code->field[i]->port == port) |
| 944 | | { |
| 945 | | value = code->field[i]->mask; |
| 946 | | *digital |= value; |
| 947 | | } |
| 948 | | } |
| 949 | | } |
| 950 | | } |
| 951 | | } |
| 952 | | } |
| 953 | | |
| 954 | | |
| 955 | | |
| 956 | | /*------------------------------------------------- |
| 957 | | mess_get_keyboard_key_name - builds the name of |
| 958 | | a key based on natural keyboard characters |
| 959 | | -------------------------------------------------*/ |
| 960 | | |
| 961 | | astring *mess_get_keyboard_key_name(const input_field_config *field) |
| 962 | | { |
| 963 | | astring *result = astring_alloc(); |
| 964 | | int i; |
| 965 | | unicode_char ch; |
| 966 | | |
| 967 | | |
| 968 | | /* loop through each character on the field*/ |
| 969 | | for (i = 0; i < ARRAY_LENGTH(field->chars) && (field->chars[i] != '\0'); i++) |
| 970 | | { |
| 971 | | ch = get_keyboard_code(field, i); |
| 972 | | astring_printf(result, "%s%-*s ", astring_c(result), MAX(SPACE_COUNT - 1, 0), inputx_key_name(ch)); |
| 973 | | } |
| 974 | | |
| 975 | | /* trim extra spaces */ |
| 976 | | astring_trimspace(result); |
| 977 | | |
| 978 | | /* special case */ |
| 979 | | if (astring_len(result) == 0) |
| 980 | | astring_cpyc(result, "Unnamed Key"); |
| 981 | | |
| 982 | | return result; |
| 983 | | } |
| 984 | | |
| 985 | | |
| 986 | | |
| 987 | | /*------------------------------------------------- |
| 988 | | inputx_key_name - returns the name of a |
| 989 | | specific key |
| 990 | | -------------------------------------------------*/ |
| 991 | | |
| 992 | | const char *inputx_key_name(unicode_char ch) |
| 993 | | { |
| 994 | | static char buf[UTF8_CHAR_MAX + 1]; |
| 995 | | const char_info *ci; |
| 996 | | const char *result; |
| 997 | | int pos; |
| 998 | | |
| 999 | | ci = find_charinfo(ch); |
| 1000 | | result = ci ? ci->name : NULL; |
| 1001 | | |
| 1002 | | if (ci && ci->name) |
| 1003 | | { |
| 1004 | | result = ci->name; |
| 1005 | | } |
| 1006 | | else |
| 1007 | | { |
| 1008 | | if ((ch > 0x7F) || isprint(ch)) |
| 1009 | | { |
| 1010 | | pos = utf8_from_uchar(buf, ARRAY_LENGTH(buf), ch); |
| 1011 | | buf[pos] = '\0'; |
| 1012 | | result = buf; |
| 1013 | | } |
| 1014 | | else |
| 1015 | | result = "???"; |
| 1016 | | } |
| 1017 | | return result; |
| 1018 | | } |
| 1019 | | |
| 1020 | | |
| 1021 | | |
| 1022 | | /* --------------------------------------------------------------------- */ |
| 1023 | | |
| 1024 | | int inputx_is_posting(running_machine *machine) |
| 1025 | | { |
| 1026 | | const key_buffer *keybuf; |
| 1027 | | keybuf = get_buffer(machine); |
| 1028 | | return (keybuf->begin_pos != keybuf->end_pos) || (charqueue_empty && !charqueue_empty()); |
| 1029 | | } |
| 1030 | | |
| 1031 | | |
| 1032 | | |
| 1033 | | /*************************************************************************** |
| 1034 | | |
| 1035 | | Coded input |
| 1036 | | |
| 1037 | | ***************************************************************************/ |
| 1038 | | |
| 1039 | | void inputx_postn_coded_rate(running_machine *machine, const char *text, size_t text_len, attotime rate) |
| 1040 | | { |
| 1041 | | size_t i, j, key_len, increment; |
| 1042 | | unicode_char ch; |
| 1043 | | |
| 1044 | | static const struct |
| 1045 | | { |
| 1046 | | const char *key; |
| 1047 | | unicode_char code; |
| 1048 | | } codes[] = |
| 1049 | | { |
| 1050 | | { "BACKSPACE", 8 }, |
| 1051 | | { "BS", 8 }, |
| 1052 | | { "BKSP", 8 }, |
| 1053 | | { "DEL", UCHAR_MAMEKEY(DEL) }, |
| 1054 | | { "DELETE", UCHAR_MAMEKEY(DEL) }, |
| 1055 | | { "END", UCHAR_MAMEKEY(END) }, |
| 1056 | | { "ENTER", 13 }, |
| 1057 | | { "ESC", '\033' }, |
| 1058 | | { "HOME", UCHAR_MAMEKEY(HOME) }, |
| 1059 | | { "INS", UCHAR_MAMEKEY(INSERT) }, |
| 1060 | | { "INSERT", UCHAR_MAMEKEY(INSERT) }, |
| 1061 | | { "PGDN", UCHAR_MAMEKEY(PGDN) }, |
| 1062 | | { "PGUP", UCHAR_MAMEKEY(PGUP) }, |
| 1063 | | { "SPACE", 32 }, |
| 1064 | | { "TAB", 9 }, |
| 1065 | | { "F1", UCHAR_MAMEKEY(F1) }, |
| 1066 | | { "F2", UCHAR_MAMEKEY(F2) }, |
| 1067 | | { "F3", UCHAR_MAMEKEY(F3) }, |
| 1068 | | { "F4", UCHAR_MAMEKEY(F4) }, |
| 1069 | | { "F5", UCHAR_MAMEKEY(F5) }, |
| 1070 | | { "F6", UCHAR_MAMEKEY(F6) }, |
| 1071 | | { "F7", UCHAR_MAMEKEY(F7) }, |
| 1072 | | { "F8", UCHAR_MAMEKEY(F8) }, |
| 1073 | | { "F9", UCHAR_MAMEKEY(F9) }, |
| 1074 | | { "F10", UCHAR_MAMEKEY(F10) }, |
| 1075 | | { "F11", UCHAR_MAMEKEY(F11) }, |
| 1076 | | { "F12", UCHAR_MAMEKEY(F12) }, |
| 1077 | | { "QUOTE", '\"' } |
| 1078 | | }; |
| 1079 | | |
| 1080 | | i = 0; |
| 1081 | | while(i < text_len) |
| 1082 | | { |
| 1083 | | ch = text[i]; |
| 1084 | | increment = 1; |
| 1085 | | |
| 1086 | | if (ch == '{') |
| 1087 | | { |
| 1088 | | for (j = 0; j < ARRAY_LENGTH(codes); j++) |
| 1089 | | { |
| 1090 | | key_len = strlen(codes[j].key); |
| 1091 | | if (i + key_len + 2 <= text_len) |
| 1092 | | { |
| 1093 | | if (!memcmp(codes[j].key, &text[i + 1], key_len) && (text[i + key_len + 1] == '}')) |
| 1094 | | { |
| 1095 | | ch = codes[j].code; |
| 1096 | | increment = key_len + 2; |
| 1097 | | } |
| 1098 | | } |
| 1099 | | } |
| 1100 | | } |
| 1101 | | |
| 1102 | | if (ch) |
| 1103 | | inputx_postc_rate(machine, ch, rate); |
| 1104 | | i += increment; |
| 1105 | | } |
| 1106 | | } |
| 1107 | | |
| 1108 | | |
| 1109 | | |
| 1110 | | /*************************************************************************** |
| 1111 | | |
| 1112 | | Alternative calls |
| 1113 | | |
| 1114 | | ***************************************************************************/ |
| 1115 | | |
| 1116 | | void inputx_postn(running_machine *machine, const unicode_char *text, size_t text_len) |
| 1117 | | { |
| 1118 | | inputx_postn_rate(machine, text, text_len, attotime_make(0, 0)); |
| 1119 | | } |
| 1120 | | |
| 1121 | | |
| 1122 | | |
| 1123 | | void inputx_post_rate(running_machine *machine, const unicode_char *text, attotime rate) |
| 1124 | | { |
| 1125 | | size_t len = 0; |
| 1126 | | while(text[len]) |
| 1127 | | len++; |
| 1128 | | inputx_postn_rate(machine, text, len, rate); |
| 1129 | | } |
| 1130 | | |
| 1131 | | |
| 1132 | | |
| 1133 | | void inputx_post(running_machine *machine, const unicode_char *text) |
| 1134 | | { |
| 1135 | | inputx_post_rate(machine, text, attotime_make(0, 0)); |
| 1136 | | } |
| 1137 | | |
| 1138 | | |
| 1139 | | |
| 1140 | | void inputx_postc_rate(running_machine *machine, unicode_char ch, attotime rate) |
| 1141 | | { |
| 1142 | | inputx_postn_rate(machine, &ch, 1, rate); |
| 1143 | | } |
| 1144 | | |
| 1145 | | |
| 1146 | | |
| 1147 | | void inputx_postc(running_machine *machine, unicode_char ch) |
| 1148 | | { |
| 1149 | | inputx_postc_rate(machine, ch, attotime_make(0, 0)); |
| 1150 | | } |
| 1151 | | |
| 1152 | | |
| 1153 | | |
| 1154 | | void inputx_postn_utf16_rate(running_machine *machine, const utf16_char *text, size_t text_len, attotime rate) |
| 1155 | | { |
| 1156 | | size_t len = 0; |
| 1157 | | unicode_char c; |
| 1158 | | utf16_char w1, w2; |
| 1159 | | unicode_char buf[256]; |
| 1160 | | |
| 1161 | | while(text_len > 0) |
| 1162 | | { |
| 1163 | | if (len == ARRAY_LENGTH(buf)) |
| 1164 | | { |
| 1165 | | inputx_postn(machine, buf, len); |
| 1166 | | len = 0; |
| 1167 | | } |
| 1168 | | |
| 1169 | | w1 = *(text++); |
| 1170 | | text_len--; |
| 1171 | | |
| 1172 | | if ((w1 >= 0xd800) && (w1 <= 0xdfff)) |
| 1173 | | { |
| 1174 | | if (w1 <= 0xDBFF) |
| 1175 | | { |
| 1176 | | w2 = 0; |
| 1177 | | if (text_len > 0) |
| 1178 | | { |
| 1179 | | w2 = *(text++); |
| 1180 | | text_len--; |
| 1181 | | } |
| 1182 | | if ((w2 >= 0xdc00) && (w2 <= 0xdfff)) |
| 1183 | | { |
| 1184 | | c = w1 & 0x03ff; |
| 1185 | | c <<= 10; |
| 1186 | | c |= w2 & 0x03ff; |
| 1187 | | } |
| 1188 | | else |
| 1189 | | { |
| 1190 | | c = INVALID_CHAR; |
| 1191 | | } |
| 1192 | | } |
| 1193 | | else |
| 1194 | | { |
| 1195 | | c = INVALID_CHAR; |
| 1196 | | } |
| 1197 | | } |
| 1198 | | else |
| 1199 | | { |
| 1200 | | c = w1; |
| 1201 | | } |
| 1202 | | buf[len++] = c; |
| 1203 | | } |
| 1204 | | inputx_postn_rate(machine, buf, len, rate); |
| 1205 | | } |
| 1206 | | |
| 1207 | | |
| 1208 | | |
| 1209 | | void inputx_postn_utf16(running_machine *machine, const utf16_char *text, size_t text_len) |
| 1210 | | { |
| 1211 | | inputx_postn_utf16_rate(machine, text, text_len, attotime_make(0, 0)); |
| 1212 | | } |
| 1213 | | |
| 1214 | | |
| 1215 | | |
| 1216 | | void inputx_post_utf16_rate(running_machine *machine, const utf16_char *text, attotime rate) |
| 1217 | | { |
| 1218 | | size_t len = 0; |
| 1219 | | while(text[len]) |
| 1220 | | len++; |
| 1221 | | inputx_postn_utf16_rate(machine, text, len, rate); |
| 1222 | | } |
| 1223 | | |
| 1224 | | |
| 1225 | | |
| 1226 | | void inputx_post_utf16(running_machine *machine, const utf16_char *text) |
| 1227 | | { |
| 1228 | | inputx_post_utf16_rate(machine, text, attotime_make(0, 0)); |
| 1229 | | } |
| 1230 | | |
| 1231 | | |
| 1232 | | |
| 1233 | | void inputx_postn_utf8_rate(running_machine *machine, const char *text, size_t text_len, attotime rate) |
| 1234 | | { |
| 1235 | | size_t len = 0; |
| 1236 | | unicode_char buf[256]; |
| 1237 | | unicode_char c; |
| 1238 | | int rc; |
| 1239 | | |
| 1240 | | while(text_len > 0) |
| 1241 | | { |
| 1242 | | if (len == ARRAY_LENGTH(buf)) |
| 1243 | | { |
| 1244 | | inputx_postn(machine, buf, len); |
| 1245 | | len = 0; |
| 1246 | | } |
| 1247 | | |
| 1248 | | rc = uchar_from_utf8(&c, text, text_len); |
| 1249 | | if (rc < 0) |
| 1250 | | { |
| 1251 | | rc = 1; |
| 1252 | | c = INVALID_CHAR; |
| 1253 | | } |
| 1254 | | text += rc; |
| 1255 | | text_len -= rc; |
| 1256 | | buf[len++] = c; |
| 1257 | | } |
| 1258 | | inputx_postn_rate(machine, buf, len, rate); |
| 1259 | | } |
| 1260 | | |
| 1261 | | |
| 1262 | | |
| 1263 | | void inputx_postn_utf8(running_machine *machine, const char *text, size_t text_len) |
| 1264 | | { |
| 1265 | | inputx_postn_utf8_rate(machine, text, text_len, attotime_make(0, 0)); |
| 1266 | | } |
| 1267 | | |
| 1268 | | |
| 1269 | | |
| 1270 | | void inputx_post_utf8_rate(running_machine *machine, const char *text, attotime rate) |
| 1271 | | { |
| 1272 | | inputx_postn_utf8_rate(machine, text, strlen(text), rate); |
| 1273 | | } |
| 1274 | | |
| 1275 | | |
| 1276 | | |
| 1277 | | void inputx_post_utf8(running_machine *machine, const char *text) |
| 1278 | | { |
| 1279 | | inputx_post_utf8_rate(machine, text, attotime_make(0, 0)); |
| 1280 | | } |
| 1281 | | |
| 1282 | | |
| 1283 | | |
| 1284 | | void inputx_post_coded(running_machine *machine, const char *text) |
| 1285 | | { |
| 1286 | | inputx_postn_coded(machine, text, strlen(text)); |
| 1287 | | } |
| 1288 | | |
| 1289 | | |
| 1290 | | |
| 1291 | | void inputx_post_coded_rate(running_machine *machine, const char *text, attotime rate) |
| 1292 | | { |
| 1293 | | inputx_postn_coded_rate(machine, text, strlen(text), rate); |
| 1294 | | } |
| 1295 | | |
| 1296 | | |
| 1297 | | |
| 1298 | | void inputx_postn_coded(running_machine *machine, const char *text, size_t text_len) |
| 1299 | | { |
| 1300 | | inputx_postn_coded_rate(machine, text, text_len, attotime_make(0, 0)); |
| 1301 | | } |
| 1302 | | |
| 1303 | | |
| 1304 | | |
| 1305 | | /*************************************************************************** |
| 1306 | | |
| 1307 | | Other stuff |
| 1308 | | |
| 1309 | | This stuff is here more out of convienience than anything else |
| 1310 | | ***************************************************************************/ |
| 1311 | | |
| 1312 | | int input_classify_port(const input_field_config *field) |
| 1313 | | { |
| 1314 | | int result; |
| 1315 | | |
| 1316 | | if (field->category && (field->type != IPT_CATEGORY)) |
| 1317 | | return INPUT_CLASS_CATEGORIZED; |
| 1318 | | |
| 1319 | | switch(field->type) |
| 1320 | | { |
| 1321 | | case IPT_JOYSTICK_UP: |
| 1322 | | case IPT_JOYSTICK_DOWN: |
| 1323 | | case IPT_JOYSTICK_LEFT: |
| 1324 | | case IPT_JOYSTICK_RIGHT: |
| 1325 | | case IPT_JOYSTICKLEFT_UP: |
| 1326 | | case IPT_JOYSTICKLEFT_DOWN: |
| 1327 | | case IPT_JOYSTICKLEFT_LEFT: |
| 1328 | | case IPT_JOYSTICKLEFT_RIGHT: |
| 1329 | | case IPT_JOYSTICKRIGHT_UP: |
| 1330 | | case IPT_JOYSTICKRIGHT_DOWN: |
| 1331 | | case IPT_JOYSTICKRIGHT_LEFT: |
| 1332 | | case IPT_JOYSTICKRIGHT_RIGHT: |
| 1333 | | case IPT_BUTTON1: |
| 1334 | | case IPT_BUTTON2: |
| 1335 | | case IPT_BUTTON3: |
| 1336 | | case IPT_BUTTON4: |
| 1337 | | case IPT_BUTTON5: |
| 1338 | | case IPT_BUTTON6: |
| 1339 | | case IPT_BUTTON7: |
| 1340 | | case IPT_BUTTON8: |
| 1341 | | case IPT_BUTTON9: |
| 1342 | | case IPT_BUTTON10: |
| 1343 | | case IPT_AD_STICK_X: |
| 1344 | | case IPT_AD_STICK_Y: |
| 1345 | | case IPT_AD_STICK_Z: |
| 1346 | | case IPT_TRACKBALL_X: |
| 1347 | | case IPT_TRACKBALL_Y: |
| 1348 | | case IPT_LIGHTGUN_X: |
| 1349 | | case IPT_LIGHTGUN_Y: |
| 1350 | | case IPT_MOUSE_X: |
| 1351 | | case IPT_MOUSE_Y: |
| 1352 | | case IPT_START: |
| 1353 | | case IPT_SELECT: |
| 1354 | | result = INPUT_CLASS_CONTROLLER; |
| 1355 | | break; |
| 1356 | | |
| 1357 | | case IPT_KEYBOARD: |
| 1358 | | result = INPUT_CLASS_KEYBOARD; |
| 1359 | | break; |
| 1360 | | |
| 1361 | | case IPT_CONFIG: |
| 1362 | | result = INPUT_CLASS_CONFIG; |
| 1363 | | break; |
| 1364 | | |
| 1365 | | case IPT_DIPSWITCH: |
| 1366 | | result = INPUT_CLASS_DIPSWITCH; |
| 1367 | | break; |
| 1368 | | |
| 1369 | | case 0: |
| 1370 | | if (field->name && (field->name != (const char *) -1)) |
| 1371 | | result = INPUT_CLASS_MISC; |
| 1372 | | else |
| 1373 | | result = INPUT_CLASS_INTERNAL; |
| 1374 | | break; |
| 1375 | | |
| 1376 | | default: |
| 1377 | | result = INPUT_CLASS_INTERNAL; |
| 1378 | | break; |
| 1379 | | } |
| 1380 | | return result; |
| 1381 | | } |
| 1382 | | |
| 1383 | | |
| 1384 | | |
| 1385 | | int input_player_number(const input_field_config *port) |
| 1386 | | { |
| 1387 | | return port->player; |
| 1388 | | } |
| 1389 | | |
| 1390 | | |
| 1391 | | |
| 1392 | | /*------------------------------------------------- |
| 1393 | | input_has_input_class - checks to see if a |
| 1394 | | particular input class is present |
| 1395 | | -------------------------------------------------*/ |
| 1396 | | |
| 1397 | | int input_has_input_class(running_machine *machine, int inputclass) |
| 1398 | | { |
| 1399 | | const input_port_config *port; |
| 1400 | | const input_field_config *field; |
| 1401 | | |
| 1402 | | for (port = machine->portlist.first(); port != NULL; port = port->next) |
| 1403 | | { |
| 1404 | | for (field = port->fieldlist; field != NULL; field = field->next) |
| 1405 | | { |
| 1406 | | if (input_classify_port(field) == inputclass) |
| 1407 | | return TRUE; |
| 1408 | | } |
| 1409 | | } |
| 1410 | | return FALSE; |
| 1411 | | } |
| 1412 | | |
| 1413 | | |
| 1414 | | |
| 1415 | | /*------------------------------------------------- |
| 1416 | | input_count_players - counts the number of |
| 1417 | | active players |
| 1418 | | -------------------------------------------------*/ |
| 1419 | | |
| 1420 | | int input_count_players(running_machine *machine) |
| 1421 | | { |
| 1422 | | const input_port_config *port; |
| 1423 | | const input_field_config *field; |
| 1424 | | int joystick_count; |
| 1425 | | |
| 1426 | | joystick_count = 0; |
| 1427 | | for (port = machine->portlist.first(); port != NULL; port = port->next) |
| 1428 | | { |
| 1429 | | for (field = port->fieldlist; field != NULL; field = field->next) |
| 1430 | | { |
| 1431 | | if (input_classify_port(field) == INPUT_CLASS_CONTROLLER) |
| 1432 | | { |
| 1433 | | if (joystick_count <= field->player + 1) |
| 1434 | | joystick_count = field->player + 1; |
| 1435 | | } |
| 1436 | | } |
| 1437 | | } |
| 1438 | | return joystick_count; |
| 1439 | | } |
| 1440 | | |
| 1441 | | |
| 1442 | | |
| 1443 | | /*------------------------------------------------- |
| 1444 | | input_category_active - checks to see if a |
| 1445 | | specific category is active |
| 1446 | | -------------------------------------------------*/ |
| 1447 | | |
| 1448 | | int input_category_active(running_machine *machine, int category) |
| 1449 | | { |
| 1450 | | const input_port_config *port; |
| 1451 | | const input_field_config *field = NULL; |
| 1452 | | const input_setting_config *setting; |
| 1453 | | input_field_user_settings settings; |
| 1454 | | |
| 1455 | | assert(category >= 1); |
| 1456 | | |
| 1457 | | /* loop through the input ports */ |
| 1458 | | for (port = machine->portlist.first(); port != NULL; port = port->next) |
| 1459 | | { |
| 1460 | | for (field = port->fieldlist; field != NULL; field = field->next) |
| 1461 | | { |
| 1462 | | /* is this field a category? */ |
| 1463 | | if (field->type == IPT_CATEGORY) |
| 1464 | | { |
| 1465 | | /* get the settings value */ |
| 1466 | | input_field_get_user_settings(field, &settings); |
| 1467 | | |
| 1468 | | for (setting = field->settinglist; setting != NULL; setting = setting->next) |
| 1469 | | { |
| 1470 | | /* is this the category we want? if so, is this settings value correct? */ |
| 1471 | | if ((setting->category == category) && (settings.value == setting->value)) |
| 1472 | | return TRUE; |
| 1473 | | } |
| 1474 | | } |
| 1475 | | } |
| 1476 | | } |
| 1477 | | return FALSE; |
| 1478 | | } |
| 1479 | | |
| 1480 | | |
| 1481 | | |
| 1482 | | /*************************************************************************** |
| 1483 | | DEBUGGER SUPPORT |
| 1484 | | ***************************************************************************/ |
| 1485 | | |
| 1486 | | /*------------------------------------------------- |
| 1487 | | execute_input - debugger command to enter |
| 1488 | | natural keyboard input |
| 1489 | | -------------------------------------------------*/ |
| 1490 | | |
| 1491 | | static void execute_input(running_machine *machine, int ref, int params, const char *param[]) |
| 1492 | | { |
| 1493 | | inputx_post_coded(machine, param[0]); |
| 1494 | | } |
| 1495 | | |
| 1496 | | |
| 1497 | | |
| 1498 | | /*------------------------------------------------- |
| 1499 | | execute_dumpkbd - debugger command to natural |
| 1500 | | keyboard codes |
| 1501 | | -------------------------------------------------*/ |
| 1502 | | |
| 1503 | | static void execute_dumpkbd(running_machine *machine, int ref, int params, const char *param[]) |
| 1504 | | { |
| 1505 | | const char *filename; |
| 1506 | | FILE *file = NULL; |
| 1507 | | const mess_input_code *code; |
| 1508 | | char buffer[512]; |
| 1509 | | size_t pos; |
| 1510 | | int i, j; |
| 1511 | | size_t left_column_width = 24; |
| 1512 | | |
| 1513 | | /* was there a file specified? */ |
| 1514 | | filename = (params > 0) ? param[0] : NULL; |
| 1515 | | if (filename != NULL) |
| 1516 | | { |
| 1517 | | /* if so, open it */ |
| 1518 | | file = fopen(filename, "w"); |
| 1519 | | if (file == NULL) |
| 1520 | | { |
| 1521 | | debug_console_printf(machine, "Cannot open \"%s\"\n", filename); |
| 1522 | | return; |
| 1523 | | } |
| 1524 | | } |
| 1525 | | |
| 1526 | | if ((codes != NULL) && (codes[0].ch != 0)) |
| 1527 | | { |
| 1528 | | /* loop through all codes */ |
| 1529 | | for (i = 0; codes[i].ch; i++) |
| 1530 | | { |
| 1531 | | code = &codes[i]; |
| 1532 | | pos = 0; |
| 1533 | | |
| 1534 | | /* describe the character code */ |
| 1535 | | pos += snprintf(&buffer[pos], ARRAY_LENGTH(buffer) - pos, "%08X (%s) ", |
| 1536 | | code->ch, |
| 1537 | | code_point_string(machine, code->ch)); |
| 1538 | | |
| 1539 | | /* pad with spaces */ |
| 1540 | | while(pos < left_column_width) |
| 1541 | | buffer[pos++] = ' '; |
| 1542 | | buffer[pos] = '\0'; |
| 1543 | | |
| 1544 | | /* identify the keys used */ |
| 1545 | | for (j = 0; j < ARRAY_LENGTH(code->field) && (code->field[j] != NULL); j++) |
| 1546 | | { |
| 1547 | | pos += snprintf(&buffer[pos], ARRAY_LENGTH(buffer) - pos, "%s'%s'", |
| 1548 | | (j > 0) ? ", " : "", |
| 1549 | | code->field[j]->name); |
| 1550 | | } |
| 1551 | | |
| 1552 | | /* and output it as appropriate */ |
| 1553 | | if (file != NULL) |
| 1554 | | fprintf(file, "%s\n", buffer); |
| 1555 | | else |
| 1556 | | debug_console_printf(machine, "%s\n", buffer); |
| 1557 | | } |
| 1558 | | } |
| 1559 | | else |
| 1560 | | { |
| 1561 | | debug_console_printf(machine, "No natural keyboard support\n"); |
| 1562 | | } |
| 1563 | | |
| 1564 | | /* cleanup */ |
| 1565 | | if (file != NULL) |
| 1566 | | fclose(file); |
| 1567 | | |
| 1568 | | } |
src/mess/osd/sdl/glob.c
| r7335 | r7336 | |
| 1 | | /* |
| 2 | | * Copyright (c) 1989, 1993 |
| 3 | | * The Regents of the University of California. All rights reserved. |
| 4 | | * |
| 5 | | * This code is derived from software contributed to Berkeley by |
| 6 | | * Guido van Rossum. |
| 7 | | * |
| 8 | | * Redistribution and use in source and binary forms, with or without |
| 9 | | * modification, are permitted provided that the following conditions |
| 10 | | * are met: |
| 11 | | * 1. Redistributions of source code must retain the above copyright |
| 12 | | * notice, this list of conditions and the following disclaimer. |
| 13 | | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | | * notice, this list of conditions and the following disclaimer in the |
| 15 | | * documentation and/or other materials provided with the distribution. |
| 16 | | * 3. All advertising materials mentioning features or use of this software |
| 17 | | * must display the following acknowledgement: |
| 18 | | * This product includes software developed by the University of |
| 19 | | * California, Berkeley and its contributors. |
| 20 | | * 4. Neither the name of the University nor the names of its contributors |
| 21 | | * may be used to endorse or promote products derived from this software |
| 22 | | * without specific prior written permission. |
| 23 | | * |
| 24 | | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | | * SUCH DAMAGE. |
| 35 | | */ |
| 36 | | |
| 37 | | #if defined(LIBC_SCCS) && !defined(lint) |
| 38 | | static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; |
| 39 | | #endif /* LIBC_SCCS and not lint */ |
| 40 | | |
| 41 | | /* |
| 42 | | * glob(3) -- a superset of the one defined in POSIX 1003.2. |
| 43 | | * |
| 44 | | * The [!...] convention to negate a range is supported (SysV, Posix, ksh). |
| 45 | | * |
| 46 | | * Optional extra services, controlled by flags not defined by POSIX: |
| 47 | | * |
| 48 | | * GLOB_QUOTE: |
| 49 | | * Escaping convention: \ inhibits any special meaning the following |
| 50 | | * character might have (except \ at end of string is retained). |
| 51 | | * GLOB_MAGCHAR: |
| 52 | | * Set in gl_flags if pattern contained a globbing character. |
| 53 | | * GLOB_NOMAGIC: |
| 54 | | * Same as GLOB_NOCHECK, but it will only append pattern if it did |
| 55 | | * not contain any magic characters. [Used in csh style globbing] |
| 56 | | * GLOB_ALTDIRFUNC: |
| 57 | | * Use alternately specified directory access functions. |
| 58 | | * GLOB_TILDE: |
| 59 | | * expand ~user/foo to the /home/dir/of/user/foo |
| 60 | | * GLOB_BRACE: |
| 61 | | * expand {1,2}{a,b} to 1a 1b 2a 2b |
| 62 | | * gl_matchc: |
| 63 | | * Number of matches in the current invocation of glob. |
| 64 | | */ |
| 65 | | |
| 66 | | #ifdef WIN32 |
| 67 | | #ifdef _MSC_VER |
| 68 | | #define __STDC__ 0 |
| 69 | | #endif |
| 70 | | #else |
| 71 | | #include <sys/param.h> |
| 72 | | #endif |
| 73 | | #include <sys/stat.h> |
| 74 | | |
| 75 | | #include <ctype.h> |
| 76 | | |
| 77 | | #include "osdcore.h" |
| 78 | | #include "osdmess.h" |
| 79 | | |
| 80 | | #include <errno.h> |
| 81 | | #include "glob.h" |
| 82 | | #include <stdio.h> |
| 83 | | #include <stdlib.h> |
| 84 | | #include <string.h> |
| 85 | | |
| 86 | | #define DOLLAR '$' |
| 87 | | #define DOT '.' |
| 88 | | #define EOS '\0' |
| 89 | | #define LBRACKET '[' |
| 90 | | #define NOT '!' |
| 91 | | #define QUESTION '?' |
| 92 | | #define QUOTE '\\' |
| 93 | | #define RANGE '-' |
| 94 | | #define RBRACKET ']' |
| 95 | | #define SEP '/' |
| 96 | | #define STAR '*' |
| 97 | | #define TILDE '~' |
| 98 | | #define UNDERSCORE '_' |
| 99 | | #define LBRACE '{' |
| 100 | | #define RBRACE '}' |
| 101 | | #define SLASH '/' |
| 102 | | #define COMMA ',' |
| 103 | | |
| 104 | | #ifdef WIN32 |
| 105 | | #define S_ISLNK(s) (0) |
| 106 | | #undef SEP |
| 107 | | #define SEP '\\' |
| 108 | | struct passwd |
| 109 | | { |
| 110 | | char *pw_dir; |
| 111 | | }; |
| 112 | | #define getlogin() (NULL) |
| 113 | | #define getpwnam(h) (NULL) |
| 114 | | #define getpwuid(h) (NULL) |
| 115 | | #define getuid() (0) |
| 116 | | #define lstat stat |
| 117 | | typedef unsigned int u_int; |
| 118 | | typedef unsigned short u_short; |
| 119 | | typedef unsigned char u_char; |
| 120 | | #else |
| 121 | | #include <unistd.h> |
| 122 | | #include <pwd.h> |
| 123 | | #endif |
| 124 | | |
| 125 | | #ifndef DEBUG |
| 126 | | |
| 127 | | #define M_QUOTE 0x8000 |
| 128 | | #define M_PROTECT 0x4000 |
| 129 | | #define M_MASK 0xffff |
| 130 | | #define M_ASCII 0x00ff |
| 131 | | |
| 132 | | typedef u_short Char; |
| 133 | | |
| 134 | | #else |
| 135 | | |
| 136 | | #define M_QUOTE 0x80 |
| 137 | | #define M_PROTECT 0x40 |
| 138 | | #define M_MASK 0xff |
| 139 | | #define M_ASCII 0x7f |
| 140 | | |
| 141 | | typedef char Char; |
| 142 | | |
| 143 | | #endif |
| 144 | | |
| 145 | | |
| 146 | | #define CHAR(c) ((Char)((c)&M_ASCII)) |
| 147 | | #define META(c) ((Char)((c)|M_QUOTE)) |
| 148 | | #define M_ALL META('*') |
| 149 | | #define M_END META(']') |
| 150 | | #define M_NOT META('!') |
| 151 | | #define M_ONE META('?') |
| 152 | | #define M_RNG META('-') |
| 153 | | #define M_SET META('[') |
| 154 | | #define ismeta(c) (((c)&M_QUOTE) != 0) |
| 155 | | |
| 156 | | |
| 157 | | static int compare __P((const void *, const void *)); |
| 158 | | static void g_Ctoc __P((const Char *, char *)); |
| 159 | | //static int g_lstat __P((Char *, struct stat *, glob_t *)); |
| 160 | | static osd_directory *g_opendir __P((Char *, glob_t *)); |
| 161 | | static Char *g_strchr __P((Char *, int)); |
| 162 | | #ifdef notdef |
| 163 | | static Char *g_strcat __P((Char *, const Char *)); |
| 164 | | #endif |
| 165 | | //static int g_stat __P((Char *, struct stat *, glob_t *)); |
| 166 | | static int glob0 __P((const Char *, glob_t *)); |
| 167 | | static int glob1 __P((Char *, glob_t *)); |
| 168 | | static int glob2 __P((Char *, Char *, Char *, glob_t *)); |
| 169 | | static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *)); |
| 170 | | static int globextend __P((const Char *, glob_t *)); |
| 171 | | static const Char * globtilde __P((const Char *, Char *, size_t, glob_t *)); |
| 172 | | static int globexp1 __P((const Char *, glob_t *)); |
| 173 | | static int globexp2 __P((const Char *, const Char *, glob_t *, int *)); |
| 174 | | static int match __P((Char *, Char *, Char *)); |
| 175 | | #ifdef DEBUG |
| 176 | | static void qprintf __P((const char *, Char *)); |
| 177 | | #endif |
| 178 | | |
| 179 | | int glob (const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob) |
| 180 | | { |
| 181 | | const u_char *patnext; |
| 182 | | int c; |
| 183 | | Char *bufnext, *bufend, patbuf[_MAX_PATH+1]; |
| 184 | | |
| 185 | | patnext = (u_char *) pattern; |
| 186 | | if (!(flags & GLOB_APPEND)) { |
| 187 | | pglob->gl_pathc = 0; |
| 188 | | pglob->gl_pathv = NULL; |
| 189 | | if (!(flags & GLOB_DOOFFS)) |
| 190 | | pglob->gl_offs = 0; |
| 191 | | } |
| 192 | | pglob->gl_flags = flags & ~GLOB_MAGCHAR; |
| 193 | | pglob->gl_errfunc = errfunc; |
| 194 | | pglob->gl_matchc = 0; |
| 195 | | |
| 196 | | bufnext = patbuf; |
| 197 | | bufend = bufnext + _MAX_PATH; |
| 198 | | if (flags & GLOB_QUOTE) { |
| 199 | | /* Protect the quoted characters. */ |
| 200 | | while (bufnext < bufend && (c = *patnext++) != EOS) |
| 201 | | if (c == QUOTE) { |
| 202 | | if ((c = *patnext++) == EOS) { |
| 203 | | c = QUOTE; |
| 204 | | --patnext; |
| 205 | | } |
| 206 | | *bufnext++ = c | M_PROTECT; |
| 207 | | } |
| 208 | | else |
| 209 | | *bufnext++ = c; |
| 210 | | } |
| 211 | | else |
| 212 | | while (bufnext < bufend && (c = *patnext++) != EOS) |
| 213 | | *bufnext++ = c; |
| 214 | | *bufnext = EOS; |
| 215 | | |
| 216 | | if (flags & GLOB_BRACE) |
| 217 | | return globexp1(patbuf, pglob); |
| 218 | | else |
| 219 | | return glob0(patbuf, pglob); |
| 220 | | } |
| 221 | | |
| 222 | | /* |
| 223 | | * Expand recursively a glob {} pattern. When there is no more expansion |
| 224 | | * invoke the standard globbing routine to glob the rest of the magic |
| 225 | | * characters |
| 226 | | */ |
| 227 | | static int globexp1(const Char *pattern, glob_t *pglob) |
| 228 | | { |
| 229 | | const Char* ptr = (const Char *) pattern; |
| 230 | | int rv; |
| 231 | | |
| 232 | | /* Protect a single {}, for find(1), like csh */ |
| 233 | | if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) |
| 234 | | return glob0(pattern, pglob); |
| 235 | | |
| 236 | | while ((ptr = (Char *) g_strchr((Char *) ptr, LBRACE)) != NULL) |
| 237 | | if (!globexp2(ptr, pattern, pglob, &rv)) |
| 238 | | return rv; |
| 239 | | |
| 240 | | return glob0(pattern, pglob); |
| 241 | | } |
| 242 | | |
| 243 | | |
| 244 | | /* |
| 245 | | * Recursive brace globbing helper. Tries to expand a single brace. |
| 246 | | * If it succeeds then it invokes globexp1 with the new pattern. |
| 247 | | * If it fails then it tries to glob the rest of the pattern and returns. |
| 248 | | */ |
| 249 | | static int globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv) |
| 250 | | { |
| 251 | | int i; |
| 252 | | Char *lm, *ls; |
| 253 | | const Char *pe, *pm, *pl; |
| 254 | | Char patbuf[_MAX_PATH + 1]; |
| 255 | | |
| 256 | | /* copy part up to the brace */ |
| 257 | | for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) |
| 258 | | continue; |
| 259 | | ls = lm; |
| 260 | | |
| 261 | | /* Find the balanced brace */ |
| 262 | | for (i = 0, pe = ++ptr; *pe; pe++) |
| 263 | | if (*pe == LBRACKET) { |
| 264 | | /* Ignore everything between [] */ |
| 265 | | for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) |
| 266 | | continue; |
| 267 | | if (*pe == EOS) { |
| 268 | | /* |
| 269 | | * We could not find a matching RBRACKET. |
| 270 | | * Ignore and just look for RBRACE |
| 271 | | */ |
| 272 | | pe = pm; |
| 273 | | } |
| 274 | | } |
| 275 | | else if (*pe == LBRACE) |
| 276 | | i++; |
| 277 | | else if (*pe == RBRACE) { |
| 278 | | if (i == 0) |
| 279 | | break; |
| 280 | | i--; |
| 281 | | } |
| 282 | | |
| 283 | | /* Non matching braces; just glob the pattern */ |
| 284 | | if (i != 0 || *pe == EOS) { |
| 285 | | *rv = glob0(patbuf, pglob); |
| 286 | | return 0; |
| 287 | | } |
| 288 | | |
| 289 | | for (i = 0, pl = pm = ptr; pm <= pe; pm++) |
| 290 | | switch (*pm) { |
| 291 | | case LBRACKET: |
| 292 | | /* Ignore everything between [] */ |
| 293 | | for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++) |
| 294 | | continue; |
| 295 | | if (*pm == EOS) { |
| 296 | | /* |
| 297 | | * We could not find a matching RBRACKET. |
| 298 | | * Ignore and just look for RBRACE |
| 299 | | */ |
| 300 | | pm = pl; |
| 301 | | } |
| 302 | | break; |
| 303 | | |
| 304 | | case LBRACE: |
| 305 | | i++; |
| 306 | | break; |
| 307 | | |
| 308 | | case RBRACE: |
| 309 | | if (i) { |
| 310 | | i--; |
| 311 | | break; |
| 312 | | } |
| 313 | | /* FALLTHROUGH */ |
| 314 | | case COMMA: |
| 315 | | if (i && *pm == COMMA) |
| 316 | | break; |
| 317 | | else { |
| 318 | | /* Append the current string */ |
| 319 | | for (lm = ls; (pl < pm); *lm++ = *pl++) |
| 320 | | continue; |
| 321 | | /* |
| 322 | | * Append the rest of the pattern after the |
| 323 | | * closing brace |
| 324 | | */ |
| 325 | | for (pl = pe + 1; (*lm++ = *pl++) != EOS;) |
| 326 | | continue; |
| 327 | | |
| 328 | | /* Expand the current pattern */ |
| 329 | | #ifdef DEBUG |
| 330 | | qprintf("globexp2:", patbuf); |
| 331 | | #endif |
| 332 | | *rv = globexp1(patbuf, pglob); |
| 333 | | |
| 334 | | /* move after the comma, to the next string */ |
| 335 | | pl = pm + 1; |
| 336 | | } |
| 337 | | break; |
| 338 | | |
| 339 | | default: |
| 340 | | break; |
| 341 | | } |
| 342 | | *rv = 0; |
| 343 | | return 0; |
| 344 | | } |
| 345 | | |
| 346 | | |
| 347 | | |
| 348 | | /* |
| 349 | | * expand tilde from the passwd file. |
| 350 | | */ |
| 351 | | static const Char *globtilde(const Char *pattern,Char *patbuf, size_t patbuf_len, glob_t *pglob) |
| 352 | | { |
| 353 | | struct passwd *pwd; |
| 354 | | char *h; |
| 355 | | const Char *p; |
| 356 | | Char *b, *eb; |
| 357 | | |
| 358 | | if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) |
| 359 | | return pattern; |
| 360 | | |
| 361 | | /* |
| 362 | | * Copy up to the end of the string or / |
| 363 | | */ |
| 364 | | eb = &patbuf[patbuf_len - 1]; |
| 365 | | for (p = pattern + 1, h = (char *) patbuf; |
| 366 | | h < (char *)eb && *p && *p != SLASH; *h++ = *p++) |
| 367 | | continue; |
| 368 | | |
| 369 | | *h = EOS; |
| 370 | | |
| 371 | | if (((char *) patbuf)[0] == EOS) { |
| 372 | | /* |
| 373 | | * handle a plain ~ or ~/ by expanding $HOME first (iff |
| 374 | | * we're not running setuid or setgid) and then trying |
| 375 | | * the password file |
| 376 | | */ |
| 377 | | if ( |
| 378 | | (h = getenv("HOME")) == NULL) { |
| 379 | | if (((h = getlogin()) != NULL && |
| 380 | | (pwd = getpwnam(h)) != NULL) || |
| 381 | | (pwd = getpwuid(getuid())) != NULL) |
| 382 | | h = pwd->pw_dir; |
| 383 | | else |
| 384 | | return pattern; |
| 385 | | } |
| 386 | | } |
| 387 | | else { |
| 388 | | /* |
| 389 | | * Expand a ~user |
| 390 | | */ |
| 391 | | if ((pwd = getpwnam((char*) patbuf)) == NULL) |
| 392 | | return pattern; |
| 393 | | else |
| 394 | | h = pwd->pw_dir; |
| 395 | | } |
| 396 | | |
| 397 | | /* Copy the home directory */ |
| 398 | | for (b = patbuf; b < eb && *h; *b++ = *h++) |
| 399 | | continue; |
| 400 | | |
| 401 | | /* Append the rest of the pattern */ |
| 402 | | while (b < eb && (*b++ = *p++) != EOS) |
| 403 | | continue; |
| 404 | | *b = EOS; |
| 405 | | |
| 406 | | return patbuf; |
| 407 | | } |
| 408 | | |
| 409 | | |
| 410 | | /* |
| 411 | | * The main glob() routine: compiles the pattern (optionally processing |
| 412 | | * quotes), calls glob1() to do the real pattern matching, and finally |
| 413 | | * sorts the list (unless unsorted operation is requested). Returns 0 |
| 414 | | * if things went well, nonzero if errors occurred. It is not an error |
| 415 | | * to find no matches. |
| 416 | | */ |
| 417 | | static int glob0(const Char *pattern,glob_t *pglob) |
| 418 | | { |
| 419 | | const Char *qpatnext; |
| 420 | | int c, err, oldpathc; |
| 421 | | Char *bufnext, patbuf[_MAX_PATH+1]; |
| 422 | | |
| 423 | | qpatnext = globtilde(pattern, patbuf, ARRAY_LENGTH(patbuf), pglob); |
| 424 | | oldpathc = pglob->gl_pathc; |
| 425 | | bufnext = patbuf; |
| 426 | | |
| 427 | | /* We don't need to check for buffer overflow any more. */ |
| 428 | | while ((c = *qpatnext++) != EOS) { |
| 429 | | switch (c) { |
| 430 | | case LBRACKET: |
| 431 | | c = *qpatnext; |
| 432 | | if (c == NOT) |
| 433 | | ++qpatnext; |
| 434 | | if (*qpatnext == EOS || |
| 435 | | g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) { |
| 436 | | *bufnext++ = LBRACKET; |
| 437 | | if (c == NOT) |
| 438 | | --qpatnext; |
| 439 | | break; |
| 440 | | } |
| 441 | | *bufnext++ = M_SET; |
| 442 | | if (c == NOT) |
| 443 | | *bufnext++ = M_NOT; |
| 444 | | c = *qpatnext++; |
| 445 | | do { |
| 446 | | *bufnext++ = CHAR(c); |
| 447 | | if (*qpatnext == RANGE && |
| 448 | | (c = qpatnext[1]) != RBRACKET) { |
| 449 | | *bufnext++ = M_RNG; |
| 450 | | *bufnext++ = CHAR(c); |
| 451 | | qpatnext += 2; |
| 452 | | } |
| 453 | | } while ((c = *qpatnext++) != RBRACKET); |
| 454 | | pglob->gl_flags |= GLOB_MAGCHAR; |
| 455 | | *bufnext++ = M_END; |
| 456 | | break; |
| 457 | | case QUESTION: |
| 458 | | pglob->gl_flags |= GLOB_MAGCHAR; |
| 459 | | *bufnext++ = M_ONE; |
| 460 | | break; |
| 461 | | case STAR: |
| 462 | | pglob->gl_flags |= GLOB_MAGCHAR; |
| 463 | | /* collapse adjacent stars to one, |
| 464 | | * to avoid exponential behavior |
| 465 | | */ |
| 466 | | if (bufnext == patbuf || bufnext[-1] != M_ALL) |
| 467 | | *bufnext++ = M_ALL; |
| 468 | | break; |
| 469 | | default: |
| 470 | | *bufnext++ = CHAR(c); |
| 471 | | break; |
| 472 | | } |
| 473 | | } |
| 474 | | *bufnext = EOS; |
| 475 | | #ifdef DEBUG |
| 476 | | qprintf("glob0:", patbuf); |
| 477 | | #endif |
| 478 | | |
| 479 | | if ((err = glob1(patbuf, pglob)) != 0) |
| 480 | | return(err); |
| 481 | | |
| 482 | | /* |
| 483 | | * If there was no match we are going to append the pattern |
| 484 | | * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified |
| 485 | | * and the pattern did not contain any magic characters |
| 486 | | * GLOB_NOMAGIC is there just for compatibility with csh. |
| 487 | | */ |
| 488 | | if (pglob->gl_pathc == oldpathc && |
| 489 | | ((pglob->gl_flags & GLOB_NOCHECK) || |
| 490 | | ((pglob->gl_flags & GLOB_NOMAGIC) && |
| 491 | | !(pglob->gl_flags & GLOB_MAGCHAR)))) |
| 492 | | return(globextend(pattern, pglob)); |
| 493 | | else if (!(pglob->gl_flags & GLOB_NOSORT)) |
| 494 | | qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, |
| 495 | | pglob->gl_pathc - oldpathc, sizeof(char *), compare); |
| 496 | | return(0); |
| 497 | | } |
| 498 | | |
| 499 | | static int compare(const void *p, const void *q) |
| 500 | | { |
| 501 | | return(strcmp(*(char **)p, *(char **)q)); |
| 502 | | } |
| 503 | | |
| 504 | | static int glob1(Char *pattern,glob_t *pglob) |
| 505 | | { |
| 506 | | Char pathbuf[_MAX_PATH+1]; |
| 507 | | |
| 508 | | /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ |
| 509 | | if (*pattern == EOS) |
| 510 | | return(0); |
| 511 | | return(glob2(pathbuf, pathbuf, pattern, pglob)); |
| 512 | | } |
| 513 | | |
| 514 | | /* |
| 515 | | * The functions glob2 and glob3 are mutually recursive; there is one level |
| 516 | | * of recursion for each segment in the pattern that contains one or more |
| 517 | | * meta characters. |
| 518 | | */ |
| 519 | | static int glob2(Char *pathbuf, Char *pathend, Char *pattern,glob_t *pglob) |
| 520 | | { |
| 521 | | Char *p, *q; |
| 522 | | int anymeta; |
| 523 | | osd_directory_entry *entry = NULL; |
| 524 | | osd_directory_entry *entry2 = NULL; |
| 525 | | char buf[_MAX_PATH]; |
| 526 | | |
| 527 | | /* |
| 528 | | * Loop over pattern segments until end of pattern or until |
| 529 | | * segment with meta character found. |
| 530 | | */ |
| 531 | | for (anymeta = 0;;) { |
| 532 | | if (*pattern == EOS) { /* End of pattern? */ |
| 533 | | *pathend = EOS; |
| 534 | | g_Ctoc(pathbuf, buf); |
| 535 | | entry = osd_stat(buf); |
| 536 | | if (!entry) |
| 537 | | return(0); |
| 538 | | |
| 539 | | if ((((pglob->gl_flags & GLOB_MARK) && |
| 540 | | pathend[-1] != SEP) && (entry->type == ENTTYPE_DIR)) |
| 541 | | || ((S_ISLNK(sb.st_mode) && |
| 542 | | ((entry2 = osd_stat(buf)) != NULL) && |
| 543 | | (entry2->type == ENTTYPE_DIR)))) { |
| 544 | | *pathend++ = SEP; |
| 545 | | *pathend = EOS; |
| 546 | | } |
| 547 | | free(entry); |
| 548 | | if (entry2) |
| 549 | | free(entry2); |
| 550 | | ++pglob->gl_matchc; |
| 551 | | return(globextend(pathbuf, pglob)); |
| 552 | | } |
| 553 | | |
| 554 | | /* Find end of next segment, copy tentatively to pathend. */ |
| 555 | | q = pathend; |
| 556 | | p = pattern; |
| 557 | | while (*p != EOS && *p != SEP) { |
| 558 | | if (ismeta(*p)) |
| 559 | | anymeta = 1; |
| 560 | | *q++ = *p++; |
| 561 | | } |
| 562 | | |
| 563 | | if (!anymeta) { /* No expansion, do next segment. */ |
| 564 | | pathend = q; |
| 565 | | pattern = p; |
| 566 | | while (*pattern == SEP) |
| 567 | | *pathend++ = *pattern++; |
| 568 | | } else /* Need expansion, recurse. */ |
| 569 | | return(glob3(pathbuf, pathend, pattern, p, pglob)); |
| 570 | | } |
| 571 | | /* NOTREACHED */ |
| 572 | | } |
| 573 | | |
| 574 | | static int glob3(Char *pathbuf,Char *pathend,Char *pattern,Char *restpattern,glob_t *pglob) |
| 575 | | { |
| 576 | | register const osd_directory_entry *dp; |
| 577 | | osd_directory *dirp; |
| 578 | | int err; |
| 579 | | char buf[_MAX_PATH]; |
| 580 | | |
| 581 | | /* |
| 582 | | * The readdirfunc declaration can't be prototyped, because it is |
| 583 | | * assigned, below, to two functions which are prototyped in glob.h |
| 584 | | * and dirent.h as taking pointers to differently typed opaque |
| 585 | | * structures. |
| 586 | | */ |
| 587 | | typedef const osd_directory_entry *(*readdirfunc_t)(osd_directory *); |
| 588 | | readdirfunc_t readdirfunc; |
| 589 | | |
| 590 | | *pathend = EOS; |
| 591 | | errno = 0; |
| 592 | | |
| 593 | | if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { |
| 594 | | /* TODO: don't call for ENOENT or ENOTDIR? */ |
| 595 | | if (pglob->gl_errfunc) { |
| 596 | | g_Ctoc(pathbuf, buf); |
| 597 | | if (pglob->gl_errfunc(buf, errno) || |
| 598 | | pglob->gl_flags & GLOB_ERR) |
| 599 | | return (GLOB_ABEND); |
| 600 | | } |
| 601 | | return(0); |
| 602 | | } |
| 603 | | |
| 604 | | err = 0; |
| 605 | | |
| 606 | | /* Search directory for matching names. */ |
| 607 | | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| 608 | | readdirfunc = (readdirfunc_t) pglob->gl_readdir; |
| 609 | | else |
| 610 | | readdirfunc = osd_readdir; |
| 611 | | while ((dp = (*readdirfunc)(dirp))) { |
| 612 | | register u_char *sc; |
| 613 | | register Char *dc; |
| 614 | | |
| 615 | | /* Initial DOT must be matched literally. */ |
| 616 | | if (dp->name[0] == DOT && *pattern != DOT) |
| 617 | | continue; |
| 618 | | for (sc = (u_char *) dp->name, dc = pathend; |
| 619 | | (*dc++ = *sc++) != EOS;) |
| 620 | | continue; |
| 621 | | if (!match(pathend, pattern, restpattern)) { |
| 622 | | *pathend = EOS; |
| 623 | | continue; |
| 624 | | } |
| 625 | | err = glob2(pathbuf, --dc, restpattern, pglob); |
| 626 | | if (err) |
| 627 | | break; |
| 628 | | } |
| 629 | | |
| 630 | | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| 631 | | (*pglob->gl_closedir)(dirp); |
| 632 | | else |
| 633 | | osd_closedir(dirp); |
| 634 | | return(err); |
| 635 | | } |
| 636 | | |
| 637 | | |
| 638 | | /* |
| 639 | | * Extend the gl_pathv member of a glob_t structure to accomodate a new item, |
| 640 | | * add the new item, and update gl_pathc. |
| 641 | | * |
| 642 | | * This assumes the BSD realloc, which only copies the block when its size |
| 643 | | * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic |
| 644 | | * behavior. |
| 645 | | * |
| 646 | | * Return 0 if new item added, error code if memory couldn't be allocated. |
| 647 | | * |
| 648 | | * Invariant of the glob_t structure: |
| 649 | | * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and |
| 650 | | * gl_pathv points to (gl_offs + gl_pathc + 1) items. |
| 651 | | */ |
| 652 | | static int globextend(const Char *path, glob_t *pglob) |
| 653 | | { |
| 654 | | register char **pathv; |
| 655 | | register int i; |
| 656 | | u_int newsize; |
| 657 | | char *copy; |
| 658 | | const Char *p; |
| 659 | | |
| 660 | | newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); |
| 661 | | pathv = pglob->gl_pathv ? |
| 662 | | (char **)realloc(pglob->gl_pathv, newsize) : |
| 663 | | (char **)malloc(newsize); |
| 664 | | if (pathv == NULL) |
| 665 | | return(GLOB_NOSPACE); |
| 666 | | |
| 667 | | if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { |
| 668 | | /* first time around -- clear initial gl_offs items */ |
| 669 | | pathv += pglob->gl_offs; |
| 670 | | for (i = pglob->gl_offs; --i >= 0; ) |
| 671 | | *--pathv = NULL; |
| 672 | | } |
| 673 | | pglob->gl_pathv = pathv; |
| 674 | | |
| 675 | | for (p = path; *p++;) |
| 676 | | continue; |
| 677 | | if ((copy = (char *)malloc(p - path)) != NULL) { |
| 678 | | g_Ctoc(path, copy); |
| 679 | | pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; |
| 680 | | } |
| 681 | | pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; |
| 682 | | return(copy == NULL ? GLOB_NOSPACE : 0); |
| 683 | | } |
| 684 | | |
| 685 | | /* |
| 686 | | * pattern matching function for filenames. Each occurrence of the * |
| 687 | | * pattern causes a recursion level. |
| 688 | | */ |
| 689 | | static int match(register Char *name, register Char *pat, register Char *patend) |
| 690 | | { |
| 691 | | int ok, negate_range; |
| 692 | | Char c, k; |
| 693 | | |
| 694 | | while (pat < patend) { |
| 695 | | c = *pat++; |
| 696 | | switch (c & M_MASK) { |
| 697 | | case M_ALL: |
| 698 | | if (pat == patend) |
| 699 | | return(1); |
| 700 | | do |
| 701 | | if (match(name, pat, patend)) |
| 702 | | return(1); |
| 703 | | while (*name++ != EOS); |
| 704 | | return(0); |
| 705 | | case M_ONE: |
| 706 | | if (*name++ == EOS) |
| 707 | | return(0); |
| 708 | | break; |
| 709 | | case M_SET: |
| 710 | | ok = 0; |
| 711 | | if ((k = *name++) == EOS) |
| 712 | | return(0); |
| 713 | | if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) |
| 714 | | ++pat; |
| 715 | | while (((c = *pat++) & M_MASK) != M_END) |
| 716 | | if ((*pat & M_MASK) == M_RNG) { |
| 717 | | if (CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1])) |
| 718 | | ok = 1; |
| 719 | | pat += 2; |
| 720 | | } else if (c == k) |
| 721 | | ok = 1; |
| 722 | | if (ok == negate_range) |
| 723 | | return(0); |
| 724 | | break; |
| 725 | | default: |
| 726 | | if (*name++ != c) |
| 727 | | return(0); |
| 728 | | break; |
| 729 | | } |
| 730 | | } |
| 731 | | return(*name == EOS); |
| 732 | | } |
| 733 | | |
| 734 | | /* Free allocated data belonging to a glob_t structure. */ |
| 735 | | void globfree(glob_t *pglob) |
| 736 | | { |
| 737 | | register int i; |
| 738 | | register char **pp; |
| 739 | | |
| 740 | | if (pglob->gl_pathv != NULL) { |
| 741 | | pp = pglob->gl_pathv + pglob->gl_offs; |
| 742 | | for (i = pglob->gl_pathc; i--; ++pp) |
| 743 | | if (*pp) |
| 744 | | free(*pp); |
| 745 | | free(pglob->gl_pathv); |
| 746 | | } |
| 747 | | } |
| 748 | | |
| 749 | | static osd_directory *g_opendir(register Char *str,glob_t *pglob) |
| 750 | | { |
| 751 | | char buf[_MAX_PATH]; |
| 752 | | |
| 753 | | if (!*str) |
| 754 | | strcpy(buf, "."); |
| 755 | | else |
| 756 | | g_Ctoc(str, buf); |
| 757 | | |
| 758 | | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| 759 | | return((osd_directory *)(*pglob->gl_opendir)(buf)); |
| 760 | | |
| 761 | | return osd_opendir(buf); |
| 762 | | } |
| 763 | | |
| 764 | | static Char *g_strchr(Char *str,int ch) |
| 765 | | { |
| 766 | | do { |
| 767 | | if (*str == ch) |
| 768 | | return (str); |
| 769 | | } while (*str++); |
| 770 | | return (NULL); |
| 771 | | } |
| 772 | | |
| 773 | | #ifdef notdef |
| 774 | | static Char * |
| 775 | | g_strcat(dst, src) |
| 776 | | Char *dst; |
| 777 | | const Char* src; |
| 778 | | { |
| 779 | | Char *sdst = dst; |
| 780 | | |
| 781 | | while (*dst++) |
| 782 | | continue; |
| 783 | | --dst; |
| 784 | | while((*dst++ = *src++) != EOS) |
| 785 | | continue; |
| 786 | | |
| 787 | | return (sdst); |
| 788 | | } |
| 789 | | #endif |
| 790 | | |
| 791 | | static void g_Ctoc(register const Char *str, char *buf) |
| 792 | | { |
| 793 | | register char *dc; |
| 794 | | |
| 795 | | for (dc = buf; (*dc++ = *str++) != EOS;) |
| 796 | | continue; |
| 797 | | } |
| 798 | | |
| 799 | | #ifdef DEBUG |
| 800 | | static void |
| 801 | | qprintf(str, s) |
| 802 | | const char *str; |
| 803 | | register Char *s; |
| 804 | | { |
| 805 | | register Char *p; |
| 806 | | |
| 807 | | (void)printf("%s:\n", str); |
| 808 | | for (p = s; *p; p++) |
| 809 | | (void)printf("%c", CHAR(*p)); |
| 810 | | (void)printf("\n"); |
| 811 | | for (p = s; *p; p++) |
| 812 | | (void)printf("%c", *p & M_PROTECT ? '"' : ' '); |
| 813 | | (void)printf("\n"); |
| 814 | | for (p = s; *p; p++) |
| 815 | | (void)printf("%c", ismeta(*p) ? '_' : ' '); |
| 816 | | (void)printf("\n"); |
| 817 | | } |
| 818 | | #endif |
src/mess/osd/sdl/glob.h
| r7335 | r7336 | |
| 1 | | /* |
| 2 | | * Copyright (c) 1991, 1993 |
| 3 | | * The Regents of the University of California. All rights reserved. |
| 4 | | * |
| 5 | | * This code is derived from software contributed to Berkeley by |
| 6 | | * Berkeley Software Design, Inc. |
| 7 | | * |
| 8 | | * Redistribution and use in source and binary forms, with or without |
| 9 | | * modification, are permitted provided that the following conditions |
| 10 | | * are met: |
| 11 | | * 1. Redistributions of source code must retain the above copyright |
| 12 | | * notice, this list of conditions and the following disclaimer. |
| 13 | | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | | * notice, this list of conditions and the following disclaimer in the |
| 15 | | * documentation and/or other materials provided with the distribution. |
| 16 | | * 3. All advertising materials mentioning features or use of this software |
| 17 | | * must display the following acknowledgement: |
| 18 | | * This product includes software developed by the University of |
| 19 | | * California, Berkeley and its contributors. |
| 20 | | * 4. Neither the name of the University nor the names of its contributors |
| 21 | | * may be used to endorse or promote products derived from this software |
| 22 | | * without specific prior written permission. |
| 23 | | * |
| 24 | | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | | * SUCH DAMAGE. |
| 35 | | * |
| 36 | | * @(#)cdefs.h 8.8 (Berkeley) 1/9/95 |
| 37 | | * $FreeBSD: src/sys/sys/cdefs.h,v 1.28.2.1 2000/03/18 23:30:03 jasone Exp $ |
| 38 | | */ |
| 39 | | |
| 40 | | #ifndef _SYS_CDEFS_H /* guard against newer GLIBC too */ |
| 41 | | #define _SYS_CDEFS_H |
| 42 | | |
| 43 | | #ifndef _SYS_CDEFS_H_ |
| 44 | | #define _SYS_CDEFS_H_ |
| 45 | | |
| 46 | | #if defined(__cplusplus) |
| 47 | | #define __BEGIN_DECLS extern "C" { |
| 48 | | #define __END_DECLS } |
| 49 | | #else |
| 50 | | #define __BEGIN_DECLS |
| 51 | | #define __END_DECLS |
| 52 | | #endif |
| 53 | | |
| 54 | | /* |
| 55 | | * The __CONCAT macro is used to concatenate parts of symbol names, e.g. |
| 56 | | * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo. |
| 57 | | * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI |
| 58 | | * mode -- there must be no spaces between its arguments, and for nested |
| 59 | | * __CONCAT's, all the __CONCAT's must be at the left. __CONCAT can also |
| 60 | | * concatenate double-quoted strings produced by the __STRING macro, but |
| 61 | | * this only works with ANSI C. |
| 62 | | * |
| 63 | | * __XSTRING is like __STRING, but it expands any macros in its argument |
| 64 | | * first. It is only available with ANSI C. |
| 65 | | */ |
| 66 | | #if defined(__STDC__) || defined(__cplusplus) |
| 67 | | #define __P(protos) protos /* full-blown ANSI C */ |
| 68 | | #define __STRING(x) #x /* stringify without expanding x */ |
| 69 | | #define __XSTRING(x) __STRING(x) /* expand x, then stringify */ |
| 70 | | |
| 71 | | #define __const const /* define reserved names to standard */ |
| 72 | | #define __signed signed |
| 73 | | #define __volatile volatile |
| 74 | | #if defined(__cplusplus) |
| 75 | | #define __inline inline /* convert to C++ keyword */ |
| 76 | | #else |
| 77 | | #ifndef __GNUC__ |
| 78 | | #define __inline /* delete GCC keyword */ |
| 79 | | #endif /* !__GNUC__ */ |
| 80 | | #endif /* !__cplusplus */ |
| 81 | | |
| 82 | | #else /* !(__STDC__ || __cplusplus) */ |
| 83 | | #define __P(protos) () /* traditional C preprocessor */ |
| 84 | | #define __STRING(x) "x" |
| 85 | | |
| 86 | | #ifndef __GNUC__ |
| 87 | | #define __const /* delete pseudo-ANSI C keywords */ |
| 88 | | #define __inline |
| 89 | | #define __signed |
| 90 | | #define __volatile |
| 91 | | /* |
| 92 | | * In non-ANSI C environments, new programs will want ANSI-only C keywords |
| 93 | | * deleted from the program and old programs will want them left alone. |
| 94 | | * When using a compiler other than gcc, programs using the ANSI C keywords |
| 95 | | * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS. |
| 96 | | * When using "gcc -traditional", we assume that this is the intent; if |
| 97 | | * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone. |
| 98 | | */ |
| 99 | | #ifndef NO_ANSI_KEYWORDS |
| 100 | | #define const /* delete ANSI C keywords */ |
| 101 | | #ifndef inline |
| 102 | | #define inline |
| 103 | | #endif |
| 104 | | #define signed |
| 105 | | #define volatile |
| 106 | | #endif /* !NO_ANSI_KEYWORDS */ |
| 107 | | #endif /* !__GNUC__ */ |
| 108 | | #endif /* !(__STDC__ || __cplusplus) */ |
| 109 | | |
| 110 | | /* |
| 111 | | * Compiler-dependent macros to help declare dead (non-returning) and |
| 112 | | * pure (no side effects) functions, and unused variables. They are |
| 113 | | * null except for versions of gcc that are known to support the features |
| 114 | | * properly (old versions of gcc-2 supported the dead and pure features |
| 115 | | * in a different (wrong) way). |
| 116 | | */ |
| 117 | | #if __GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 5 |
| 118 | | #define __dead2 |
| 119 | | #define __pure2 |
| 120 | | #define __unused |
| 121 | | #endif |
| 122 | | #if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 && __GNUC_MINOR__ < 7 |
| 123 | | #define __dead2 __attribute__((__noreturn__)) |
| 124 | | #define __pure2 __attribute__((__const__)) |
| 125 | | #define __unused |
| 126 | | #endif |
| 127 | | #if __GNUC__ == 2 && __GNUC_MINOR__ >= 7 |
| 128 | | #define __dead2 __attribute__((__noreturn__)) |
| 129 | | #define __pure2 __attribute__((__const__)) |
| 130 | | #define __unused __attribute__((__unused__)) |
| 131 | | #endif |
| 132 | | |
| 133 | | /* |
| 134 | | * Compiler-dependent macros to declare that functions take printf-like |
| 135 | | * or scanf-like arguments. They are null except for versions of gcc |
| 136 | | * that are known to support the features properly (old versions of gcc-2 |
| 137 | | * didn't permit keeping the keywords out of the application namespace). |
| 138 | | */ |
| 139 | | #if __GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 7 |
| 140 | | #define __printflike(fmtarg, firstvararg) |
| 141 | | #define __scanflike(fmtarg, firstvararg) |
| 142 | | #else |
| 143 | | #define __printflike(fmtarg, firstvararg) \ |
| 144 | | __attribute__((__format__ (__printf__, fmtarg, firstvararg))) |
| 145 | | #define __scanflike(fmtarg, firstvararg) \ |
| 146 | | __attribute__((__format__ (__scanf__, fmtarg, firstvararg))) |
| 147 | | #endif |
| 148 | | |
| 149 | | /* Compiler-dependent macros that rely on FreeBSD-specific extensions. */ |
| 150 | | #define __FreeBSD_cc_version 0 |
| 151 | | #if __FreeBSD_cc_version >= 300001 |
| 152 | | #define __printf0like(fmtarg, firstvararg) \ |
| 153 | | __attribute__((__format__ (__printf0__, fmtarg, firstvararg))) |
| 154 | | #else |
| 155 | | #define __printf0like(fmtarg, firstvararg) |
| 156 | | #endif |
| 157 | | |
| 158 | | #ifdef __GNUC__ |
| 159 | | #define __strong_reference(sym,aliassym) \ |
| 160 | | extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym))); |
| 161 | | #ifdef __ELF__ |
| 162 | | #ifdef __STDC__ |
| 163 | | #define __weak_reference(sym,alias) \ |
| 164 | | __asm__(".weak " #alias); \ |
| 165 | | __asm__(".equ " #alias ", " #sym) |
| 166 | | #define __warn_references(sym,msg) \ |
| 167 | | __asm__(".section .gnu.warning." #sym); \ |
| 168 | | __asm__(".asciz \"" msg "\""); \ |
| 169 | | __asm__(".previous") |
| 170 | | #else |
| 171 | | #define __weak_reference(sym,alias) \ |
| 172 | | __asm__(".weak alias"); \ |
| 173 | | __asm__(".equ alias, sym") |
| 174 | | #define __warn_references(sym,msg) \ |
| 175 | | __asm__(".section .gnu.warning.sym"); \ |
| 176 | | __asm__(".asciz \"msg\""); \ |
| 177 | | __asm__(".previous") |
| 178 | | #endif /* __STDC__ */ |
| 179 | | #else /* !__ELF__ */ |
| 180 | | #ifdef __STDC__ |
| 181 | | #define __weak_reference(sym,alias) \ |
| 182 | | __asm__(".stabs \"_" #alias "\",11,0,0,0"); \ |
| 183 | | __asm__(".stabs \"_" #sym "\",1,0,0,0") |
| 184 | | #define __warn_references(sym,msg) \ |
| 185 | | __asm__(".stabs \"" msg "\",30,0,0,0"); \ |
| 186 | | __asm__(".stabs \"_" #sym "\",1,0,0,0") |
| 187 | | #else |
| 188 | | #define __weak_reference(sym,alias) \ |
| 189 | | __asm__(".stabs \"_/**/alias\",11,0,0,0"); \ |
| 190 | | __asm__(".stabs \"_/**/sym\",1,0,0,0") |
| 191 | | #define __warn_references(sym,msg) \ |
| 192 | | __asm__(".stabs msg,30,0,0,0"); \ |
| 193 | | __asm__(".stabs \"_/**/sym\",1,0,0,0") |
| 194 | | #endif /* __STDC__ */ |
| 195 | | #endif /* __ELF__ */ |
| 196 | | #endif /* __GNUC__ */ |
| 197 | | |
| 198 | | #if defined(__GNUC__) && defined(__ELF__) |
| 199 | | #define __IDSTRING(name,string) __asm__(".ident\t\"" string "\"") |
| 200 | | #else |
| 201 | | #define __IDSTRING(name,string) static const char name[] __unused = string |
| 202 | | #endif |
| 203 | | |
| 204 | | #ifndef __RCSID |
| 205 | | #define __RCSID(s) __IDSTRING(rcsid,s) |
| 206 | | #endif |
| 207 | | |
| 208 | | #ifndef __RCSID_SOURCE |
| 209 | | #define __RCSID_SOURCE(s) __IDSTRING(rcsid_source,s) |
| 210 | | #endif |
| 211 | | |
| 212 | | #ifndef __COPYRIGHT |
| 213 | | #define __COPYRIGHT(s) __IDSTRING(copyright,s) |
| 214 | | #endif |
| 215 | | |
| 216 | | #endif /* !_SYS_CDEFS_H_ */ |
| 217 | | #endif /* ~_SYS_CDEFS_H */ |
| 218 | | |
| 219 | | /* |
| 220 | | * Copyright (c) 1989, 1993 |
| 221 | | * The Regents of the University of California. All rights reserved. |
| 222 | | * |
| 223 | | * This code is derived from software contributed to Berkeley by |
| 224 | | * Guido van Rossum. |
| 225 | | * |
| 226 | | * Redistribution and use in source and binary forms, with or without |
| 227 | | * modification, are permitted provided that the following conditions |
| 228 | | * are met: |
| 229 | | * 1. Redistributions of source code must retain the above copyright |
| 230 | | * notice, this list of conditions and the following disclaimer. |
| 231 | | * 2. Redistributions in binary form must reproduce the above copyright |
| 232 | | * notice, this list of conditions and the following disclaimer in the |
| 233 | | * documentation and/or other materials provided with the distribution. |
| 234 | | * 3. All advertising materials mentioning features or use of this software |
| 235 | | * must display the following acknowledgement: |
| 236 | | * This product includes software developed by the University of |
| 237 | | * California, Berkeley and its contributors. |
| 238 | | * 4. Neither the name of the University nor the names of its contributors |
| 239 | | * may be used to endorse or promote products derived from this software |
| 240 | | * without specific prior written permission. |
| 241 | | * |
| 242 | | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 243 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 244 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 245 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 246 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 247 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 248 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 249 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 250 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 251 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 252 | | * SUCH DAMAGE. |
| 253 | | * |
| 254 | | * @(#)glob.h 8.1 (Berkeley) 6/2/93 |
| 255 | | */ |
| 256 | | |
| 257 | | #ifndef _GLOB_H_ |
| 258 | | #define _GLOB_H_ |
| 259 | | |
| 260 | | //BEOS: sys/cdefs.h included above |
| 261 | | |
| 262 | | struct stat; |
| 263 | | typedef struct { |
| 264 | | int gl_pathc; /* Count of total paths so far. */ |
| 265 | | int gl_matchc; /* Count of paths matching pattern. */ |
| 266 | | int gl_offs; /* Reserved at beginning of gl_pathv. */ |
| 267 | | int gl_flags; /* Copy of flags parameter to glob. */ |
| 268 | | char **gl_pathv; /* List of paths matching pattern. */ |
| 269 | | /* Copy of errfunc parameter to glob. */ |
| 270 | | int (*gl_errfunc) __P((const char *, int)); |
| 271 | | |
| 272 | | /* |
| 273 | | * Alternate filesystem access methods for glob; replacement |
| 274 | | * versions of closedir(3), readdir(3), opendir(3), stat(2) |
| 275 | | * and lstat(2). |
| 276 | | */ |
| 277 | | void (*gl_closedir) __P((void *)); |
| 278 | | struct dirent *(*gl_readdir) __P((void *)); |
| 279 | | void *(*gl_opendir) __P((const char *)); |
| 280 | | int (*gl_lstat) __P((const char *, struct stat *)); |
| 281 | | int (*gl_stat) __P((const char *, struct stat *)); |
| 282 | | } glob_t; |
| 283 | | |
| 284 | | #define GLOB_APPEND 0x0001 /* Append to output from previous call. */ |
| 285 | | #define GLOB_DOOFFS 0x0002 /* Use gl_offs. */ |
| 286 | | #define GLOB_ERR 0x0004 /* Return on error. */ |
| 287 | | #define GLOB_MARK 0x0008 /* Append / to matching directories. */ |
| 288 | | #define GLOB_NOCHECK 0x0010 /* Return pattern itself if nothing matches. */ |
| 289 | | #define GLOB_NOSORT 0x0020 /* Don't sort. */ |
| 290 | | |
| 291 | | #define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */ |
| 292 | | #define GLOB_BRACE 0x0080 /* Expand braces ala csh. */ |
| 293 | | #define GLOB_MAGCHAR 0x0100 /* Pattern had globbing characters. */ |
| 294 | | #define GLOB_NOMAGIC 0x0200 /* GLOB_NOCHECK without magic chars (csh). */ |
| 295 | | #define GLOB_QUOTE 0x0400 /* Quote special chars with \. */ |
| 296 | | #define GLOB_TILDE 0x0800 /* Expand tilde names from the passwd file. */ |
| 297 | | |
| 298 | | #define GLOB_NOSPACE (-1) /* Malloc call failed. */ |
| 299 | | #define GLOB_ABEND (-2) /* Unignored error. */ |
| 300 | | |
| 301 | | __BEGIN_DECLS |
| 302 | | int glob __P((const char *, int, int (*)(const char *, int), glob_t *)); |
| 303 | | void globfree __P((glob_t *)); |
| 304 | | __END_DECLS |
| 305 | | |
| 306 | | #endif /* !_GLOB_H_ */ |
| 307 | | |
src/mess/osd/windows/glob.c
| r7335 | r7336 | |
| 1 | | /* |
| 2 | | * Copyright (c) 1989, 1993 |
| 3 | | * The Regents of the University of California. All rights reserved. |
| 4 | | * |
| 5 | | * This code is derived from software contributed to Berkeley by |
| 6 | | * Guido van Rossum. |
| 7 | | * |
| 8 | | * Redistribution and use in source and binary forms, with or without |
| 9 | | * modification, are permitted provided that the following conditions |
| 10 | | * are met: |
| 11 | | * 1. Redistributions of source code must retain the above copyright |
| 12 | | * notice, this list of conditions and the following disclaimer. |
| 13 | | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | | * notice, this list of conditions and the following disclaimer in the |
| 15 | | * documentation and/or other materials provided with the distribution. |
| 16 | | * 3. All advertising materials mentioning features or use of this software |
| 17 | | * must display the following acknowledgement: |
| 18 | | * This product includes software developed by the University of |
| 19 | | * California, Berkeley and its contributors. |
| 20 | | * 4. Neither the name of the University nor the names of its contributors |
| 21 | | * may be used to endorse or promote products derived from this software |
| 22 | | * without specific prior written permission. |
| 23 | | * |
| 24 | | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | | * SUCH DAMAGE. |
| 35 | | */ |
| 36 | | |
| 37 | | #if defined(LIBC_SCCS) && !defined(lint) |
| 38 | | static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; |
| 39 | | #endif /* LIBC_SCCS and not lint */ |
| 40 | | |
| 41 | | /* |
| 42 | | * glob(3) -- a superset of the one defined in POSIX 1003.2. |
| 43 | | * |
| 44 | | * The [!...] convention to negate a range is supported (SysV, Posix, ksh). |
| 45 | | * |
| 46 | | * Optional extra services, controlled by flags not defined by POSIX: |
| 47 | | * |
| 48 | | * GLOB_QUOTE: |
| 49 | | * Escaping convention: \ inhibits any special meaning the following |
| 50 | | * character might have (except \ at end of string is retained). |
| 51 | | * GLOB_MAGCHAR: |
| 52 | | * Set in gl_flags if pattern contained a globbing character. |
| 53 | | * GLOB_NOMAGIC: |
| 54 | | * Same as GLOB_NOCHECK, but it will only append pattern if it did |
| 55 | | * not contain any magic characters. [Used in csh style globbing] |
| 56 | | * GLOB_ALTDIRFUNC: |
| 57 | | * Use alternately specified directory access functions. |
| 58 | | * GLOB_TILDE: |
| 59 | | * expand ~user/foo to the /home/dir/of/user/foo |
| 60 | | * GLOB_BRACE: |
| 61 | | * expand {1,2}{a,b} to 1a 1b 2a 2b |
| 62 | | * gl_matchc: |
| 63 | | * Number of matches in the current invocation of glob. |
| 64 | | */ |
| 65 | | |
| 66 | | #ifndef WIN32 |
| 67 | | #define WIN32 |
| 68 | | #endif |
| 69 | | |
| 70 | | #ifdef WIN32 |
| 71 | | #ifdef _MSC_VER |
| 72 | | #define __STDC__ 0 |
| 73 | | #endif |
| 74 | | #else |
| 75 | | #include <sys/param.h> |
| 76 | | #endif |
| 77 | | #include <sys/stat.h> |
| 78 | | |
| 79 | | #include <ctype.h> |
| 80 | | |
| 81 | | #include "osdcore.h" |
| 82 | | #include "osdmess.h" |
| 83 | | |
| 84 | | #include <errno.h> |
| 85 | | #include "glob.h" |
| 86 | | #include <stdio.h> |
| 87 | | #include <stdlib.h> |
| 88 | | #include <string.h> |
| 89 | | |
| 90 | | #define DOLLAR '$' |
| 91 | | #define DOT '.' |
| 92 | | #define EOS '\0' |
| 93 | | #define LBRACKET '[' |
| 94 | | #define NOT '!' |
| 95 | | #define QUESTION '?' |
| 96 | | #define QUOTE '\\' |
| 97 | | #define RANGE '-' |
| 98 | | #define RBRACKET ']' |
| 99 | | #define SEP '/' |
| 100 | | #define STAR '*' |
| 101 | | #define TILDE '~' |
| 102 | | #define UNDERSCORE '_' |
| 103 | | #define LBRACE '{' |
| 104 | | #define RBRACE '}' |
| 105 | | #define SLASH '/' |
| 106 | | #define COMMA ',' |
| 107 | | |
| 108 | | #ifdef WIN32 |
| 109 | | #define S_ISLNK(s) (0) |
| 110 | | #undef SEP |
| 111 | | #define SEP '\\' |
| 112 | | struct passwd |
| 113 | | { |
| 114 | | char *pw_dir; |
| 115 | | }; |
| 116 | | #define getlogin() (NULL) |
| 117 | | #define getpwnam(h) (NULL) |
| 118 | | #define getpwuid(h) (NULL) |
| 119 | | #define getuid() (0) |
| 120 | | #define lstat stat |
| 121 | | typedef unsigned int u_int; |
| 122 | | typedef unsigned short u_short; |
| 123 | | typedef unsigned char u_char; |
| 124 | | #else |
| 125 | | #include <unistd.h> |
| 126 | | #include <pwd.h> |
| 127 | | #endif |
| 128 | | |
| 129 | | #ifndef DEBUG |
| 130 | | |
| 131 | | #define M_QUOTE 0x8000 |
| 132 | | #define M_PROTECT 0x4000 |
| 133 | | #define M_MASK 0xffff |
| 134 | | #define M_ASCII 0x00ff |
| 135 | | |
| 136 | | typedef u_short Char; |
| 137 | | |
| 138 | | #else |
| 139 | | |
| 140 | | #define M_QUOTE 0x80 |
| 141 | | #define M_PROTECT 0x40 |
| 142 | | #define M_MASK 0xff |
| 143 | | #define M_ASCII 0x7f |
| 144 | | |
| 145 | | typedef char Char; |
| 146 | | |
| 147 | | #endif |
| 148 | | |
| 149 | | |
| 150 | | #define CHAR(c) ((Char)((c)&M_ASCII)) |
| 151 | | #define META(c) ((Char)((c)|M_QUOTE)) |
| 152 | | #define M_ALL META('*') |
| 153 | | #define M_END META(']') |
| 154 | | #define M_NOT META('!') |
| 155 | | #define M_ONE META('?') |
| 156 | | #define M_RNG META('-') |
| 157 | | #define M_SET META('[') |
| 158 | | #define ismeta(c) (((c)&M_QUOTE) != 0) |
| 159 | | |
| 160 | | #ifndef DECL_SPEC |
| 161 | | #define DECL_SPEC |
| 162 | | #endif |
| 163 | | |
| 164 | | |
| 165 | | static int DECL_SPEC compare __P((const void *, const void *)); |
| 166 | | static void g_Ctoc __P((const Char *, char *)); |
| 167 | | static osd_directory *g_opendir __P((Char *, glob_t *)); |
| 168 | | static Char *g_strchr __P((Char *, int)); |
| 169 | | #ifdef notdef |
| 170 | | static Char *g_strcat __P((Char *, const Char *)); |
| 171 | | #endif |
| 172 | | static int glob0 __P((const Char *, glob_t *)); |
| 173 | | static int glob1 __P((Char *, glob_t *)); |
| 174 | | static int glob2 __P((Char *, Char *, Char *, glob_t *)); |
| 175 | | static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *)); |
| 176 | | static int globextend __P((const Char *, glob_t *)); |
| 177 | | static const Char * globtilde __P((const Char *, Char *, size_t, glob_t *)); |
| 178 | | static int globexp1 __P((const Char *, glob_t *)); |
| 179 | | static int globexp2 __P((const Char *, const Char *, glob_t *, int *)); |
| 180 | | static int match __P((Char *, Char *, Char *)); |
| 181 | | #ifdef DEBUG |
| 182 | | static void qprintf __P((const char *, Char *)); |
| 183 | | #endif |
| 184 | | |
| 185 | | int glob (const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob) |
| 186 | | { |
| 187 | | const u_char *patnext; |
| 188 | | int c; |
| 189 | | Char *bufnext, *bufend, patbuf[_MAX_PATH+1]; |
| 190 | | |
| 191 | | patnext = (u_char *) pattern; |
| 192 | | if (!(flags & GLOB_APPEND)) { |
| 193 | | pglob->gl_pathc = 0; |
| 194 | | pglob->gl_pathv = NULL; |
| 195 | | if (!(flags & GLOB_DOOFFS)) |
| 196 | | pglob->gl_offs = 0; |
| 197 | | } |
| 198 | | pglob->gl_flags = flags & ~GLOB_MAGCHAR; |
| 199 | | pglob->gl_errfunc = errfunc; |
| 200 | | pglob->gl_matchc = 0; |
| 201 | | |
| 202 | | bufnext = patbuf; |
| 203 | | bufend = bufnext + _MAX_PATH; |
| 204 | | if (flags & GLOB_QUOTE) { |
| 205 | | /* Protect the quoted characters. */ |
| 206 | | while (bufnext < bufend && (c = *patnext++) != EOS) |
| 207 | | if (c == QUOTE) { |
| 208 | | if ((c = *patnext++) == EOS) { |
| 209 | | c = QUOTE; |
| 210 | | --patnext; |
| 211 | | } |
| 212 | | *bufnext++ = c | M_PROTECT; |
| 213 | | } |
| 214 | | else |
| 215 | | *bufnext++ = c; |
| 216 | | } |
| 217 | | else |
| 218 | | while (bufnext < bufend && (c = *patnext++) != EOS) |
| 219 | | *bufnext++ = c; |
| 220 | | *bufnext = EOS; |
| 221 | | |
| 222 | | if (flags & GLOB_BRACE) |
| 223 | | return globexp1(patbuf, pglob); |
| 224 | | else |
| 225 | | return glob0(patbuf, pglob); |
| 226 | | } |
| 227 | | |
| 228 | | /* |
| 229 | | * Expand recursively a glob {} pattern. When there is no more expansion |
| 230 | | * invoke the standard globbing routine to glob the rest of the magic |
| 231 | | * characters |
| 232 | | */ |
| 233 | | static int globexp1(const Char *pattern, glob_t *pglob) |
| 234 | | { |
| 235 | | Char* ptr = (Char *) pattern; |
| 236 | | int rv; |
| 237 | | |
| 238 | | /* Protect a single {}, for find(1), like csh */ |
| 239 | | if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) |
| 240 | | return glob0(pattern, pglob); |
| 241 | | |
| 242 | | while ((ptr = (Char *) g_strchr((Char *) ptr, LBRACE)) != NULL) |
| 243 | | if (!globexp2(ptr, pattern, pglob, &rv)) |
| 244 | | return rv; |
| 245 | | |
| 246 | | return glob0(pattern, pglob); |
| 247 | | } |
| 248 | | |
| 249 | | |
| 250 | | /* |
| 251 | | * Recursive brace globbing helper. Tries to expand a single brace. |
| 252 | | * If it succeeds then it invokes globexp1 with the new pattern. |
| 253 | | * If it fails then it tries to glob the rest of the pattern and returns. |
| 254 | | */ |
| 255 | | static int globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv) |
| 256 | | { |
| 257 | | int i; |
| 258 | | Char *lm, *ls; |
| 259 | | const Char *pe, *pm, *pl; |
| 260 | | Char patbuf[_MAX_PATH + 1]; |
| 261 | | |
| 262 | | /* copy part up to the brace */ |
| 263 | | for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) |
| 264 | | continue; |
| 265 | | ls = lm; |
| 266 | | |
| 267 | | /* Find the balanced brace */ |
| 268 | | for (i = 0, pe = ++ptr; *pe; pe++) |
| 269 | | if (*pe == LBRACKET) { |
| 270 | | /* Ignore everything between [] */ |
| 271 | | for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) |
| 272 | | continue; |
| 273 | | if (*pe == EOS) { |
| 274 | | /* |
| 275 | | * We could not find a matching RBRACKET. |
| 276 | | * Ignore and just look for RBRACE |
| 277 | | */ |
| 278 | | pe = pm; |
| 279 | | } |
| 280 | | } |
| 281 | | else if (*pe == LBRACE) |
| 282 | | i++; |
| 283 | | else if (*pe == RBRACE) { |
| 284 | | if (i == 0) |
| 285 | | break; |
| 286 | | i--; |
| 287 | | } |
| 288 | | |
| 289 | | /* Non matching braces; just glob the pattern */ |
| 290 | | if (i != 0 || *pe == EOS) { |
| 291 | | *rv = glob0(patbuf, pglob); |
| 292 | | return 0; |
| 293 | | } |
| 294 | | |
| 295 | | for (i = 0, pl = pm = ptr; pm <= pe; pm++) |
| 296 | | switch (*pm) { |
| 297 | | case LBRACKET: |
| 298 | | /* Ignore everything between [] */ |
| 299 | | for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++) |
| 300 | | continue; |
| 301 | | if (*pm == EOS) { |
| 302 | | /* |
| 303 | | * We could not find a matching RBRACKET. |
| 304 | | * Ignore and just look for RBRACE |
| 305 | | */ |
| 306 | | pm = pl; |
| 307 | | } |
| 308 | | break; |
| 309 | | |
| 310 | | case LBRACE: |
| 311 | | i++; |
| 312 | | break; |
| 313 | | |
| 314 | | case RBRACE: |
| 315 | | if (i) { |
| 316 | | i--; |
| 317 | | break; |
| 318 | | } |
| 319 | | /* FALLTHROUGH */ |
| 320 | | case COMMA: |
| 321 | | if (i && *pm == COMMA) |
| 322 | | break; |
| 323 | | else { |
| 324 | | /* Append the current string */ |
| 325 | | for (lm = ls; (pl < pm); *lm++ = *pl++) |
| 326 | | continue; |
| 327 | | /* |
| 328 | | * Append the rest of the pattern after the |
| 329 | | * closing brace |
| 330 | | */ |
| 331 | | for (pl = pe + 1; (*lm++ = *pl++) != EOS;) |
| 332 | | continue; |
| 333 | | |
| 334 | | /* Expand the current pattern */ |
| 335 | | #ifdef DEBUG |
| 336 | | qprintf("globexp2:", patbuf); |
| 337 | | #endif |
| 338 | | *rv = globexp1(patbuf, pglob); |
| 339 | | |
| 340 | | /* move after the comma, to the next string */ |
| 341 | | pl = pm + 1; |
| 342 | | } |
| 343 | | break; |
| 344 | | |
| 345 | | default: |
| 346 | | break; |
| 347 | | } |
| 348 | | *rv = 0; |
| 349 | | return 0; |
| 350 | | } |
| 351 | | |
| 352 | | |
| 353 | | |
| 354 | | /* |
| 355 | | * expand tilde from the passwd file. |
| 356 | | */ |
| 357 | | static const Char *globtilde(const Char *pattern,Char *patbuf, size_t patbuf_len, glob_t *pglob) |
| 358 | | { |
| 359 | | struct passwd *pwd; |
| 360 | | char *h; |
| 361 | | const Char *p; |
| 362 | | Char *b, *eb; |
| 363 | | |
| 364 | | if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) |
| 365 | | return pattern; |
| 366 | | |
| 367 | | /* |
| 368 | | * Copy up to the end of the string or / |
| 369 | | */ |
| 370 | | eb = &patbuf[patbuf_len - 1]; |
| 371 | | for (p = pattern + 1, h = (char *) patbuf; |
| 372 | | h < (char *)eb && *p && *p != SLASH; *h++ = *p++) |
| 373 | | continue; |
| 374 | | |
| 375 | | *h = EOS; |
| 376 | | |
| 377 | | if (((char *) patbuf)[0] == EOS) { |
| 378 | | /* |
| 379 | | * handle a plain ~ or ~/ by expanding $HOME first (iff |
| 380 | | * we're not running setuid or setgid) and then trying |
| 381 | | * the password file |
| 382 | | */ |
| 383 | | if ( |
| 384 | | (h = getenv("HOME")) == NULL) { |
| 385 | | if (((h = getlogin()) != NULL && |
| 386 | | (pwd = getpwnam(h)) != NULL) || |
| 387 | | (pwd = getpwuid(getuid())) != NULL) |
| 388 | | h = pwd->pw_dir; |
| 389 | | else |
| 390 | | return pattern; |
| 391 | | } |
| 392 | | } |
| 393 | | else { |
| 394 | | /* |
| 395 | | * Expand a ~user |
| 396 | | */ |
| 397 | | if ((pwd = getpwnam((char*) patbuf)) == NULL) |
| 398 | | return pattern; |
| 399 | | else |
| 400 | | h = pwd->pw_dir; |
| 401 | | } |
| 402 | | |
| 403 | | /* Copy the home directory */ |
| 404 | | for (b = patbuf; b < eb && *h; *b++ = *h++) |
| 405 | | continue; |
| 406 | | |
| 407 | | /* Append the rest of the pattern */ |
| 408 | | while (b < eb && (*b++ = *p++) != EOS) |
| 409 | | continue; |
| 410 | | *b = EOS; |
| 411 | | |
| 412 | | return patbuf; |
| 413 | | } |
| 414 | | |
| 415 | | |
| 416 | | /* |
| 417 | | * The main glob() routine: compiles the pattern (optionally processing |
| 418 | | * quotes), calls glob1() to do the real pattern matching, and finally |
| 419 | | * sorts the list (unless unsorted operation is requested). Returns 0 |
| 420 | | * if things went well, nonzero if errors occurred. It is not an error |
| 421 | | * to find no matches. |
| 422 | | */ |
| 423 | | static int glob0(const Char *pattern,glob_t *pglob) |
| 424 | | { |
| 425 | | const Char *qpatnext; |
| 426 | | int c, err, oldpathc; |
| 427 | | Char *bufnext, patbuf[_MAX_PATH+1]; |
| 428 | | |
| 429 | | qpatnext = globtilde(pattern, patbuf, ARRAY_LENGTH(patbuf), pglob); |
| 430 | | oldpathc = pglob->gl_pathc; |
| 431 | | bufnext = patbuf; |
| 432 | | |
| 433 | | /* We don't need to check for buffer overflow any more. */ |
| 434 | | while ((c = *qpatnext++) != EOS) { |
| 435 | | switch (c) { |
| 436 | | case LBRACKET: |
| 437 | | c = *qpatnext; |
| 438 | | if (c == NOT) |
| 439 | | ++qpatnext; |
| 440 | | if (*qpatnext == EOS || |
| 441 | | g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) { |
| 442 | | *bufnext++ = LBRACKET; |
| 443 | | if (c == NOT) |
| 444 | | --qpatnext; |
| 445 | | break; |
| 446 | | } |
| 447 | | *bufnext++ = M_SET; |
| 448 | | if (c == NOT) |
| 449 | | *bufnext++ = M_NOT; |
| 450 | | c = *qpatnext++; |
| 451 | | do { |
| 452 | | *bufnext++ = CHAR(c); |
| 453 | | if (*qpatnext == RANGE && |
| 454 | | (c = qpatnext[1]) != RBRACKET) { |
| 455 | | *bufnext++ = M_RNG; |
| 456 | | *bufnext++ = CHAR(c); |
| 457 | | qpatnext += 2; |
| 458 | | } |
| 459 | | } while ((c = *qpatnext++) != RBRACKET); |
| 460 | | pglob->gl_flags |= GLOB_MAGCHAR; |
| 461 | | *bufnext++ = M_END; |
| 462 | | break; |
| 463 | | case QUESTION: |
| 464 | | pglob->gl_flags |= GLOB_MAGCHAR; |
| 465 | | *bufnext++ = M_ONE; |
| 466 | | break; |
| 467 | | case STAR: |
| 468 | | pglob->gl_flags |= GLOB_MAGCHAR; |
| 469 | | /* collapse adjacent stars to one, |
| 470 | | * to avoid exponential behavior |
| 471 | | */ |
| 472 | | if (bufnext == patbuf || bufnext[-1] != M_ALL) |
| 473 | | *bufnext++ = M_ALL; |
| 474 | | break; |
| 475 | | default: |
| 476 | | *bufnext++ = CHAR(c); |
| 477 | | break; |
| 478 | | } |
| 479 | | } |
| 480 | | *bufnext = EOS; |
| 481 | | #ifdef DEBUG |
| 482 | | qprintf("glob0:", patbuf); |
| 483 | | #endif |
| 484 | | |
| 485 | | if ((err = glob1(patbuf, pglob)) != 0) |
| 486 | | return(err); |
| 487 | | |
| 488 | | /* |
| 489 | | * If there was no match we are going to append the pattern |
| 490 | | * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified |
| 491 | | * and the pattern did not contain any magic characters |
| 492 | | * GLOB_NOMAGIC is there just for compatibility with csh. |
| 493 | | */ |
| 494 | | if (pglob->gl_pathc == oldpathc && |
| 495 | | ((pglob->gl_flags & GLOB_NOCHECK) || |
| 496 | | ((pglob->gl_flags & GLOB_NOMAGIC) && |
| 497 | | !(pglob->gl_flags & GLOB_MAGCHAR)))) |
| 498 | | return(globextend(pattern, pglob)); |
| 499 | | else if (!(pglob->gl_flags & GLOB_NOSORT)) |
| 500 | | qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, |
| 501 | | pglob->gl_pathc - oldpathc, sizeof(char *), compare); |
| 502 | | return(0); |
| 503 | | } |
| 504 | | |
| 505 | | static int compare(const void *p, const void *q) |
| 506 | | { |
| 507 | | return(strcmp(*(char **)p, *(char **)q)); |
| 508 | | } |
| 509 | | |
| 510 | | static int glob1(Char *pattern,glob_t *pglob) |
| 511 | | { |
| 512 | | Char pathbuf[_MAX_PATH+1]; |
| 513 | | |
| 514 | | /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ |
| 515 | | if (*pattern == EOS) |
| 516 | | return(0); |
| 517 | | return(glob2(pathbuf, pathbuf, pattern, pglob)); |
| 518 | | } |
| 519 | | |
| 520 | | /* |
| 521 | | * The functions glob2 and glob3 are mutually recursive; there is one level |
| 522 | | * of recursion for each segment in the pattern that contains one or more |
| 523 | | * meta characters. |
| 524 | | */ |
| 525 | | static int glob2(Char *pathbuf, Char *pathend, Char *pattern,glob_t *pglob) |
| 526 | | { |
| 527 | | Char *p, *q; |
| 528 | | int anymeta; |
| 529 | | osd_directory_entry *entry = NULL; |
| 530 | | osd_directory_entry *entry2 = NULL; |
| 531 | | char buf[_MAX_PATH]; |
| 532 | | |
| 533 | | /* |
| 534 | | * Loop over pattern segments until end of pattern or until |
| 535 | | * segment with meta character found. |
| 536 | | */ |
| 537 | | for (anymeta = 0;;) { |
| 538 | | if (*pattern == EOS) { /* End of pattern? */ |
| 539 | | *pathend = EOS; |
| 540 | | g_Ctoc(pathbuf, buf); |
| 541 | | entry = osd_stat(buf); |
| 542 | | if (!entry) |
| 543 | | return(0); |
| 544 | | |
| 545 | | if ((((pglob->gl_flags & GLOB_MARK) && |
| 546 | | pathend[-1] != SEP) && (entry->type == ENTTYPE_DIR)) |
| 547 | | || ((S_ISLNK(sb.st_mode) && |
| 548 | | ((entry2 = osd_stat(buf)) != NULL) && |
| 549 | | (entry2->type == ENTTYPE_DIR)))) { |
| 550 | | *pathend++ = SEP; |
| 551 | | *pathend = EOS; |
| 552 | | } |
| 553 | | free(entry); |
| 554 | | if (entry2) |
| 555 | | free(entry2); |
| 556 | | ++pglob->gl_matchc; |
| 557 | | return(globextend(pathbuf, pglob)); |
| 558 | | } |
| 559 | | |
| 560 | | /* Find end of next segment, copy tentatively to pathend. */ |
| 561 | | q = pathend; |
| 562 | | p = pattern; |
| 563 | | while (*p != EOS && *p != SEP) { |
| 564 | | if (ismeta(*p)) |
| 565 | | anymeta = 1; |
| 566 | | *q++ = *p++; |
| 567 | | } |
| 568 | | |
| 569 | | if (!anymeta) { /* No expansion, do next segment. */ |
| 570 | | pathend = q; |
| 571 | | pattern = p; |
| 572 | | while (*pattern == SEP) |
| 573 | | *pathend++ = *pattern++; |
| 574 | | } else /* Need expansion, recurse. */ |
| 575 | | return(glob3(pathbuf, pathend, pattern, p, pglob)); |
| 576 | | } |
| 577 | | /* NOTREACHED */ |
| 578 | | } |
| 579 | | |
| 580 | | static int glob3(Char *pathbuf,Char *pathend,Char *pattern,Char *restpattern,glob_t *pglob) |
| 581 | | { |
| 582 | | register const osd_directory_entry *dp; |
| 583 | | osd_directory *dirp; |
| 584 | | int err; |
| 585 | | char buf[_MAX_PATH]; |
| 586 | | |
| 587 | | /* |
| 588 | | * The readdirfunc declaration can't be prototyped, because it is |
| 589 | | * assigned, below, to two functions which are prototyped in glob.h |
| 590 | | * and dirent.h as taking pointers to differently typed opaque |
| 591 | | * structures. |
| 592 | | */ |
| 593 | | typedef const osd_directory_entry *(*readdirfunc_t)(osd_directory *); |
| 594 | | readdirfunc_t readdirfunc; |
| 595 | | |
| 596 | | *pathend = EOS; |
| 597 | | errno = 0; |
| 598 | | |
| 599 | | if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { |
| 600 | | /* TODO: don't call for ENOENT or ENOTDIR? */ |
| 601 | | if (pglob->gl_errfunc) { |
| 602 | | g_Ctoc(pathbuf, buf); |
| 603 | | if (pglob->gl_errfunc(buf, errno) || |
| 604 | | pglob->gl_flags & GLOB_ERR) |
| 605 | | return (GLOB_ABEND); |
| 606 | | } |
| 607 | | return(0); |
| 608 | | } |
| 609 | | |
| 610 | | err = 0; |
| 611 | | |
| 612 | | /* Search directory for matching names. */ |
| 613 | | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| 614 | | readdirfunc = (readdirfunc_t) pglob->gl_readdir; |
| 615 | | else |
| 616 | | readdirfunc = osd_readdir; |
| 617 | | while ((dp = (*readdirfunc)(dirp))) { |
| 618 | | register u_char *sc; |
| 619 | | register Char *dc; |
| 620 | | |
| 621 | | /* Initial DOT must be matched literally. */ |
| 622 | | if (dp->name[0] == DOT && *pattern != DOT) |
| 623 | | continue; |
| 624 | | for (sc = (u_char *) dp->name, dc = pathend; |
| 625 | | (*dc++ = *sc++) != EOS;) |
| 626 | | continue; |
| 627 | | if (!match(pathend, pattern, restpattern)) { |
| 628 | | *pathend = EOS; |
| 629 | | continue; |
| 630 | | } |
| 631 | | err = glob2(pathbuf, --dc, restpattern, pglob); |
| 632 | | if (err) |
| 633 | | break; |
| 634 | | } |
| 635 | | |
| 636 | | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| 637 | | (*pglob->gl_closedir)(dirp); |
| 638 | | else |
| 639 | | osd_closedir(dirp); |
| 640 | | return(err); |
| 641 | | } |
| 642 | | |
| 643 | | |
| 644 | | /* |
| 645 | | * Extend the gl_pathv member of a glob_t structure to accomodate a new item, |
| 646 | | * add the new item, and update gl_pathc. |
| 647 | | * |
| 648 | | * This assumes the BSD realloc, which only copies the block when its size |
| 649 | | * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic |
| 650 | | * behavior. |
| 651 | | * |
| 652 | | * Return 0 if new item added, error code if memory couldn't be allocated. |
| 653 | | * |
| 654 | | * Invariant of the glob_t structure: |
| 655 | | * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and |
| 656 | | * gl_pathv points to (gl_offs + gl_pathc + 1) items. |
| 657 | | */ |
| 658 | | static int globextend(const Char *path, glob_t *pglob) |
| 659 | | { |
| 660 | | register char **pathv; |
| 661 | | register int i; |
| 662 | | u_int newsize; |
| 663 | | char *copy; |
| 664 | | const Char *p; |
| 665 | | |
| 666 | | newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); |
| 667 | | pathv = pglob->gl_pathv ? |
| 668 | | (char **)realloc(pglob->gl_pathv, newsize) : |
| 669 | | (char **)malloc(newsize); |
| 670 | | if (pathv == NULL) |
| 671 | | return(GLOB_NOSPACE); |
| 672 | | |
| 673 | | if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { |
| 674 | | /* first time around -- clear initial gl_offs items */ |
| 675 | | pathv += pglob->gl_offs; |
| 676 | | for (i = pglob->gl_offs; --i >= 0; ) |
| 677 | | *--pathv = NULL; |
| 678 | | } |
| 679 | | pglob->gl_pathv = pathv; |
| 680 | | |
| 681 | | for (p = path; *p++;) |
| 682 | | continue; |
| 683 | | if ((copy = (char *)malloc(p - path)) != NULL) { |
| 684 | | g_Ctoc(path, copy); |
| 685 | | pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; |
| 686 | | } |
| 687 | | pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; |
| 688 | | return(copy == NULL ? GLOB_NOSPACE : 0); |
| 689 | | } |
| 690 | | |
| 691 | | /* |
| 692 | | * pattern matching function for filenames. Each occurrence of the * |
| 693 | | * pattern causes a recursion level. |
| 694 | | */ |
| 695 | | static int match(register Char *name, register Char *pat, register Char *patend) |
| 696 | | { |
| 697 | | int ok, negate_range; |
| 698 | | Char c, k; |
| 699 | | |
| 700 | | while (pat < patend) { |
| 701 | | c = *pat++; |
| 702 | | switch (c & M_MASK) { |
| 703 | | case M_ALL: |
| 704 | | if (pat == patend) |
| 705 | | return(1); |
| 706 | | do |
| 707 | | if (match(name, pat, patend)) |
| 708 | | return(1); |
| 709 | | while (*name++ != EOS); |
| 710 | | return(0); |
| 711 | | case M_ONE: |
| 712 | | if (*name++ == EOS) |
| 713 | | return(0); |
| 714 | | break; |
| 715 | | case M_SET: |
| 716 | | ok = 0; |
| 717 | | if ((k = *name++) == EOS) |
| 718 | | return(0); |
| 719 | | if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) |
| 720 | | ++pat; |
| 721 | | while (((c = *pat++) & M_MASK) != M_END) |
| 722 | | if ((*pat & M_MASK) == M_RNG) { |
| 723 | | if (CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1])) |
| 724 | | ok = 1; |
| 725 | | pat += 2; |
| 726 | | } else if (c == k) |
| 727 | | ok = 1; |
| 728 | | if (ok == negate_range) |
| 729 | | return(0); |
| 730 | | break; |
| 731 | | default: |
| 732 | | if (*name++ != c) |
| 733 | | return(0); |
| 734 | | break; |
| 735 | | } |
| 736 | | } |
| 737 | | return(*name == EOS); |
| 738 | | } |
| 739 | | |
| 740 | | /* Free allocated data belonging to a glob_t structure. */ |
| 741 | | void globfree(glob_t *pglob) |
| 742 | | { |
| 743 | | register int i; |
| 744 | | register char **pp; |
| 745 | | |
| 746 | | if (pglob->gl_pathv != NULL) { |
| 747 | | pp = pglob->gl_pathv + pglob->gl_offs; |
| 748 | | for (i = pglob->gl_pathc; i--; ++pp) |
| 749 | | if (*pp) |
| 750 | | free(*pp); |
| 751 | | free(pglob->gl_pathv); |
| 752 | | } |
| 753 | | } |
| 754 | | |
| 755 | | static osd_directory *g_opendir(register Char *str,glob_t *pglob) |
| 756 | | { |
| 757 | | char buf[_MAX_PATH]; |
| 758 | | |
| 759 | | if (!*str) |
| 760 | | strcpy(buf, "."); |
| 761 | | else |
| 762 | | g_Ctoc(str, buf); |
| 763 | | |
| 764 | | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| 765 | | return((osd_directory *)(*pglob->gl_opendir)(buf)); |
| 766 | | |
| 767 | | return osd_opendir(buf); |
| 768 | | } |
| 769 | | |
| 770 | | static Char *g_strchr(Char *str,int ch) |
| 771 | | { |
| 772 | | do { |
| 773 | | if (*str == ch) |
| 774 | | return (str); |
| 775 | | } while (*str++); |
| 776 | | return (NULL); |
| 777 | | } |
| 778 | | |
| 779 | | #ifdef notdef |
| 780 | | static Char * |
| 781 | | g_strcat(dst, src) |
| 782 | | Char *dst; |
| 783 | | const Char* src; |
| 784 | | { |
| 785 | | Char *sdst = dst; |
| 786 | | |
| 787 | | while (*dst++) |
| 788 | | continue; |
| 789 | | --dst; |
| 790 | | while((*dst++ = *src++) != EOS) |
| 791 | | continue; |
| 792 | | |
| 793 | | return (sdst); |
| 794 | | } |
| 795 | | #endif |
| 796 | | |
| 797 | | static void g_Ctoc(register const Char *str, char *buf) |
| 798 | | { |
| 799 | | register char *dc; |
| 800 | | |
| 801 | | for (dc = buf; (*dc++ = *str++) != EOS;) |
| 802 | | continue; |
| 803 | | } |
| 804 | | |
| 805 | | #ifdef DEBUG |
| 806 | | static void |
| 807 | | qprintf(str, s) |
| 808 | | const char *str; |
| 809 | | register Char *s; |
| 810 | | { |
| 811 | | register Char *p; |
| 812 | | |
| 813 | | (void)printf("%s:\n", str); |
| 814 | | for (p = s; *p; p++) |
| 815 | | (void)printf("%c", CHAR(*p)); |
| 816 | | (void)printf("\n"); |
| 817 | | for (p = s; *p; p++) |
| 818 | | (void)printf("%c", *p & M_PROTECT ? '"' : ' '); |
| 819 | | (void)printf("\n"); |
| 820 | | for (p = s; *p; p++) |
| 821 | | (void)printf("%c", ismeta(*p) ? '_' : ' '); |
| 822 | | (void)printf("\n"); |
| 823 | | } |
| 824 | | #endif |