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