Debian Codegraph @

src/dos/dos_devices.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: dos_devices.cpp,v 1.22 2009-05-27 09:15:41 qbix79 Exp $ */
20
21 #include <string.h>
22 #include "dosbox.h"
23 #include "callback.h"
24 #include "regs.h"
25 #include "mem.h"
26 #include "bios.h"
27 #include "dos_inc.h"
28 #include "support.h"
29 #include "drives.h" //Wildcmp
30 /* Include all the devices */
31
32 #include "dev_con.h"
33
34
35 DOS_Device * Devices[DOS_DEVICES];
36
37 class device_NUL : public DOS_Device {
38 public:
39 device_NUL() { SetName("NUL"); };
40 virtual bool Read(Bit8u * data,Bit16u * size) {
41 for(Bitu i = 0; i < *size;i++)
42 data[i]=0;
43 LOG(LOG_IOCTL,LOG_NORMAL)("%s:READ",GetName());
44 return true;
45 }
46 virtual bool Write(Bit8u * data,Bit16u * size) {
47 LOG(LOG_IOCTL,LOG_NORMAL)("%s:WRITE",GetName());
48 return true;
49 }
50 virtual bool Seek(Bit32u * pos,Bit32u type) {
51 LOG(LOG_IOCTL,LOG_NORMAL)("%s:SEEK",GetName());
52 return true;
53 }
54 virtual bool Close() { return true; }
55 virtual Bit16u GetInformation(void) { return 0x8084; }
56 virtual bool ReadFromControlChannel(PhysPt bufptr,Bit16u size,Bit16u * retcode){return false;}
57 virtual bool WriteToControlChannel(PhysPt bufptr,Bit16u size,Bit16u * retcode){return false;}
58 };
59
60 class device_LPT1 : public device_NUL {
61 public:
62 device_LPT1() { SetName("LPT1");}
63 Bit16u GetInformation(void) { return 0x80A0; }
64 };
65
66 bool DOS_Device::Read(Bit8u * data,Bit16u * size) {
67 return Devices[devnum]->Read(data,size);
68 }
69
70 bool DOS_Device::Write(Bit8u * data,Bit16u * size) {
71 return Devices[devnum]->Write(data,size);
72 }
73
74 bool DOS_Device::Seek(Bit32u * pos,Bit32u type) {
75 return Devices[devnum]->Seek(pos,type);
76 }
77
78 bool DOS_Device::Close() {
79 return Devices[devnum]->Close();
80 }
81
82 Bit16u DOS_Device::GetInformation(void) {
83 return Devices[devnum]->GetInformation();
84 }
85
86 bool DOS_Device::ReadFromControlChannel(PhysPt bufptr,Bit16u size,Bit16u * retcode) {
87 return Devices[devnum]->ReadFromControlChannel(bufptr,size,retcode);
88 }
89
90 bool DOS_Device::WriteToControlChannel(PhysPt bufptr,Bit16u size,Bit16u * retcode) {
91 return Devices[devnum]->WriteToControlChannel(bufptr,size,retcode);
92 }
93
94 DOS_File::DOS_File(const DOS_File& orig) {
95 flags=orig.flags;
96 time=orig.time;
97 date=orig.date;
98 attr=orig.attr;
99 refCtr=orig.refCtr;
100 open=orig.open;
101 hdrive=orig.hdrive;
102 name=0;
103 if(orig.name) {
104 name=new char [strlen(orig.name) + 1];strcpy(name,orig.name);
105 }
106 }
107
108 DOS_File & DOS_File::operator= (const DOS_File & orig) {
109 flags=orig.flags;
110 time=orig.time;
111 date=orig.date;
112 attr=orig.attr;
113 refCtr=orig.refCtr;
114 open=orig.open;
115 hdrive=orig.hdrive;
116 if(name) {
117 delete [] name; name=0;
118 }
119 if(orig.name) {
120 name=new char [strlen(orig.name) + 1];strcpy(name,orig.name);
121 }
122 return *this;
123 }
124
125 Bit8u DOS_FindDevice(char const * name) {
126 /* should only check for the names before the dot and spacepadded */
127 char fullname[DOS_PATHLENGTH];Bit8u drive;
128 // if(!name || !(*name)) return DOS_DEVICES; //important, but makename does it
129 if (!DOS_MakeName(name,fullname,&drive)) return DOS_DEVICES;
130
131 char* name_part = strrchr(fullname,'\\');
132 if(name_part) {
133 *name_part++ = 0;
134 //Check validity of leading directory.
135 if(!Drives[drive]->TestDir(fullname)) return DOS_DEVICES;
136 } else name_part = fullname;
137
138 char* dot = strrchr(name_part,'.');
139 if(dot) *dot = 0; //no ext checking
140
141 static char com[5] = { 'C','O','M','1',0 };
142 static char lpt[5] = { 'L','P','T','1',0 };
143 // AUX is alias for COM1 and PRN for LPT1
144 // A bit of a hack. (but less then before).
145 // no need for casecmp as makename returns uppercase
146 if (strcmp(name_part, "AUX") == 0) name_part = com;
147 if (strcmp(name_part, "PRN") == 0) name_part = lpt;
148
149 /* loop through devices */
150 for(Bit8u index = 0;index < DOS_DEVICES;index++) {
151 if (Devices[index]) {
152 if (WildFileCmp(name_part,Devices[index]->name)) return index;
153 }
154 }
155 return DOS_DEVICES;
156 }
157
158
159 void DOS_AddDevice(DOS_Device * adddev) {
160 //Caller creates the device. We store a pointer to it
161 //TODO Give the Device a real handler in low memory that responds to calls
162 for(Bitu i = 0; i < DOS_DEVICES;i++) {
163 if(!Devices[i]){
164 Devices[i] = adddev;
165 Devices[i]->SetDeviceNumber(i);
166 return;
167 }
168 }
169 E_Exit("DOS:Too many devices added");
170 }
171
172 void DOS_DelDevice(DOS_Device * dev) {
173 // We will destroy the device if we find it in our list.
174 // TODO:The file table is not checked to see the device is opened somewhere!
175 for (Bitu i = 0; i <DOS_DEVICES;i++) {
176 if(Devices[i] && !strcasecmp(Devices[i]->name,dev->name)){
177 delete Devices[i];
178 Devices[i] = 0;
179 return;
180 }
181 }
182 }
183
184 void DOS_SetupDevices(void) {
185 DOS_Device * newdev;
186 newdev=new device_CON();
187 DOS_AddDevice(newdev);
188 DOS_Device * newdev2;
189 newdev2=new device_NUL();
190 DOS_AddDevice(newdev2);
191 DOS_Device * newdev3;
192 newdev3=new device_LPT1();
193 DOS_AddDevice(newdev3);
194 }