Template usage ReadArgs

10 posts / 0 new
Last post
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2012-01-08 20:57
Template usage ReadArgs
In my program getsizes i get this CLI response 25.Amiga OS 4:> ram:getsizes mywindow SCREEN Workbench ACTIVE wrong number of arguments usage: WINDOW/A,SCREEN/K, ACTIVE/S Why do i get that error? I would expect the command to be correct
jabirulo
jabirulo's picture
Offline
Last seen: 1 month 1 week ago
Joined: 2013-05-30 00:53
Maybe you have some typo in
Maybe you have some typo in your code ;-) Can you post the ReadArgs() codelines you're using?
AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2012-01-08 20:57
I have this #define
I have this
  1. #define __USE_INLINE__
  2. //#define __USE_BASETYPE__
  3. #include <exec/types.h>
  4. #include <proto/intuition.h>
  5. #include <proto/utility.h>
  6. #include <intuition/intuition.h>
  7. #include <proto/dos.h> //Printf
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. #define TEMPLATE "WINDOW/A,SCREEN/K, ACTIVE/S" // if screen not mentioned Workbench
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. char scrnname[MAXPUBSCREENNAME+1]; //= ""; "Workbench"; //default pubscreen
  16. scrnname[0] = 0;
  17. char winnamesh[31];
  18. LONG args[] = { 0, 0};
  19. BOOL active = FALSE;
  20. struct RDArgs *rdargs;
  21. int RC = RETURN_FAIL;
  22. struct Window *win;
  23. rdargs = ReadArgs(TEMPLATE,args, NULL);
  24. if (rdargs)
  25. {
  26. if (args[0]) strncpy (winnamesh, (char *)args[0] , 30); //OK windowname abbreviated to 30 characters (if more)
  27. if (args[1]) strncpy (scrnname, (char *)args[1], MAXPUBSCREENNAME);
  28. if (args[2]) {active = args[2];}
  29. if(active)
  30. {}
  31. else
  32. {}
  33. } //rdargs
  34. else
  35. {
  36. PrintFault(IoErr(), NULL);
  37. Printf("usage: %s\n",TEMPLATE);
  38. }
  39. FreeArgs(rdargs);
  40. return (RC);
  41. }
  42.  
salass00
salass00's picture
Offline
Last seen: 1 month 1 week ago
Joined: 2011-02-03 11:27
Try removing the space in the
Try removing the space in the TEMPLATE string before "ACTIVE/S". Also some other unrelated notes: Don't use strncpy()/strncat() unless you really need their weird behaviors for some reason, use the more sane strlcpy()/strlcat() instead (these are also available in utility.library as Strlcpy() and Strlcat() BTW). Your args array has only room for two elements yet you need room for three. Also I suggest defining the arg indexes as an enum rather than hardcoding the numbers directly in your code:
  1. enum {
  2. ARG_WINDOW,
  3. ARG_SCREEN,
  4. ARG_ACTIVE,
  5. NUM_ARGS
  6. };
Then you can define your args array simply as:
  1. LONG args[NUM_ARGS] = {0};
OldFart
OldFart's picture
Offline
Last seen: 2 months 6 days ago
Joined: 2010-11-30 14:09
@JosDuchIt Again, IIRC the
@JosDuchIt Again, IIRC the problem is in this line:
  1. #define TEMPLATE "WINDOW/A,SCREEN/K, ACTIVE/S"
where there is a space between a comma and a keyword. There may be a space leading the comma, but not trailing. Years ago I've constructed some testing examples about some DOS-functions, o.a. concerning ReadArgs(). It has been long since, but I think they're to be found in this archive: http://www.os4depot.net/index.php?function=showfile&file=development/example/dostutorials.lha Hope I could help you out OldFart P.s.: ever thought of 'just in time' declaration of variables? You declare them first thing in this function, but as the function 'ReadArgs()' fails, they've been declared for naught. Same goes for FreeArgs(), which is called whether ReadArgs() returns fine or not. It only needs to be called when ReadArgs() returns with a value.
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2012-01-08 20:57
@all thanks for the hints
@all thanks for the hints
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2012-01-08 20:57
@Oldfart ever thought of
@Oldfart ever thought of 'just in time' declaration of variables? Not yet. I am a "one-pogram" maintainer & i copied the style from the source i inherited. There are some advantages to this style too: i can easily enter (copying) debugging statements without worrying if the variables are blocklocal as i am coding on my SAM without a debugger, it saves me time. It is easier too see what variables are used too. I am a bit lazy probably.
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2012-01-08 20:57
@salas00 Using the enum: I
@salas00 Using the enum: I don't quite see how to modify my source. This is how it looks now. (Works OK.) Could you modify it as you suggest? Thanks in advance. The new template works fine examples: getsizes W NotePad S OrigynWebBrowser // ok getsizes S OrigynWebBrowser // no active window there getsizes S Workbench // my active shell getsizes W "" S Workbench // my active shell getsizes W CM.gc // ok
  1. * compiles and runs, use -lauto */
  2. /* aka JosDuchIt */
  3.  
  4. #define __USE_INLINE__
  5. //#define __USE_BASETYPE__
  6. #include <exec/types.h>
  7. #include <proto/intuition.h>
  8. #include <proto/utility.h>
  9. #include <intuition/intuition.h>
  10. #include <proto/dos.h> //Printf
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #define TEMPLATE "W=WINDOW/K,S=SCREEN/K"
  15. // if screen not mentioned Workbench
  16. // if window not mentioned active window
  17.  
  18. struct Window * get_window( STRPTR , STRPTR ) ;
  19. struct Window * get_active( STRPTR) ;
  20.  
  21. BOOL print_wsizes(struct Window *) ;
  22.  
  23. BOOL print_wsizes(struct Window *win)
  24. {
  25. //struct Window *win=NULL;
  26. printf("%s\n", " Title LeftEdge TopEdge Width Height MinWidht MinHeight GZZWidth GZZHeight "); //Mouse X,y GEEX,Y ??
  27. printf("%s\n", win->Title); /// crash
  28. printf("%ld\n",win->LeftEdge);
  29. printf("%ld\n",win->TopEdge);
  30. printf("%ld\n",win->Width);
  31. printf("%ld\n",win->Height);
  32. printf("%ld\n",win->MinWidth);
  33. printf("%ld\n",win->MinHeight);
  34. printf("%ld\n",win->GZZWidth);
  35. printf("%ld\n",win->GZZHeight);
  36. return (TRUE);
  37. }
  38.  
  39. struct Window *get_window (STRPTR screenname, STRPTR windowabbrev)
  40. {
  41. struct Window *win = NULL;
  42.  
  43. if (windowabbrev)
  44. {
  45. long abbrlen = strlen(windowabbrev);
  46. struct Screen *scr;
  47.  
  48. if (scr = LockPubScreen (screenname && !screenname[0] ? NULL : screenname))
  49. {
  50. for (win = scr->FirstWindow; win; win = win->NextWindow)
  51. {
  52. if (win->Title && !Strnicmp(win->Title,windowabbrev,abbrlen))
  53. break;
  54. }
  55. UnlockPubScreen (NULL,scr);
  56. }
  57. }
  58. return (win);
  59. }
  60.  
  61.  
  62. struct Window *get_active (STRPTR screenname)
  63. {
  64. struct Window *win = NULL;
  65. struct Screen *scr = NULL;
  66. ULONG ib;
  67. if (scr = LockPubScreen (screenname && !screenname[0] ? NULL : screenname))
  68. {
  69. ib = LockIBase(0);
  70. win = ((struct IntuitionBase *)IntuitionBase)->ActiveWindow; //OS4E was IntuitionBase->ActiveWindow; //OS4E
  71. UnlockIBase(ib);
  72. //if (wn && wn->Title) strncpy (buff, wn->Title, bfsize)
  73. }
  74. return (win);
  75. }
  76.  
  77.  
  78. int main(int argc, char *argv[])
  79. {
  80. char scrnname[MAXPUBSCREENNAME+1]; //= ""; "Workbench"; //default pubscreen
  81. scrnname[0] = 0;
  82. char winnamesh[31];
  83. LONG args[] = { 0, 0};
  84. BOOL active = FALSE;
  85. struct RDArgs *rdargs;
  86. int RC = RETURN_FAIL;
  87. struct Window *win;
  88. rdargs = ReadArgs(TEMPLATE,args, NULL);
  89. if (rdargs)
  90. {
  91. if (args[0])
  92. {
  93. strncpy (winnamesh, (char *)args[0] , 30); //OK windowname abbreviated to 30 characters (if more)
  94. }
  95. else
  96. {
  97. active =TRUE;
  98. }
  99.  
  100. if (args[1]) strncpy (scrnname, (char *)args[1], MAXPUBSCREENNAME); // test for existing screenname
  101. if(active)
  102. {
  103. if (win = get_active((STRPTR)scrnname))
  104. {
  105. print_wsizes(win);
  106. RC = RETURN_OK;
  107. }
  108. }
  109. else // WINDOW/K used
  110. {
  111. if ((winnamesh) && (winnamesh[0])) /// check for empty ?? if empty=>active window
  112. {
  113. if (win = get_window((STRPTR)scrnname, (STRPTR)winnamesh)) //screename or windowname may be wrong
  114. {
  115. print_wsizes(win);
  116. RC = RETURN_OK;
  117. }
  118. else
  119. {
  120. printf("No window %s or screen %s found, Case correct?\n", winnamesh,scrnname);
  121. }
  122. }
  123. else
  124. {
  125. if (win = get_active((STRPTR)scrnname))
  126. {
  127. print_wsizes(win);
  128. RC = RETURN_OK;
  129. }
  130. }
  131. }
  132. } //rdargs
  133. else
  134. {
  135. PrintFault(IoErr(), NULL);
  136. printf("usage: %s\n",TEMPLATE);
  137. }
  138. FreeArgs(rdargs);
  139. return (RC);
  140. }
  141.  
  142. </cod>
salass00
salass00's picture
Offline
Last seen: 1 month 1 week ago
Joined: 2011-02-03 11:27
I rewrote your getsizes
I rewrote your getsizes program for you as you asked me to: https://dl.dropboxusercontent.com/u/26599983/getsizes.c I removed the scrnname parameter from the get_active() function as it was not used for anything meaningful. I also made the window list scanning and window structure reading safer by surrounding this whole code section with LockIBase()/UnlockIBase() which in turn made it necessary to copy the data into a temporary data structure (struct WindowData) for printing later with print_wdata(). I also added the commandline I used for compiling to the source code. I recommend always using the following options "-Wall -Werror -Wwrite-strings" when compiling your own code using gcc. The -Wall and -Wwrite-strings are for enabling useful compiler warnings and the -Werror makes the compiler treat them as errors (especially useful when building larger projects where it can be easy to miss some warnings if there are a lot of different files to compile).
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2012-01-08 20:57
@salass00 Thanks for help &
@salass00 Thanks for help & friendly coding lesson
Log in or register to post comments