home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff284.lzh / ARPTools / src / Trunc.c < prev    next >
C/C++ Source or Header  |  1989-11-27  |  5KB  |  243 lines

  1. /*
  2.           Trunc - CR-LF conversion and simple formatting.
  3.  
  4.           Original effort by Fabio Rossetti. Inspired by the trunc program
  5.           by Gary Brant found on <>< 179.
  6.  
  7.       (c) 1989 by Fabio Rossetti
  8.  
  9.       To compile under Lattice C v5.0x use:
  10.  
  11.         lc -O -v -cus  trunc
  12.         blink lib:cres.o trunc.o to trunc lib lib:a.lib lib:lc.lib sd nd
  13.  
  14. */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/libraries.h>
  19. #include <libraries/dos.h>
  20. #include <libraries/dosextens.h>
  21. #include <libraries/arpbase.h>
  22. #include <arpfunctions.h>
  23. #include <proto/exec.h>
  24. #include <proto/dos.h>        
  25.  
  26. struct ArpBase *ArpBase=NULL;
  27. struct Process *Pr;
  28.  
  29. LONG argc;
  30.  
  31. #define NARGS 8
  32.  
  33. STRPTR argv[NARGS];
  34.  
  35. #define FROM    argv[0]
  36. #define TO    argv[1]
  37. #define LCL    argv[2]
  38. #define SC    argv[3]
  39. #define CL    argv[4]
  40. #define LM    argv[5]
  41. #define LC    argv[6]
  42. #define RC    argv[7]
  43.  
  44. LONG lm,lc,rc;
  45.  
  46. TEXT *Buf,*OBuf,*Margin;
  47.  
  48. #define MAXLIN 1024
  49. #define BFSIZE 512    
  50.  
  51. struct UserAnchor {
  52.     struct    AnchorPath    ua_AP;
  53.     BYTE    moremem[255];    /* extension */
  54. };
  55.  
  56. struct    UserAnchor *Anchor=NULL;
  57. TEXT *Lin[256];
  58.  
  59. /* Trick to keep code down to size */
  60. VOID MemCleanup()
  61. {
  62. }
  63.  
  64. /* shutdown routine */
  65. VOID Cleanup(r1,r2,msg)
  66. LONG r1,r2;
  67. STRPTR msg;
  68. {
  69.     if (ArpBase) CloseLibrary((struct Library *)ArpBase);
  70.     Pr->pr_Result2 = r2;
  71.     if (msg) Puts(msg);
  72.     exit(r1);
  73. }
  74.  
  75. /* output a line */
  76. VOID PrOut (o)
  77. BPTR o;
  78. {
  79.  
  80.     if (LM) FPrintf(o,"%s%s",Margin,OBuf);
  81.     if (LC) if (strlen(OBuf)> lc)
  82.         FPrintf(o,"%s",(TEXT *)(OBuf+lc));
  83.         else FPrintf(o,"\n");
  84.     if (RC) if  (strlen(OBuf) > rc) {
  85.             *(OBuf + rc) = '\000';
  86.             FPrintf(o,"%s\n",OBuf);
  87.             }
  88.         else FPrintf(o,OBuf);
  89.     if (!(RC || LC || LM)) FPrintf(o,"%s",OBuf);
  90.  
  91. }
  92.  
  93. /* perform CR-LF stuff */
  94. VOID Trunc(Inp,Out)
  95. BPTR Inp,Out;
  96.  
  97. {
  98.  
  99. REGISTER LONG count, bufp=0,fl=0;
  100. REGISTER TEXT *i,*il = OBuf,c;
  101.  
  102. if (LM) {
  103.     Margin = ArpAllocMem(lm+3,MEMF_CLEAR);
  104.     for (i=Margin;(LONG)( i - Margin) < (LONG)lm; i++) *i=' ';
  105.     }
  106.  
  107.     for (;;) {
  108.     if (!bufp) if (!(count = Read(Inp,Buf,BFSIZE))) {        
  109.             if(fl) {
  110.             ++bufp;    
  111.             *il++ = '\n';
  112.             *il = '\0';
  113.             PrOut(Out);
  114.             }
  115.             return((VOID)0);    
  116.             }
  117.     fl = 1;
  118.         /* check CTRL-C */
  119.         if (SetSignal(0,0) & SIGBREAKF_CTRL_C) 
  120.         Cleanup(RETURN_WARN,NULL,"***Break");
  121.  
  122.  
  123.     if (((TEXT *)il - (TEXT *)OBuf) > MAXLIN) 
  124.                 Cleanup(RETURN_FAIL,"Buffer overflow");
  125.     
  126.  
  127.     switch(c = *(Buf + bufp++)) {
  128.  
  129.         case '\012':
  130.         fl=0;
  131.         if (LCL) *il++ = '\015';
  132.         *il++ = c;
  133.         *il = '\0';
  134.         PrOut(Out);
  135.         il = OBuf;
  136.         break;
  137.         case '\015':
  138.         if (CL) *il++ = '\012';
  139.         if (!CL && !SC)    *il++ = c;
  140.         break;
  141.  
  142.         default:
  143.         *il++ = c;
  144.     }
  145.     
  146.  
  147.     if (bufp == count) bufp = 0;
  148.  
  149.     }
  150. }
  151.  
  152. VOID _main(Line)
  153. STRPTR Line;
  154. {
  155.  
  156.  
  157.  
  158.     BPTR infh,outfh;
  159.  
  160.     Pr = (struct Process*)FindTask(NULL);
  161.     
  162.     if(!(ArpBase = (struct ArpBase*)OpenLibrary(ArpName,ArpVersion)))
  163.             Cleanup(RETURN_FAIL,ERROR_INVALID_RESIDENT_LIBRARY,NULL);
  164.     
  165.     /* parse command line */
  166.     for (argc=0;argc < NARGS ;++argc)
  167.         argv[argc] = (STRPTR) NULL;
  168.  
  169.     while(*Line > ' ')
  170.         ++Line;
  171.  
  172.     if((argc = GADS(++Line,
  173.         strlen(Line),
  174.         "Usage: Trunc [FROM Pattern] [TO Filename] LCL | SC | CL |\n       LM leftmargin | LC leftcolumn | RC rightcolumn\n\n       LCL : Linefeeds to CR+LFs\n       SC  : Strip CRs\n       CL  : CRs to Linefeeds",
  175.         argv,
  176.         "FROM,TO,LCL/S,SC/S,CL/S,LM,LC,RC" )) < 0)
  177.             Cleanup(RETURN_WARN,NULL,FROM);
  178.  
  179.     if (!(Buf = ArpAllocMem(BFSIZE,MEMF_CLEAR)) || 
  180.         !(OBuf = ArpAllocMem(MAXLIN+1,MEMF_CLEAR)))
  181.                 Cleanup(RETURN_FAIL,ERROR_NO_FREE_STORE,"Error: no memory");
  182.  
  183.     if ( Anchor = (struct UserAnchor *)ArpAlloc( (ULONG)sizeof( *Anchor )) )
  184.     {
  185.         Anchor->ua_AP.ap_Length = 255;    /* Want full path built */
  186.         
  187.     }
  188.     else Cleanup(RETURN_FAIL,ERROR_NO_FREE_STORE,"Error:No memory");
  189.     
  190.     if (FROM)  {
  191.  
  192.     if (!(FindFirst(FROM,(struct AnchorPath*) Anchor)))
  193.         if (Anchor->ua_AP.ap_Info.fib_DirEntryType < 0) {
  194.             if(!(infh=ArpOpen(Anchor->ua_AP.ap_Buf,MODE_OLDFILE))) {
  195.                 Printf("Can't open %s\n",FROM);
  196.                 Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  197.                 }
  198.             }
  199.         else {
  200.             Printf("%s is a directory !\n",TO);
  201.             Cleanup(RETURN_ERROR,ERROR_OBJECT_WRONG_TYPE,NULL);
  202.             }
  203.         else {
  204.             Printf("Can't find %s\n",FROM);
  205.             Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  206.             }
  207.         }
  208.     else infh = Input();
  209.  
  210.     if(TO) {
  211.             if(!(outfh=ArpOpen(TO,MODE_NEWFILE))) {
  212.                 Printf("Can't open %s\n",TO);
  213.                 Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  214.                 }
  215.             }
  216.     else outfh = Output();
  217.  
  218. /*    if((CL && SC && LCL) || (LC && RC && LM) || (LC && RC) || (LC && LM) || (RC && LM))
  219.                         Cleanup(RETURN_ERROR,NULL,"Bad args"); 
  220. */
  221.     if    (( argc > 3 ) ||
  222.         (( argc == 3) && !(FROM && TO)) || 
  223.         (( argc == 2) && (!FROM && !TO)))    
  224.                     Cleanup(RETURN_ERROR,NULL,"Bad args");
  225.  
  226.     if (LM)
  227.         if( ((lm=Atol(LM)) <=0 || Errno == ERRBADINT))
  228.             Cleanup(RETURN_ERROR,NULL,"Bad args");
  229.  
  230.     if (LC)
  231.         if( ((lc=Atol(LC)) <=0 || Errno == ERRBADINT))
  232.             Cleanup(RETURN_ERROR,NULL,"Bad args");
  233.  
  234.     if (RC)
  235.         if( ((rc=Atol(RC)) <=0 || Errno == ERRBADINT))
  236.             Cleanup(RETURN_ERROR,NULL,"Bad args");
  237.             
  238.     Trunc(infh,outfh);
  239.  
  240.     Cleanup(NULL,NULL,NULL);
  241.  
  242. }
  243.