~peter-pearse/ubuntu/natty/lftp/prop001

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * lftp and utils
 *
 * Copyright (c) 1996-2007 by Alexander V. Lukyanov (lav@yars.free.net)
 *
 * 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 3 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.
 */

/* $Id: Job.h,v 1.36 2009/08/13 09:42:44 lav Exp $ */

#ifndef JOB_H
#define JOB_H

#include <stdarg.h>
#include "trio.h"
#undef printf

#include "SMTask.h"
#include "StatusLine.h"
#include "fg.h"
#include "FileAccess.h"

#define JobRef SMTaskRef // it is basically the same

class Job : public SMTask
{
   static void SortJobs();

   Job  *next;
   static Job *chain;

protected:
   bool fg;
   Ref<FgData> fg_data;

   bool job_prepared_to_die;
   void PrepareToDie();
   virtual ~Job();

public:
   int	 jobno;
   Job	 *parent;

   xarray<Job*> waiting;

   void AddWaiting(Job *);
   template<class T> void AddWaiting(const JobRef<T>& r) { AddWaiting(r.get_non_const()); }
   void RemoveWaiting(const Job *);
   void ReplaceWaiting(Job *from,Job *to);

   void SetParent(Job *j) { parent=j; }
   void SetParentFg(Job *j, bool f=true)
      {
	 SetParent(j);
	 if(f && j->fg)
	    Fg();
//	 else if(f && !j->fg)
//	    Bg();
      }

   void	 AllocJobno();

   virtual void	PrintStatus(int,const char *prefix="\t") {};
   virtual void	ShowRunStatus(const SMTaskRef<StatusLine>&);
   void ClearStatus()
      {
	 const char *empty="";
	 eprintf(empty);
      }
   virtual void	  SayFinal() {}; // final phrase of fg job
   virtual int	  Done()=0;
   virtual int	  ExitCode() { return 0; }
   virtual int	  Do()=0;
   virtual int	  AcceptSig(int);
   virtual void	  Bg();
   virtual void	  Fg();

   xstring cmdline;
   virtual void ListJobs(int verbose_level,int indent=0);
   void PrintJobTitle(int indent=0,const char *suffix=0);
   void ListOneJob(int verbose,int indent=0,const char *suffix=0);
   void ListOneJobRecursively(int verbose,int indent);
   void ListDoneJobs();
   void BuryDoneJobs();
   Job *FindAnyChild();
   bool WaitsFor(Job *);
   static Job *FindWhoWaitsFor(Job *);
   bool CheckForWaitLoop(Job *parent);
   int NumAwaitedJobs() { return waiting.count(); }
   Job *FindDoneAwaitedJob();
   void WaitForAllChildren();
   void AllWaitingFg();

   static int NumberOfJobs();
   static Job *FindJob(int n);
   static bool Running(int n)
   {
      Job *j=FindJob(n);
      return j && !j->Done();
   }
   void Kill(int n);
   static void Kill(Job*);
   void SendSig(int n,int sig);
   static void KillAll();
   static void Cleanup();

   void vfprintf(FILE *file,const char *fmt,va_list v);
   // CmdExec redefines this, and traps all messages of its children.
   virtual void top_vfprintf(FILE *file,const char *fmt,va_list v);

   // C-like functions calling vfprintf
   void eprintf(const char *fmt,...) PRINTF_LIKE(2,3);
   void fprintf(FILE *file,const char *fmt,...) PRINTF_LIKE(3,4);
   void printf(const char *fmt,...) PRINTF_LIKE(2,3);
   void perror(const char *);
   void puts(const char *);

   Job();

   virtual const char *GetConnectURL() { return 0; }
   virtual void lftpMovesToBackground() { Resume(); }
   static  void lftpMovesToBackground_ToAll();

   virtual off_t GetBytesCount() { return 0; }
   virtual double GetTimeSpent() { return 0; }

   void WaitDone();
};

class SessionJob : public Job
{
protected:
   SessionJob(FileAccess *s) : session(s) {}
   FileAccess *Clone() const { return session->Clone(); }

public:
   FileAccessRef session;

   void PrintStatus(int v,const char *);
   const char *GetConnectURL()
      {
	 if(!session)
	    return 0;
	 return session->GetConnectURL();
      }
   void Fg();
   void Bg();
};


#endif /* JOB_H */