home *** CD-ROM | disk | FTP | other *** search
- /*
- * file.c: hterm file transfer (upload/download)
- *
- * Author: HIRANO Satoshi
- * (C) 1989 Halca Computer Science Laboratory TM
- * University of Tokyo
- *
- * NOTE:
- * I don't recomment to use binary upload.
- *
- * 1.1 89/06/20 Halca.Hirano creation
- * add simple logging
- * 1.2 89/07/20 Halca.Hirano add upload
- * ---- V2.3.-1 distribution ----
- * 1.3 89/09/25 Halca.Hirano add xmodem/kermit panel
- * add up load delay
- * ---- V2.4.0 distribution ----
- * 1.4 89/10/10 Halca.Hirano
- * move change directory to file.c from setup.c and add drive select
- * 1.5 89/11/24 Halca.Hirano
- * add critical error handler
- * 1.6 89/12/08 Halca.Hirano
- * prevent CTRL-C checking by DOS
- * 1.7 89/12/12 Halca.Hirano
- * move getNextFile() from kermit.c/xmodem.c to file.c
- * 1.8 89/12/12 yamamoto@kyoto-u
- * suppress control-Z while Text mode uploading
- * 1.9 90/07/07 Halca.Hirano
- * fix logPut() bug
- * 2.0 90/07/08 Halca.Hirano
- * fix logPut() again
- *
- * $Header: file.cv 1.12 90/07/04 00:51:16 hirano Exp $
- *
- */
-
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <io.h>
- #include <stdlib.h>
- #include "config.h"
- #include "hterm.h"
- #include "option.h"
- #include "default.h"
- #include "global.h"
-
- static FILE *logFp;
- static FILE *upFp;
- static int upLoadTimer;
-
- void fileInit()
- {
- logging = NO;
- upLoading = NO;
- dialStartup = NO;
- redial = NO;
- upLoadType = UP_CONVERT;
- upLoadDelay = UP_LOAD_TIMER;
- loggingType = LOG_CONVERT;
- strcpy(logFileName, DEFAULT_LOG_FILE);
- strcpy(upFileName, DEFAULT_UP_FILE);
- strcpy(phone, "AT\rATDT0000000\r");
- logFp = upFp = NULL;
- }
-
- void fileSetup()
- {
- /* nothing to do */
- }
-
- void fileEnd()
- {
- logStop();
- upStop();
- }
-
- /*
- ** int setLogging()
- *
- * set logging mode
- * return: 1=logging, NO_LOGGING = not logging
- */
- int setLogging()
- {
- char buf[MAX_FILE_NAME+20];
- int oldMode = mode;
-
- if ((oldMode == M_SETUP && loggingSave) || (oldMode == M_COMM && logging)) {
- logStop();
- sprintf(buf, "logging off %s", logFileName);
- if (oldMode == M_SETUP)
- putStatus(buf);
- else
- putComment(buf, "");
- mode = oldMode;
- return(logging = loggingSave = NO_LOGGING);
- }
- if (oldMode == M_COMM)
- showPage1();
- mode = M_SETUP;
- logging = loggingSave = NO_LOGGING;
- if (emacs("Log filename: ", logFileName, MAX_FILE_NAME, (E_HELP|E_FILE)) == ERR) {
- strcpy(buf, "Abort");
- goto err;
- }
- if (logStart(logFileName) == ERR) {
- sprintf(buf, msg_cantOpen, logFileName); bell();
- err:
- if (oldMode == M_SETUP)
- putStatus(buf);
- else {
- showPage0();
- putComment(buf, "");
- }
- mode = oldMode;
- return(logging = loggingSave = NO_LOGGING);
- }
- loggingSave = loggingType;
- if (oldMode == M_SETUP) {
- putStatus(logFileName);
- } else {
- showPage0();
- }
- mode = oldMode;
- return(1);
- }
-
- /*
- ** int setUpLoad()
- *
- * set up load mode
- * return: 1 upload on, 0 upload off
- */
- int setUpLoad()
- {
- char buf[MAX_FILE_NAME+20];
- int oldMode = mode;
-
- if ((oldMode == M_SETUP && upLoadSave) || (oldMode == M_COMM && upLoading)) {
- upStop();
- sprintf(buf, msg_close, upFileName);
- if (oldMode == M_SETUP)
- putStatus(buf);
- else
- putComment(buf, "");
- mode = oldMode;
- return(logging = loggingSave = NO_LOGGING);
- }
- if (oldMode == M_COMM)
- showPage1();
- mode = M_SETUP;
- upLoading = upLoadSave = NO_UPLOAD;
- if (emacs("Upload file name: ", upFileName, MAX_FILE_NAME, (E_HELP|E_FILE)) == ERR) {
- strcpy(buf, "Abort");
- goto err;
- }
- if (upStart(upFileName) == ERR) {
- sprintf(buf, msg_cantOpen, upFileName); bell();
- err:
- if (oldMode == M_SETUP) {
- putStatus(buf);
- } else {
- showPage0();
- putComment(buf, "");
- }
- mode = oldMode;
- return(upLoading = upLoadSave = NO_UPLOAD);
- }
- upLoadSave = upLoadType;
- if (oldMode == M_SETUP) {
- putStatus(upFileName);
- } else {
- showPage0();
- putComment("start ", upFileName);
- }
- mode = oldMode;
- return(1);
- }
-
- int logStart(fileName)
- char *fileName;
- {
- if ((logFp = fopen(fileName, DEFAULT_LOG_OPEN_MODE)) == NULL)
- return(ERR);
- return(0);
- }
-
- void logStop()
- {
- if (logFp)
- fclose(logFp);
- logFp = NULL;
- logging = NO;
- }
-
- void logPut(c)
- u_short c;
- {
- u_short sc;
-
- if (logging && logFp) {
- if (c & 0xff00) {
- sc = JIStoSJIS(c >> 8, c & 0xff);
- putc(sc >> 8, logFp);
- putc(sc & 0xff, logFp);
- } else
- putc(c, logFp);
- }
- }
-
- int upStart(fileName)
- char *fileName;
- {
- if ((upFp = fopen(fileName, DEFAULT_UP_OPEN_MODE)) == NULL)
- return(ERR);
- upLoadTimer = upLoadDelay;
- return(0);
- }
-
- void upStop()
- {
- if (upFp)
- fclose(upFp);
- upFp = NULL;
- upLoading = NO;
- }
-
- short upGet()
- /*
- * upGet: get char for upload
- *
- * out: short charactor code
- * xxyy JIS kanji
- * 00xx kana, ascii
- * -1 EOF
- */
- {
- register int c;
- u_char cu;
-
- if (--upLoadTimer > 0)
- return(-2);
- upLoadTimer = upLoadDelay;
- if (upLoading && upFp) {
- if (upLoading == UP_NO_CONVERT)
- return(getc(upFp));
- #ifdef MSDOS
- /* else stip LF and ^Z */
- do {
- c = getc(upFp);
- } while (c == '\n' || c == CTRL('Z'));
- #endif /* MSDOS */
- #ifdef OSK
- c = getc(upFp);
- #endif /* OSK */
- if (c == -1)
- return(c);
- cu = (u_char)(c & 0xff);
- #ifdef KANJI
- if (isSJIS1(c)) {
- if ((c = getc(upFp)) == EOF)
- return((short)cu);
- return(SJIStoJIS(cu, (c & 0xff)));
- }
- #endif /* KANJI */
- return(cu);
- } else
- return(EOF);
- }
-
- #if defined(XMODEM) || defined(KERMIT)
- void xferPanel(name)
- char *name;
- {
- sprintf(tmpBuf, "\x1b[1;1fhterm %s\x1b[0K", name);
- conPrint(tmpBuf);
- /* 27 (0 base ) */
- conPrint("\x1b[3;1f File name:");
- conPrint("\x1b[4;1f Protocol:");
- conPrint("\x1b[5;1f Transferred percentage:");
- conPrint("\x1b[6;1f KBytes transferred:");
- conPrint("\x1b[7;1f Packets sent:");
- conPrint("\x1b[8;1f Packets received:");
- conPrint("\x1b[9;1fTransfer rate(bytes/sec):");
- conPrint("\x1b[10;1f Number of retries:");
- conPrint("\x1b[11;1f Packet length:");
- conPrint("\x1b[12;1f Status:");
- conPrint("\x1b[13;1f Error message:");
- conPrint("\x1b[15;1fESC for abort");
- }
-
- void xprintMsg(to, s, n)
- int to;
- long n;
- char *s;
- {
- char msg2[20], *p;
- int y;
-
- p = msg2;
- switch (to) {
- case X_FILENAME: y = 2; p = s; break;
- case X_PROTOCOL: y = 3; p = s; break;
- case X_PERCENT: y = 4;
- if (n == -1)
- p = "unavailable";
- else
- sprintf(msg2, "%d %%", n); break;
- case X_KBYTES: y = 5; goto dispNum;
- case X_PSENT: y = 6; goto dispNum;
- case X_PRECV: y = 7; goto dispNum;
- case X_RATE: y = 8; goto dispNum;
- case X_RETRY: y = 9; goto dispNum;
- case X_PSIZE: y = 10;
- dispNum:
- sprintf(msg2, "%ld", n); break;
- case X_STATUS: y = 11; p = s; break;
- case X_ERROR: y = 12; p = s; break;
- }
- locate(27, y);
- conPrint(p);
- clearColumn(cursorX, MAX_COLUMN);
- }
- #endif /* defined(XMODEM) || defined(KERMIT) */
-
- /*
- * search file
- * 1) on current directory
- * 2) on HOME
- * 3) on PATH
- */
- char *searchPath(file)
- char *file;
- {
- static char path[65]; /* MAXPATH + 1 */
- register char *p, *q;
- char buf[300]; /* MAXENV + strlen(";") + alpha */
- char *getenv(), *strcat();
-
- /*
- * current directory or absolute path
- */
- if (access(file, 0) == 0)
- return(file);
- /*
- * on HOME directory
- */
- if ((p = getenv("HOME")) != NULL) {
- q = path;
- while (*p)
- *q++ = *p++;
- if (q[-1] != PATH_CHAR)
- *q++ = PATH_CHAR;
- *q = 0;
- if (access(strcat(path, file), 0) == 0)
- return(path);
- }
- /*
- * on PATH
- */
- *buf = 0;
- if ((p = getenv("PATH")) == NULL)
- return(NULL);
- sprintf(buf, "%s%c", p, PATH_DELIM);
- p = buf;
- while (*p) { /* take another directory */
- q = path;
- while (*p != PATH_DELIM) /* not end of directory name */
- *q++ = *p++;
- if (q[-1] != PATH_CHAR) /* directory not end in '/' */
- *q++ = PATH_CHAR; /* append '/' */
- *q = 0;
- strcat(path, file);
- if (access(path, 0) == 0) /* file found */
- return(path);
- p++; /* skip ; */
- }
- return NULL;
- }
-
-
-