xtsubs.c
上传用户:duobangkj
上传日期:2007-01-07
资源大小:70k
文件大小:6k
- /* xtsubs.c -- subroutines for XT
- This file uses 4-character tabstops
- */
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/timeb.h>
- #include <sys/stat.h>
- #include <ctype.h>
- #include <signal.h>
- #include <termio.h>
- #include <setjmp.h>
- #include "xt.h"
- extern jmp_buf erret;
- char line[SM_BUFF], /* Input line */
- word[SM_BUFF], /* Parsed word */
- *wptr, *lptr, /* Word and line pointers */
- *tgetstr(), *tgoto();
- int LI, /* One less than screen length in termcap entry */
- CO; /* Screen width */
- short ospeed;
- static char tc[LG_BUFF], /* termcap buffer */
- tbuf[LG_BUFF], *CD, *CE, *CF, *CL, *CM, *CN, *SO, *SE;
- void show();
- #define Tgetstr(code) ((s = tgetstr(code,&p)) ? s : "")
- time_t
- mtime()
- {
- static struct timeb tb;
- ftime(&tb);
- return 1000 * tb.time + tb.millitm;
- }
- void
- msecs(t)
- time_t t;
- {
- time_t start;
- start = mtime();
- while (mtime()-start < t)
- ;
- }
- /* Do the fork call, packaging the error return so that the caller
- need not have code for it.
- */
- forkem()
- {
- int i;
- if ((i = fork()) < 0){
- S1("XT: Fork failed");
- longjmp(erret,1);
- }
- return i;
- }
- /* Line input routine to be used when the raw terminal mode is in effect. */
- void
- getline()
- {
- int c, i = 0;
- char *ptr;
- lptr = line;
- memset(line, 0, SM_BUFF);
- while ((c = getchar()) != 'r' && c != 'n'){
- if (c == BS){
- if (i > 0){
- ptr = unctrl(line[--i]);
- line[i] = ' ';
- while (*ptr++ != ' ')
- fputs("b b",tfp);
- } else
- beep();
- continue;
- }
- if (c == LK){
- while (i > 0){
- ptr = unctrl(line[--i]);
- while (*ptr++ != ' ')
- fputs("b b",tfp);
- }
- memset(line, 0, SM_BUFF);
- continue;
- }
- if (c == ('v' & 0x1f))
- c = getchar();
- line[i++] = c;
- fputs(unctrl(c), tfp);
- }
- }
- /* Parse the "line" array for a word */
- void
- getword()
- {
- char quote, *ptr = word;
- short carat = FALSE, backslash = FALSE;
- wptr = lptr;
- memset(word, 0, SM_BUFF);
- while (isspace(*lptr))
- lptr++;
- if (*lptr == ' ')
- return;
- if (*lptr == ''' || *lptr == '"' || *lptr == 0x7f)
- quote = *lptr++;
- else
- quote = ' ';
- for (; *lptr != ' '; lptr++){
- if (quote){
- if (*lptr == ' '){
- word[0] = ' ';
- sprintf(Msg,"Unmatched quote: %s", line);
- S;
- s_exit();
- }
- if (*lptr == quote)
- break;
- } else if (!backslash && isspace(*lptr))
- break;
- if (carat)
- *ptr++ = *lptr & 0x1f,
- carat = FALSE;
- else if (backslash)
- *ptr++ = *lptr,
- backslash = FALSE;
- else if (*lptr == '^')
- carat = TRUE;
- else if (*lptr == '\')
- backslash = TRUE;
- else
- *ptr++ = *lptr;
- }
- lptr++;
- }
- /* Make the specified word all lower case */
- void
- lc_word(ptr)
- char *ptr;
- {
- while (*ptr){
- *ptr = tolower(*ptr);
- ptr++;
- }
- }
- /* Make the specified word all upper case */
- void
- uc_word(ptr)
- char *ptr;
- {
- while (*ptr){
- *ptr = toupper(*ptr);
- ptr++;
- }
- }
- void
- mode(flag)
- int flag;
- {
- if (flag == NEWMODE)
- ioctl(0, TCSETAF, &newmode);
- else if (flag == SIGMODE)
- ioctl(0, TCSETAF, &sigmode);
- else if (flag == OLDMODE)
- ioctl(0, TCSETAF, &oldmode);
- }
- beep()
- {
- putc(7,tfp);
- return SUCCESS;
- }
- /* initialize termcap stuff */
- void
- get_ttype()
- {
- char *ttytype;
- char *p = tbuf;
- char *s;
- if (!(ttytype = getenv("TERM"))){
- S1("TERM not set");
- exit(6);
- }
- if (tgetent(tc, ttytype) != 1){
- sprintf(Msg,"Can't load %s", ttytype);
- S;
- exit(7);
- }
- ospeed = newmode.c_cflag & CBAUD;
- if (s = getenv("LINES"))
- LI=atoi(s);
- else
- LI = tgetnum("li");
- if (LI == -1)
- LI = 24;
- LI--;
- if (s = getenv("COLUMNS"))
- CO=atoi(s);
- else
- CO = tgetnum("co");
- if (CO == -1)
- CO = 80;
-
- CD = Tgetstr("cd");
- CE = Tgetstr("ce");
- CL = Tgetstr("cl");
- CM = Tgetstr("cm");
- SE = Tgetstr("se");
- SO = Tgetstr("so");
- CF = Tgetstr("CF");
- CN = Tgetstr("CN");
- if (strlen(CF) && ! strlen(CN))
- CN = Tgetstr("CO");
- }
- /* putchr() is a routine to pass to tputs() */
- void
- putchr(c) int c; { putc(c,tfp); }
- void
- cls() { tputs(CL,LI,putchr); }
- void
- cur_on() { tputs(CN,1,putchr); }
- void
- cur_off() { tputs(CF,1,putchr); }
- void
- cl_line() { tputs(CE,1,putchr); }
- void
- cl_end() { tputs(CD,LI,putchr); }
- void
- ttgoto(row, col) int row, col; { tputs(tgoto(CM, col, row),1,putchr); }
- void
- drawline(row, col, len)
- int row, col, len;
- {
- ttgoto(row, col);
- while (len--)
- fputc('-', tfp);
- }
- void
- show(flag, str)
- short flag;
- char *str;
- {
- if (!flag){
- beep();
- ttgoto(LI,0),
- cl_line(),
- ttgoto(LI,(CO-(int)strlen(str))/2 -1);
- }
- if (flag == 2)
- putc('n',tfp),
- putc('r',tfp);
- tputs(SO,1,putchr);
- putc(' ',tfp);
- fputs(str, tfp);
- putc(' ',tfp);
- tputs(SE,1,putchr);
- if (flag > 0)
- putc('n',tfp),
- putc('r',tfp);
- }
- void
- show_abort() { S2("USER ABORT"); }
- FILE *
- isregfile(pathname)
- char *pathname;
- {
- struct stat statbuf;
- if (stat(pathname,&statbuf) || (statbuf.st_mode & S_IFMT) != S_IFREG)
- return NIL(FILE);
- return fopen(pathname, "r");
- }
- FILE *
- openfile(name)
- char *name;
- {
- FILE *fp = NIL(FILE);
- char *home, fullname[SM_BUFF], *path, *pathend;
- int pathlen;
- if ((path = getenv("XT_PATH"))){
- while (!fp){
- if (!(pathend = strchr(path, ':')))
- pathlen = (int)strlen(path);
- else
- pathlen = pathend - path;
- sprintf(fullname, "%.*s/%s", pathlen, path, name);
- fp = isregfile(fullname);
- path += pathlen;
- if (*path == ' ')
- break;
- path++;
- }
- }
- if (!fp)
- fp = isregfile(name);
-
- if (!fp){
- if ((home = getenv("HOME")))
- sprintf(fullname, "%s/%s", home, name);
- fp = isregfile(fullname);
- }
- if (!fp){
- sprintf(fullname, "%s/%s", LIBDIR, name);
- fp = isregfile(fullname);
- }
- return fp;
- }
- /* Translate the character specified by 'c' to its ASCII display name.
- Note: This routine is specific to the ASCII character set.
- */
- char *
- unctrl(c)
- int c;
- {
- static char buffer[3], buf1[2];
- memset(buffer, 0, 3);
- memset(buf1, 0, 2);
- if (c == 0x7f)
- strcpy(buffer, "^?");
- else {
- if (iscntrl(c))
- strcpy(buffer, "^"),
- c += '@';
- buf1[0] = c;
- strcat(buffer, buf1);
- }
- return buffer;
- }