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

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