forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsklnx.cpp
More file actions
3601 lines (3200 loc) · 115 KB
/
dsklnx.cpp
File metadata and controls
3601 lines (3200 loc) · 115 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode 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 LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "lnxprefix.h"
#include "osspec.h"
#include "globdefs.h"
#include "filedefs.h"
#include "objdefs.h"
#include "parsedef.h"
#include "mcio.h"
#include "system.h"
#include "foundation.h"
#include "object.h"
#include "stack.h"
#include "card.h"
#include "mcerror.h"
//#include "execpt.h"
//#include "param.h"
#include "handler.h"
#include "util.h"
#include "globals.h"
//#include "ports.cpp"
#include "socket.h"
//#include "mcssl.h"
//#include "securemode.h"
#include "mode.h"
#include "player.h"
#include "text.h"
#include "variable.h"
#include <sys/utsname.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/dir.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/statvfs.h>
#include <dlfcn.h>
#include <termios.h>
//#include <langinfo.h>
#include <locale.h>
#include <pwd.h>
#include <fcntl.h>
// SN-2014-12-17: [[ Bug 14220 ]] Server should not wait put rather poll
#include <poll.h>
#include <libgnome/gnome-url.h>
#include <libgnome/gnome-program.h>
#include <libgnome/gnome-init.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libgnomevfs/gnome-vfs-mime.h>
#include <libgnomevfs/gnome-vfs-mime-handlers.h>
#include <gtk/gtk.h>
////////////////////////////////////////////////////////////////////////////////
// This is in here so we do not need GLIBC2.4
extern "C" void __attribute__ ((noreturn)) __stack_chk_fail (void)
{
abort();
}
////////////////////////////////////////////////////////////////////////////////
// MW-2005-02-22: Make global for now so opensslsocket.cpp can access it
real8 curtime;
static Boolean alarmpending;
// MW-2013-10-08: [[ Bug 11259 ]] We use our own tables on linux since
// we use a fixed locale which isn't available on all systems.
#if !defined(_LINUX_SERVER) && !defined(_LINUX_DESKTOP)
uint1 *MClowercasingtable = NULL;
uint1 *MCuppercasingtable = NULL;
#endif
//
////////////////////////////////////////////////////////////////////////////////
static void parseSerialControlStr(char *setting, struct termios *theTermios)
{
int baud = 0;
char *type = setting;
char *value = NULL;
if ((value = strchr(type, '=')) != NULL)
{
*value++ = '\0';
if (MCU_strncasecmp(type, "baud", strlen(type)) == 0)
{
long baudrate = strtol(value, NULL, 10);
if (baudrate == 57600)
baud = B57600;
else if (baudrate == 38400)
baud = B38400;
else if (baudrate == 19200)
baud = B19200;
else if (baudrate == 9600)
baud = B9600;
else if (baudrate == 4800)
baud = B4800;
else if (baudrate == 3600)
baud = B4800;
else if (baudrate == 2400)
baud = B2400;
else if (baudrate == 1800)
baud = B1800;
else if (baudrate == 1200)
baud = B1200;
else if (baudrate == 600)
baud = B600;
else if (baudrate == 300)
baud = B300;
cfsetispeed(theTermios, baud);
cfsetospeed(theTermios, baud);
}
else if (MCU_strncasecmp(type, "parity", strlen(type)) == 0)
{
if (value[0] == 'N' || value[0] == 'n')
theTermios->c_cflag &= ~(PARENB | PARODD);
else if (value[0] == 'O' || value[0] == 'o')
theTermios->c_cflag |= PARENB | PARODD;
else if (value[0] == 'E' || value[0] == 'e')
theTermios->c_cflag |= PARENB;
}
else if (MCU_strncasecmp(type, "data", strlen(type)) == 0)
{
short data = atoi(value);
switch (data)
{
case 5:
theTermios->c_cflag |= CS5;
break;
case 6:
theTermios->c_cflag |= CS6;
break;
case 7:
theTermios->c_cflag |= CS7;
break;
case 8:
theTermios->c_cflag |= CS8;
break;
}
}
else if (MCU_strncasecmp(type, "stop", strlen(type)) == 0)
{
double stopbit = strtol(value, NULL, 10);
if (stopbit == 1.0)
theTermios->c_cflag &= ~CSTOPB;
else if (stopbit == 1.5)
theTermios->c_cflag &= ~CSTOPB;
else if (stopbit == 2.0)
theTermios->c_cflag |= CSTOPB;
}
}
}
static void configureSerialPort(int sRefNum)
{/****************************************************************************
*parse MCserialcontrolstring and set the serial output port to the settings*
*defined by MCserialcontrolstring accordingly *
****************************************************************************/
//initialize to the default setting
struct termios theTermios;
if (tcgetattr(sRefNum, &theTermios) < 0)
{
MCLog("Error getting terminous attributes", nil);
}
cfsetispeed(&theTermios, B9600);
theTermios.c_cflag = CS8;
MCAutoStringRefAsSysString t_serial_settings;
/* UNCHECKED */ t_serial_settings.Lock(MCserialcontrolsettings);
char *controlptr = strclone(*t_serial_settings);
char *str = controlptr;
char *each = NULL;
while ((each = strchr(str, ' ')) != NULL)
{
*each = '\0';
each++;
parseSerialControlStr(str, &theTermios);
str = each;
}
//configure the serial output device
parseSerialControlStr(str,&theTermios);
if (tcsetattr(sRefNum, TCSANOW, &theTermios) < 0)
{
MCLog("Error setting terminous attributes", nil);
}
delete[] controlptr;
return;
}
////////////////////////////////////////////////////////////////////////////////
static IO_stat MCS_lnx_shellread(int fd, char *&buffer, uint4 &buffersize, uint4 &size)
{
MCshellfd = fd;
size = 0;
while (True)
{
int readsize = 0;
ioctl(fd, FIONREAD, (char *)&readsize);
readsize += READ_PIPE_SIZE;
if (size + readsize > buffersize)
{
MCU_realloc((char **)&buffer, buffersize,
buffersize + readsize + 1, sizeof(char));
buffersize += readsize;
}
errno = 0;
int4 amount = read(fd, &buffer[size], readsize);
if (amount <= 0)
{
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
break;
MCU_play();
// SN-2014-12-17: [[ Bug 14220 ]] The server wasn't waiting pre-7.0
#ifdef _SERVER
pollfd t_poll_fd;
t_poll_fd . fd = fd;
t_poll_fd . events = POLLIN;
t_poll_fd . revents = 0;
// SN-2015-02-12: [[ Bug 14441 ]] poll might as well get signal interrupted
// and we don't want to miss the reading for that only reason.
int t_result;
do
{
t_result = poll(&t_poll_fd, 1, -1);
}
while (t_result != 1 ||
(errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK));
// SN-2015-02-26: [[ CID 37859 ]] Dead code removed (t_result is
// always different from -1 here
#else
if (MCscreen->wait(READ_INTERVAL, False, True))
{
MCshellfd = -1;
return IO_ERROR;
}
#endif
}
else
size += amount;
}
MCshellfd = -1;
return IO_NORMAL;
}
////////////////////////////////////////////////////////////////////////////////
static Boolean MCS_lnx_nodelay(int4 fd)
{
return fcntl(fd, F_SETFL, (fcntl(fd, F_GETFL, 0) & O_APPEND) | O_NONBLOCK)
>= 0;
}
////////////////////////////////////////////////////////////////////////////////
bool MCS_lnx_is_link(MCStringRef p_path)
{
#ifdef /* MCS_is_link_dsk_lnx */ LEGACY_SYSTEM_ORPHAN
struct stat64 buf;
return (lstat64(MCStringGetCString(p_path), &buf) == 0 && S_ISLNK(buf.st_mode));
#endif /* MCS_is_link_dsk_lnx */
MCAutoStringRefAsSysString t_path;
/* UNCHECKED */ t_path.Lock(p_path);
struct stat64 buf;
return (lstat64(*t_path, &buf) == 0 && S_ISLNK(buf.st_mode));
}
bool MCS_lnx_readlink(MCStringRef p_path, MCStringRef& r_link)
{
#ifdef /* MCS_readlink_dsk_lnx */ LEGACY_SYSTEM_ORPHAN
struct stat64 t_stat;
ssize_t t_size;
MCAutoNativeCharArray t_buffer;
if (lstat64(MCStringGetCString(p_path), &t_stat) == -1 ||
!t_buffer.New(t_stat.st_size))
return false;
t_size = readlink(MCStringGetCString(p_path), (char*)t_buffer.Chars(), t_stat.st_size);
return (t_size == t_stat.st_size) && t_buffer.CreateStringAndRelease(r_link);
#endif /* MCS_readlink_dsk_lnx */
struct stat64 t_stat;
ssize_t t_size;
MCAutoArray<char> t_buffer;
MCAutoStringRefAsSysString t_path;
/* UNCHECKED */ t_path.Lock(p_path);
if (lstat64(*t_path, &t_stat) == -1 ||
// SN-2014-09-02: [[ Bug 13323 ]] The size needs be 1 bigger to allow
// a final NIL-byte.
!t_buffer.New(t_stat.st_size + 1))
return false;
t_size = readlink(*t_path, (char*)t_buffer.Ptr(), t_stat.st_size);
if (t_size != t_stat.st_size)
return false;
return MCStringCreateWithSysString(t_buffer.Ptr(), r_link);
}
////////////////////////////////////////////////////////////////////////////////
// REFACTORED FROM lnxmisc.cpp
#include <iconv.h>
struct ConverterRecord
{
ConverterRecord *next;
const char *encoding;
iconv_t converter;
};
static iconv_t fetch_converter(const char *p_encoding)
{
static ConverterRecord *s_records = NULL;
ConverterRecord *t_previous, *t_current;
for(t_previous = NULL, t_current = s_records; t_current != NULL; t_previous = t_current, t_current = t_current -> next)
if (t_current -> encoding == p_encoding)
break;
if (t_current == NULL)
{
// SN-2015-11-19: [[ Bug 16450 ]] Ask for UTF-16LE to avoid iconv to put
// a BOM char at the start of the stream
iconv_t t_converter;
t_converter = iconv_open("UTF-16LE", p_encoding);
ConverterRecord *t_record;
t_record = new ConverterRecord;
t_record -> next = s_records;
t_record -> encoding = p_encoding;
t_record -> converter = t_converter;
s_records = t_record;
return t_record -> converter;
}
if (t_previous != NULL)
{
t_previous -> next = t_current -> next;
t_current -> next = s_records;
s_records = t_current;
}
return s_records -> converter;
}
////////////////////////////////////////////////////////////////////////////////
#define DNS_SCRIPT "repeat for each line l in url \"binfile:/etc/resolv.conf\";\
if word 1 of l is \"nameserver\" then put word 2 of l & cr after it; end repeat;\
delete last char of it; return it"
#define LAUNCH_URL_SCRIPT "put \"%@\" into tFilename; " \
"put empty into tCmd; " \
"if tCmd is empty and shell(\"which xdg-open\") is not empty then; " \
" put \"xdg-open\" into tCmd; "\
"end if; " \
"if tCmd is empty and $GNOME_DESKTOP_SESSION_ID is not empty and shell(\"which gnome-open\") is not empty then; " \
" put \"gnome-open\" into tCmd; " \
"end if; " \
"if tCmd is empty and $KDE_FULL_SESSION is not empty then; " \
" if shell(\"which kde-open\") is not empty then; " \
" put \"kde-open\" into tCmd; " \
" else if shell(\"which kfmclient\") is not empty then; " \
" put \"kfmclient exec\" into tCmd; " \
" end if; " \
"end if; " \
"if tCmd is not empty then; " \
" launch tFilename with tCmd; " \
"else; " \
" return \"not supported\"; " \
"end if;"
////////////////////////////////////////////////////////////////////////////////
#ifdef NOATON
int inet_aton(const char *cp, struct in_addr *inp)
{
unsigned long rv = inet_addr(cp);
//fprintf(stderr, "%d\n", rv);
if (rv == -1)
return False;
memcpy(inp, &rv, sizeof(unsigned long));
return True;
}
#endif
// TS-2008-03-03 : Make this global so that MCS_checkprocesses () can look at it
// and figure out if one of its children have already been waited on in the
// SIGCHLD handler.
pid_t waitedpid;
static void handle_signal(int sig)
{
MCHandler handler(HT_MESSAGE);
switch (sig)
{
case SIGUSR1:
MCsiguser1++;
break;
case SIGUSR2:
MCsiguser2++;
break;
case SIGTERM:
switch (MCdefaultstackptr->getcard()->message(MCM_shut_down_request))
{
case ES_NORMAL:
return;
case ES_NOT_HANDLED:
if (handler.getpass())
{
MCdefaultstackptr->getcard()->message(MCM_shut_down);
MCquit = True; //set MC quit flag, to invoke quitting
return;
}
default:
break;
}
case SIGILL:
case SIGBUS:
case SIGSEGV:
{
MCAutoStringRefAsSysString t_cmd;
/* UNCHECKED */ t_cmd.Lock(MCcmd);
fprintf(stderr, "%s exiting on signal %d\n", *t_cmd, sig);
MCS_killall();
exit(-1);
}
case SIGHUP:
case SIGINT:
case SIGQUIT:
case SIGIOT:
fprintf(stderr,"\n\nGot SIGIOT\n");
if (MCnoui)
exit(1);
MCabortscript = True;
break;
case SIGFPE:
errno = EDOM;
break;
case SIGCHLD:
{
#if defined(_LINUX_DESKTOP)
MCPlayer *tptr = MCplayers;
// If we have some players waiting then deal with these first
waitedpid = -1;
if ( tptr != NULL)
{
waitedpid = wait(NULL);
// Moving these two lines half fixes bug 5966 - however it still isn't quite right
// as there will still be some interaction between a player and shell command
while(tptr != NULL)
{
if ( waitedpid == tptr -> getpid())
{
if (tptr->isdisposable())
tptr->playstop();
else
MCscreen->delaymessage(tptr, MCM_play_stopped, NULL, NULL);
tptr->shutdown();
break;
}
tptr = tptr -> getnextplayer() ;
}
}
else
{
// Check to see if we have created a video window. If we have got to here it means
// that we could not start mplayer -- so the child thread has exited, but the player
// object has not had a chance to be created yet. TODO - investigate if there is a
// cleaner way of dealing with this situation.
if ( MClastvideowindow != DNULL )
{
gdk_window_hide(MClastvideowindow);
gdk_window_destroy(MClastvideowindow);
MClastvideowindow = DNULL ;
}
else
{
MCS_checkprocesses();
}
}
#endif /* LINUX_DESKTOP */
}
break;
case SIGALRM:
MCalarm = True;
#ifdef NOITIMERS
alarm(1);
#endif
break;
case SIGPIPE:
default:
break;
}
return;
}
////////////////////////////////////////////////////////////////////////////////
bool MCS_generate_uuid(char p_buffer[128])
{
typedef void (*uuid_generate_ptr)(unsigned char uuid[16]);
typedef void (*uuid_unparse_ptr)(unsigned char uuid[16], char *out);
static void *s_uuid_generate = NULL, *s_uuid_unparse = NULL;
if (s_uuid_generate == NULL && s_uuid_unparse == NULL)
{
void *t_libuuid;
t_libuuid = dlopen("libuuid.so", RTLD_LAZY);
if (t_libuuid == NULL)
t_libuuid = dlopen("libuuid.so.1", RTLD_LAZY);
if (t_libuuid != NULL)
{
s_uuid_generate = dlsym(t_libuuid, "uuid_generate");
s_uuid_unparse = dlsym(t_libuuid, "uuid_unparse");
}
}
if (s_uuid_generate != NULL && s_uuid_unparse != NULL)
{
unsigned char t_uuid[16];
((uuid_generate_ptr)s_uuid_generate)(t_uuid);
((uuid_unparse_ptr)s_uuid_unparse)(t_uuid, p_buffer);
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
class MCStdioFileHandle: public MCSystemFileHandle
{
public:
MCStdioFileHandle(FILE* p_fptr)
{
m_fptr = p_fptr;
m_is_eof = false;
}
virtual void Close(void)
{
#ifdef /* MCS_close_dsk_lnx */ LEGACY_SYSTEM
if (stream->fptr == NULL)
{
if (stream->fd == 0)
{
if (!(stream->flags & IO_FAKE))
delete stream->buffer;
}
#ifndef NOMMAP
else
{
munmap((char *)stream->buffer, stream->len);
close(stream->fd);
}
#endif
}
else
fclose(stream->fptr);
delete stream;
stream = NULL;
return IO_NORMAL;
#endif /* MCS_close_dsk_lnx */
if (m_fptr != NULL)
fclose(m_fptr);
delete this;
}
// Returns true if an attempt has been made to read past the end of the
// stream.
virtual bool IsExhausted(void)
{
return feof(m_fptr);
}
virtual bool Read(void *p_buffer, uint32_t p_length, uint32_t& r_read)
{
#ifdef /* MCS_read_dsk_lnx */ LEGACY_SYSTEM
if (MCabortscript || ptr == NULL)
return IO_ERROR;
if ((stream -> flags & IO_FAKEWRITE) == IO_FAKEWRITE)
return IO_ERROR;
// MW-2009-06-25: If this is a custom stream, call the appropriate callback.
// MW-2009-06-30: Refactored to common (platform-independent) implementation
// in mcio.cpp
if ((stream -> flags & IO_FAKECUSTOM) == IO_FAKECUSTOM)
return MCS_fake_read(ptr, size, n, stream);
IO_stat stat = IO_NORMAL;
if (stream->fptr == NULL)
{
uint4 nread = size * n;
if (nread > stream->len - (stream->ioptr - stream->buffer))
{
n = (stream->len - (stream->ioptr - stream->buffer)) / size;
nread = (stream->len - (stream->ioptr - stream->buffer)) / size;
stat = IO_EOF;
}
if (nread == 1)
{
char *tptr = (char *)ptr;
*tptr = *stream->ioptr++;
}
else
{
memcpy(ptr, stream->ioptr, nread);
stream->ioptr += nread;
}
}
else
{
char *sptr = (char *)ptr;
uint4 nread;
uint4 toread = n * size;
uint4 offset = 0;
errno = 0;
while ((nread = fread(&sptr[offset], 1, toread, stream->fptr)) != toread)
{
offset += nread;
n = offset / size;
if (ferror(stream->fptr))
{
clearerr(stream->fptr);
if (errno == EAGAIN)
return IO_NORMAL;
if (errno == EINTR)
{
toread -= nread;
continue;
}
else
return IO_ERROR;
}
if (MCS_eof(stream))
{
return IO_EOF;
}
return IO_NONE;
}
}
return stat;
#endif /* MCS_read_dsk_lnx */
if (MCabortscript || p_buffer == NULL)
return false;
char *sptr = (char *)p_buffer;
uint4 nread;
uint4 toread = p_length;
uint4 offset = 0;
errno = 0;
while ((nread = fread(&sptr[offset], 1, toread, m_fptr)) != toread)
{
offset += nread;
r_read = offset;
if (ferror(m_fptr))
{
clearerr(m_fptr);
if (errno == EAGAIN)
return true;
if (errno == EINTR)
{
toread -= nread;
continue;
}
}
return false;
}
r_read = offset + nread;
return true;
}
virtual bool Write(const void *p_buffer, uint32_t p_length)
{
#ifdef /* MCS_write_dsk_lnx */ LEGACY_SYSTEM
if ((stream -> flags & IO_FAKEWRITE) == IO_FAKEWRITE)
return MCU_dofakewrite(stream -> buffer, stream -> len, ptr, size, n);
if (fwrite(ptr, size, n, stream->fptr) != n)
return IO_ERROR;
return IO_NORMAL;
#endif /* MCS_write_dsk_lnx */
if (fwrite(p_buffer, 1, p_length, m_fptr) != p_length)
return false;
return true;
}
virtual bool Seek(int64_t p_offset, int p_dir)
{
#ifdef /* MCS_seek_cur_dsk_lnx */ LEGACY_SYSTEM
// MW-2009-06-25: If this is a custom stream, call the appropriate callback.
// MW-2009-06-30: Refactored to common implementation in mcio.cpp.
if ((stream -> flags & IO_FAKECUSTOM) == IO_FAKECUSTOM)
return MCS_fake_seek_cur(stream, offset);
if (stream->fptr == NULL)
IO_set_stream(stream, stream->ioptr + offset);
else
if (fseeko64(stream->fptr, offset, SEEK_CUR) != 0)
return IO_ERROR;
return IO_NORMAL;
#endif /* MCS_seek_cur_dsk_lnx */
#ifdef /* MCS_seek_set_dsk_lnx */ LEGACY_SYSTEM
// MW-2009-06-30: If this is a custom stream, call the appropriate callback.
if ((stream -> flags & IO_FAKECUSTOM) == IO_FAKECUSTOM)
return MCS_fake_seek_set(stream, offset);
if (stream->fptr == NULL)
IO_set_stream(stream, stream->buffer + offset);
else
if (fseeko64(stream->fptr, offset, SEEK_SET) != 0)
return IO_ERROR;
return IO_NORMAL;
#endif /* MCS_seek_set_dsk_lnx */
#ifdef /* MCS_seek_end_dsk_lnx */ LEGACY_SYSTEM
if (stream->fptr == NULL)
IO_set_stream(stream, stream->buffer + stream->len + offset);
else
if (fseeko64(stream->fptr, offset, SEEK_END) != 0)
return IO_ERROR;
return IO_NORMAL;
#endif /* MCS_seek_end_dsk_lnx */
return fseeko64(m_fptr, p_offset, p_dir > 0 ? SEEK_SET : (p_dir < 0 ? SEEK_END : SEEK_CUR)) == 0;
}
virtual bool Truncate(void)
{
#ifdef /* MCS_trunc_dsk_lnx */ LEGACY_SYSTEM
if (ftruncate(fileno(stream->fptr), ftell(stream->fptr)))
return IO_ERROR;
return IO_NORMAL;
#endif /* MCS_trunc_dsk_lnx */
return ftruncate(fileno(m_fptr), ftell(m_fptr)) == 0;
}
virtual bool Sync(void)
{
#ifdef /* MCS_sync_dsk_lnx */ LEGACY_SYSTEM
if (stream->fptr != NULL)
{
int64_t pos = ftello64(stream->fptr);
if (fseeko64(stream->fptr, pos, SEEK_SET) != 0)
return IO_ERROR;
}
return IO_NORMAL;
#endif /* MCS_sync_dsk_lnx */
if (m_fptr != NULL)
{
int64_t pos = ftello64(m_fptr);
if (fseeko64(m_fptr, pos, SEEK_SET) != 0)
return false;
}
return true;
}
virtual bool Flush(void)
{
#ifdef /* MCS_flush_dsk_lnx */ LEGACY_SYSTEM
if (stream->fptr != NULL)
if (fflush(stream->fptr))
return IO_ERROR;
return IO_NORMAL;
#endif /* MCS_flush_dsk_lnx */
if (fflush(m_fptr))
return false;
return true;
}
virtual bool PutBack(char p_char)
{
#ifdef /* MCS_putback_dsk_lnx */ LEGACY_SYSTEM
if (stream -> fptr == NULL)
return MCS_seek_cur(stream, -1);
if (ungetc(c, stream -> fptr) != c)
return IO_ERROR;
return IO_NORMAL;
#endif /* MCS_putback_dsk_lnx */
if (ungetc(p_char, m_fptr) != p_char)
return false;
return true;
}
virtual int64_t Tell(void)
{
#ifdef /* MCS_tell_dsk_lnx */ LEGACY_SYSTEM
// MW-2009-06-30: If this is a custom stream, call the appropriate callback.
if ((stream -> flags & IO_FAKECUSTOM) == IO_FAKECUSTOM)
return MCS_fake_tell(stream);
if (stream->fptr != NULL)
return ftello64(stream->fptr);
else
return stream->ioptr - stream->buffer;
#endif /* MCS_tell_dsk_lnx */
return ftello64(m_fptr);
}
virtual void *GetFilePointer(void)
{
return (void*)m_fptr;
}
virtual int64_t GetFileSize(void)
{
#ifdef /* MCS_fsize_dsk_lnx */ LEGACY_SYSTEM
if ((stream -> flags & IO_FAKECUSTOM) == IO_FAKECUSTOM)
return MCS_fake_fsize(stream);
struct stat64 buf;
if (stream->fptr == NULL)
return stream->len;
int fd = fileno(stream->fptr);
if (fstat64(fd, &buf))
return 0;
return buf.st_size;
#endif /* MCS_fsize_dsk_lnx */
struct stat64 buf;
int fd = fileno(m_fptr);
if (fstat64(fd, &buf))
return 0;
return buf.st_size;
}
virtual bool TakeBuffer(void*& r_buffer, size_t& r_length)
{
return false;
}
virtual FILE* GetStream(void)
{
return m_fptr;
}
private:
FILE *m_fptr;
bool m_is_eof;
};
////////////////////////////////////////////////////////////////////////////////
class MCLinuxDesktop: public MCSystemInterface
{
public:
virtual MCServiceInterface *QueryService(MCServiceType type)
{
// No Linux-specific services
return nil;
}
virtual bool Initialize(void)
{
#ifdef /* MCS_init_dsk_lnx */ LEGACY_SYSTEM
IO_stdin = new IO_header(stdin, NULL, 0, 0, 0);
IO_stdout = new IO_header(stdout, NULL, 0, 0, 0);
IO_stderr = new IO_header(stderr, NULL, 0, 0, 0);
setlocale(LC_CTYPE, MCnullstring);
setlocale(LC_COLLATE, MCnullstring);
MCinfinity = HUGE_VAL;
struct sigaction action;
memset((char *)&action, 0, sizeof(action));
action.sa_handler = handle_signal;
action.sa_flags = SA_RESTART;
action.sa_flags |= SA_NOCLDSTOP;
sigaction(SIGCHLD, &action, NULL);
sigaction(SIGALRM, &action, NULL);
#ifndef _DEBUG
sigaction(SIGHUP, &action, NULL);
sigaction(SIGINT, &action, NULL);
sigaction(SIGQUIT, &action, NULL);
sigaction(SIGILL, &action, NULL);
sigaction(SIGIOT, &action, NULL);
sigaction(SIGFPE, &action, NULL);
sigaction(SIGBUS, &action, NULL);
sigaction(SIGSEGV, &action, NULL);
sigaction(SIGPIPE, &action, NULL);
sigaction(SIGTERM, &action, NULL);
sigaction(SIGUSR1, &action, NULL);
sigaction(SIGUSR2, &action, NULL);
#ifndef LINUX
sigaction(SIGSYS, &action, NULL);
#endif
#endif
if (!MCS_isatty(0))
MCS_nodelay(0);
// MW-2013-10-01: [[ Bug 11160 ]] At the moment NBSP is not considered a space.
MCctypetable[160] &= ~(1 << 4);
MCshellcmd = strclone("/bin/sh");
#endif /* MCS_init_dsk_lnx */
IO_stdin = MCsystem -> OpenFd(0, kMCOpenFileModeRead);
IO_stdout = MCsystem -> OpenFd(1, kMCOpenFileModeWrite);
IO_stderr = MCsystem -> OpenFd(2, kMCOpenFileModeWrite);
// Internally, LiveCode assumes sorting orders etc are those of en_US.
// Additionally, the "native" string encoding for Linux is ISO-8859-1
// (even if the Linux system is using something different).
const char *t_internal_locale = "en_US.ISO-8859-1";
setlocale(LC_CTYPE, t_internal_locale);
setlocale(LC_COLLATE, t_internal_locale);
MCinfinity = HUGE_VAL;
struct sigaction action;
memset((char *)&action, 0, sizeof(action));
action.sa_handler = handle_signal;
action.sa_flags = SA_RESTART;
action.sa_flags |= SA_NOCLDSTOP;
sigaction(SIGCHLD, &action, NULL);
sigaction(SIGALRM, &action, NULL);
#ifndef _DEBUG
sigaction(SIGHUP, &action, NULL);
sigaction(SIGINT, &action, NULL);
sigaction(SIGQUIT, &action, NULL);
sigaction(SIGILL, &action, NULL);
sigaction(SIGIOT, &action, NULL);
sigaction(SIGFPE, &action, NULL);
sigaction(SIGBUS, &action, NULL);
sigaction(SIGSEGV, &action, NULL);
sigaction(SIGPIPE, &action, NULL);
sigaction(SIGTERM, &action, NULL);
sigaction(SIGUSR1, &action, NULL);
sigaction(SIGUSR2, &action, NULL);
#ifndef _LINUX
sigaction(SIGSYS, &action, NULL);
#endif
#endif
#ifndef _SERVER
// ST-2015-04-20: [[ Bug 15257 ]] Stdin shouldn't be set to
// non-blocking in server.
if (!IsInteractiveConsole(0))
MCS_lnx_nodelay(0);
#endif