Added PSharedptr class
[pwlib.git] / src / ptlib / msos / msdos.cxx
blobef2a4a7c15015419c1907089f1fa34bb10bf0d96
1 /*
2 * msdos.cxx
4 * General class implementation for MS-DOS
6 * Portable Windows Library
8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25 * All Rights Reserved.
27 * Contributor(s): ______________________________________.
29 * $Log$
30 * Revision 1.13 1998/09/24 03:30:50 robertj
31 * Added open software license.
33 * Revision 1.12 1996/01/28 02:55:32 robertj
34 * WIN16 support.
36 * Revision 1.11 1995/08/24 12:41:00 robertj
37 * Changed PChannel so not a PContainer.
39 * Revision 1.10 1995/06/17 00:59:21 robertj
40 * Moved PPipeChannel::Execute from common dos/windows to individual files.
42 * Revision 1.9 1995/04/01 08:06:03 robertj
43 * Fixed yield for straight DOS and QUICKWIN systems.
45 * Revision 1.8 1995/03/12 05:00:05 robertj
46 * Re-organisation of DOS/WIN16 and WIN32 platforms to maximise common code.
47 * Used built-in equate for WIN32 API (_WIN32).
49 * Revision 1.7 1994/12/13 11:53:44 robertj
50 * Added missing PConfig Construct() function for pure DOS.
52 * Revision 1.6 1994/10/30 11:25:36 robertj
53 * Fixed DOS version of configuration files.
55 * Revision 1.5 1994/08/22 00:18:02 robertj
56 * Added dummy socket function.
58 * Revision 1.4 1994/07/27 06:00:10 robertj
59 * Backup
61 * Revision 1.3 1994/07/17 11:01:04 robertj
62 * Ehancements, implementation, bug fixes etc.
64 * Revision 1.2 1994/07/02 03:18:09 robertj
65 * Multi-threading implementation.
67 * Revision 1.1 1994/06/25 12:13:01 robertj
68 * Initial revision
71 #include "ptlib.h"
73 #include <bios.h>
74 #include <fcntl.h>
77 ///////////////////////////////////////////////////////////////////////////////
78 // PTime
80 PString PTime::GetTimeSeparator()
82 return "";
86 BOOL PTime::GetTimeAMPM()
88 return FALSE;
92 PString PTime::GetTimeAM()
94 return "am";
98 PString PTime::GetTimePM()
100 return "pm";
104 PString PTime::GetDayName(Weekdays dayOfWeek, NameType type)
106 static const char * const weekdays[] = {
107 "Sunday", "Monday", "Tuesday", "Wednesday",
108 "Thursday", "Friday", "Saturday"
110 static const char * const abbrev_weekdays[] = {
111 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
113 return (type != FullName ? abbrev_weekdays : weekdays)[dayOfWeek];
117 PString PTime::GetDateSeparator()
119 return "-";
123 PString PTime::GetMonthName(Months month, NameType type)
125 static const char * const months[] = { "",
126 "January", "February", "March", "April", "May", "June",
127 "July", "August", "September", "October", "November", "December"
129 static const char * const abbrev_months[] = {
130 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
131 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
133 return (type != FullName ? abbrev_months : months)[month];
137 PTime::DateOrder PTime::GetDateOrder()
139 return DayMonthYear;
143 long PTime::GetTimeZone() const
145 return 0;
149 PString PTime::GetTimeZoneString(TimeZoneType type) const
151 return "";
156 ///////////////////////////////////////////////////////////////////////////////
157 // PSerialChannel
159 void PSerialChannel::Construct()
161 biosParm = 0xe3; // 9600 baud, no parity, 1 stop bit, 8 data bits
165 PString PSerialChannel::GetName() const
167 if (IsOpen())
168 return psprintf("COM%i", os_handle+1);
170 return PString();
174 BOOL PSerialChannel::Read(void * buf, PINDEX len)
176 char * b = (char *)buf;
177 while (len > 0) {
178 int c = ReadChar();
179 if (c >= 0) {
180 *b++ = (char)c;
181 len--;
184 return len == 0;
188 BOOL PSerialChannel::Write(const void * buf, PINDEX len)
190 const char * b = (const char *)buf;
191 while (len-- > 0) {
192 if (!WriteChar(*b++))
193 return FALSE;
195 return TRUE;
199 BOOL PSerialChannel::Close()
201 if (!IsOpen())
202 return FALSE;
204 os_handle = -1;
205 return TRUE;
209 BOOL PSerialChannel::SetCommsParam(DWORD speed, BYTE data, Parity parity,
210 BYTE stop, FlowControl inputFlow, FlowControl outputFlow)
212 switch (speed) {
213 case 0 :
214 break;
215 case 110 :
216 biosParm &= 0x1f;
217 break;
218 case 150 :
219 biosParm &= 0x1f;
220 biosParm |= 0x20;
221 break;
222 case 300 :
223 biosParm &= 0x1f;
224 biosParm |= 0x40;
225 break;
226 case 600 :
227 biosParm &= 0x1f;
228 biosParm |= 0x60;
229 break;
230 case 1200 :
231 biosParm &= 0x1f;
232 biosParm |= 0x80;
233 break;
234 case 2400 :
235 biosParm &= 0x1f;
236 biosParm |= 0xa0;
237 break;
238 case 4800 :
239 biosParm &= 0x1f;
240 biosParm |= 0xc0;
241 break;
242 case 9600 :
243 biosParm &= 0x1f;
244 biosParm |= 0xe0;
245 break;
246 default :
247 return FALSE;
250 switch (data) {
251 case 0 :
252 break;
253 case 5 :
254 biosParm &= 0xfc;
255 break;
256 case 6 :
257 biosParm &= 0xfc;
258 biosParm |= 1;
259 break;
260 case 7 :
261 biosParm &= 0xfc;
262 biosParm |= 2;
263 break;
264 case 8 :
265 biosParm &= 0xfc;
266 biosParm |= 3;
267 break;
268 default :
269 return FALSE;
272 switch (parity) {
273 case DefaultParity :
274 break;
275 case NoParity :
276 biosParm &= 0xe7;
277 break;
278 case OddParity :
279 biosParm &= 0xe7;
280 biosParm |= 8;
281 break;
282 case EvenParity :
283 biosParm &= 0xe7;
284 biosParm |= 0x10;
285 break;
286 default :
287 return FALSE;
290 switch (stop) {
291 case 0 :
292 break;
293 case 1 :
294 biosParm &= ~4;
295 break;
296 case 2 :
297 biosParm |= 4;
298 break;
299 default :
300 return FALSE;
303 if (outputFlow != DefaultFlowControl || inputFlow != DefaultFlowControl)
304 return FALSE;
306 _bios_serialcom(_COM_INIT, os_handle, biosParm);
307 return TRUE;
310 BOOL PSerialChannel::Open(const PString & port, DWORD speed, BYTE data,
311 Parity parity, BYTE stop, FlowControl inputFlow, FlowControl outputFlow)
313 Close();
315 os_handle = -1;
316 if (PCaselessString("COM") != port.Left(3) &&
317 port[3] >= '1' && port[3] <= '4')
318 return FALSE;
319 os_handle = port[3] - '1';
320 return SetCommsParam(speed, data, parity, stop, inputFlow, outputFlow);
324 BOOL PSerialChannel::SetSpeed(DWORD speed)
326 return SetCommsParam(speed,
327 0, DefaultParity, 0, DefaultFlowControl, DefaultFlowControl);
331 DWORD PSerialChannel::GetSpeed() const
333 static int speed[8] = { 110, 150, 300, 600, 1200, 2400, 4800, 9600 };
334 return speed[biosParm>>5];
338 BOOL PSerialChannel::SetDataBits(BYTE data)
340 return SetCommsParam(0,
341 data, DefaultParity, 0, DefaultFlowControl, DefaultFlowControl);
345 BYTE PSerialChannel::GetDataBits() const
347 return (BYTE)((biosParm&3)+5);
351 BOOL PSerialChannel::SetParity(Parity parity)
353 return SetCommsParam(0,0, parity, 0, DefaultFlowControl, DefaultFlowControl);
357 PSerialChannel::Parity PSerialChannel::GetParity() const
359 return (biosParm&8) == 0 ? NoParity :
360 (biosParm&0x10) == 0 ? OddParity : EvenParity;
364 BOOL PSerialChannel::SetStopBits(BYTE stop)
366 return SetCommsParam(0,
367 0, DefaultParity, stop, DefaultFlowControl, DefaultFlowControl);
371 BYTE PSerialChannel::GetStopBits() const
373 return (BYTE)(((biosParm&4)>>3)+1);
377 BOOL PSerialChannel::SetInputFlowControl(FlowControl flowControl)
379 return SetCommsParam(0,0, DefaultParity, 0, flowControl, DefaultFlowControl);
383 PSerialChannel::FlowControl PSerialChannel::GetInputFlowControl() const
385 return RtsCts;
389 BOOL PSerialChannel::SetOutputFlowControl(FlowControl flowControl)
391 return SetCommsParam(0,0, DefaultParity, 0, DefaultFlowControl, flowControl);
395 PSerialChannel::FlowControl PSerialChannel::GetOutputFlowControl() const
397 return RtsCts;
401 void PSerialChannel::SetDTR(BOOL state)
403 if (!IsOpen())
404 return;
409 void PSerialChannel::SetRTS(BOOL state)
411 if (!IsOpen())
412 return;
417 void PSerialChannel::SetBreak(BOOL state)
419 if (!IsOpen())
420 return;
422 int s = state;
426 BOOL PSerialChannel::GetCTS()
428 if (!IsOpen())
429 return FALSE;
431 return (_bios_serialcom(_COM_STATUS, os_handle, 0)&0x8010) == 0x10;
435 BOOL PSerialChannel::GetDSR()
437 if (!IsOpen())
438 return FALSE;
440 return (_bios_serialcom(_COM_STATUS, os_handle, 0)&0x8020) == 0x20;
444 BOOL PSerialChannel::GetDCD()
446 if (!IsOpen())
447 return FALSE;
449 return (_bios_serialcom(_COM_STATUS, os_handle, 0)&0x8080) == 0x80;
453 BOOL PSerialChannel::GetRing()
455 if (!IsOpen())
456 return FALSE;
458 return (_bios_serialcom(_COM_STATUS, os_handle, 0)&0x8040) == 0x40;
462 PStringList PSerialChannel::GetPortNames()
464 static char buf[] = "COM ";
465 PStringList ports;
466 for (char p = '1'; p <= '4'; p++) {
467 if (*(WORD *)(0x00400000+p-'1') != 0) {
468 buf[3] = p;
469 ports.Append(new PString(buf));
472 return ports;
476 ///////////////////////////////////////////////////////////////////////////////
477 // PPipeChannel
479 BOOL PPipeChannel::Execute()
481 if (hasRun)
482 return FALSE;
484 flush();
485 if (os_handle >= 0) {
486 _close(os_handle);
487 os_handle = -1;
490 if (!ConvertOSError(system(subProgName)))
491 return FALSE;
493 if (!fromChild.IsEmpty()) {
494 os_handle = _open(fromChild, _O_RDONLY);
495 if (!ConvertOSError(os_handle))
496 return FALSE;
499 return TRUE;
503 ///////////////////////////////////////////////////////////////////////////////
504 // Configuration files
506 void PConfig::Construct(Source src)
508 switch (src) {
509 case Application :
510 PFilePath appFile = PProcess::Current()->GetFile();
511 location = appFile.GetVolume() +
512 appFile.GetPath() + appFile.GetTitle() + ".INI";
517 void PConfig::Construct(const PFilePath & file)
519 location = file;
523 PStringList PConfig::GetSections()
525 PStringList sections;
527 if (!location.IsEmpty()) {
528 PAssertAlways(PUnimplementedFunction);
531 return sections;
535 PStringList PConfig::GetKeys(const PString &) const
537 PStringList keys;
539 if (location.IsEmpty()) {
540 char ** ptr = _environ;
541 while (*ptr != NULL) {
542 PString buf = *ptr++;
543 keys.AppendString(buf.Left(buf.Find('=')));
546 else {
547 PAssertAlways(PUnimplementedFunction);
550 return keys;
554 void PConfig::DeleteSection(const PString &)
556 if (location.IsEmpty())
557 return;
559 PAssertAlways(PUnimplementedFunction);
563 void PConfig::DeleteKey(const PString &, const PString & key)
565 if (location.IsEmpty()) {
566 PAssert(key.Find('=') == P_MAX_INDEX, PInvalidParameter);
567 _putenv(key + "=");
569 else
570 PAssertAlways(PUnimplementedFunction);
574 PString PConfig::GetString(const PString &,
575 const PString & key, const PString & dflt)
577 PString str;
579 if (location.IsEmpty()) {
580 PAssert(key.Find('=') == P_MAX_INDEX, PInvalidParameter);
581 char * env = getenv(key);
582 if (env != NULL)
583 str = env;
584 else
585 str = dflt;
587 else {
588 PAssertAlways(PUnimplementedFunction);
590 return str;
594 void PConfig::SetString(const PString &, const PString & key, const PString & value)
596 if (location.IsEmpty()) {
597 PAssert(key.Find('=') == P_MAX_INDEX, PInvalidParameter);
598 _putenv(key + "=" + value);
600 else
601 PAssertAlways(PUnimplementedFunction);
606 ///////////////////////////////////////////////////////////////////////////////
607 // Threads
609 void PThread::SwitchContext(PThread * from)
611 if (setjmp(from->context) != 0) // Are being reactivated from previous yield
612 return;
614 if (status == Starting) {
615 if (setjmp(context) != 0) {
616 status = Running;
617 Main();
618 Terminate(); // Never returns from here
620 context[7] = (int)stackTop-16; // Change the stack pointer in jmp_buf
623 longjmp(context, TRUE);
624 PAssertAlways("longjmp failed"); // Should never get here
629 ///////////////////////////////////////////////////////////////////////////////
630 // PDynaLink
632 PDynaLink::PDynaLink()
634 PAssertAlways(PUnimplementedFunction);
638 PDynaLink::PDynaLink(const PString &)
640 PAssertAlways(PUnimplementedFunction);
644 PDynaLink::~PDynaLink()
649 BOOL PDynaLink::Open(const PString & name)
651 PAssertAlways(PUnimplementedFunction);
652 return FALSE;
656 void PDynaLink::Close()
661 BOOL PDynaLink::IsLoaded() const
663 return FALSE;
667 BOOL PDynaLink::GetFunction(PINDEX index, Function & func)
669 return FALSE;
673 BOOL PDynaLink::GetFunction(const PString & name, Function & func)
675 return FALSE;
680 // End Of File ///////////////////////////////////////////////////////////////