home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3594 / xcsubs.c < prev   
Encoding:
C/C++ Source or Header  |  1991-07-11  |  6.8 KB  |  406 lines

  1. /*    xcsubs.c -- subroutines for XC
  2.     This file uses 4-character tabstops
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/times.h>
  8. #include <sys/param.h>
  9. #include <sys/stat.h>
  10. #include <ctype.h>
  11. #include <signal.h>
  12. #include <termio.h>
  13. #ifdef T6000
  14. #include <sys/ioctl.h>
  15. #endif
  16. #include <setjmp.h>
  17. #include "xc.h"
  18.  
  19. extern jmp_buf erret;
  20.  
  21. char line[SM_BUFF],    /* Input line */
  22.      word[SM_BUFF],    /* Parsed word */
  23.      *wptr, *lptr,    /* Word and line pointers */
  24.      *tgetstr(),
  25.      *tgoto();
  26.  
  27. int    LI,    /* One less than screen length in termcap entry */
  28.     CO;    /* Screen width */
  29. short ospeed;
  30.  
  31. static char tc[LG_BUFF],    /* termcap buffer */
  32.             tbuf[LG_BUFF], PC, *CD, *CE, *CF, *CL, *CM, *CN, *SO, *SE;
  33.  
  34. void show();
  35.  
  36. #define Tgetstr(code) ((s = tgetstr(code,&p)) ? s : "")
  37.  
  38. #if    !STRSTR        /* For those that do not have strstr() */
  39. /*    Find first occurence of str2 in str1 and return a pointer to it */
  40. char *strstr(str1, str2)
  41. char *str1, *str2;
  42. {
  43.     register char *Sptr, *Tptr;
  44.     int len = strlen(str1) -strlen(str2) + 1;
  45.  
  46.     if (*str2)
  47.         for (; len > 0; len--, str1++) {
  48.             if (*str1 != *str2)
  49.                 continue;
  50.  
  51.             for (Sptr = str1, Tptr = str2; *Tptr != '\0'; Sptr++, Tptr++)
  52.                 if (*Sptr != *Tptr)
  53.                     break;
  54.  
  55.             if (*Tptr == '\0')
  56.                 return str1;
  57.         }
  58.  
  59.     return NULLS;
  60. }
  61. #endif
  62.  
  63. #if !DUP2        /* For those that do not have dup2() */
  64. #include <fcntl.h>
  65. dup2(oldfd, newfd)
  66. int oldfd, newfd;
  67. {
  68. if (fcntl(oldfd, F_GETFL, 0) == -1)            /* Valid file descriptor? */
  69.         return (-1);                        /* No, return an error */
  70.     close(newfd);                            /* Ensure newfd is closed */
  71.     return (fcntl(oldfd, F_DUPFD, newfd));    /* Dup oldfd into newfd */
  72. }
  73. #endif /* !DUP2    Thanks to Bill Allie CIS: 76703,2061 */
  74.  
  75. #if !STRDUP    /* For those that do not have strdup() */
  76. char *strdup(s)
  77. char *s;
  78. {
  79.     return strcpy((char *)malloc(strlen(s)+1), s);
  80. }
  81. #endif
  82.  
  83. #if !MEMSET        /* For those that do not have memset() */
  84. char *memset(dst, chr, len)
  85. char *dst;
  86. register chr;
  87. register len;
  88. {
  89.     char *d;
  90.     for (d = dst; --len >= 0; *d++ = chr)
  91.         ;
  92.     return dst;
  93. }
  94. #endif
  95.  
  96. void msecs(t)
  97. long t;
  98. {
  99.     long start;
  100.     struct tms Tbuf;
  101.  
  102.     start = times(&Tbuf);
  103.     while ((times(&Tbuf)-start) < (t*HZ)/1000)
  104.         ;
  105. }
  106.  
  107. /*    Do the fork call, packaging the error return so that the caller
  108.     need not have code for it.
  109. */
  110. forkem()
  111. {
  112.     int i;
  113.  
  114.     if ((i = fork()) < 0) {
  115.         show(1,"XC: Fork failed");
  116.         longjmp(erret,1);
  117.     }
  118.     return(i);
  119. }
  120.  
  121. /*    Throw away all input characters until no more are sent. */
  122. void purge()
  123. {
  124.     while (readbyte(1) != -1)
  125.         ;
  126. }
  127.  
  128. /*    Line input routine to be used when the raw terminal mode is in effect. */
  129. void getline()
  130. {
  131.     int c, i = 0;
  132.  
  133.     lptr = line;
  134.     memset(line, 0, SM_BUFF);
  135.  
  136.     while ((c = trminp()) != '\r' && c != '\n') {
  137.         if (c == '\b') {
  138.             if (i > 0) {
  139.                 line[--i] = '\0';
  140.                 fprintf(tfp,"\b \b");
  141.             } else
  142.                 beep();
  143.             continue;
  144.         }
  145.         line[i++] = c;
  146.         fputc(c, tfp);
  147.     }
  148. }
  149.  
  150. /*    Parse the "line" array for a word */
  151. void getword()
  152. {
  153.     char quote, *ptr = word;
  154.     short carat = FALSE, backslash = FALSE;
  155.  
  156.     wptr = lptr;
  157.     memset(word, 0, SM_BUFF);
  158.  
  159.     while (isspace(*lptr))
  160.         lptr++;
  161.  
  162.     if (*lptr == '\0')
  163.         return;
  164.  
  165.     if (*lptr == '\'' || *lptr == '\"')
  166.         quote = *lptr++;
  167.     else
  168.         quote = '\0';
  169.  
  170.     for (; *lptr != '\0'; lptr++) {
  171.         if (quote) {
  172.             if (*lptr == '\0') {
  173.                 word[0] = '\0';
  174.                 sprintf(Msg,"Unmatched quote: %s", line);
  175.                 S;
  176.                 s_exit();
  177.             }
  178.             if (*lptr == quote)
  179.                 break;
  180.         } else if (!backslash && isspace(*lptr))
  181.             break;
  182.  
  183.         if (carat)
  184.             *ptr++ = *lptr & 0x1f,
  185.             carat = FALSE;
  186.         else if (backslash)
  187.             *ptr++ = *lptr,
  188.             backslash = FALSE;
  189.         else if (*lptr == '^')
  190.             carat = TRUE;
  191.         else if (*lptr == '\\')
  192.             backslash = TRUE;
  193.         else
  194.             *ptr++ = *lptr;
  195.     }
  196.  
  197.     lptr++;
  198. }
  199.  
  200. /*    Make the specified word all lower case */
  201. void lc_word(ptr)
  202. char *ptr;
  203. {
  204.     while (*ptr) {
  205.         *ptr = tolower(*ptr);
  206.         ptr++;
  207.     }
  208. }
  209.  
  210. /*    Get a single character from the local terminal */
  211. trminp()
  212. {
  213.     static int count = 0;
  214.     static char *p, kbbuf[SM_BUFF];
  215.  
  216.     if (count > 0) {
  217.         count--;
  218.         return(*p++ & 0xff);
  219.     }
  220.  
  221.     while ((count = read(0, p = kbbuf, SM_BUFF)) < 1)
  222.         ;
  223.  
  224.     count--;
  225.     return(*p++ & 0xff);
  226. }
  227.  
  228. void intdel(flag)
  229. {
  230.     if (flag)
  231.         ioctl(0, TCSETAW, &sigmode);
  232.     else
  233.         ioctl(0, TCSETAW, &newmode);
  234. }
  235.  
  236. beep()
  237. {
  238.     putc(7,tfp);
  239. }
  240.  
  241. /*    initialize termcap stuff */
  242. void get_ttype()
  243. {
  244.     char *ttytype;
  245.     char *p = tbuf;
  246.     char *s;
  247.  
  248.     if ((ttytype = getenv("TERM")) == NULLS) {
  249.         show(1,"TERM not set");
  250.         exit(6);
  251.     }
  252.     if (tgetent(tc, ttytype) != 1) {
  253.         sprintf(Msg,"Can't load %s", ttytype);
  254.         S;
  255.         exit(7);
  256.     }
  257.     ospeed = newmode.c_cflag & CBAUD;
  258.     LI = tgetnum("li") - 1;
  259.     CO = tgetnum("co");
  260.     if ((s=Tgetstr("pc")) == NULLS)
  261.         PC = '\0';
  262.     else
  263.         PC = *s;
  264.     
  265.     CD = Tgetstr("cd");
  266.     CE = Tgetstr("ce");
  267.     CL = Tgetstr("cl");
  268.     CM = Tgetstr("cm");
  269.     SE = Tgetstr("se");
  270.     SO = Tgetstr("so");
  271.     CF = Tgetstr("CF");
  272.     CN = Tgetstr("CN");
  273. }
  274.  
  275. /*    putchr() is a routine to pass to tputs() */
  276. void putchr(c)
  277. int c;
  278. {
  279.     putc(c,tfp);
  280. }
  281.  
  282. void cls()        { tputs(CL,LI,putchr); }
  283.  
  284. void cur_on()    { tputs(CN,1,putchr); }
  285.  
  286. void cur_off()    { tputs(CF,1,putchr); }
  287.  
  288. void cl_line()    { tputs(CE,1,putchr); }
  289.  
  290. void cl_end()    { tputs(CD,LI,putchr); }
  291.  
  292. void ttgoto(row, col)
  293. int row, col;
  294. {
  295.     tputs(tgoto(CM, col, row),1,putchr);
  296. }
  297.  
  298. void drawline(row, col, len)
  299. int row, col, len;
  300. {
  301.  
  302.     ttgoto(row, col);
  303.     while (len--)
  304.         fputc('-', tfp);
  305. }
  306.  
  307. void show(flag, str)
  308. short flag;
  309. char *str;
  310. {
  311.     if (!flag) {
  312.         beep();
  313.         ttgoto(LI,0);
  314.         cl_line();
  315.         ttgoto(LI,(CO-(int)strlen(str))/2 -1);
  316.     }
  317.     if (flag == 2)
  318.         putc('\n', tfp);
  319.     tputs(SO,1,putchr);
  320.     putc(' ',tfp);
  321.     fprintf(tfp, str);
  322.     putc(' ',tfp);
  323.     tputs(SE,1,putchr);
  324.     if (flag > 0)
  325.         putc('\n',tfp);
  326. }
  327.  
  328. void show_abort()
  329. {
  330.     show(2,"USER ABORT");
  331. }
  332.  
  333. /*    fetch a "hot key"
  334.     returns: (int)
  335. */
  336. dgetch()
  337. {
  338.     char c;
  339.     struct    termio oldt, t;
  340.  
  341.     ioctl(0, TCGETA, &oldt);
  342.     t = oldt;
  343.     t.c_lflag &= ~ICANON;    /* turn off canonicalization */
  344.     t.c_lflag &= ~ECHO;        /* turn off echo */
  345.     t.c_lflag &= ~ISIG;        /* turn off signal processing */
  346.     t.c_cc[VMIN] = 1;        /* single-character I/O */
  347.     ioctl(0, TCSETAW, &t);
  348.  
  349.     read(0, &c, 1);
  350.  
  351.     ioctl(0, TCSETAW, &oldt);
  352.     return((int)c);
  353. }
  354.  
  355. FILE *isregfile(pathname)
  356. char *pathname;
  357. {
  358.     struct stat statbuf;
  359.  
  360.     if (stat(pathname,&statbuf) || statbuf.st_mode & S_IFMT != S_IFREG)
  361.         return NULLF;
  362.     return fopen(pathname, "r");
  363. }
  364.  
  365.  
  366. FILE *openfile(name)
  367. char *name;
  368. {
  369.     FILE *fp = NULLF;
  370.     char *home, fullname[SM_BUFF], *path, *pathend;
  371.     int pathlen;
  372.  
  373.     if ((path = getenv("XC_PATH")) != NULLS) {
  374.         while (fp == NULLF) {
  375.             if ((pathend = strchr(path, ':')) == NULLS)
  376.                 pathlen = strlen(path);
  377.             else
  378.                 pathlen = pathend - path;
  379.  
  380.             sprintf(fullname, "%.*s/%s", pathlen, path, name);
  381.             fp = isregfile(fullname);
  382.  
  383.             path += pathlen;
  384.             if (*path == '\0')
  385.                 break;
  386.             path++;
  387.         }
  388.     }
  389.  
  390.     if (fp == NULLF)
  391.         fp = isregfile(name);
  392.     
  393.     if (fp == NULLF) {
  394.         if ((home = getenv("HOME")) != NULLS)
  395.             sprintf(fullname, "%s/%s", home, name);
  396.         fp = isregfile(fullname);
  397.     }
  398.  
  399.     if (fp == NULLF) {
  400.         sprintf(fullname, "%s/%s", LIBDIR, name);
  401.         fp = isregfile(fullname);
  402.     }
  403.  
  404.     return fp;
  405. }
  406.