home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / STVI369G.ZIP / TOS.C < prev    next >
C/C++ Source or Header  |  1990-05-01  |  8KB  |  438 lines

  1. /* $Header: /nw/tony/src/stevie/src/RCS/tos.c,v 1.5 89/07/13 22:45:31 tony Exp $
  2.  *
  3.  * System-dependent routines for the Atari ST.
  4.  */
  5.  
  6. #include "stevie.h"
  7.  
  8. #include <osbind.h>
  9.  
  10. /*
  11.  * inchar() - get a character from the keyboard
  12.  *
  13.  * Certain special keys are mapped to values above 0x80. These
  14.  * mappings are defined in keymap.h. If the key has a non-zero
  15.  * ascii value, it is simply returned. Otherwise it may be a
  16.  * special key we want to map.
  17.  *
  18.  * The ST has a bug involving keyboard input that seems to occur
  19.  * when typing quickly, especially typing capital letters. Sometimes
  20.  * a value of 0x02540000 is read. This doesn't correspond to anything
  21.  * on the keyboard, according to my documentation. My solution is to
  22.  * loop when any unknown key is seen. Normally, the bell is rung to
  23.  * indicate the error. If the "bug" value is seen, we ignore it completely.
  24.  */
  25. int
  26. inchar()
  27. {
  28.     register long    c;
  29.  
  30.     for (;;) {
  31.         c = Bconin(2);
  32.     
  33.         if ((c & 0xff) != 0)
  34.             return ((int) c);
  35.     
  36.         switch ((int) (c >> 16) & 0xff) {
  37.     
  38.         case 0x62: return K_HELP;
  39.         case 0x61: return K_UNDO;
  40.         case 0x52: return K_INSERT;
  41.         case 0x47: return K_HOME;
  42.         case 0x48: return K_UARROW;
  43.         case 0x50: return K_DARROW;
  44.         case 0x4b: return K_LARROW;
  45.         case 0x4d: return K_RARROW;
  46.         case 0x29: return K_CCIRCM;    /* control-circumflex */
  47.         
  48.         /*
  49.          * Occurs due to a bug in TOS.
  50.          */
  51.         case 0x54:
  52.             break;
  53.         /*
  54.          * Add the function keys here later if we put in support
  55.          * for macros.
  56.          */
  57.     
  58.         default:
  59.             beep();
  60.             break;
  61.     
  62.         }
  63.     }
  64. }
  65.  
  66. void
  67. outchar(c)
  68. char    c;
  69. {
  70.     if (c < ' ')
  71.         Bconout(2, c);
  72.     else
  73.         Bconout(5, c);
  74. }
  75.  
  76. void
  77. outstr(s)
  78. register char    *s;
  79. {
  80.     while (*s)
  81.         Bconout(2, *s++);
  82. }
  83.  
  84. /*
  85.  * flushbuf() - a no-op for TOS
  86.  */
  87. void
  88. flushbuf()
  89. {
  90. }
  91.  
  92. #define    BGND    0
  93. #define    TEXT    3
  94.  
  95. /*
  96.  * vbeep() - visual bell
  97.  */
  98. static void
  99. vbeep()
  100. {
  101.     int    text, bgnd;        /* text and background colors */
  102.     long    l;
  103.  
  104.     text = Setcolor(TEXT, -1);
  105.     bgnd = Setcolor(BGND, -1);
  106.  
  107.     Setcolor(TEXT, bgnd);        /* swap colors */
  108.     Setcolor(BGND, text);
  109.  
  110.     for (l=0; l < 5000 ;l++)    /* short pause */
  111.         ;
  112.  
  113.     Setcolor(TEXT, text);        /* restore colors */
  114.     Setcolor(BGND, bgnd);
  115. }
  116.  
  117. void
  118. beep()
  119. {
  120.     if (P(P_VB))
  121.         vbeep();
  122.     else
  123.         outchar('\007');
  124. }
  125.  
  126. /*
  127.  * remove(file) - remove a file
  128.  */
  129. void
  130. remove(file)
  131. char    *file;
  132. {
  133.     Fdelete(file);
  134. }
  135.  
  136. /*
  137.  * rename(of, nf) - rename existing file 'of' to 'nf'
  138.  */
  139. void
  140. rename(of, nf)
  141. char    *of, *nf;
  142. {
  143.     Fdelete(nf);        /* if 'nf' exists, remove it */
  144.     Frename(0, of, nf);
  145. }
  146.  
  147. void
  148. windinit()
  149. {
  150.     if (Getrez() == 0)
  151.         Columns = 40;        /* low resolution */
  152.     else
  153.         Columns = 80;        /* medium or high */
  154.  
  155.     P(P_LI) = Rows = 25;
  156.  
  157.     Cursconf(1,NULL);
  158. }
  159.  
  160. void
  161. windexit(r)
  162. int    r;
  163. {
  164.     exit(r);
  165. }
  166.  
  167. static    char    gobuf[5] = { '\033', 'Y', '\0', '\0', '\0' };
  168.  
  169. void
  170. windgoto(r, c)
  171. int    r, c;
  172. {
  173.     gobuf[2] = r + 040;
  174.     gobuf[3] = c + 040;
  175.     outstr(gobuf);
  176. }
  177.  
  178. /*
  179.  * System calls or library routines missing in TOS.
  180.  */
  181.  
  182. void
  183. sleep(n)
  184. int    n;
  185. {
  186.     int    k;
  187.  
  188.     k = Tgettime();
  189.     while ( Tgettime() <= k+n )
  190.         ;
  191. }
  192.  
  193. void
  194. pause()
  195. {
  196.     long    n;
  197.  
  198.     for (n = 0; n < 8000 ;n++)
  199.         ;
  200. }
  201.  
  202. int
  203. system(cmd)
  204. char    *cmd;
  205. {
  206.     char    arg[1];
  207.  
  208.     arg[0] = (char) 0;    /* no arguments passed to the shell */
  209.  
  210.     if (Pexec(0, cmd, arg, 0L) < 0)
  211.         return -1;
  212.     else
  213.         return 0;
  214. }
  215.  
  216. #ifdef    SOZOBON
  217.  
  218. FILE *
  219. fopenb(fname, mode)
  220. char    *fname;
  221. char    *mode;
  222. {
  223.     char    modestr[10];
  224.  
  225.     sprintf(modestr, "%sb", mode);
  226.  
  227.     return fopen(fname, modestr);
  228. }
  229.  
  230. #endif
  231.  
  232. #ifndef    SOZOBON
  233. /*
  234.  * getenv() - get a string from the environment
  235.  *
  236.  * Both Alcyon and Megamax are missing getenv(). This routine works for
  237.  * both compilers and with the Beckemeyer and Gulam shells. With gulam,
  238.  * the env_style variable should be set to either "mw" or "gu".
  239.  */
  240. char *
  241. getenv(name)
  242. char    *name;
  243. {
  244.     extern long    _base;
  245.     char    *envp, *p;
  246.  
  247.     envp = *((char **) (_base + 0x2c));
  248.  
  249.     for (; *envp ;envp += strlen(envp)+1) {
  250.         if (strncmp(envp, name, strlen(name)) == 0) {
  251.             p = envp + strlen(name);
  252.             if (*p++ == '=')
  253.                 return p;
  254.         }
  255.     }
  256.     return (char *) 0;
  257. }
  258. #endif
  259.  
  260. /*
  261.  * mktemp() - quick hack since there isn't one here
  262.  */
  263. char *
  264. mktemp(name)
  265. char    *name;
  266. {
  267.     int    num;        /* pasted into the string to make it unique */
  268.     char    cbuf[7];
  269.     char    *s;        /* where the X's start in name */
  270.     int    fd;
  271.  
  272.     if ((s = strchr(name, 'X')) == NULL)    /* needs to be an X */
  273.         return (char *) NULL;
  274.  
  275.     if (strlen(s) != 6)            /* should be 6 X's */
  276.         return (char *) NULL;
  277.  
  278.     for (num = 0; num < 1000 ;num++) {
  279.         sprintf(cbuf, "%06d", num);
  280.         strcpy(s, cbuf);
  281.         if ((fd = open(name, 0)) < 0)
  282.             return name;
  283.         close(fd);
  284.     }
  285.     return (char *) NULL;
  286. }
  287.  
  288. void
  289. doshell(cmd)
  290. char    *cmd;
  291. {
  292.     if (cmd == NULL) {
  293.         shell();
  294.         return;
  295.     }
  296.     system(cmd);
  297.     wait_return();
  298. }
  299.  
  300. #define    PSIZE    128
  301.  
  302. /*
  303.  * fixname(s) - fix up a dos name
  304.  *
  305.  * Takes a name like:
  306.  *
  307.  *    \x\y\z\base.ext
  308.  *
  309.  * and trims 'base' to 8 characters, and 'ext' to 3.
  310.  */
  311. char *
  312. fixname(s)
  313. char    *s;
  314. {
  315.     char    *strchr(), *strrchr();
  316.     static    char    f[PSIZE];
  317.     char    base[32];
  318.     char    ext[32];
  319.     char    *p;
  320.     int    i;
  321.  
  322.     strcpy(f, s);
  323.  
  324.     for (i=0; i < PSIZE ;i++)
  325.         if (f[i] == '/')
  326.             f[i] = '\\';
  327.  
  328.     /*
  329.      * Split the name into directory, base, extension.
  330.      */
  331.     if ((p = strrchr(f, '\\')) != NULL) {
  332.         strcpy(base, p+1);
  333.         p[1] = '\0';
  334.     } else {
  335.         strcpy(base, f);
  336.         f[0] = '\0';
  337.     }
  338.  
  339.     if ((p = strchr(base, '.')) != NULL) {
  340.         strcpy(ext, p+1);
  341.         *p = '\0';
  342.     } else
  343.         ext[0] = '\0';
  344.  
  345.     /*
  346.      * Trim the base name if necessary.
  347.      */
  348.     if (strlen(base) > 8)
  349.         base[8] = '\0';
  350.     
  351.     if (strlen(ext) > 3)
  352.         ext[3] = '\0';
  353.  
  354.     /*
  355.      * Paste it all back together
  356.      */
  357.     strcat(f, base);
  358.     strcat(f, ".");
  359.     strcat(f, ext);
  360.  
  361.     return f;
  362. }
  363.  
  364. /*
  365.  *    FILL IT IN, FOR YOUR SYSTEM, AND SHARE IT!
  366.  *
  367.  *    The next couple of functions do system-specific stuff.
  368.  *    They currently do nothing; I'm not familiar enough with
  369.  *    system-specific programming on this system.
  370.  *    If you fill it in for your system, please post the results
  371.  *    and share with the rest of us.
  372.  */
  373.  
  374.  
  375. setcolor (c)
  376. /*
  377.  * Set the color to c, using the local system convention for numbering
  378.  * colors or video attributes.
  379.  *
  380.  * If you implement this, remember to note the original color in
  381.  * windinit(), before you do any setcolor() commands, and
  382.  * do a setcolor() back to the original as part of windexit().
  383.  */
  384.   int c:
  385. {
  386. }
  387.  
  388.  
  389. setrows (r)
  390. /*
  391.  * Set the number of lines to r, if possible.  Otherwise
  392.  * "do the right thing".  Return the number of lines actually set.
  393.  *
  394.  * If you implement this, remember to note the original number of rows
  395.  * in windinit(), before you do any setrows() commands, and
  396.  * do a setrows() back to the original as part of windexit().
  397.  */
  398.   int r;
  399. {
  400.     /* Since we do nothing, just return the current number of lines */
  401.     return ( P(P_LI) );
  402. }
  403.  
  404.  
  405. vbeep ()
  406. /*
  407.  * Do a "visual bell".  This generally consists of flashing the screen
  408.  * once in inverse video.
  409.  */
  410. {
  411.     int    color, revco;
  412.  
  413.     color = P( P_CO );        /* get current color */
  414.     revco = reverse_color (color);    /* system-specific */
  415.     setcolor (revco);
  416.     flushbuf ();
  417.     pause ();
  418.     setcolor (color);
  419.     windgoto (Cursrow, Curscol);
  420.     flushbuf ();
  421. }
  422.  
  423. reverse_color (co)
  424. /*
  425.  * Returns the inverse video attribute or color of co.
  426.  * The existing code below is VERY simple-minded.
  427.  * Replace it with proper code for your system.
  428.  */
  429.  int co;
  430. {
  431.     if (co)        return (0);
  432.     else        return (1);
  433. }
  434.  
  435.  
  436. /********** End of do-it-yourself kit **********************/
  437.  
  438.