comparison src/os_msdos.c @ 7:3fc0f57ecb91 v7.0001

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