... yes, me again ...

I've been experimenting with the idea I presented of an explicit queue feeder.
It looks like it'd work rather well.

The most obvious benefit is robust queue editing.  Moving queued items around
doesn't break cached pwd/lpwd.  The jobs list could (optionally) display the
pwd/lpwd for each job, or just show when it changes; users don't need to see
the generated "cd" and "lcd" commands.

A less obvious, but possibly just as significant benefit: it pulls a few queue
variables out of CmdExec.  The pwd and lpwd caches move to QueueFeeder. 
is_queue goes away since we can just search for the feeder type.

Are you deliberately avoiding rtti, or has it not been used due to lack of
application?  I'm using it to search for QueueFeeders.  It'd mean an extra
override for feeders to avoid this, and that's what RTTI is for ...

This is a more significant change than the others, so it's a judgement call
if you want to take the patch or not.  It turned out to be much easier than
I thought it'd be.  I've attached the patch as-is for feedback.  It's not
done--it breaks "jobs" (since it feeds commands one at a time), lacks actual
queue editing (and, incidentally, queue --edit, for the same reason as jobs)
and isn't tested well enough.

Fixing jobs will probably involve letting feeders have a pass at jobs outputs;
this will allow showing feeder-specific data (ie pwd/lpwd), and could
potentially allow optimizing FileFeeder, too (if it's a seekable stream, and
the user asks for the *whole* queue, that can be done straight from disk.) I'd
probably replace queue editing with a small set of editing commands (delete,
move, change pwd/lpwd, a queue flag to insert a command at a given position.)
It's possible to keep direct queue editing for this, but it'd probably not be
worth the work.

Awaiting feedback before working on this further.  I've attached
QueueFeeder.{cc,h}; my diff is being glitchy and isn't taking -N for some
reason.  If you want to run it, -fno-rtti needs to be removed, too (that's not
in this patch since that'd also include my other configure.in patch.)

-- 
Glenn Maynard
Index: CmdExec.cc
===================================================================
RCS file: /home/lav/cvsroot/lftp/src/CmdExec.cc,v
retrieving revision 1.78
diff -u -r1.78 CmdExec.cc
--- CmdExec.cc  2001/07/19 10:15:28     1.78
+++ CmdExec.cc  2001/07/24 00:32:40
@@ -25,6 +25,7 @@
 #include <unistd.h>
 #include <ctype.h>
 #include <fcntl.h>
+#include <typeinfo>
 #include "CmdExec.h"
 #include "xmalloc.h"
 #include "xstring.h"
@@ -34,6 +35,7 @@
 #include "ResMgr.h"
 #include "module.h"
 #include "url.h"
+#include "QueueFeeder.h"
 
 #define RL_PROMPT_START_IGNORE '\001'
 #define RL_PROMPT_END_IGNORE   '\002'
@@ -827,8 +829,6 @@
    redirections=0;
 
    is_queue=false;
-   queue_cwd=0;
-   queue_lcwd=0;
 
    saved_session=0;
 
@@ -866,9 +866,6 @@
    if(args_glob)
       delete args_glob;
 
-   xfree(queue_cwd);
-   xfree(queue_lcwd);
-
    Reuse(saved_session);
 
    Delete(status_line);
@@ -1378,27 +1375,43 @@
    xfree(cmd);
 }
 
-CmdExec *CmdExec::GetQueue()
+/* return the CmdExec containing a queue feeder; create if necessary */
+CmdExec  *CmdExec::GetQueue()
 {
    for(CmdExec *scan=chain; scan; scan=scan->next)
    {
       if(scan->is_queue
       && !strcmp(this->session->GetConnectURL(FA::NO_PATH),
-                scan->session->GetConnectURL(FA::NO_PATH)))
-        return scan;
+                scan->session->GetConnectURL(FA::NO_PATH))) {
+        QueueFeeder *f = scan->GetQueueFeeder();
+        if(f != 0) return scan;
+      }
    }
 
+   // parent->SetCmdFeeder(new FileFeeder(new 
+FileStream(args->getarg(1),O_RDONLY|O_ASCII)));
+   
    CmdExec *queue=new CmdExec(session->Clone());
+   QueueFeeder *queuef=new QueueFeeder(session->GetCwd(), cwd);
+
    queue->SetParentFg(this,false);
    queue->AllocJobno();
    const char *url=session->GetConnectURL(FA::NO_PATH);
    queue->cmdline=(char*)xmalloc(9+strlen(url));
    sprintf(queue->cmdline,"queue (%s)",url);
    queue->is_queue=true;
-   queue->queue_cwd=xstrdup(session->GetCwd());
-   queue->queue_lcwd=xstrdup(cwd);
+   queue->SetCmdFeeder(queuef);
 
 //   assert(FindQueue()==queue);
 
    return queue;
+}
+
+QueueFeeder *CmdExec::GetQueueFeeder() const
+{
+   /* find the queue feeder */
+   for(CmdFeeder *f = feeder; f; f=f->prev) {
+      if(typeid(*f) == typeid(QueueFeeder)) 
+        return dynamic_cast<QueueFeeder *>(f);
+   }
+   return 0;
 }
Index: CmdExec.h
===================================================================
RCS file: /home/lav/cvsroot/lftp/src/CmdExec.h,v
retrieving revision 1.41
diff -u -r1.41 CmdExec.h
--- CmdExec.h   2001/07/19 10:15:28     1.41
+++ CmdExec.h   2001/07/24 00:32:40
@@ -32,6 +32,8 @@
 #include "history.h"
 #include "bookmark.h"
 
+class QueueFeeder;
+
 extern History cwd_history;
 extern Bookmark lftp_bookmarks;
 
@@ -130,9 +132,8 @@
    CmdExec *next;
 
    bool is_queue;
-   char *queue_cwd;
-   char *queue_lcwd;
    CmdExec *GetQueue();
+   QueueFeeder *GetQueueFeeder() const;
 
    FileAccess *saved_session;
    void ReuseSavedSession();
Index: Makefile.am
===================================================================
RCS file: /home/lav/cvsroot/lftp/src/Makefile.am,v
retrieving revision 1.39
diff -u -r1.39 Makefile.am
--- Makefile.am 2001/06/18 13:47:26     1.39
+++ Makefile.am 2001/07/24 00:32:42
@@ -60,7 +60,7 @@
  mvJob.cc mvJob.h alias.cc alias.h\
  bookmark.cc bookmark.h CatJob.cc CatJob.h GetJob.cc GetJob.h\
  mkdirJob.cc mkdirJob.h pgetJob.cc pgetJob.h FileFeeder.cc FileFeeder.h\
- keyvalue.cc keyvalue.h history.cc history.h\
+ QueueFeeder.cc QueueFeeder.h keyvalue.cc keyvalue.h history.cc history.h\
  SleepJob.cc SleepJob.h getdate.y getdate.h FindJob.cc FindJob.h\
  ChmodJob.cc ChmodJob.h TreatFileJob.cc TreatFileJob.h CopyJob.cc CopyJob.h
 
Index: commands.cc
===================================================================
RCS file: /home/lav/cvsroot/lftp/src/commands.cc,v
retrieving revision 1.131
diff -u -r1.131 commands.cc
--- commands.cc 2001/07/20 08:19:20     1.131
+++ commands.cc 2001/07/24 00:32:48
@@ -63,6 +63,7 @@
 #include "getopt.h"
 #include "FileCopy.h"
 #include "DummyProto.h"
+#include "QueueFeeder.h"
 
 #include "confpaths.h"
 
@@ -895,13 +896,14 @@
       return builtin_queue_edit();
 
    if(src != NULL) {
+          /*
       int q_fd=open(src,O_RDONLY);
       if(q_fd==-1) {
         eprintf(_("%s: %s: %s\n"), args->a0(), src, strerror(errno));
         return 0;
       }
 
-      CmdExec *queue=GetQueue();
+      QueueFeeder *queue=GetQueue();
       if(!queue->ReadCmds(q_fd)) {
         eprintf(_("%s: error reading %s: %s\n"), args->a0(), src, strerror(errno));
       }
@@ -909,6 +911,7 @@
       close(q_fd);
 
       return 0;
+      */
    }
 
    if(args->count()==1)
@@ -918,26 +921,16 @@
    }
 
    CmdExec *queue=GetQueue();
-   if(xstrcmp(queue->queue_cwd,session->GetCwd()))
-   {
-      queue->FeedCmd("cd ");
-      queue->FeedQuoted(session->GetCwd());
-      queue->FeedCmd(" &\n");
-
-      xfree(queue->queue_cwd);
-      queue->queue_cwd=xstrdup(session->GetCwd());
-   }
-   if(xstrcmp(queue->queue_lcwd,cwd))
-   {
-      queue->FeedCmd("lcd ");
-      queue->FeedQuoted(cwd);
-      queue->FeedCmd(" &\n");
+   QueueFeeder *queuef=queue->GetQueueFeeder();
+   char *cmd;
 
-      xfree(queue->queue_lcwd);
-      queue->queue_lcwd=xstrdup(cwd);
-   }
+   if(args->count() == 2)
+          cmd=args->Combine(1);
+   else
+          cmd=args->CombineQuoted(1);
 
-   queue->FeedArgV(args,1);
+   queuef->QueueCmd(cmd, session->GetCwd(), cwd);
+   xfree(cmd);
 
    last_bg=queue->jobno;
 
/*
 * lftp and utils
 *
 * Copyright (c) 1996-1997 by Alexander V. Lukyanov ([EMAIL PROTECTED])
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <config.h>
#include <errno.h>
#include <unistd.h>
#include "QueueFeeder.h"

const char *QueueFeeder::NextCmd(CmdExec *exec, const char *)
{
   if(jobs == NULL) return NULL;
  
   /* delink the first job */
   QueueJob *job = jobs;
   jobs = jobs->link;
   if(job == lastjob) lastjob = NULL;
   job->link = NULL;

   int bufsize = 1;
   buffer = (char *) xrealloc(buffer, bufsize);
   buffer[0] = 0;
   
   if(cur_pwd == NULL || strcmp(cur_pwd, job->pwd)) {
      bufsize += strlen(job->pwd) + 5;
      buffer = (char *) xrealloc(buffer, bufsize);
      sprintf(buffer+strlen(buffer), "cd %s; ", job->pwd);
      xfree(cur_pwd);
      cur_pwd = strdup(job->pwd);
   }

   if(cur_lpwd == NULL || strcmp(cur_lpwd, job->lpwd)) {
      bufsize += strlen(job->lpwd) + 6;
      buffer = (char *) xrealloc(buffer, bufsize);
      sprintf(buffer+strlen(buffer), "lcd %s; ", job->lpwd);
      xfree(cur_lpwd);
      cur_lpwd = strdup(job->lpwd);
   }

   bufsize += strlen(job->cmd) + 1;
   buffer = (char *) xrealloc(buffer, bufsize);
   strcat(buffer, job->cmd);
   strcat(buffer, "\n");

   delete job;

   return buffer;
}

void QueueFeeder::QueueCmd(const char *cmd, const char *pwd, const char *lpwd)
{
   QueueJob *job = new QueueJob;
   job->cmd = strdup(cmd);
   job->pwd = strdup(pwd);
   job->lpwd = strdup(lpwd);

   /* we never want a newline at the end: */
   if(job->cmd[strlen(job->cmd)-1] == '\n')
      job->cmd[strlen(job->cmd)-1] = 0;

   if(lastjob != NULL) {
      lastjob->link = job;
      lastjob = job;
   } else {
      jobs = lastjob = job;
   }
      
}
   
/*
 * lftp and utils
 *
 * Copyright (c) 1996-1997 by Alexander V. Lukyanov ([EMAIL PROTECTED])
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef QUEUEFEEDER_H
#define QUEUEFEEDER_H

#include "CmdExec.h"

class QueueFeeder : public CmdFeeder
{
   struct QueueJob {
      char *cmd;
      char *pwd, *lpwd;

      QueueJob *link;
      QueueJob(): cmd(0), pwd(0), lpwd(0), link(0) { }
   } *jobs, *lastjob;
   char *cur_pwd, *cur_lpwd;

   char *buffer;

public:
   const char *NextCmd(CmdExec *exec,const char *prompt);
   void QueueCmd(const char *cmd, const char *cwd, const char *lcwd);

   QueueFeeder(const char *pwd, const char *lpwd):
      jobs(0), lastjob(0), cur_pwd(0), cur_lpwd(0), buffer(0)
   {
      if(pwd != 0)
	 cur_pwd = strdup(pwd);
      if(lpwd != 0)
	 cur_lpwd = strdup(lpwd);
   }
   virtual ~QueueFeeder()
   {
   }
};

#endif//QUEUEFEEDER_H

Reply via email to