Todo updates from Andy Dougherty <doughera@lafayette.edu>
[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) 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(getpid(),SIGABRT);
68
69 #define BIT_BUCKET "/dev/nul"  /* Will this work? */
70
71 #if defined(I_SYS_UN) && !defined(TCPIPV4)
72 /* It is not working without TCPIPV4 defined. */
73 # undef I_SYS_UN
74 #endif 
75
76 #ifdef USE_THREADS
77
78 #define OS2_ERROR_ALREADY_POSTED 299    /* Avoid os2.h */
79
80 extern int rc;
81
82 #define MUTEX_INIT(m) \
83     STMT_START {                                                \
84         int rc;                                                 \
85         if ((rc = _rmutex_create(m,0)))                         \
86             croak("panic: MUTEX_INIT: rc=%i", rc);              \
87     } STMT_END
88 #define MUTEX_LOCK(m) \
89     STMT_START {                                                \
90         int rc;                                                 \
91         if ((rc = _rmutex_request(m,_FMR_IGNINT)))              \
92             croak("panic: MUTEX_LOCK: rc=%i", rc);              \
93     } STMT_END
94 #define MUTEX_UNLOCK(m) \
95     STMT_START {                                                \
96         int rc;                                                 \
97         if ((rc = _rmutex_release(m)))                          \
98             croak("panic: MUTEX_UNLOCK: rc=%i", rc);            \
99     } STMT_END
100 #define MUTEX_DESTROY(m) \
101     STMT_START {                                                \
102         int rc;                                                 \
103         if ((rc = _rmutex_close(m)))                            \
104             croak("panic: MUTEX_DESTROY: rc=%i", rc);           \
105     } STMT_END
106
107 #define COND_INIT(c) \
108     STMT_START {                                                \
109         int rc;                                                 \
110         if ((rc = DosCreateEventSem(NULL,c,0,0)))               \
111             croak("panic: COND_INIT: rc=%i", rc);               \
112     } STMT_END
113 #define COND_SIGNAL(c) \
114     STMT_START {                                                \
115         int rc;                                                 \
116         if ((rc = DosPostEventSem(*(c))) && rc != OS2_ERROR_ALREADY_POSTED)             \
117             croak("panic: COND_SIGNAL, rc=%ld", rc);            \
118     } STMT_END
119 #define COND_BROADCAST(c) \
120     STMT_START {                                                \
121         int rc;                                                 \
122         if ((rc = DosPostEventSem(*(c))) && rc != OS2_ERROR_ALREADY_POSTED)\
123             croak("panic: COND_BROADCAST, rc=%i", rc);          \
124     } STMT_END
125 /* #define COND_WAIT(c, m) \
126     STMT_START {                                                \
127         if (WaitForSingleObject(*(c),INFINITE) == WAIT_FAILED)  \
128             croak("panic: COND_WAIT");                          \
129     } STMT_END
130 */
131 #define COND_WAIT(c, m) os2_cond_wait(c,m)
132
133 #define COND_WAIT_win32(c, m) \
134     STMT_START {                                                \
135         int rc;                                                 \
136         if ((rc = SignalObjectAndWait(*(m),*(c),INFINITE,FALSE)))\
137             croak("panic: COND_WAIT");                          \
138         else                                                    \
139             MUTEX_LOCK(m);                                      \
140     } STMT_END
141 #define COND_DESTROY(c) \
142     STMT_START {                                                \
143         int rc;                                                 \
144         if ((rc = DosCloseEventSem(*(c))))                      \
145             croak("panic: COND_DESTROY, rc=%i", rc);            \
146     } STMT_END
147 /*#define THR ((struct thread *) TlsGetValue(PL_thr_key))
148 #define dTHR struct thread *thr = THR
149 */
150
151 #define pthread_getspecific(k)          (*_threadstore())
152 #define pthread_setspecific(k,v)        (*_threadstore()=v,0)
153 #define pthread_self()                  _gettid()
154 #define pthread_key_create(keyp,flag)   (*keyp=_gettid(),0)
155 #define sched_yield()   DosSleep(0)
156
157 #ifdef PTHREADS_INCLUDED                /* For ./x2p stuff. */
158 int pthread_join(pthread_t tid, void **status);
159 int pthread_detach(pthread_t tid);
160 int pthread_create(pthread_t *tid, const pthread_attr_t *attr,
161                    void *(*start_routine)(void*), void *arg);
162 #endif 
163
164 #define THREADS_ELSEWHERE
165
166 #endif 
167  
168 void Perl_OS2_init(char **);
169
170 /* XXX This code hideously puts env inside: */
171
172 #ifdef __EMX__
173 #  define PERL_SYS_INIT(argcp, argvp) STMT_START {      \
174     _response(argcp, argvp);                    \
175     _wildcard(argcp, argvp);                    \
176     Perl_OS2_init(env); } STMT_END
177 #else  /* Compiling embedded Perl with non-EMX compiler */
178 #  define PERL_SYS_INIT(argcp, argvp) STMT_START {      \
179     Perl_OS2_init(env); } STMT_END
180 #  define PERL_CALLCONV _System
181 #endif
182 #define PERL_SYS_TERM()         MALLOC_TERM
183
184 /* #define PERL_SYS_TERM() STMT_START { \
185     if (Perl_HAB_set) WinTerminate(Perl_hab);   } STMT_END */
186
187 #define dXSUB_SYS OS2_XS_init()
188
189 #ifdef PERL_IS_AOUT
190 /* #  define HAS_FORK */
191 /* #  define HIDEMYMALLOC */
192 /* #  define PERL_SBRK_VIA_MALLOC */ /* gets off-page sbrk... */
193 #else /* !PERL_IS_AOUT */
194 #  ifndef PERL_FOR_X2P
195 #    ifdef EMX_BAD_SBRK
196 #      define USE_PERL_SBRK
197 #    endif 
198 #  else
199 #    define PerlIO FILE
200 #  endif 
201 #  define SYSTEM_ALLOC(a) sys_alloc(a)
202
203 void *sys_alloc(int size);
204
205 #endif /* !PERL_IS_AOUT */
206 #if !defined(PERL_CORE) && !defined(PerlIO) /* a2p */
207 #  define PerlIO FILE
208 #endif 
209
210 #define TMPPATH tmppath
211 #define TMPPATH1 "plXXXXXX"
212 extern char *tmppath;
213 PerlIO *my_syspopen(char *cmd, char *mode);
214 /* Cannot prototype with I32 at this point. */
215 int my_syspclose(PerlIO *f);
216 FILE *my_tmpfile (void);
217 char *my_tmpnam (char *);
218
219 #define tmpfile my_tmpfile
220 #define tmpnam  my_tmpnam
221 #define isatty  _isterm
222 #define rand    random
223 #define srand   srandom
224
225 /*
226  * fwrite1() should be a routine with the same calling sequence as fwrite(),
227  * but which outputs all of the bytes requested as a single stream (unlike
228  * fwrite() itself, which on some systems outputs several distinct records
229  * if the number_of_items parameter is >1).
230  */
231 #define fwrite1 fwrite
232
233 #define my_getenv(var) getenv(var)
234 #define flock   my_flock
235
236 void *emx_calloc (size_t, size_t);
237 void emx_free (void *);
238 void *emx_malloc (size_t);
239 void *emx_realloc (void *, size_t);
240
241 /*****************************************************************************/
242
243 #include <stdlib.h>     /* before the following definitions */
244 #include <unistd.h>     /* before the following definitions */
245
246 #define chdir   _chdir2
247 #define getcwd  _getcwd2
248
249 /* This guy is needed for quick stdstd  */
250
251 #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE)
252         /* Perl uses ungetc only with successful return */
253 #  define ungetc(c,fp) \
254         (FILE_ptr(fp) > FILE_base(fp) && c == (int)*(FILE_ptr(fp) - 1) \
255          ? (--FILE_ptr(fp), ++FILE_cnt(fp), (int)c) : ungetc(c,fp))
256 #endif
257
258 #define OP_BINARY O_BINARY
259
260 #define OS2_STAT_HACK 1
261 #if OS2_STAT_HACK
262
263 #define Stat(fname,bufptr) os2_stat((fname),(bufptr))
264 #define Fstat(fd,bufptr)   fstat((fd),(bufptr))
265 #define Fflush(fp)         fflush(fp)
266 #define Mkdir(path,mode)   mkdir((path),(mode))
267
268 #undef S_IFBLK
269 #undef S_ISBLK
270 #define S_IFBLK         0120000
271 #define S_ISBLK(mode)   (((mode) & S_IFMT) == S_IFBLK)
272
273 #else
274
275 #define Stat(fname,bufptr) stat((fname),(bufptr))
276 #define Fstat(fd,bufptr)   fstat((fd),(bufptr))
277 #define Fflush(fp)         fflush(fp)
278 #define Mkdir(path,mode)   mkdir((path),(mode))
279
280 #endif
281
282 /* With SD386 it is impossible to debug register variables. */
283 #if !defined(PERL_IS_AOUT) && defined(DEBUGGING) && !defined(register)
284 #  define register
285 #endif
286
287 /* Our private OS/2 specific data. */
288
289 typedef struct OS2_Perl_data {
290   unsigned long flags;
291   unsigned long phab;
292   int (*xs_init)();
293   unsigned long rc;
294   unsigned long severity;
295   unsigned long phmq;                   /* Handle to message queue */
296   unsigned long phmq_refcnt;
297   unsigned long phmq_servers;
298   unsigned long initial_mode;           /* VIO etc. mode we were started in */
299 } OS2_Perl_data_t;
300
301 extern OS2_Perl_data_t OS2_Perl_data;
302
303 #define Perl_hab                ((HAB)OS2_Perl_data.phab)
304 #define Perl_rc                 (OS2_Perl_data.rc)
305 #define Perl_severity           (OS2_Perl_data.severity)
306 #define errno_isOS2             12345678
307 #define OS2_Perl_flags  (OS2_Perl_data.flags)
308 #define Perl_HAB_set_f  1
309 #define Perl_HAB_set    (OS2_Perl_flags & Perl_HAB_set_f)
310 #define set_Perl_HAB_f  (OS2_Perl_flags |= Perl_HAB_set_f)
311 #define set_Perl_HAB(h) (set_Perl_HAB_f, Perl_hab = h)
312 #define _obtain_Perl_HAB (init_PMWIN_entries(),                         \
313                           Perl_hab = (*PMWIN_entries.Initialize)(0),    \
314                           set_Perl_HAB_f, Perl_hab)
315 #define perl_hab_GET()  (Perl_HAB_set ? Perl_hab : _obtain_Perl_HAB)
316 #define Acquire_hab()   perl_hab_GET()
317 #define Perl_hmq        ((HMQ)OS2_Perl_data.phmq)
318 #define Perl_hmq_refcnt (OS2_Perl_data.phmq_refcnt)
319 #define Perl_hmq_servers        (OS2_Perl_data.phmq_servers)
320 #define Perl_os2_initial_mode   (OS2_Perl_data.initial_mode)
321
322 unsigned long Perl_hab_GET();
323 unsigned long Perl_Register_MQ(int serve);
324 void    Perl_Deregister_MQ(int serve);
325 int     Perl_Serve_Messages(int force);
326 /* Cannot prototype with I32 at this point. */
327 int     Perl_Process_Messages(int force, long *cntp);
328
329 struct _QMSG;
330 struct PMWIN_entries_t {
331     unsigned long (*Initialize)( unsigned long fsOptions );
332     unsigned long (*CreateMsgQueue)(unsigned long hab, long cmsg);
333     int (*DestroyMsgQueue)(unsigned long hmq);
334     int (*PeekMsg)(unsigned long hab, struct _QMSG *pqmsg,
335                    unsigned long hwndFilter, unsigned long msgFilterFirst,
336                    unsigned long msgFilterLast, unsigned long fl);
337     int (*GetMsg)(unsigned long hab, struct _QMSG *pqmsg,
338                   unsigned long hwndFilter, unsigned long msgFilterFirst,
339                   unsigned long msgFilterLast);
340     void * (*DispatchMsg)(unsigned long hab, struct _QMSG *pqmsg);
341 };
342 extern struct PMWIN_entries_t PMWIN_entries;
343 void init_PMWIN_entries(void);
344
345 #define perl_hmq_GET(serve)     Perl_Register_MQ(serve);
346 #define perl_hmq_UNSET(serve)   Perl_Deregister_MQ(serve);
347
348 #define OS2_XS_init() (*OS2_Perl_data.xs_init)()
349 /* The expressions below return true on error. */
350 /* INCL_DOSERRORS needed. rc should be declared outside. */
351 #define CheckOSError(expr) (!(rc = (expr)) ? 0 : (FillOSError(rc), 1))
352 /* INCL_WINERRORS needed. */
353 #define SaveWinError(expr) ((expr) ? : (FillWinError, 0))
354 #define CheckWinError(expr) ((expr) ? 0: (FillWinError, 1))
355 #define FillOSError(rc) (Perl_rc = rc,                                  \
356                         errno = errno_isOS2,                            \
357                         Perl_severity = SEVERITY_ERROR) 
358 #define FillWinError (Perl_rc = WinGetLastError(Perl_hab),              \
359                         errno = errno_isOS2,                            \
360                         Perl_severity = ERRORIDSEV(Perl_rc),            \
361                         Perl_rc = ERRORIDERROR(Perl_rc)) 
362
363 #define STATIC_FILE_LENGTH 127
364
365 #define PERLLIB_MANGLE(s, n) perllib_mangle((s), (n))
366 char *perllib_mangle(char *, unsigned int);
367
368 char *os2error(int rc);
369
370 /* ************************************************************ */
371 #define Dos32QuerySysState DosQuerySysState
372 #define QuerySysState(flags, pid, buf, bufsz) \
373         Dos32QuerySysState(flags, 0,  pid, 0, buf, bufsz)
374
375 #define QSS_PROCESS     1
376 #define QSS_MODULE      4
377 #define QSS_SEMAPHORES  2
378 #define QSS_FILE        8               /* Buggy until fixpack18 */
379 #define QSS_SHARED      16
380
381 #ifdef _OS2EMX_H
382
383 APIRET APIENTRY Dos32QuerySysState(ULONG func,ULONG arg1,ULONG pid,
384                         ULONG _res_,PVOID buf,ULONG bufsz);
385 typedef struct {
386         ULONG   threadcnt;
387         ULONG   proccnt;
388         ULONG   modulecnt;
389 } QGLOBAL, *PQGLOBAL;
390
391 typedef struct {
392         ULONG   rectype;
393         USHORT  threadid;
394         USHORT  slotid;
395         ULONG   sleepid;
396         ULONG   priority;
397         ULONG   systime;
398         ULONG   usertime;
399         UCHAR   state;
400         UCHAR   _reserved1_;    /* padding to ULONG */
401         USHORT  _reserved2_;    /* padding to ULONG */
402 } QTHREAD, *PQTHREAD;
403
404 typedef struct {
405         USHORT  sfn;
406         USHORT  refcnt;
407         USHORT  flags1;
408         USHORT  flags2;
409         USHORT  accmode1;
410         USHORT  accmode2;
411         ULONG   filesize;
412         USHORT  volhnd;
413         USHORT  attrib;
414         USHORT  _reserved_;
415 } QFDS, *PQFDS;
416
417 typedef struct qfile {
418         ULONG           rectype;
419         struct qfile    *next;
420         ULONG           opencnt;
421         PQFDS           filedata;
422         char            name[1];
423 } QFILE, *PQFILE;
424
425 typedef struct {
426         ULONG   rectype;
427         PQTHREAD threads;
428         USHORT  pid;
429         USHORT  ppid;
430         ULONG   type;
431         ULONG   state;
432         ULONG   sessid;
433         USHORT  hndmod;
434         USHORT  threadcnt;
435         ULONG   privsem32cnt;
436         ULONG   _reserved2_;
437         USHORT  sem16cnt;
438         USHORT  dllcnt;
439         USHORT  shrmemcnt;
440         USHORT  fdscnt;
441         PUSHORT sem16s;
442         PUSHORT dlls;
443         PUSHORT shrmems;
444         PUSHORT fds;
445 } QPROCESS, *PQPROCESS;
446
447 typedef struct sema {
448         struct sema *next;
449         USHORT  refcnt;
450         UCHAR   sysflags;
451         UCHAR   sysproccnt;
452         ULONG   _reserved1_;
453         USHORT  index;
454         CHAR    name[1];
455 } QSEMA, *PQSEMA;
456
457 typedef struct {
458         ULONG   rectype;
459         ULONG   _reserved1_;
460         USHORT  _reserved2_;
461         USHORT  syssemidx;
462         ULONG   index;
463         QSEMA   sema;
464 } QSEMSTRUC, *PQSEMSTRUC;
465
466 typedef struct {
467         USHORT  pid;
468         USHORT  opencnt;
469 } QSEMOWNER32, *PQSEMOWNER32;
470
471 typedef struct {
472         PQSEMOWNER32    own;
473         PCHAR           name;
474         PVOID           semrecs; /* array of associated sema's */
475         USHORT          flags;
476         USHORT          semreccnt;
477         USHORT          waitcnt;
478         USHORT          _reserved_;     /* padding to ULONG */
479 } QSEMSMUX32, *PQSEMSMUX32;
480
481 typedef struct {
482         PQSEMOWNER32    own;
483         PCHAR           name;
484         PQSEMSMUX32     mux;
485         USHORT          flags;
486         USHORT          postcnt;
487 } QSEMEV32, *PQSEMEV32;
488
489 typedef struct {
490         PQSEMOWNER32    own;
491         PCHAR           name;
492         PQSEMSMUX32     mux;
493         USHORT          flags;
494         USHORT          refcnt;
495         USHORT          thrdnum;
496         USHORT          _reserved_;     /* padding to ULONG */
497 } QSEMMUX32, *PQSEMMUX32;
498
499 typedef struct semstr32 {
500         struct semstr *next;
501         QSEMEV32 evsem;
502         QSEMMUX32  muxsem;
503         QSEMSMUX32 smuxsem;
504 } QSEMSTRUC32, *PQSEMSTRUC32;
505
506 typedef struct shrmem {
507         struct shrmem *next;
508         USHORT  hndshr;
509         USHORT  selshr;
510         USHORT  refcnt;
511         CHAR    name[1];
512 } QSHRMEM, *PQSHRMEM;
513
514 typedef struct module {
515         struct module *next;
516         USHORT  hndmod;
517         USHORT  type;
518         ULONG   refcnt;
519         ULONG   segcnt;
520         PVOID   _reserved_;
521         PCHAR   name;
522         USHORT  modref[1];
523 } QMODULE, *PQMODULE;
524
525 typedef struct {
526         PQGLOBAL        gbldata;
527         PQPROCESS       procdata;
528         PQSEMSTRUC      semadata;
529         PQSEMSTRUC32    sem32data;
530         PQSHRMEM        shrmemdata;
531         PQMODULE        moddata;
532         PVOID           _reserved2_;
533         PQFILE          filedata;
534 } QTOPLEVEL, *PQTOPLEVEL;
535 /* ************************************************************ */
536
537 PQTOPLEVEL get_sysinfo(ULONG pid, ULONG flags);
538
539 #endif /* _OS2EMX_H */
540