home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / JED / JED097-1.TAR / jed / src / i386.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  9.2 KB  |  461 lines

  1. /*
  2.  *  Copyright (c) 1992, 1994 John E. Davis  (davis@amy.tch.harvard.edu)
  3.  *  All Rights Reserved.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #ifndef __WATCOMC__
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <dir.h>
  11. #include <pc.h>
  12. #else
  13. #include <sys\types.h>
  14. #include <sys\stat.h>
  15. #include <direct.h>
  16. #include <conio.h>
  17. #include <io.h>
  18.  /* for some reason _bios_keybrd seems to work better than kbhit() 
  19.   * even though the documentation indicates they do the same thing.
  20.   */
  21. #define kbhit() _bios_keybrd(_NKEYBRD_READY)
  22. #endif /* NOT WATCOM */
  23.  
  24. #include <errno.h>
  25.  
  26. #include "config.h"
  27. #include "sysdep.h"
  28.  
  29. #define BIOSKEY i386_bioskey
  30.  
  31. #include <dos.h>
  32. #include <bios.h>
  33. #include <process.h>
  34.  
  35. #include "dos_os2.c"
  36.  
  37. int Abort_Char = 7;               /* scan code for G (control) */
  38.  
  39. #ifndef __WATCOMC__
  40. unsigned int i386_bioskey(int f)
  41. {
  42.    union REGS in, out;
  43.    in.h.ah = f | 0x10;               /* use enhanced kbd */
  44.    int86(0x16, &in, &out);
  45.    return(out.x.ax & 0xFFFF);
  46. }
  47. #endif
  48.  
  49. /* Here I also map keys to edt keys */
  50. #ifndef __WATCOMC__
  51. unsigned char sys_getkey()
  52. {
  53. #if 0
  54.    char *keypad_scan = 
  55.       "\x4e\x53\x52\x4f\x50\x51\x4b\x4c\x4d\x47\x48\x49\x37\x4a";
  56.    char *edt_chars = "lnpqrstuvwxyRS";
  57.    int bra = 'O', P = 'P', keypad, weird;
  58.    char *p;
  59. #endif
  60.    char *normal = "!@#$%^&*()-=\t*\0177QWERTYUIOP[]\r*ASDFGHJKL;'`*\\ZXCVBNM<>/";
  61.    int wit = 300;
  62.    unsigned int c, c1, shft;
  63.    unsigned int i;
  64.  
  65.    if (!kbhit()) while (!sys_input_pending(&wit))
  66.      {
  67.     if (Display_Time)
  68.       {
  69.          JWindow->trashed = 1;
  70.          update((Line *) NULL, 0, 1);
  71.       }
  72.      }
  73.    
  74.    i = (unsigned int) BIOSKEY(0);
  75.    c = i & 0xFF;
  76.    c1 = i >> 8;
  77.    
  78.    shft = BIOSKEY(2) & 0xF;
  79. #if 0   
  80.    keypad = (c1 > 0x36) && NumLock_Is_Gold;
  81.    /* allow Shift-keypad to give non edt keypad chars */
  82.    keypad = keypad && !(*shift & 0x3);
  83.    
  84.    /* This is for the damned enter and slash keys */
  85.    weird = keypad && ((c1 == 0xE0) && ((c == 0xD) || (c == 0x2F)));
  86.    
  87.    keypad = keypad && (c != 0xE0);  /* excludes small keypad */
  88. #endif
  89.    if (i == 0x0E08) c = 127;
  90.    else if (i == 0x1C0A) c = 13;   /* ^Enter --> Enter */
  91.    else if ((c == 32) && (shft & 0x04))   /* ^ space = ^@ */
  92.      {
  93.     c = 3;
  94.     ungetkey((int *) &c);
  95.     c = 0;
  96.      }
  97. #if 0
  98.    else if (weird || (keypad && (NULL != (p = strchr(keypad_scan, c1)))))
  99.      {
  100.     if (c1 == 0xE0) if (c == 0x2f) c = 'Q'; else c = 'M';
  101.     else c = (unsigned int) edt_chars[(int) (p - keypad_scan)];
  102.     ungetkey((int *) &c);
  103.     ungetkey((int *) &bra);
  104.     c = 27;
  105.      }
  106. #endif
  107.    else if ((!c) || ((c == 0xe0) && (c1 > 0x43)))
  108.      {
  109. #if 0
  110.     if (NumLock_Is_Gold && (c1 == 0x3B))
  111.       {
  112.          ungetkey(&P); ungetkey(&bra); c = 27;
  113.       } 
  114.     /* if key is 5 of keypad, get another char */
  115.     else if ((c1 == 'L') && bios_key_f && (c == 0)) return(sys_getkey());
  116.     else 
  117.       {
  118. #endif
  119.          if ((shft == 0x8) && (c == 0) && PC_Alt_Char)
  120.            {
  121.           if ((c1 >= 14) && (c1 <= 53))
  122.             {
  123.                c1 = (unsigned int) normal[c1];
  124.                c = PC_Alt_Char;
  125.             }
  126.           else if ((c1 >= 120) && (c1 <= 131))
  127.             {
  128.                c1 = (unsigned int) normal[c1 - 120];
  129.                c = PC_Alt_Char;
  130.             }
  131.           else if (c1 == 165) /* tab */
  132.             {
  133.                c1 = (unsigned int) normal[c1 - 165 + 12];
  134.                c = PC_Alt_Char;
  135.             }
  136.            }
  137.          else c = 0;
  138.          ungetkey((int *) &c1);
  139. #if 0
  140.       }
  141. #endif
  142.      }
  143.    return(c);
  144. }
  145. #else
  146. /* WATCOM C */
  147. /* the interrupt bios routine did not seem to work with watcom c,
  148.  * however, the _bios_keybrd routine does seem to work. */
  149. unsigned char sys_getkey()
  150. {
  151.     char *normal = "!@#$%^&*()-=\t*\0177QWERTYUIOP[]\r*ASDFGHJKL;'`*\\ZXCVBNM<>/";
  152.     int wit = 300;
  153.     unsigned int c, c1, shft, i;
  154.  
  155.     if (!_bios_keybrd(_NKEYBRD_READY)) while (!sys_input_pending(&wit))
  156.       {
  157.       if (Display_Time)
  158.         {
  159.         JWindow->trashed = 1;
  160.         update((Line *) NULL, 0, 1);
  161.         }
  162.       }
  163.     i = _bios_keybrd(_NKEYBRD_READ);
  164.     c = i & 0xFF;
  165.     c1 = i >> 8;
  166.    
  167.     shft = _bios_keybrd(_NKEYBRD_SHIFTSTATUS) & 0xF;
  168.  
  169.     if (i == 0x0E08) c = 127;
  170.     else if (i == 0x1C0A) c = 13;   /* ^Enter --> Enter */
  171.     else if ((c == 32) && (shft & 0x04))   /* ^ space = ^@ */
  172.       {
  173.       c = 3;
  174.       ungetkey((int *) &c);
  175.       c = 0;
  176.       }
  177.     else if ((!c) || ((c == 0xe0) && (c1 > 0x43)))
  178.       {
  179.       if ((shft == 0x8) && (c == 0) && PC_Alt_Char)
  180.         {
  181.         if ((c1 >= 14) && (c1 <= 53))
  182.           {
  183.               c1 = (unsigned int) normal[c1];
  184.               c = PC_Alt_Char;
  185.           }
  186.         else if ((c1 >= 120) && (c1 <= 131))
  187.           {
  188.               c1 = (unsigned int) normal[c1 - 120];
  189.               c = PC_Alt_Char;
  190.           }
  191.         else if (c1 == 165) /* tab */
  192.           {
  193.               c1 = (unsigned int) normal[c1 - 165 + 12];
  194.               c = PC_Alt_Char;
  195.           }
  196.            }
  197.       else c = 0;
  198.       ungetkey((int *) &c1);
  199.       }
  200.     return(c);
  201. }
  202.  
  203. #endif  /* NOT __WATCOMC__ */
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. static int sys_input_pending(int *tsecs)
  211. {
  212.    int count = *tsecs * 5;
  213.    
  214.    if (Batch || Input_Buffer_Len) return(Input_Buffer_Len);
  215.    if (kbhit()) return 1;
  216.    
  217.    while (count > 0)
  218.      {
  219.     delay(20);               /* 20 ms or 1/50 sec */
  220.     if (kbhit()) break;
  221.     count--;
  222.      }
  223.    
  224.    return (count);
  225. }
  226.  
  227. /*  This is to get the size of the terminal  */
  228. #ifndef __WATCOMC__
  229. void get_term_dimensions(int *cols, int *rows)
  230. {
  231.    *rows = ScreenRows();
  232.    *cols = ScreenCols();
  233. }
  234. #else
  235. #include <graph.h>
  236. void get_term_dimensions(int *cols, int *rows)
  237. {
  238.    struct videoconfig vc;
  239.    _getvideoconfig(&vc);
  240.    
  241.    *rows = vc.numtextrows;
  242.    *cols = vc.numtextcols;
  243. }
  244. #endif
  245.  
  246.  
  247. /* returns 0 if file does not exist, 1 if it is not a dir, 2 if it is */
  248. int sys_chmod(char *file, int what, int *mode, short *dum1, short *dum2)
  249. {
  250.    struct stat buf;
  251.    int m;
  252.    *dum1 = *dum2 = 0;
  253.    
  254.    file = msdos_pinhead_fix_dir (file);
  255.    
  256.    if (what)
  257.      {
  258.     chmod(file, *mode);
  259.     return(0);
  260.      }
  261.  
  262.    if (stat(file, &buf) < 0) switch (errno)
  263.      {
  264.     case EACCES: return(-1); /* es = "Access denied."; break; */
  265.     case ENOENT: return(0);  /* ms = "File does not exist."; */
  266.     case ENOTDIR: return(-2); /* es = "Invalid Path."; */
  267.     default: return(-3); /* "stat: unknown error."; break;*/
  268.      }
  269.  
  270.    m = buf.st_mode;
  271.  
  272. /* AIX requires this */
  273. #ifdef _S_IFDIR
  274. #ifndef S_IFDIR
  275. #define S_IFDIR _S_IFDIR
  276. #endif
  277. #endif
  278.  
  279.    *mode = m & 0777;
  280.  
  281. #ifndef __WATCOMC__
  282.    if (m & S_IFDIR) return (2);
  283. #else
  284.    if (S_ISDIR(m)) return(2);
  285. #endif
  286.    return(1);
  287. }
  288.  
  289. unsigned long sys_file_mod_time(char *file)
  290. {
  291.    struct stat buf;
  292.  
  293.    if (stat(file, &buf) < 0) return(0);
  294.    return((unsigned long) buf.st_mtime);
  295. }
  296.  
  297. #ifndef __WATCOMC__
  298. static int cbreak;
  299. #endif
  300.  
  301. void reset_tty()
  302. {
  303. #ifndef __WATCOMC__
  304.    setcbrk(cbreak);
  305. #endif
  306. }
  307. void init_tty()
  308. {
  309. #ifndef __WATCOMC__
  310.    cbreak = getcbrk();
  311.    setcbrk(0);
  312. #endif
  313. }
  314.  
  315.  
  316. #ifndef __WATCOMC__
  317. static struct ffblk Dos_DTA;
  318. #else
  319. static struct find_t fileinfo;
  320. #endif
  321.  
  322. static char Found_Dir[256], *Found_File;
  323. /* found_File is a pointer into found_Dir such that the
  324.  * full pathname is stored in the following form
  325.  * "c:/dir/path/\0filename.ext\0"
  326.  */
  327.  
  328. #define lcase(x) if (((x) >= 'A') && ((x) <= 'Z')) (x) |= 0x20
  329.  
  330. static void dta_fixup_name (char *file)
  331. {
  332.    char *p;
  333.  
  334. #ifndef __WATCOMC__
  335.    strcpy (Found_File, Dos_DTA.ff_name);
  336. #else
  337.    strcpy (Found_File, fileinfo.name);
  338. #endif
  339.    
  340.    p = Found_File;
  341.    while (*p)
  342.      {
  343.     lcase(*p);
  344.     p++;
  345.      }
  346.    
  347.    strcpy(file, Found_Dir);
  348.    strcat(file, Found_File);
  349.    
  350. #ifndef __WATCOMC__
  351.    if (Dos_DTA.ff_attrib & FA_DIREC) strcat(file, "\\");
  352. #else
  353.    if (fileinfo.attrib & _A_SUBDIR) strcat(file, "\\");
  354. #endif
  355. }
  356.  
  357.  
  358.  
  359. int sys_findfirst(char *file)
  360. {
  361.    char *f;
  362.  
  363.    strcpy(Found_Dir, expand_filename(file) );
  364.    Found_File = extract_file( Found_Dir );
  365.  
  366.    f = Found_File;
  367.    
  368.    while (*f && (*f != '*')) f++;
  369.    if (! *f)
  370.      {
  371.     f = Found_File;
  372.     while (*f && (*f != '.')) f++;
  373.     if (*f) strcat(Found_Dir, "*"); 
  374.     else strcat(Found_Dir, "*.*");
  375.      }
  376.  
  377. #ifndef __WATCOMC__
  378.    if (findfirst(Found_Dir, &Dos_DTA, FA_RDONLY | FA_DIREC))
  379. #else
  380.    if (_dos_findfirst(Found_Dir, _A_RDONLY | _A_SUBDIR, &fileinfo))
  381. #endif
  382.      {
  383.     *Found_File++ = 0;
  384.     return 0;
  385.      }
  386.    *Found_File++ = 0;
  387.    
  388.    dta_fixup_name(file);
  389.    return(1);
  390. }
  391.  
  392. int sys_findnext(char *file)
  393. {
  394. #ifndef __WATCOMC__
  395.    if (findnext(&Dos_DTA)) return(0);
  396. #else
  397.    if (_dos_findnext (&fileinfo)) return(0);
  398. #endif
  399.    dta_fixup_name(file);
  400.    return(1);
  401. }
  402.  
  403. /* This routine is called from S-Lang inner interpreter.  It serves
  404.    as a poor mans version of an interrupt 9 handler */
  405.  
  406. void i386_check_kbd()
  407. {
  408.    int ch;
  409.    while (kbhit()) 
  410.      {
  411.     if ((ch = sys_getkey()) == Abort_Char) SLang_Error = USER_BREAK;
  412.     ungetkey(&ch);
  413.      }
  414. }
  415.  
  416. char *djgpp_current_time (void)
  417. {
  418.    union REGS rg;
  419.    unsigned int year;
  420.    unsigned char month, day, weekday, hour, minute, sec;
  421.    char days[] = "SunMonTueWedThuFriSat";
  422.    char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  423.    static char the_date[26];
  424.    
  425.    rg.h.ah = 0x2A;
  426. #ifndef __WATCOMC__
  427.    int86(0x21, &rg, &rg);
  428.    year = rg.x.cx & 0xFFFF;
  429. #else
  430.    int386(0x21, &rg, &rg);
  431.    year = rg.x.ecx & 0xFFFF;
  432. #endif    
  433.   
  434.    month = 3 * (rg.h.dh - 1);
  435.    day = rg.h.dl;
  436.    weekday = 3 * rg.h.al;
  437.    
  438.    rg.h.ah = 0x2C;
  439.    
  440. #ifndef __WATCOMC__
  441.    int86(0x21, &rg, &rg);
  442. #else
  443.    int386(0x21, &rg, &rg);
  444. #endif
  445.   
  446.    hour = rg.h.ch;
  447.    minute = rg.h.cl;
  448.    sec = rg.h.dh;
  449.    
  450.    /* we want this form: Thu Apr 14 15:43:39 1994\n  */
  451.    sprintf(the_date, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
  452.        days + weekday, months + month, 
  453.        day, hour, minute, sec, year);
  454.    return the_date;
  455. }
  456.  
  457.        
  458.    
  459.  
  460.  
  461.