-M -A -C broken
[p5sagit/p5-mst-13.2.git] / os2 / os2ish.h
1 #include <signal.h>
2 #include <io.h>
3 /* #include <sys/select.h> */
4
5 /* HAS_IOCTL:
6  *      This symbol, if defined, indicates that the ioctl() routine is
7  *      available to set I/O characteristics
8  */
9 #define HAS_IOCTL               /**/
10  
11 /* HAS_UTIME:
12  *      This symbol, if defined, indicates that the routine utime() is
13  *      available to update the access and modification times of files.
14  */
15 #define HAS_UTIME               /**/
16
17 /* BIG_TIME:
18  *      This symbol is defined if Time_t is an unsigned type on this system.
19  */
20 #define BIG_TIME
21
22 #define HAS_KILL
23 #define HAS_WAIT
24 #define HAS_DLERROR
25 #define HAS_WAITPID_RUNTIME (_emx_env & 0x200)
26
27 /* HAS_PASSWD
28  *      This symbol, if defined, indicates that the getpwnam() and
29  *      getpwuid() routines are available to get password entries.
30  *      The getpwent() has a separate definition, HAS_GETPWENT.
31  */
32 #define HAS_PASSWD
33
34 /* HAS_GROUP
35  *      This symbol, if defined, indicates that the getgrnam() and
36  *      getgrgid() routines are available to get group entries.
37  *      The getgrent() has a separate definition, HAS_GETGRENT.
38  */
39 #define HAS_GROUP
40 #define HAS_GETGRENT                    /* fake */
41 #define HAS_SETGRENT                    /* fake */
42 #define HAS_ENDGRENT                    /* fake */
43
44 /* USEMYBINMODE
45  *      This symbol, if defined, indicates that the program should
46  *      use the routine my_binmode(FILE *fp, char iotype, int mode) to insure
47  *      that a file is in "binary" mode -- that is, that no translation
48  *      of bytes occurs on read or write operations.
49  */
50 #undef USEMYBINMODE
51
52 /* Stat_t:
53  *      This symbol holds the type used to declare buffers for information
54  *      returned by stat().  It's usually just struct stat.  It may be necessary
55  *      to include <sys/stat.h> and <sys/types.h> to get any typedef'ed
56  *      information.
57  */
58 #define Stat_t struct stat
59
60 /* USE_STAT_RDEV:
61  *      This symbol is defined if this system has a stat structure declaring
62  *      st_rdev
63  */
64 #define USE_STAT_RDEV   /**/
65
66 /* ACME_MESS:
67  *      This symbol, if defined, indicates that error messages should be 
68  *      should be generated in a format that allows the use of the Acme
69  *      GUI/editor's autofind feature.
70  */
71 #undef ACME_MESS        /**/
72
73 /* ALTERNATE_SHEBANG:
74  *      This symbol, if defined, contains a "magic" string which may be used
75  *      as the first line of a Perl program designed to be executed directly
76  *      by name, instead of the standard Unix #!.  If ALTERNATE_SHEBANG
77  *      begins with a character other then #, then Perl will only treat
78  *      it as a command line if if finds the string "perl" in the first
79  *      word; otherwise it's treated as the first line of code in the script.
80  *      (IOW, Perl won't hand off to another interpreter via an alternate
81  *      shebang sequence that might be legal Perl code.)
82  */
83 #define ALTERNATE_SHEBANG "extproc "
84
85 #ifndef SIGABRT
86 #    define SIGABRT SIGILL
87 #endif
88 #ifndef SIGILL
89 #    define SIGILL 6         /* blech */
90 #endif
91 #define ABORT() kill(PerlProc_getpid(),SIGABRT);
92
93 #define BIT_BUCKET "/dev/nul"  /* Will this work? */
94
95 /* Apparently TCPIPV4 defines may be included even with only IAK present */
96
97 #if !defined(NO_TCPIPV4) && !defined(TCPIPV4)
98 #  define TCPIPV4
99 #  define TCPIPV4_FORCED                /* Just in case */
100 #endif
101
102 #if defined(I_SYS_UN) && !defined(TCPIPV4)
103 /* It is not working without TCPIPV4 defined. */
104 # undef I_SYS_UN
105 #endif 
106
107 #ifdef USE_ITHREADS
108
109 #define do_spawn(a)      os2_do_spawn(aTHX_ (a))
110 #define do_aspawn(a,b,c) os2_do_aspawn(aTHX_ (a),(b),(c))
111
112 #define OS2_ERROR_ALREADY_POSTED 299    /* Avoid os2.h */
113
114 extern int rc;
115
116 #define MUTEX_INIT(m) \
117     STMT_START {                                                \
118         int rc;                                                 \
119         if ((rc = _rmutex_create(m,0)))                         \
120             Perl_croak_nocontext("panic: MUTEX_INIT: rc=%i", rc);       \
121     } STMT_END
122 #define MUTEX_LOCK(m) \
123     STMT_START {                                                \
124         int rc;                                                 \
125         if ((rc = _rmutex_request(m,_FMR_IGNINT)))              \
126             Perl_croak_nocontext("panic: MUTEX_LOCK: rc=%i", rc);       \
127     } STMT_END
128 #define MUTEX_UNLOCK(m) \
129     STMT_START {                                                \
130         int rc;                                                 \
131         if ((rc = _rmutex_release(m)))                          \
132             Perl_croak_nocontext("panic: MUTEX_UNLOCK: rc=%i", rc);     \
133     } STMT_END
134 #define MUTEX_DESTROY(m) \
135     STMT_START {                                                \
136         int rc;                                                 \
137         if ((rc = _rmutex_close(m)))                            \
138             Perl_croak_nocontext("panic: MUTEX_DESTROY: rc=%i", rc);    \
139     } STMT_END
140
141 #define COND_INIT(c) \
142     STMT_START {                                                \
143         int rc;                                                 \
144         if ((rc = DosCreateEventSem(NULL,c,0,0)))               \
145             Perl_croak_nocontext("panic: COND_INIT: rc=%i", rc);        \
146     } STMT_END
147 #define COND_SIGNAL(c) \
148     STMT_START {                                                \
149         int rc;                                                 \
150         if ((rc = DosPostEventSem(*(c))) && rc != OS2_ERROR_ALREADY_POSTED)\
151             Perl_croak_nocontext("panic: COND_SIGNAL, rc=%ld", rc);     \
152     } STMT_END
153 #define COND_BROADCAST(c) \
154     STMT_START {                                                \
155         int rc;                                                 \
156         if ((rc = DosPostEventSem(*(c))) && rc != OS2_ERROR_ALREADY_POSTED)\
157             Perl_croak_nocontext("panic: COND_BROADCAST, rc=%i", rc);   \
158     } STMT_END
159 /* #define COND_WAIT(c, m) \
160     STMT_START {                                                \
161         if (WaitForSingleObject(*(c),INFINITE) == WAIT_FAILED)  \
162             Perl_croak_nocontext("panic: COND_WAIT");           \
163     } STMT_END
164 */
165 #define COND_WAIT(c, m) os2_cond_wait(c,m)
166
167 #define COND_WAIT_win32(c, m) \
168     STMT_START {                                                \
169         int rc;                                                 \
170         if ((rc = SignalObjectAndWait(*(m),*(c),INFINITE,FALSE)))       \
171             Perl_croak_nocontext("panic: COND_WAIT");                   \
172         else                                                    \
173             MUTEX_LOCK(m);                                      \
174     } STMT_END
175 #define COND_DESTROY(c) \
176     STMT_START {                                                \
177         int rc;                                                 \
178         if ((rc = DosCloseEventSem(*(c))))                      \
179             Perl_croak_nocontext("panic: COND_DESTROY, rc=%i", rc);     \
180     } STMT_END
181 /*#define THR ((struct thread *) TlsGetValue(PL_thr_key))
182 */
183
184 #ifdef USE_SLOW_THREAD_SPECIFIC
185 #  define pthread_getspecific(k)        (*_threadstore())
186 #  define pthread_setspecific(k,v)      (*_threadstore()=v,0)
187 #  define pthread_key_create(keyp,flag) (*keyp=_gettid(),0)
188 #else /* USE_SLOW_THREAD_SPECIFIC */
189 #  define pthread_getspecific(k)        (*(k))
190 #  define pthread_setspecific(k,v)      (*(k)=(v),0)
191 #  define pthread_key_create(keyp,flag)                 \
192         ( DosAllocThreadLocalMemory(1,(unsigned long**)keyp)    \
193           ? Perl_croak_nocontext("LocalMemory"),1       \
194           : 0                                           \
195         )
196 #endif /* USE_SLOW_THREAD_SPECIFIC */
197 #define pthread_key_delete(keyp)
198 #define pthread_self()                  _gettid()
199 #define YIELD                           DosSleep(0)
200
201 #ifdef PTHREADS_INCLUDED                /* For ./x2p stuff. */
202 int pthread_join(pthread_t tid, void **status);
203 int pthread_detach(pthread_t tid);
204 int pthread_create(pthread_t *tid, const pthread_attr_t *attr,
205                    void *(*start_routine)(void*), void *arg);
206 #endif /* PTHREAD_INCLUDED */
207
208 #define THREADS_ELSEWHERE
209
210 #else /* USE_ITHREADS */
211
212 #define do_spawn(a)      os2_do_spawn(a)
213 #define do_aspawn(a,b,c) os2_do_aspawn((a),(b),(c))
214  
215 void Perl_OS2_init(char **);
216 void Perl_OS2_init3(char **envp, void **excH, int flags);
217 void Perl_OS2_term(void **excH, int exitstatus, int flags);
218
219 /* The code without INIT3 hideously puts env inside: */
220
221 /* These ones should be in the same block as PERL_SYS_TERM() */
222 #ifdef PERL_CORE
223
224 #  define PERL_SYS_INIT3(argcp, argvp, envp)    \
225   { void *xreg[2];                              \
226     MALLOC_CHECK_TAINT(*argcp, *argvp, *envp)   \
227     _response(argcp, argvp);                    \
228     _wildcard(argcp, argvp);                    \
229     Perl_OS2_init3(*envp, xreg, 0)
230
231 #  define PERL_SYS_INIT(argcp, argvp)  {        \
232   { void *xreg[2];                              \
233     _response(argcp, argvp);                    \
234     _wildcard(argcp, argvp);                    \
235     Perl_OS2_init3(NULL, xreg, 0)
236
237 #else  /* Compiling embedded Perl or Perl extension */
238
239 #  define PERL_SYS_INIT3(argcp, argvp, envp)    \
240   { void *xreg[2];                              \
241     Perl_OS2_init3(*envp, xreg, 0)
242 #  define PERL_SYS_INIT(argcp, argvp)   {       \
243   { void *xreg[2];                              \
244     Perl_OS2_init3(NULL, xreg, 0)
245 #endif
246
247 #define FORCE_EMX_DEINIT_EXIT           1
248 #define FORCE_EMX_DEINIT_CRT_TERM       2
249 #define FORCE_EMX_DEINIT_RUN_ATEXIT     4
250
251 #define PERL_SYS_TERM2(xreg,flags)                                      \
252   Perl_OS2_term(xreg, 0, flags);                                        \
253   MALLOC_TERM
254
255 #define PERL_SYS_TERM1(xreg)                                            \
256      Perl_OS2_term(xreg, 0, FORCE_EMX_DEINIT_RUN_ATEXIT)
257
258 /* This one should come in pair with PERL_SYS_INIT() and in the same block */
259 #define PERL_SYS_TERM()                                                 \
260      PERL_SYS_TERM1(xreg);                                              \
261   }
262
263 #ifndef __EMX__
264 #  define PERL_CALLCONV _System
265 #endif
266
267 /* #define PERL_SYS_TERM() STMT_START { \
268     if (Perl_HAB_set) WinTerminate(Perl_hab);   } STMT_END */
269
270 #define dXSUB_SYS OS2_XS_init()
271
272 #ifdef PERL_IS_AOUT
273 /* #  define HAS_FORK */
274 /* #  define HIDEMYMALLOC */
275 /* #  define PERL_SBRK_VIA_MALLOC */ /* gets off-page sbrk... */
276 #else /* !PERL_IS_AOUT */
277 #  ifndef PERL_FOR_X2P
278 #    ifdef EMX_BAD_SBRK
279 #      define USE_PERL_SBRK
280 #    endif 
281 #  else
282 #    define PerlIO FILE
283 #  endif 
284 #  define SYSTEM_ALLOC(a) sys_alloc(a)
285
286 void *sys_alloc(int size);
287
288 #endif /* !PERL_IS_AOUT */
289 #if !defined(PERL_CORE) && !defined(PerlIO) /* a2p */
290 #  define PerlIO FILE
291 #endif 
292
293 /* os2ish is used from a2p/a2p.h without pTHX/pTHX_ first being
294  * defined.  Hack around this to get us to compile.
295 */
296 #ifdef PTHX_UNUSED
297 # ifndef pTHX
298 #  define pTHX
299 # endif
300 # ifndef pTHX_
301 #  define pTHX_
302 # endif
303 #endif
304
305 #define TMPPATH1 "plXXXXXX"
306 extern const char *tmppath;
307 PerlIO *my_syspopen(pTHX_ char *cmd, char *mode);
308 /* Cannot prototype with I32 at this point. */
309 int my_syspclose(PerlIO *f);
310 FILE *my_tmpfile (void);
311 char *my_tmpnam (char *);
312 int my_mkdir (__const__ char *, long);
313 int my_rmdir (__const__ char *);
314 struct passwd *my_getpwent (void);
315 void my_setpwent (void);
316 void my_endpwent (void);
317 char *gcvt_os2(double value, int digits, char *buffer);
318
319 #define MAX_SLEEP       (((1<30) / (1000/4))-1) /* 1<32 msec */
320
321 static __inline__ unsigned
322 my_sleep(unsigned sec)
323 {
324   int remain;
325   while (sec > MAX_SLEEP) {
326     sec -= MAX_SLEEP;
327     remain = sleep(MAX_SLEEP);
328     if (remain)
329       return remain + sec;
330   }
331   return sleep(sec);
332 }
333
334 #define sleep           my_sleep
335
336 #ifndef INCL_DOS
337 unsigned long DosSleep(unsigned long);
338 unsigned long DosAllocThreadLocalMemory (unsigned long cb, unsigned long **p);
339 #endif
340
341 struct group *getgrent (void);
342 void setgrent (void);
343 void endgrent (void);
344
345 struct passwd *my_getpwuid (uid_t);
346 struct passwd *my_getpwnam (__const__ char *);
347
348 #undef L_tmpnam
349 #define L_tmpnam MAXPATHLEN
350
351 #define tmpfile my_tmpfile
352 #define tmpnam  my_tmpnam
353 #define isatty  _isterm
354 #define rand    random
355 #define srand   srandom
356 #define strtoll _strtoll
357 #define strtoull        _strtoull
358
359 #define usleep(usec)    ((void)_sleep2(((usec)+500)/1000))
360
361
362 /*
363  * fwrite1() should be a routine with the same calling sequence as fwrite(),
364  * but which outputs all of the bytes requested as a single stream (unlike
365  * fwrite() itself, which on some systems outputs several distinct records
366  * if the number_of_items parameter is >1).
367  */
368 #define fwrite1 fwrite
369
370 #define my_getenv(var) getenv(var)
371 #define flock   my_flock
372 #define rmdir   my_rmdir
373 #define mkdir   my_mkdir
374 #define setpwent        my_setpwent
375 #define getpwent        my_getpwent
376 #define endpwent        my_endpwent
377 #define getpwuid        my_getpwuid
378 #define getpwnam        my_getpwnam
379
380 void *emx_calloc (size_t, size_t);
381 void emx_free (void *);
382 void *emx_malloc (size_t);
383 void *emx_realloc (void *, size_t);
384
385 /*****************************************************************************/
386
387 #include <stdlib.h>     /* before the following definitions */
388 #include <unistd.h>     /* before the following definitions */
389 #include <fcntl.h>
390 #include <sys/stat.h>
391
392 #define chdir   _chdir2
393 #define getcwd  _getcwd2
394
395 /* This guy is needed for quick stdstd  */
396
397 #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE)
398         /* Perl uses ungetc only with successful return */
399 #  define ungetc(c,fp) \
400         (FILE_ptr(fp) > FILE_base(fp) && c == (int)*(FILE_ptr(fp) - 1) \
401          ? (--FILE_ptr(fp), ++FILE_cnt(fp), (int)c) : ungetc(c,fp))
402 #endif
403
404 #define PERLIO_IS_BINMODE_FD(fd) _PERLIO_IS_BINMODE_FD(fd)
405
406 #ifdef __GNUG__
407 # define HAS_BOOL 
408 #endif
409 #ifndef HAS_BOOL
410 # define bool char
411 # define HAS_BOOL 1
412 #endif
413
414 #include <emx/io.h> /* for _fd_flags() prototype */
415
416 static inline bool
417 _PERLIO_IS_BINMODE_FD(int fd)
418 {
419     int *pflags = _fd_flags(fd);
420
421     return pflags && (*pflags) & O_BINARY;
422 }
423
424 /* ctermid is missing from emx0.9d */
425 char *ctermid(char *s);
426
427 #define OP_BINARY O_BINARY
428
429 #define OS2_STAT_HACK 1
430 #if OS2_STAT_HACK
431
432 #define Stat(fname,bufptr) os2_stat((fname),(bufptr))
433 #define Fstat(fd,bufptr)   os2_fstat((fd),(bufptr))
434 #define Fflush(fp)         fflush(fp)
435 #define Mkdir(path,mode)   mkdir((path),(mode))
436 #define chmod(path,mode)   os2_chmod((path),(mode))
437
438 #undef S_IFBLK
439 #undef S_ISBLK
440 #define S_IFBLK         0120000         /* Hacks to make things compile... */
441 #define S_ISBLK(mode)   (((mode) & S_IFMT) == S_IFBLK)
442
443 int os2_chmod(const char *name, int pmode);
444 int os2_fstat(int handle, struct stat *st);
445
446 #else
447
448 #define Stat(fname,bufptr) stat((fname),(bufptr))
449 #define Fstat(fd,bufptr)   fstat((fd),(bufptr))
450 #define Fflush(fp)         fflush(fp)
451 #define Mkdir(path,mode)   mkdir((path),(mode))
452
453 #endif
454
455 /* With SD386 it is impossible to debug register variables. */
456 #if !defined(PERL_IS_AOUT) && defined(DEBUGGING) && !defined(register)
457 #  define register
458 #endif
459
460 /* Our private OS/2 specific data. */
461
462 typedef struct OS2_Perl_data {
463   unsigned long flags;
464   unsigned long phab;
465   int (*xs_init)();
466   unsigned long rc;
467   unsigned long severity;
468   unsigned long phmq;                   /* Handle to message queue */
469   unsigned long phmq_refcnt;
470   unsigned long phmq_servers;
471   unsigned long initial_mode;           /* VIO etc. mode we were started in */
472   unsigned long morph_refcnt;
473 } OS2_Perl_data_t;
474
475 extern OS2_Perl_data_t OS2_Perl_data;
476
477 #define Perl_hab                ((HAB)OS2_Perl_data.phab)
478 #define Perl_rc                 (OS2_Perl_data.rc)
479 #define Perl_severity           (OS2_Perl_data.severity)
480 #define errno_isOS2             12345678
481 #define errno_isOS2_set         12345679
482 #define OS2_Perl_flags  (OS2_Perl_data.flags)
483 #define Perl_HAB_set_f  1
484 #define Perl_HAB_set    (OS2_Perl_flags & Perl_HAB_set_f)
485 #define set_Perl_HAB_f  (OS2_Perl_flags |= Perl_HAB_set_f)
486 #define set_Perl_HAB(h) (set_Perl_HAB_f, Perl_hab = h)
487 #define _obtain_Perl_HAB (init_PMWIN_entries(),                         \
488                           Perl_hab = (*PMWIN_entries.Initialize)(0),    \
489                           set_Perl_HAB_f, Perl_hab)
490 #define perl_hab_GET()  (Perl_HAB_set ? Perl_hab : _obtain_Perl_HAB)
491 #define Acquire_hab()   perl_hab_GET()
492 #define Perl_hmq        ((HMQ)OS2_Perl_data.phmq)
493 #define Perl_hmq_refcnt (OS2_Perl_data.phmq_refcnt)
494 #define Perl_hmq_servers        (OS2_Perl_data.phmq_servers)
495 #define Perl_os2_initial_mode   (OS2_Perl_data.initial_mode)
496 #define Perl_morph_refcnt       (OS2_Perl_data.morph_refcnt)
497
498 unsigned long Perl_hab_GET();
499 unsigned long Perl_Register_MQ(int serve);
500 void    Perl_Deregister_MQ(int serve);
501 int     Perl_Serve_Messages(int force);
502 /* Cannot prototype with I32 at this point. */
503 int     Perl_Process_Messages(int force, long *cntp);
504 char    *os2_execname(pTHX);
505
506 struct _QMSG;
507 struct PMWIN_entries_t {
508     unsigned long (*Initialize)( unsigned long fsOptions );
509     unsigned long (*CreateMsgQueue)(unsigned long hab, long cmsg);
510     int (*DestroyMsgQueue)(unsigned long hmq);
511     int (*PeekMsg)(unsigned long hab, struct _QMSG *pqmsg,
512                    unsigned long hwndFilter, unsigned long msgFilterFirst,
513                    unsigned long msgFilterLast, unsigned long fl);
514     int (*GetMsg)(unsigned long hab, struct _QMSG *pqmsg,
515                   unsigned long hwndFilter, unsigned long msgFilterFirst,
516                   unsigned long msgFilterLast);
517     void * (*DispatchMsg)(unsigned long hab, struct _QMSG *pqmsg);
518     unsigned long (*GetLastError)(unsigned long hab);
519     unsigned long (*CancelShutdown)(unsigned long hmq, unsigned long fCancelAlways);
520 };
521 extern struct PMWIN_entries_t PMWIN_entries;
522 void init_PMWIN_entries(void);
523
524 #define perl_hmq_GET(serve)     Perl_Register_MQ(serve)
525 #define perl_hmq_UNSET(serve)   Perl_Deregister_MQ(serve)
526
527 #define OS2_XS_init() (*OS2_Perl_data.xs_init)(aTHX)
528
529 #if _EMX_CRT_REV_ >= 60
530 # define os2_setsyserrno(rc)    (Perl_rc = rc, errno = errno_isOS2_set, \
531                                 _setsyserrno(rc))
532 #else
533 # define os2_setsyserrno(rc)    (Perl_rc = rc, errno = errno_isOS2)
534 #endif
535
536 /* The expressions below return true on error. */
537 /* INCL_DOSERRORS needed. rc should be declared outside. */
538 #define CheckOSError(expr) ((rc = (expr)) ? (FillOSError(rc), rc) : 0)
539 /* INCL_WINERRORS needed. */
540 #define CheckWinError(expr) ((expr) ? 0: (FillWinError, 1))
541
542 /* This form propagates the return value, setting $^E if needed */
543 #define SaveWinError(expr) ((expr) ? : (FillWinError, 0))
544
545 /* This form propagates the return value, dieing with $^E if needed */
546 #define SaveCroakWinError(expr,die,name1,name2)         \
547   ((expr) ? : (CroakWinError(die,name1 name2), 0))
548
549 #define FillOSError(rc) (os2_setsyserrno(rc),                           \
550                         Perl_severity = SEVERITY_ERROR) 
551
552 #define WinError_2_Perl_rc      \
553  (      init_PMWIN_entries(),   \
554         Perl_rc=(*PMWIN_entries.GetLastError)(perl_hab_GET()) )
555
556 /* Calling WinGetLastError() resets the error code of the current thread.
557    Since for some Win* API return value 0 is normal, one needs to call
558    this before calling them to distinguish normal and anomalous returns.  */
559 /*#define ResetWinError()       WinError_2_Perl_rc */
560
561 /* At this moment init_PMWIN_entries() should be a nop (WinInitialize should
562    be called already, right?), so we do not risk stepping over our own error */
563 #define FillWinError (  WinError_2_Perl_rc,                             \
564                         Perl_severity = ERRORIDSEV(Perl_rc),            \
565                         Perl_rc = ERRORIDERROR(Perl_rc),                \
566                         os2_setsyserrno(Perl_rc))
567
568 #define STATIC_FILE_LENGTH 127
569
570     /* This should match loadOrdinals[] array in os2.c */
571 enum entries_ordinals {
572     ORD_DosQueryExtLibpath,
573     ORD_DosSetExtLibpath,
574     ORD_DosVerifyPidTid,
575     ORD_SETHOSTENT,
576     ORD_SETNETENT, 
577     ORD_SETPROTOENT,
578     ORD_SETSERVENT,
579     ORD_GETHOSTENT,
580     ORD_GETNETENT, 
581     ORD_GETPROTOENT,
582     ORD_GETSERVENT,
583     ORD_ENDHOSTENT,
584     ORD_ENDNETENT,
585     ORD_ENDPROTOENT,
586     ORD_ENDSERVENT,
587     ORD_WinInitialize,
588     ORD_WinCreateMsgQueue,
589     ORD_WinDestroyMsgQueue,
590     ORD_WinPeekMsg,
591     ORD_WinGetMsg,
592     ORD_WinDispatchMsg,
593     ORD_WinGetLastError,
594     ORD_WinCancelShutdown,
595     ORD_RexxStart,
596     ORD_RexxVariablePool,
597     ORD_RexxRegisterFunctionExe,
598     ORD_RexxDeregisterFunction,
599     ORD_DOSSMSETTITLE,
600     ORD_PRF32QUERYPROFILESIZE,
601     ORD_PRF32OPENPROFILE,
602     ORD_PRF32CLOSEPROFILE,
603     ORD_PRF32QUERYPROFILE,
604     ORD_PRF32RESET,
605     ORD_PRF32QUERYPROFILEDATA,
606     ORD_PRF32WRITEPROFILEDATA,
607
608     ORD_WinChangeSwitchEntry,
609     ORD_WinQuerySwitchEntry,
610     ORD_WinQuerySwitchHandle,
611     ORD_WinQuerySwitchList,
612     ORD_WinSwitchToProgram,
613     ORD_WinBeginEnumWindows,
614     ORD_WinEndEnumWindows,
615     ORD_WinEnumDlgItem,
616     ORD_WinGetNextWindow,
617     ORD_WinIsChild,
618     ORD_WinQueryActiveWindow,
619     ORD_WinQueryClassName,
620     ORD_WinQueryFocus,
621     ORD_WinQueryWindow,
622     ORD_WinQueryWindowPos,
623     ORD_WinQueryWindowProcess,
624     ORD_WinQueryWindowText,
625     ORD_WinQueryWindowTextLength,
626     ORD_WinSetFocus,
627     ORD_WinSetWindowPos,
628     ORD_WinSetWindowText,
629     ORD_WinShowWindow,
630     ORD_WinIsWindow,
631     ORD_WinWindowFromId,
632     ORD_WinWindowFromPoint,
633     ORD_WinPostMsg,
634     ORD_WinEnableWindow,
635     ORD_WinEnableWindowUpdate,
636     ORD_WinIsWindowEnabled,
637     ORD_WinIsWindowShowing,
638     ORD_WinIsWindowVisible,
639     ORD_WinQueryWindowPtr,
640     ORD_WinQueryWindowULong,
641     ORD_WinQueryWindowUShort,
642     ORD_WinSetWindowBits,
643     ORD_WinSetWindowPtr,
644     ORD_WinSetWindowULong,
645     ORD_WinSetWindowUShort,
646     ORD_WinQueryDesktopWindow,
647     ORD_WinSetActiveWindow,
648     ORD_DosQueryModFromEIP,
649     ORD_Dos32QueryHeaderInfo,
650     ORD_DosTmrQueryFreq,
651     ORD_DosTmrQueryTime,
652     ORD_WinQueryActiveDesktopPathname,
653     ORD_WinInvalidateRect,
654     ORD_WinCreateFrameControls,
655     ORD_WinQueryClipbrdFmtInfo,
656     ORD_WinQueryClipbrdOwner,
657     ORD_WinQueryClipbrdViewer,
658     ORD_WinQueryClipbrdData,
659     ORD_WinOpenClipbrd,
660     ORD_WinCloseClipbrd,
661     ORD_WinSetClipbrdData,
662     ORD_WinSetClipbrdOwner,
663     ORD_WinSetClipbrdViewer,
664     ORD_WinEnumClipbrdFmts, 
665     ORD_WinEmptyClipbrd,
666     ORD_WinAddAtom,
667     ORD_WinFindAtom,
668     ORD_WinDeleteAtom,
669     ORD_WinQueryAtomUsage,
670     ORD_WinQueryAtomName,
671     ORD_WinQueryAtomLength,
672     ORD_WinQuerySystemAtomTable,
673     ORD_WinCreateAtomTable,
674     ORD_WinDestroyAtomTable,
675     ORD_WinOpenWindowDC,
676     ORD_DevOpenDC,
677     ORD_DevQueryCaps,
678     ORD_DevCloseDC,
679     ORD_WinMessageBox,
680     ORD_WinMessageBox2,
681     ORD_WinQuerySysValue,
682     ORD_WinSetSysValue,
683     ORD_WinAlarm,
684     ORD_WinFlashWindow,
685     ORD_WinLoadPointer,
686     ORD_WinQuerySysPointer,
687     ORD_DosReplaceModule,
688     ORD_NENTRIES
689 };
690
691 /* RET: return type, AT: argument signature in (), ARGS: should be in () */
692 #define CallORD(ret,o,at,args)  (((ret (*)at) loadByOrdinal(o, 1))args)
693 #define DeclFuncByORD(ret,name,o,at,args)       \
694   ret name at { return CallORD(ret,o,at,args); }
695 #define DeclVoidFuncByORD(name,o,at,args)       \
696   void name at { CallORD(void,o,at,args); }
697
698 /* This function returns error code on error, and saves the error info in $^E and Perl_rc */
699 #define DeclOSFuncByORD_native(ret,name,o,at,args)      \
700   ret name at { unsigned long rc; return CheckOSError(CallORD(ret,o,at,args)); }
701
702 /* These functions return false on error, and save the error info in $^E and Perl_rc */
703 #define DeclOSFuncByORD(ret,name,o,at,args)     \
704   ret name at { unsigned long rc; return !CheckOSError(CallORD(ret,o,at,args)); }
705 #define DeclWinFuncByORD(ret,name,o,at,args)    \
706   ret name at { return SaveWinError(CallORD(ret,o,at,args)); }
707
708 #define AssignFuncPByORD(p,o)   (*(Perl_PFN*)&(p) = (loadByOrdinal(o, 1)))
709
710 /* This flavor caches the procedure pointer (named as p__Win#name) locally */
711 #define DeclWinFuncByORD_CACHE(ret,name,o,at,args)      \
712         DeclWinFuncByORD_CACHE_r(ret,name,o,at,args,0,1)
713
714 /* This flavor may reset the last error before the call (if ret=0 may be OK) */
715 #define DeclWinFuncByORD_CACHE_resetError(ret,name,o,at,args)   \
716         DeclWinFuncByORD_CACHE_r(ret,name,o,at,args,1,1)
717
718 /* Two flavors below do the same as above, but do not auto-croak */
719 /* This flavor caches the procedure pointer (named as p__Win#name) locally */
720 #define DeclWinFuncByORD_CACHE_survive(ret,name,o,at,args)      \
721         DeclWinFuncByORD_CACHE_r(ret,name,o,at,args,0,0)
722
723 /* This flavor may reset the last error before the call (if ret=0 may be OK) */
724 #define DeclWinFuncByORD_CACHE_resetError_survive(ret,name,o,at,args)   \
725         DeclWinFuncByORD_CACHE_r(ret,name,o,at,args,1,0)
726
727 #define DeclWinFuncByORD_CACHE_r(ret,name,o,at,args,r,die)      \
728   static ret (*CAT2(p__Win,name)) at;                           \
729   static ret name at {                                          \
730         if (!CAT2(p__Win,name))                                 \
731             AssignFuncPByORD(CAT2(p__Win,name), o);             \
732         if (r) ResetWinError();                                 \
733         return SaveCroakWinError(CAT2(p__Win,name) args, die, "[Win]", STRINGIFY(name)); }
734
735 /* These flavors additionally assume ORD is name with prepended ORD_Win  */
736 #define DeclWinFunc_CACHE(ret,name,at,args)     \
737         DeclWinFuncByORD_CACHE(ret,name,CAT2(ORD_Win,name),at,args)
738 #define DeclWinFunc_CACHE_resetError(ret,name,at,args)  \
739         DeclWinFuncByORD_CACHE_resetError(ret,name,CAT2(ORD_Win,name),at,args)
740 #define DeclWinFunc_CACHE_survive(ret,name,at,args)     \
741         DeclWinFuncByORD_CACHE_survive(ret,name,CAT2(ORD_Win,name),at,args)
742 #define DeclWinFunc_CACHE_resetError_survive(ret,name,at,args)  \
743         DeclWinFuncByORD_CACHE_resetError_survive(ret,name,CAT2(ORD_Win,name),at,args)
744
745 void ResetWinError(void);
746 void CroakWinError(int die, char *name);
747
748 #define PERLLIB_MANGLE(s, n) perllib_mangle((s), (n))
749 char *perllib_mangle(char *, unsigned int);
750
751 #define fork    fork_with_resources
752
753 #ifdef EINTR                            /* x2p do not include perl.h!!! */
754 static __inline__ int
755 my_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
756 {
757   if (nfds == 0 && timeout && (_emx_env & 0x200)) {
758     if (DosSleep(1000 * timeout->tv_sec + (timeout->tv_usec + 500)/1000) == 0)
759       return 0;
760     errno = EINTR;
761     return -1;
762   }
763   return select(nfds, readfds, writefds, exceptfds, timeout);
764 }
765
766 #define select          my_select
767 #endif
768
769
770 typedef int (*Perl_PFN)();
771 Perl_PFN loadByOrdinal(enum entries_ordinals ord, int fail);
772 extern const Perl_PFN * const pExtFCN;
773 char *os2error(int rc);
774 int os2_stat(const char *name, struct stat *st);
775 int fork_with_resources();
776 int setpriority(int which, int pid, int val);
777 int getpriority(int which /* ignored */, int pid);
778
779 void croak_with_os2error(char *s) __attribute__((noreturn));
780
781 #ifdef PERL_CORE
782 int os2_do_spawn(pTHX_ char *cmd);
783 int os2_do_aspawn(pTHX_ SV *really, SV **vmark, SV **vsp);
784 #endif
785
786 #ifndef LOG_DAEMON
787
788 /* Replacement for syslog.h */
789 #  define       LOG_EMERG       0       /* system is unusable */
790 #  define       LOG_ALERT       1       /* action must be taken immediately */
791 #  define       LOG_CRIT        2       /* critical conditions */
792 #  define       LOG_ERR 3       /* error conditions */
793 #  define       LOG_WARNING     4       /* warning conditions */
794 #  define       LOG_NOTICE      5       /* normal but significant condition */
795 #  define       LOG_INFO        6       /* informational */
796 #  define       LOG_DEBUG       7       /* debug-level messages */
797
798 #  define       LOG_PRIMASK     0x007   /* mask to extract priority part (internal) */
799                                 /* extract priority */
800 #  define       LOG_PRI(p)      ((p) & LOG_PRIMASK)
801 #  define       LOG_MAKEPRI(fac, pri)   (((fac) << 3) | (pri))
802
803 /* facility codes */
804 #  define       LOG_KERN        (0<<3)  /* kernel messages */
805 #  define       LOG_USER        (1<<3)  /* random user-level messages */
806 #  define       LOG_MAIL        (2<<3)  /* mail system */
807 #  define       LOG_DAEMON      (3<<3)  /* system daemons */
808 #  define       LOG_AUTH        (4<<3)  /* security/authorization messages */
809 #  define       LOG_SYSLOG      (5<<3)  /* messages generated internally by syslogd */
810 #  define       LOG_LPR (6<<3)  /* line printer subsystem */
811 #  define       LOG_NEWS        (7<<3)  /* network news subsystem */
812 #  define       LOG_UUCP        (8<<3)  /* UUCP subsystem */
813 #  define       LOG_CRON        (15<<3) /* clock daemon */
814         /* other codes through 15 reserved for system use */
815 #  define       LOG_LOCAL0      (16<<3) /* reserved for local use */
816 #  define       LOG_LOCAL1      (17<<3) /* reserved for local use */
817 #  define       LOG_LOCAL2      (18<<3) /* reserved for local use */
818 #  define       LOG_LOCAL3      (19<<3) /* reserved for local use */
819 #  define       LOG_LOCAL4      (20<<3) /* reserved for local use */
820 #  define       LOG_LOCAL5      (21<<3) /* reserved for local use */
821 #  define       LOG_LOCAL6      (22<<3) /* reserved for local use */
822 #  define       LOG_LOCAL7      (23<<3) /* reserved for local use */
823
824 #  define       LOG_NFACILITIES 24      /* current number of facilities */
825 #  define       LOG_FACMASK     0x03f8  /* mask to extract facility part */
826                                 /* facility of pri */
827 #  define       LOG_FAC(p)      (((p) & LOG_FACMASK) >> 3)
828
829 /*
830  * arguments to setlogmask.
831  */
832 #  define       LOG_MASK(pri)   (1 << (pri))            /* mask for one priority */
833 #  define       LOG_UPTO(pri)   ((1 << ((pri)+1)) - 1)  /* all priorities through pri */
834
835 /*
836  * Option flags for openlog.
837  *
838  * LOG_ODELAY no longer does anything.
839  * LOG_NDELAY is the inverse of what it used to be.
840  */
841 #  define       LOG_PID         0x01    /* log the pid with each message */
842 #  define       LOG_CONS        0x02    /* log on the console if errors in sending */
843 #  define       LOG_ODELAY      0x04    /* delay open until first syslog() (default) */
844 #  define       LOG_NDELAY      0x08    /* don't delay open */
845 #  define       LOG_NOWAIT      0x10    /* don't wait for console forks: DEPRECATED */
846 #  define       LOG_PERROR      0x20    /* log to stderr as well */
847
848 #endif
849
850 /* ************************************************************ */
851 #define Dos32QuerySysState DosQuerySysState
852 #define QuerySysState(flags, pid, buf, bufsz) \
853         Dos32QuerySysState(flags, 0,  pid, 0, buf, bufsz)
854
855 #define QSS_PROCESS     1
856 #define QSS_MODULE      4
857 #define QSS_SEMAPHORES  2
858 #define QSS_FILE        8               /* Buggy until fixpack18 */
859 #define QSS_SHARED      16
860
861 #ifdef _OS2_H
862
863 APIRET APIENTRY Dos32QuerySysState(ULONG func,ULONG arg1,ULONG pid,
864                         ULONG _res_,PVOID buf,ULONG bufsz);
865 typedef struct {
866         ULONG   threadcnt;
867         ULONG   proccnt;
868         ULONG   modulecnt;
869 } QGLOBAL, *PQGLOBAL;
870
871 typedef struct {
872         ULONG   rectype;
873         USHORT  threadid;
874         USHORT  slotid;
875         ULONG   sleepid;
876         ULONG   priority;
877         ULONG   systime;
878         ULONG   usertime;
879         UCHAR   state;
880         UCHAR   _reserved1_;    /* padding to ULONG */
881         USHORT  _reserved2_;    /* padding to ULONG */
882 } QTHREAD, *PQTHREAD;
883
884 typedef struct {
885         USHORT  sfn;
886         USHORT  refcnt;
887         USHORT  flags1;
888         USHORT  flags2;
889         USHORT  accmode1;
890         USHORT  accmode2;
891         ULONG   filesize;
892         USHORT  volhnd;
893         USHORT  attrib;
894         USHORT  _reserved_;
895 } QFDS, *PQFDS;
896
897 typedef struct qfile {
898         ULONG           rectype;
899         struct qfile    *next;
900         ULONG           opencnt;
901         PQFDS           filedata;
902         char            name[1];
903 } QFILE, *PQFILE;
904
905 typedef struct {
906         ULONG   rectype;
907         PQTHREAD threads;
908         USHORT  pid;
909         USHORT  ppid;
910         ULONG   type;
911         ULONG   state;
912         ULONG   sessid;
913         USHORT  hndmod;
914         USHORT  threadcnt;
915         ULONG   privsem32cnt;
916         ULONG   _reserved2_;
917         USHORT  sem16cnt;
918         USHORT  dllcnt;
919         USHORT  shrmemcnt;
920         USHORT  fdscnt;
921         PUSHORT sem16s;
922         PUSHORT dlls;
923         PUSHORT shrmems;
924         PUSHORT fds;
925 } QPROCESS, *PQPROCESS;
926
927 typedef struct sema {
928         struct sema *next;
929         USHORT  refcnt;
930         UCHAR   sysflags;
931         UCHAR   sysproccnt;
932         ULONG   _reserved1_;
933         USHORT  index;
934         CHAR    name[1];
935 } QSEMA, *PQSEMA;
936
937 typedef struct {
938         ULONG   rectype;
939         ULONG   _reserved1_;
940         USHORT  _reserved2_;
941         USHORT  syssemidx;
942         ULONG   index;
943         QSEMA   sema;
944 } QSEMSTRUC, *PQSEMSTRUC;
945
946 typedef struct {
947         USHORT  pid;
948         USHORT  opencnt;
949 } QSEMOWNER32, *PQSEMOWNER32;
950
951 typedef struct {
952         PQSEMOWNER32    own;
953         PCHAR           name;
954         PVOID           semrecs; /* array of associated sema's */
955         USHORT          flags;
956         USHORT          semreccnt;
957         USHORT          waitcnt;
958         USHORT          _reserved_;     /* padding to ULONG */
959 } QSEMSMUX32, *PQSEMSMUX32;
960
961 typedef struct {
962         PQSEMOWNER32    own;
963         PCHAR           name;
964         PQSEMSMUX32     mux;
965         USHORT          flags;
966         USHORT          postcnt;
967 } QSEMEV32, *PQSEMEV32;
968
969 typedef struct {
970         PQSEMOWNER32    own;
971         PCHAR           name;
972         PQSEMSMUX32     mux;
973         USHORT          flags;
974         USHORT          refcnt;
975         USHORT          thrdnum;
976         USHORT          _reserved_;     /* padding to ULONG */
977 } QSEMMUX32, *PQSEMMUX32;
978
979 typedef struct semstr32 {
980         struct semstr *next;
981         QSEMEV32 evsem;
982         QSEMMUX32  muxsem;
983         QSEMSMUX32 smuxsem;
984 } QSEMSTRUC32, *PQSEMSTRUC32;
985
986 typedef struct shrmem {
987         struct shrmem *next;
988         USHORT  hndshr;
989         USHORT  selshr;
990         USHORT  refcnt;
991         CHAR    name[1];
992 } QSHRMEM, *PQSHRMEM;
993
994 typedef struct module {
995         struct module *next;
996         USHORT  hndmod;
997         USHORT  type;
998         ULONG   refcnt;
999         ULONG   segcnt;
1000         PVOID   _reserved_;
1001         PCHAR   name;
1002         USHORT  modref[1];
1003 } QMODULE, *PQMODULE;
1004
1005 typedef struct {
1006         PQGLOBAL        gbldata;
1007         PQPROCESS       procdata;
1008         PQSEMSTRUC      semadata;
1009         PQSEMSTRUC32    sem32data;
1010         PQSHRMEM        shrmemdata;
1011         PQMODULE        moddata;
1012         PVOID           _reserved2_;
1013         PQFILE          filedata;
1014 } QTOPLEVEL, *PQTOPLEVEL;
1015 /* ************************************************************ */
1016
1017 PQTOPLEVEL get_sysinfo(ULONG pid, ULONG flags);
1018
1019 #endif /* _OS2_H */
1020