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

  1. /*
  2.  * file.c: hterm file transfer (upload/download)
  3.  *
  4.  * Author: HIRANO Satoshi
  5.  * (C) 1989  Halca Computer Science Laboratory TM
  6.  *           University of Tokyo
  7.  *
  8.  * NOTE:
  9.  *    I don't recomment to use binary upload.
  10.  *
  11.  * 1.1 89/06/20 Halca.Hirano creation
  12.  *    add simple logging
  13.  * 1.2 89/07/20 Halca.Hirano add upload
  14.  *    ---- V2.3.-1 distribution ----
  15.  * 1.3 89/09/25 Halca.Hirano add xmodem/kermit panel
  16.  *    add up load delay
  17.  *      ---- V2.4.0 distribution ----
  18.  * 1.4 89/10/10 Halca.Hirano
  19.  *    move change directory to file.c from setup.c and add drive select
  20.  * 1.5 89/11/24 Halca.Hirano
  21.  *  add critical error handler
  22.  * 1.6 89/12/08 Halca.Hirano
  23.  *    prevent CTRL-C checking by DOS
  24.  * 1.7 89/12/12 Halca.Hirano
  25.  *    move getNextFile() from kermit.c/xmodem.c to file.c
  26.  * 1.8 89/12/12 yamamoto@kyoto-u
  27.  *      suppress control-Z while Text mode uploading
  28.  * 1.9 90/07/07 Halca.Hirano
  29.  *    fix logPut() bug
  30.  * 2.0 90/07/08 Halca.Hirano
  31.  *    fix logPut() again
  32.  * 
  33.  * $Header: file.cv  1.12  90/07/04 00:51:16  hirano  Exp $
  34.  *
  35.  */
  36.  
  37. #include <stdio.h>
  38. #include <ctype.h>
  39. #include <string.h>
  40. #include <io.h>
  41. #include <stdlib.h>
  42. #include "config.h"
  43. #include "hterm.h"
  44. #include "option.h"
  45. #include "default.h"
  46. #include "global.h"
  47.  
  48. static FILE *logFp;
  49. static FILE *upFp;
  50. static int upLoadTimer;
  51.  
  52. void fileInit()
  53. {
  54.     logging = NO;
  55.     upLoading = NO;
  56.     dialStartup = NO;
  57.     redial = NO;
  58.     upLoadType  = UP_CONVERT;
  59.     upLoadDelay = UP_LOAD_TIMER;
  60.     loggingType = LOG_CONVERT;
  61.     strcpy(logFileName, DEFAULT_LOG_FILE);
  62.     strcpy(upFileName, DEFAULT_UP_FILE);
  63.     strcpy(phone, "AT\rATDT0000000\r");
  64.     logFp = upFp = NULL;
  65. }
  66.  
  67. void fileSetup()
  68. {
  69.     /* nothing to do */
  70. }
  71.  
  72. void fileEnd()
  73. {
  74.     logStop();
  75.     upStop();
  76. }
  77.  
  78. /*
  79. ** int setLogging()
  80.  *
  81.  * set logging mode
  82.  * return: 1=logging, NO_LOGGING = not logging
  83.  */
  84. int setLogging()
  85. {
  86.     char buf[MAX_FILE_NAME+20];
  87.     int oldMode = mode;
  88.  
  89.     if ((oldMode == M_SETUP && loggingSave) || (oldMode == M_COMM && logging)) {
  90.           logStop();
  91.         sprintf(buf, "logging off %s", logFileName);
  92.         if (oldMode == M_SETUP)
  93.             putStatus(buf);
  94.         else
  95.             putComment(buf, "");
  96.         mode = oldMode;
  97.         return(logging = loggingSave = NO_LOGGING);
  98.     }
  99.     if (oldMode == M_COMM)
  100.         showPage1();
  101.     mode = M_SETUP;
  102.     logging = loggingSave = NO_LOGGING;
  103.     if (emacs("Log filename: ", logFileName, MAX_FILE_NAME, (E_HELP|E_FILE)) == ERR) {
  104.         strcpy(buf, "Abort");
  105.         goto err;
  106.     }
  107.     if (logStart(logFileName) == ERR) {
  108.         sprintf(buf, msg_cantOpen, logFileName); bell();
  109. err:
  110.         if (oldMode == M_SETUP)
  111.             putStatus(buf);
  112.         else {
  113.             showPage0();
  114.             putComment(buf, "");
  115.         }
  116.         mode = oldMode;
  117.         return(logging = loggingSave = NO_LOGGING);
  118.     }
  119.     loggingSave = loggingType;
  120.     if (oldMode == M_SETUP) {
  121.         putStatus(logFileName);
  122.     } else {
  123.         showPage0();
  124.     }
  125.     mode = oldMode;
  126.     return(1);
  127. }
  128.  
  129. /*
  130. ** int setUpLoad()
  131.  *
  132.  * set up load mode
  133.  * return: 1 upload on, 0 upload off
  134.  */
  135. int setUpLoad()
  136. {
  137.     char buf[MAX_FILE_NAME+20];
  138.     int oldMode = mode;
  139.  
  140.     if ((oldMode == M_SETUP && upLoadSave) || (oldMode == M_COMM && upLoading)) {
  141.         upStop();
  142.         sprintf(buf, msg_close, upFileName);
  143.         if (oldMode == M_SETUP)
  144.             putStatus(buf);
  145.         else
  146.             putComment(buf, "");
  147.         mode = oldMode;
  148.         return(logging = loggingSave = NO_LOGGING);
  149.     }
  150.     if (oldMode == M_COMM)
  151.         showPage1();
  152.     mode = M_SETUP;
  153.     upLoading =  upLoadSave = NO_UPLOAD;
  154.     if (emacs("Upload file name: ", upFileName, MAX_FILE_NAME, (E_HELP|E_FILE)) == ERR) {
  155.         strcpy(buf, "Abort");
  156.         goto err;
  157.     }
  158.     if (upStart(upFileName) == ERR) {
  159.         sprintf(buf, msg_cantOpen, upFileName); bell();
  160. err:
  161.         if (oldMode == M_SETUP) {
  162.             putStatus(buf);
  163.         } else {
  164.             showPage0();
  165.             putComment(buf, "");
  166.         }
  167.         mode = oldMode;
  168.         return(upLoading = upLoadSave = NO_UPLOAD);
  169.     }
  170.     upLoadSave = upLoadType;
  171.     if (oldMode == M_SETUP) {
  172.         putStatus(upFileName);
  173.     } else {
  174.         showPage0();
  175.         putComment("start ", upFileName);
  176.     }
  177.     mode = oldMode;
  178.     return(1);
  179. }
  180.  
  181. int logStart(fileName)
  182. char *fileName;
  183. {
  184.     if ((logFp = fopen(fileName, DEFAULT_LOG_OPEN_MODE)) == NULL)
  185.         return(ERR);
  186.     return(0);
  187. }
  188.  
  189. void logStop()
  190. {
  191.     if (logFp)
  192.         fclose(logFp);
  193.     logFp = NULL;
  194.     logging = NO;
  195. }
  196.  
  197. void logPut(c)
  198. u_short c;
  199. {
  200.     u_short sc;
  201.  
  202.     if (logging && logFp) {
  203.         if (c & 0xff00) {
  204.             sc = JIStoSJIS(c >> 8, c & 0xff);
  205.             putc(sc >> 8, logFp);
  206.             putc(sc & 0xff, logFp);
  207.         } else
  208.             putc(c, logFp);
  209.     }
  210. }
  211.  
  212. int upStart(fileName)
  213. char *fileName;
  214. {
  215.     if ((upFp = fopen(fileName, DEFAULT_UP_OPEN_MODE)) == NULL)
  216.         return(ERR);
  217.     upLoadTimer = upLoadDelay;
  218.     return(0);
  219. }
  220.  
  221. void upStop()
  222. {
  223.     if (upFp)
  224.         fclose(upFp);
  225.     upFp = NULL;
  226.     upLoading = NO;
  227. }
  228.  
  229. short upGet()
  230. /*
  231.  * upGet: get char for upload
  232.  *
  233.  * out: short charactor code
  234.  *        xxyy    JIS kanji
  235.  *        00xx    kana, ascii
  236.  *        -1    EOF
  237.  */
  238. {
  239.     register int c;
  240.     u_char cu;
  241.     
  242.     if (--upLoadTimer > 0)
  243.         return(-2);
  244.     upLoadTimer = upLoadDelay;
  245.     if (upLoading && upFp) {
  246.         if (upLoading == UP_NO_CONVERT)
  247.             return(getc(upFp));
  248. #ifdef MSDOS
  249.         /* else stip LF and ^Z */
  250.         do {
  251.              c = getc(upFp);
  252.         } while (c == '\n' || c == CTRL('Z'));
  253. #endif /* MSDOS */
  254. #ifdef OSK
  255.         c = getc(upFp);
  256. #endif /* OSK */
  257.         if (c == -1)
  258.             return(c);
  259.         cu = (u_char)(c & 0xff);
  260. #ifdef KANJI
  261.         if (isSJIS1(c)) {
  262.             if ((c = getc(upFp)) == EOF)
  263.                 return((short)cu);
  264.             return(SJIStoJIS(cu, (c & 0xff)));
  265.         }
  266. #endif /* KANJI */
  267.         return(cu);
  268.     } else
  269.         return(EOF);
  270. }
  271.  
  272. #if defined(XMODEM) || defined(KERMIT)
  273. void xferPanel(name)
  274. char *name;
  275. {
  276.     sprintf(tmpBuf, "\x1b[1;1fhterm %s\x1b[0K", name);
  277.     conPrint(tmpBuf);
  278.                         /*   27 (0 base ) */
  279.     conPrint("\x1b[3;1f               File name:");
  280.     conPrint("\x1b[4;1f                Protocol:");
  281.     conPrint("\x1b[5;1f  Transferred percentage:");
  282.     conPrint("\x1b[6;1f      KBytes transferred:");
  283.     conPrint("\x1b[7;1f            Packets sent:");
  284.     conPrint("\x1b[8;1f        Packets received:");
  285.     conPrint("\x1b[9;1fTransfer rate(bytes/sec):");
  286.     conPrint("\x1b[10;1f       Number of retries:");
  287.     conPrint("\x1b[11;1f           Packet length:");
  288.     conPrint("\x1b[12;1f                  Status:");
  289.     conPrint("\x1b[13;1f           Error message:");
  290.     conPrint("\x1b[15;1fESC for abort");
  291. }
  292.  
  293. void xprintMsg(to, s, n)
  294. int to;
  295. long n;
  296. char *s;
  297. {
  298.     char    msg2[20], *p;
  299.     int y;
  300.  
  301.     p = msg2;
  302.     switch (to) {
  303.     case X_FILENAME:    y = 2; p = s; break;
  304.     case X_PROTOCOL:    y = 3; p = s; break;
  305.     case X_PERCENT:        y = 4; 
  306.         if (n == -1)
  307.             p = "unavailable";
  308.         else
  309.             sprintf(msg2, "%d %%", n); break;
  310.     case X_KBYTES:        y = 5; goto dispNum;
  311.     case X_PSENT:        y = 6; goto dispNum;
  312.     case X_PRECV:        y = 7; goto dispNum;
  313.     case X_RATE:        y = 8; goto dispNum;
  314.     case X_RETRY:        y = 9; goto dispNum;
  315.     case X_PSIZE:        y = 10; 
  316.     dispNum:
  317.                         sprintf(msg2, "%ld", n); break;
  318.     case X_STATUS:        y = 11; p = s; break;
  319.     case X_ERROR:        y = 12; p = s; break;
  320.     }
  321.     locate(27, y);
  322.     conPrint(p);
  323.     clearColumn(cursorX, MAX_COLUMN);
  324. }
  325. #endif /* defined(XMODEM) || defined(KERMIT) */
  326.  
  327. /*
  328.  * search file 
  329.  *    1) on current directory
  330.  *    2) on HOME
  331.  *    3) on PATH 
  332.  */
  333. char *searchPath(file)
  334. char *file;
  335. {
  336.     static char path[65];        /* MAXPATH + 1            */
  337.     register char *p, *q;
  338.     char buf[300];            /* MAXENV + strlen(";") + alpha    */
  339.     char *getenv(), *strcat();
  340.  
  341.     /*
  342.      * current directory or absolute path
  343.      */
  344.     if (access(file, 0) == 0)
  345.         return(file);
  346.     /*
  347.      * on HOME directory
  348.      */
  349.     if ((p = getenv("HOME")) != NULL) {
  350.         q = path;
  351.         while (*p)
  352.             *q++ = *p++;
  353.         if (q[-1] != PATH_CHAR)
  354.             *q++ = PATH_CHAR;
  355.         *q = 0;
  356.         if (access(strcat(path, file), 0) == 0)
  357.             return(path);
  358.     }
  359.     /*
  360.      * on PATH
  361.      */
  362.     *buf = 0;
  363.     if ((p = getenv("PATH")) == NULL)
  364.         return(NULL);
  365.     sprintf(buf, "%s%c", p, PATH_DELIM);
  366.     p = buf;
  367.     while (*p) {            /* take another directory    */
  368.         q = path;
  369.         while (*p != PATH_DELIM)    /* not end of directory name    */
  370.             *q++ = *p++;
  371.         if (q[-1] != PATH_CHAR)    /* directory not end in '/'    */
  372.             *q++ = PATH_CHAR;    /* append '/'            */
  373.         *q = 0;
  374.         strcat(path, file);
  375.         if (access(path, 0) == 0)    /* file found        */
  376.             return(path);
  377.         p++;            /* skip ;            */
  378.     }
  379.     return NULL;
  380. }
  381.  
  382.  
  383.