Update hey.cpp to the one in the Haiku repository.

This version has the advantage (at least) to compile with gcc 4.
This commit is contained in:
ocoursiere
2012-10-18 22:49:05 +00:00
parent fd1986cff4
commit 345c1d7463

View File

@@ -5,7 +5,24 @@
// //
// public domain, use it at your own risk // public domain, use it at your own risk
// //
// 1.2.6: syntax extended by Sander Stoks <sander@adamation.com> to allow: // 1.2.8: (Sander Stoks): Added command-line option -o which will output the "result" value
// in the reply message to stdout, so you can use it in shell scripting more easily:
// "hey Becasso get AspectRatio of Canvas 0"
// outputs
// Reply BMessage(B_REPLY):
// "result" (B_DOUBLE_TYPE) : 0.600
// but "hey -o Becasso get AspectRatio of Canvas 0"
// outputs 0.600000 directly.
//
// 1.2.7: by Sander Stoks: Made a fork since I don't think Attila still supports "hey", and
// because the latest version on BeBits seems to be 1.2.4.
// Changes w.r.t. 1.2.6: When an application returns an error on a message, hey now
// keeps iterating over applications with the same signature. This is useful because,
// for instance, Terminal starts as a new process for each instance, so it previously
// wouldn't work to move a specific Terminal window using hey. You can now say
// "hey Terminal set Frame of Window foo to BRect[...]".
//
// 1.2.6: syntax extended by Sander Stoks <sander@stoks.nl to allow:
// "hey Application let Specifier do ..." // "hey Application let Specifier do ..."
// allowing you to send messages directly to other handlers than the app itself. // allowing you to send messages directly to other handlers than the app itself.
// In cooperation with the new Application defined commands (note that some // In cooperation with the new Application defined commands (note that some
@@ -84,15 +101,29 @@
//v1.0.0 First public release //v1.0.0 First public release
#include "hey.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <vector>
#include <AppKit.h> #include <AppKit.h>
#include <Path.h> #include <Path.h>
#include <SupportDefs.h>
const char VERSION[]="v1.2.6"; int32 HeyInterpreterThreadHook(void* arg);
status_t Hey(BMessenger* target, const char* arg, BMessage* reply);
bool isSpace(char c);
status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage* reply);
status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 argc);
status_t add_data(BMessage *to_message, char *argv[], int32 *argx);
status_t add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc);
void add_message_contents(BList *textlist, BMessage *msg, int32 level);
char *get_datatype_string(int32 type);
char *format_data(int32 type, char *ptr, long size);
void print_message(BMessage *message);
char *id_to_string(long ID, char *here);
bool is_valid_char(uint8 c);
const char VERSION[] = "v1.2.8";
#define DEBUG_HEY 0 // 1: prints the script message to be sent to the target application, 0: prints only the reply #define DEBUG_HEY 0 // 1: prints the script message to be sent to the target application, 0: prints only the reply
@@ -103,15 +134,111 @@ const char VERSION[]="v1.2.6";
// flag for silent mode // flag for silent mode
bool silent; bool silent;
// flag for stdout mode
bool output;
status_t
parse(BMessenger& the_application, int argc, char *argv[], int32 argapp)
{
if (!the_application.IsValid()) {
if (!silent)
fprintf(stderr, "Cannot find the application (%s)\n", argv[argapp]);
return B_ERROR;
}
if (argc < 3) {
if (!silent)
fprintf(stderr, "Cannot find the verb!\n");
return B_ERROR;
}
int main(int argc, char *argv[]) BMessage the_reply;
int32 argx = argapp+1;
status_t err = Hey(&the_application, argv, &argx, argc, &the_reply);
if (err != B_OK) {
if (!silent)
fprintf(stderr, "Error when sending message to %s!\n", argv[argapp]);
return B_ERROR;
} else {
if (the_reply.what == (uint32)B_MESSAGE_NOT_UNDERSTOOD || the_reply.what==(uint32)B_ERROR){ // I do it myself
if (the_reply.HasString("message")){
if (!silent)
printf("%s (error 0x%8lX)\n", the_reply.FindString("message"), the_reply.FindInt32("error"));
} else {
if (!silent)
printf("error 0x%8lX\n", the_reply.FindInt32("error"));
}
return 1;
} else {
if (!silent){
if (output){
type_code tc;
if (the_reply.GetInfo("result", &tc) == B_OK){
if(tc==B_INT8_TYPE){
int8 v;
the_reply.FindInt8("result", &v);
printf("%d\n", v);
} else if(tc==B_INT16_TYPE){
int16 v;
the_reply.FindInt16("result", &v);
printf("%d\n", v);
} else if(tc==B_INT32_TYPE){
int32 v;
the_reply.FindInt32("result", &v);
printf("%ld\n", v);
} else if(tc==B_UINT8_TYPE){
uint8 v;
the_reply.FindInt8("result", (int8*)&v);
printf("%u\n", v);
} else if(tc==B_UINT16_TYPE){
uint16 v;
the_reply.FindInt16("result", (int16*)&v);
printf("%u\n", v);
} else if(tc==B_UINT32_TYPE){
uint32 v;
the_reply.FindInt32("result", (int32*)&v);
printf("%lu\n", v);
} else if(tc==B_STRING_TYPE){
const char* v;
the_reply.FindString("result", &v);
printf("%s\n", v);
} else if(tc==B_FLOAT_TYPE){
float v;
the_reply.FindFloat("result", &v);
printf("%f\n", v);
} else if(tc==B_DOUBLE_TYPE){
double v;
the_reply.FindDouble("result", &v);
printf("%f\n", v);
} else if(tc==B_BOOL_TYPE){
bool v;
the_reply.FindBool("result", &v);
printf("%s\n", v?"true":"false");
} else {
printf("Unsupported type\n");
}
}
} else {
printf("Reply ");
print_message(&the_reply);
printf("\n");
}
}
}
}
return B_OK;
}
int
main(int argc, char *argv[])
{ {
BApplication app("application/x-amezei-hey"); BApplication app("application/x-amezei-hey");
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "hey %s, written by Attila Mezei (attila.mezei@mail.datanet.hu)\n" \ fprintf(stderr, "hey %s, written by Attila Mezei (attila.mezei@mail.datanet.hu)\n" \
"usage: hey [-s] <app|signature> [let <specifier> do] <verb> <specifier_1> <of\n" \ "usage: hey [-s][-o] <app|signature|teamid> [let <specifier> do] <verb> <specifier_1> <of\n" \
" <specifier_n>>* [to <value>] [with name=<value> [and name=<value>]*]\n" \ " <specifier_n>>* [to <value>] [with name=<value> [and name=<value>]*]\n" \
"where <verb> : DO|GET|SET|COUNT|CREATE|DELETE|GETSUITES|QUIT|SAVE|LOAD|'what'\n" \ "where <verb> : DO|GET|SET|COUNT|CREATE|DELETE|GETSUITES|QUIT|SAVE|LOAD|'what'\n" \
" <specifier> : [the] <property_name> [ <index> | name | \"name\" | '\"name\"' ]\n" \ " <specifier> : [the] <property_name> [ <index> | name | \"name\" | '\"name\"' ]\n" \
@@ -119,20 +246,29 @@ int main(int argc, char *argv[])
" <value> : \"string\" | <integer> | <float> | bool(value) | int8(value)\n" \ " <value> : \"string\" | <integer> | <float> | bool(value) | int8(value)\n" \
" | int16(value) | int32(value) | float(value) | double(value)\n" \ " | int16(value) | int32(value) | float(value) | double(value)\n" \
" | BPoint(x,y) | BRect(l,t,r,b) | rgb_color(r,g,b,a) | file(path)\n" \ " | BPoint(x,y) | BRect(l,t,r,b) | rgb_color(r,g,b,a) | file(path)\n" \
"options: -s: silent\n\n", VERSION); "options: -s: silent\n" \
" -o: output result to stdout for easy parsing\n\n", VERSION);
// Updated Usage string to reflect "do", "the", bare -index, and '"name"' changes below // Updated Usage string to reflect "do", "the", bare -index, and '"name"' changes below
// -- pfolk@uni.uiuc.edu 1999-11-03 // -- pfolk@uni.uiuc.edu 1999-11-03
return -1; return 1;
} }
int32 argapp = 1; int32 argapp = 1;
silent = false; silent = false;
output = false;
if(strcmp(argv[1], "-s")==0 || strcmp(argv[1], "-S")==0){ // Updated option mechanism --SS
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-s")==0 || strcmp(argv[i], "-S")==0){
silent = true; silent = true;
argapp++; argapp++;
} }
if (strcmp(argv[i], "-o")==0 || strcmp(argv[i], "-O")==0){
output=true;
argapp++;
}
}
// find the application // find the application
BMessenger the_application; BMessenger the_application;
@@ -140,71 +276,53 @@ int main(int argc, char *argv[])
team_id teamid; team_id teamid;
app_info appinfo; app_info appinfo;
teamid = atoi(argv[argapp]);
if (teamid > 0) {
if (be_roster->GetRunningAppInfo(teamid, &appinfo)!=B_OK)
return 1;
the_application=BMessenger(NULL, teamid);
if (!parse(the_application, argc, argv, argapp))
return 0;
return 1;
}
be_roster->GetAppList(&team_list); be_roster->GetAppList(&team_list);
for (int32 i=0; i<team_list.CountItems(); i++){ for (int32 i=0; i<team_list.CountItems(); i++){
teamid = (team_id)team_list.ItemAt(i); teamid = (team_id)team_list.ItemAt(i);
be_roster->GetRunningAppInfo(teamid, &appinfo); be_roster->GetRunningAppInfo(teamid, &appinfo);
if (strcmp(appinfo.signature, argv[argapp])==0){ if (strcmp(appinfo.signature, argv[argapp])==0){
the_application=BMessenger(appinfo.signature); the_application=BMessenger(appinfo.signature);
break; if (!parse(the_application, argc, argv, argapp))
return 0;
} else { } else {
if (strcmp(appinfo.ref.name, argv[argapp])==0){ if (strcmp(appinfo.ref.name, argv[argapp])==0){
the_application=BMessenger(0, teamid); the_application=BMessenger(0, teamid);
break; if (!parse(the_application, argc, argv, argapp))
}
}
}
if(!the_application.IsValid()){
if(!silent) fprintf(stderr, "Cannot find the application (%s)\n", argv[argapp]);
return -1;
}
if (argc < 3) {
if(!silent) fprintf(stderr, "Cannot find the verb!\n");
return -1;
}
BMessage the_reply;
int32 argx = argapp+1;
// const char *test_string = "set File of Window Sample to file(/boot/home/media/images/BeLogo.psd)";
// status_t err = Hey(&the_application, test_string, &the_reply);
status_t err = Hey(&the_application, argv, &argx, argc, &the_reply);
if (err!=B_OK) {
if(!silent) fprintf(stderr, "Error when sending message to %s!\n", argv[argapp]);
return -1;
} else {
if(the_reply.what==(uint32)B_MESSAGE_NOT_UNDERSTOOD || the_reply.what==(uint32)B_ERROR){ // I do it myself
if(the_reply.HasString("message")){
if(!silent) printf("%s (error 0x%8lX)\n", the_reply.FindString("message"), the_reply.FindInt32("error"));
}else{
if(!silent) printf("error 0x%8lX\n", the_reply.FindInt32("error"));
}
}else{
if(!silent){
printf("Reply ");
print_message(&the_reply);
printf("\n");
}
}
}
return 0; return 0;
} }
}
}
return 1;
}
int32 HeyInterpreterThreadHook(void* arg) int32
HeyInterpreterThreadHook(void* arg)
{ {
if (arg) { if (!arg)
return 1;
BMessage environment(*(BMessage*) arg); BMessage environment(*(BMessage*) arg);
char* prompt = "Hey"; const char* prompt = "Hey";
if (environment.HasString("prompt")) environment.FindString("prompt", (const char **)&prompt); if (environment.HasString("prompt"))
environment.FindString("prompt", &prompt);
printf("%s> ", prompt); printf("%s> ", prompt);
BMessenger target; BMessenger target;
if (environment.HasMessenger("Target")) environment.FindMessenger("Target", &target); if (environment.HasMessenger("Target"))
environment.FindMessenger("Target", &target);
char command[1024]; char command[1024];
status_t err; status_t err;
@@ -221,15 +339,12 @@ int32 HeyInterpreterThreadHook(void* arg)
} }
return 0; return 0;
} else {
return 1;
}
} }
status_t Hey(BMessenger* target, const char* arg, BMessage* reply) status_t
Hey(BMessenger* target, const char* arg, BMessage* reply)
{ {
vector<char*> argv; // number of tokens is now limited only by memory -- pfolk@uni.uiuc.edu 1999-11-03 BList argv; // number of tokens is now limited only by memory -- pfolk@uni.uiuc.edu 1999-11-03
char* tokens = new char[strlen(arg)*2]; char* tokens = new char[strlen(arg)*2];
char* currentToken = tokens; char* currentToken = tokens;
int32 tokenNdex = 0; int32 tokenNdex = 0;
@@ -237,11 +352,12 @@ status_t Hey(BMessenger* target, const char* arg, BMessage* reply)
bool inquotes = false; bool inquotes = false;
while (arg[argNdex] != 0) { // for each character in arg while (arg[argNdex] != 0) { // for each character in arg
if (arg[argNdex] == '\"') inquotes = !inquotes; if (arg[argNdex] == '\"')
inquotes = !inquotes;
if (!inquotes && isSpace(arg[argNdex])) { // if the character is white space if (!inquotes && isSpace(arg[argNdex])) { // if the character is white space
if (tokenNdex!=0) { // close off currentToken token if (tokenNdex!=0) { // close off currentToken token
currentToken[tokenNdex] = 0; currentToken[tokenNdex] = 0;
argv.push_back(currentToken); argv.AddItem(currentToken);
currentToken += tokenNdex+1; currentToken += tokenNdex+1;
tokenNdex=0; tokenNdex=0;
argNdex++; argNdex++;
@@ -257,18 +373,20 @@ status_t Hey(BMessenger* target, const char* arg, BMessage* reply)
if (tokenNdex!=0) { // close off currentToken token if (tokenNdex!=0) { // close off currentToken token
currentToken[tokenNdex] = 0; currentToken[tokenNdex] = 0;
argv.push_back(currentToken); argv.AddItem(currentToken);
} }
argv.push_back(NULL); argv.AddItem(NULL);
int32 argx = 0; int32 argx = 0;
status_t ret = Hey(target, argv.begin(), &argx, argv.size()-1, reply); status_t ret = Hey(target, (char **)argv.Items(), &argx, argv.CountItems()-1, reply);
// This used to be "return Hey(...);"---so tokens wasn't delete'd. -- pfolk@uni.uiuc.edu 1999-11-03 // This used to be "return Hey(...);"---so tokens wasn't delete'd. -- pfolk@uni.uiuc.edu 1999-11-03
delete tokens; delete[] tokens;
return ret; return ret;
} }
bool isSpace(char c)
bool
isSpace(char c)
{ {
switch (c) { switch (c) {
case ' ': case ' ':
@@ -280,7 +398,9 @@ bool isSpace(char c)
} }
} }
status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage* reply)
status_t
Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage* reply)
{ {
bool direct_what = false; bool direct_what = false;
BMessage the_message; BMessage the_message;
@@ -290,24 +410,29 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage
// parse the specifiers // parse the specifiers
(*argx)++; (*argx)++;
status_t result=B_OK; status_t result=B_OK;
while((result=add_specifier(&get_target, argv, argx, argc))==B_OK){}; while ((result = add_specifier(&get_target, argv, argx, argc))==B_OK)
;
if (result!=B_ERROR){ // bad syntax if (result!=B_ERROR){ // bad syntax
if(!silent) fprintf(stderr, "Bad specifier syntax!\n"); if (!silent)
fprintf(stderr, "Bad specifier syntax!\n");
return result; return result;
} }
BMessage msgr; BMessage msgr;
if (target && target->IsValid()) { if (target && target->IsValid()) {
result = target->SendMessage(&get_target, &msgr); result = target->SendMessage(&get_target, &msgr);
if (result!=B_OK) return result; if (result!=B_OK)
return result;
result = msgr.FindMessenger ("result", target); result = msgr.FindMessenger ("result", target);
if (result!=B_OK) { if (result!=B_OK) {
if (!silent) fprintf(stderr, "Couldn't retrieve the BMessenger!\n"); if (!silent)
fprintf(stderr, "Couldn't retrieve the BMessenger!\n");
return result; return result;
} }
} }
if (!argv[*argx]) { if (!argv[*argx]) {
if (!silent) fprintf(stderr, "Syntax error - forgot \"do\"?\n"); if (!silent)
fprintf(stderr, "Syntax error - forgot \"do\"?\n");
return B_ERROR; return B_ERROR;
} }
} }
@@ -350,8 +475,7 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage
bool found=false; bool found=false;
if (target && target->IsValid()) { if (target && target->IsValid()) {
BMessage rply; BMessage rply;
BMessage req(B_GET_SUPPORTED_SUITES); if(target->SendMessage(B_GET_SUPPORTED_SUITES, &rply)==B_OK){
if(target->SendMessage(&req, &rply)==B_OK){
// if all goes well, rply contains all kinds of property infos // if all goes well, rply contains all kinds of property infos
int32 j=0; int32 j=0;
void *voidptr; void *voidptr;
@@ -397,7 +521,8 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage
if(!found){ if(!found){
if(!silent) fprintf(stderr, "Bad verb (\"%s\")\n", argv[*argx]); if(!silent)
fprintf(stderr, "Bad verb (\"%s\")\n", argv[*argx]);
return -1; return -1;
} }
} }
@@ -410,14 +535,15 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage
// One exception: Single data item at end of line. // One exception: Single data item at end of line.
if (direct_what && *argx == argc - 1 && argv[*argx] != NULL) { if (direct_what && *argx == argc - 1 && argv[*argx] != NULL) {
add_data(&the_message, argv, argx); add_data(&the_message, argv, argx);
} } else {
else {
// parse the specifiers // parse the specifiers
if (the_message.what!=B_REFS_RECEIVED){ // LOAD has no specifier if (the_message.what!=B_REFS_RECEIVED){ // LOAD has no specifier
while((result=add_specifier(&the_message, argv, argx, argc))==B_OK){}; while ((result=add_specifier(&the_message, argv, argx, argc))==B_OK)
;
if (result!=B_ERROR){ // bad syntax if (result!=B_ERROR){ // bad syntax
if(!silent) fprintf(stderr, "Bad specifier syntax!\n"); if (!silent)
fprintf(stderr, "Bad specifier syntax!\n");
return result; return result;
} }
} }
@@ -431,9 +557,11 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage
result = add_data(&the_message, argv, argx); result = add_data(&the_message, argv, argx);
if (result!=B_OK) { if (result!=B_OK) {
if (result==B_FILE_NOT_FOUND){ if (result==B_FILE_NOT_FOUND){
if(!silent) fprintf(stderr, "File not found!\n"); if (!silent)
fprintf(stderr, "File not found!\n");
} else { } else {
if(!silent) fprintf(stderr, "Invalid 'to...' value format!\n"); if (!silent)
fprintf(stderr, "Invalid 'to...' value format!\n");
} }
return result; return result;
} }
@@ -459,7 +587,8 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage
// There can be a with <name>=<type>() [and <name>=<type> ...] // There can be a with <name>=<type>() [and <name>=<type> ...]
// I treat "and" just the same as "with", it's just to make the script syntax more English-like. // I treat "and" just the same as "with", it's just to make the script syntax more English-like.
status_t add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc) status_t
add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc)
{ {
status_t result = B_OK; status_t result = B_OK;
if (*argx < argc - 1 && argv[++(*argx)]!=NULL){ if (*argx < argc - 1 && argv[++(*argx)]!=NULL){
@@ -468,24 +597,23 @@ status_t add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc)
// printf ("\"with\" detected!\n"); // printf ("\"with\" detected!\n");
(*argx)++; (*argx)++;
bool done = false; bool done = false;
do do {
{
result=add_data(to_message, argv, argx); result=add_data(to_message, argv, argx);
if (result!=B_OK){ if (result!=B_OK){
if (result==B_FILE_NOT_FOUND){ if (result==B_FILE_NOT_FOUND){
if(!silent) fprintf(stderr, "File not found!\n"); if (!silent)
fprintf(stderr, "File not found!\n");
} else { } else {
if(!silent) fprintf(stderr, "Invalid 'with...' value format!\n"); if (!silent)
fprintf(stderr, "Invalid 'with...' value format!\n");
} }
return result; return result;
} }
(*argx)++; (*argx)++;
// printf ("argc = %d, argv[%d] = %s\n", argc, *argx, argv[*argx]); // printf ("argc = %d, argv[%d] = %s\n", argc, *argx, argv[*argx]);
if (*argx < argc - 1 && strcasecmp(argv[*argx], "and")==0) if (*argx < argc - 1 && strcasecmp(argv[*argx], "and")==0) {
{
(*argx)++; (*argx)++;
} } else
else
done = true; done = true;
} while (!done); } while (!done);
} }
@@ -496,12 +624,13 @@ status_t add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc)
// returns B_OK if successful // returns B_OK if successful
// B_ERROR if no more specifiers // B_ERROR if no more specifiers
// B_BAD_SCRIPT_SYNTAX if syntax error // B_BAD_SCRIPT_SYNTAX if syntax error
status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 argc) status_t
add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 argc)
{ {
char *property=argv[*argx]; char *property=argv[*argx];
if(property==NULL) return B_ERROR; // no more specifiers if (property==NULL)
return B_ERROR; // no more specifiers
(*argx)++; (*argx)++;
@@ -521,13 +650,15 @@ status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 ar
if (strcasecmp(property, "of")==0){ // skip "of", read real property if (strcasecmp(property, "of")==0){ // skip "of", read real property
property = argv[*argx]; property = argv[*argx];
if(property==NULL) return B_BAD_SCRIPT_SYNTAX; // bad syntax if (property==NULL)
return B_BAD_SCRIPT_SYNTAX; // bad syntax
(*argx)++; (*argx)++;
} }
if (strcasecmp(property, "the")==0){ // skip "the", read real property -- pfolk@uni.uiuc.edu 1999-11-03 if (strcasecmp(property, "the")==0){ // skip "the", read real property -- pfolk@uni.uiuc.edu 1999-11-03
property = argv[*argx]; property = argv[*argx];
if(property==NULL) return B_BAD_SCRIPT_SYNTAX; // bad syntax if (property==NULL)
return B_BAD_SCRIPT_SYNTAX; // bad syntax
(*argx)++; (*argx)++;
} }
@@ -606,20 +737,19 @@ status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 ar
} }
if (index_spec){ if (index_spec){
if (reverse) if (reverse) {
{
// Copied from above -- pfolk@uni.uiuc.edu 1999-11-03 // Copied from above -- pfolk@uni.uiuc.edu 1999-11-03
BMessage revspec(B_REVERSE_INDEX_SPECIFIER); BMessage revspec(B_REVERSE_INDEX_SPECIFIER);
revspec.AddString("property", property); revspec.AddString("property", property);
revspec.AddInt32("index", atol(specifier+1)); revspec.AddInt32("index", atol(specifier+1));
to_message->AddSpecifier(&revspec); to_message->AddSpecifier(&revspec);
} }
else to_message->AddSpecifier(property, atol(specifier)); else
to_message->AddSpecifier(property, atol(specifier));
} else { } else {
// Allow any name by counting an initial " as a literal-string indicator // Allow any name by counting an initial " as a literal-string indicator
// -- pfolk@uni.uiuc.edu 1999-11-03 // -- pfolk@uni.uiuc.edu 1999-11-03
if(specifier[0]=='\"') if (specifier[0]=='\"') {
{
if (specifier[speclen-1]=='\"') if (specifier[speclen-1]=='\"')
specifier[speclen-1]='\0'; specifier[speclen-1]='\0';
++specifier; ++specifier;
@@ -627,18 +757,19 @@ status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 ar
} }
to_message->AddSpecifier(property, specifier); to_message->AddSpecifier(property, specifier);
} }
} }
return B_OK; return B_OK;
} }
status_t add_data(BMessage *to_message, char *argv[], int32 *argx) status_t
add_data(BMessage *to_message, char *argv[], int32 *argx)
{ {
char *valuestring=argv[*argx]; char *valuestring=argv[*argx];
if(valuestring==NULL) return B_ERROR; if (valuestring==NULL)
return B_ERROR;
// try to interpret it as an integer or float // try to interpret it as an integer or float
bool contains_only_digits = true; bool contains_only_digits = true;
@@ -686,8 +817,7 @@ status_t add_data(BMessage *to_message, char *argv[], int32 *argx)
while (*++s && *s != '=') while (*++s && *s != '=')
// Look for a '=' character... // Look for a '=' character...
; ;
if (*s == '=') // We found a <name>= if (*s == '=') { // We found a <name>=
{
*s = 0; *s = 0;
strcpy (curname, valuestring); // Use the new <name> strcpy (curname, valuestring); // Use the new <name>
valuestring = s + 1; // Reposition the valuestring ptr. valuestring = s + 1; // Reposition the valuestring ptr.
@@ -807,15 +937,15 @@ status_t add_data(BMessage *to_message, char *argv[], int32 *argx)
} }
void
void print_message(BMessage *message) print_message(BMessage *message)
{ {
BList textlist; BList textlist;
add_message_contents(&textlist, message, 0); add_message_contents(&textlist, message, 0);
char *whatString = get_datatype_string(message->what);
printf("BMessage(%s):\n", get_datatype_string(message->what)); printf("BMessage(%s):\n", whatString);
free(whatString);
for (int32 i=0;i<textlist.CountItems();i++){ for (int32 i=0;i<textlist.CountItems();i++){
printf(" %s\n", (char*)textlist.ItemAt(i)); printf(" %s\n", (char*)textlist.ItemAt(i));
free(textlist.ItemAt(i)); free(textlist.ItemAt(i));
@@ -824,18 +954,21 @@ void print_message(BMessage *message)
} }
void
void add_message_contents(BList *textlist, BMessage *msg, int32 level) add_message_contents(BList *textlist, BMessage *msg, int32 level)
{ {
int32 count; int32 count;
int32 i, sizefound, j; int32 i, sizefound, j;
ulong typefound; ulong typefound;
#ifdef HAIKU_TARGET_PLATFORM_DANO
const char *namefound;
#else
char *namefound; char *namefound;
#endif
void *voidptr; void *voidptr;
BMessage a_message; BMessage a_message;
char *textline, *datatype, *content; char *textline, *datatype, *content;
// go though all message data // go though all message data
count = msg->CountNames(B_ANY_TYPE); count = msg->CountNames(B_ANY_TYPE);
for (i=0; i<count; i++){ for (i=0; i<count; i++){
@@ -855,21 +988,18 @@ void add_message_contents(BList *textlist, BMessage *msg, int32 level)
if (typefound==B_MESSAGE_TYPE){ if (typefound==B_MESSAGE_TYPE){
msg->FindMessage(namefound, j-1, &a_message); msg->FindMessage(namefound, j-1, &a_message);
add_message_contents(textlist, &a_message, level+1); add_message_contents(textlist, &a_message, level+1);
}else } else if (typefound==B_RAW_TYPE && strcmp(namefound, "_previous_")==0){
if(typefound==B_RAW_TYPE && strcmp(namefound, "_previous_")==0){
if (a_message.Unflatten((const char *)voidptr)==B_OK){ if (a_message.Unflatten((const char *)voidptr)==B_OK){
add_message_contents(textlist, &a_message, level+1); add_message_contents(textlist, &a_message, level+1);
} }
} }
} }
} }
} }
char *
char *get_datatype_string(int32 type) get_datatype_string(int32 type)
{ {
char *str = new char[128]; char *str = new char[128];
@@ -988,14 +1118,14 @@ char *get_datatype_string(int32 type)
} }
char *format_data(int32 type, char *ptr, long size) char *
format_data(int32 type, char *ptr, long size)
{ {
char idtext[32]; char idtext[32];
char *str; char *str;
float *fptr; float *fptr;
double *dptr; double *dptr;
// BRect *brptr; // BRect *brptr;
long i;
entry_ref aref; entry_ref aref;
BEntry entry; BEntry entry;
BPath path; BPath path;
@@ -1008,14 +1138,8 @@ char *format_data(int32 type, char *ptr, long size)
uint16 ui16; uint16 ui16;
uint8 ui8; uint8 ui8;
BMessage anothermsg; BMessage anothermsg;
BPropertyInfo propinfo;
const property_info *pinfo;
int32 pinfo_index;
const value_info *vinfo;
int32 vinfo_index, vinfo_count;
char *tempstr; char *tempstr;
if (size<=0L){ if (size<=0L){
str = new char; str = new char;
*str = 0; *str = 0;
@@ -1026,7 +1150,8 @@ char *format_data(int32 type, char *ptr, long size)
case B_MIME_TYPE: case B_MIME_TYPE:
case B_ASCII_TYPE: case B_ASCII_TYPE:
case B_STRING_TYPE: case B_STRING_TYPE:
if(size>512) size=512; if (size>512)
size=512;
str = new char[size+4]; str = new char[size+4];
*str='\"'; *str='\"';
strncpy(str+1, ptr, size); strncpy(str+1, ptr, size);
@@ -1141,7 +1266,7 @@ char *format_data(int32 type, char *ptr, long size)
case B_COLOR_8_BIT_TYPE: case B_COLOR_8_BIT_TYPE:
str = new char[size*6+4]; str = new char[size*6+4];
*str = 0; *str = 0;
for(i=0;i<min_c(256,size);i++){ for (int32 i=0; i<min_c(256,size); i++){
sprintf(idtext, "%u ", ((unsigned char*)ptr)[i]); sprintf(idtext, "%u ", ((unsigned char*)ptr)[i]);
strcat(str,idtext); strcat(str,idtext);
} }
@@ -1151,26 +1276,26 @@ char *format_data(int32 type, char *ptr, long size)
case B_MESSAGE_TYPE: case B_MESSAGE_TYPE:
str = new char[64]; str = new char[64];
if (anothermsg.Unflatten((const char *)ptr)==B_OK){ if (anothermsg.Unflatten((const char *)ptr)==B_OK){
sprintf(str, "what=%s", get_datatype_string(anothermsg.what)); char *whatString = get_datatype_string(anothermsg.what);
sprintf(str, "what=%s", whatString);
free(whatString);
} else { } else {
strcpy(str, "error when unflattening"); strcpy(str, "error when unflattening");
} }
break; break;
case B_PROPERTY_INFO_TYPE: case B_PROPERTY_INFO_TYPE: {
BPropertyInfo propinfo;
if (propinfo.Unflatten(B_PROPERTY_INFO_TYPE, (const void *)ptr, size)==B_OK){ if (propinfo.Unflatten(B_PROPERTY_INFO_TYPE, (const void *)ptr, size)==B_OK){
str = new char[size*32]; // an approximation str = new char[size*32]; // an approximation
//propinfo.PrintToStream(); //propinfo.PrintToStream();
//sprintf(str, "see the printout above"); //sprintf(str, "see the printout above");
pinfo=propinfo.Properties(); const property_info *pinfo = propinfo.Properties();
pinfo_index=0;
sprintf(str, "\n property commands specifiers\n--------------------------------------------------------------------------------\n");
while(pinfo_index<propinfo.CountProperties()){
sprintf(str, "\n property commands specifiers types\n---------------------------------------------------------------------------------------------------\n");
for (int32 pinfo_index = 0; pinfo_index<propinfo.CountProperties(); pinfo_index++) {
strcat(str, " "+(strlen(pinfo[pinfo_index].name) <16 ? strlen(pinfo[pinfo_index].name) : 16 )); strcat(str, " "+(strlen(pinfo[pinfo_index].name) <16 ? strlen(pinfo[pinfo_index].name) : 16 ));
strcat(str, pinfo[pinfo_index].name); strcat(str, pinfo[pinfo_index].name);
strcat(str, " "); strcat(str, " ");
@@ -1204,6 +1329,38 @@ char *format_data(int32 type, char *ptr, long size)
default: strcat(str, "<NONE> "); break; default: strcat(str, "<NONE> "); break;
} }
} }
// pad the rest with spaces
if (strlen(start)<60){
strcat(str, " "+strlen(start) );
} else {
strcat(str, " " );
}
for (int32 i = 0; i < 10 && pinfo[pinfo_index].types[i] != 0; i++) {
uint32 type = pinfo[pinfo_index].types[i];
char str2[6];
snprintf(str2, sizeof(str2), "%c%c%c%c ",
int(type & 0xFF000000) >> 24,
int(type & 0xFF0000) >> 16,
int(type & 0xFF00) >> 8,
(int)type & 0xFF);
strcat(str, str2);
}
for (int32 i = 0; i < 3; i++) {
for (int32 j = 0; j < 5 && pinfo[pinfo_index].ctypes[i].pairs[j].type != 0; j++) {
uint32 type = pinfo[pinfo_index].ctypes[i].pairs[j].type;
char str2[strlen(pinfo[pinfo_index].ctypes[i].pairs[j].name) + 8];
snprintf(str2, sizeof(str2),
"(%s %c%c%c%c)",
pinfo[pinfo_index].ctypes[i].pairs[j].name,
int(type & 0xFF000000) >> 24,
int(type & 0xFF0000) >> 16,
int(type & 0xFF00) >> 8,
(int)type & 0xFF);
strcat(str, str2);
}
}
strcat(str, "\n"); strcat(str, "\n");
// is there usage info? // is there usage info?
@@ -1213,15 +1370,12 @@ char *format_data(int32 type, char *ptr, long size)
strcat(str, "\n"); strcat(str, "\n");
} }
pinfo_index++; // take next propertyinfo
} }
// handle value infos.... // handle value infos....
vinfo=propinfo.Values(); const value_info *vinfo = propinfo.Values();
vinfo_index=0; int32 vinfo_count = propinfo.CountValues();
vinfo_count=propinfo.CountValues();
#if TEST_VALUEINFO>0 #if TEST_VALUEINFO>0
value_info vinfo[10] = { {"Backup", 'back', B_COMMAND_KIND, "This command backs up your hard drive."}, value_info vinfo[10] = { {"Backup", 'back', B_COMMAND_KIND, "This command backs up your hard drive."},
{"Abort", 'abor', B_COMMAND_KIND, "Stops the current operation..."}, {"Abort", 'abor', B_COMMAND_KIND, "Stops the current operation..."},
@@ -1233,7 +1387,7 @@ char *format_data(int32 type, char *ptr, long size)
if (vinfo && vinfo_count>0){ if (vinfo && vinfo_count>0){
sprintf(str+strlen(str), "\n name value kind\n--------------------------------------------------------------------------------\n"); sprintf(str+strlen(str), "\n name value kind\n--------------------------------------------------------------------------------\n");
while(vinfo_index<vinfo_count){ for (int32 vinfo_index = 0; vinfo_index<vinfo_count; vinfo_index++){
char *start = str+strlen(str); char *start = str+strlen(str);
strcat(str, " "+(strlen(vinfo[vinfo_index].name) <16 ? strlen(vinfo[vinfo_index].name) : 16 )); strcat(str, " "+(strlen(vinfo[vinfo_index].name) <16 ? strlen(vinfo[vinfo_index].name) : 16 ));
@@ -1257,7 +1411,6 @@ char *format_data(int32 type, char *ptr, long size)
default: strcat(str, "unknown "); break; default: strcat(str, "unknown "); break;
} }
strcat(str, "\n"); strcat(str, "\n");
// is there usage info? // is there usage info?
@@ -1266,8 +1419,6 @@ char *format_data(int32 type, char *ptr, long size)
strcat(str, vinfo[vinfo_index].usage); strcat(str, vinfo[vinfo_index].usage);
strcat(str, "\n"); strcat(str, "\n");
} }
vinfo_index++; // take next valueinfo
} }
} }
@@ -1276,11 +1427,12 @@ char *format_data(int32 type, char *ptr, long size)
strcpy(str, "error when unflattening"); strcpy(str, "error when unflattening");
} }
break; break;
}
default: default:
str = new char[min_c(256,size)*20+4]; str = new char[min_c(256,size)*20+4];
*str = 0; *str = 0;
for(i=0;i<min_c(256,size);i++){ for (int32 i=0; i<min_c(256,size); i++){
//sprintf(idtext, "0x%02X ('%c'), ", (uint16)ptr[i], ptr[i]<32 ? 32 : ptr[i]); //sprintf(idtext, "0x%02X ('%c'), ", (uint16)ptr[i], ptr[i]<32 ? 32 : ptr[i]);
sprintf(idtext, "0x%02X, ", (uint16)ptr[i] ); sprintf(idtext, "0x%02X, ", (uint16)ptr[i] );
strcat(str,idtext); strcat(str,idtext);
@@ -1294,8 +1446,8 @@ char *format_data(int32 type, char *ptr, long size)
} }
char *
char *id_to_string(long ID, char *here) id_to_string(long ID, char *here)
{ {
uint8 digit0 = (ID>>24)& 255; uint8 digit0 = (ID>>24)& 255;
uint8 digit1 = (ID>>16)& 255; uint8 digit1 = (ID>>16)& 255;
@@ -1307,21 +1459,25 @@ char *id_to_string(long ID, char *here)
if (digit1==0){ if (digit1==0){
if (digit2==0) { if (digit2==0) {
// 1 digits // 1 digits
if(is_valid_char(digit3) ) itsvalid=TRUE; if (is_valid_char(digit3) )
itsvalid=TRUE;
sprintf(here, "'%c'", digit3); sprintf(here, "'%c'", digit3);
} else { } else {
// 2 digits // 2 digits
if(is_valid_char(digit2) && is_valid_char(digit3) ) itsvalid=TRUE; if (is_valid_char(digit2) && is_valid_char(digit3) )
itsvalid=TRUE;
sprintf(here, "'%c%c'", digit2, digit3); sprintf(here, "'%c%c'", digit2, digit3);
} }
} else { } else {
// 3 digits // 3 digits
if(is_valid_char(digit1) && is_valid_char(digit2) && is_valid_char(digit3) ) itsvalid=TRUE; if (is_valid_char(digit1) && is_valid_char(digit2) && is_valid_char(digit3) )
itsvalid=TRUE;
sprintf(here, "'%c%c%c'", digit1, digit2, digit3); sprintf(here, "'%c%c%c'", digit1, digit2, digit3);
} }
} else { } else {
// 4 digits // 4 digits
if(is_valid_char(digit0) && is_valid_char(digit1) && is_valid_char(digit2) && is_valid_char(digit3) ) itsvalid=TRUE; if (is_valid_char(digit0) && is_valid_char(digit1) && is_valid_char(digit2) && is_valid_char(digit3) )
itsvalid=TRUE;
sprintf(here, "'%c%c%c%c'", digit0, digit1, digit2, digit3); sprintf(here, "'%c%c%c%c'", digit0, digit1, digit2, digit3);
} }
@@ -1333,7 +1489,8 @@ char *id_to_string(long ID, char *here)
} }
bool is_valid_char(uint8 c) bool
is_valid_char(uint8 c)
{ {
return (c>=32 && c<128); return (c>=32 && c<128);
} }