blob: 5596e346da34ad9bcf5071163ce63bd82101d045 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * os_msdos.c
12 *
13 * MSDOS system-dependent routines.
14 * A cheap plastic imitation of the amiga dependent code.
15 * A lot in this file was made by Juergen Weigert (jw).
16 *
17 * DJGPP changes by Gert van Antwerpen
18 * Faster text screens by John Lange (jlange@zilker.net)
19 * Windows clipboard functionality added by David Kotchan (dk)
20 *
21 * Some functions are also used for Win16 (MS-Windows 3.1).
22 */
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#include "vim.h"
25
Bram Moolenaar82881492012-11-20 16:53:39 +010026/* cproto fails on missing include files */
27#ifndef PROTO
28# include <conio.h>
29#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
Bram Moolenaar82881492012-11-20 16:53:39 +010031#ifndef PROTO
32# include <bios.h>
33# ifdef DJGPP
34# include <dpmi.h>
35# include <signal.h>
36# include <sys/movedata.h>
37# include <crt0.h>
38# ifdef FEAT_CLIPBOARD
39# include <sys/segments.h>
40# endif
41# else
42# include <alloc.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#endif
45
46#if defined(DJGPP) || defined(PROTO)
47# define _cdecl /* DJGPP doesn't have this */
48#endif
49
50static int cbrk_pressed = FALSE; /* set by ctrl-break interrupt */
51static int ctrlc_pressed = FALSE; /* set when ctrl-C or ctrl-break detected */
52static int delayed_redraw = FALSE; /* set when ctrl-C detected */
53
54static int bioskey_read = _NKEYBRD_READ; /* bioskey() argument: read key */
55static int bioskey_ready = _NKEYBRD_READY; /* bioskey() argument: key ready? */
56
57#ifdef FEAT_MOUSE
58static int mouse_avail = FALSE; /* mouse present */
59static int mouse_active; /* mouse enabled */
60static int mouse_hidden; /* mouse not shown */
61static int mouse_click = -1; /* mouse status */
62static int mouse_last_click = -1; /* previous status at click */
Bram Moolenaar035db9f2007-05-10 18:02:27 +000063static int mouse_x = -1; /* mouse x coordinate */
64static int mouse_y = -1; /* mouse y coordinate */
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static long mouse_click_time = 0; /* biostime() of last click */
66static int mouse_click_count = 0; /* count for multi-clicks */
67static int mouse_click_x = 0; /* x of previous mouse click */
68static int mouse_click_y = 0; /* y of previous mouse click */
69static linenr_T mouse_topline = 0; /* w_topline at previous mouse click */
70#ifdef FEAT_DIFF
71static int mouse_topfill = 0; /* w_topfill at previous mouse click */
72#endif
73static int mouse_x_div = 8; /* column = x coord / mouse_x_div */
74static int mouse_y_div = 8; /* line = y coord / mouse_y_div */
75#endif
76
77#define BIOSTICK 55 /* biostime() increases one tick about
78 every 55 msec */
79
80static int orig_attr = 0x0700; /* video attributes when starting */
81
82static int S_iLeft = 0; /* Scroll window; these are 1 offset */
83static int S_iTop = 0;
84static int S_iRight = 0;
85static int S_iBottom = 0;
86
87/*
88 * Need to remember the values, because we set horizontal and vertical
89 * edges separately.
90 */
91 static void
92mywindow(int iLeft, int iTop, int iRight, int iBottom)
93{
94 S_iLeft = iLeft;
95 S_iTop = iTop;
96 S_iRight = iRight;
97 S_iBottom = iBottom;
98 window(iLeft, iTop, iRight, iBottom);
99}
100
101#ifdef DJGPP
102/*
103 * For DJGPP, use our own functions for fast text screens. JML 1/18/98
104 */
105
106unsigned long S_ulScreenBase = 0xb8000;
107unsigned short S_uiAttribute = 0;
108int S_iCurrentRow = 0; /* These are 0 offset */
109int S_iCurrentColumn = 0;
110short S_selVideo; /* Selector for DJGPP direct video transfers */
111
112/*
113 * Use burst writes to improve mch_write speed - VJN 01/10/99
114 */
115unsigned short S_linebuffer[8000]; /* <VN> enough for 160x50 */
116unsigned short S_blankbuffer[256]; /* <VN> max length of console line */
117unsigned short *S_linebufferpos = S_linebuffer;
118int S_iBufferRow;
119int S_iBufferColumn;
120
121 static void
122myflush(void)
123{
124 if (S_linebufferpos != S_linebuffer)
125 {
126 _dosmemputw(S_linebuffer, (S_linebufferpos - S_linebuffer),
127 S_ulScreenBase
128 + S_iBufferRow * (Columns << 1) + (S_iBufferColumn << 1));
129 S_linebufferpos = S_linebuffer;
130 }
131}
132
133 static void
134mygotoxy(int x, int y)
135{
136 S_iCurrentRow = y - 1;
137 S_iCurrentColumn = x - 1;
138}
139
140/*
141 * Set the system cursor to our cursor position.
142 */
143 static void
144set_sys_cursor(void)
145{
146 if (term_console && full_screen)
147 {
148 myflush();
149 gotoxy(S_iCurrentColumn + 1, S_iCurrentRow + 1);
150 }
151}
152
153 static void
154setblankbuffer(unsigned short uiValue)
155{
156 int i;
157 static unsigned short olduiValue = 0;
158
159 if (olduiValue != uiValue)
160 {
161 /* Load blank line buffer with spaces */
162 for (i = 0; i < Columns; ++i)
163 S_blankbuffer[i] = uiValue;
164 olduiValue = uiValue;
165 }
166}
167
168 static void
169myclreol(void)
170{
171 /* Clear to end of line */
172 setblankbuffer(S_uiAttribute | ' ');
173 _dosmemputw(S_blankbuffer, S_iRight - S_iCurrentColumn, S_ulScreenBase
174 + (S_iCurrentRow) * (Columns << 1)
175 + (S_iCurrentColumn << 1));
176}
177
178 static void
179myclrscr(void)
180{
181 /* Clear whole screen */
182 short iColumn;
183 int endpoint = (Rows * Columns) << 1;
184
185 setblankbuffer(S_uiAttribute | ' ');
186
187 for (iColumn = 0; iColumn < endpoint; iColumn += (Columns << 1))
188 _dosmemputw(S_blankbuffer, Columns, S_ulScreenBase + iColumn);
189}
190
191 static void
192mydelline(void)
193{
194 short iRow, iColumn;
195
196 iColumn = (S_iLeft - 1) << 1;
197
198 /* Copy the lines underneath */
199 for (iRow = S_iCurrentRow; iRow < S_iBottom - 1; iRow++)
200 movedata(S_selVideo, (((iRow + 1) * Columns) << 1) + iColumn,
201 S_selVideo, ((iRow * Columns) << 1) + iColumn,
202 (S_iRight - S_iLeft + 1) << 1);
203
204 /* Clear the new row */
205 setblankbuffer(S_uiAttribute | ' ');
206
207 _dosmemputw(S_blankbuffer, (S_iRight - S_iLeft) + 1, S_ulScreenBase
208 + (S_iBottom - 1) * (Columns << 1) + iColumn);
209}
210
211 static void
212myinsline(void)
213{
214 short iRow, iColumn;
215
216 iColumn = (S_iLeft - 1) << 1;
217
218 /* Copy the lines underneath */
219 for (iRow = S_iBottom - 1; iRow >= S_iTop; iRow--)
220 movedata(S_selVideo, (((iRow - 1) * Columns) << 1) + iColumn,
221 S_selVideo, ((iRow * Columns) << 1) + iColumn,
222 (S_iRight - S_iLeft + 1) << 1);
223
224 /* Clear the new row */
225 setblankbuffer(S_uiAttribute | ' ');
226
227 _dosmemputw(S_blankbuffer, (S_iRight - S_iLeft) + 1, S_ulScreenBase
228 + (S_iTop - 1) * (Columns << 1) + iColumn);
229}
230
231/*
232 * Scroll the screen one line up, clear the last line.
233 */
234 static void
235myscroll(void)
236{
237 short iRow, iColumn;
238
239 iColumn = (S_iLeft - 1) << 1;
240
241 /* Copy the screen */
242 for (iRow = S_iTop; iRow < S_iBottom; iRow++)
243 movedata(S_selVideo, ((iRow * Columns) << 1) + iColumn,
244 S_selVideo, (((iRow - 1) * Columns) << 1) + iColumn,
245 (S_iRight - S_iLeft + 1) << 1);
246
247 /* Clear the bottom row */
248 setblankbuffer(S_uiAttribute | ' ');
249
250 _dosmemputw(S_blankbuffer, (S_iRight - S_iLeft) + 1, S_ulScreenBase
251 + (S_iBottom - 1) * (Columns << 1) + iColumn);
252}
253
254 static int
255myputch(int iChar)
256{
257 unsigned short uiValue;
258
259 if (iChar == '\n')
260 {
261 myflush();
262 if (S_iCurrentRow >= S_iBottom - S_iTop)
263 myscroll();
264 else
265 {
266 S_iCurrentColumn = S_iLeft - 1;
267 S_iCurrentRow++;
268 }
269 }
270 else if (iChar == '\r')
271 {
272 myflush();
273 S_iCurrentColumn = S_iLeft - 1;
274 }
275 else if (iChar == '\b')
276 {
277 myflush();
278 if (S_iCurrentColumn >= S_iLeft)
279 S_iCurrentColumn--;
280 }
281 else if (iChar == 7)
282 {
283 sound(440); /* short beep */
284 delay(200);
285 nosound();
286 }
287 else
288 {
289 uiValue = S_uiAttribute | (unsigned char)iChar;
290
291 /*
292 * Normal char - are we starting to buffer?
293 */
294 if (S_linebufferpos == S_linebuffer)
295 {
296 S_iBufferColumn = S_iCurrentColumn;
297 S_iBufferRow = S_iCurrentRow;
298 }
299
300 *S_linebufferpos++ = uiValue;
301
302 S_iCurrentColumn++;
303 if (S_iCurrentColumn >= S_iRight && S_iCurrentRow >= S_iBottom - S_iTop)
304 {
305 myflush();
306 myscroll();
307 S_iCurrentColumn = S_iLeft - 1;
308 S_iCurrentRow++;
309 }
310 }
311
312 return 0;
313}
314
315 static void
316mytextinit(struct text_info *pTextinfo)
317{
318 S_selVideo = __dpmi_segment_to_descriptor(S_ulScreenBase >> 4);
319 S_uiAttribute = pTextinfo->normattr << 8;
320}
321
322 static void
323get_screenbase(void)
324{
325 static union REGS regs;
326
327 /* old Hercules grafic card has different base address (Macewicz) */
328 regs.h.ah = 0x0f;
329 (void)int86(0x10, &regs, &regs); /* int 10 0f */
330 if (regs.h.al == 0x07) /* video mode 7 -- hercules mono */
331 S_ulScreenBase = 0xb0000;
332 else
333 S_ulScreenBase = 0xb8000;
334}
335
336 static void
337mytextattr(int iAttribute)
338{
339 S_uiAttribute = (unsigned short)iAttribute << 8;
340}
341
342 static void
343mynormvideo(void)
344{
345 mytextattr(orig_attr);
346}
347
348 static void
349mytextcolor(int iTextColor)
350{
351 S_uiAttribute = (unsigned short)((S_uiAttribute & 0xf000)
352 | (unsigned short)iTextColor << 8);
353}
354
355 static void
356mytextbackground(int iBkgColor)
357{
358 S_uiAttribute = (unsigned short)((S_uiAttribute & 0x0f00)
359 | (unsigned short)(iBkgColor << 12));
360}
361/*
362 * Getdigits: Get a number from a string and skip over it.
363 * Note: the argument is a pointer to a char_u pointer!
364 */
365
366 static long
Bram Moolenaar779a7752016-01-30 23:26:34 +0100367mygetdigits(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368{
369 char_u *p;
370 long retval = 0;
371
372 p = *pp;
373 if (*p == '-') /* skip negative sign */
374 ++p;
375 while (VIM_ISDIGIT(*p))
376 {
377 retval = (retval * 10) + (*p - '0');
378 ++p;
379 }
380 if (**pp == '-') /* process negative sign */
381 retval = -retval;
382
383 *pp = p;
384 return retval;
385}
386#else
387# define mygotoxy gotoxy
388# define myputch putch
389# define myscroll scroll
390# define mynormvideo normvideo
391# define mytextattr textattr
392# define mytextcolor textcolor
393# define mytextbackground textbackground
394# define mygetdigits getdigits
395# define myclreol clreol
396# define myclrscr clrscr
397# define myinsline insline
398# define mydelline delline
399#endif
400
401static const struct
402{
403 char_u scancode;
404 char_u metakey;
405} altkey_table[] =
406{
407 {0x1e, 0xe1}, /* a */
408 {0x30, 0xe2}, /* b */
409 {0x2e, 0xe3}, /* c */
410 {0x20, 0xe4}, /* d */
411 {0x12, 0xe5}, /* e */
412 {0x21, 0xe6}, /* f */
413 {0x22, 0xe7}, /* g */
414 {0x23, 0xe8}, /* h */
415 {0x17, 0xe9}, /* i */
416 {0x24, 0xea}, /* j */
417 {0x25, 0xeb}, /* k */
418 {0x26, 0xec}, /* l */
419 {0x32, 0xed}, /* m */
420 {0x31, 0xee}, /* n */
421 {0x18, 0xef}, /* o */
422 {0x19, 0xf0}, /* p */
423 {0x10, 0xf1}, /* q */
424 {0x13, 0xf2}, /* r */
425 {0x1f, 0xf3}, /* s */
426 {0x14, 0xf4}, /* t */
427 {0x16, 0xf5}, /* u */
428 {0x2f, 0xf6}, /* v */
429 {0x11, 0xf7}, /* w */
430 {0x2d, 0xf8}, /* x */
431 {0x15, 0xf9}, /* y */
432 {0x2c, 0xfa}, /* z */
433 {0x78, 0xb1}, /* 1 */
434 {0x79, 0xb2}, /* 2 */
435 {0x7a, 0xb3}, /* 3 */
436 {0x7b, 0xb4}, /* 4 */
437 {0x7c, 0xb5}, /* 5 */
438 {0x7d, 0xb6}, /* 6 */
439 {0x7e, 0xb7}, /* 7 */
440 {0x7f, 0xb8}, /* 8 */
441 {0x80, 0xb9}, /* 9 */
442 {0x81, 0xb0}, /* 0 */
443};
444
445/*
446 * Translate extended keycodes into meta-chars where applicable
447 */
448 static int
449translate_altkeys(int rawkey)
450{
451 int i, c;
452
453 if ((rawkey & 0xff) == 0)
454 {
455 c = (rawkey >> 8);
456 for (i = sizeof(altkey_table) / sizeof(altkey_table[0]); --i >= 0; )
457 {
458 if (c == altkey_table[i].scancode)
459 return (int)altkey_table[i].metakey;
460 }
461 }
462 return rawkey;
463}
464
465/*
Bram Moolenaar035db9f2007-05-10 18:02:27 +0000466 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 */
468 void
Bram Moolenaar779a7752016-01-30 23:26:34 +0100469mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 char_u *p;
472 int n;
473
474 cterm_normal_fg_color = (orig_attr & 0xf) + 1;
475 cterm_normal_bg_color = ((orig_attr >> 4) & 0xf) + 1;
476 if (T_ME[0] == ESC && T_ME[1] == '|')
477 {
478 p = T_ME + 2;
479 n = getdigits(&p);
480 if (*p == 'm' && n > 0)
481 {
482 cterm_normal_fg_color = (n & 0xf) + 1;
483 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
484 }
485 }
486}
487
488#if defined(MCH_CURSOR_SHAPE) || defined(PROTO)
489/*
490 * Save/restore the shape of the cursor.
491 * call with FALSE to save, TRUE to restore
492 */
493 static void
494mch_restore_cursor_shape(int restore)
495{
496 static union REGS regs;
497 static int saved = FALSE;
498
499 if (restore)
500 {
501 if (saved)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000502 regs.h.ah = 0x01; /* Set Cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 else
504 return;
505 }
506 else
507 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000508 regs.h.ah = 0x03; /* Get Cursor */
509 regs.h.bh = 0x00; /* Page */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 saved = TRUE;
511 }
512
513 (void)int86(0x10, &regs, &regs);
514}
515
516/*
517 * Set the shape of the cursor.
518 * 'thickness' can be from 0 (thin) to 7 (block)
519 */
520 static void
521mch_set_cursor_shape(int thickness)
522{
523 union REGS regs;
524
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000525 regs.h.ch = 7 - thickness; /* Starting Line */
526 regs.h.cl = 7; /* Ending Line */
527 regs.h.ah = 0x01; /* Set Cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 (void)int86(0x10, &regs, &regs);
529}
530
531 void
532mch_update_cursor(void)
533{
534 int idx;
535 int thickness;
536
537 /*
538 * How the cursor is drawn depends on the current mode.
539 */
540 idx = get_shape_idx(FALSE);
541
542 if (shape_table[idx].shape == SHAPE_BLOCK)
543 thickness = 7;
544 else
545 thickness = (7 * shape_table[idx].percentage + 90) / 100;
546 mch_set_cursor_shape(thickness);
547}
548#endif
549
550/*
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200551 * Return amount of memory currently available in Kbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
553 long_u
554mch_avail_mem(int special)
555{
556#ifdef DJGPP
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200557 return _go32_dpmi_remaining_virtual_memory() >> 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#else
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200559 return coreleft() >> 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#endif
561}
562
563#ifdef FEAT_MOUSE
564
565/*
566 * Set area where mouse can be moved to: The whole screen.
567 * Rows and Columns must be valid when calling!
568 */
569 static void
570mouse_area(void)
571{
572 union REGS regs;
573
574 if (mouse_avail)
575 {
576 regs.x.cx = 0; /* mouse visible between cx and dx */
577 regs.x.dx = Columns * mouse_x_div - 1;
578 regs.x.ax = 7;
579 (void)int86(0x33, &regs, &regs);
580
581 regs.x.cx = 0; /* mouse visible between cx and dx */
582 regs.x.dx = Rows * mouse_y_div - 1;
583 regs.x.ax = 8;
584 (void)int86(0x33, &regs, &regs);
585 }
586}
587
588 static void
589show_mouse(int on)
590{
591 static int was_on = FALSE;
592 union REGS regs;
593
594 if (mouse_avail)
595 {
596 if (!mouse_active || mouse_hidden)
597 on = FALSE;
598 /*
599 * Careful: Each switch on must be compensated by exactly one switch
600 * off
601 */
602 if ((on && !was_on) || (!on && was_on))
603 {
604 was_on = on;
605 regs.x.ax = on ? 1 : 2;
606 int86(0x33, &regs, &regs); /* show mouse */
607 if (on)
608 mouse_area();
609 }
610 }
611}
612
613#endif
614
615/*
616 * Version of kbhit() and getch() that use direct console I/O.
617 * This avoids trouble with CTRL-P and the like, and should work over a telnet
618 * connection (it works for Xvi).
619 */
620
621static int cons_key = -1;
622
623/*
624 * Try to get one character directly from the console.
625 * If there is a key, it is stored in cons_key.
626 * Only call when cons_key is -1!
627 */
628 static void
629cons_getkey(void)
630{
631 union REGS regs;
632
633 /* call DOS function 6: Direct console I/O */
634 regs.h.ah = 0x06;
635 regs.h.dl = 0xff;
636 (void)intdos(&regs, &regs);
637 if ((regs.x.flags & 0x40) == 0) /* zero flag not set? */
638 cons_key = (regs.h.al & 0xff);
639}
640
641/*
642 * Return TRUE if a character is available.
643 */
644 static int
645cons_kbhit(void)
646{
647 if (cons_key < 0)
648 cons_getkey();
649 return (cons_key >= 0);
650}
651
652/*
653 * Return a character from the console.
654 * Should only be called when vim_kbhit() returns TRUE.
655 */
656 static int
657cons_getch(void)
658{
659 int c = -1;
660
661 if (cons_key < 0)
662 cons_getkey();
663 c = cons_key;
664 cons_key = -1;
665 return c;
666}
667
668
669#ifdef DJGPP
670/*
671 * DJGPP provides a kbhit() function that goes to the BIOS instead of DOS.
672 * This doesn't work for terminals connected to a serial port.
673 * Redefine kbhit() here to make it work.
674 */
675 static int
676vim_kbhit(void)
677{
678 union REGS regs;
679
680 regs.h.ah = 0x0b;
681 (void)intdos(&regs, &regs);
682 return regs.h.al;
683}
684
685#ifdef kbhit
686# undef kbhit /* might have been defined in conio.h */
687#endif
688#define kbhit() vim_kbhit()
689
690#endif
691
692/*
693 * Simulate WaitForChar() by slowly polling with bioskey(1) or kbhit().
694 *
695 * If Vim should work over the serial line after a 'ctty com1' we must use
696 * kbhit() and getch(). (jw)
697 * Usually kbhit() is not used, because then CTRL-C and CTRL-P
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200698 * will be caught by DOS (mool).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 *
700 * return TRUE if a character is available, FALSE otherwise
701 */
702
703#define FOREVER 1999999999L
704
705 static int
706WaitForChar(long msec)
707{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 long starttime = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
710 if (msec != 0)
711 starttime = biostime(0, 0L);
712
713 for (;;)
714 {
715#ifdef FEAT_MOUSE
716 long clicktime;
717 static int old_status = 0;
Bram Moolenaarc01140a2006-03-24 22:21:52 +0000718 union REGS regs;
719 int x, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
721 if (mouse_avail && mouse_active && mouse_click < 0)
722 {
723 regs.x.ax = 3;
724 int86(0x33, &regs, &regs); /* check mouse status */
725 /* only recognize button-down and button-up event */
726 x = regs.x.cx / mouse_x_div;
727 y = regs.x.dx / mouse_y_div;
728 if ((old_status == 0) != (regs.x.bx == 0))
729 {
730 if (old_status) /* button up */
731 mouse_click = MOUSE_RELEASE;
732 else /* button down */
733 {
734 /*
735 * Translate MSDOS mouse events to Vim mouse events.
736 * TODO: should handle middle mouse button, by pressing
737 * left and right at the same time.
738 */
739 if (regs.x.bx & MSDOS_MOUSE_LEFT)
740 mouse_click = MOUSE_LEFT;
741 else if (regs.x.bx & MSDOS_MOUSE_RIGHT)
742 mouse_click = MOUSE_RIGHT;
743 else if (regs.x.bx & MSDOS_MOUSE_MIDDLE)
744 mouse_click = MOUSE_MIDDLE;
745
746 /*
747 * Find out if this is a multi-click
748 */
749 clicktime = biostime(0, 0L);
750 if (mouse_click_x == x && mouse_click_y == y
751 && mouse_topline == curwin->w_topline
752#ifdef FEAT_DIFF
753 && mouse_topfill == curwin->w_topfill
754#endif
755 && mouse_click_count != 4
756 && mouse_click == mouse_last_click
757 && clicktime < mouse_click_time
758 + p_mouset / BIOSTICK)
759 ++mouse_click_count;
760 else
761 mouse_click_count = 1;
762 mouse_click_time = clicktime;
763 mouse_last_click = mouse_click;
764 mouse_click_x = x;
765 mouse_click_y = y;
766 mouse_topline = curwin->w_topline;
767#ifdef FEAT_DIFF
768 mouse_topfill = curwin->w_topfill;
769#endif
770 SET_NUM_MOUSE_CLICKS(mouse_click, mouse_click_count);
771 }
772 }
773 else if (old_status && (x != mouse_x || y != mouse_y))
774 mouse_click = MOUSE_DRAG;
775 old_status = regs.x.bx;
776 if (mouse_hidden && mouse_x >= 0 && (mouse_x != x || mouse_y != y))
777 {
778 mouse_hidden = FALSE;
779 show_mouse(TRUE);
780 }
781 mouse_x = x;
782 mouse_y = y;
783 }
784#endif
785
786 if ((p_consk ? cons_kbhit()
787 : p_biosk ? bioskey(bioskey_ready) : kbhit())
788 || cbrk_pressed
789#ifdef FEAT_MOUSE
790 || mouse_click >= 0
791#endif
792 )
793 return TRUE;
794 /*
795 * Use biostime() to wait until our time is done.
796 * We busy-wait here. Unfortunately, delay() and usleep() have been
797 * reported to give problems with the original Windows 95. This is
798 * fixed in service pack 1, but not everybody installed that.
799 * The DJGPP implementation of usleep() uses a busy-wait loop too.
800 */
801 if (msec == 0 || (msec != FOREVER
802 && biostime(0, 0L) > starttime + msec / BIOSTICK))
803 break;
804
805#ifdef DJGPP
806 /* Yield the CPU to the next process. */
807 __dpmi_yield();
808#endif
809 }
810 return FALSE;
811}
812
813/*
814 * don't do anything for about "msec" msec
815 */
816 void
817mch_delay(
818 long msec,
819 int ignoreinput)
820{
821 long starttime;
822
823 if (ignoreinput)
824 {
825 /*
826 * We busy-wait here. Unfortunately, delay() and usleep() have been
827 * reported to give problems with the original Windows 95. This is
828 * fixed in service pack 1, but not everybody installed that.
829 */
830 starttime = biostime(0, 0L);
831 while (biostime(0, 0L) < starttime + msec / BIOSTICK)
832 ;
833 }
834 else
835 WaitForChar(msec);
836}
837
838/*
839 * mch_write(): write the output buffer to the screen
840 */
841 void
842mch_write(
843 char_u *s,
844 int len)
845{
846 char_u *p;
847 int row, col;
848
849 if (term_console && full_screen)
850 while (len--)
851 {
852 /* translate ESC | sequences into bios calls */
853 if (p_wd) /* testing: wait a bit for each char */
854 WaitForChar(p_wd);
855
856 if (s[0] == '\n')
857#ifdef DJGPP
858 {
859 myflush();
860 S_iCurrentColumn = S_iLeft - 1;
861 }
862#else
863 myputch('\r');
864#endif
865 else if (s[0] == ESC && len > 1 && s[1] == '|')
866 {
867 switch (s[2])
868 {
869#ifdef DJGPP
870 case 'B': ScreenVisualBell();
871 goto got3;
872#endif
873 case 'J':
874#ifdef DJGPP
875 myflush();
876#endif
877 myclrscr();
878 goto got3;
879
880 case 'K':
881#ifdef DJGPP
882 myflush();
883#endif
884 myclreol();
885 goto got3;
886
887 case 'L':
888#ifdef DJGPP
889 myflush();
890#endif
891 myinsline();
892 goto got3;
893
894 case 'M':
895#ifdef DJGPP
896 myflush();
897#endif
898 mydelline();
899got3: s += 3;
900 len -= 2;
901 continue;
902
903 case '0':
904 case '1':
905 case '2':
906 case '3':
907 case '4':
908 case '5':
909 case '6':
910 case '7':
911 case '8':
912 case '9': p = s + 2;
913 row = mygetdigits(&p); /* no check for length! */
914 if (p > s + len)
915 break;
916 if (*p == ';')
917 {
918 ++p;
919 col = mygetdigits(&p); /* no check for length! */
920 if (p > s + len)
921 break;
922 if (*p == 'H' || *p == 'r' || *p == 'V')
923 {
924#ifdef DJGPP
925 myflush();
926#endif
927 if (*p == 'H') /* set cursor position */
928 mygotoxy(col, row);
929 else if (*p == 'V')
930 mywindow(row, S_iTop, col, S_iBottom);
931 else /* set scroll region */
932 mywindow(S_iLeft, row, S_iRight, col);
933 len -= p - s;
934 s = p + 1;
935 continue;
936 }
937 }
938 else if (*p == 'm' || *p == 'f' || *p == 'b')
939 {
940 if (*p == 'm') /* set color */
941 {
942 if (row == 0)
943 mynormvideo();/* reset color */
944 else
945 mytextattr(row);
946 }
947 else if (*p == 'f') /* set foreground color */
948 mytextcolor(row);
949 else /* set background color */
950 mytextbackground(row);
951
952 len -= p - s;
953 s = p + 1;
954 continue;
955 }
956 }
957 }
958 myputch(*s++);
959 }
960 else
961 {
962 write(1, s, (unsigned)len);
963 }
964}
965
966/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200967 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 * Get a characters from the keyboard.
969 * If time == 0 do not wait for characters.
970 * If time == n wait a short time for characters.
971 * If time == -1 wait forever for characters.
972 *
973 * return the number of characters obtained
974 */
975 int
976mch_inchar(
977 char_u *buf,
978 int maxlen,
979 long time,
980 int tb_change_cnt)
981{
982 int len = 0;
983 int c;
984 int tmp_c;
985 static int nextchar = 0; /* may keep character when maxlen == 1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 /*
988 * if we got a ctrl-C when we were busy, there will be a "^C" somewhere
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200989 * on the screen, so we need to redisplay it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 */
991 if (delayed_redraw)
992 {
993 delayed_redraw = FALSE;
994 update_screen(CLEAR);
995 setcursor();
996 out_flush();
997 }
998
999 /* return remaining character from last call */
1000 if (nextchar)
1001 {
1002 *buf = nextchar;
1003 nextchar = 0;
1004 return 1;
1005 }
1006
1007#ifdef FEAT_MOUSE
1008 if (time != 0)
1009 show_mouse(TRUE);
1010#endif
1011#ifdef DJGPP
1012 set_sys_cursor();
1013#endif
1014 if (time >= 0)
1015 {
1016 if (WaitForChar(time) == 0) /* no character available */
1017 {
1018#ifdef FEAT_MOUSE
1019 show_mouse(FALSE);
1020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 return 0;
1022 }
1023 }
1024 else /* time == -1 */
1025 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 /*
1027 * If there is no character available within 2 seconds (default)
Bram Moolenaar916b7af2005-03-16 09:52:38 +00001028 * write the autoscript file to disk. Or cause the CursorHold event
1029 * to be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00001031 if (WaitForChar(p_ut) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 {
1033#ifdef FEAT_AUTOCMD
Bram Moolenaare3226be2005-12-18 22:10:00 +00001034 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
Bram Moolenaar916b7af2005-03-16 09:52:38 +00001036 buf[0] = K_SPECIAL;
1037 buf[1] = KS_EXTRA;
1038 buf[2] = (int)KE_CURSORHOLD;
1039 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041#endif
Bram Moolenaar39a58ca2005-06-27 22:42:44 +00001042 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 }
1044 }
1045 WaitForChar(FOREVER); /* wait for key or mouse click */
1046
1047/*
1048 * Try to read as many characters as there are, until the buffer is full.
1049 */
1050 /*
1051 * we will get at least one key. Get more if they are available
1052 * After a ctrl-break we have to read a 0 (!) from the buffer.
1053 * bioskey(1) will return 0 if no key is available and when a
1054 * ctrl-break was typed. When ctrl-break is hit, this does not always
1055 * implies a key hit.
1056 */
1057 cbrk_pressed = FALSE;
1058#ifdef FEAT_MOUSE
1059 if (mouse_click >= 0 && maxlen >= 5)
1060 {
1061 len = 5;
1062 *buf++ = ESC + 128;
1063 *buf++ = 'M';
1064 *buf++ = mouse_click;
1065 *buf++ = mouse_x + '!';
1066 *buf++ = mouse_y + '!';
1067 mouse_click = -1;
1068 }
1069 else
1070#endif
1071 {
1072#ifdef FEAT_MOUSE
1073 mouse_hidden = TRUE;
1074#endif
1075 if (p_biosk && !p_consk)
1076 {
1077 while ((len == 0 || bioskey(bioskey_ready)) && len < maxlen)
1078 {
1079 c = translate_altkeys(bioskey(bioskey_read)); /* get the key */
1080 /*
1081 * translate a few things for inchar():
1082 * 0x0000 == CTRL-break -> 3 (CTRL-C)
1083 * 0x0300 == CTRL-@ -> NUL
1084 * 0xnn00 == extended key code -> K_NUL, nn
1085 * 0xnne0 == enhanced keyboard -> K_NUL, nn
1086 * K_NUL -> K_NUL, 3
1087 */
1088 if (c == 0)
1089 c = 3;
1090 else if (c == 0x0300)
1091 c = NUL;
1092 else if ((c & 0xff) == 0
1093 || c == K_NUL
1094 || c == 0x4e2b
1095 || c == 0x4a2d
1096 || c == 0x372a
1097 || ((c & 0xff) == 0xe0 && c != 0xe0))
1098 {
1099 if (c == K_NUL)
1100 c = 3;
1101 else
1102 c >>= 8;
1103 *buf++ = K_NUL;
1104 ++len;
1105 }
1106
1107 if (len < maxlen)
1108 {
1109 *buf++ = c;
1110 len++;
1111#ifdef FEAT_MBYTE
1112 /* Convert from 'termencoding' to 'encoding'. Only
1113 * translate normal characters, not key codes. */
1114 if (input_conv.vc_type != CONV_NONE
1115 && (len == 1 || buf[-2] != K_NUL))
1116 len += convert_input(buf - 1, 1, maxlen - len + 1) - 1;
1117#endif
1118 }
1119 else
1120 nextchar = c;
1121 }
1122 }
1123 else
1124 {
1125 while ((len == 0 || (p_consk ? cons_kbhit() : kbhit()))
1126 && len < maxlen)
1127 {
1128 switch (c = (p_consk ? cons_getch() : getch()))
1129 {
1130 case 0:
1131 /* NUL means that there is another character.
1132 * Get it immediately, because kbhit() doesn't always
1133 * return TRUE for the second character.
1134 */
1135 if (p_consk)
1136 c = cons_getch();
1137 else
1138 c = getch();
1139 tmp_c = translate_altkeys(c << 8);
1140 if (tmp_c == (c << 8))
1141 {
1142 *buf++ = K_NUL;
1143 ++len;
1144 }
1145 else
1146 c = tmp_c;
1147 break;
1148 case K_NUL:
1149 *buf++ = K_NUL;
1150 ++len;
1151 c = 3;
1152 break;
1153 case 3:
1154 cbrk_pressed = TRUE;
1155 /*FALLTHROUGH*/
1156 default:
1157 break;
1158 }
1159 if (len < maxlen)
1160 {
1161 *buf++ = c;
1162 ++len;
1163 }
1164 else
1165 nextchar = c;
1166 }
1167 }
1168 }
1169#ifdef FEAT_MOUSE
1170 show_mouse(FALSE);
1171#endif
1172
1173 beep_count = 0; /* may beep again now that we got some chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 return len;
1175}
1176
1177/*
1178 * return non-zero if a character is available
1179 */
1180 int
1181mch_char_avail(void)
1182{
1183 return WaitForChar(0L);
1184}
1185
1186#ifdef DJGPP
1187# define INT_ARG int
1188#else
1189# define INT_ARG
1190#endif
1191
1192/*
1193 * function for ctrl-break interrupt
1194 */
1195 static void interrupt
1196#ifdef DJGPP
1197catch_cbrk(int a)
1198#else
1199catch_cbrk(void)
1200#endif
1201{
1202 cbrk_pressed = TRUE;
1203 ctrlc_pressed = TRUE;
1204}
1205
1206#ifndef DJGPP
1207/*
1208 * ctrl-break handler for DOS. Never called when a ctrl-break is typed, because
1209 * we catch interrupt 1b. If you type ctrl-C while Vim is waiting for a
1210 * character this function is not called. When a ctrl-C is typed while Vim is
1211 * busy this function may be called. By that time a ^C has been displayed on
1212 * the screen, so we have to redisplay the screen. We can't do that here,
1213 * because we may be called by DOS. The redraw is in mch_inchar().
1214 */
1215 static int _cdecl
1216cbrk_handler(void)
1217{
1218 delayed_redraw = TRUE;
1219 return 1; /* resume operation after ctrl-break */
1220}
1221
1222/*
1223 * function for critical error interrupt
1224 * For DOS 1 and 2 return 0 (Ignore).
1225 * For DOS 3 and later return 3 (Fail)
1226 */
1227 static void interrupt
Bram Moolenaar779a7752016-01-30 23:26:34 +01001228catch_cint(
1229 unsigned bp,
1230 unsigned di,
1231 unsigned si,
1232 unsigned ds,
1233 unsigned es,
1234 unsigned dx,
1235 unsigned cx,
1236 unsigned bx,
1237 unsigned ax)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238{
1239 ax = (ax & 0xff00); /* set AL to 0 */
1240 if (_osmajor >= 3)
1241 ax |= 3; /* set AL to 3 */
1242}
1243#endif
1244
1245/*
1246 * Set the interrupt vectors for use with Vim on or off.
1247 * on == TRUE means as used within Vim
1248 */
1249 static void
1250set_interrupts(int on)
1251{
1252 static int saved_cbrk;
1253#ifndef DJGPP
1254 static void interrupt (*old_cint)();
1255#endif
1256 static void interrupt (*old_cbrk)(INT_ARG);
1257
1258 if (on)
1259 {
1260 saved_cbrk = getcbrk(); /* save old ctrl-break setting */
1261 setcbrk(0); /* do not check for ctrl-break */
1262#ifdef DJGPP
1263 old_cbrk = signal(SIGINT, catch_cbrk); /* critical error interrupt */
1264#else
1265 old_cint = getvect(0x24); /* save old critical error interrupt */
1266 setvect(0x24, catch_cint); /* install our critical error interrupt */
1267 old_cbrk = getvect(0x1B); /* save old ctrl-break interrupt */
1268 setvect(0x1B, catch_cbrk); /* install our ctrl-break interrupt */
1269 ctrlbrk(cbrk_handler); /* vim's ctrl-break handler */
1270#endif
1271 if (term_console)
1272 out_str(T_ME); /* set colors */
1273 }
1274 else
1275 {
1276 setcbrk(saved_cbrk); /* restore ctrl-break setting */
1277#ifdef DJGPP
1278 signal(SIGINT,old_cbrk); /* critical error interrupt */
1279#else
1280 setvect(0x24, old_cint); /* restore critical error interrupt */
1281 setvect(0x1B, old_cbrk); /* restore ctrl-break interrupt */
1282#endif
1283 /* restore ctrl-break handler, how ??? */
1284 if (term_console)
1285 mynormvideo(); /* restore screen colors */
1286 }
1287}
1288
1289/*
1290 * We have no job control, fake it by starting a new shell.
1291 */
1292 void
1293mch_suspend(void)
1294{
1295 suspend_shell();
1296}
1297
1298extern int _fmode;
1299
1300/*
1301 * Prepare window for use by Vim.
1302 */
1303 void
1304mch_init(void)
1305{
1306 union REGS regs;
1307
1308#if defined(DJGPP) && defined(FEAT_CLIPBOARD)
1309 __dpmi_regs dpmi_regs;
1310#endif
1311
1312 /*
1313 * Get the video attributes at the cursor. These will be used as the
1314 * default attributes.
1315 */
1316 regs.h.ah = 0x08;
1317 regs.h.bh = 0x00; /* video page 0 */
1318 int86(0x10, &regs, &regs);
1319 orig_attr = regs.h.ah;
1320 mynormvideo();
1321 if (cterm_normal_fg_color == 0)
1322 cterm_normal_fg_color = (orig_attr & 0xf) + 1;
1323 if (cterm_normal_bg_color == 0)
1324 cterm_normal_bg_color = ((orig_attr >> 4) & 0xf) + 1;
1325
1326 term_console = TRUE; /* assume using the console for the things here */
1327 _fmode = O_BINARY; /* we do our own CR-LF translation */
1328 out_flush();
1329 set_interrupts(TRUE); /* catch interrupts */
1330
1331#ifdef DJGPP
1332 /*
1333 * Use Long File Names by default, if $LFN not set.
1334 */
1335 if (getenv("LFN") == NULL)
1336 putenv("LFN=y");
1337
1338 get_screenbase();
1339#endif
1340
1341#ifdef FEAT_MOUSE
1342/* find out if a MS compatible mouse is available */
1343 regs.x.ax = 0;
1344 (void)int86(0x33, &regs, &regs);
1345 mouse_avail = regs.x.ax;
1346 /* best guess for mouse coordinate computations */
1347 mch_get_shellsize();
1348 if (Columns <= 40)
1349 mouse_x_div = 16;
1350 if (Rows == 30)
1351 mouse_y_div = 16;
1352#endif
1353
1354 /*
1355 * Try switching to 16 colors for background, instead of 8 colors and
1356 * blinking. Does this always work? Can the old value be restored?
1357 */
1358 regs.x.ax = 0x1003;
1359 regs.h.bl = 0x00;
1360 regs.h.bh = 0x00;
1361 int86(0x10, &regs, &regs);
1362
1363 /*
1364 * Test if we have an enhanced AT keyboard. Write 0xFFFF to the keyboard
1365 * buffer and try to read it back. If we can't in 16 tries, it's an old
1366 * type XT keyboard.
1367 */
1368 regs.h.ah = 0x05;
1369 regs.x.cx = 0xffff;
1370 int86(0x16, &regs, &regs);
1371 if (regs.h.al != 1) /* skip this when keyboard buffer is full */
1372 {
1373 int i;
1374
1375 for (i = 0; i < 16; ++i)
1376 {
1377 regs.h.ah = 0x10;
1378 int86(0x16, &regs, &regs);
1379 if (regs.x.ax == 0xffff)
1380 break;
1381 }
1382 if (i == 16) /* 0xffff not read, must be old keyboard */
1383 {
1384 bioskey_read = 0;
1385 bioskey_ready = 1;
1386 }
1387 }
1388
1389#ifdef MCH_CURSOR_SHAPE
1390 /* Save the old cursor shape */
1391 mch_restore_cursor_shape(FALSE);
1392 /* Initialise the cursor shape */
1393 mch_update_cursor();
1394#endif
1395
1396#if defined(DJGPP) && defined(FEAT_CLIPBOARD)
1397 /*
1398 * Check to see if the Windows clipboard is available, ie. are we
1399 * running from a DOS session within Windows. Obviously, the Windows
1400 * clipboard will not be available if we're running under pure DOS.
1401 *
1402 * int 0x2f, AX = 0x1700 identifies the Windows version we're running
1403 * under. Upon return from the interrupt, if AX is unchanged, we're
1404 * running under pure DOS and no Windows clipboard is available.
1405 *
1406 * Remark: could use int86() here but __dpmi_int() is recommended in
1407 * the DJGPP docs, since int86() doesn't cover all available interrupts.
1408 */
1409 dpmi_regs.x.ax = 0x1700;
1410 if (__dpmi_int(0x2f, &dpmi_regs) == -1)
1411 /* real-mode interrupt failed? */
1412 dpmi_regs.x.ax = 0x1700; /* force failure */
1413
1414 if (dpmi_regs.x.ax == 0x1700) /* no change in AX? */
1415 clip_init(FALSE); /* no clipboard available, too bad */
1416 else /* else, running under Windows, OK */
1417 clip_init(TRUE); /* clipboard is available */
1418#endif
1419}
1420
1421 int
1422mch_check_win(
1423 int argc,
1424 char **argv)
1425{
1426 /* store argv[0], may be used for $VIM */
1427 if (*argv[0] != NUL)
1428 exe_name = FullName_save((char_u *)argv[0], FALSE);
1429
1430 /*
1431 * Try the DOS search path. The executable may in
1432 * fact be called differently, so try this last.
1433 */
1434 if (exe_name == NULL || *exe_name == NUL)
1435 exe_name = searchpath("vim.exe");
1436
1437 if (isatty(1))
1438 return OK;
1439 return FAIL;
1440}
1441
1442/*
1443 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1444 */
1445 int
1446mch_input_isatty(void)
1447{
1448 if (isatty(read_cmd_fd))
1449 return TRUE;
1450 return FALSE;
1451}
1452
1453#if defined(USE_FNAME_CASE) || defined(PROTO)
1454/*
1455 * fname_case(): Set the case of the file name, if it already exists.
1456 * TODO: should expand short to long file names. Need to use DOS interrupts,
1457 * see DJGPP sources libc/dos/dir/findfirs.c.
1458 */
1459 void
1460fname_case(char_u *name, int len)
1461{
1462 char_u *tail;
1463 struct ffblk fb;
1464
1465 slash_adjust(name);
1466 if (findfirst(name, &fb, 0) == 0)
1467 {
1468 tail = gettail(name);
1469 if (len == 0 ? STRLEN(tail) == STRLEN(fb.ff_name)
1470 : (tail - name) + STRLEN(fb.ff_name) < len)
1471 STRCPY(tail, fb.ff_name);
1472 }
1473}
1474#endif
1475
1476/*
1477 * return process ID
1478 */
1479 long
1480mch_get_pid(void)
1481{
1482 return (long)0;
1483}
1484
1485/*
1486 * Change default drive (just like _chdrive of Borland C 3.1)
1487 */
1488 static int
1489change_drive(int drive)
1490{
1491 union REGS regs;
1492
1493 regs.h.ah = 0x0e;
1494 regs.h.dl = drive - 1;
1495 intdos(&regs, &regs); /* set default drive */
1496 regs.h.ah = 0x19;
1497 intdos(&regs, &regs); /* get default drive */
1498 if (regs.h.al == drive - 1)
1499 return 0;
1500 return -1;
1501}
1502
1503/*
1504 * Get absolute file name into buffer 'buf' of length 'len' bytes.
1505 * All slashes are replaced with backslashes, to avoid trouble when comparing
1506 * file names. When 'shellslash' set do it the other way around.
1507 *
1508 * return FAIL for failure, OK otherwise
1509 */
1510 int
1511mch_FullName(
1512 char_u *fname,
1513 char_u *buf,
1514 int len,
1515 int force)
1516{
1517 if (!force && mch_isFullName(fname)) /* already expanded */
1518 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00001519 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 slash_adjust(buf);
1521 return OK;
1522 }
1523
1524#ifdef __BORLANDC__ /* Only Borland C++ has this */
1525 if (_fullpath((char *)buf, (char *)fname, len - 1) == NULL)
1526 return FAIL;
1527 return OK;
1528#else /* almost the same as mch_FullName() in os_unix.c */
1529 {
1530# if 1
1531 char_u fullpath[MAXPATHL];
1532
1533 if (!_truename(fname, fullpath))
1534 return FAIL;
1535 slash_adjust(fullpath); /* Only needed when 'shellslash' set */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00001536 vim_strncpy(buf, fullpath, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 return OK;
1538
1539# else /* Old code, to be deleted... */
1540 int l;
1541 char_u olddir[MAXPATHL];
1542 char_u *p, *q;
1543 int c;
1544 int retval = OK;
1545
1546 *buf = 0;
1547 /*
1548 * change to the directory for a moment,
1549 * and then do the getwd() (and get back to where we were).
1550 * This will get the correct path name with "../" things.
1551 */
1552 p = vim_strrchr(fname, '/');
1553 q = vim_strrchr(fname, '\\');
1554 if (q != NULL && (p == NULL || q > p))
1555 p = q;
1556 q = vim_strrchr(fname, ':');
1557 if (q != NULL && (p == NULL || q > p))
1558 p = q;
1559 if (p != NULL)
1560 {
1561 if (getcwd(olddir, MAXPATHL) == NULL)
1562 {
1563 p = NULL; /* can't get current dir: don't chdir */
1564 retval = FAIL;
1565 }
1566 else
1567 {
1568 if (p == fname) /* /fname */
1569 q = p + 1; /* -> / */
1570 else if (q + 1 == p) /* ... c:\foo */
1571 q = p + 1; /* -> c:\ */
1572 else /* but c:\foo\bar */
1573 q = p; /* -> c:\foo */
1574
1575 c = *q; /* truncate at start of fname */
1576 *q = NUL;
1577# ifdef DJGPP
1578 STRCPY(buf, fname);
1579 slash_adjust(buf); /* needed when fname starts with \ */
1580 if (mch_chdir(buf)) /* change to the directory */
1581# else
1582 if (mch_chdir(fname)) /* change to the directory */
1583# endif
1584 retval = FAIL;
1585 else
1586 {
1587 fname = q;
1588 if (c == psepc) /* if we cut the name at a */
1589 fname++; /* '\', don't add it again */
1590 }
1591 *q = c;
1592 }
1593 }
1594 if (getcwd(buf, len) == NULL)
1595 {
1596 retval = FAIL;
1597 *buf = NUL;
1598 }
1599# ifdef USE_FNAME_CASE
1600 else
1601 {
1602 char_u *head;
1603 char_u *tail;
1604 struct ffblk fb;
1605 int c;
1606 int added;
1607
1608 /* Apparently "longna~1" isn't expanded by getcwd(), at least not
1609 * for DJGPP. Expand it here. Have to do each dirname
1610 * separately. */
1611 slash_adjust(buf);
1612 head = buf;
1613 if (isalpha(*head) && head[1] == ':')
1614 head += 2; /* skip "c:" */
1615 while (*head != NUL)
1616 {
1617 /* Advance "head" to the start of a dirname and "tail" to just
1618 * after it. */
1619 while (*head == '/' || *head == '\\')
1620 ++head;
1621 for (tail = head; *tail != NUL; ++tail)
1622 if (*tail == '/' || *tail == '\\')
1623 break;
1624 c = *tail;
1625 *tail = NUL;
1626
1627 if (findfirst(buf, &fb, FA_DIREC) == 0)
1628 {
1629 added = STRLEN(fb.ff_name);
1630 if ((head - buf) + added + STRLEN(tail + 1) + 2 < len)
1631 {
1632 added -= (tail - head);
1633 if (added != 0)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001634 STRMOVE(tail + 1 + added, tail + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 STRCPY(head, fb.ff_name);
1636 tail += added;
1637 }
1638 }
1639 *tail = c;
1640 head = tail;
1641 }
1642 }
1643# endif
1644 if (p != NULL)
1645 mch_chdir(olddir);
1646 /*
1647 * Concatenate the file name to the path.
1648 */
1649 if (*fname != NUL)
1650 {
1651 l = STRLEN(buf);
1652 if (l > 0 && buf[l - 1] != '/' && buf[l - 1] != '\\')
1653 strcat(buf, pseps);
1654 strcat(buf, fname);
1655 }
1656 return retval;
1657# endif
1658 }
1659#endif
1660}
1661
1662/*
1663 * Replace all slashes by backslashes.
1664 * This used to be the other way around, but MS-DOS sometimes has problems
1665 * with slashes (e.g. in a command name). We can't have mixed slashes and
1666 * backslashes, because comparing file names will not work correctly. The
1667 * commands that use a file name should try to avoid the need to type a
1668 * backslash twice.
1669 * When 'shellslash' set do it the other way around.
1670 */
1671 void
1672slash_adjust(char_u *p)
1673{
1674#ifdef OLD_DJGPP /* this seems to have been fixed in DJGPP 2.01 */
1675 /* DJGPP can't handle a file name that starts with a backslash, and when it
1676 * starts with a slash there should be no backslashes */
1677 if (*p == '\\' || *p == '/')
1678 while (*p)
1679 {
1680 if (*p == '\\')
1681 *p = '/';
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001682 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684 else
1685#endif
1686 while (*p)
1687 {
1688 if (*p == psepcN)
1689 *p = psepc;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001690 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 }
1692}
1693
1694/*
1695 * Return TRUE if "fname" does not depend on the current directory.
1696 */
1697 int
1698mch_isFullName(char_u *fname)
1699{
1700 /* A name like "d:/foo" and "//server/share" is absolute */
1701 return (fname[0] != NUL && fname[1] == ':'
1702 && (fname[2] == '/' || fname[2] == '\\'))
1703 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\'));
1704}
1705
1706
1707 void
1708mch_early_init(void)
1709{
1710}
1711
1712/*
1713 * Careful: mch_exit() may be called before mch_init()!
1714 */
1715 void
1716mch_exit(int r)
1717{
1718 settmode(TMODE_COOK);
1719 stoptermcap();
1720 set_interrupts(FALSE); /* restore interrupts */
1721#ifdef DJGPP
1722 set_sys_cursor();
1723#endif
1724 /* Somehow outputting CR-NL causes the original colors to be restored */
1725 out_char('\r');
1726 out_char('\n');
1727 out_flush();
1728 ml_close_all(TRUE); /* remove all memfiles */
1729#ifdef MCH_CURSOR_SHAPE
1730 mch_restore_cursor_shape(TRUE);
1731#endif
1732 exit(r);
1733}
1734
1735/*
1736 * set the tty in (raw) ? "raw" : "cooked" mode
1737 * Does not change the tty, as bioskey() and kbhit() work raw all the time.
1738 */
1739 void
1740mch_settmode(int tmode)
1741{
1742}
1743
1744#ifdef FEAT_MOUSE
1745 void
1746mch_setmouse(int on)
1747{
1748 mouse_active = on;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001749 mouse_hidden = TRUE; /* don't show it until moved */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750}
1751#endif
1752
1753/*
1754 * set screen mode
1755 * return FAIL for failure, OK otherwise
1756 */
1757 int
1758mch_screenmode(char_u *arg)
1759{
1760 int mode;
1761 int i;
1762 static char *(names[]) = {"BW40", "C40", "BW80", "C80", "MONO", "C4350"};
1763 static int modes[] = { BW40, C40, BW80, C80, MONO, C4350};
1764
1765 mode = -1;
1766 if (VIM_ISDIGIT(*arg)) /* mode number given */
1767 mode = atoi((char *)arg);
1768 else
1769 {
1770 for (i = 0; i < sizeof(names) / sizeof(char_u *); ++i)
1771 if (stricmp(names[i], (char *)arg) == 0)
1772 {
1773 mode = modes[i];
1774 break;
1775 }
1776 }
1777 if (mode == -1)
1778 {
1779 EMSG("E362: Unsupported screen mode");
1780 return FAIL;
1781 }
1782 textmode(mode); /* use Borland function */
1783#ifdef DJGPP
1784 /* base address may have changed */
1785 get_screenbase();
1786#endif
1787
1788 /* Screen colors may have changed. */
1789 out_str(T_ME);
1790
1791#ifdef FEAT_MOUSE
1792 if (mode <= 1 || mode == 4 || mode == 5 || mode == 13 || mode == 0x13)
1793 mouse_x_div = 16;
1794 else
1795 mouse_x_div = 8;
1796 if (mode == 0x11 || mode == 0x12)
1797 mouse_y_div = 16;
1798 else if (mode == 0x10)
1799 mouse_y_div = 14;
1800 else
1801 mouse_y_div = 8;
1802 shell_resized();
1803#endif
1804 return OK;
1805}
1806
1807/*
1808 * Structure used by Turbo-C/Borland-C to store video parameters.
1809 */
1810#ifndef DJGPP
1811extern struct text_info _video;
1812#endif
1813
1814/*
1815 * try to get the real window size
1816 * return FAIL for failure, OK otherwise
1817 */
1818 int
1819mch_get_shellsize(void)
1820{
1821 struct text_info textinfo;
1822
1823 /*
1824 * The screenwidth is returned by the BIOS OK.
1825 * The screenheight is in a location in the bios RAM, if the display is
1826 * EGA or VGA.
1827 */
1828 if (!term_console)
1829 return FAIL;
1830 gettextinfo(&textinfo);
1831 Columns = textinfo.screenwidth;
1832 Rows = textinfo.screenheight;
1833#ifndef DJGPP
1834 if (textinfo.currmode > 10)
1835 Rows = *(char far *)MK_FP(0x40, 0x84) + 1;
1836#endif
1837
1838 if (Columns < MIN_COLUMNS || Rows < MIN_LINES)
1839 {
1840 /* these values are overwritten by termcap size or default */
1841 Columns = 80;
1842 Rows = 25;
1843 return FAIL;
1844 }
1845#ifdef DJGPP
1846 mytextinit(&textinfo); /* Added by JML, 1/15/98 */
1847#endif
1848
1849 return OK;
1850}
1851
1852/*
1853 * Set the active window for delline/insline.
1854 */
1855 static void
1856set_window(void)
1857{
1858 if (term_console)
1859 {
1860#ifndef DJGPP
1861 _video.screenheight = Rows;
1862#endif
1863 mywindow(1, 1, Columns, Rows);
1864 }
1865 screen_start();
1866}
1867
1868 void
1869mch_set_shellsize(void)
1870{
1871 /* Should try to set the window size to Rows and Columns.
1872 * May involve switching display mode....
1873 * We assume the user knows the size and just use it. */
1874}
1875
1876/*
1877 * Rows and/or Columns has changed.
1878 */
1879 void
Bram Moolenaar779a7752016-01-30 23:26:34 +01001880mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
1882#ifdef FEAT_MOUSE
1883 /* best guess for mouse coordinate computations */
1884 if (Columns <= 40)
1885 mouse_x_div = 16;
1886 if (Rows == 30)
1887 mouse_y_div = 16;
1888#endif
1889 set_window();
1890#ifdef FEAT_MOUSE
1891 mouse_area(); /* set area where mouse can go */
1892#endif
1893}
1894
1895#if defined(DJGPP) || defined(PROTO)
1896/*
1897 * Check the number of Columns with a BIOS call. This avoids a crash of the
1898 * DOS console when 'columns' is set to a too large value.
1899 */
1900 void
Bram Moolenaar779a7752016-01-30 23:26:34 +01001901mch_check_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902{
1903 static union REGS regs;
1904
1905 regs.h.ah = 0x0f;
1906 (void)int86(0x10, &regs, &regs);
1907 if ((unsigned)Columns > (unsigned)regs.h.ah)
1908 Columns = (unsigned)regs.h.ah;
1909}
1910#endif
1911
1912/*
1913 * call shell, return FAIL for failure, OK otherwise
1914 * options: SHELL_*, see vim.h.
1915 */
1916 int
1917mch_call_shell(
1918 char_u *cmd,
1919 int options)
1920{
1921 int x;
1922 int tmode = cur_tmode;
1923#ifndef DJGPP
1924 char_u *newcmd;
1925#endif
1926
1927 out_flush();
1928#ifdef DJGPP
1929 set_sys_cursor();
1930#endif
1931
1932 if (options & SHELL_COOKED)
1933 settmode(TMODE_COOK); /* set to normal mode */
1934 set_interrupts(FALSE); /* restore interrupts */
1935
1936#ifdef DJGPP
1937 /* ignore signals while external command is running */
1938 signal(SIGINT, SIG_IGN);
1939 signal(SIGHUP, SIG_IGN);
1940 signal(SIGQUIT, SIG_IGN);
1941 signal(SIGTERM, SIG_IGN);
1942#endif
1943 if (cmd == NULL)
1944 x = system((char *)p_sh);
1945 else
1946 {
1947#ifdef DJGPP
1948 /*
1949 * Use 'shell' for system().
1950 */
1951 setenv("SHELL", (char *)p_sh, 1);
1952 x = system(cmd);
1953#else
1954 /* we use "command" to start the shell, slow but easy */
1955 newcmd = alloc(STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 3);
1956 if (newcmd == NULL)
1957 x = -1;
1958 else
1959 {
1960 sprintf((char *)newcmd, "%s %s %s", p_sh, p_shcf, cmd);
1961 x = system((char *)newcmd);
1962 vim_free(newcmd);
1963 }
1964#endif
1965 }
1966#ifdef DJGPP
1967 signal(SIGINT, SIG_DFL);
1968 signal(SIGHUP, SIG_DFL);
1969 signal(SIGQUIT, SIG_DFL);
1970 signal(SIGTERM, SIG_DFL);
1971#endif
1972 if (tmode == TMODE_RAW)
1973 settmode(TMODE_RAW); /* set to raw mode */
1974 set_interrupts(TRUE); /* catch interrupts */
1975
1976 if (x && !(options & SHELL_SILENT) && !emsg_silent)
1977 {
1978 MSG_PUTS("\nshell returned ");
1979 msg_outnum((long)x);
1980 msg_putchar('\n');
1981 }
1982
1983 return x;
1984}
1985
1986/*
1987 * check for an "interrupt signal": CTRL-break or CTRL-C
1988 */
1989 void
1990mch_breakcheck(void)
1991{
1992 if (ctrlc_pressed)
1993 {
1994 ctrlc_pressed = FALSE;
1995 got_int = TRUE;
1996 }
1997}
1998
1999/*
2000 * Return TRUE if "p" contain a wildcard that can be expanded by
2001 * dos_expandpath().
2002 */
2003 int
2004mch_has_exp_wildcard(char_u *p)
2005{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002006 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 if (vim_strchr((char_u *)"?*[", *p) != NULL
2009 || (*p == '~' && p[1] != NUL))
2010 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 return FALSE;
2013}
2014
2015/*
2016 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
2017 * shortened file name).
2018 */
2019 int
2020mch_has_wildcard(char_u *p)
2021{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002022 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
2024 if (vim_strchr((char_u *)
2025# ifdef VIM_BACKTICK
2026 "?*$[`"
2027# else
2028 "?*$["
2029# endif
2030 , *p) != NULL
2031 || (*p == '~' && p[1] != NUL))
2032 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 }
2034 return FALSE;
2035}
2036
2037/*
2038 * Change directory to "path".
2039 * The normal chdir() does not change the default drive. This one does.
2040 * Return 0 for success, -1 for failure.
2041 */
2042 int
2043mch_chdir(char *path)
2044{
2045 if (path[0] == NUL) /* just checking... */
2046 return 0;
Bram Moolenaara2974d72009-07-14 16:38:36 +00002047 if (p_verbose >= 5)
2048 {
2049 verbose_enter();
2050 smsg((char_u *)"chdir(%s)", path);
2051 verbose_leave();
2052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 if (path[1] == ':') /* has a drive name */
2054 {
2055 if (change_drive(TOLOWER_ASC(path[0]) - 'a' + 1))
2056 return -1; /* invalid drive name */
2057 path += 2;
2058 }
2059 if (*path == NUL) /* drive name only */
2060 return 0;
2061 return chdir(path); /* let the normal chdir() do the rest */
2062}
2063
2064#ifdef DJGPP
2065/*
2066 * mch_rename() works around a bug in rename (aka MoveFile) in
2067 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
2068 * file whose short file name is "FOO.BAR" (its long file name will
2069 * be correct: "foo.bar~"). Because a file can be accessed by
2070 * either its SFN or its LFN, "foo.bar" has effectively been
2071 * renamed to "foo.bar", which is not at all what was wanted. This
2072 * seems to happen only when renaming files with three-character
2073 * extensions by appending a suffix that does not include ".".
2074 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
2075 * This works like mch_rename in os_win32.c, but is a bit simpler.
2076 *
2077 * Like rename(), returns 0 upon success, non-zero upon failure.
2078 * Should probably set errno appropriately when errors occur.
2079 */
2080
2081 int
2082mch_rename(const char *OldFile, const char *NewFile)
2083{
2084 char_u *TempFile;
2085 int retval;
2086 int fd;
2087
2088 /* rename() works correctly without long file names, so use that */
2089 if (!_USE_LFN)
2090 return rename(OldFile, NewFile);
2091
2092 if ((TempFile = alloc((unsigned)(STRLEN(OldFile) + 13))) == NULL)
2093 return -1;
2094
2095 STRCPY(TempFile, OldFile);
2096 STRCPY(gettail(TempFile), "axlqwqhy.ba~");
2097 if (rename(OldFile, TempFile))
2098 retval = -1;
2099 else
2100 {
2101 /* now create an empty file called OldFile; this prevents
2102 * the operating system using OldFile as an alias (SFN)
2103 * if we're renaming within the same directory. For example,
2104 * we're editing a file called filename.asc.txt by its SFN,
2105 * filena~1.txt. If we rename filena~1.txt to filena~1.txt~
2106 * (i.e., we're making a backup while writing it), the SFN
2107 * for filena~1.txt~ will be filena~1.txt, by default, which
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002108 * will cause all sorts of problems later in buf_write(). So, we
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 * create an empty file called filena~1.txt and the system will have
2110 * to find some other SFN for filena~1.txt~, such as filena~2.txt
2111 */
2112 if ((fd = open(OldFile, O_RDWR|O_CREAT|O_EXCL, 0444)) < 0)
2113 return -1;
2114 retval = rename(TempFile, NewFile);
2115 close(fd);
2116 mch_remove((char_u *)OldFile);
2117
2118 /* If renaming to NewFile failed, rename TempFile back to OldFile, so
2119 * that it looks like nothing happened. */
2120 if (retval)
2121 rename(TempFile, OldFile);
2122 }
2123 vim_free(TempFile);
2124
2125 return retval; /* success */
2126}
2127#endif
2128
2129#if defined(DJGPP) || defined(PROTO)
2130/*
2131 * setlocale() for DJGPP with MS-DOS codepage support
2132 * Author: Cyril Slobin <slobin@fe.msk.ru>
2133 *
2134 * Scaled down a lot for use by Vim: Only support setlocale(LC_ALL, "").
2135 */
2136
2137#undef setlocale
2138
Bram Moolenaar82881492012-11-20 16:53:39 +01002139#ifndef PROTO
2140# include <go32.h>
2141# include <inlines/ctype.ha>
2142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143#include <locale.h>
2144
2145#define UPCASE (__dj_ISALNUM | __dj_ISALPHA | __dj_ISGRAPH | __dj_ISPRINT | __dj_ISUPPER)
2146#define LOCASE (__dj_ISALNUM | __dj_ISALPHA | __dj_ISGRAPH | __dj_ISPRINT | __dj_ISLOWER)
2147
2148 char *
2149djgpp_setlocale(void)
2150{
2151 __dpmi_regs regs;
2152 struct { char id; unsigned short off, seg; } __attribute__ ((packed)) info;
2153 unsigned char buffer[0x82], lower, upper;
2154 int i;
2155
2156 regs.x.ax = 0x6502;
2157 regs.x.bx = 0xffff;
2158 regs.x.dx = 0xffff;
2159 regs.x.cx = 5;
2160 regs.x.es = __tb >> 4;
2161 regs.x.di = __tb & 0xf;
2162
2163 __dpmi_int(0x21, &regs);
2164
2165 if (regs.x.flags & 1)
2166 return NULL;
2167
2168 dosmemget(__tb, 5, &info);
2169 dosmemget((info.seg << 4) + info.off, 0x82, buffer);
2170
2171 if (*(short *)buffer != 0x80)
2172 return NULL;
2173
2174 /* Fix problem of underscores being replaced with y-umlaut. (Levin) */
2175 if (buffer[26] == 0x5f)
2176 buffer[26] = 0x98;
2177
2178 for (i = 0; i < 0x80; i++)
2179 {
2180 lower = i + 0x80;
2181 upper = (buffer+2)[i];
2182 if (lower != upper)
2183 {
2184 __dj_ctype_flags[lower+1] = LOCASE;
2185 __dj_ctype_toupper[lower+1] = upper;
2186 if (__dj_ctype_flags[upper+1] == 0)
2187 __dj_ctype_flags[upper+1] = UPCASE;
2188 if (__dj_ctype_tolower[upper+1] == upper)
2189 __dj_ctype_tolower[upper+1] = lower;
2190 }
2191 }
2192
2193 return "C";
2194}
2195
2196#if defined(FEAT_CLIPBOARD) || defined(PROTO)
2197
2198/*
2199 * Clipboard stuff, for cutting and pasting text to other windows.
2200 *
2201 * Implementation of DOS/Windows clipboard data transfer
2202 * by David Kotchan (dkotchan@sympatico.ca)
2203 */
2204
2205#define CF_TEXT 0x01 /* Windows clipboard format: Windows (ANSI) text */
2206#define CF_OEMTEXT 0x07 /* Windows clipboard format: OEM (DOS) text */
2207#define CF_VIMCLIP 0x04 /* trick: SYLK clipboard format for VimClipboard */
2208
2209static int Win16OpenClipboard(void);
2210static int Win16CloseClipboard(void);
2211static int Win16EmptyClipboard(void);
2212static char_u *Win16GetClipboardData(int clip_data_format);
2213static int Win16SetClipboardData(int clip_data_format, char_u *clip_data, int clip_data_size, int clip_data_type);
2214
2215/*
2216 * Make vim the owner of the current selection. Return OK upon success.
2217 */
2218 int
2219clip_mch_own_selection(VimClipboard *cbd)
2220{
2221 /*
2222 * Never actually own the clipboard. If another application sets the
2223 * clipboard, we don't want to think that we still own it.
2224 */
2225 return FAIL;
2226}
2227
2228/*
2229 * Make vim NOT the owner of the current selection.
2230 */
2231 void
2232clip_mch_lose_selection(VimClipboard *cbd)
2233{
2234 /* Nothing needs to be done here */
2235}
2236
2237/*
2238 * Read the Windows clipboard text and put it in Vim's clipboard register.
2239 */
2240 void
2241clip_mch_request_selection(VimClipboard *cbd)
2242{
Bram Moolenaard44347f2011-06-19 01:14:29 +02002243 int type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 char_u *pAllocated = NULL;
2245 char_u *pClipText = NULL;
2246 int clip_data_format = 0;
2247
2248 if (Win16OpenClipboard())
2249 {
2250 /* Check for Vim's own clipboard format first. The CF_VIMCLIP format
2251 * is just ordinary text (like CF_TEXT) except prepended by the
2252 * selection type (as a single character). Note that under DOS we
2253 * actually cannot define a custom CF_VIMCLIP clipboard format; we
2254 * use instead one of the existing Windows-defined formats, usually
2255 * "DIF" or "SYLK". See Win16GetClipboardData() for details.
2256 *
2257 * Note that Win16GetClipboardData() returns the address of the memory
2258 * block it allocated. This is not necessary the start of the
2259 * clipboard text data: there may be other bytes ahead of the
2260 * text (particularly for CF_VIMCLIP) which are used for data
2261 * management. So pClipText is not necessarily == pAllocated.
2262 */
2263
2264 if ((pAllocated = Win16GetClipboardData(CF_VIMCLIP)) != NULL)
2265 {
2266 clip_data_format = CF_VIMCLIP;
2267 pClipText = pAllocated;
2268
2269 switch (*pClipText++) /* after ++, pClipText points to text */
2270 {
2271 default:
2272 case 'L': type = MLINE; break;
2273 case 'C': type = MCHAR; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 case 'B': type = MBLOCK; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276 }
2277
2278 /* Otherwise, check for the normal Windows text formats. There are
2279 * two of these: CF_TEXT (common) and CF_OEMTEXT (used for DOS
2280 * compatibility). Experiments show that, under the DOS/Windows
2281 * clipboard interface, writing CF_TEXT data to the clipboard
2282 * automatically creates a CF_OEMTEXT format as well.
2283 */
2284
2285 else if ((pAllocated = Win16GetClipboardData(CF_TEXT)) != NULL)
2286 {
2287 clip_data_format = CF_TEXT;
2288 pClipText = pAllocated;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290
2291 else if ((pAllocated = Win16GetClipboardData(CF_OEMTEXT)) != NULL)
2292 {
2293 clip_data_format = CF_OEMTEXT;
2294 pClipText = pAllocated;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296
2297 /* Did we get anything? */
2298
2299 if (pClipText != NULL)
2300 {
2301 char_u *pDest;
2302 char_u *pStart;
2303 char_u *pEnd;
2304
2305 long_u clip_data_size = 0;
2306
2307 /* The Windows clipboard normally stores its text lines terminated
2308 * by <CR><NL>. But Vim uses only <NL>, so translate the <CR><NL>
2309 * into <NL>. Also, watch for possible null bytes at the end of
2310 * pClipText. These are padding added by "get_clipboard_data"
2311 * (int 0x2f, AX= 0x1705) in order to round the data size up to the
2312 * next multiple of 32 bytes. See Win16GetClipboardData() for
2313 * details.
2314 */
2315
2316 pDest = strstr( pClipText, "\r\n" ); /* find first <CR><NL> */
2317
2318 if (pDest != NULL) /* found one? */
2319 {
2320 pStart = pDest + 1; /* points to <NL> after <CR> */
2321 pEnd = strstr( pStart, "\r\n" );/* find next <CR><NL> */
2322
2323 while (pEnd != NULL) /* found one? */
2324 {
2325 memmove(pDest, pStart, (long)(pEnd - pStart));
2326 /* exclude <CR> */
2327 pDest += (long)(pEnd - pStart); /* new destination */
2328 pStart = pEnd + 1; /* new starting point */
2329 pEnd = strstr(pStart, "\r\n"); /* find next <CR><NL> */
2330 }
2331
2332 /* Fell out of while() loop: no more <CR><NL> pairs. Just copy
2333 * the rest of the data, up to the first null byte. */
2334 pEnd = strchr(pStart, '\0'); /* find first null */
2335
2336 memmove(pDest, pStart, (long)(pEnd - pStart)); /* exclude nul */
2337 pDest += (long)(pEnd - pStart);
2338 *pDest = '\0'; /* terminate */
2339
2340 /* Now that all <CR><NL> pairs have been "compressed" into just
2341 * <NL>'s, determine the true text length. */
2342 clip_data_size = (long_u)(pDest - pClipText);
2343 }
2344 else
2345 {
2346 /* no <CR><NL> pairs at all */
2347 /* Since the data may have been padded with trailing nulls,
2348 * determine the true string length. */
2349 clip_data_size = STRLEN(pClipText); /* true data length */
2350 }
2351
2352 /* Copy the cleaned-up data over to Vim's clipboard "*" register. */
2353 clip_yank_selection(type, pClipText, clip_data_size, cbd);
2354
2355 /* Free the memory that Win16GetClipboardData() allocated. */
2356 vim_free(pAllocated);
2357 }
2358
2359 Win16CloseClipboard();
2360
2361 } // end if (Win16OpenClipboard())
2362}
2363
2364/*
2365 * Send the currently selected Vim text to the Windows clipboard.
2366 */
2367 void
2368clip_mch_set_selection( VimClipboard *cbd )
2369{
2370 char_u *pClipData = NULL;
2371 long_u clip_data_size;
2372 int clip_data_type;
2373
2374 /* If the '*' register isn't already filled in, fill it in now. */
2375 cbd->owned = TRUE;
2376 clip_get_selection(cbd);
2377 cbd->owned = FALSE;
2378
2379 /*
2380 * clip_convert_selection() returns a pointer to a buffer containing
2381 * the text to send to the Windows clipboard, together with a count
2382 * of the number of characters (bytes) in the buffer. The function's
2383 * return value is the 'type' of selection: MLINE, MCHAR, or MBLOCK;
2384 * or -1 for failure.
2385 */
2386 clip_data_type = clip_convert_selection(&pClipData, &clip_data_size, cbd);
2387
2388 if (clip_data_type < 0) /* could not convert? */
2389 return; /* early exit */
2390
2391 if (Win16OpenClipboard())
2392 {
2393 if (Win16EmptyClipboard())
2394 {
2395 int sentOK;
2396
2397 sentOK = Win16SetClipboardData(CF_TEXT, pClipData,
2398 clip_data_size, clip_data_type);
2399 sentOK = Win16SetClipboardData(CF_VIMCLIP,
2400 pClipData, clip_data_size, clip_data_type) && sentOK;
2401
2402 if (!sentOK)
2403 {
2404 /* one or both of Win16SetClipboardData() failed. */
2405 /* Technically we don't know why Win16SetClipboardData()
2406 * failed, but almost always it will be because there wasn't
Bram Moolenaar035db9f2007-05-10 18:02:27 +00002407 * enough DOS memory to buffer the data, so report that as the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 * problem.
2409 *
2410 * We report the error here (instead of in
2411 * Win16SetClipboardData()) because we don't want the error
2412 * reported twice.
2413 */
2414 EMSG("E450: Selection too large, cannot allocate DOS buffer");
2415 }
2416 }
2417
2418 Win16CloseClipboard();
2419 }
2420
2421 /* release memory allocated by clip_convert_selection() */
2422 vim_free(pClipData);
2423
2424 return;
2425}
2426
2427/*
2428 * Win16OpenClipboard: open the Windows clipboard. The clipboard must be open
2429 * before it can be communicated with at all. Return TRUE on success,
2430 * FALSE on failure.
2431 */
2432 static int
2433Win16OpenClipboard(void)
2434{
2435 __dpmi_regs dpmi_regs;
2436
2437 long start_time;
2438 int tick_count;
2439
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002440 /* int 0x2f, AX = 0x1701 attempts to open the Windows clipboard. Upon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 * return from the interrupt, if AX is non-zero, the clipboard was
2442 * successfully opened. If AX is zero, the clipboard could not be opened
2443 * because it is currently in use by another process.
2444 *
2445 * Remark: other DOS programs I (dk) have written that use the Windows
2446 * clipboard sometimes encounter the problem that the clipboard cannot
2447 * be opened even though it is demonstrably not in use by any other
2448 * process. In all cases, repeated attempts to open the clipboard
2449 * eventually succeed, but the initial attempt occasionally fails.
2450 *
2451 * The problem is intermittent and appears to be related to DOS being
2452 * "busy" at certain unpredictable times. DOS maintains two internal
2453 * flags that indicate whether it's busy: InDOS and CritErr. The
2454 * location of InDOS can be found by calling int 0x21, AH = 0x34. The
2455 * location of CritErr can be found by calling int 0x21, AX = 0x5d06.
2456 * If either of these flags is set, DOS is "busy" and cannot be
2457 * interrupted. See "Undocumented DOS" by Schulman et al for details.
2458 *
2459 * However here I take the easier approach that if the first call to open
2460 * the clipboard does not succeed, just try again. In fact, try once per
2461 * biostime() clock tick, up to 18 times (about one second).
2462 */
2463
2464 tick_count = 0;
2465
2466 dpmi_regs.x.ax = 0x1701; /* open Windows clipboard */
2467 if (__dpmi_int(0x2f, &dpmi_regs) == -1)
2468 {
2469 /* real-mode interrupt failed? */
2470 return FALSE; /* FALSE --> clipboard not open */
2471 }
2472
2473 /* wait up to one second */
2474 while (dpmi_regs.x.ax == 0 && tick_count++ < 18)
2475 {
2476 /* Wait one clock tick (18.2 ticks/sec = 55 msec per tick).
2477 *
2478 * We busy-wait here. Unfortunately, delay() and usleep() have been
2479 * reported to give problems with the original Windows 95. This is
2480 * fixed in service pack 1, but not everybody installed that.
2481 */
2482 start_time = biostime(0, 0L);
2483 while (biostime(0, 0L) == start_time)
2484 ;
2485
2486 dpmi_regs.x.ax = 0x1701; /* open Windows clipboard */
2487 if (__dpmi_int(0x2f, &dpmi_regs) == -1)
2488 {
2489 /* real-mode interrupt failed? */
2490 return FALSE; /* FALSE --> clipboard not open */
2491 }
2492 }
2493
2494 /* Couldn't open the clipboard, even after 18 attempts? */
2495
2496 if (tick_count >= 18 && dpmi_regs.x.ax == 0)
2497 return FALSE; /* FALSE --> clipboard not open */
2498
2499 return TRUE; /* TRUE --> clipboard opened successfully, OK */
2500}
2501
2502/*
2503 * Win16CloseClipboard: close the Windows clipboard. Return TRUE on
2504 * success, FALSE on failure. This function can always be called,
2505 * whether the clipboard is open or not.
2506 */
2507 static int
2508Win16CloseClipboard(void)
2509{
2510 __dpmi_regs dpmi_regs;
2511
2512 /* Close the clipboard. This interrupt can always be called, even
2513 * if the clipboard is already closed.
2514 */
2515
2516 dpmi_regs.x.ax = 0x1708; /* close the clipboard */
2517 if (__dpmi_int(0x2f, &dpmi_regs) == -1)
2518 {
2519 /* real-mode interrupt failed? */
2520 return FALSE; /* FALSE --> clipboard could not be closed */
2521 }
2522
2523 return TRUE; /* TRUE --> clipboard closed successfully, OK */
2524}
2525
2526/*
2527 * Win16EmptyClipboard: empty the (previously opened) Windows clipboard.
2528 * Return TRUE on success, FALSE on failure.
2529 */
2530 static int
2531Win16EmptyClipboard(void)
2532{
2533 __dpmi_regs dpmi_regs;
2534
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002535 /* int 0x2f, AX = 0x1702 attempts to empty the Windows clipboard. Upon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 * return from the interrupt, if AX == 0, the clipboard could not be
2537 * emptied (for some reason).
2538 */
2539 dpmi_regs.x.ax = 0x1702; /* empty the Windows clipboard */
2540 if (__dpmi_int(0x2f, &dpmi_regs) == -1)
2541 {
2542 /* real-mode interrupt failed? */
2543 return FALSE; /* FALSE --> clipboard could not be emptied */
2544 }
2545
2546 /* Did we succeed in clearing the clipboard? */
2547 if (dpmi_regs.x.ax == 0)
2548 return FALSE; /* FALSE --> clipboard could not be emptied */
2549
2550 return TRUE; /* TRUE --> clipboard was emptied, OK */
2551}
2552
2553/*
2554 * FreeDOSMemory: a helper function to free memory previously
2555 * allocated by a call to __dpmi_allocate_dos_memory().
2556 */
2557 static void
2558FreeDOSMemory(int protected_mode_selector)
2559{
2560 /* Free the DOS buffer and release the DPMI prot-mode selector.
2561 *
2562 * It's important that DOS memory be properly released because
2563 * there's only a limited amount of it. Therefore, if the call
2564 * to __dpmi_free_dos_memory() fails, emit an error message
2565 * unconditionally.
2566 */
2567 if (__dpmi_free_dos_memory(protected_mode_selector) == -1)
2568 EMSG("E451: could not free DOS memory buffer (DJGPP)");
2569}
2570
2571/*
2572 * Win16GetClipboardData: query the Windows clipboard as to whether data
2573 * is available in a particular clipboard format. If data is
2574 * available, allocate a buffer for it and read the data from the
2575 * clipboard into the buffer. Return a pointer to the buffer. If
2576 * no data is available in the requested format, return NULL.
2577 *
2578 * This routine allocates memory to hold the retrieved clipboard
2579 * data. It's the caller's responsibility to free this memory
2580 * once it's finished using it. The memory should be freed by
2581 * calling vim_free().
2582 */
2583 static char_u *
2584Win16GetClipboardData(int clip_data_format)
2585{
2586 __dpmi_regs dpmi_regs;
2587
2588 int real_mode_segment_address;
2589 int protected_mode_selector;
2590
2591 char_u *clip_data_buffer;
2592 long_u clip_data_size;
2593
2594 /* We only handle clipboard formats we recognize, others are ignored.
2595 *
2596 * It's not possible to create a custom clipboard format for VimClipboard
2597 * data under DOS, so one of the predefined Windows formats had to be
2598 * used for CF_VIMCLIP. Two obscure formats, popular when Windows 3.0
2599 * came out but no longer in much use today, are the DIF and SYLK formats.
2600 * DIF is the Data Interchange Format, SYLK is the Symbolic Link format.
2601 * They are both text formats and either one can be hijacked for use as
2602 * "the VimClipboard format". Of course, this conflicts with anyone who
2603 * still *is* using DIF or SYLK data formats, but that will be very few
2604 * people.
2605 *
2606 * I (dk) chose SYLK as the more obscure format because it was used
2607 * mostly for Microsoft Multiplan (the pre-cursor to Excel) and it's not
2608 * likely Multiplan is used anywhere much anymore. Mind you, Excel can
2609 * still export to both DIF and SYLK formats.
2610 */
2611
2612 switch (clip_data_format)
2613 {
2614 case CF_VIMCLIP: /* Vim's own special clipboard format */
2615 case CF_TEXT: /* Windows text */
2616 case CF_OEMTEXT: /* DOS (OEM) text */
2617
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002618 /* int 0x2f, AX = 0x1704 returns the number of bytes of data currently
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 * on the Windows clipboard, for the specified format. Upon return
2620 * from the interrupt, DX:AX = the number of bytes, rounded up to the
2621 * nearest multiple of 32.
2622 */
2623
2624 dpmi_regs.x.ax = 0x1704; /* get size of clipbd data */
2625 dpmi_regs.x.dx = clip_data_format;
2626 if (__dpmi_int(0x2f, &dpmi_regs) == -1)
2627 {
2628 /* real-mode interrupt failed? */
2629 return NULL; /* early exit */
2630 }
2631
2632 /* Did we get anything? If not, this is not an error. */
2633 if (dpmi_regs.x.dx == 0 && dpmi_regs.x.ax == 0)
2634 {
2635 /* no CF_VIMCLIP data? */
2636 return NULL; /* early exit */
2637 }
2638
2639 /* There is data available in the requested clipboard format.
2640 *
2641 * Calculate data size. Remember this is rounded up to the nearest
2642 * multiple of 32, so clip_data_size is actually an upper limit.
2643 * The extra bytes, if any, are set to null (0x00) when the data is
2644 * read from the clipboard. (Later:) actually I'm no longer sure
2645 * this is strictly true: the end-of-data is marked by a null, but
2646 * the extra bytes appear to sometimes be null, sometimes not.
2647 * They may just be garbage.
2648 */
2649 clip_data_size = dpmi_regs.x.ax + (dpmi_regs.x.dx << 16);
2650
2651 /* Allocate memory to retrieve the data. The buffer has to lie in the
2652 * DOS memory region (in the first 1 MByte of address space) because
2653 * the Windows clipboard interface expects a 16-bit segment:offset
2654 * pointer to a buffer address within the DOS region. Must therefore
2655 * use __dpmi_allocate_dos_memory() instead of lalloc() or alloc().
2656 */
2657 real_mode_segment_address = __dpmi_allocate_dos_memory(
2658 (clip_data_size + 15) >> 4, /* buffer size, in 16-byte paragraphs */
2659 &protected_mode_selector); /* prot-mode selector for the address */
2660
2661 if (real_mode_segment_address == -1)
2662 {
2663 /* memory allocation failed. */
2664
2665 /* Technically we don't know why the allocation failed, but
2666 * almost always it will be because there wasn't enough DOS
2667 * memory to satisfy the request, so report that as the problem.
2668 * On my system, DJGPP is able to satisfy a DOS allocation request
2669 * up to about 600K in size. This depends on your HIMEM.SYS and
2670 * EMM386.EXE settings however.
2671 */
2672 EMSG("E452: Clipboard data too large, cannot allocate DOS buffer");
2673 return NULL; /* early exit */
2674 }
2675
2676 /* Copy data from the clipboard into the buffer. Experiments show that
2677 * the Windows clipboard is smart enough to handle data transfers
2678 * larger than 64K properly, even though the buffer address is a 16-bit
2679 * segment:offset (which would normally limit the block size to 64K
2680 * unless ES gets incremented).
2681 */
2682 dpmi_regs.x.ax = 0x1705; /* get clipboard data */
2683 dpmi_regs.x.dx = clip_data_format; /* CF_VIMCLIP */
2684 dpmi_regs.x.es = real_mode_segment_address; /* buffer ad: segment */
2685 dpmi_regs.x.bx = 0; /* buffer ad: offset */
2686 if (__dpmi_int( 0x2f, &dpmi_regs) == -1)
2687 {
2688 /* real-mode interrupt failed? */
2689 EMSG("E453: could not copy clipboard data to DOS buffer");
2690 FreeDOSMemory(protected_mode_selector); /* clean up DOS mem */
2691 return NULL; /* early exit */
2692 }
2693
2694 /* Clipboard data is now in DOS memory in the buffer pointed to by
2695 * ES:BX. Copy this into ordinary memory that Vim can access (ie.
2696 * prot-mode memory). Allocate one extra byte to ensure the text
2697 * is terminated properly (in case it was somehow corrupted).
2698 */
2699 clip_data_buffer = (char_u *)lalloc(clip_data_size + 1, TRUE);
2700
2701 if (clip_data_buffer == NULL)
2702 {
2703 /* allocation failed? */
2704 EMSG("E454: could not allocate clipboard memory buffer");
2705 FreeDOSMemory(protected_mode_selector); /* clean up DOS mem */
2706 return NULL; /* early exit */
2707 }
2708
2709 *(clip_data_buffer + clip_data_size) = '\0'; /* ensure terminated */
2710
2711 /* Copy the data from DOS memory to Vim-accessible memory. */
2712 movedata( /* DJGPP version of memcpy() */
2713 protected_mode_selector, 0, /* source: DOS ad (via selector) */
2714 _my_ds(), (unsigned)clip_data_buffer,
2715 /* target: normal mem address */
2716 clip_data_size); /* how many bytes */
2717
2718 /* Free the DOS buffer and release the DPMI prot-mode selector. */
2719 FreeDOSMemory(protected_mode_selector); /* clean up DOS memory */
2720
2721 return clip_data_buffer; /* return pointer to allocated buffer */
2722
2723 default: /* unknown clipboard format */
2724 return NULL;
2725 }
2726}
2727
2728/*
2729 * Win16SetClipboardData: send 'clip_data_size' bytes of data from the buffer
2730 * pointed to by 'clip_data', to the Windows clipboard. The data is
2731 * registered with the clipboard as being in the 'clip_data_format'
2732 * format.
2733 */
2734 static int
2735Win16SetClipboardData(
2736 int clip_data_format,
2737 char_u *clip_data,
2738 int clip_data_size,
2739 int clip_data_type)
2740{
2741 __dpmi_regs dpmi_regs;
2742
2743 int real_mode_segment_address;
2744 int protected_mode_selector;
2745 long_u protected_mode_offset = 0L;
2746 int total_size = clip_data_size;
2747
2748 char_u *clip_sel_type;
2749
2750 /* If we're using the CF_VIMCLIP custom format, allocate an extra
2751 * byte for clip_sel_type, which is a character indicating the type
2752 * of text selection: MLINE, MCHAR, or MBLOCK.
2753 */
2754 if (clip_data_format == CF_VIMCLIP)
2755 total_size++; /* extra byte for marker */
2756
2757 /* Data cannot be sent directly from a Vim string (pClipData) to
2758 * the Windows clipboard, because the Windows clipboard interface
2759 * expects a 16-bit (DOS) segment:offset address for the source
2760 * buffer. Therefore we must create a "transfer buffer" in the DOS
2761 * memory region (in the first 1 MByte of address space) and copy
2762 * the Vim string into that. From there, the data can then be sent
2763 * to the Windows clipboard.
2764 *
2765 * To allocate DOS memory, we must use __dpmi_allocate_dos_memory()
2766 * instead of lalloc() or alloc(). If the allocation fails, it will
2767 * almost invariably be because there is not enough DOS memory
2768 * available to accommodate the size of clip_data. There is nothing
2769 * we can do about this, we simply have to fail.
2770 */
2771 real_mode_segment_address = __dpmi_allocate_dos_memory(
2772 (total_size + 15) >> 4, /* buffer size, in 16-byte paragraphs */
2773 &protected_mode_selector); /* prot-mode selector for the address */
2774
2775 if (real_mode_segment_address == -1)
2776 {
2777 /* memory allocation failed. */
2778 /* Technically we don't know why the allocation failed, but
2779 * almost always it will be because there wasn't enough DOS
2780 * memory to satisfy the request. On my system, DJGPP is able
2781 * to satisfy a DOS allocation request up to about 600K in size.
2782 * This depends however on HIMEM.SYS and EMM386.EXE settings.
2783 */
2784 return FALSE; /* early exit */
2785 }
2786
2787 /* Copy data from Vim's buffer (clip_data) into the DOS transfer buffer.
2788 * This can be larger than 64K; movedata() takes care of crossing any
2789 * 16-bit segment boundaries.
2790 *
2791 * If we're using Vim's custom clipboard format, we must copy one extra
2792 * byte to indicate the type of selection: line, character, or block.
2793 */
2794 if (clip_data_format == CF_VIMCLIP)
2795 {
2796 switch (clip_data_type)
2797 {
2798 default:
2799 case MLINE: clip_sel_type = "L"; break;
2800 case MCHAR: clip_sel_type = "C"; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 case MBLOCK: clip_sel_type = "B"; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 }
2803
2804 movedata(
2805 _my_ds(), (unsigned)clip_sel_type,
2806 /* source: normal memory address */
2807 protected_mode_selector, 0, /* target: DOS ad (via selector) */
2808 1); /* how many bytes to copy */
2809
2810 protected_mode_offset += STRLEN(clip_sel_type); /* allow for marker */
2811 }
2812
2813 movedata(
2814 _my_ds(), (unsigned)clip_data, /* source: normal memory address */
2815 protected_mode_selector, /* target: DOS address (via selector) */
2816 protected_mode_offset, /* non-zero, if using clip_sel_type */
2817 clip_data_size); /* how many bytes to copy */
2818
2819 /* Send data from the DOS transfer buffer to the Windows clipboard.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002820 * int 0x2f, AX = 0x1703 sends SI:CX bytes of data from the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 * at ES:BX, to the clipboard.
2822 */
2823 dpmi_regs.x.ax = 0x1703; /* send clipboard data */
2824 dpmi_regs.x.dx = clip_data_format; /* flag: format of the data */
2825 dpmi_regs.x.si = ((total_size >> 16)
2826 & 0x0000ffffL); /* hi word of data size */
2827 dpmi_regs.x.cx = (total_size & 0x0000ffffL);
2828 /* lo word of data size */
2829 dpmi_regs.x.es = real_mode_segment_address; /* buffer address: segment */
2830 dpmi_regs.x.bx = 0; /* buffer address: offset */
2831 if (__dpmi_int(0x2f, &dpmi_regs) == -1)
2832 {
2833 /* real-mode interrupt failed. */
2834 FreeDOSMemory(protected_mode_selector); /* clean up DOS memory */
2835 return FALSE; /* early exit */
2836 }
2837
2838 /* Free the DOS buffer and release the DPMI prot-mode selector. */
2839 FreeDOSMemory(protected_mode_selector); /* clean up DOS memory */
2840
2841 return TRUE; /* TRUE --> data successfully sent to clipboard */
2842}
2843
2844#endif /* FEAT_CLIPBOARD */
2845#endif /* DJGPP */
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847/* common MS-DOS and Win16 code follows */
2848
2849 static int
2850vim_chmod(char_u *name)
2851{
2852 char_u *p;
2853 int f;
2854 int c = 0;
2855
2856 /* chmod() can't handle a file name with a trailing slash, remove it.
2857 * But don't remove it for "/" or "c:/". */
2858 p = name + STRLEN(name);
2859 if (p > name)
2860 --p;
2861 if (p > name && (*p == '\\' || *p == '/') && p[-1] != ':')
2862 {
2863 c = *p; /* remove trailing (back)slash */
2864 *p = NUL;
2865 }
2866 else
2867 p = NULL;
2868#if defined(__BORLANDC__) && (__BORLANDC__ > 0x410)
2869 /* this also sets the archive bit, supported by Borland C 4.0 and later,
2870 * where __BORLANDC__ is 0x450 (3.1 is 0x410) */
2871 f = _rtl_chmod((char *)name, 0, 0);
2872#else
2873 f = _chmod((char *)name, 0, 0);
2874#endif
2875 if (p != NULL)
2876 *p = c; /* put back (back)slash */
2877 return f;
2878}
2879
2880/*
2881 * get file permissions for 'name'
2882 * Returns -1 for error.
2883 * Returns FA_attributes defined in dos.h
2884 */
2885 long
2886mch_getperm(char_u *name)
2887{
2888 return (long)vim_chmod(name); /* get file mode */
2889}
2890
2891/*
2892 * set file permission for 'name' to 'perm'
2893 *
2894 * return FAIL for failure, OK otherwise
2895 */
2896 int
2897mch_setperm(
2898 char_u *name,
2899 long perm)
2900{
2901 perm |= FA_ARCH; /* file has changed, set archive bit */
2902#if defined(__BORLANDC__) && (__BORLANDC__ > 0x410)
2903 return (_rtl_chmod((char *)name, 1, (int)perm) == -1 ? FAIL : OK);
2904#else
2905 return (_chmod((char *)name, 1, (int)perm) == -1 ? FAIL : OK);
2906#endif
2907}
2908
2909/*
2910 * Set hidden flag for "name".
2911 */
2912 void
2913mch_hide(char_u *name)
2914{
2915 /* DOS 6.2 share.exe causes "seek error on file write" errors when making
2916 * the swap file hidden. Thus don't do it. */
2917}
2918
2919/*
2920 * return TRUE if "name" is a directory
2921 * return FALSE if "name" is not a directory
2922 * return FALSE for error
2923 *
2924 * beware of a trailing (back)slash
2925 */
2926 int
2927mch_isdir(char_u *name)
2928{
2929 int f;
2930
2931 f = vim_chmod(name);
2932 if (f == -1)
2933 return FALSE; /* file does not exist at all */
2934 if ((f & FA_DIREC) == 0)
2935 return FALSE; /* not a directory */
2936 return TRUE;
2937}
2938
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939/*
2940 * Return 1 if "name" can be executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01002941 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 * Return -1 if unknown.
2943 */
2944 int
Bram Moolenaar779a7752016-01-30 23:26:34 +01002945mch_can_exe(
2946 char_u *name,
2947 char_u **path,
2948 int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002950 char *p;
Bram Moolenaarb5971142015-03-21 17:32:19 +01002951 int mode;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002952
Bram Moolenaarb5971142015-03-21 17:32:19 +01002953 if (!use_path)
2954 {
2955 /* TODO: proper check if file is executable. */
2956 mode = vim_chmod(name);
2957 return mode != -1 && (mode & FA_DIREC) == 0;
2958 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002959 p = searchpath(name);
2960 if (p == NULL || mch_isdir(p))
2961 return FALSE;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02002962 if (path != NULL)
2963 *path = vim_strsave(p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002964 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
2967/*
2968 * Check what "name" is:
2969 * NODE_NORMAL: file or directory (or doesn't exist)
2970 * NODE_WRITABLE: writable device, socket, fifo, etc.
2971 * NODE_OTHER: non-writable things
2972 */
2973 int
2974mch_nodetype(char_u *name)
2975{
2976 if (STRICMP(name, "AUX") == 0
2977 || STRICMP(name, "CON") == 0
2978 || STRICMP(name, "CLOCK$") == 0
2979 || STRICMP(name, "NUL") == 0
2980 || STRICMP(name, "PRN") == 0
2981 || ((STRNICMP(name, "COM", 3) == 0
2982 || STRNICMP(name, "LPT", 3) == 0)
2983 && VIM_ISDIGIT(name[3])
2984 && name[4] == NUL))
2985 return NODE_WRITABLE;
2986 /* TODO: NODE_OTHER? */
2987 return NODE_NORMAL;
2988}
2989
2990/*
2991 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2992 * Return OK for success, FAIL for failure.
2993 */
2994 int
2995mch_dirname(
2996 char_u *buf,
2997 int len)
2998{
2999#ifdef DJGPP
3000 if (getcwd((char *)buf, len) == NULL)
3001 return FAIL;
3002 /* turn the '/'s returned by DJGPP into '\'s */
3003 slash_adjust(buf);
3004 return OK;
3005#else
3006 return (getcwd((char *)buf, len) != NULL ? OK : FAIL);
3007#endif
3008}
3009
3010/*
3011 * this version of remove is not scared by a readonly (backup) file
3012 *
3013 * returns -1 on error, 0 otherwise (just like remove())
3014 */
3015 int
3016mch_remove(char_u *name)
3017{
3018 (void)mch_setperm(name, 0); /* default permissions */
3019 return unlink((char *)name);
3020}
3021
3022/*
3023 * Special version of getenv(): Use uppercase name.
3024 */
3025 char_u *
3026mch_getenv(char_u *name)
3027{
3028 int i;
3029#define MAXENVLEN 50
3030 char_u var_copy[MAXENVLEN + 1];
3031 char_u *p;
3032 char_u *res;
3033
3034 /*
3035 * Take a copy of the argument, and force it to upper case before passing
3036 * to getenv(). On DOS systems, getenv() doesn't like lower-case argument
3037 * (unlike Win32 et al.) If the name is too long to fit in var_copy[]
3038 * allocate memory.
3039 */
3040 if ((i = STRLEN(name)) > MAXENVLEN)
3041 p = alloc(i + 1);
3042 else
3043 p = var_copy;
3044 if (p == NULL)
3045 p = name; /* out of memory, fall back to unmodified name */
3046 else
3047 {
3048 for (i = 0; name[i] != NUL; ++i)
3049 p[i] = toupper(name[i]);
3050 p[i] = NUL;
3051 }
3052
3053 res = (char_u *)getenv((char *)p);
3054
3055 if (p != var_copy && p != name)
3056 vim_free(p);
3057
3058 return res;
3059}
3060
3061/*
3062 * Insert user name in s[len].
3063 */
3064 int
3065mch_get_user_name(
3066 char_u *s,
3067 int len)
3068{
3069 *s = NUL;
3070 return FAIL;
3071}
3072
3073/*
3074 * Insert host name is s[len].
3075 */
3076 void
3077mch_get_host_name(
3078 char_u *s,
3079 int len)
3080{
3081#ifdef DJGPP
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003082 vim_strncpy(s, "PC (32 bits Vim)", len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083#else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003084 vim_strncpy(s, "PC (16 bits Vim)", len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086}