home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 451.lha / SType / SType.c < prev    next >
C/C++ Source or Header  |  1990-12-08  |  4KB  |  167 lines

  1. /*======================================================*/
  2. /*                                                                                                            */
  3. /* Type a file                                                                                    */
  4. /* © J.Tyberghein                                                                                */
  5. /*        Thu Aug 16 17:00:45 1990 V1.0                                            */
  6. /*        Mon Sep 24 10:07:11 1990                                                    */
  7. /*                                                                                                            */
  8. /*======================================================*/
  9.  
  10. #include <exec/types.h>
  11. #include <proto/exec.h>
  12. #include <proto/dos.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/dos_protos.h>
  15. #include <exec/memory.h>
  16. #include <dos/dos.h>
  17. #include <dos/dosextens.h>
  18. #include <dos/rdargs.h>
  19. #include <string.h>
  20.  
  21. /*================================== Data ====================================*/
  22.  
  23. char CLI_Help[] = "\
  24. Special Type 1.0  Written by J.Tyberghein from PowerPeak, 24 Sep 90\n\n\
  25. Usage:\n\
  26.   SType <File> [From <line>] [TO <line>] [Page <lines>]\n\n\
  27. With:\n\
  28.   File   : the file you want to type\n\
  29.   Page   : list per pages\n\
  30.   From   : the line number where you want to start typing\n\
  31.   To     : the line number where you want to stop typing\n";
  32.  
  33. char CLI_Template[] = "File/a,F=FROM/n,TO/n,P=PAGE/n";
  34. struct RDArgs *rcrda = NULL;
  35. BPTR fh = 0;
  36.  
  37. extern APTR DOSBase;
  38.  
  39. void RealMain (char *file, int From, int To, int Page);
  40. void __regargs CloseStuff (int);
  41. void myPrintf (char *FormatStr, ...);
  42. void OpenStuff ();
  43.  
  44. typedef void (*fntype)();
  45.  
  46. #define ERR_OPEN -1
  47. #define ERR_READ -2
  48. #define ERR_LIB  -3
  49.  
  50. /*================================== Code ====================================*/
  51.  
  52.  
  53. /*------------------------------- main program -------------------------------*/
  54.  
  55. void argmain ()
  56.  
  57. {
  58.     LONG Args[4];
  59.     int From,To,Page;
  60.  
  61.     OpenStuff ();
  62.  
  63.     Args[1] = Args[2] = Args[3] = 0;
  64.     rcrda = ReadArgs (CLI_Template,Args,NULL);
  65.     if (!rcrda)
  66.         {
  67.             PutStr (CLI_Help);
  68.             CloseStuff (0);
  69.         }
  70.  
  71.     From = 0;
  72.     To = 2000000000;
  73.     Page = 0;
  74.     if (Args[1]) From = *(int *)Args[1];
  75.     if (Args[2]) To = *(int *)Args[2];
  76.     if (Args[3]) Page = *(int *)Args[3];
  77.  
  78.     RealMain ((char *)Args[0],From,To,Page);
  79. }
  80.  
  81. /*---------------------------------- printf ----------------------------------*/
  82.  
  83. void myPrintf (char *FormatStr, ...)
  84. {
  85.     VPrintf (FormatStr,(LONG *)((&FormatStr)+1));
  86. }
  87.  
  88. /*-------------------------------- CloseStuff --------------------------------*/
  89.  
  90. void __regargs CloseStuff (int Error)
  91. {
  92.     if (fh) Close (fh);
  93.     if (rcrda) FreeArgs (rcrda);
  94.     XCEXIT (Error);
  95. }
  96.  
  97. /*------------------------------ Print an error ------------------------------*/
  98.  
  99. void __regargs SayError (int Error, char *Object)
  100. {
  101.     char *Head,*ObjSort;
  102.  
  103.     ObjSort = "file";
  104.     switch (Error)
  105.         {
  106.             case ERR_OPEN        :    Head = "opening"; break;
  107.             case ERR_READ        :    Head = "reading"; break;
  108.         }
  109.     myPrintf ("\015ERROR: %s %s %s !\n",Head,ObjSort,Object);
  110.     CloseStuff (Error);
  111. }
  112.  
  113. /*------------------------- Open everything we need --------------------------*/
  114.  
  115. void OpenStuff ()
  116. {
  117.     APTR DosBase;
  118.  
  119.     if (!(DosBase = (APTR)OpenLibrary (DOSNAME,36)))
  120.         {
  121.             Write (Output (),"Needs AmigaDOS 2.0 !\n",21);
  122.             XCEXIT (ERR_LIB);
  123.         }
  124.     CloseLibrary ((struct Library *)DosBase);
  125. }
  126.  
  127. /*------------------------------ Main program --------------------------------*/
  128.  
  129. void RealMain (char *file, int From, int To, int Page)
  130. {
  131.     int lin,pl;
  132.     char line[256],*i;
  133.  
  134.     if (!(fh = Open (file,MODE_OLDFILE))) SayError (ERR_OPEN,file);
  135.  
  136.     lin = 1;
  137.     pl = 0;
  138.     while (i = FGets (fh,line,255))
  139.         {
  140.             if (CheckSignal (SIGBREAKF_CTRL_C) == SIGBREAKF_CTRL_C)
  141.                 {
  142.                     PutStr ("***Break\n");
  143.                     CloseStuff (0);
  144.                 }
  145.             if (lin>=From && lin<=To)
  146.                 {
  147.                     if (Page)
  148.                         {
  149.                             if (pl == Page)
  150.                                 {
  151.                                      Write (Output (),"--- MORE ---",20);
  152.                                     if (FGetC (Input ()) == 'q') CloseStuff (0);
  153.                                     pl = 1;
  154.                                 }
  155.                             else pl++;
  156.                         }
  157.                     PutStr (line);
  158.                 }
  159.             lin++;
  160.         }
  161.  
  162.     if (!i && IoErr ()) SayError (ERR_READ,file);
  163.     CloseStuff (0);
  164. }
  165.  
  166. /*================================ The end ===================================*/
  167.