1 /* 2 * lftp - file transfer program 3 * 4 * Copyright (c) 1996-2012 by Alexander V. Lukyanov (lav@yars.free.net) 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef JOB_H 21 #define JOB_H 22 23 #include <stdarg.h> 24 #include "trio.h" 25 #undef printf 26 27 #include "SMTask.h" 28 #include "StatusLine.h" 29 #include "fg.h" 30 #include "FileAccess.h" 31 32 #define JobRef SMTaskRef // it is basically the same 33 34 class Job : public SMTask 35 { 36 static void SortJobs(); 37 38 xlist<Job> all_jobs_node; 39 static xlist_head<Job> all_jobs; 40 41 xlist_head<Job> children_jobs; 42 xlist<Job> children_jobs_node; 43 44 protected: 45 bool fg; 46 Ref<FgData> fg_data; 47 48 void PrepareToDie(); 49 virtual ~Job(); 50 51 public: 52 int jobno; 53 Job *parent; 54 55 xarray<Job*> waiting; 56 57 void AddWaiting(Job *); AddWaiting(const JobRef<T> & r)58 template<class T> void AddWaiting(const JobRef<T>& r) { AddWaiting(r.get_non_const()); } 59 void RemoveWaiting(const Job *); 60 void ReplaceWaiting(Job *from,Job *to); 61 62 void SetParent(Job *j); 63 void SetParentFg(Job *j, bool f=true) 64 { 65 SetParent(j); 66 if(f && j->fg) 67 Fg(); 68 // else if(f && !j->fg) 69 // Bg(); 70 } 71 72 void AllocJobno(); 73 74 void PrintStatus(int,const char *prefix="\t"); 75 virtual xstring& FormatStatus(xstring& s,int v,const char *prefix="\t") { return s; } 76 virtual xstring& FormatShortStatus(xstring& s); 77 virtual void ShowRunStatus(const SMTaskRef<StatusLine>&); ClearStatus()78 void ClearStatus() 79 { 80 eprintf("%s",""); /* just "" causes a -Wformat-zero-length" warning */ 81 } SayFinal()82 virtual void SayFinal() {}; // final phrase of fg job 83 virtual int Done()=0; ExitCode()84 virtual int ExitCode() { return 0; } 85 virtual int Do()=0; 86 virtual int AcceptSig(int); 87 virtual void Bg(); 88 virtual void Fg(); 89 90 xstring cmdline; 91 GetCmdLine()92 virtual const xstring& GetCmdLine() { return cmdline?cmdline:xstring::get_tmp("?",1); } 93 xstring& FormatJobTitle(xstring& s,int indent=0,const char *suffix=0); 94 xstring& FormatOneJob(xstring& s,int verbose,int indent=0,const char *suffix=0); 95 xstring& FormatOneJobRecursively(xstring& s,int verbose,int indent=0); 96 virtual xstring& FormatJobs(xstring& s,int verbose,int indent=0); 97 98 void PrintJobTitle(int indent=0,const char *suffix=0); 99 void ListOneJob(int verbose,int indent=0,const char *suffix=0); 100 void ListOneJobRecursively(int verbose,int indent); 101 102 void ListDoneJobs(); 103 void BuryDoneJobs(); 104 105 Job *FindAnyChild(); 106 bool WaitsFor(Job *); 107 static Job *FindWhoWaitsFor(Job *); 108 bool CheckForWaitLoop(Job *parent); NumAwaitedJobs()109 int NumAwaitedJobs() { return waiting.count(); } 110 Job *FindDoneAwaitedJob(); 111 void WaitForAllChildren(); 112 void AllWaitingFg(); 113 114 static int NumberOfJobs(); 115 int NumberOfChildrenJobs(); 116 static Job *FindJob(int n); Running(int n)117 static bool Running(int n) 118 { 119 Job *j=FindJob(n); 120 return j && !j->Done(); 121 } 122 void Kill(int n); 123 static void Kill(Job*); 124 void SendSig(int n,int sig); 125 static void KillAll(); 126 static void Cleanup(); 127 128 void vfprintf(FILE *file,const char *fmt,va_list v); 129 // CmdExec redefines this, and traps all messages of its children. 130 virtual void top_vfprintf(FILE *file,const char *fmt,va_list v); 131 132 // C-like functions calling vfprintf 133 void eprintf(const char *fmt,...) PRINTF_LIKE(2,3); 134 void fprintf(FILE *file,const char *fmt,...) PRINTF_LIKE(3,4); 135 void printf(const char *fmt,...) PRINTF_LIKE(2,3); 136 void perror(const char *); 137 void puts(const char *); 138 139 Job(); 140 GetConnectURL()141 virtual const char *GetConnectURL() { return 0; } lftpMovesToBackground()142 virtual void lftpMovesToBackground() { Resume(); } 143 static void lftpMovesToBackground_ToAll(); 144 145 virtual off_t GetBytesCount(); 146 virtual double GetTimeSpent(); 147 virtual double GetTransferRate(); 148 149 void WaitDone(); 150 }; 151 152 class SessionJob : public Job 153 { 154 protected: SessionJob(FileAccess * s)155 SessionJob(FileAccess *s) : session(s) {} Clone()156 FileAccess *Clone() const { return session->Clone(); } 157 158 public: 159 FileAccessRef session; 160 161 xstring& FormatStatus(xstring&,int,const char *); GetConnectURL()162 const char *GetConnectURL() 163 { 164 if(!session) 165 return 0; 166 return session->GetConnectURL(); 167 } 168 void Fg(); 169 void Bg(); 170 }; 171 172 173 #endif /* JOB_H */ 174