Amiga.org

Amiga computer related discussion => Amiga Software Issues and Discussion => Topic started by: nyteschayde on January 11, 2012, 12:46:54 AM

Title: Coding: CLI args parsing and resident purity
Post by: nyteschayde on January 11, 2012, 12:46:54 AM
Almost all CLI commands in AmigaOS use syntax where you can pass ? and you get prompted with a list of parameters and they're relative required'ness based on an obscure /M /A /K /S type of reference.

I believe they stand for

/A Required argument
/F Final argument in list
/K Keyword must be entered with argument
/M Multiple arguments
/N Number
/S Switch (optional)

Anyhow, I doubt people manually parse these each time. Is there a known API for handling this type of input in Amiga C?

Also, where can I find information on how to make the command resident safe?


Any help would be appreciated.
Title: Re: Coding: CLI args parsing and resident purity
Post by: Karlos on January 11, 2012, 12:50:08 AM
Hi mate,

dos.library/ReadArgs() is the one you need.

http://www.tbs-software.com/guide/index.php?guide=autodocs.doc%2Fdos.doc&node=105
Title: Re: Coding: CLI args parsing and resident purity
Post by: Piru on January 11, 2012, 12:54:22 AM
Quote from: nyteschayde;675265

Also, where can I find information on how to make the command resident safe?

It's easy. Your program must be reentrant:
https://en.wikipedia.org/wiki/Reentrancy_%28computing%29

Basically this means: No global or static variables of any kind.
Title: Re: Coding: CLI args parsing and resident purity
Post by: nyteschayde on January 11, 2012, 12:56:21 AM
Thanks, you're both awesome!
Title: Re: Coding: CLI args parsing and resident purity
Post by: Karlos on January 11, 2012, 01:10:37 AM
Quote from: nyteschayde;675269
Thanks, you're both awesome!


Note carefully what Piru mentioned. Any handles to resources you acquire cannot be global or static. That includes library bases, file streams etc. It's pretty much "on-the-stack" or bust.
Title: Re: Coding: CLI args parsing and resident purity
Post by: Leffmann on January 11, 2012, 01:55:39 AM
You can use global data in your resident programs with a bit of care. F.ex you can use a mutex to enter a critical section where you can retrieve the global data, or initialize it if it's the first run of the program, to save you from having to include the data in your binary. Making this code in turn ROMable is doable too but takes a bit more work.
Title: Re: Coding: CLI args parsing and resident purity
Post by: Piru on January 11, 2012, 02:07:32 AM
Quote from: Leffmann;675285
You can use global data in your resident programs with a bit of care. F.ex you can use a mutex to enter a critical section where you can retrieve the global data
That's entirely possible, although if you need to go to such lengths I'd say it would be better to go "ROMable" right away.

Quote
or initialize it if it's the first run of the program
This however will lead to resource leak and there's no "cleanup" called when the resident command is removed.

I say nuke the site from orbit, err, go for ROMable from the beginning...
Title: Re: Coding: CLI args parsing and resident purity
Post by: nyteschayde on January 11, 2012, 08:38:55 PM
So I tried to compile something that uses ReadArgs. I can successfully get it to prompt me using the template string I pass in but I don't fully understand the second parameter (the LONG* array). I am also not 100% clear on how to check the parsed args afterwards.

Do you guys have any example usages anywhere. Google failed me on this account.
Title: Re: Coding: CLI args parsing and resident purity
Post by: Piru on January 11, 2012, 08:44:53 PM
Quote from: nyteschayde;675397
So I tried to compile something that uses ReadArgs. I can successfully get it to prompt me using the template string I pass in but I don't fully understand the second parameter (the LONG* array). I am also not 100% clear on how to check the parsed args afterwards.

Do you guys have any example usages anywhere. Google failed me on this account.
Here's a simple program that requests a number:
http://www.amiga.org/forums/showthread.php?t=3348
It has a mistake in it that I correct in my reply.

Here's a an example of a more complex argument parsing:
http://www.sinz.org/Michael.Sinz/Enforcer/MMU.c.html
(Just skip the irrelevant parts and look for the ReadArgs usage)

MMU.c already shows how you can read arguments from an arbitrary input buffer instead of stdin. Here's another example of that:
http://www.control.auc.dk/~ksst01/readargs.c

NOTE: Normally you just use NULL as the 3rd argument to read from stdin.
Title: Re: Coding: CLI args parsing and resident purity
Post by: Thomas on January 11, 2012, 09:12:25 PM
Here is an example which concentrates on ReadArgs: http://www.amigans.net/modules/xforum/viewtopic.php?post_id=63398#forumpost63398

It also shows how you can name your arguments instead of using an array of a non-fitting type.
Title: Re: Coding: CLI args parsing and resident purity
Post by: nyteschayde on January 11, 2012, 09:12:27 PM
Wow, ask and you shall receive. You guys rock. I wish I could just pull example source out of my back pocket like that. I really appreciate it.