Once again syncing after too long an absence
[p5sagit/p5-mst-13.2.git] / os2 / os2ish.h
1 #include <signal.h>
2
3 /* HAS_IOCTL:
4  *      This symbol, if defined, indicates that the ioctl() routine is
5  *      available to set I/O characteristics
6  */
7 #define HAS_IOCTL               /**/
8  
9 /* HAS_UTIME:
10  *      This symbol, if defined, indicates that the routine utime() is
11  *      available to update the access and modification times of files.
12  */
13 #define HAS_UTIME               /**/
14
15 #define HAS_KILL
16 #define HAS_WAIT
17 #define HAS_DLERROR
18 #define HAS_WAITPID_RUNTIME (_emx_env & 0x200)
19
20 /* USEMYBINMODE
21  *      This symbol, if defined, indicates that the program should
22  *      use the routine my_binmode(FILE *fp, char iotype, int mode) to insure
23  *      that a file is in "binary" mode -- that is, that no translation
24  *      of bytes occurs on read or write operations.
25  */
26 #undef USEMYBINMODE
27
28 /* Stat_t:
29  *      This symbol holds the type used to declare buffers for information
30  *      returned by stat().  It's usually just struct stat.  It may be necessary
31  *      to include <sys/stat.h> and <sys/types.h> to get any typedef'ed
32  *      information.
33  */
34 #define Stat_t struct stat
35
36 /* USE_STAT_RDEV:
37  *      This symbol is defined if this system has a stat structure declaring
38  *      st_rdev
39  */
40 #define USE_STAT_RDEV   /**/
41
42 /* ACME_MESS:
43  *      This symbol, if defined, indicates that error messages should be 
44  *      should be generated in a format that allows the use of the Acme
45  *      GUI/editor's autofind feature.
46  */
47 #undef ACME_MESS        /**/
48
49 /* ALTERNATE_SHEBANG:
50  *      This symbol, if defined, contains a "magic" string which may be used
51  *      as the first line of a Perl program designed to be executed directly
52  *      by name, instead of the standard Unix #!.  If ALTERNATE_SHEBANG
53  *      begins with a character other then #, then Perl will only treat
54  *      it as a command line if if finds the string "perl" in the first
55  *      word; otherwise it's treated as the first line of code in the script.
56  *      (IOW, Perl won't hand off to another interpreter via an alternate
57  *      shebang sequence that might be legal Perl code.)
58  */
59 #define ALTERNATE_SHEBANG "extproc "
60
61 #ifndef SIGABRT
62 #    define SIGABRT SIGILL
63 #endif
64 #ifndef SIGILL
65 #    define SIGILL 6         /* blech */
66 #endif
67 #define ABORT() kill(PerlProc_getpid(),SIGABRT);
68
69 #define BIT_BUCKET "/dev/nul"  /* Will this work? */
70
71 /* Apparently TCPIPV4 defines may be included even with only IAK present */
72
73 #if !defined(NO_TCPIPV4) && !defined(TCPIPV4)
74 #  define TCPIPV4
75 #  define TCPIPV4_FORCED                /* Just in case */
76 #endif
77
78 #if defined(I_SYS_UN) && !defined(TCPIPV4)
79 /* It is not working without TCPIPV4 defined. */
80 # undef I_SYS_UN
81 #endif 
82
83 #ifdef USE_THREADS
84
85 #define do_spawn(a)      os2_do_spawn(aTHX_ (a))
86 #define do_aspawn(a,b,c) os2_do_aspawn(aTHX_ (a),(b),(c))
87
88 #define OS2_ERROR_ALREADY_POSTED 299    /* Avoid os2.h */
89
90 extern int rc;
91
92 #define MUTEX_INIT(m) \
93     STMT_START {                                                \
94         int rc;                                                 \
95         if ((rc = _rmutex_create(m,0)))                         \
96             Perl_croak_nocontext("panic: MUTEX_INIT: rc=%i", rc);       \
97     } STMT_END
98 #define MUTEX_LOCK(m) \
99     STMT_START {                                                \
100         int rc;                                                 \
101         if ((rc = _rmutex_request(m,_FMR_IGNINT)))              \
102             Perl_croak_nocontext("panic: MUTEX_LOCK: rc=%i", rc);       \
103     } STMT_END
104 #define MUTEX_UNLOCK(m) \
105     STMT_START {                                                \
106         int rc;                                                 \
107         if ((rc = _rmutex_release(m)))                          \
108             Perl_croak_nocontext("panic: MUTEX_UNLOCK: rc=%i", rc);     \
109     } STMT_END
110 #define MUTEX_DESTROY(m) \
111     STMT_START {                                                \
112         int rc;                                                 \
113         if ((rc = _rmutex_close(m)))                            \
114             Perl_croak_nocontext("panic: MUTEX_DESTROY: rc=%i", rc);    \
115     } STMT_END
116
117 #define COND_INIT(c) \
118     STMT_START {                                                \
119         int rc;                                                 \
120         if ((rc = DosCreateEventSem(NULL,c,0,0)))               \
121             Perl_croak_nocontext("panic: COND_INIT: rc=%i", rc);        \
122     } STMT_END
123 #define COND_SIGNAL(c) \
124     STMT_START {                                                \
125         int rc;                                                 \
126         if ((rc = DosPostEventSem(*(c))) && rc != OS2_ERROR_ALREADY_POSTED)\
127             Perl_croak_nocontext("panic: COND_SIGNAL, rc=%ld", rc);     \
128     } STMT_END
129 #define COND_BROADCAST(c) \
130     STMT_START {                                                \
131         int rc;                                                 \
132         if ((rc = DosPostEventSem(*(c))) && rc != OS2_ERROR_ALREADY_POSTED)\
133             Perl_croak_nocontext("panic: COND_BROADCAST, rc=%i", rc);   \
134     } STMT_END
135 /* #define COND_WAIT(c, m) \
136     STMT_START {                                                \
137         if (WaitForSingleObject(*(c),INFINITE) == WAIT_FAILED)  \
138             Perl_croak_nocontext("panic: COND_WAIT");           \
139     } STMT_END
140 */
141 #define COND_WAIT(c, m) os2_cond_wait(c,m)
142
143 #define COND_WAIT_win32(c, m) \
144     STMT_START {                                                \
145         int rc;                                                 \
146         if ((rc = SignalObjectAndWait(*(m),*(c),INFINITE,FALSE)))       \
147             Perl_croak_nocontext("panic: COND_WAIT");                   \
148         else                                                    \
149             MUTEX_LOCK(m);                                      \
150     } STMT_END
151 #define COND_DESTROY(c) \
152     STMT_START {                                                \
153         int rc;                                                 \
154         if ((rc = DosCloseEventSem(*(c))))                      \
155             Perl_croak_nocontext("panic: COND_DESTROY, rc=%i", rc);     \
156     } STMT_END
157 /*#define THR ((struct thread *) TlsGetValue(PL_thr_key))
158 */
159
160 #ifdef USE_SLOW_THREAD_SPECIFIC
161 #  define pthread_getspecific(k)        (*_threadstore())
162 #  define pthread_setspecific(k,v)      (*_threadstore()=v,0)
163 #  define pthread_key_create(keyp,flag) (*keyp=_gettid(),0)
164 #else /* USE_SLOW_THREAD_SPECIFIC */
165 #  define pthread_getspecific(k)        (*(k))
166 #  define pthread_setspecific(k,v)      (*(k)=(v),0)
167 #  define pthread_key_create(keyp,flag)                 \
168         ( DosAllocThreadLocalMemory(1,(U32*)keyp)       \
169           ? Perl_croak_nocontext("LocalMemory"),1       \
170           : 0                                           \
171         )
172 #endif /* USE_SLOW_THREAD_SPECIFIC */
173 #define pthread_key_delete(keyp)
174 #define pthread_self()                  _gettid()
175 #define YIELD                           DosSleep(0)
176
177 #ifdef PTHREADS_INCLUDED                /* For ./x2p stuff. */
178 int pthread_join(pthread_t tid, void **status);
179 int pthread_detach(pthread_t tid);
180 int pthread_create(pthread_t *tid, const pthread_attr_t *attr,
181                    void *(*start_routine)(void*), void *arg);
182 #endif /* PTHREAD_INCLUDED */
183
184 #define THREADS_ELSEWHERE
185
186 #else /* USE_THREADS */
187
188 #define do_spawn(a)      os2_do_spawn(a)
189 #define do_aspawn(a,b,c) os2_do_aspawn((a),(b),(c))
190
191 #endif /* USE_THREADS */
192  
193 void Perl_OS2_init(char **);
194
195 /* XXX This code hideously puts env inside: */
196
197 #ifdef PERL_CORE
198 #  define PERL_SYS_INIT3(argcp, argvp, envp) STMT_START {       \
199     _response(argcp, argvp);                    \
200     _wildcard(argcp, argvp);                    \
201     Perl_OS2_init(*envp);       } STMT_END
202 #  define PERL_SYS_INIT(argcp, argvp) STMT_START {      \
203     _response(argcp, argvp);                    \
204     _wildcard(argcp, argvp);                    \
205     Perl_OS2_init(NULL);        } STMT_END
206 #else  /* Compiling embedded Perl or Perl extension */
207 #  define PERL_SYS_INIT3(argcp, argvp, envp) STMT_START {       \
208     Perl_OS2_init(*envp);       } STMT_END
209 #  define PERL_SYS_INIT(argcp, argvp) STMT_START {      \
210     Perl_OS2_init(NULL);        } STMT_END
211 #endif
212
213 #ifndef __EMX__
214 #  define PERL_CALLCONV _System
215 #endif
216
217 #define PERL_SYS_TERM()         MALLOC_TERM
218
219 /* #define PERL_SYS_TERM() STMT_START { \
220     if (Perl_HAB_set) WinTerminate(Perl_hab);   } STMT_END */
221
222 #define dXSUB_SYS OS2_XS_init()
223
224 #ifdef PERL_IS_AOUT
225 /* #  define HAS_FORK */
226 /* #  define HIDEMYMALLOC */
227 /* #  define PERL_SBRK_VIA_MALLOC */ /* gets off-page sbrk... */
228 #else /* !PERL_IS_AOUT */
229 #  ifndef PERL_FOR_X2P
230 #    ifdef EMX_BAD_SBRK
231 #      define USE_PERL_SBRK
232 #    endif 
233 #  else
234 #    define PerlIO FILE
235 #  endif 
236 #  define SYSTEM_ALLOC(a) sys_alloc(a)
237
238 void *sys_alloc(int size);
239
240 #endif /* !PERL_IS_AOUT */
241 #if !defined(PERL_CORE) && !defined(PerlIO) /* a2p */
242 #  define PerlIO FILE
243 #endif 
244
245 /* os2ish is used from a2p/a2p.h without pTHX/pTHX_ first being
246  * defined.  Hack around this to get us to compile.
247 */
248 #ifdef PTHX_UNUSED
249 # ifndef pTHX
250 #  define pTHX
251 # endif
252 # ifndef pTHX_
253 #  define pTHX_
254 # endif
255 #endif
256
257 #define TMPPATH1 "plXXXXXX"
258 extern char *tmppath;
259 PerlIO *my_syspopen(pTHX_ char *cmd, char *mode);
260 /* Cannot prototype with I32 at this point. */
261 int my_syspclose(PerlIO *f);
262 FILE *my_tmpfile (void);
263 char *my_tmpnam (char *);
264
265 #undef L_tmpnam
266 #define L_tmpnam MAXPATHLEN
267
268 #define tmpfile my_tmpfile
269 #define tmpnam  my_tmpnam
270 #define isatty  _isterm
271 #define rand    random
272 #define srand   srandom
273 #define strtoll _strtoll
274 #define strtoull        _strtoull
275
276 /*
277  * fwrite1() should be a routine with the same calling sequence as fwrite(),
278  * but which outputs all of the bytes requested as a single stream (unlike
279  * fwrite() itself, which on some systems outputs several distinct records
280  * if the number_of_items parameter is >1).
281  */
282 #define fwrite1 fwrite
283
284 #define my_getenv(var) getenv(var)
285 #define flock   my_flock
286
287 void *emx_calloc (size_t, size_t);
288 void emx_free (void *);
289 void *emx_malloc (size_t);
290 void *emx_realloc (void *, size_t);
291
292 /*****************************************************************************/
293
294 #include <stdlib.h>     /* before the following definitions */
295 #include <unistd.h>     /* before the following definitions */
296
297 #define chdir   _chdir2
298 #define getcwd  _getcwd2
299
300 /* This guy is needed for quick stdstd  */
301
302 #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE)
303         /* Perl uses ungetc only with successful return */
304 #  define ungetc(c,fp) \
305         (FILE_ptr(fp) > FILE_base(fp) && c == (int)*(FILE_ptr(fp) - 1) \
306          ? (--FILE_ptr(fp), ++FILE_cnt(fp), (int)c) : ungetc(c,fp))
307 #endif
308
309 /* ctermid is missing from emx0.9d */
310 char *ctermid(char *s);
311
312 #define OP_BINARY O_BINARY
313
314 #define OS2_STAT_HACK 1
315 #if OS2_STAT_HACK
316
317 #define Stat(fname,bufptr) os2_stat((fname),(bufptr))
318 #define Fstat(fd,bufptr)   fstat((fd),(bufptr))
319 #define Fflush(fp)         fflush(fp)
320 #define Mkdir(path,mode)   mkdir((path),(mode))
321
322 #undef S_IFBLK
323 #undef S_ISBLK
324 #define S_IFBLK         0120000
325 #define S_ISBLK(mode)   (((mode) & S_IFMT) == S_IFBLK)
326
327 #else
328
329 #define Stat(fname,bufptr) stat((fname),(bufptr))
330 #define Fstat(fd,bufptr)   fstat((fd),(bufptr))
331 #define Fflush(fp)         fflush(fp)
332 #define Mkdir(path,mode)   mkdir((path),(mode))
333
334 #endif
335
336 /* With SD386 it is impossible to debug register variables. */
337 #if !defined(PERL_IS_AOUT) && defined(DEBUGGING) && !defined(register)
338 #  define register
339 #endif
340
341 /* Our private OS/2 specific data. */
342
343 typedef struct OS2_Perl_data {
344   unsigned long flags;
345   unsigned long phab;
346   int (*xs_init)();
347   unsigned long rc;
348   unsigned long severity;
349   unsigned long phmq;                   /* Handle to message queue */
350   unsigned long phmq_refcnt;
351   unsigned long phmq_servers;
352   unsigned long initial_mode;           /* VIO etc. mode we were started in */
353 } OS2_Perl_data_t;
354
355 extern OS2_Perl_data_t OS2_Perl_data;
356
357 #define Perl_hab                ((HAB)OS2_Perl_data.phab)
358 #define Perl_rc                 (OS2_Perl_data.rc)
359 #define Perl_severity           (OS2_Perl_data.severity)
360 #define errno_isOS2             12345678
361 #define errno_isOS2_set         12345679
362 #define OS2_Perl_flags  (OS2_Perl_data.flags)
363 #define Perl_HAB_set_f  1
364 #define Perl_HAB_set    (OS2_Perl_flags & Perl_HAB_set_f)
365 #define set_Perl_HAB_f  (OS2_Perl_flags |= Perl_HAB_set_f)
366 #define set_Perl_HAB(h) (set_Perl_HAB_f, Perl_hab = h)
367 #define _obtain_Perl_HAB (init_PMWIN_entries(),                         \
368                           Perl_hab = (*PMWIN_entries.Initialize)(0),    \
369                           set_Perl_HAB_f, Perl_hab)
370 #define perl_hab_GET()  (Perl_HAB_set ? Perl_hab : _obtain_Perl_HAB)
371 #define Acquire_hab()   perl_hab_GET()
372 #define Perl_hmq        ((HMQ)OS2_Perl_data.phmq)
373 #define Perl_hmq_refcnt (OS2_Perl_data.phmq_refcnt)
374 #define Perl_hmq_servers        (OS2_Perl_data.phmq_servers)
375 #define Perl_os2_initial_mode   (OS2_Perl_data.initial_mode)
376
377 unsigned long Perl_hab_GET();
378 unsigned long Perl_Register_MQ(int serve);
379 void    Perl_Deregister_MQ(int serve);
380 int     Perl_Serve_Messages(int force);
381 /* Cannot prototype with I32 at this point. */
382 int     Perl_Process_Messages(int force, long *cntp);
383 char    *os2_execname(pTHX);
384
385 struct _QMSG;
386 struct PMWIN_entries_t {
387     unsigned long (*Initialize)( unsigned long fsOptions );
388     unsigned long (*CreateMsgQueue)(unsigned long hab, long cmsg);
389     int (*DestroyMsgQueue)(unsigned long hmq);
390     int (*PeekMsg)(unsigned long hab, struct _QMSG *pqmsg,
391                    unsigned long hwndFilter, unsigned long msgFilterFirst,
392                    unsigned long msgFilterLast, unsigned long fl);
393     int (*GetMsg)(unsigned long hab, struct _QMSG *pqmsg,
394                   unsigned long hwndFilter, unsigned long msgFilterFirst,
395                   unsigned long msgFilterLast);
396     void * (*DispatchMsg)(unsigned long hab, struct _QMSG *pqmsg);
397 };
398 extern struct PMWIN_entries_t PMWIN_entries;
399 void init_PMWIN_entries(void);
400
401 #define perl_hmq_GET(serve)     Perl_Register_MQ(serve)
402 #define perl_hmq_UNSET(serve)   Perl_Deregister_MQ(serve)
403
404 #define OS2_XS_init() (*OS2_Perl_data.xs_init)(aTHX)
405
406 #if _EMX_CRT_REV_ >= 60
407 # define os2_setsyserrno(rc)    (Perl_rc = rc, errno = errno_isOS2_set, \
408                                 _setsyserrno(rc))
409 #else
410 # define os2_setsyserrno(rc)    (Perl_rc = rc, errno = errno_isOS2)
411 #endif
412
413 /* The expressions below return true on error. */
414 /* INCL_DOSERRORS needed. rc should be declared outside. */
415 #define CheckOSError(expr) (!(rc = (expr)) ? 0 : (FillOSError(rc), 1))
416 /* INCL_WINERRORS needed. */
417 #define SaveWinError(expr) ((expr) ? : (FillWinError, 0))
418 #define CheckWinError(expr) ((expr) ? 0: (FillWinError, 1))
419 #define FillOSError(rc) (os2_setsyserrno(rc),                           \
420                         Perl_severity = SEVERITY_ERROR) 
421 #define FillWinError (Perl_severity = ERRORIDSEV(Perl_rc),              \
422                         Perl_rc = ERRORIDERROR(Perl_rc)),               \
423                         os2_setsyserrno(Perl_rc)
424
425 #define STATIC_FILE_LENGTH 127
426
427 #define PERLLIB_MANGLE(s, n) perllib_mangle((s), (n))
428 char *perllib_mangle(char *, unsigned int);
429
430 char *os2error(int rc);
431
432 /* ************************************************************ */
433 #define Dos32QuerySysState DosQuerySysState
434 #define QuerySysState(flags, pid, buf, bufsz) \
435         Dos32QuerySysState(flags, 0,  pid, 0, buf, bufsz)
436
437 #define QSS_PROCESS     1
438 #define QSS_MODULE      4
439 #define QSS_SEMAPHORES  2
440 #define QSS_FILE        8               /* Buggy until fixpack18 */
441 #define QSS_SHARED      16
442
443 #ifdef _OS2_H
444
445 APIRET APIENTRY Dos32QuerySysState(ULONG func,ULONG arg1,ULONG pid,
446                         ULONG _res_,PVOID buf,ULONG bufsz);
447 typedef struct {
448         ULONG   threadcnt;
449         ULONG   proccnt;
450         ULONG   modulecnt;
451 } QGLOBAL, *PQGLOBAL;
452
453 typedef struct {
454         ULONG   rectype;
455         USHORT  threadid;
456         USHORT  slotid;
457         ULONG   sleepid;
458         ULONG   priority;
459         ULONG   systime;
460         ULONG   usertime;
461         UCHAR   state;
462         UCHAR   _reserved1_;    /* padding to ULONG */
463         USHORT  _reserved2_;    /* padding to ULONG */
464 } QTHREAD, *PQTHREAD;
465
466 typedef struct {
467         USHORT  sfn;
468         USHORT  refcnt;
469         USHORT  flags1;
470         USHORT  flags2;
471         USHORT  accmode1;
472         USHORT  accmode2;
473         ULONG   filesize;
474         USHORT  volhnd;
475         USHORT  attrib;
476         USHORT  _reserved_;
477 } QFDS, *PQFDS;
478
479 typedef struct qfile {
480         ULONG           rectype;
481         struct qfile    *next;
482         ULONG           opencnt;
483         PQFDS           filedata;
484         char            name[1];
485 } QFILE, *PQFILE;
486
487 typedef struct {
488         ULONG   rectype;
489         PQTHREAD threads;
490         USHORT  pid;
491         USHORT  ppid;
492         ULONG   type;
493         ULONG   state;
494         ULONG   sessid;
495         USHORT  hndmod;
496         USHORT  threadcnt;
497         ULONG   privsem32cnt;
498         ULONG   _reserved2_;
499         USHORT  sem16cnt;
500         USHORT  dllcnt;
501         USHORT  shrmemcnt;
502         USHORT  fdscnt;
503         PUSHORT sem16s;
504         PUSHORT dlls;
505         PUSHORT shrmems;
506         PUSHORT fds;
507 } QPROCESS, *PQPROCESS;
508
509 typedef struct sema {
510         struct sema *next;
511         USHORT  refcnt;
512         UCHAR   sysflags;
513         UCHAR   sysproccnt;
514         ULONG   _reserved1_;
515         USHORT  index;
516         CHAR    name[1];
517 } QSEMA, *PQSEMA;
518
519 typedef struct {
520         ULONG   rectype;
521         ULONG   _reserved1_;
522         USHORT  _reserved2_;
523         USHORT  syssemidx;
524         ULONG   index;
525         QSEMA   sema;
526 } QSEMSTRUC, *PQSEMSTRUC;
527
528 typedef struct {
529         USHORT  pid;
530         USHORT  opencnt;
531 } QSEMOWNER32, *PQSEMOWNER32;
532
533 typedef struct {
534         PQSEMOWNER32    own;
535         PCHAR           name;
536         PVOID           semrecs; /* array of associated sema's */
537         USHORT          flags;
538         USHORT          semreccnt;
539         USHORT          waitcnt;
540         USHORT          _reserved_;     /* padding to ULONG */
541 } QSEMSMUX32, *PQSEMSMUX32;
542
543 typedef struct {
544         PQSEMOWNER32    own;
545         PCHAR           name;
546         PQSEMSMUX32     mux;
547         USHORT          flags;
548         USHORT          postcnt;
549 } QSEMEV32, *PQSEMEV32;
550
551 typedef struct {
552         PQSEMOWNER32    own;
553         PCHAR           name;
554         PQSEMSMUX32     mux;
555         USHORT          flags;
556         USHORT          refcnt;
557         USHORT          thrdnum;
558         USHORT          _reserved_;     /* padding to ULONG */
559 } QSEMMUX32, *PQSEMMUX32;
560
561 typedef struct semstr32 {
562         struct semstr *next;
563         QSEMEV32 evsem;
564         QSEMMUX32  muxsem;
565         QSEMSMUX32 smuxsem;
566 } QSEMSTRUC32, *PQSEMSTRUC32;
567
568 typedef struct shrmem {
569         struct shrmem *next;
570         USHORT  hndshr;
571         USHORT  selshr;
572         USHORT  refcnt;
573         CHAR    name[1];
574 } QSHRMEM, *PQSHRMEM;
575
576 typedef struct module {
577         struct module *next;
578         USHORT  hndmod;
579         USHORT  type;
580         ULONG   refcnt;
581         ULONG   segcnt;
582         PVOID   _reserved_;
583         PCHAR   name;
584         USHORT  modref[1];
585 } QMODULE, *PQMODULE;
586
587 typedef struct {
588         PQGLOBAL        gbldata;
589         PQPROCESS       procdata;
590         PQSEMSTRUC      semadata;
591         PQSEMSTRUC32    sem32data;
592         PQSHRMEM        shrmemdata;
593         PQMODULE        moddata;
594         PVOID           _reserved2_;
595         PQFILE          filedata;
596 } QTOPLEVEL, *PQTOPLEVEL;
597 /* ************************************************************ */
598
599 PQTOPLEVEL get_sysinfo(ULONG pid, ULONG flags);
600
601 #endif /* _OS2_H */
602