home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / EDITOR / STEVI69S.ZIP / OS2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-23  |  7.4 KB  |  419 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 0x52:
  43.                 return K_INSERT;
  44.             case 0x47:        /* Home key */
  45.                 stuffin("1G");
  46.                 return -1;
  47.             case 0x4f:        /* End key */
  48.                 stuffin("G");
  49.                 return -1;
  50.             case 0x51:        /* PgDn key */
  51.                 stuffin(mkstr(CTRL('F')));
  52.                 return -1;
  53.             case 0x49:        /* PgUp key */
  54.                 stuffin(mkstr(CTRL('B')));
  55.                 return -1;
  56.             case 0x52:        /* insert key */
  57.                 return K_INSERT;
  58.             case 0x53:        /* delete key */
  59.                 stuffin("x");
  60.                 return -1;
  61.             /*
  62.              * Hard-code some useful function key macros.
  63.              */
  64.             case 0x3b: /* F1 */
  65.                 stuffin(":help\n");
  66.                 return -1;
  67.             case 0x3c: /* F2 */
  68.                 stuffin(":n\n");
  69.                 return -1;
  70.             case 0x55: /* SF2 */
  71.                 stuffin(":n!\n");
  72.                 return -1;
  73.             case 0x3d: /* F3 */
  74.                 stuffin(":N\n");
  75.                 return -1;
  76.             case 0x56: /* SF3 */
  77.                 stuffin(":N!\n");
  78.                 return -1;
  79.             case 0x3e: /* F4 */
  80.                 stuffin(":e #\n");
  81.                 return -1;
  82.             case 0x57: /* SF4 */
  83.                 stuffin(":e! #\n");
  84.                 return -1;
  85.             case 0x3f: /* F5 */
  86.                 stuffin(":rew\n");
  87.                 return -1;
  88.             case 0x58: /* SF5 */
  89.                 stuffin(":rew!\n");
  90.                 return -1;
  91.             case 0x40: /* F6 */
  92.                 stuffin("]]");
  93.                 return -1;
  94.             case 0x59: /* SF6 */
  95.                 stuffin("[[");
  96.                 return -1;
  97.             case 0x42: /* F8 - Set up global substitute */
  98.                 stuffin(":1,$s/");
  99.                 return -1;
  100.             case 0x43: /* F9 - declare C variable */
  101.                 stuffin("yyp!!cdecl\n");
  102.                 return -1;
  103.             case 0x5C: /* SF9 - explain C declaration */
  104.                 stuffin("yyp^iexplain \033!!cdecl\n");
  105.                 return -1;
  106.             case 0x44: /* F10 - save & quit */
  107.                 stuffin(":x\n");
  108.                 return -1;
  109.             case 0x5D: /* F10 - quit without saving */
  110.                 stuffin(":q!\n");
  111.                 return -1;
  112.             default:
  113.                 break;
  114.             }
  115.             break;
  116.  
  117.         default:
  118.             return c;
  119.         }
  120.     }
  121. }
  122.  
  123. #define    BSIZE    2048
  124. static    char    outbuf[BSIZE];
  125. static    int    bpos = 0;
  126.  
  127. void
  128. flushbuf()
  129. {
  130.     if (bpos != 0)
  131.         write(1, outbuf, bpos);
  132.     bpos = 0;
  133. }
  134.  
  135. /*
  136.  * Macro to output a character. Used within this file for speed.
  137.  */
  138. #define    outone(c)    outbuf[bpos++] = c; if (bpos >= BSIZE) flushbuf()
  139.  
  140. /*
  141.  * Function version for use outside this file.
  142.  */
  143. void
  144. outchar(c)
  145. register char    c;
  146. {
  147.     outbuf[bpos++] = c;
  148.     if (bpos >= BSIZE)
  149.         flushbuf();
  150. }
  151.  
  152. static    char    cell[2] = { 0, 7 };
  153.  
  154. /*
  155.  * outstr(s) - write a string to the console
  156.  *
  157.  * We implement insert/delete line escape sequences here. This is kind
  158.  * of a kludge, but at least it's localized to a single point.
  159.  */
  160. void
  161. outstr(s)
  162. register char    *s;
  163. {
  164.     if (strcmp(s, T_DL) == 0) {        /* delete line */
  165.         int    r, c;
  166.  
  167.         flushbuf();
  168.         VioGetCurPos(&r, &c, 0);
  169.         VioScrollUp(r, 0, 100, 100, 1, cell, 0);
  170.         return;
  171.     }
  172.     if (strcmp(s, T_IL) == 0) {        /* insert line */
  173.         int    r, c;
  174.  
  175.         flushbuf();
  176.         VioGetCurPos(&r, &c, 0);
  177.         VioScrollDn(r, 0, 100, 100, 1, cell, 0);
  178.         return;
  179.     }
  180.  
  181.     while (*s) {
  182.         outone(*s++);
  183.     }
  184. }
  185.  
  186. void
  187. beep()
  188. {
  189.     in ( P(P_VB) )
  190.         vbeep();
  191.     else
  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.     /*
  299.      * Split the name into directory, base, extension.
  300.      */
  301.     if ((p = strrchr(f, '\\')) != NULL) {
  302.         strcpy(base, p+1);
  303.         p[1] = '\0';
  304.     } else {
  305.         strcpy(base, f);
  306.         f[0] = '\0';
  307.     }
  308.  
  309.     if ((p = strchr(base, '.')) != NULL) {
  310.         strcpy(ext, p+1);
  311.         *p = '\0';
  312.     } else
  313.         ext[0] = '\0';
  314.  
  315.     /*
  316.      * Trim the base name if necessary.
  317.      */
  318.     if (strlen(base) > 8)
  319.         base[8] = '\0';
  320.     
  321.     if (strlen(ext) > 3)
  322.         ext[3] = '\0';
  323.  
  324.     /*
  325.      * Paste it all back together
  326.      */
  327.     strcat(f, base);
  328.     strcat(f, ".");
  329.     strcat(f, ext);
  330.  
  331.     return f;
  332. }
  333.  
  334. void
  335. doshell(cmd)
  336. char    *cmd;
  337. {
  338.     if (cmd == NULL)
  339.         cmd = "cmd.exe";
  340.  
  341.     system(cmd);
  342.     wait_return();
  343. }
  344.  
  345. /*
  346.  *    FILL IT IN, FOR YOUR SYSTEM, AND SHARE IT!
  347.  *
  348.  *    The next couple of functions do system-specific stuff.
  349.  *    They currently do nothing; I'm not familiar enough with
  350.  *    system-specific programming on this system.
  351.  *    If you fill it in for your system, please post the results
  352.  *    and share with the rest of us.
  353.  */
  354.  
  355.  
  356. setcolor (c)
  357. /*
  358.  * Set the color to c, using the local system convention for numbering
  359.  * colors or video attributes.
  360.  *
  361.  * If you implement this, remember to note the original color in
  362.  * windinit(), before you do any setcolor() commands, and
  363.  * do a setcolor() back to the original as part of windexit().
  364.  */
  365.   int c:
  366. {
  367. }
  368.  
  369.  
  370. setrows (r)
  371. /*
  372.  * Set the number of lines to r, if possible.  Otherwise
  373.  * "do the right thing".  Return the number of lines actually set.
  374.  *
  375.  * If you implement this, remember to note the original number of rows
  376.  * in windinit(), before you do any setrows() commands, and
  377.  * do a setrows() back to the original as part of windexit().
  378.  */
  379.   int r;
  380. {
  381.     /* Since we do nothing, just return the current number of lines */
  382.     return ( P(P_LI) );
  383. }
  384.  
  385.  
  386. vbeep ()
  387. /*
  388.  * Do a "visual bell".  This generally consists of flashing the screen
  389.  * once in inverse video.
  390.  */
  391. {
  392.     int    color, revco;
  393.  
  394.     color = P( P_CO );        /* get current color */
  395.     revco = reverse_color (color);    /* system-specific */
  396.     setcolor (revco);
  397.     flushbuf ();
  398.     pause ();
  399.     setcolor (color);
  400.     windgoto (Cursrow, Curscol);
  401.     flushbuf ();
  402. }
  403.  
  404. reverse_color (co)
  405. /*
  406.  * Returns the inverse video attribute or color of co.
  407.  * The existing code below is VERY simple-minded.
  408.  * Replace it with proper code for your system.
  409.  */
  410.  int co;
  411. {
  412.     if (co)        return (0);
  413.     else        return (1);
  414. }
  415.  
  416.  
  417. /********** End of do-it-yourself kit **********************/
  418.  
  419.