1 // hey 2 // a small scripting utility 3 // written by Attila Mezei (attila.mezei@mail.datanet.hu) 4 // contributions by Sander Stoks, Peter Folk, Chris Herborth, Marco Nelissen, Scott Lindsey and others 5 // 6 // public domain, use it at your own risk 7 // 8 // 1.2.8: (Sander Stoks): Added command-line option -o which will output the "result" value 9 // in the reply message to stdout, so you can use it in shell scripting more easily: 10 // "hey Becasso get AspectRatio of Canvas 0" 11 // outputs 12 // Reply BMessage(B_REPLY): 13 // "result" (B_DOUBLE_TYPE) : 0.600 14 // but "hey -o Becasso get AspectRatio of Canvas 0" 15 // outputs 0.600000 directly. 16 // 17 // 1.2.7: by Sander Stoks: Made a fork since I don't think Attila still supports "hey", and 18 // because the latest version on BeBits seems to be 1.2.4. 19 // Changes w.r.t. 1.2.6: When an application returns an error on a message, hey now 20 // keeps iterating over applications with the same signature. This is useful because, 21 // for instance, Terminal starts as a new process for each instance, so it previously 22 // wouldn't work to move a specific Terminal window using hey. You can now say 23 // "hey Terminal set Frame of Window foo to BRect[...]". 24 // 25 // 1.2.6: syntax extended by Sander Stoks <sander@stoks.nl to allow: 26 // "hey Application let Specifier do ..." 27 // allowing you to send messages directly to other handlers than the app itself. 28 // In cooperation with the new Application defined commands (note that some 29 // Be classes, e.g. BWindow, publish commands as well) this allows, for example: 30 // "hey NetPositive let Window 0 do MoveBy BPoint[10,10]" 31 // Note that this is partly redundant, since 32 // "hey NetPositive let Window 0 do get Title" 33 // duplicates "hey NetPositive get Title of Window 0" 34 // But with the old system, 35 // "hey NetPositive MoveBy of Window 0 with data=BPoint[10,10]" 36 // couldn't work ("MoveBy" is not defined by the Application itself). 37 // 38 // 1.2.5: value_info is supported in BPropertyInfo. This means when you send GETSUITES (B_GET_SUPPORTED_SUITES) 39 // the value info is printed after the property infos, like this: 40 // "messages" (B_PROPERTY_INFO_TYPE) : 41 // property commands specifiers 42 // -------------------------------------------------------------------------------- 43 // Suites B_GET_PROPERTY DIRECT 44 // Messenger B_GET_PROPERTY DIRECT 45 // InternalName B_GET_PROPERTY DIRECT 46 // 47 // name value kind 48 // -------------------------------------------------------------------------------- 49 // Backup 0x6261636B ('back') COMMAND 50 // Usage: This command backs up your hard drive. 51 // Abort 0x61626F72 ('abor') COMMAND 52 // Usage: Stops the current operation... 53 // Type Code 0x74797065 ('type') TYPE CODE 54 // Usage: Type code info... 55 // 56 // You can also use the application defined commands (as the verb) with hey: 57 // hey MyBackupApp Backup "Maui" 58 // 59 // 1.2.4: the syntax is extended by Peter Folk <pfolk@uni.uiuc.edu> to contain: 60 // do the x of y -3 of z '"1"' 61 // I.e. "do" => B_EXECUTE_PROPERTY, optional "the" makes direct specifiers 62 // more like english, bare reverse-index-specifiers are now handled, and 63 // named specifiers can contain only digits by quoting it (but make sure the 64 // shell passed the quotes through). 65 // 66 // Hey(target,const char*,reply) was previously limited to 100 tokens. It 67 // now uses a vector<> so it's only limited by available memory. 68 // 69 // Also, the archive name is now Y2K compliant =) 70 // 71 // 1.2.3: new option: -s for silent processing (no reply or errors printed) AM 72 // 73 // 1.2.2: Fixes by Marco Nelissen (marcone@xs4all.nl) 74 // - fixed parsing of negative numbers 75 // - fixed "with" syntax, which was broken (after a create, "with" would be taken as a specifier) 76 // 77 // 1.2.1: compiled for x86, R4 with minor modifications at BPropertyInfo 78 // 79 // 1.2.0: the syntax is extended by Sander Stoks (sander@adamation.com) to contain 80 // with name=<value> [and name=<value> [...]] 81 // at the end of the command which will add additional data to the scripting message. E.g: 82 // hey Becasso create Canvas with name=MyCanvas and size=BRect(100,100,300,300) 83 // Also a small interpreter is included. 84 // 85 // Detailed printout of B_PROPERTY_INFO in BMessages. Better than BPropertyInfo::PrintToStream(). 86 // Also prints usage info for a property if defined. 87 // 88 // 1.1.1: minor change from chrish@qnx.com to return -1 if an error is 89 // sent back in the reply message; also added B_COUNT_PROPERTIES support 90 // 91 // The range specifier sent to the target was 1 greater than it should've been. Fixed. 92 // 93 // 'hey' made the assumption that the first thread in a team will be the 94 // application thread (and therefore have the application's name). 95 // This was not always the case. Fix from Scott Lindsey <wombat@gobe.com>. 96 // 97 //v1.1.0: Flattened BPropertyInfo is printed if found in the reply of B_GET_SUPPORTED_SUITES 98 // 1,2,3 and 4 character message constant is supported (e.g. '1', '12', '123', '1234') 99 // Alpha is sent with rgb_color 100 // 101 //v1.0.0 First public release 102 103 104 #include <stdio.h> 105 #include <stdlib.h> 106 #include <string.h> 107 #include <AppKit.h> 108 #include <Path.h> 109 #include <SupportDefs.h> 110 111 int32 HeyInterpreterThreadHook(void* arg); 112 113 status_t Hey(BMessenger* target, const char* arg, BMessage* reply); 114 bool isSpace(char c); 115 status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage* reply); 116 status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 argc); 117 status_t add_data(BMessage *to_message, char *argv[], int32 *argx); 118 status_t add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc); 119 void add_message_contents(BList *textlist, BMessage *msg, int32 level); 120 char *get_datatype_string(int32 type); 121 char *format_data(int32 type, char *ptr, long size); 122 void print_message(BMessage *message); 123 char *id_to_string(long ID, char *here); 124 bool is_valid_char(uint8 c); 125 126 const char VERSION[] = "v1.2.8"; 127 128 #define DEBUG_HEY 0 // 1: prints the script message to be sent to the target application, 0: prints only the reply 129 130 131 // test, these should be zero for normal operation 132 #define TEST_VALUEINFO 0 133 134 135 // flag for silent mode 136 bool silent; 137 // flag for stdout mode 138 bool output; 139 140 status_t 141 parse(BMessenger& the_application, int argc, char *argv[], int32 argapp) 142 { 143 if (!the_application.IsValid()) { 144 if (!silent) 145 fprintf(stderr, "Cannot find the application (%s)\n", argv[argapp]); 146 return B_ERROR; 147 } 148 149 if (argc < 3) { 150 if (!silent) 151 fprintf(stderr, "Cannot find the verb!\n"); 152 return B_ERROR; 153 } 154 155 156 BMessage the_reply; 157 int32 argx = argapp+1; 158 status_t err = Hey(&the_application, argv, &argx, argc, &the_reply); 159 160 if (err != B_OK) { 161 if (!silent) 162 fprintf(stderr, "Error when sending message to %s!\n", argv[argapp]); 163 return B_ERROR; 164 } else { 165 if (the_reply.what == (uint32)B_MESSAGE_NOT_UNDERSTOOD || the_reply.what==(uint32)B_ERROR){ // I do it myself 166 if (the_reply.HasString("message")){ 167 if (!silent) 168 printf("%s (error 0x%8lX)\n", the_reply.FindString("message"), the_reply.FindInt32("error")); 169 } else { 170 if (!silent) 171 printf("error 0x%8lX\n", the_reply.FindInt32("error")); 172 } 173 return 1; 174 } else { 175 if (!silent){ 176 if (output){ 177 type_code tc; 178 if (the_reply.GetInfo("result", &tc) == B_OK){ 179 if(tc==B_INT8_TYPE){ 180 int8 v; 181 the_reply.FindInt8("result", &v); 182 printf("%d\n", v); 183 } else if(tc==B_INT16_TYPE){ 184 int16 v; 185 the_reply.FindInt16("result", &v); 186 printf("%d\n", v); 187 } else if(tc==B_INT32_TYPE){ 188 int32 v; 189 the_reply.FindInt32("result", &v); 190 printf("%ld\n", v); 191 } else if(tc==B_UINT8_TYPE){ 192 uint8 v; 193 the_reply.FindInt8("result", (int8*)&v); 194 printf("%u\n", v); 195 } else if(tc==B_UINT16_TYPE){ 196 uint16 v; 197 the_reply.FindInt16("result", (int16*)&v); 198 printf("%u\n", v); 199 } else if(tc==B_UINT32_TYPE){ 200 uint32 v; 201 the_reply.FindInt32("result", (int32*)&v); 202 printf("%lu\n", v); 203 } else if(tc==B_STRING_TYPE){ 204 const char* v; 205 the_reply.FindString("result", &v); 206 printf("%s\n", v); 207 } else if(tc==B_FLOAT_TYPE){ 208 float v; 209 the_reply.FindFloat("result", &v); 210 printf("%f\n", v); 211 } else if(tc==B_DOUBLE_TYPE){ 212 double v; 213 the_reply.FindDouble("result", &v); 214 printf("%f\n", v); 215 } else if(tc==B_BOOL_TYPE){ 216 bool v; 217 the_reply.FindBool("result", &v); 218 printf("%s\n", v?"true":"false"); 219 } else { 220 printf("Unsupported type\n"); 221 } 222 } 223 } else { 224 printf("Reply "); 225 print_message(&the_reply); 226 printf("\n"); 227 } 228 } 229 } 230 } 231 return B_OK; 232 } 233 234 int 235 main(int argc, char *argv[]) 236 { 237 BApplication app("application/x-amezei-hey"); 238 239 if (argc < 2) { 240 fprintf(stderr, "hey %s, written by Attila Mezei (attila.mezei@mail.datanet.hu)\n" \ 241 "usage: hey [-s][-o] <app|signature|teamid> [let <specifier> do] <verb> <specifier_1> <of\n" \ 242 " <specifier_n>>* [to <value>] [with name=<value> [and name=<value>]*]\n" \ 243 "where <verb> : DO|GET|SET|COUNT|CREATE|DELETE|GETSUITES|QUIT|SAVE|LOAD|'what'\n" \ 244 " <specifier> : [the] <property_name> [ <index> | name | \"name\" | '\"name\"' ]\n" \ 245 " <index> : int | -int | '['int']' | '['-int']' | '['startint to end']'\n" \ 246 " <value> : \"string\" | <integer> | <float> | bool(value) | int8(value)\n" \ 247 " | int16(value) | int32(value) | float(value) | double(value)\n" \ 248 " | BPoint(x,y) | BRect(l,t,r,b) | rgb_color(r,g,b,a) | file(path)\n" \ 249 "options: -s: silent\n" \ 250 " -o: output result to stdout for easy parsing\n\n", VERSION); 251 // Updated Usage string to reflect "do", "the", bare -index, and '"name"' changes below 252 // -- pfolk@uni.uiuc.edu 1999-11-03 253 254 return 1; 255 } 256 257 int32 argapp = 1; 258 silent = false; 259 output = false; 260 261 // Updated option mechanism --SS 262 for (int i = 0; i < argc; i++) { 263 if (strcmp(argv[i], "-s")==0 || strcmp(argv[i], "-S")==0){ 264 silent = true; 265 argapp++; 266 } 267 if (strcmp(argv[i], "-o")==0 || strcmp(argv[i], "-O")==0){ 268 output=true; 269 argapp++; 270 } 271 } 272 273 // find the application 274 BMessenger the_application; 275 BList team_list; 276 team_id teamid; 277 app_info appinfo; 278 279 teamid = atoi(argv[argapp]); 280 if (teamid > 0) { 281 if (be_roster->GetRunningAppInfo(teamid, &appinfo)!=B_OK) 282 return 1; 283 the_application=BMessenger(NULL, teamid); 284 if (!parse(the_application, argc, argv, argapp)) 285 return 0; 286 return 1; 287 } 288 289 be_roster->GetAppList(&team_list); 290 291 for (int32 i=0; i<team_list.CountItems(); i++){ 292 teamid = (team_id)team_list.ItemAt(i); 293 be_roster->GetRunningAppInfo(teamid, &appinfo); 294 if (strcmp(appinfo.signature, argv[argapp])==0){ 295 the_application=BMessenger(appinfo.signature); 296 if (!parse(the_application, argc, argv, argapp)) 297 return 0; 298 } else { 299 if (strcmp(appinfo.ref.name, argv[argapp])==0){ 300 the_application=BMessenger(0, teamid); 301 if (!parse(the_application, argc, argv, argapp)) 302 return 0; 303 } 304 } 305 } 306 307 return 1; 308 } 309 310 311 int32 312 HeyInterpreterThreadHook(void* arg) 313 { 314 if (!arg) 315 return 1; 316 317 BMessage environment(*(BMessage*) arg); 318 char* prompt = "Hey"; 319 if (environment.HasString("prompt")) 320 environment.FindString("prompt", (const char **)&prompt); 321 printf("%s> ", prompt); 322 323 BMessenger target; 324 if (environment.HasMessenger("Target")) 325 environment.FindMessenger("Target", &target); 326 327 char command[1024]; 328 status_t err; 329 BMessage reply; 330 while (gets(command)) { 331 reply.MakeEmpty(); 332 err = Hey(&target, command, &reply); 333 if (!err) { 334 print_message(&reply); 335 } else { 336 printf("Error!\n"); 337 } 338 printf("%s> ", prompt); 339 } 340 341 return 0; 342 } 343 344 status_t 345 Hey(BMessenger* target, const char* arg, BMessage* reply) 346 { 347 BList argv; // number of tokens is now limited only by memory -- pfolk@uni.uiuc.edu 1999-11-03 348 char* tokens = new char[strlen(arg)*2]; 349 char* currentToken = tokens; 350 int32 tokenNdex = 0; 351 int32 argNdex = 0; 352 bool inquotes = false; 353 354 while (arg[argNdex] != 0) { // for each character in arg 355 if (arg[argNdex] == '\"') 356 inquotes = !inquotes; 357 if (!inquotes && isSpace(arg[argNdex])) { // if the character is white space 358 if (tokenNdex!=0) { // close off currentToken token 359 currentToken[tokenNdex] = 0; 360 argv.AddItem(currentToken); 361 currentToken += tokenNdex+1; 362 tokenNdex=0; 363 argNdex++; 364 } else { // just skip the whitespace 365 argNdex++; 366 } 367 } else { // copy char into current token 368 currentToken[tokenNdex] = arg[argNdex]; 369 tokenNdex++; 370 argNdex++; 371 } 372 } 373 374 if (tokenNdex!=0) { // close off currentToken token 375 currentToken[tokenNdex] = 0; 376 argv.AddItem(currentToken); 377 } 378 argv.AddItem(NULL); 379 380 int32 argx = 0; 381 status_t ret = Hey(target, (char **)argv.Items(), &argx, argv.CountItems()-1, reply); 382 // This used to be "return Hey(...);"---so tokens wasn't delete'd. -- pfolk@uni.uiuc.edu 1999-11-03 383 delete tokens; 384 return ret; 385 } 386 387 388 bool 389 isSpace(char c) 390 { 391 switch (c) { 392 case ' ': 393 case '\t': 394 return true; 395 396 default: 397 return false; 398 } 399 } 400 401 402 status_t 403 Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage* reply) 404 { 405 bool direct_what = false; 406 BMessage the_message; 407 if (strcasecmp(argv[*argx], "let")==0) { // added "let" -- sander@adamation.com 31may2000 408 BMessage get_target (B_GET_PROPERTY); 409 get_target.AddSpecifier ("Messenger"); 410 // parse the specifiers 411 (*argx)++; 412 status_t result=B_OK; 413 while ((result = add_specifier(&get_target, argv, argx, argc))==B_OK) 414 ; 415 416 if (result!=B_ERROR){ // bad syntax 417 if (!silent) 418 fprintf(stderr, "Bad specifier syntax!\n"); 419 return result; 420 } 421 BMessage msgr; 422 if (target && target->IsValid()) { 423 result = target->SendMessage(&get_target, &msgr); 424 if (result!=B_OK) 425 return result; 426 result = msgr.FindMessenger ("result", target); 427 if (result!=B_OK) { 428 if (!silent) 429 fprintf(stderr, "Couldn't retrieve the BMessenger!\n"); 430 return result; 431 } 432 } 433 if (!argv[*argx]) { 434 if (!silent) 435 fprintf(stderr, "Syntax error - forgot \"do\"?\n"); 436 return B_ERROR; 437 } 438 } 439 if (strcasecmp(argv[*argx], "do")==0){ // added "do" -- pfolk@uni.uiuc.edu 1999-11-03 440 the_message.what=B_EXECUTE_PROPERTY; 441 } else if (strcasecmp(argv[*argx], "get")==0){ 442 the_message.what=B_GET_PROPERTY; 443 } else if (strcasecmp(argv[*argx], "set")==0){ 444 the_message.what=B_SET_PROPERTY; 445 } else if (strcasecmp(argv[*argx], "create")==0){ 446 the_message.what=B_CREATE_PROPERTY; 447 } else if (strcasecmp(argv[*argx], "delete")==0){ 448 the_message.what=B_DELETE_PROPERTY; 449 } else if (strcasecmp(argv[*argx], "quit")==0){ 450 the_message.what=B_QUIT_REQUESTED; 451 } else if (strcasecmp(argv[*argx], "save")==0){ 452 the_message.what=B_SAVE_REQUESTED; 453 } else if (strcasecmp(argv[*argx], "load")==0){ 454 the_message.what=B_REFS_RECEIVED; 455 } else if(strcasecmp(argv[*argx], "count")==0){ 456 the_message.what=B_COUNT_PROPERTIES; 457 } else if(strcasecmp(argv[*argx], "getsuites")==0){ 458 the_message.what=B_GET_SUPPORTED_SUITES; 459 } else { 460 switch(strlen(argv[*argx])){ // can be a message constant if 1,2,3 or 4 chars 461 case 1: 462 the_message.what=(int32)argv[*argx][0]; 463 break; 464 case 2: 465 the_message.what=(((int32)argv[*argx][0])<<8)|(((int32)argv[*argx][1])); 466 break; 467 case 3: 468 the_message.what=(((int32)argv[*argx][0])<<16)|(((int32)argv[*argx][1])<<8)|(((int32)argv[*argx][2])); 469 break; 470 case 4: 471 the_message.what=(((int32)argv[*argx][0])<<24)|(((int32)argv[*argx][1])<<16)|(((int32)argv[*argx][2])<<8)|(((int32)argv[*argx][3])); 472 break; 473 default: 474 // maybe this is a user defined command, ask for the supported suites 475 bool found=false; 476 if (target && target->IsValid()) { 477 BMessage rply; 478 if(target->SendMessage(B_GET_SUPPORTED_SUITES, &rply)==B_OK){ 479 // if all goes well, rply contains all kinds of property infos 480 int32 j=0; 481 void *voidptr; 482 int32 sizefound; 483 BPropertyInfo propinfo; 484 const value_info *vinfo; 485 int32 vinfo_index, vinfo_count; 486 487 // const char *str; 488 // while (rply.FindString("suites", j++, &str) == B_OK) 489 // printf ("Suite %ld: %s\n", j, str); 490 // 491 // j = 0; 492 while(rply.FindData("messages", B_PROPERTY_INFO_TYPE, j++, (const void **)&voidptr, &sizefound)==B_OK && !found){ 493 if(propinfo.Unflatten(B_PROPERTY_INFO_TYPE, (const void *)voidptr, sizefound)==B_OK){ 494 vinfo=propinfo.Values(); 495 vinfo_index=0; 496 vinfo_count=propinfo.CountValues(); 497 #if TEST_VALUEINFO>0 498 value_info vinfo[10]={ {"Backup", 'back', B_COMMAND_KIND, "This command backs up your hard drive."}, 499 {"Abort", 'abor', B_COMMAND_KIND, "Stops the current operation..."}, 500 {"Type Code", 'type', B_TYPE_CODE_KIND, "Type code info..."} 501 }; 502 vinfo_count=3; 503 #endif 504 505 while(vinfo_index<vinfo_count){ 506 if(strcmp(vinfo[vinfo_index].name, argv[*argx])==0){ 507 found=true; 508 the_message.what=vinfo[vinfo_index].value; 509 #if TEST_VALUEINFO>0 510 printf("FOUND COMMAND \"%s\" = %lX\n", vinfo[vinfo_index].name, the_message.what); 511 #endif 512 break; 513 } 514 vinfo_index++; 515 } 516 517 } 518 } 519 } 520 } 521 522 523 if(!found){ 524 if(!silent) 525 fprintf(stderr, "Bad verb (\"%s\")\n", argv[*argx]); 526 return -1; 527 } 528 } 529 direct_what = true; 530 } 531 532 status_t result = B_OK; 533 (*argx)++; 534 535 // One exception: Single data item at end of line. 536 if (direct_what && *argx == argc - 1 && argv[*argx] != NULL) { 537 add_data(&the_message, argv, argx); 538 } else { 539 // parse the specifiers 540 if (the_message.what!=B_REFS_RECEIVED){ // LOAD has no specifier 541 while ((result=add_specifier(&the_message, argv, argx, argc))==B_OK) 542 ; 543 544 if (result!=B_ERROR){ // bad syntax 545 if (!silent) 546 fprintf(stderr, "Bad specifier syntax!\n"); 547 return result; 548 } 549 } 550 } 551 552 // if verb is SET or LOAD, there should be a to <value> 553 if ((the_message.what==B_SET_PROPERTY || the_message.what==B_REFS_RECEIVED) && argv[*argx]!=NULL){ 554 if (strcasecmp(argv[*argx], "to")==0) { 555 (*argx)++; 556 } 557 result = add_data(&the_message, argv, argx); 558 if (result!=B_OK) { 559 if (result==B_FILE_NOT_FOUND){ 560 if (!silent) 561 fprintf(stderr, "File not found!\n"); 562 } else { 563 if (!silent) 564 fprintf(stderr, "Invalid 'to...' value format!\n"); 565 } 566 return result; 567 } 568 } 569 570 add_with(&the_message, argv, argx, argc); 571 572 #if DEBUG_HEY>0 573 fprintf(stderr, "Send "); 574 print_message(&the_message); 575 fprintf(stderr, "\n"); 576 #endif 577 578 if (target && target->IsValid()) { 579 if (reply) { 580 result = target->SendMessage(&the_message, reply); 581 } else { 582 result = target->SendMessage(&the_message); 583 } 584 } 585 return result; 586 } 587 588 // There can be a with <name>=<type>() [and <name>=<type> ...] 589 // I treat "and" just the same as "with", it's just to make the script syntax more English-like. 590 status_t 591 add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc) 592 { 593 status_t result = B_OK; 594 if (*argx < argc - 1 && argv[++(*argx)]!=NULL){ 595 // printf ("argv[%ld] = %s\n", *argx, argv[*argx]); 596 if (strcasecmp(argv[*argx], "with")==0){ 597 // printf ("\"with\" detected!\n"); 598 (*argx)++; 599 bool done = false; 600 do { 601 result=add_data(to_message, argv, argx); 602 if (result!=B_OK){ 603 if (result==B_FILE_NOT_FOUND){ 604 if (!silent) 605 fprintf(stderr, "File not found!\n"); 606 } else { 607 if (!silent) 608 fprintf(stderr, "Invalid 'with...' value format!\n"); 609 } 610 return result; 611 } 612 (*argx)++; 613 // printf ("argc = %d, argv[%d] = %s\n", argc, *argx, argv[*argx]); 614 if (*argx < argc - 1 && strcasecmp(argv[*argx], "and")==0) { 615 (*argx)++; 616 } else 617 done = true; 618 } while (!done); 619 } 620 } 621 return result; 622 } 623 624 // returns B_OK if successful 625 // B_ERROR if no more specifiers 626 // B_BAD_SCRIPT_SYNTAX if syntax error 627 status_t 628 add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 argc) 629 { 630 char *property=argv[*argx]; 631 632 if (property==NULL) 633 return B_ERROR; // no more specifiers 634 635 (*argx)++; 636 637 if (strcasecmp(property, "do")==0){ // Part of the "hey App let Specifier do Verb". 638 return B_ERROR; // no more specifiers 639 } 640 641 if (strcasecmp(property, "to")==0){ // it is the 'to' string!!! 642 return B_ERROR; // no more specifiers 643 } 644 645 if (strcasecmp(property, "with")==0){ // it is the 'with' string!!! 646 *argx -= 2; 647 add_with (to_message, argv, argx, argc); 648 return B_ERROR; // no more specifiers 649 } 650 651 if (strcasecmp(property, "of")==0){ // skip "of", read real property 652 property = argv[*argx]; 653 if (property==NULL) 654 return B_BAD_SCRIPT_SYNTAX; // bad syntax 655 (*argx)++; 656 } 657 658 if (strcasecmp(property, "the")==0){ // skip "the", read real property -- pfolk@uni.uiuc.edu 1999-11-03 659 property = argv[*argx]; 660 if (property==NULL) 661 return B_BAD_SCRIPT_SYNTAX; // bad syntax 662 (*argx)++; 663 } 664 665 // decide the specifier 666 667 char *specifier = NULL; 668 if (to_message->what == B_CREATE_PROPERTY) // create is always direct. without this, a "with" would be taken as a specifier 669 (*argx)--; 670 else 671 specifier = argv[*argx]; 672 if (specifier == NULL){ // direct specifier 673 to_message->AddSpecifier(property); 674 return B_ERROR; // no more specifiers 675 } 676 677 (*argx)++; 678 679 if (strcasecmp(specifier, "of")==0){ // direct specifier 680 to_message->AddSpecifier(property); 681 return B_OK; 682 } 683 684 if (strcasecmp(specifier, "to")==0){ // direct specifier 685 to_message->AddSpecifier(property); 686 return B_ERROR; // no more specifiers 687 } 688 689 690 if (specifier[0]=='['){ // index, reverse index or range 691 char *end; 692 int32 ix1, ix2; 693 if (specifier[1]=='-'){ // reverse index 694 ix1 = strtoul(specifier+2, &end, 10); 695 BMessage revspec(B_REVERSE_INDEX_SPECIFIER); 696 revspec.AddString("property", property); 697 revspec.AddInt32("index", ix1); 698 to_message->AddSpecifier(&revspec); 699 } else { // index or range 700 ix1 = strtoul(specifier+1, &end, 10); 701 if (end[0]==']'){ // it was an index 702 to_message->AddSpecifier(property, ix1); 703 return B_OK; 704 } else { 705 specifier=argv[*argx]; 706 if (specifier==NULL){ 707 // I was wrong, it was just an index 708 to_message->AddSpecifier(property, ix1); 709 return B_OK; 710 } 711 (*argx)++; 712 if (strcasecmp(specifier, "to")==0){ 713 specifier = argv[*argx]; 714 if (specifier==NULL){ 715 return B_BAD_SCRIPT_SYNTAX; // wrong syntax 716 } 717 (*argx)++; 718 ix2 = strtoul(specifier, &end, 10); 719 to_message->AddSpecifier(property, ix1, ix2-ix1>0 ? ix2-ix1 : 1); 720 return B_OK; 721 } else { 722 return B_BAD_SCRIPT_SYNTAX; // wrong syntax 723 } 724 } 725 } 726 } else { // name specifier 727 // if it contains only digits, it will be an index... 728 bool index_spec=true; 729 bool reverse = specifier[0]=='-'; 730 // accept bare reverse-index-specs -- pfolk@uni.uiuc.edu 1999-11-03 731 size_t speclen = strlen(specifier); 732 for (int32 i=(reverse?1:0); i<(int32)speclen; ++i){ 733 if (specifier[i]<'0' || specifier[i]>'9'){ 734 index_spec=false; 735 break; 736 } 737 } 738 739 if (index_spec){ 740 if (reverse) { 741 // Copied from above -- pfolk@uni.uiuc.edu 1999-11-03 742 BMessage revspec(B_REVERSE_INDEX_SPECIFIER); 743 revspec.AddString("property", property); 744 revspec.AddInt32("index", atol(specifier+1)); 745 to_message->AddSpecifier(&revspec); 746 } 747 else 748 to_message->AddSpecifier(property, atol(specifier)); 749 } else { 750 // Allow any name by counting an initial " as a literal-string indicator 751 // -- pfolk@uni.uiuc.edu 1999-11-03 752 if (specifier[0]=='\"') { 753 if (specifier[speclen-1]=='\"') 754 specifier[speclen-1]='\0'; 755 ++specifier; 756 --speclen; 757 } 758 to_message->AddSpecifier(property, specifier); 759 } 760 } 761 762 return B_OK; 763 } 764 765 766 status_t 767 add_data(BMessage *to_message, char *argv[], int32 *argx) 768 { 769 char *valuestring=argv[*argx]; 770 771 if (valuestring==NULL) 772 return B_ERROR; 773 774 // try to interpret it as an integer or float 775 bool contains_only_digits = true; 776 bool is_floating_point = false; 777 for (int32 i=0;i<(int32)strlen(valuestring);i++){ 778 if (i!=0 || valuestring[i]!='-') { 779 if (valuestring[i]<'0' || valuestring[i]>'9'){ 780 if (valuestring[i]=='.'){ 781 is_floating_point = true; 782 } else { 783 contains_only_digits = false; 784 break; 785 } 786 } 787 } 788 } 789 //printf("%d %d\n", contains_only_digits,is_floating_point); 790 if (contains_only_digits){ 791 if (is_floating_point){ 792 to_message->AddFloat("data", atof(valuestring)); 793 return B_OK; 794 } else { 795 to_message->AddInt32("data", atol(valuestring)); 796 return B_OK; 797 } 798 } 799 800 // if true or false, it is bool 801 if (strcasecmp(valuestring, "true")==0){ 802 to_message->AddBool("data", true); 803 return B_OK; 804 } else if (strcasecmp(valuestring, "false")==0){ 805 to_message->AddBool("data", false); 806 return B_OK; 807 } 808 809 // Add support for "<name>=<type>()" here: 810 // The type is then added under the name "name". 811 812 #define MAX_NAME_LENGTH 128 813 char curname[MAX_NAME_LENGTH]; 814 strcpy (curname, "data"); // This is the default. 815 816 char *s = valuestring; 817 while (*++s && *s != '=') 818 // Look for a '=' character... 819 ; 820 if (*s == '=') { // We found a <name>= 821 *s = 0; 822 strcpy (curname, valuestring); // Use the new <name> 823 valuestring = s + 1; // Reposition the valuestring ptr. 824 } 825 826 // must begin with a type( value ) 827 if (strncasecmp(valuestring, "int8", strlen("int8"))==0){ 828 to_message->AddInt8(curname, atol(valuestring+strlen("int8("))); 829 return B_OK; 830 } else if (strncasecmp(valuestring, "int16", strlen("int16"))==0){ 831 to_message->AddInt16(curname, atol(valuestring+strlen("int16("))); 832 return B_OK; 833 } else if (strncasecmp(valuestring, "int32", strlen("int32"))==0){ 834 to_message->AddInt32(curname, atol(valuestring+strlen("int32("))); 835 return B_OK; 836 } else if (strncasecmp(valuestring, "int64", strlen("int64"))==0){ 837 to_message->AddInt64(curname, atol(valuestring+strlen("int64("))); 838 return B_OK; 839 } else if (strncasecmp(valuestring, "bool", strlen("bool"))==0){ 840 if (strncasecmp(valuestring+strlen("bool("), "true", 4)==0){ 841 to_message->AddBool(curname, true); 842 } else if (strncasecmp(valuestring+strlen("bool("), "false", 5)==0){ 843 to_message->AddBool(curname, false); 844 } else { 845 to_message->AddBool(curname, atol(valuestring+strlen("bool("))==0 ? false : true); 846 } 847 return B_OK; 848 } else if (strncasecmp(valuestring, "float", strlen("float"))==0){ 849 to_message->AddFloat(curname, atof(valuestring+strlen("float("))); 850 return B_OK; 851 } else if (strncasecmp(valuestring, "double", strlen("double"))==0){ 852 to_message->AddDouble(curname, atof(valuestring+strlen("double("))); 853 return B_OK; 854 } else if (strncasecmp(valuestring, "BPoint", strlen("BPoint"))==0){ 855 float x,y; 856 x = atof(valuestring+strlen("BPoint(")); 857 if (strchr(valuestring, ',')){ 858 y = atof(strchr(valuestring, ',')+1); 859 } else if (strchr(valuestring, ' ')){ 860 y = atof(strchr(valuestring, ' ')+1); 861 } else { // bad syntax 862 y=0.0f; 863 } 864 to_message->AddPoint(curname, BPoint(x,y)); 865 return B_OK; 866 } else if (strncasecmp(valuestring, "BRect", strlen("BRect"))==0){ 867 float l=0.0f, t=0.0f, r=0.0f, b=0.0f; 868 char *ptr; 869 l = atof(valuestring+strlen("BRect(")); 870 ptr = strchr(valuestring, ','); 871 if (ptr){ 872 t = atof(ptr+1); 873 ptr = strchr(ptr+1, ','); 874 if (ptr){ 875 r = atof(ptr+1); 876 ptr = strchr(ptr+1, ','); 877 if (ptr){ 878 b = atof(ptr+1); 879 } 880 } 881 } 882 883 to_message->AddRect(curname, BRect(l,t,r,b)); 884 return B_OK; 885 } else if (strncasecmp(valuestring, "rgb_color", strlen("rgb_color"))==0){ 886 rgb_color clr; 887 char *ptr; 888 clr.red = atol(valuestring+strlen("rgb_color(")); 889 ptr = strchr(valuestring, ','); 890 if (ptr){ 891 clr.green = atol(ptr+1); 892 ptr = strchr(ptr+1, ','); 893 if (ptr){ 894 clr.blue = atol(ptr+1); 895 ptr = strchr(ptr+1, ','); 896 if (ptr){ 897 clr.alpha = atol(ptr+1); 898 } 899 } 900 } 901 902 to_message->AddData(curname, B_RGB_COLOR_TYPE, &clr, sizeof(rgb_color)); 903 return B_OK; 904 } else if (strncasecmp(valuestring, "file", strlen("file"))==0){ 905 entry_ref file_ref; 906 907 // remove the last ] or ) 908 if (valuestring[strlen(valuestring)-1]==')' || valuestring[strlen(valuestring)-1]==']'){ 909 valuestring[strlen(valuestring)-1] = 0; 910 } 911 912 if (get_ref_for_path(valuestring+5, &file_ref)!=B_OK){ 913 return B_FILE_NOT_FOUND; 914 } 915 916 // check if the ref is valid 917 BEntry entry; 918 if (entry.SetTo(&file_ref)!=B_OK) return B_FILE_NOT_FOUND; 919 //if(!entry.Exists()) return B_FILE_NOT_FOUND; 920 921 // add both ways, refsreceived needs it as "refs" while scripting needs "data" 922 to_message->AddRef("refs", &file_ref); 923 to_message->AddRef(curname, &file_ref); 924 return B_OK; 925 } else { // it is string 926 // does it begin with a quote? 927 if (valuestring[0]=='\"'){ 928 if (valuestring[strlen(valuestring)-1]=='\"') valuestring[strlen(valuestring)-1]=0; 929 to_message->AddString(curname, valuestring+1); 930 } else { 931 to_message->AddString(curname, valuestring); 932 } 933 return B_OK; 934 } 935 936 return B_OK; 937 } 938 939 940 void 941 print_message(BMessage *message) 942 { 943 BList textlist; 944 add_message_contents(&textlist, message, 0); 945 946 printf("BMessage(%s):\n", get_datatype_string(message->what)); 947 for (int32 i=0;i<textlist.CountItems();i++){ 948 printf(" %s\n", (char*)textlist.ItemAt(i)); 949 free(textlist.ItemAt(i)); 950 } 951 952 } 953 954 955 void 956 add_message_contents(BList *textlist, BMessage *msg, int32 level) 957 { 958 int32 count; 959 int32 i, sizefound, j; 960 ulong typefound; 961 #ifdef HAIKU_TARGET_PLATFORM_DANO 962 const char *namefound; 963 #else 964 char *namefound; 965 #endif 966 void *voidptr; 967 BMessage a_message; 968 char *textline, *datatype, *content; 969 970 // go though all message data 971 count = msg->CountNames(B_ANY_TYPE); 972 for (i=0; i<count; i++){ 973 msg->GetInfo(B_ANY_TYPE, i, &namefound, &typefound); 974 j = 0; 975 976 while (msg->FindData(namefound, typefound, j++, (const void **)&voidptr, &sizefound)==B_OK){ 977 datatype = get_datatype_string(typefound); 978 content = format_data(typefound, (char*)voidptr, sizefound); 979 textline = (char*)malloc(20+level*4+strlen(namefound)+strlen(datatype)+strlen(content)); 980 memset(textline, 32, 20+level*4); 981 sprintf(textline+level*4, "\"%s\" (%s) : %s", namefound, datatype, content); 982 textlist->AddItem(textline); 983 delete [] datatype; 984 delete [] content; 985 986 if (typefound==B_MESSAGE_TYPE){ 987 msg->FindMessage(namefound, j-1, &a_message); 988 add_message_contents(textlist, &a_message, level+1); 989 } else if (typefound==B_RAW_TYPE && strcmp(namefound, "_previous_")==0){ 990 if (a_message.Unflatten((const char *)voidptr)==B_OK){ 991 add_message_contents(textlist, &a_message, level+1); 992 } 993 } 994 } 995 } 996 } 997 998 999 char * 1000 get_datatype_string(int32 type) 1001 { 1002 char *str = new char[128]; 1003 1004 switch (type){ 1005 case B_ANY_TYPE: strcpy(str, "B_ANY_TYPE"); break; 1006 case B_ASCII_TYPE: strcpy(str, "B_ASCII_TYPE"); break; 1007 case B_BOOL_TYPE: strcpy(str, "B_BOOL_TYPE"); break; 1008 case B_CHAR_TYPE: strcpy(str, "B_CHAR_TYPE"); break; 1009 case B_COLOR_8_BIT_TYPE: strcpy(str, "B_COLOR_8_BIT_TYPE"); break; 1010 case B_DOUBLE_TYPE: strcpy(str, "B_DOUBLE_TYPE"); break; 1011 case B_FLOAT_TYPE: strcpy(str, "B_FLOAT_TYPE"); break; 1012 case B_GRAYSCALE_8_BIT_TYPE: strcpy(str, "B_GRAYSCALE_8_BIT_TYPE"); break; 1013 case B_INT64_TYPE: strcpy(str, "B_INT64_TYPE"); break; 1014 case B_INT32_TYPE: strcpy(str, "B_INT32_TYPE"); break; 1015 case B_INT16_TYPE: strcpy(str, "B_INT16_TYPE"); break; 1016 case B_INT8_TYPE: strcpy(str, "B_INT8_TYPE"); break; 1017 case B_MESSAGE_TYPE: strcpy(str, "B_MESSAGE_TYPE"); break; 1018 case B_MESSENGER_TYPE: strcpy(str, "B_MESSENGER_TYPE"); break; 1019 case B_MIME_TYPE: strcpy(str, "B_MIME_TYPE"); break; 1020 case B_MONOCHROME_1_BIT_TYPE: strcpy(str, "B_MONOCHROME_1_BIT_TYPE"); break; 1021 case B_OBJECT_TYPE: strcpy(str, "B_OBJECT_TYPE"); break; 1022 case B_OFF_T_TYPE: strcpy(str, "B_OFF_T_TYPE"); break; 1023 case B_PATTERN_TYPE: strcpy(str, "B_PATTERN_TYPE"); break; 1024 case B_POINTER_TYPE: strcpy(str, "B_POINTER_TYPE"); break; 1025 case B_POINT_TYPE: strcpy(str, "B_POINT_TYPE"); break; 1026 case B_RAW_TYPE: strcpy(str, "B_RAW_TYPE"); break; 1027 case B_RECT_TYPE: strcpy(str, "B_RECT_TYPE"); break; 1028 case B_REF_TYPE: strcpy(str, "B_REF_TYPE"); break; 1029 case B_RGB_32_BIT_TYPE: strcpy(str, "B_RGB_32_BIT_TYPE"); break; 1030 case B_RGB_COLOR_TYPE: strcpy(str, "B_RGB_COLOR_TYPE"); break; 1031 case B_SIZE_T_TYPE: strcpy(str, "B_SIZE_T_TYPE"); break; 1032 case B_SSIZE_T_TYPE : strcpy(str, "B_SSIZE_T_TYPE"); break; 1033 case B_STRING_TYPE: strcpy(str, "B_STRING_TYPE"); break; 1034 case B_TIME_TYPE : strcpy(str, "B_TIME_TYPE"); break; 1035 case B_UINT64_TYPE : strcpy(str, "B_UINT64_TYPE"); break; 1036 case B_UINT32_TYPE: strcpy(str, "B_UINT32_TYPE"); break; 1037 case B_UINT16_TYPE : strcpy(str, "B_UINT16_TYPE"); break; 1038 case B_UINT8_TYPE : strcpy(str, "B_UINT8_TYPE"); break; 1039 case B_PROPERTY_INFO_TYPE: strcpy(str, "B_PROPERTY_INFO_TYPE"); break; 1040 // message constants: 1041 case B_ABOUT_REQUESTED : strcpy(str, "B_ABOUT_REQUESTED"); break; 1042 case B_WINDOW_ACTIVATED : strcpy(str, "B_WINDOW_ACTIVATED"); break; 1043 case B_ARGV_RECEIVED : strcpy(str, "B_ARGV_RECEIVED"); break; 1044 case B_QUIT_REQUESTED : strcpy(str, "B_QUIT_REQUESTED"); break; 1045 case B_CANCEL : strcpy(str, "B_CANCEL"); break; 1046 case B_KEY_DOWN : strcpy(str, "B_KEY_DOWN"); break; 1047 case B_KEY_UP : strcpy(str, "B_KEY_UP"); break; 1048 case B_MINIMIZE : strcpy(str, "B_MINIMIZE"); break; 1049 case B_MOUSE_DOWN : strcpy(str, "B_MOUSE_DOWN"); break; 1050 case B_MOUSE_MOVED : strcpy(str, "B_MOUSE_MOVED"); break; 1051 case B_MOUSE_ENTER_EXIT : strcpy(str, "B_MOUSE_ENTER_EXIT"); break; 1052 case B_MOUSE_UP : strcpy(str, "B_MOUSE_UP"); break; 1053 case B_PULSE : strcpy(str, "B_PULSE"); break; 1054 case B_READY_TO_RUN : strcpy(str, "B_READY_TO_RUN"); break; 1055 case B_REFS_RECEIVED : strcpy(str, "B_REFS_RECEIVED"); break; 1056 case B_SCREEN_CHANGED : strcpy(str, "B_SCREEN_CHANGED"); break; 1057 case B_VALUE_CHANGED : strcpy(str, "B_VALUE_CHANGED"); break; 1058 case B_VIEW_MOVED : strcpy(str, "B_VIEW_MOVED"); break; 1059 case B_VIEW_RESIZED : strcpy(str, "B_VIEW_RESIZED"); break; 1060 case B_WINDOW_MOVED : strcpy(str, "B_WINDOW_MOVED"); break; 1061 case B_WINDOW_RESIZED : strcpy(str, "B_WINDOW_RESIZED"); break; 1062 case B_WORKSPACES_CHANGED : strcpy(str, "B_WORKSPACES_CHANGED"); break; 1063 case B_WORKSPACE_ACTIVATED : strcpy(str, "B_WORKSPACE_ACTIVATED"); break; 1064 case B_ZOOM : strcpy(str, "B_ZOOM"); break; 1065 case _APP_MENU_ : strcpy(str, "_APP_MENU_"); break; 1066 case _BROWSER_MENUS_ : strcpy(str, "_BROWSER_MENUS_"); break; 1067 case _MENU_EVENT_ : strcpy(str, "_MENU_EVENT_"); break; 1068 case _QUIT_ : strcpy(str, "_QUIT_"); break; 1069 case _VOLUME_MOUNTED_ : strcpy(str, "_VOLUME_MOUNTED_"); break; 1070 case _VOLUME_UNMOUNTED_ : strcpy(str, "_VOLUME_UNMOUNTED_"); break; 1071 case _MESSAGE_DROPPED_ : strcpy(str, "_MESSAGE_DROPPED_"); break; 1072 case _MENUS_DONE_ : strcpy(str, "_MENUS_DONE_"); break; 1073 case _SHOW_DRAG_HANDLES_ : strcpy(str, "_SHOW_DRAG_HANDLES_"); break; 1074 case B_SET_PROPERTY : strcpy(str, "B_SET_PROPERTY"); break; 1075 case B_GET_PROPERTY : strcpy(str, "B_GET_PROPERTY"); break; 1076 case B_CREATE_PROPERTY : strcpy(str, "B_CREATE_PROPERTY"); break; 1077 case B_DELETE_PROPERTY : strcpy(str, "B_DELETE_PROPERTY"); break; 1078 case B_COUNT_PROPERTIES : strcpy(str, "B_COUNT_PROPERTIES"); break; 1079 case B_EXECUTE_PROPERTY : strcpy(str, "B_EXECUTE_PROPERTY"); break; 1080 case B_GET_SUPPORTED_SUITES : strcpy(str, "B_GET_SUPPORTED_SUITES"); break; 1081 case B_CUT : strcpy(str, "B_CUT"); break; 1082 case B_COPY : strcpy(str, "B_COPY"); break; 1083 case B_PASTE : strcpy(str, "B_PASTE"); break; 1084 case B_SELECT_ALL : strcpy(str, "B_SELECT_ALL"); break; 1085 case B_SAVE_REQUESTED : strcpy(str, "B_SAVE_REQUESTED"); break; 1086 case B_MESSAGE_NOT_UNDERSTOOD : strcpy(str, "B_MESSAGE_NOT_UNDERSTOOD"); break; 1087 case B_NO_REPLY : strcpy(str, "B_NO_REPLY"); break; 1088 case B_REPLY : strcpy(str, "B_REPLY"); break; 1089 case B_SIMPLE_DATA : strcpy(str, "B_SIMPLE_DATA"); break; 1090 //case B_MIME_DATA : strcpy(str, "B_MIME_DATA"); break; 1091 case B_ARCHIVED_OBJECT : strcpy(str, "B_ARCHIVED_OBJECT"); break; 1092 case B_UPDATE_STATUS_BAR : strcpy(str, "B_UPDATE_STATUS_BAR"); break; 1093 case B_RESET_STATUS_BAR : strcpy(str, "B_RESET_STATUS_BAR"); break; 1094 case B_NODE_MONITOR : strcpy(str, "B_NODE_MONITOR"); break; 1095 case B_QUERY_UPDATE : strcpy(str, "B_QUERY_UPDATE"); break; 1096 case B_BAD_SCRIPT_SYNTAX: strcpy(str, "B_BAD_SCRIPT_SYNTAX"); break; 1097 1098 // specifiers: 1099 case B_NO_SPECIFIER : strcpy(str, "B_NO_SPECIFIER"); break; 1100 case B_DIRECT_SPECIFIER : strcpy(str, "B_DIRECT_SPECIFIER"); break; 1101 case B_INDEX_SPECIFIER : strcpy(str, "B_INDEX_SPECIFIER"); break; 1102 case B_REVERSE_INDEX_SPECIFIER : strcpy(str, "B_REVERSE_INDEX_SPECIFIER"); break; 1103 case B_RANGE_SPECIFIER : strcpy(str, "B_RANGE_SPECIFIER"); break; 1104 case B_REVERSE_RANGE_SPECIFIER : strcpy(str, "B_REVERSE_RANGE_SPECIFIER"); break; 1105 case B_NAME_SPECIFIER : strcpy(str, "B_NAME_SPECIFIER"); break; 1106 1107 case B_ERROR : strcpy(str, "B_ERROR"); break; 1108 1109 default: // unknown 1110 id_to_string(type, str); 1111 break; 1112 } 1113 1114 return str; 1115 1116 } 1117 1118 1119 char * 1120 format_data(int32 type, char *ptr, long size) 1121 { 1122 char idtext[32]; 1123 char *str; 1124 float *fptr; 1125 double *dptr; 1126 // BRect *brptr; 1127 entry_ref aref; 1128 BEntry entry; 1129 BPath path; 1130 int64 i64; 1131 int32 i32; 1132 int16 i16; 1133 int8 i8; 1134 uint64 ui64; 1135 uint32 ui32; 1136 uint16 ui16; 1137 uint8 ui8; 1138 BMessage anothermsg; 1139 char *tempstr; 1140 1141 if (size<=0L){ 1142 str = new char; 1143 *str = 0; 1144 return str; 1145 } 1146 1147 switch (type){ 1148 case B_MIME_TYPE: 1149 case B_ASCII_TYPE: 1150 case B_STRING_TYPE: 1151 if (size>512) 1152 size=512; 1153 str = new char[size+4]; 1154 *str='\"'; 1155 strncpy(str+1, ptr, size); 1156 strcat(str, "\""); 1157 break; 1158 case B_POINTER_TYPE: 1159 str = new char[64]; 1160 sprintf(str, "%p", *(void**)ptr); 1161 break; 1162 1163 case B_REF_TYPE: 1164 str = new char[1024]; 1165 anothermsg.AddData("myref", B_REF_TYPE, ptr, size); 1166 anothermsg.FindRef("myref", &aref); 1167 if (entry.SetTo(&aref)==B_OK){ 1168 entry.GetPath(&path); 1169 strcpy(str, path.Path()); 1170 } else { 1171 strcpy(str, "invalid entry_ref"); 1172 } 1173 break; 1174 1175 case B_SSIZE_T_TYPE: 1176 case B_INT64_TYPE: 1177 str = new char[64]; 1178 i64 = *(int64*)ptr; 1179 sprintf(str, "%Ld (0x%LX)", i64, i64); 1180 break; 1181 1182 case B_SIZE_T_TYPE: 1183 case B_INT32_TYPE: 1184 str = new char[64]; 1185 i32 = *(int32*)ptr; 1186 sprintf(str, "%ld (0x%08lX)", i32, i32); 1187 break; 1188 1189 case B_INT16_TYPE: 1190 str = new char[64]; 1191 i16 = *(int16*)ptr; 1192 sprintf(str, "%d (0x%04X)", i16, i16); 1193 break; 1194 1195 case B_CHAR_TYPE: 1196 case B_INT8_TYPE: 1197 str = new char[64]; 1198 i8 = *(int8*)ptr; 1199 sprintf(str, "%d (0x%02X)", i8, i8); 1200 break; 1201 1202 case B_UINT64_TYPE: 1203 str = new char[64]; 1204 ui64 = *(uint64*)ptr; 1205 sprintf(str, "%Lu (0x%LX)", ui64, ui64); 1206 break; 1207 1208 case B_UINT32_TYPE: 1209 str = new char[64]; 1210 ui32 = *(uint32*)ptr; 1211 sprintf(str, "%lu (0x%08lX)", ui32, ui32); 1212 break; 1213 1214 case B_UINT16_TYPE: 1215 str = new char[64]; 1216 ui16 = *(uint16*)ptr; 1217 sprintf(str, "%u (0x%04X)", ui16, ui16); 1218 break; 1219 1220 case B_UINT8_TYPE: 1221 str = new char[64]; 1222 ui8 = *(uint8*)ptr; 1223 sprintf(str, "%u (0x%02X)", ui8, ui8); 1224 break; 1225 1226 case B_BOOL_TYPE: 1227 str = new char[10]; 1228 if (*ptr){ 1229 strcpy(str, "TRUE"); 1230 } else { 1231 strcpy(str, "FALSE"); 1232 } 1233 break; 1234 1235 case B_FLOAT_TYPE: 1236 str = new char[40]; 1237 fptr = (float*)ptr; 1238 sprintf(str, "%.3f", *fptr); 1239 break; 1240 1241 case B_DOUBLE_TYPE: 1242 str = new char[40]; 1243 dptr = (double*)ptr; 1244 sprintf(str, "%.3f", *dptr); 1245 break; 1246 1247 case B_RECT_TYPE: 1248 str = new char[200]; 1249 fptr = (float*)ptr; 1250 sprintf(str, "BRect(%.1f, %.1f, %.1f, %.1f)", fptr[0], fptr[1], fptr[2], fptr[3]); 1251 break; 1252 1253 case B_POINT_TYPE: 1254 str = new char[200]; 1255 fptr = (float*)ptr; 1256 sprintf(str, "BPoint(%.1f, %.1f)", fptr[0], fptr[1]); 1257 break; 1258 1259 case B_RGB_COLOR_TYPE: 1260 str = new char[64]; 1261 sprintf(str, "Red=%u Green=%u Blue=%u Alpha=%u", ((uint8*)ptr)[0], ((uint8*)ptr)[1], ((uint8*)ptr)[2], ((uint8*)ptr)[3] ); 1262 break; 1263 1264 case B_COLOR_8_BIT_TYPE: 1265 str = new char[size*6+4]; 1266 *str = 0; 1267 for (int32 i=0; i<min_c(256,size); i++){ 1268 sprintf(idtext, "%u ", ((unsigned char*)ptr)[i]); 1269 strcat(str,idtext); 1270 } 1271 *(str+strlen(str)-2) = 0; 1272 break; 1273 1274 case B_MESSAGE_TYPE: 1275 str = new char[64]; 1276 if (anothermsg.Unflatten((const char *)ptr)==B_OK){ 1277 sprintf(str, "what=%s", get_datatype_string(anothermsg.what)); 1278 } else { 1279 strcpy(str, "error when unflattening"); 1280 } 1281 break; 1282 1283 case B_PROPERTY_INFO_TYPE: { 1284 BPropertyInfo propinfo; 1285 if (propinfo.Unflatten(B_PROPERTY_INFO_TYPE, (const void *)ptr, size)==B_OK){ 1286 str = new char[size*32]; // an approximation 1287 1288 //propinfo.PrintToStream(); 1289 //sprintf(str, "see the printout above"); 1290 1291 const property_info *pinfo = propinfo.Properties(); 1292 1293 sprintf(str, "\n property commands specifiers types\n---------------------------------------------------------------------------------------------------\n"); 1294 for (int32 pinfo_index = 0; pinfo_index<propinfo.CountProperties(); pinfo_index++) { 1295 strcat(str, " "+(strlen(pinfo[pinfo_index].name) <16 ? strlen(pinfo[pinfo_index].name) : 16 )); 1296 strcat(str, pinfo[pinfo_index].name); 1297 strcat(str, " "); 1298 char *start = str+strlen(str); 1299 1300 for (int32 i=0; i<10 && pinfo[pinfo_index].commands[i]; i++){ 1301 //id_to_string(pinfo[pinfo_index].commands[i], str+strlen(str) ); 1302 tempstr = get_datatype_string(pinfo[pinfo_index].commands[i]); 1303 strcat(str, tempstr); 1304 strcat(str, " "); 1305 delete [] tempstr; 1306 } 1307 1308 // pad the rest with spaces 1309 if (strlen(start)<36){ 1310 strcat(str, " "+strlen(start) ); 1311 } else { 1312 strcat(str, " " ); 1313 } 1314 1315 for (int32 i=0; i<10 && pinfo[pinfo_index].specifiers[i]; i++){ 1316 switch (pinfo[pinfo_index].specifiers[i]){ 1317 case B_NO_SPECIFIER: strcat(str, "NONE "); break; 1318 case B_DIRECT_SPECIFIER: strcat(str, "DIRECT "); break; 1319 case B_INDEX_SPECIFIER: strcat(str, "INDEX "); break; 1320 case B_REVERSE_INDEX_SPECIFIER: strcat(str, "REV.INDEX "); break; 1321 case B_RANGE_SPECIFIER: strcat(str, "RANGE "); break; 1322 case B_REVERSE_RANGE_SPECIFIER: strcat(str, "REV.RANGE "); break; 1323 case B_NAME_SPECIFIER: strcat(str, "NAME "); break; 1324 case B_ID_SPECIFIER: strcat(str, "ID "); break; 1325 default: strcat(str, "<NONE> "); break; 1326 } 1327 } 1328 1329 // pad the rest with spaces 1330 if (strlen(start)<60){ 1331 strcat(str, " "+strlen(start) ); 1332 } else { 1333 strcat(str, " " ); 1334 } 1335 for (int32 i = 0; i < 10 && pinfo[pinfo_index].types[i] != 0; i++) { 1336 uint32 type = pinfo[pinfo_index].types[i]; 1337 char str2[4]; 1338 sprintf(str2, "%c%c%c%c ", int(type & 0xFF000000) >> 24, 1339 int(type & 0xFF0000) >> 16, int(type & 0xFF00) >> 8, (int)type & 0xFF); 1340 strcat(str, str2); 1341 } 1342 1343 for (int32 i = 0; i < 3; i++) { 1344 for (int32 j = 0; j < 5 && pinfo[pinfo_index].ctypes[i].pairs[j].type != 0; j++) { 1345 uint32 type = pinfo[pinfo_index].ctypes[i].pairs[j].type; 1346 char str2[4]; 1347 sprintf(str2, "(%s %c%c%c%c)", pinfo[pinfo_index].ctypes[i].pairs[j].name, 1348 int(type & 0xFF000000) >> 24, 1349 int(type & 0xFF0000) >> 16, 1350 int(type & 0xFF00) >> 8, (int)type & 0xFF); 1351 strcat(str, str2); 1352 } 1353 } 1354 strcat(str, "\n"); 1355 1356 // is there usage info? 1357 if (pinfo[pinfo_index].usage){ 1358 strcat(str, " Usage: "); 1359 strcat(str, pinfo[pinfo_index].usage); 1360 strcat(str, "\n"); 1361 } 1362 1363 } 1364 1365 1366 // handle value infos.... 1367 const value_info *vinfo = propinfo.Values(); 1368 int32 vinfo_count = propinfo.CountValues(); 1369 #if TEST_VALUEINFO>0 1370 value_info vinfo[10] = { {"Backup", 'back', B_COMMAND_KIND, "This command backs up your hard drive."}, 1371 {"Abort", 'abor', B_COMMAND_KIND, "Stops the current operation..."}, 1372 {"Type Code", 'type', B_TYPE_CODE_KIND, "Type code info..."} 1373 }; 1374 vinfo_count = 3; 1375 #endif 1376 1377 if (vinfo && vinfo_count>0){ 1378 sprintf(str+strlen(str), "\n name value kind\n--------------------------------------------------------------------------------\n"); 1379 1380 for (int32 vinfo_index = 0; vinfo_index<vinfo_count; vinfo_index++){ 1381 1382 char *start = str+strlen(str); 1383 strcat(str, " "+(strlen(vinfo[vinfo_index].name) <16 ? strlen(vinfo[vinfo_index].name) : 16 )); 1384 strcat(str, vinfo[vinfo_index].name); 1385 strcat(str, " "); 1386 1387 sprintf(str+strlen(str), "0x%8lX (", vinfo[vinfo_index].value); 1388 id_to_string(vinfo[vinfo_index].value, str+strlen(str)); 1389 strcat(str, ")"); 1390 1391 // pad the rest with spaces 1392 if (strlen(start)<36+19){ 1393 strcat(str, " "+strlen(start) ); 1394 } else { 1395 strcat(str, " " ); 1396 } 1397 1398 switch (vinfo[vinfo_index].kind){ 1399 case B_COMMAND_KIND: strcat(str, "COMMAND "); break; 1400 case B_TYPE_CODE_KIND: strcat(str, "TYPE CODE "); break; 1401 default: strcat(str, "unknown "); break; 1402 } 1403 1404 strcat(str, "\n"); 1405 1406 // is there usage info? 1407 if (vinfo[vinfo_index].usage){ 1408 strcat(str, " Usage: "); 1409 strcat(str, vinfo[vinfo_index].usage); 1410 strcat(str, "\n"); 1411 } 1412 } 1413 } 1414 1415 } else { 1416 str = new char[64]; 1417 strcpy(str, "error when unflattening"); 1418 } 1419 break; 1420 } 1421 1422 default: 1423 str = new char[min_c(256,size)*20+4]; 1424 *str = 0; 1425 for (int32 i=0; i<min_c(256,size); i++){ 1426 //sprintf(idtext, "0x%02X ('%c'), ", (uint16)ptr[i], ptr[i]<32 ? 32 : ptr[i]); 1427 sprintf(idtext, "0x%02X, ", (uint16)ptr[i] ); 1428 strcat(str,idtext); 1429 } 1430 *(str+strlen(str)-2) = 0; 1431 break; 1432 } 1433 1434 return str; 1435 1436 } 1437 1438 1439 char * 1440 id_to_string(long ID, char *here) 1441 { 1442 uint8 digit0 = (ID>>24)& 255; 1443 uint8 digit1 = (ID>>16)& 255; 1444 uint8 digit2 = (ID>>8) & 255; 1445 uint8 digit3 = (ID) & 255; 1446 bool itsvalid = false; 1447 1448 if (digit0==0){ 1449 if (digit1==0){ 1450 if (digit2==0) { 1451 // 1 digits 1452 if (is_valid_char(digit3) ) 1453 itsvalid=TRUE; 1454 sprintf(here, "'%c'", digit3); 1455 } else { 1456 // 2 digits 1457 if (is_valid_char(digit2) && is_valid_char(digit3) ) 1458 itsvalid=TRUE; 1459 sprintf(here, "'%c%c'", digit2, digit3); 1460 } 1461 } else { 1462 // 3 digits 1463 if (is_valid_char(digit1) && is_valid_char(digit2) && is_valid_char(digit3) ) 1464 itsvalid=TRUE; 1465 sprintf(here, "'%c%c%c'", digit1, digit2, digit3); 1466 } 1467 } else { 1468 // 4 digits 1469 if (is_valid_char(digit0) && is_valid_char(digit1) && is_valid_char(digit2) && is_valid_char(digit3) ) 1470 itsvalid=TRUE; 1471 sprintf(here, "'%c%c%c%c'", digit0, digit1, digit2, digit3); 1472 } 1473 1474 if (!itsvalid){ 1475 sprintf(here, "%ldL", ID); 1476 } 1477 1478 return here; 1479 } 1480 1481 1482 bool 1483 is_valid_char(uint8 c) 1484 { 1485 return (c>=32 && c<128); 1486 } 1487 1488