home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / bbs / libdisks / d700t799 / disk774.lha / ExtraCmds / src / Lower.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-05  |  6.0 KB  |  200 lines

  1. /*
  2.  * Lower - run a command at a nice, possibly lower, priority.
  3.  *         AmigaDOS equivalent of UNIX nice.
  4.  *
  5.  * Version 37.1 =TP= 16-Jan-92
  6.  * Compile with SAS/C 5.10 and link without startup code:
  7.  *      lc -cqfist -v -b0 -rr -O -ms Lower
  8.  *      blink Lower.o to Lower sd sc
  9.  *      protect Lower +p
  10.  *
  11.  * Copyright (c) 1992 Torsten Poulin
  12.  *
  13.  * Torsten Poulin
  14.  * Banebrinken 99, 2, lejlighed 77
  15.  * DK 2400  København NV
  16.  * DENMARK
  17.  */
  18.  
  19. /****** English:LOWER *****************************************************
  20. *
  21. *   FORMAT
  22. *       LOWER [BY <value>] [STACK <size>] [COMMAND] <command line>
  23. *
  24. *   TEMPLATE
  25. *       BY/K/N,STACK/K/N,COMMAND/F/A
  26. *
  27. *   PURPOSE
  28. *       To run a command at a nice, possibly lower, priority.
  29. *
  30. *   SPECIFICATION
  31. *       Since the Amiga is multitasking, it uses priority numbers to
  32. *       determine the order in which current tasks should be serviced.
  33. *       Normally, most tasks have a priority of 0, and the time and
  34. *       instruction cycles of the CPU are divided equally among them.
  35. *
  36. *       LOWER executes the specified command line with the priority
  37. *       lowered by <value>.  If no BY option is specified a default
  38. *       value of 1 is used.  If a negative value is specified the
  39. *       priority will be raised instead.  The resulting priority will
  40. *       be between -128 and +10.  This is enforced silently.
  41. *
  42. *       The STACK option sets the stack size of the command to be
  43. *       executed.  Default is to use the current stack size.
  44. *
  45. *       The <command line> is any valid shell command line; i.e. it
  46. *       is possible to use redirection, aliases, resident commands
  47. *       etc.
  48. *
  49. *       LOWER leaves the value of the executed command in the
  50. *       condition flags RC and Result2.  Ctrl-C, D, E, and F are
  51. *       sent to the executed command.
  52. *
  53. *       In short, the use of LOWER is tranparent.
  54. *
  55. *   EXAMPLES
  56. *       To print the file Text at lower priority, type:
  57. *
  58. *           1> LOWER COPY Text TO PRT:
  59. *
  60. *       And to search, in the background, for occurences of the
  61. *       string "foo", in the S: directory, you might issue
  62. *       the following:
  63. *
  64. *           1> RUN LOWER BY 2 SEARCH S: "foo" TO MyOutputFile
  65. *
  66. *   SEE ALSO
  67. *       CHANGETASKPRI, STACK, STATUS
  68. *
  69. ***************************************************************************
  70. *
  71. */
  72. /****** dansk:LOWER *******************************************************
  73. *
  74. *   FORMAT
  75. *       LOWER [BY <værdi>] [STACK <størrelse>]
  76. *             [COMMAND] <kommandlinje>
  77. *
  78. *   SKABELON
  79. *       BY/K/N,STACK/K/N,COMMAND/F/A
  80. *
  81. *   FORMÅL
  82. *       At udføre en kommando med en rar, muligvis lavere, prioritet.
  83. *
  84. *   SPECIFIKATION
  85. *       Da Amigaen er et samkørselssystem bruger den prioritetstal
  86. *       til at afgøre i hvilken orden de nuværende opgaver skal
  87. *       betjenes.  Normalt har de fleste opgaver prioriteten 0, og
  88. *       centralenhedens tid deles lige mellem dem.
  89. *
  90. *       LOWER udfører den angivne kommandolinje med prioriteten
  91. *       sænket med <værdi>.  Hvis der ikke er angivet nogen værdi
  92. *       med BY, bruges 1 som standardværdi.  Hvis en negativ værdi
  93. *       angives vil prioriteten blive hævet istedet.  Den
  94. *       resulterende prioritet skal være mellem -128 og +10.  Dette
  95. *       bliver håndhævet tavst.
  96. *
  97. *       Argumentet STACK sætter stakstørrelsen for den kommando der
  98. *       skal udføres.  Standard er at beholde den nuværende
  99. *       størrelse.
  100. *
  101. *       Argumentet <kommandolinje> kan være enhver tilladt shell-
  102. *       kommandolinje.  Dvs. at det er muligt at benytte omdiri-
  103. *       gering, aliaser, residente kommandoer osv.
  104. *
  105. *       LOWER efterlader resultaterne af den udførte kommando
  106. *       i tilstandsflagene RC og Result2.  Ctrl-C, D, E og F
  107. *       sendes til kommandoen der er ved at blive udført.
  108. *
  109. *       Kort sagt, brugen af LOWER er transparent.
  110. *
  111. *   EKSEMPLER
  112. *       Skriv
  113. *
  114. *          1> LOWER COPY Tekst TO PRT:
  115. *
  116. *       for at udskrive filen Tekst på skriveren.
  117. *       Og for at søge, i baggrunden, efter forekomster af tegn-
  118. *       følgen "foo", i kataloget S:, kan man gøre følgende:
  119. *
  120. *           1> RUN LOWER BY 2 SEARCH S: "foo" TO MinUddatafil
  121. *
  122. *   SE OGSÅ
  123. *       CHANGETASKPRI, STACK, STATUS
  124. *
  125. ***************************************************************************
  126. *
  127. */
  128.  
  129.  
  130. #include <exec/types.h>
  131. #include <exec/tasks.h>
  132. #include <exec/libraries.h>
  133. #include <libraries/dos.h>
  134. #include <dos/dos.h>
  135. #include <dos/dostags.h>
  136. #include <proto/exec.h>
  137. #include <proto/dos.h>
  138.  
  139. static LONG MySystemTags(struct DosLibrary *DOSBase,
  140.                          UBYTE *command, ULONG firsttag, ...);
  141.  
  142. char const *version = "\0$VER: Lower 37.1 (16.1.92) ©1992 Torsten Poulin";
  143.  
  144. int entrypoint(void)
  145. {
  146.     struct RDArgs     *args;
  147.     struct DosLibrary *DOSBase;
  148.     struct Library    *SysBase;
  149.     LONG niceval, stack;
  150.     LONG arg[3];
  151.     LONG rc = RETURN_OK;
  152.  
  153.     SysBase = *(struct Library **) 4L;
  154.     if(!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
  155.         return RETURN_FAIL;
  156.  
  157.     arg[0] = arg[1] = arg[2] = 0L;
  158.     
  159.     if(args = ReadArgs("BY/K/N,STACK/K/N,COMMAND/F/A", arg, NULL))
  160.     {
  161.         niceval = (FindTask(NULL))->tc_Node.ln_Pri;
  162.         if(arg[0])
  163.             niceval -= *(LONG *) arg[0];
  164.         else
  165.             niceval--;
  166.         if(niceval < -128L) niceval = -128L;
  167.         else if(niceval > 10L) niceval = 10L;
  168.  
  169.         if(arg[1])
  170.             stack = (*(LONG *) arg[1]) < 1600L ? 1600L : *(LONG *) arg[1];
  171.         else
  172.             stack = (Cli())->cli_DefaultStack << 2;
  173.                
  174.         rc = MySystemTags(DOSBase, (UBYTE *) arg[2],
  175.                           SYS_UserShell, TRUE,
  176.                           NP_Priority,   niceval,
  177.                           NP_StackSize,  stack,
  178.                           TAG_DONE);
  179.         if(rc == -1)
  180.             rc = RETURN_ERROR;
  181.  
  182.         FreeArgs(args);
  183.     }
  184.     else
  185.     {
  186.         LONG err = IoErr();
  187.         PrintFault(err, "Lower");
  188.         rc = RETURN_ERROR;
  189.     }
  190.     CloseLibrary((struct Library *) DOSBase);
  191.     return rc;
  192. }
  193.  
  194.  
  195. static LONG MySystemTags(struct DosLibrary *DOSBase,
  196.                          UBYTE *command, ULONG firsttag, ...)
  197. {
  198.     return SystemTagList(command, (struct TagItem *) &firsttag);
  199. }
  200.