Welcome, Guest. Please login or register.

Author Topic: Coding: CLI args parsing and resident purity  (Read 1807 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline nyteschaydeTopic starter

  • VIP / Donor - Lifetime Member
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 643
    • Show only replies by nyteschayde
    • http://www.nyteshade.com
Coding: CLI args parsing and resident purity
« 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.
Senior MTS Software Engineer with PayPal
Amigas: A1200T 060/603e PPC • A1200T 060 • A4000D 040 • A3000 (x2) • A2000 Vamp/V2 • A1200 (x4) • A1000 (x3) • A600 Vamp/V1 • A500
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: Coding: CLI args parsing and resident purity
« Reply #1 on: January 11, 2012, 12:50:08 AM »
int p; // A
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Coding: CLI args parsing and resident purity
« Reply #2 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.
 

Offline nyteschaydeTopic starter

  • VIP / Donor - Lifetime Member
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 643
    • Show only replies by nyteschayde
    • http://www.nyteshade.com
Re: Coding: CLI args parsing and resident purity
« Reply #3 on: January 11, 2012, 12:56:21 AM »
Thanks, you're both awesome!
Senior MTS Software Engineer with PayPal
Amigas: A1200T 060/603e PPC • A1200T 060 • A4000D 040 • A3000 (x2) • A2000 Vamp/V2 • A1200 (x4) • A1000 (x3) • A600 Vamp/V1 • A500
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: Coding: CLI args parsing and resident purity
« Reply #4 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.
int p; // A
 

Offline Leffmann

  • Full Member
  • ***
  • Join Date: Feb 2011
  • Posts: 119
    • Show only replies by Leffmann
Re: Coding: CLI args parsing and resident purity
« Reply #5 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.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Coding: CLI args parsing and resident purity
« Reply #6 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...
 

Offline nyteschaydeTopic starter

  • VIP / Donor - Lifetime Member
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 643
    • Show only replies by nyteschayde
    • http://www.nyteshade.com
Re: Coding: CLI args parsing and resident purity
« Reply #7 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.
Senior MTS Software Engineer with PayPal
Amigas: A1200T 060/603e PPC • A1200T 060 • A4000D 040 • A3000 (x2) • A2000 Vamp/V2 • A1200 (x4) • A1000 (x3) • A600 Vamp/V1 • A500
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Coding: CLI args parsing and resident purity
« Reply #8 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.
« Last Edit: January 11, 2012, 08:49:47 PM by Piru »
 

Offline Thomas

Re: Coding: CLI args parsing and resident purity
« Reply #9 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.

Offline nyteschaydeTopic starter

  • VIP / Donor - Lifetime Member
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 643
    • Show only replies by nyteschayde
    • http://www.nyteshade.com
Re: Coding: CLI args parsing and resident purity
« Reply #10 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.
Senior MTS Software Engineer with PayPal
Amigas: A1200T 060/603e PPC • A1200T 060 • A4000D 040 • A3000 (x2) • A2000 Vamp/V2 • A1200 (x4) • A1000 (x3) • A600 Vamp/V1 • A500