home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 313_01 / dos.c < prev    next >
C/C++ Source or Header  |  1990-04-22  |  5KB  |  349 lines

  1. /* $Header: /nw2/tony/src/stevie/src/RCS/dos.c,v 1.7 89/07/11 21:22:01 tony Exp $
  2.  *
  3.  * DOS System-dependent routines.
  4.  *
  5.  * System-specific code for MS-DOS. This has been tested with
  6.  * MSDOS 3.3 on an AT. Also, the console driver "nansi.sys" is
  7.  * required.
  8.  */
  9.  
  10. #include "stevie.h"
  11. #include <stdio.h>
  12. #include <dos.h>
  13. #include <signal.h>
  14.  
  15. static    char    getswitch();
  16. static    void    setswitch();
  17.  
  18. /*
  19.  * inchar() - get a character from the keyboard
  20.  */
  21. int
  22. inchar()
  23. {
  24.     int    c;
  25.  
  26.     got_int = FALSE;
  27.  
  28.     for (;;beep()) {    /* loop until we get a valid character */
  29.  
  30.         flushbuf();    /* flush any pending output */
  31.  
  32.         switch (c = getch()) {
  33.         case 0x1e:
  34.             return K_CGRAVE;
  35.         case 0:                /* special key */
  36.             if (State != NORMAL) {
  37.                 c = getch();    /* throw away next char */
  38.                 continue;    /* and loop for another char */
  39.             }
  40.             switch (c = getch()) {
  41.             case 0x50:
  42.                 return K_DARROW;
  43.             case 0x48:
  44.                 return K_UARROW;
  45.             case 0x4b:
  46.                 return K_LARROW;
  47.             case 0x4d:
  48.                 return K_RARROW;
  49.             case 0x52:
  50.                 return K_INSERT;
  51.             case 0x47:
  52.                 stuffin("1G");
  53.                 return -1;
  54.             case 0x4f:
  55.                 stuffin("G");
  56.                 return -1;
  57.             case 0x51:
  58.                 stuffin(mkstr(CTRL('F')));
  59.                 return -1;
  60.             case 0x49:
  61.                 stuffin(mkstr(CTRL('B')));
  62.                 return -1;
  63.             /*
  64.              * Hard-code some useful function key macros.
  65.              */
  66.             case 0x3b: /* F1 */
  67.                 stuffin(":N\n");
  68.                 return -1;
  69.             case 0x54: /* SF1 */
  70.                 stuffin(":N!\n");
  71.                 return -1;
  72.             case 0x3c: /* F2 */
  73.                 stuffin(":n\n");
  74.                 return -1;
  75.             case 0x55: /* SF2 */
  76.                 stuffin(":n!\n");
  77.                 return -1;
  78.             case 0x3d: /* F3 */
  79.                 stuffin(":e #\n");
  80.                 return -1;
  81.             case 0x3e: /* F4 */
  82.                 stuffin(":rew\n");
  83.                 return -1;
  84.             case 0x57: /* SF4 */
  85.                 stuffin(":rew!\n");
  86.                 return -1;
  87.             case 0x3f: /* F5 */
  88.                 stuffin("[[");
  89.                 return -1;
  90.             case 0x40: /* F6 */
  91.                 stuffin("]]");
  92.                 return -1;
  93.             case 0x41: /* F7 - explain C declaration */
  94.                 stuffin("yyp^iexplain \033!!cdecl\n");
  95.                 return -1;
  96.             case 0x42: /* F8 - declare C variable */
  97.                 stuffin("yyp!!cdecl\n");
  98.                 return -1;
  99.             case 0x43: /* F9 */
  100.                 stuffin(":x\n");
  101.                 return -1;
  102.             case 0x44: /* F10 */
  103.                 stuffin(":help\n");
  104.                 return -1;
  105.             default:
  106.                 break;
  107.             }
  108.             break;
  109.  
  110.         default:
  111.             return c;
  112.         }
  113.     }
  114. }
  115.  
  116. #define    BSIZE    2048
  117. static    char    outbuf[BSIZE];
  118. static    int    bpos = 0;
  119.  
  120. void
  121. flushbuf()
  122. {
  123.     if (bpos != 0)
  124.         write(1, outbuf, bpos);
  125.     bpos = 0;
  126. }
  127.  
  128. /*
  129.  * Macro to output a character. Used within this file for speed.
  130.  */
  131. #define    outone(c)    outbuf[bpos++] = c; if (bpos >= BSIZE) flushbuf()
  132.  
  133. /*
  134.  * Function version for use outside this file.
  135.  */
  136. void
  137. outchar(c)
  138. char    c;
  139. {
  140.     outone(c);
  141. }
  142.  
  143. /*
  144.  * outstr(s) - write a string to the console
  145.  */
  146. void
  147. outstr(s)
  148. register char    *s;
  149. {
  150.     while (*s) {
  151.         outone(*s++);
  152.     }
  153. }
  154.  
  155. void
  156. beep()
  157. {
  158.     outone('\007');
  159. }
  160.  
  161. sleep(n)
  162. int    n;
  163. {
  164.     /*
  165.      * Should do something reasonable here.
  166.      */
  167. }
  168.  
  169. void
  170. delay()
  171. {
  172.     long    l;
  173.  
  174.     flushbuf();
  175.     /*
  176.      * Should do something better here...
  177.      */
  178.     for (l=0; l < 5000 ;l++)
  179.         ;
  180. }
  181.  
  182. void
  183. sig()
  184. {
  185.     signal(SIGINT, sig);
  186.  
  187.     got_int = TRUE;
  188. }
  189.  
  190. static    char    schar;        /* save original switch character */
  191.  
  192. void
  193. windinit()
  194. {
  195.     Columns = 80;
  196.     P(P_LI) = Rows = 25;
  197.     schar = getswitch();
  198.     setswitch('/');
  199.  
  200.     signal(SIGINT, sig);
  201. }
  202.  
  203. void
  204. windexit(r)
  205. int r;
  206. {
  207.     flushbuf();
  208.     setswitch(schar);
  209.     exit(r);
  210. }
  211.  
  212. void
  213. windgoto(r, c)
  214. register int    r, c;
  215. {
  216.     r += 1;
  217.     c += 1;
  218.  
  219.     /*
  220.      * Check for overflow once, to save time.
  221.      */
  222.     if (bpos + 8 >= BSIZE)
  223.         flushbuf();
  224.  
  225.     outbuf[bpos++] = '\033';
  226.     outbuf[bpos++] = '[';
  227.     if (r >= 10)
  228.         outbuf[bpos++] = r/10 + '0';
  229.     outbuf[bpos++] = r%10 + '0';
  230.     outbuf[bpos++] = ';';
  231.     if (c >= 10)
  232.         outbuf[bpos++] = c/10 + '0';
  233.     outbuf[bpos++] = c%10 + '0';
  234.     outbuf[bpos++] = 'H';
  235. }
  236.  
  237. FILE *
  238. fopenb(fname, mode)
  239. char    *fname;
  240. char    *mode;
  241. {
  242.     FILE    *fopen();
  243.     char    modestr[16];
  244.  
  245.     sprintf(modestr, "%sb", mode);
  246.     return fopen(fname, modestr);
  247. }
  248.  
  249. static    char
  250. getswitch()
  251. {
  252.     union    REGS    inregs, outregs;
  253.  
  254.     inregs.h.ah = 0x37;
  255.     inregs.h.al = 0;
  256.  
  257.     intdos(&inregs, &outregs);
  258.  
  259.     return outregs.h.dl;
  260. }
  261.  
  262. static    void
  263. setswitch(c)
  264. char    c;
  265. {
  266.     union    REGS    inregs, outregs;
  267.  
  268.     inregs.h.ah = 0x37;
  269.     inregs.h.al = 1;
  270.     inregs.h.dl = c;
  271.  
  272.     intdos(&inregs, &outregs);
  273. }
  274.  
  275. #define    PSIZE    128
  276.  
  277. /*
  278.  * fixname(s) - fix up a dos name
  279.  *
  280.  * Takes a name like:
  281.  *
  282.  *    \x\y\z\base.ext
  283.  *
  284.  * and trims 'base' to 8 characters, and 'ext' to 3.
  285.  */
  286. char *
  287. fixname(s)
  288. char    *s;
  289. {
  290.     char    *strchr(), *strrchr();
  291.     static    char    f[PSIZE];
  292.     char    base[32];
  293.     char    ext[32];
  294.     char    *p;
  295.     int    i;
  296.  
  297.     strcpy(f, s);
  298.  
  299.     for (i=0; i < PSIZE ;i++)
  300.         if (f[i] == '/')
  301.             f[i] = '\\';
  302.  
  303.     /*
  304.      * Split the name into directory, base, extension.
  305.      */
  306.     if ((p = strrchr(f, '\\')) != NULL) {
  307.         strcpy(base, p+1);
  308.         p[1] = '\0';
  309.     } else {
  310.         strcpy(base, f);
  311.         f[0] = '\0';
  312.     }
  313.  
  314.     if ((p = strchr(base, '.')) != NULL) {
  315.         strcpy(ext, p+1);
  316.         *p = '\0';
  317.     } else
  318.         ext[0] = '\0';
  319.  
  320.     /*
  321.      * Trim the base name if necessary.
  322.      */
  323.     if (strlen(base) > 8)
  324.         base[8] = '\0';
  325.     
  326.     if (strlen(ext) > 3)
  327.         ext[3] = '\0';
  328.  
  329.     /*
  330.      * Paste it all back together
  331.      */
  332.     strcat(f, base);
  333.     strcat(f, ".");
  334.     strcat(f, ext);
  335.  
  336.     return f;
  337. }
  338.  
  339. void
  340. doshell(cmd)
  341. char    *cmd;
  342. {
  343.     if (cmd == NULL)
  344.         cmd = "command.com";
  345.  
  346.     system(cmd);
  347.     wait_return();
  348. }
  349.