home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / COMM / MISC / SRC26_2.ZIP / SRC / MSDOS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-04  |  6.7 KB  |  294 lines

  1. /*
  2.  * msdos.c: hterm msdos dependent part
  3.  *
  4.  * Author: HIRANO Satoshi
  5.  *
  6.  * (C) 1986 Halca Computer Science Laboratory UEC  TM
  7.  *          University of Electro Communications
  8.  *          University of Tokyo
  9.  *
  10.  * 
  11.  * Edition history:
  12.  * 1.1 90/02/04 Halca.Hirano creation
  13.  * 1.2 90/06/17 Halca.Hirano change directory conversion way in getNextFileName()
  14.  *    old:    a:dir -> a:/dir
  15.  *    new:    a:dir -> a:dir    (This is correct on MSDOS.)
  16.  *
  17.  * $Header: msdos.cv  1.3  90/07/04 00:31:20  hirano  Exp $
  18.  */
  19.  
  20. #ifdef MSDOS
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #ifdef __TURBOC__
  25. #include    <dir.h>
  26. #endif
  27. #if !defined(__TURBOC__) || __TURBOC__ >= 0x0200
  28. #include    <sys/types.h>
  29. #endif
  30. #include    <sys/stat.h>
  31. #include "config.h"
  32. #include "hterm.h"
  33. #include "option.h"
  34. #include "default.h"
  35. #include "global.h"
  36.  
  37. #ifdef COMP_MSC
  38. static struct find_t    fileNext;   /* wild card             */
  39. #define fn_name    name
  40. #define fn_attr attrib
  41. #endif
  42. #ifdef COMP_TURBOC
  43. static struct ffblk fileNext;
  44. #define    _A_NORMAL        0
  45. #define _A_SUBDIR       0x10
  46. #define fn_name    ff_name
  47. #define fn_attr ff_attrib
  48. #endif
  49. #define skipSpace(p) {while(*(p)==' '||*(p)=='\t')(p)++;}
  50. #define skipText(p) {for (;*(p)&&*(p)!=' '&&*(p)!='\t'&&*(p)!='\n';(p)++);}
  51.  
  52. static void (INTERRUPT FAR *origCritHandler)() = 0;    /* original error handler    */
  53. static u_char ctrlCCheck;
  54.  
  55. void FAR INTERRUPT critHandler(unsigned int es,unsigned int ds,unsigned int di,unsigned int si,unsigned int bp,unsigned int sp,unsigned int bx,unsigned int dx,unsigned int cx,unsigned int ax);
  56.  
  57. void FAR *allocMem(size)
  58. /*
  59.  * allocate memory, if we couldn't get enough memory return 0
  60.  */
  61. long size;
  62. {
  63.     unsigned int seg;
  64.     void FAR *ptr;
  65.  
  66.     if (ALLOCMEM((unsigned int)((size/16L)+1), &seg) != ALLOCOK) {
  67.         return((void FAR *)NULL);
  68.     }
  69.     FP_SEG(ptr) = seg; FP_OFF(ptr) = 0;
  70.     return(ptr);
  71. }
  72.  
  73. void freeMem(ptr)
  74. void FAR *ptr;
  75. {
  76.     if (ptr)
  77.         FREEMEM(FP_SEG(ptr));
  78. }    
  79.  
  80. void moveData(to, from, size)
  81. void FAR *to;
  82. void FAR *from;
  83. short size;
  84. {
  85.     MOVEDATA(FP_SEG(from), FP_OFF(from), FP_SEG(to), FP_OFF(to), size);
  86. }    
  87.  
  88. int getNextFileName(first, fileList, fileName, dirFlag)
  89. /*
  90.  *  Get next file in a 'file list'
  91.  *
  92.  * fileList should be like 'abc*.* a:def.x c:/*.* e:\slkj\lkj'
  93.  *
  94.  * return NO if not found
  95.  * return YES if found
  96.  */
  97. int first;                    /* first or not flag                    */
  98. char **fileList;            /* pointer to file name list pointer    */
  99. char **fileName;            /* pointer to got file name pointer        */
  100. int dirFlag;                /* find sub directory also or not        */
  101. {
  102.     char *p, *q, *r, buf2[MAX_FILE_NAME];
  103.     static char myDirName[MAX_FILE_NAME] = "";
  104.     static char myFileName[MAX_FILE_NAME] = "";
  105.  
  106. redo:
  107.     if (first || FINDNEXT(&fileNext) != 0) {
  108.         /*
  109.          * first or file not found
  110.          *
  111.          * separate current file and prepare next file
  112.          */
  113.         skipSpace(*fileList);
  114.         q = p = *fileList;                /* q = p = first char    */
  115.         if (*p == '\0' || *p == '\n')    /* end of files     */
  116.             return(NO);
  117.         skipText(q);
  118.         r = q;                            /* r = q = last char + 1    */
  119.         skipSpace(q);                    /* q = next file             */
  120.         *r = '\0';                        /* terminate file name        */
  121.         if (*q != '\0' && *q != '\n')    /* if next file exists        */
  122.             *fileList = q;                /* update next file pointer    */
  123.         else
  124.             *fileList = r;                /* next file is null        */
  125.  
  126.         /*
  127.          * convert \ char to / char
  128.          */
  129.         toSlash(p);
  130.  
  131.         /*
  132.          * get directory part
  133.          */
  134.         strcpy(buf2, p);
  135.         p = buf2;
  136.         q = strrchr(p, (int)'/');
  137.         if (q) {                        /* with directory         */
  138.             strncpy(myDirName, p, q-p+1); 
  139.             myDirName[q-p+1] = '\0';
  140.         } else
  141.             myDirName[0] = '\0';
  142.  
  143.         /*
  144.          * OK, find first entry
  145.          */
  146.         if (FINDFIRST(p, (_A_NORMAL|(dirFlag ? _A_SUBDIR : 0)), &fileNext)) {
  147.             return(NO);        /* not found    */
  148.         }
  149.     }
  150.     /*
  151.      * found something
  152.      */
  153.     if (fileNext.fn_name[0] == '.') {
  154.         first = NO;
  155.         goto redo;                /* skip current or parent dir    */
  156.     }
  157.     sprintf(myFileName, "%s%s", myDirName, fileNext.fn_name);
  158.     if (fileNext.fn_attr & _A_SUBDIR)
  159.         strcat(myFileName, "/");
  160.     *fileName = myFileName;
  161.     return(YES);
  162. }
  163.  
  164. void changeDirectory()
  165. {
  166.     char drive = (char)0;
  167.     char *p, *q;
  168. #ifdef COMP_MSC
  169.     int junk = 0;
  170. #endif
  171.     char localFile[MAX_FILE_NAME];
  172.     char *err = "No such directory or drive";
  173.     int oldMode;
  174.  
  175.     if (mode == M_COMM)
  176.         showPage1();
  177.     oldMode = mode;
  178.     mode = M_SETUP;
  179.     getcwd(localFile, MAX_FILE_NAME);
  180.     toSlash(localFile);
  181.     if (localFile[1] == ':')    /* remember current drive    */
  182.         drive = localFile[0];
  183.     if (emacs("Change directory: ", localFile, MAX_FILE_NAME, (E_HELP|E_FILE)) == ERR 
  184.             || localFile[0] == '\0') {
  185.         if (oldMode == M_COMM)
  186.             showPage0();
  187.         mode = oldMode;
  188.         return;
  189.     }
  190.     /*
  191.      * change drive if desired
  192.      */
  193.     q = p = localFile;
  194.     if (*(p+1) == ':') {
  195.         q = &localFile[2];
  196.         if (drive && *p != drive) {
  197. #ifdef COMP_MSC
  198.             _dos_setdrive(toupper(*p) - 'A' + 1, &junk);
  199.             if (junk == 0) {
  200. #endif /* COMP_MSC */
  201. #ifdef COMP_TURBOC
  202.             if (setdisk(toupper(*p) - 'A' + 1/* by manual */) == ERR) {
  203. #endif /* COMP_TURBOC */
  204.                 /*
  205.                  * CAUTION! setdisk() does not return error code
  206.                  *     even if the drive is not ready.
  207.                  *    I hate this.
  208.                  *    Thus following message will not be used.
  209.                  */
  210.                 goto error;
  211.             }
  212.         }
  213.     }
  214.     /*
  215.      * change directory
  216.      */
  217.     if (*q && strlen(q) != 1) {        /* if not root dir    */
  218.         p = q + strlen(q)-1;
  219.         if (*p == '/' || *p == '\\')
  220.             *p = '\0';                /* strip extra '/'     */
  221.     }
  222.     if ((*q && chdir(q) == ERR) || getcwd(localFile, MAX_FILE_NAME) == NULL) {
  223. error:        
  224.         if (oldMode == M_SETUP)
  225.             putStatus(err);
  226.         else {
  227.             showPage0();
  228.             putComment(err, "");
  229.         }
  230.         bell();
  231.         mode = oldMode;
  232.         return;
  233.     }
  234.     toSlash(localFile);
  235.     if (oldMode == M_SETUP)
  236.         putStatus(localFile);
  237.     else {
  238.         showPage0();
  239.         putComment(localFile, "");
  240.     }
  241.     mode = oldMode;
  242. }
  243.  
  244. /*
  245. ** void interrupt far critHandler(es, ds, di, si, bp, sp, bx, dx, cx, ax)
  246.  * critical error handler
  247.  */
  248. void interrupt far critHandler(es, ds, di, si, bp, sp, bx, dx, cx, ax)
  249. unsigned es, ds, di, si, bp, sp, bx, dx, cx, ax;
  250. {
  251.     ax &= 0xff00;
  252.     ax |= 0x3;        /* system call failure    */
  253. }
  254.  
  255. void setCritHandler()
  256. /*
  257.  * set critical error handler        
  258.  */
  259. {
  260.     origCritHandler = GET_DOSVECT(0x24);
  261.     SET_DOSVECT(0x24, critHandler);
  262.     /*
  263.      * get current CTRL-C check status
  264.      */
  265.     rg.h.ah = 0x33;
  266.     rg.h.al = 0;            /* get code */
  267.     int86(0x21, &rg, &rg);
  268.     ctrlCCheck = rg.h.dl;
  269.     /*
  270.      * set new CTRL-C check status
  271.      */
  272.     rg.h.ah = 0x33;
  273.     rg.h.al = 1;            /* get code */
  274.     rg.h.dl = 0;            /* no CTRL-C check please    */
  275.     int86(0x21, &rg, &rg);
  276. }
  277.  
  278. void restoreCritHandler()
  279. {
  280.     if (origCritHandler)
  281.         SET_DOSVECT(0x24, origCritHandler);
  282.     origCritHandler = NULL;
  283.     /*
  284.      * set old CTRL-C check status
  285.      */
  286.     rg.h.ah = 0x33;
  287.     rg.h.al = 1;            /* get code */
  288.     rg.h.dl = ctrlCCheck;
  289.     int86(0x21, &rg, &rg);
  290. }
  291.  
  292.  
  293. #endif /* MSDOS */
  294.