00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #define MAIN
00041 #include "copyright.h"
00042 #undef MAIN
00043
00044 #include "main.h"
00045 #include "filesys.h"
00046 #include "openfile.h"
00047 #include "sysdep.h"
00048
00049 #ifdef TUT
00050
00051 #include "tut.h"
00052
00053 #include "tut_reporter.h"
00054 namespace tut
00055 {
00056 test_runner_singleton runner;
00057 }
00058
00059 #endif TUT
00060
00061
00062 Kernel *kernel;
00063 Debug *debug;
00064
00065
00066
00067
00068
00069
00070
00071 static void
00072 Cleanup(int x)
00073 {
00074 cerr << "\nCleaning up after signal " << x << "\n";
00075 delete kernel;
00076 }
00077
00078
00079
00080
00081
00082
00083 static const int TransferSize = 128;
00084
00085
00086 #ifndef FILESYS_STUB
00087
00088
00089
00090
00091
00092 static void
00093 Copy(char *from, char *to)
00094 {
00095 int fd;
00096 OpenFile* openFile;
00097 int amountRead, fileLength;
00098 char *buffer;
00099
00100
00101 if ((fd = OpenForReadWrite(from,FALSE)) < 0) {
00102 cerr <<"Copy: couldn't open input file"<< from << "\n";
00103 return;
00104 }
00105
00106
00107 Lseek(fd, 0, 2);
00108 fileLength = Tell(fd);
00109 Lseek(fd, 0, 0);
00110
00111
00112 DEBUG('f', "Copying file " << from << " of size " << fileLength << " to file " << to);
00113 if (!kernel->fileSystem->Create(to, fileLength)) {
00114 cerr<< "Copy: couldn't create output file"<< to << "\n";
00115 Close(fd);
00116 return;
00117 }
00118
00119 openFile = kernel->fileSystem->Open(to);
00120 ASSERT(openFile != NULL);
00121
00122
00123 buffer = new char[TransferSize];
00124 while ((amountRead=ReadPartial(fd, buffer, sizeof(char)*TransferSize)) > 0)
00125 openFile->Write(buffer, amountRead);
00126 delete [] buffer;
00127
00128
00129 delete openFile;
00130 Close(fd);
00131 }
00132
00133 #endif // FILESYS_STUB
00134
00135
00136
00137
00138
00139
00140 void
00141 Print(char *name)
00142 {
00143 OpenFile *openFile;
00144 int i, amountRead;
00145 char *buffer;
00146
00147 if ((openFile = kernel->fileSystem->Open(name)) == NULL) {
00148 cerr << "Print: unable to open file"<< name<< "\n";
00149 return;
00150 }
00151
00152 buffer = new char[TransferSize];
00153 while ((amountRead = openFile->Read(buffer, TransferSize)) > 0)
00154 for (i = 0; i < amountRead; i++)
00155 cerr << buffer[i];
00156 cerr <<"\n";
00157 delete [] buffer;
00158
00159 delete openFile;
00160 return;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 int
00180 main(int argc, char **argv)
00181 {
00182 int i;
00183 char *debugArg = "";
00184 char *userProgName = NULL;
00185 bool threadTestFlag = false;
00186 bool consoleTestFlag = false;
00187 bool networkTestFlag = false;
00188 #ifndef FILESYS_STUB
00189 char *copyUnixFileName = NULL;
00190 char *copyNachosFileName = NULL;
00191 char *printFileName = NULL;
00192 char *removeFileName = NULL;
00193 bool dirListFlag = false;
00194 bool dumpFlag = false;
00195 #endif //FILESYS_STUB
00196
00197
00198
00199
00200 for (i = 1; i < argc; i++) {
00201 if (strcmp(argv[i], "-d") == 0) {
00202 ASSERT(i + 1 < argc);
00203 debugArg = argv[i + 1];
00204 i++;
00205 }
00206 else if (strcmp(argv[i], "-z") == 0) {
00207 cerr << copyright << "\n";
00208 }
00209 else if (strcmp(argv[i], "-x") == 0) {
00210 ASSERT(i + 1 < argc);
00211 userProgName = argv[i + 1];
00212 i++;
00213 }
00214 else if (strcmp(argv[i], "-K") == 0) {
00215 threadTestFlag = TRUE;
00216 }
00217 else if (strcmp(argv[i], "-C") == 0) {
00218 consoleTestFlag = TRUE;
00219 }
00220 else if (strcmp(argv[i], "-N") == 0) {
00221 networkTestFlag = TRUE;
00222 }
00223 #ifndef FILESYS_STUB
00224 else if (strcmp(argv[i], "-cp") == 0) {
00225 ASSERT(i + 2 < argc);
00226 copyUnixFileName = argv[i + 1];
00227 copyNachosFileName = argv[i + 2];
00228 i += 2;
00229 }
00230 else if (strcmp(argv[i], "-p") == 0) {
00231 ASSERT(i + 1 < argc);
00232 printFileName = argv[i + 1];
00233 i++;
00234 }
00235 else if (strcmp(argv[i], "-r") == 0) {
00236 ASSERT(i + 1 < argc);
00237 removeFileName = argv[i + 1];
00238 i++;
00239 }
00240 else if (strcmp(argv[i], "-l") == 0) {
00241 dirListFlag = true;
00242 }
00243 else if (strcmp(argv[i], "-D") == 0) {
00244 dumpFlag = true;
00245 }
00246 #endif //FILESYS_STUB
00247 else if (strcmp(argv[i], "-u") == 0) {
00248 cerr << "Partial usage: nachos [-z -d debugFlags]\n";
00249 cerr<< "Partial usage: nachos [-x programName]\n";
00250 cerr << "Partial usage: nachos [-K] [-C] [-N]\n";
00251 #ifndef FILESYS_STUB
00252 cerr << "Partial usage: nachos [-cp UnixFile NachosFile]\n";
00253 cerr << "Partial usage: nachos [-p fileName] [-r fileName]\n";
00254 cerr << "Partial usage: nachos [-l] [-D]\n";
00255 #endif //FILESYS_STUB
00256 }
00257
00258 }
00259 debug = new Debug(debugArg);
00260
00261 DEBUG(dbgThread, "Entering main");
00262
00263 #ifdef TUT
00264 ::tut::callback * clbk = new tut::reporter(cerr);
00265 ::tut::runner.get().set_callback(clbk);
00266 ::tut::runner.get().run_tests();
00267 #endif
00268
00269 kernel = new Kernel(argc, argv);
00270
00271 kernel->Initialize();
00272
00273 CallOnUserAbort(Cleanup);
00274
00275
00276
00277 if (threadTestFlag) {
00278 kernel->ThreadSelfTest();
00279 }
00280 if (consoleTestFlag) {
00281 kernel->ConsoleTest();
00282 }
00283 if (networkTestFlag) {
00284 kernel->NetworkTest();
00285 }
00286
00287 #ifndef FILESYS_STUB
00288 if (removeFileName != NULL) {
00289 kernel->fileSystem->Remove(removeFileName);
00290 }
00291 if (copyUnixFileName != NULL && copyNachosFileName != NULL) {
00292 Copy(copyUnixFileName,copyNachosFileName);
00293 }
00294 if (dumpFlag) {
00295 kernel->fileSystem->Print();
00296 }
00297 if (dirListFlag) {
00298 kernel->fileSystem->List();
00299 }
00300 if (printFileName != NULL) {
00301 Print(printFileName);
00302 }
00303 #endif // FILESYS_STUB
00304
00305
00306 if (userProgName != NULL) {
00307 AddrSpace *space = new AddrSpace;
00308 ASSERT(space != (AddrSpace *)NULL);
00309 if (space->Load(userProgName)) {
00310 kernel->currentThread->space = space;
00311 space->Execute();
00312 ASSERTNOTREACHED();
00313 }
00314 }
00315
00316
00317
00318
00319
00320 kernel->interrupt->Halt();
00321
00322 ASSERTNOTREACHED();
00323 }
00324