home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / util / dir / browserii / sources / for.c < prev    next >
C/C++ Source or Header  |  1994-11-09  |  21KB  |  747 lines

  1. /*
  2.  *  Copyright © 1992-94 by S.R. & P.C.
  3.  *
  4.  *  Created:    30 Mar 1992  18:26:36
  5.  *  Modified:   09 Nov 1994  23:11:45
  6.  *
  7.  *  Make>> rx BumpRev For 3 8
  8.  *  Make>> sc <file>.c
  9.  *  Make>> slink LIB:cs.o <file>.o SC SD BATCH NOICONS TO <file> LIB LIB:c.lib
  10.  *  Make>> protect <file> P ADD
  11.  *
  12.  *
  13.  *    30/03/1992: V1.0 (Pierre Carrette)
  14.  *        Simple version with template: "Args/M,ALL/S,DONTFAIL/S,DO/K/A/F"
  15.  *  4/07/1994: V2.2 (Sylvain Rougier)
  16.  *      No longer add the name of the object if no '%%' present
  17.  *  5/07/1994: V3.0
  18.  *      Add the 'Quiet' option not to print hierarchy (but still print FileType if asked)
  19.  *      Now work as it should always work: don't scan all the directory if no pattern
  20.  *      but scan all subdir if 'All' option specified.
  21.  *      Add the 'GivePathName' option to provide a Full Path arguments to the command.
  22.  *      No longuer add '"' around the file name. User can type '"%%"' if he need it.
  23.  *  7/07/1994 V3.1
  24.  *      If a full pathname file entry was specified, For don't find it type. fixed
  25.  *  15/07/1994 V3.2
  26.  *      Guru if a single file was suplied. fixed
  27.  *      Now FullPathArgs realy work
  28.  *      Don't put optimize or it guru ! (see note)
  29.  *  31/07/1994 V3.3
  30.  *      Check the stack size because it need at least 8192 (probably !...)
  31.  *      put optimize again. just to see.
  32.  *  07/08/1994 V3.4
  33.  *      Remove optimize because the sasc bug still here
  34.  *      correct some error int the doc ( the '*' was incorrectly used instead of the '"')
  35.  *  21/08/1994 V3.5
  36.  *      Now use StackSwap() to avoid stack pb
  37.  *  22/09/1994 V3.6    (Pierre Carrette is back :-)
  38.  *        Major code cleanup. No more need StackSwap(). Bug fixes in template. whatis.library now Optional.
  39.  *        Inverted 'Quiet' to 'Verbose'. Changed 'VNFileType' to more comprehensive 'FileTypeVar' keyword.
  40.  *        Changed 'GivePathName' to 'FullPathArgs'.
  41.  *      FileTypeVar was not set if Showbytes was off.
  42.  *      Showbytes/ShowFileType is now off if no Cmd supplied.
  43.  *        'Type' argument syntax simplified. now:  "#text,!source c"
  44.  *        MatchFileType() bug fix. "#text,!source c" was not working!!
  45.  *        SAS Optimisation now works!!!
  46.  *  22/09/1994 V3.7
  47.  *        Recompiled with new version of c.lib which had a bug for 'ENV:' var parsing
  48.  *  09/11/1994 V3.8
  49.  *        Bug fix. File UnLock()ed before processed so that command can open it for writing.
  50.  */
  51.  
  52. #include <clib.h>
  53. #include <libraries/WhatIsBase.h>
  54. #include <proto/WhatIs.h>
  55. #include "For_rev.h"
  56.  
  57. // Startup Modules...
  58. CliArgs; ReadEnvArgs("For");
  59.  
  60. static char Version[] = VERSTAG;
  61.  
  62. STRPTR Template = "\
  63. Pattern/A/M,Files/K,Dirs/K,Since/K,Before/K,MinSize/K/N,MaxSize/K/N,\
  64. PosProtect/K,NegProtect/K,Type/K,All/S,ASync/S,ReadSize/K/N,ShowBytes/K/N,\
  65. D=Deep/K/N,SFT=ShowFileType/S,FTV=FileTypeVar/K,FPA=FullPathArgs/S,Verbose/S,Do/K/F";
  66.  
  67. STRPTR CliHelp = VERS" ("DATE") © Sylvain Rougier & Pierre Carrette.\n\
  68. Usage: For <Pattern> [Files <MATCH|YES|NO>] [Dirs <MATCH|YES|NO>]\n\
  69. [Since <Date>] [Before <Date>] [MinSize <Number>] [MaxSize <Number>]\n\
  70. [PosProtect <L|C|H|S|P|A|R|W|E|D>] [NegProtect <L|C|H|S|P|A|R|W|E|D>]\n\
  71. [Type <[#=SUB,~=EXLC]FileType0,,,FileType15>] [All] [ASync] [ReadSize]\n\
  72. [ShowBytes] [Deep] [ShowFileType] [FileTypeVar <var name>] [FullPathArgs]\n\
  73. [DO <Command [args] [,Command [args] [,...]]>]\n";
  74.  
  75. #define ARG_Template        0
  76. #define ARG_Files            1
  77. #define ARG_Dirs            2
  78. #define ARG_Since            3
  79. #define ARG_Before            4
  80. #define ARG_MinSize            5
  81. #define ARG_MaxSize            6
  82. #define ARG_PosProtect        7
  83. #define ARG_NegProtect        8
  84. #define ARG_Type            9
  85. #define ARG_ALL                10
  86. #define ARG_ASYNC            11
  87. #define ARG_READSIZE        12
  88. #define ARG_SHOWBYTES        13
  89. #define ARG_DEEPWHATIS        14
  90. #define ARG_SHOWFILETYPE    15
  91. #define ARG_FileTypeVar        16
  92. #define ARG_FullPathArgs    17
  93. #define ARG_Verbose            18
  94. #define ARG_CMD                19
  95. #define ARG_ENDARG            20
  96.  
  97.  
  98. struct Library *WhatIsBase;
  99.  
  100.  
  101. #define MAX_PATHLEN        256
  102. #define MAX_CMDLEN         512
  103.  
  104. /* char used in the filetype spec string */
  105. #define NOTFileType        '~'
  106. #define WITHSubTypes    '#'
  107.  
  108. /*******************************************************/
  109. /* not in dos.h !! */
  110. #define    FIBB_HOLD        7
  111. #define    FIBF_HOLD        (1<<FIBB_HOLD)
  112.  
  113. /* it is my own definition: not standard */
  114. #define    FIBB_COMMENT    8
  115. #define    FIBF_COMMENT    (1<<FIBB_COMMENT)
  116. #define    FIBB_LINK        9
  117. #define    FIBF_LINK        (1<<FIBB_LINK)
  118.  
  119.  
  120. #define PATTERN_BUF_SIZE    40
  121. #define MAX_FTS                16    /* Max FileTypeSpec in SelectInfo */
  122.  
  123. struct FileTypeSpec {
  124.     FileType fs_FileType;
  125.     UWORD fs_Flags;
  126. };
  127.  
  128. /* FileTypeSpec flags */
  129. #define FTSF_EXCLUDETYPE    0x0001    /* Exclude this type (otherwise include) */
  130. #define FTSF_WITHSUBTYPES    0x0002    /* Affect Include/Exclude to subtypes */
  131.  
  132.  
  133. struct SelectInfo {
  134.     UBYTE si_PatTok[PATTERN_BUF_SIZE];        /* PreParsed pattern                        */
  135.     LONG si_MinSize;                        /* Show files bigger than that              */
  136.     LONG si_MaxSize;                        /*   and smaller than that                  */
  137.     struct DateStamp si_SinceDate;            /* Show files newer than that               */
  138.     struct DateStamp si_BeforeDate;            /*   and older than that                    */
  139.     UWORD si_PosProtect;                    /* Show files that have these bits set       */
  140.     UWORD si_NegProtect;                    /* Show files that have these bits clear     */
  141.     struct FileTypeSpec si_FileTypes[MAX_FTS];    /* Include and Exclude file types         */
  142.     UWORD si_NumFts;                        /* Number of FileTypeSpec in previous array */
  143.     UWORD si_Flags;                            /* Flags. See below                         */
  144. };
  145.  
  146. /* SelectInfo flags */
  147.  
  148. #define SI_ALL_FILES        0x0001
  149. #define SI_MATCH_FILES        0x0002
  150. #define SI_ALL_DIRS            0x0004
  151. #define SI_MATCH_DIRS        0x0008
  152. #define SI_AFFECT_SUBDIRS    0x0010
  153. #define SI_NAME                0x0020
  154. #define SI_SIZE                0x0040
  155. #define SI_SINCEDATE        0x0080
  156. #define SI_BEFOREDATE        0x0100
  157. #define SI_POSPROTECTION    0x0200
  158. #define SI_NEGPROTECTION    0x0400
  159. #define SI_POSFILETYPE        0x0800
  160. #define SI_NEGFILETYPE        0x1000
  161.  
  162. #define SI_FILETYPE            (SI_POSFILETYPE|SI_NEGFILETYPE)
  163. #define SI_MATCHBITS        (SI_NAME|SI_SIZE|SI_SINCEDATE|SI_BEFOREDATE|SI_POSPROTECTION|SI_NEGPROTECTION|SI_FILETYPE)
  164.  
  165.  
  166. struct Opt {
  167.     BOOL Verbose;                /* Print some information */
  168.     BOOL Async;                    /* Run commands Asynchronously */
  169.     BOOL FullPathArgs;            /* Give Full PathName in Fmt ? */
  170.     BOOL ShowFileType;            /* Show FileType ? */
  171.     LONG ReadSize;                /* Size to read to find filetype */
  172.     ULONG Deep;                    /* deep level to find FileType */
  173.     UWORD Showbytes;            /* number of bytes to show */
  174.     STRPTR FileTypeVar;            /* Name of FileType var. no var if null */
  175. };
  176.  
  177.  
  178. /* return pointer to the first non blank char in a string, or to the '\0' if the string is empty */
  179.  
  180. static char *FirstNonBlank(char *buf)
  181. {
  182.     while (*buf && *buf == ' ')
  183.         buf++;
  184.     return buf;
  185. }
  186.  
  187.  
  188. /* check if buffer is empty or not */
  189.  
  190. static BOOL IsEmpty(char *buf)
  191. {
  192.     return (BOOL) ((*FirstNonBlank(buf)) ? FALSE : TRUE);
  193. }
  194.  
  195.  
  196. static BOOL String2Date(char *src, struct DateStamp *ds)
  197. {
  198.     struct DateTime *dt;
  199.     BOOL Ok = TRUE;
  200.     char *s1, *s2;
  201.     char buf[60];
  202.  
  203.     if (!(dt = AllocMem(sizeof(struct DateTime), MEMF_PUBLIC | MEMF_CLEAR)))
  204.          return FALSE;
  205.  
  206.     strcpy(buf, src);
  207.     s1 = s2 = FirstNonBlank(buf);
  208.     if (*s1) {
  209.         dt->dat_StrDate = s1;
  210.         while (*s2 && *s2 != ' ')
  211.             s2++;
  212.         if (*s2) {
  213.             *s2++ = '\0';
  214.             s2 = FirstNonBlank(s2);
  215.         }
  216.         if (*s2) {
  217.             dt->dat_StrTime = s2;
  218.             if (!StrToDate(dt))
  219.                 Ok = FALSE;
  220.         }
  221.         else if (!StrToDate(dt)) {
  222.             dt->dat_StrDate = NULL;
  223.             dt->dat_StrTime = s1;
  224.             if (!StrToDate(dt))
  225.                 Ok = FALSE;
  226.         }
  227.         if (Ok)
  228.             *ds = dt->dat_Stamp;
  229.     }
  230.     FreeMem(dt, sizeof(struct DateTime));
  231.  
  232.     return Ok;
  233. }
  234.  
  235.  
  236. static void PrintByte(UBYTE Buffer[], LONG BufLen, UBYTE Num)
  237. {
  238.     register short i;
  239.  
  240.     if (BufLen > 0) {
  241.         for (i = 0; i < Num && i < BufLen; i++)
  242.             Printf("%02lx", Buffer[i]);
  243.     }
  244. }
  245.  
  246.  
  247. /*
  248.  *  Parse a line that may contain comas. Backslash ('\') is the override char.
  249.  */
  250.  
  251. static UWORD ProtectBit(char P[])
  252. {
  253.     UWORD PB = 0;
  254.  
  255.     while (*P) {
  256.         switch (*P) {
  257.         case 'L':
  258.         case 'l':
  259.             PB |= FIBF_LINK;    /* another magic bit! */
  260.             break;
  261.         case 'C':
  262.         case 'c':
  263.             PB |= FIBF_COMMENT;    /* our magic bit! */
  264.             break;
  265.         case 'H':
  266.         case 'h':
  267.             PB |= FIBF_HOLD;
  268.             break;
  269.         case 'S':
  270.         case 's':
  271.             PB |= FIBF_SCRIPT;
  272.             break;
  273.         case 'P':
  274.         case 'p':
  275.             PB |= FIBF_PURE;
  276.             break;
  277.         case 'A':
  278.         case 'a':
  279.             PB |= FIBF_ARCHIVE;
  280.             break;
  281.         case 'R':
  282.         case 'r':
  283.             PB |= FIBF_READ;
  284.             break;
  285.         case 'W