home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / STVI369G.ZIP / OS2.C < prev    next >
C/C++ Source or Header  |  1991-10-08  |  8KB  |  420 lines

  1. /* $Header: /nw/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_CCIRCM;
  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 0x47:        /* Home key */
  43.                 stuffin("1G");
  44.                 return -1;
  45.             case 0x4f:        /* End key */
  46.                 stuffin("G");
  47.                 return -1;
  48.             case 0x51:        /* PgDn key */
  49.                 stuffin(mkstr(CTRL('F')));
  50.                 return -1;
  51.             case 0x49:        /* PgUp key */
  52.                 stuffin(mkstr(CTRL('B')));
  53.                 return -1;
  54.             case 0x52:        /* insert key */
  55.                 return K_INSERT;
  56.             case 0x53:        /* delete key */
  57.                 stuffin("x");
  58.                 return -1;
  59.             /*
  60.              * Hard-code some useful function key macros.
  61.              */
  62.             case 0x3b: /* F1 */
  63.                 stuffin(":help\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(":N\n");
  73.                 return -1;
  74.             case 0x56: /* SF3 */
  75.                 stuffin(":N!\n");
  76.                 return -1;
  77.             case 0x3e: /* F4 */
  78.                 stuffin(":e #\n");
  79.                 return -1;
  80.             case 0x57: /* SF4 */
  81.                 stuffin(":e! #\n");
  82.                 return -1;
  83.             case 0x3f: /* F5 */
  84.                 stuffin(":rew\n");
  85.                 return -1;
  86.             case 0x58: /* SF5 */
  87.                 stuffin(":rew!\n");
  88.                 return -1;
  89.             case 0x40: /* F6 */
  90.                 stuffin("]]");
  91.                 return -1;
  92.             case 0x59: /* SF6 */
  93.                 stuffin("[[");
  94.                 return -1;
  95.             case 0x42: /* F8 - Set up global substitute */
  96.                 stuffin(":1,$s/");
  97.                 return -1;
  98.             case 0x43: /* F9 - declare C variable */
  99.                 stuffin("yyp!!cdecl\n");
  100.                 return -1;
  101.             case 0x5C: /* SF9 - explain C declaration */
  102.                 stuffin("yyp^iexplain \033!!cdecl\n");
  103.                 return -1;
  104.             case 0x44: /* F10 - save & quit */
  105.                 stuffin(":x\n");
  106.                 return -1;
  107.             case 0x5D: /* F10 - quit without saving */
  108.                 stuffin(":q!\n");
  109.                 return -1;
  110.             default:
  111.                 break;
  112.             }
  113.             break;
  114.  
  115.         default:
  116.             return c;
  117.         }
  118.     }
  119. }
  120.  
  121. #define    BSIZE    2048
  122. static    char    outbuf[BSIZE];
  123. static    int    bpos = 0;
  124.  
  125. void
  126. flushbuf()
  127. {
  128.     if (bpos != 0)
  129.         write(1, outbuf, bpos);
  130.     bpos = 0;
  131. }
  132.  
  133. /*
  134.  * Macro to output a character. Used within this file for speed.
  135.  */
  136. #define    outone(c)    outbuf[bpos++] = c; if (bpos >= BSIZE) flushbuf()
  137.  
  138. /*
  139.  * Function version for use outside this file.
  140.  */
  141. void
  142. outchar(c)
  143. register char    c;
  144. {
  145.     outbuf[bpos++] = c;
  146.     if (bpos >= BSIZE)
  147.         flushbuf();
  148. }
  149.  
  150. static    char    cell[2] = { 0, 7 };
  151.  
  152. /*
  153.  * outstr(s) - write a string to the console
  154.  *
  155.  * We implement insert/delete line escape sequences here. This is kind
  156.  * of a kludge, but at least it's localized to a single point.
  157.  */
  158. void
  159. outstr(s)
  160. register char    *s;
  161. {
  162.     if (strcmp(s, T_DL) == 0) {        /* delete line */
  163.         int    r, c;
  164.  
  165.         flushbuf();
  166.         VioGetCurPos(&r, &c, 0);
  167.         VioScrollUp(r, 0, 100, 100, 1, cell, 0);
  168.         return;
  169.     }
  170.     if (strcmp(s, T_IL) == 0) {        /* insert line */
  171.         int    r, c;
  172.  
  173.         flushbuf();
  174.         VioGetCurPos(&r, &c, 0);
  175.         VioScrollDn(r, 0, 100, 100, 1, cell, 0);
  176.         return;
  177.     }
  178.  
  179.     while (*s) {
  180.         outone(*s++);
  181.     }
  182. }
  183.  
  184. void
  185. beep()
  186. {
  187. #ifdef VBEEP    /* doesn't seem to work, so just use bell ordinarily */
  188.     if ( P(P_VB) )
  189.         vbeep();
  190.     else
  191. #endif
  192.         outone('\007');
  193. }
  194.  
  195. sleep(n)
  196. int    n;
  197. {
  198.     DosSleep(1000L * n);
  199. }
  200.  
  201. void
  202. pause()
  203. {
  204.     flushbuf();
  205.     DosSleep(300L);
  206. }
  207.  
  208. void
  209. sig()
  210. {
  211.     signal(SIGINT, sig);
  212.  
  213.     got_int = TRUE;
  214. }
  215.  
  216. void
  217. windinit()
  218. {
  219.     Columns = 80;
  220.     P(P_LI) = Rows = 25;
  221.  
  222.     signal(SIGINT, sig);
  223. }
  224.  
  225. void
  226. windexit(r)
  227. int r;
  228. {
  229.     flushbuf();
  230.     exit(r);
  231. }
  232.  
  233. void
  234. windgoto(r, c)
  235. register int    r, c;
  236. {
  237.     r += 1;
  238.     c += 1;
  239.  
  240.     /*
  241.      * Check for overflow once, to save time.
  242.      */
  243.     if (bpos + 8 >= BSIZE)
  244.         flushbuf();
  245.  
  246.     outbuf[bpos++] = '\033';
  247.     outbuf[bpos++] = '[';
  248.     if (r >= 10)
  249.         outbuf[bpos++] = r/10 + '0';
  250.     outbuf[bpos++] = r%10 + '0';
  251.     outbuf[bpos++] = ';';
  252.     if (c >= 10)
  253.         outbuf[bpos++] = c/10 + '0';
  254.     outbuf[bpos++] = c%10 + '0';
  255.     outbuf[bpos++] = 'H';
  256. }
  257.  
  258. FILE *
  259. fopenb(fname, mode)
  260. char    *fname;
  261. char    *mode;
  262. {
  263.     FILE    *fopen();
  264.     char    modestr[16];
  265.  
  266.     sprintf(modestr, "%sb", mode);
  267.     return fopen(fname, modestr);
  268. }
  269.  
  270. #define    PSIZE    128
  271.  
  272. /*
  273.  * fixname(s) - fix up a dos name
  274.  *
  275.  * Takes a name like:
  276.  *
  277.  *    \x\y\z\base.ext
  278.  *
  279.  * and trims 'base' to 8 characters, and 'ext' to 3.
  280.  */
  281. char *
  282. fixname(s)
  283. char    *s;
  284. {
  285.     char    *strchr(), *strrchr();
  286.     static    char    f[PSIZE];
  287.     char    base[32];
  288.     char    ext[32];
  289.     char    *p;
  290.     int    i;
  291.  
  292.     strcpy(f, s);
  293.  
  294.     for (i=0; i < PSIZE ;i++)
  295.         if (f[i] == '/')
  296.             f[i] = '\\';
  297.  
  298.     if (IsFileSystemFAT(f)) {
  299.         /*
  300.          * Split the name into directory, base, extension.
  301.          */
  302.         if ((p = strrchr(f, '\\')) != NULL) {
  303.             strcpy(base, p+1);
  304.             p[1] = '\0';
  305.         } else {
  306.             strcpy(base, f);
  307.             f[0] = '\0';
  308.         }
  309.     
  310.         if ((p = strchr(base, '.')) != NULL) {
  311.             strcpy(ext, p+1);
  312.             *p = '\0';
  313.         } else
  314.             ext[0] = '\0';
  315.     
  316.         /*
  317.          * Trim the base name if necessary.
  318.          */
  319.         if (strlen(base) > 8)
  320.             base[8] = '\0';
  321.         
  322.         if (strlen(ext) > 3)
  323.             ext[3] = '\0';
  324.     
  325.         /*
  326.          * Paste it all back together
  327.          */
  328.         strcat(f, base);
  329.         strcat(f, ".");
  330.         strcat(f, ext);
  331.     }
  332.  
  333.     return f;
  334. }
  335.  
  336. void
  337. doshell(cmd)
  338. char    *cmd;
  339. {
  340.     if (cmd == NULL)
  341.         cmd = "cmd.exe";
  342.  
  343.     system(cmd);
  344.     wait_return();
  345. }
  346.  
  347. /*
  348.  *    FILL IT IN, FOR YOUR SYSTEM, AND SHARE IT!
  349.  *
  350.  *    The next couple of functions do system-specific stuff.
  351.  *    They currently do nothing; I'm not familiar enough with
  352.  *    system-specific programming on this system.
  353.  *    If you fill it in for your system, please post the results
  354.  *    and share with the rest of us.
  355.  */
  356.  
  357.  
  358. setcolor (c)
  359. /*
  360.  * Set the color to c, using the local system convention for numbering
  361.  * colors or video attributes.
  362.  *
  363.  * If you implement this, remember to note the original color in
  364.  * windinit(), before you do any setcolor() commands, and
  365.  * do a setcolor() back to the original as part of windexit().
  366.  */
  367.   int c;
  368. {
  369. }
  370.  
  371.  
  372. setrows (r)
  373. /*
  374.  * Set the number of lines to r, if possible.  Otherwise
  375.  * "do the right thing".  Return the number of lines actually set.
  376.  *
  377.  * If you implement this, remember to note the original number of rows
  378.  * in windinit(), before you do any setrows() commands, and
  379.  * do a setrows() back to the original as part of windexit().
  380.  */
  381.   int r;
  382. {
  383.     /* Since we do nothing, just return the current number of lines */
  384.     return ( P(P_LI) );
  385. }
  386.  
  387.  
  388. vbeep ()
  389. /*
  390.  * Do a "visual bell".  This generally consists of flashing the screen
  391.  * once in inverse video.
  392.  */
  393. {
  394.     int    color, revco;
  395.  
  396.     color = P( P_CO );        /* get current color */
  397.     revco = reverse_color (color);    /* system-specific */
  398.     setcolor (revco);
  399.     flushbuf ();
  400.     pause ();
  401.     setcolor (color);
  402.     windgoto (Cursrow, Curscol);
  403.     flushbuf ();
  404. }
  405.  
  406. reverse_color (co)
  407. /*
  408.  * Returns the inverse video attribute or color of co.
  409.  * The existing code below is VERY simple-minded.
  410.  * Replace it with proper code for your system.
  411.  */
  412.  int co;
  413. {
  414.     if (co)        return (0);
  415.     else        return (1);
  416. }
  417.  
  418.  
  419. /********** End of do-it-yourself kit **********************/
  420.