home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 206.lha / Flist_v1.2 / Sources / parse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-28  |  9.1 KB  |  356 lines

  1. /*
  2. * parse routines for flist...
  3. *
  4. * uses arp functions.
  5. *
  6. */
  7.  
  8. #include <libraries/arpbase.h>
  9. #include <stdio.h>
  10. #include "flist.h"
  11.  
  12. /* Global stuff for ARP */
  13.  
  14. char *CLI_Template = ",FROM/K";
  15. char *CLI_Help = "Type filename(s) or FROM and a file with name of files";
  16.  
  17. /* global flist stuff */
  18.  
  19. struct AnchorPath *ap = NULL;
  20.  
  21. long (*finfo[MAXDIR])[4];  /* miscelaneous info -- file size,blocks,dir type etc... */
  22. char (*fname[MAXDIR])[FCHARS];     /* file name and comment */
  23. char (*comm[MAXDIR])[FCHARS];
  24. char (*fstring[MAXDIR])[LEN_DATSTRING];
  25. char (*ftime[MAXDIR])[LEN_DATSTRING];
  26.  
  27. struct FileLock *startdir = NULL, *lock = NULL, *olddir = NULL;
  28.  
  29. long numfiles,getcurrentdir();
  30.  
  31. long parse(argc,argv)
  32. long argc;
  33. char *argv[];
  34. {
  35.     long i,rc = -99;
  36.     
  37.     for (i=0;i<MAXDIR;i++)  /* zero out the memory arrays */
  38.         fname[i] = 0;
  39.  
  40.     ap = (struct AnchorPath *)ArpAllocMem(sizeof(struct AnchorPath),FAST);
  41.  
  42.     if(ap == NULL)
  43.         return OUT_OF_MEMORY;
  44.  
  45.     ap->ap_BreakBits = SIGBREAKB_CTRL_C;
  46.     ap->ap_Length = 0;
  47.  
  48.     if((argv[1] != 0) && (argc > 0)){
  49.         if(!getcurrentdir(argv[1]))
  50.             return -98;
  51.         strcat(argv[1],"*");
  52.         rc = FindFirst(argv[1],ap);
  53.         if(rc == NULL)
  54.             rc = Fillarray();
  55.     }
  56.     else{ 
  57.         if ((argc > 1) && (argv[2] != NULL))
  58.             rc = other_input(argv[2]);
  59.         else{
  60.             if(argc == 0)  
  61.                 rc = Wbargs();   /* do we have Workbench? */
  62.             else{
  63.                 if(!getcurrentdir(""))
  64.                         return -98;
  65.                 rc = FindFirst("*",ap); /* user only typed 'flist' */
  66.                 if(rc == NULL)          /* so we are going to give him the */
  67.                     rc = Fillarray();   /* current directory */
  68.             }
  69.         }
  70.     }
  71.     return rc;    /* return code should always be sent */
  72. }
  73.  
  74. FreeFlistMem()
  75. {
  76.     if(ap != NULL )
  77.         FreeAnchorChain(ap);
  78. }
  79.  
  80. /* Stuff all the filenames and pertinent information into */
  81. /* the flist arrays */
  82.  
  83. long Fillarray()
  84. {
  85.     long rc = NULL;
  86.     struct DateTime dt;
  87.     int i;
  88.  
  89.     numfiles = 0;
  90.  
  91.     dt.dat_Format = FORMAT_DOS;
  92.     dt.dat_StrDay = NULL;
  93.     dt.dat_Flags = DTB_SUBST;
  94.  
  95.     while(rc != ERROR_NO_MORE_ENTRIES){
  96.  
  97.         if(get_array_mem(numfiles) == OUT_OF_MEMORY)
  98.                 return OUT_OF_MEMORY;
  99.                 
  100.         strcpy(fname[numfiles],&ap->ap_Info.fib_FileName);
  101.         strcpy(comm[numfiles],&ap->ap_Info.fib_Comment); 
  102.         dt.dat_Stamp = ap->ap_Info.fib_Date;
  103.         dt.dat_StrDate = fstring[numfiles];
  104.         dt.dat_StrTime = ftime[numfiles];
  105.  
  106.         (*finfo[numfiles])[0] = ap->ap_Info.fib_DirEntryType;
  107.         (*finfo[numfiles])[1] = ap->ap_Info.fib_Protection;
  108.         (*finfo[numfiles])[2] = ap->ap_Info.fib_Size;
  109.         (*finfo[numfiles])[3] = ap->ap_Info.fib_NumBlocks;
  110.  
  111.         if((rc = StamptoStr(&dt)) != NULL)
  112.                 break;
  113.  
  114.         rc = FindNext(ap);
  115.  
  116.         if( ++numfiles > MAXDIR || rc != NULL)
  117.             break;
  118.     }
  119.  
  120.     for (i=numfiles;i<MAXDIR;i++)   /* free all array memory not being used */
  121.         if (fname[i] != 0) {
  122.             FreeMem(fname[i],FCHARS);
  123. /*            FreeMem(comm[i],FCHARS); */
  124.             FreeMem(finfo[i],16L);
  125.             FreeMem(fstring[i],LEN_DATSTRING);
  126.             FreeMem(ftime[i],LEN_DATSTRING);
  127.             fname[i] = 0;
  128.             finfo[i] = 0;
  129.             fstring[i] = 0;
  130.             ftime[i] = 0;
  131.         }
  132.  
  133.     if(rc == ERROR_NO_MORE_ENTRIES)
  134.         return NULL;
  135.     return rc;
  136. }
  137.  
  138. /* The user has not supplied any filenames or input for the File */
  139. /* template. Here we check for the optional FROM keyword. */
  140.  
  141. long other_input(argv)
  142. char *argv[];
  143. {
  144.  
  145.     FILE *fp;
  146.     char *buffer;
  147.     struct DateTime dt;
  148.     register long rc;
  149.     int i;
  150.  
  151.     if ((buffer = (char *)ArpAllocMem(LINESIZE,FAST)) == NULL)
  152.         return OUT_OF_MEMORY;
  153.  
  154.     if ((fp = fopen(argv,"r")) == NULL)
  155.         return -99L;
  156.  
  157.     if (!getcurrentdir(""))
  158.         return -98L;
  159.  
  160.     dt.dat_Format = FORMAT_USA;
  161.     dt.dat_StrDay = NULL;
  162.     dt.dat_Flags = DTB_SUBST;
  163.  
  164.     numfiles = 0;
  165.     while (feof(fp) == NULL){
  166.  
  167.         if(get_array_mem(numfiles) == OUT_OF_MEMORY)
  168.                 return OUT_OF_MEMORY;
  169.                 
  170.         fscanf(fp,"%s",buffer);
  171.         rc = FindFirst(buffer,ap);
  172.         if (rc != NULL )
  173.             break;
  174.  
  175.         strcpy(fname[numfiles],buffer);
  176.         strcpy(comm[numfiles],&ap->ap_Info.fib_Comment); 
  177.         (*finfo[numfiles])[0] = ap->ap_Info.fib_DirEntryType;
  178.         (*finfo[numfiles])[1] = ap->ap_Info.fib_Protection;
  179.         (*finfo[numfiles])[2] = ap->ap_Info.fib_Size;
  180.         (*finfo[numfiles])[3] = ap->ap_Info.fib_NumBlocks;
  181.         dt.dat_Stamp = ap->ap_Info.fib_Date;
  182.         dt.dat_StrDate = fstring[numfiles];
  183.         dt.dat_StrTime = ftime[numfiles];
  184.  
  185.         if((rc = StamptoStr(&dt)) != NULL)
  186.                 break;
  187.  
  188.         if( ++numfiles > MAXDIR | rc != NULL)
  189.             break;
  190.         }
  191.  
  192.     if (feof(fp) == NULL){
  193.         if (rc == NULL)
  194.             rc = (LONG)ferror(fp);
  195.     }
  196.     else
  197.         numfiles--;
  198.  
  199.     for (i=numfiles;i<MAXDIR;i++)   /* free all array memory not being used */
  200.         if (fname[i] != 0) {
  201.             FreeMem(fname[i],FCHARS);
  202. /*            FreeMem(comm[i],FCHARS); */
  203.             FreeMem(finfo[i],16L);
  204.             FreeMem(fstring[i],LEN_DATSTRING);
  205.             FreeMem(ftime[i],LEN_DATSTRING);
  206.             fname[i] = 0;
  207.             finfo[i] = 0;
  208.             fstring[i] = 0;
  209.             ftime[i] = 0;
  210.         }
  211.     fclose(fp);
  212.     return rc;        
  213. }
  214.  
  215. long get_array_mem(index)
  216. long    index;
  217. {
  218.  
  219. /* If the memory array already has a block allocated to it return to 
  220.    caller... otherwise allocate one */
  221.  
  222.     if (fname[index] != 0L)
  223.         return 0L;
  224.     
  225.     fname[index] =      (char *)AllocMem(FCHARS,FAST);
  226. /*    comm[index]  =      (char *)AllocMem(FCHARS,FAST); */
  227.     finfo[index] =      (LONG *)AllocMem(16L,FAST);
  228.     fstring[index] =    (char *)AllocMem(LEN_DATSTRING,FAST);
  229.     ftime[index] =      (char *)AllocMem(LEN_DATSTRING,FAST);
  230.  
  231.     if( ftime[index] == NULL | fstring[index] == NULL \
  232.     | fname[index] == NULL | /* comm[index] == NULL | */ finfo[index] == NULL )
  233.         return OUT_OF_MEMORY;
  234.     else
  235.         return 0L;
  236. }
  237.  
  238. /*
  239. ** more Arp stuff for Workbench support
  240. */
  241.  
  242. BYTE dir[DSIZE], name[FCHARS];
  243.  
  244. getarp(mask,fib)
  245. ULONG mask;
  246. struct FileInfoBlock *fib;
  247. {
  248.  
  249.     long rc;
  250.     struct DateTime dt;
  251.  
  252.     dt.dat_Format = FORMAT_USA;
  253.     dt.dat_StrDay = NULL;
  254.     dt.dat_Flags = DTB_SUBST;
  255.  
  256.     strcpy(fname[numfiles],&fib->fib_FileName);
  257.     strcpy(comm[numfiles],&fib->fib_Comment); 
  258.     (*finfo[numfiles])[0] = fib->fib_DirEntryType;
  259.     (*finfo[numfiles])[1] = fib->fib_Protection;
  260.     (*finfo[numfiles])[2] = fib->fib_Size;
  261.     (*finfo[numfiles])[3] = fib->fib_NumBlocks;
  262.     dt.dat_Stamp = ap->ap_Info.fib_Date;
  263.     dt.dat_StrDate = fstring[numfiles];
  264.     dt.dat_StrTime = ftime[numfiles];
  265.  
  266.     if((rc = StamptoStr(&dt)) != NULL)
  267.         return rc;
  268.  
  269.     numfiles++;
  270.  
  271.     return 0L;    /* tell Arp to put this entry in the requester */
  272.  
  273. /* ok, this is the fun part ... cheath told me that there
  274.    is a bug in the handling of the return code. this is the fix. */
  275. #asm
  276.     move.l #0,d0      ; d0 MUST contain the return code 
  277.     move.l d0,4(sp)   ; 4(sp) is the part that gets munged to the 
  278.                       ; correct value 
  279. #endasm
  280. }
  281.  
  282. struct FileRequester arpfr = {
  283.                                      /* Hailing text */
  284.     (BYTE *)"Select a directory, then click on OK.", 
  285.     NULL,                            /* string to put the final name */
  286.     NULL,                            /* string to put the directory entry */
  287.     NULL,                            /* Window for the files */
  288.     NULL,                            /* what calls getarp? */
  289.     NULL,                            /* reserved */
  290.     NULL,                            /* function to be called */
  291.     NULL                             /* reserved */
  292. };
  293.  
  294. long Wbargs()   /* use the arp file requester to get the file names */
  295. {
  296.     long dummy;
  297.     char dname[FCHARS];
  298.  
  299.     strcpy(dir,"RAM:");
  300.     strcpy(dname,"Select directory only.");
  301.  
  302.     arpfr.fr_Dir = dir;
  303.     arpfr.fr_File = dname;
  304.  
  305.     dummy = FileRequest(&arpfr);
  306.  
  307.     if(dummy == NULL)   /* user selects cancel -- so abort */
  308.         return 1;
  309.  
  310.     olddir = lock;      /* save this for later */
  311.  
  312.     if(strcmp(dir,"") == 0)
  313.         getcurrentdir("");
  314.     else {
  315.         lock = Lock(dir,ACCESS_READ);
  316.         if(lock == NULL) {
  317.             auto_req("Can't find requested directory.");
  318.             return 1;
  319.         }
  320.     }
  321.  
  322.     if (startdir == NULL)
  323.         startdir = CurrentDir(lock);
  324.     else
  325.         CurrentDir(lock);
  326.  
  327.     TackOn(dir,"*");
  328.     dummy = FindFirst(dir,ap);
  329.  
  330.     if(dummy == NULL)
  331.        dummy = Fillarray();
  332.  
  333.     return (dummy == NULL || dummy == ERROR_NO_MORE_ENTRIES) ? 0 : 1;
  334. }
  335.  
  336. long getcurrentdir(str)
  337. char *str;
  338. {
  339.     /* This is the only way I have ever been able to reliably get the
  340.        current directory */
  341.     
  342.     /* Save the Value of the Directory we are in now. */
  343.  
  344.     if (str[0] == '\0'){
  345.         struct Process *proc = FindTask(NULL);
  346.  
  347.         lock = DupLock(proc->pr_CurrentDir);
  348.         startdir = CurrentDir(lock);
  349.         return TRUE;
  350.     } else {
  351.         lock = Lock(str);
  352.         startdir = CurrentDir(lock);
  353.         return (startdir == 0) ? FALSE : TRUE;
  354.     }
  355. }
  356.