home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / IRIT / IRITS.ZIP / DOSINTR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-05  |  8.7 KB  |  280 lines

  1. /******************************************************************************
  2. *   "Irit" - the 3d polygonal solid modeller.                      *
  3. *                                          *
  4. * Written by:  Gershon Elber                 Ver 0.2, Mar. 1990   *
  5. *******************************************************************************
  6. * Procedures to handle the dos interface - print/change the current dir. etc. *
  7. ******************************************************************************/
  8.  
  9. #ifdef __MSDOS__
  10. #include <stdlib.h>
  11. #include <dir.h>
  12. #include <dos.h>
  13. #include <time.h>
  14. #include <float.h>
  15. #include <process.h>
  16. #include <graphics.h>
  17. #else
  18. #include <sys/types.h>
  19. #include <sys/times.h>
  20. #include <sys/param.h>
  21. #ifndef HZ
  22. #define HZ 60
  23. #endif  /* HZ */
  24. #endif /* __MSDOS__ */
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include "program.h"
  31.  
  32. #ifdef __MSDOS__
  33. #include "mousedrv.h"
  34. #endif /* __MSDOS__ */
  35.  
  36. #include <stdio.h>
  37. #include "dosintrl.h"
  38. #include "dosintrg.h"
  39. #include "ctrl-brk.h"
  40.  
  41. #ifndef __MSDOS__
  42. #include "xgraphic.h"
  43. #endif /* __MSDOS__ */
  44.  
  45. #include "graphgng.h"
  46. #include "windowsg.h"
  47.  
  48. /******************************************************************************
  49. * Procedure to print the current directory content in menu area:          *
  50. * Print file name and size in k bytes one per line. if page is full          *
  51. * (> ~20 lines) it pause and continue in next page. No sort is perform.       *
  52. ******************************************************************************/
  53. void DosPrintDir(char *Match)
  54. {
  55. #ifdef __MSDOS__
  56.     int StrCount, Reset = TRUE;
  57.     char s[LINE_LEN];
  58.     struct ffblk ffblk;
  59.  
  60.     getcwd(s, LINE_LEN-1);           /* Might be 64 chars at the most. */
  61.     WndwInputWindowPutStr(s, NO_COLOR);
  62.  
  63.     if (Match == NULL || strlen(Match) == 0) Match = "*.*";
  64.  
  65.     StrCount = 0;     /* Count number of file names written allready into s. */
  66.     s[0] = 0;
  67.     if (!findfirst(Match, &ffblk, FA_DIREC))
  68.     do {
  69.         switch (ffblk.ff_attrib) {
  70.         case FA_DIREC: /* Directory */
  71.             sprintf(&s[strlen(s)], "<DIR> %-13s", ffblk.ff_name);
  72.             break;
  73.         default: /* regular file */
  74.             sprintf(&s[strlen(s)], " %3dk %-13s",
  75.             (int) ((ffblk.ff_fsize+1023)/1024), ffblk.ff_name);
  76.             break;
  77.         }
  78.         if (++StrCount == 4) {            /* Print the string: */
  79.         WndwInputWindowPutStrFS(s, NO_COLOR, Reset);
  80.         Reset = FALSE;
  81.         StrCount = 0;
  82.         s[0] = 0;
  83.         }
  84.     }
  85.     while (!findnext(&ffblk));
  86.  
  87.     if (StrCount > 0)                    /* Print the string: */
  88.     WndwInputWindowPutStrFS(s, NO_COLOR, Reset);
  89. #else
  90.     fprintf(stderr, "Not implemented on this system.\n");
  91. #endif /* __MSDOS__ */
  92. }
  93.  
  94. /*****************************************************************************
  95. *   Routine to change current directory                         *
  96. *****************************************************************************/
  97. void DosChangeDir(char *s)
  98. {
  99. #ifdef __MSDOS__
  100.     char cwd[LINE_LEN], *sptr;
  101.  
  102.     getcwd(cwd, LINE_LEN-1);               /* Save current position. */
  103.  
  104.     if (s[1] == ':') sptr = &s[2];        /* Sptr points on the path only. */
  105.     else sptr = s;
  106.  
  107.     if (strlen(sptr) != 0 && chdir(s))
  108.     WndwInputWindowPutStr("CHDIR: No Such Dir!", RED);
  109.     else if (s[1] == ':') {
  110.     if (islower(s[0])) s[0] = toupper(s[0]);
  111.     setdisk(s[0] - 'A');                /* Move to the new disk. */
  112.     if (getdisk() != s[0] - 'A')
  113.         WndwInputWindowPutStr("CHDIR: No Such Disk!", RED);
  114.  
  115.     }
  116.  
  117.     if (getcwd(s, LINE_LEN-1) == NULL) {      /* Test if directory is valid! */
  118.     WndwInputWindowPutStr("CHDIR: hardware (!?) error - ignored", RED);
  119.     /* Restore old working directory: */
  120.     if (strlen(&cwd[2]) != 0) chdir(cwd); /* If directory is not root... */
  121.     if (islower(cwd[0])) cwd[0] = toupper(cwd[0]);
  122.     setdisk(cwd[0] - 'A');               /* Move to the original disk. */
  123.     }
  124. #else
  125.     fprintf(stderr, "Not implemented on this system.\n");
  126. #endif /* __MSDOS__ */
  127. }
  128.  
  129. /******************************************************************************
  130. * Procedure to return current time from last time, time was reset.          *
  131. * Time is reset if the given parameter is non zero. Time is returned in       *
  132. * seconds. This routine should be called at the beginning of the program to   *
  133. * reset it time.                                  *
  134. * As we return only one parameter, on unix systems - the sum. of the user and *
  135. * system time is returned, which is all the time used for this process.          *
  136. ******************************************************************************/
  137. double DosGetTime(double ResetTime)
  138. {
  139. #ifdef __MSDOS__
  140.     double t;
  141.     static time_t LastTime;
  142.     time_t CurrentTime;
  143.  
  144.     time(&CurrentTime);
  145.  
  146.     t = difftime(CurrentTime, LastTime);
  147.  
  148.     if (!APX_EQ(ResetTime, 0.0)) {              /* Reset the time! */
  149.     time(&LastTime);
  150.     }
  151.  
  152.     return t;
  153. #else
  154.     static double LastTime = 0.0;
  155.     double t, NewTime;
  156.     struct tms TimeBuffer;
  157.  
  158.     times(&TimeBuffer);
  159.  
  160.     NewTime = ((double) TimeBuffer.tms_utime) / HZ +
  161.           ((double) TimeBuffer.tms_stime) / HZ;
  162.  
  163.     t = NewTime - LastTime;
  164.  
  165.     if (!APX_EQ(ResetTime, 0.0)) {              /* Reset the time! */
  166.     LastTime = NewTime;
  167.     }
  168.  
  169.     return t;
  170. #endif /* __MSDOS__ */
  171. }
  172.  
  173. /******************************************************************************
  174. * Procedure to execute the editor as defined by the global variable EditPrgm  *
  175. * as a child process, and wait for its completion.                  *
  176. * The routine release all resources before and reclaim them after.          *
  177. ******************************************************************************/
  178. void DosEditFile(char *Str)
  179. {
  180. #ifdef __MSDOS__
  181.     char *Err = NULL;
  182.  
  183.     DosReleaseResources();
  184.  
  185.     if (spawnl(P_WAIT, EditPrgm, EditPrgm, Str, NULL) == -1) {       /* Do it! */
  186.     /* Error had occured - trace it: */
  187.     switch(errno) {
  188.         case ENOENT:
  189.         Err = "Editor not found in path specified";
  190.         break;
  191.         case ENOMEM:
  192.         Err = "Not enough memory to run editor";
  193.         break;
  194.         default:
  195.         Err = "Undefined error in attempt to run editor";
  196.     }
  197.     }
  198.  
  199.     DosReclaimResources();
  200.  
  201.     if (Err != NULL) WndwInputWindowPutStr(Err, RED);
  202. #else
  203.     fprintf(stderr, "Not implemented on this system.\n");
  204. #endif /* __MSDOS__ */
  205. }
  206.  
  207. /******************************************************************************
  208. * Procedure to execute the command processor as defined by COMSPEC          *
  209. * environment variable and enter it as a child process.                  *
  210. ******************************************************************************/
  211. void DosSystem(void)
  212. {
  213. #ifdef __MSDOS__
  214.     char *FullPath, *Err = NULL;
  215.  
  216.     if ((FullPath = getenv("COMSPEC")) == NULL) {
  217.     WndwInputWindowPutStr("No COMSPEC environment variable, cannt escape to system",
  218.         RED);
  219.     return;
  220.     }
  221.  
  222.     DosReleaseResources();
  223.  
  224.     if (spawnl(P_WAIT, FullPath, FullPath, NULL) == -1) {       /* Do it! */
  225.     /* Error had occured - trace it: */
  226.     switch(errno) {
  227.         case ENOENT:
  228.         Err = "No command processor found in COMSPEC path specified";
  229.         break;
  230.         case ENOMEM:
  231.         Err = "Not enough memory to escape to system";
  232.         break;
  233.         default:
  234.         Err = "Undefined error in attempt to escape to system";
  235.     }
  236.     }
  237.  
  238.     DosReclaimResources();
  239.  
  240.     if (Err != NULL) WndwInputWindowPutStr(Err, RED);
  241. #else
  242.     fprintf(stderr, "Not implemented on this system.\n");
  243. #endif /* __MSDOS__ */
  244. }
  245.  
  246. #ifdef __MSDOS__
  247.  
  248. /******************************************************************************
  249. * Procedure to release all resources used by the program.              *
  250. ******************************************************************************/
  251. static void DosReleaseResources(void)
  252. {
  253.     WndwClaimStatus();        /* Disable status printing on status window. */
  254.     GGCloseGraph();                /* Close the graphic driver. */
  255.     if (MouseExists) MouseClose();        /* Disable mouse interrupts. */
  256.     RestoreCtrlBrk();                  /* Restore ctrl-brk interrupt. */
  257. }
  258.  
  259. /******************************************************************************
  260. * Procedure to reclaim all resources used by the program.              *
  261. ******************************************************************************/
  262. static void DosReclaimResources(void)
  263. {
  264.     GGInitGraph();                  /* Initiate the graphic driver. */
  265.     GGClearAllScreen();
  266.     WndwDrawAllWndwFrames();             /* Draw the windows frames. */
  267.  
  268.     if (MouseExists) MouseInit();
  269.  
  270.     WndwStatusWindowDraw();  /* Update the status window - beginning status. */
  271.     WndwInputWindowPutStr("Back to IRIT...", YELLOW);/* Refresh input window.*/
  272.  
  273.     _fpreset();           /* Reset the floating point package to a known state. */
  274.  
  275.     WndwReclaimStatus();     /* Enable status printing on status window. */
  276.     WndwStatusWindowDraw();    /* Update the status window - current status. */
  277. }
  278.  
  279. #endif /* __MSDOS__ */
  280.