Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
19 June, 2013, 11:18:27 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: Make functions of one program available for other programs? -  (Read 1069 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
compu
LV5 Advanced (Next: 300)
*****
Offline Offline

Gender: Male
Last Login: Today at 09:52:12
Date Registered: 09 January, 2011, 22:45:52
Location: Germany
Posts: 229

Topic starter
Total Post Ratings: +53

View Profile
« on: 22 August, 2011, 21:56:41 »
0

When I launch a program via my shell, is it possible to make functions and variables of it available for the launched program?
Logged

Nspire I/O Discussion|Website - cross-compatible with Prizm!
TI-Nspire Programming Statistics
ExtendeD
Coder Of Tomorrow
LV8 Addict (Next: 1000)
*
Offline Offline

Gender: Male
Last Login: Today at 07:59:35
Date Registered: 02 January, 2010, 13:03:41
Location: France
Posts: 773

Total Post Ratings: +152

View Profile
« Reply #1 on: 22 August, 2011, 22:25:03 »
0

Sharing functions dynamically would require dynamic libraries, which is not supported and not really planned in Ndless. But these functions can also be exported as a static library, bound with the program launched at compile time (see how libndls is built and used or bwang's C library somewhere on Omnimaga).

Sharing variables would require a Shared memory, I don't know if the TI-Nspire includes anything like this. A dirty workaround would be to pass the address of a memory block allocated by the shell to the programs launched as a command line parameter, and access the variables relatively to this base address. This could also be used to call back dynamically functions from the shell by the way.
Logged

ndlessly - Progress and insights on Ndless
Ndless / Hackspire - Third-party TI-Nspire development
compu
LV5 Advanced (Next: 300)
*****
Offline Offline

Gender: Male
Last Login: Today at 09:52:12
Date Registered: 09 January, 2011, 22:45:52
Location: Germany
Posts: 229

Topic starter
Total Post Ratings: +53

View Profile
« Reply #2 on: 24 August, 2011, 20:42:06 »
0

I've tried it on this way now:

I define a structre called _rshellAPI (in a header file that is included in rshell and the launched program) which looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
typedef struct _rshellAPI
{
void (*c_draw)(void);
void (*c_clear)(void);
void (*c_writec)(char ch);
void (*c_write)(char* str);
void (*c_swrite)(char* format, int buflen, ...);

char (*cn_readc)(void);
char (*c_readc)(void);

int (*c_read)(char* str);
} rshellAPI;

In rshell's source, I initialize this structure with functions of Nspire I/O:

1
2
3
4
5
6
7
8
9
10
11
rshellAPI rshell =
{
&c_draw,
&c_clear,
&c_writec,
&c_write,
&c_swrite,
&cn_readc,
&c_readc,
&c_read
};

And then pass it to the launched program:

1
((void (*)(int, char **, rshellAPI*))(docptr + sizeof(PRGMSIG)))(n_argc, n_argv, &rshell);

Then I've changed the launched program to use these functions:


1
int main(int argc, char** argv, rshellAPI* rshell)


1
rshell->c_writec('\n');


All this compiles without errors, but when the launched program tries to call one of the functions, this happens:

1
2
3
4
5
Error at PC=11140B6C: Unaligned read_word: 11
        Backtrace:
Frame     PrvFrame Self     Return   Start
11136665: 41000000 3A41003A 003E7325 000A0D20
41000000: invalid address

What is wrong with this?
« Last Edit: 24 August, 2011, 20:59:22 by compu » Logged

Nspire I/O Discussion|Website - cross-compatible with Prizm!
TI-Nspire Programming Statistics
ExtendeD
Coder Of Tomorrow
LV8 Addict (Next: 1000)
*
Offline Offline

Gender: Male
Last Login: Today at 07:59:35
Date Registered: 02 January, 2010, 13:03:41
Location: France
Posts: 773

Total Post Ratings: +152

View Profile
« Reply #3 on: 24 August, 2011, 22:46:55 »
0

Does it crash before the call or within the function called?
Logged

ndlessly - Progress and insights on Ndless
Ndless / Hackspire - Third-party TI-Nspire development
bsl
LV4 Regular (Next: 200)
****
Offline Offline

Last Login: 22 October, 2012, 03:42:44
Date Registered: 23 February, 2010, 18:14:18
Posts: 148

Total Post Ratings: +11

View Profile
« Reply #4 on: 25 August, 2011, 01:16:29 »
0

Try isolating the problem, see if what you passed is getting there.
They should match.

from the launcner:
printf("n_argc=%i, n_argc=%p, &rshell=%X\n",n_argc, n_argv, &rshell);
((void (*)(int, char **, rshellAPI*))(docptr + sizeof(PRGMSIG)))(n_argc, n_argv, &rshell);


from the launcned:
int main(int argc, char** argv, rshellAPI* rshell){
 printf(argc=%i, argv=%p, rshell=%p\n",argc, argv, rshell);
.
.
.
}

Alignment errors: The adressess in argv, rshell should end in: 0,4,8,C


Declaring with "static" might solve the alignment problem:
static rshellAPI rshell =
{
   &c_draw,
   &c_clear,
   &c_writec,
   &c_write,
   &c_swrite,
   &cn_readc,
   &c_readc,
   &c_read
};
Logged
calc84maniac
Epic z80 roflpwner
Coder Of Tomorrow
LV11 Super Veteran (Next: 3000)
*
Offline Offline

Gender: Male
Last Login: Today at 07:09:04
Date Registered: 28 August, 2008, 05:09:05
Location: Right behind you.
Posts: 2737


Total Post Ratings: +376

View Profile
« Reply #5 on: 25 August, 2011, 01:29:57 »
0

Do function pointers work properly in the Ndless toolchain? I remember they didn't at some point. What are the GCC options in your makefile?
Logged

"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman
bsl
LV4 Regular (Next: 200)
****
Offline Offline

Last Login: 22 October, 2012, 03:42:44
Date Registered: 23 February, 2010, 18:14:18
Posts: 148

Total Post Ratings: +11

View Profile
« Reply #6 on: 25 August, 2011, 01:40:41 »
0

Yes, they do.
Look at  ploaderhook.c
Logged
ExtendeD
Coder Of Tomorrow
LV8 Addict (Next: 1000)
*
Offline Offline

Gender: Male
Last Login: Today at 07:59:35
Date Registered: 02 January, 2010, 13:03:41
Location: France
Posts: 773

Total Post Ratings: +152

View Profile
« Reply #7 on: 25 August, 2011, 08:24:41 »
0

Do function pointers work properly in the Ndless toolchain? I remember they didn't at some point. What are the GCC options in your makefile?

Wait, you're right, I forgot that one: http://hackspire.unsads.com/wiki/index.php/Ndless_features_and_limitations#Global_variables
You should use nl_relocdata() on your functions array.
Logged

ndlessly - Progress and insights on Ndless
Ndless / Hackspire - Third-party TI-Nspire development
compu
LV5 Advanced (Next: 300)
*****
Offline Offline

Gender: Male
Last Login: Today at 09:52:12
Date Registered: 09 January, 2011, 22:45:52
Location: Germany
Posts: 229

Topic starter
Total Post Ratings: +53

View Profile
« Reply #8 on: 25 August, 2011, 13:33:30 »
0

First of all, thanks for your help.
Currently I'm not at home, but how do I have to call nl_relocdata() with a structure?
What is the first argument (nl_relocdata_data on Hackspire)?
Logged

Nspire I/O Discussion|Website - cross-compatible with Prizm!
TI-Nspire Programming Statistics
compu
LV5 Advanced (Next: 300)
*****
Offline Offline

Gender: Male
Last Login: Today at 09:52:12
Date Registered: 09 January, 2011, 22:45:52
Location: Germany
Posts: 229

Topic starter
Total Post Ratings: +53

View Profile
« Reply #9 on: 30 August, 2011, 19:25:33 »
0

BUMP


1
n_argc=1, n_argv=1800e070, &rshell=111B0400

1
argc=1, argv=1800e070, rshell=00000001
Logged

Nspire I/O Discussion|Website - cross-compatible with Prizm!
TI-Nspire Programming Statistics
bsl
LV4 Regular (Next: 200)
****
Offline Offline

Last Login: 22 October, 2012, 03:42:44
Date Registered: 23 February, 2010, 18:14:18
Posts: 148

Total Post Ratings: +11

View Profile
« Reply #10 on: 31 August, 2011, 02:20:03 »
0

Try this:
Instead of passing a third argument, just use the last n_argv[19]
to pass the rshell address.
Launcher

1
2
n_argv[19] = (char*)&rshell; // put pointer in last array slot
Launched

1
2
3
4
5
6
7
8

int main(int argc, char** argv){
 rshellAPI *rshell;
  rshell = (rshellAPI *)argv[19];
.
.
.
}

Logged
compu
LV5 Advanced (Next: 300)
*****
Offline Offline

Gender: Male
Last Login: Today at 09:52:12
Date Registered: 09 January, 2011, 22:45:52
Location: Germany
Posts: 229

Topic starter
Total Post Ratings: +53

View Profile
« Reply #11 on: 31 August, 2011, 08:04:12 »
0

Doesn't work, but at least the address of rshell in the launched program isn't 00000001 anymore  Undecided

I got 2 different errors and one time it just restarted.


1
2
Launcher: 1800e93c
Launched: 111793a8
Logged

Nspire I/O Discussion|Website - cross-compatible with Prizm!
TI-Nspire Programming Statistics
ExtendeD
Coder Of Tomorrow
LV8 Addict (Next: 1000)
*
Offline Offline

Gender: Male
Last Login: Today at 07:59:35
Date Registered: 02 January, 2010, 13:03:41
Location: France
Posts: 773

Total Post Ratings: +152

View Profile
« Reply #12 on: 12 September, 2011, 19:59:45 »
0

First of all, thanks for your help.
Currently I'm not at home, but how do I have to call nl_relocdata() with a structure?
What is the first argument (nl_relocdata_data on Hackspire)?

There was a typo in the sample on Hackspire, it's fixed: http://hackspire.unsads.com/wiki/index.php/Ndless_features_and_limitations#Global_variables
The first argument would be rshell in your case.
Logged

ndlessly - Progress and insights on Ndless
Ndless / Hackspire - Third-party TI-Nspire development
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 2.112 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.