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