home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / s / seyon197.tz / seyon197 / seyon / SeGeneric.c < prev    next >
C/C++ Source or Header  |  1993-02-19  |  4KB  |  209 lines

  1.  
  2. /*
  3.  * This file is part of the Seyon, Copyright (c) 1992-1993 by Muhammad M.
  4.  * Saggaf. All rights reserved.
  5.  *
  6.  * See the file COPYING (1-COPYING) or the manual page seyon(1) for a full
  7.  * statement of rights and permissions for this program.
  8.  */
  9.  
  10. #include "config.h"
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <signal.h>
  16. #include <setjmp.h>
  17. #include <errno.h>
  18. #include <sys/types.h>
  19. #include <sys/time.h>
  20. #include <ctype.h>
  21. #include <math.h>
  22.  
  23. #include "SeDecl.h"
  24.  
  25. void
  26. ReadCommentedFile(fp, line)
  27.      FILE           *fp;
  28.      char           *line[];
  29. {
  30.   char            buffer[REG_BUF + 1],
  31.                  *bufPtr,
  32.                  *ptr;
  33.   int             i;
  34.  
  35.   for (i = 0; i < MAX_ENT && fgets(buffer, REG_BUF, fp) != NULL;) {
  36.  
  37.     /*Strip newline character from end of string*/
  38.     buffer[strlen(buffer) - 1] = '\0';
  39.  
  40.     /*Remove leading and trailing spaces*/
  41.     bufPtr = SSpc(buffer);
  42.  
  43.     /*Ignore this line if it is empty or starts with a comment*/
  44.     if (bufPtr[0] == '\0' || bufPtr[0] == '#')
  45.       continue;
  46.  
  47.     /*Ignore trailing comments*/
  48.     if ((ptr = strrchr(bufPtr, '#')) != NULL)
  49.       *ptr = '\0';
  50.  
  51.     line[i++] = XtNewString(bufPtr);
  52.   }
  53.  
  54.   line[i] = NULL;
  55. }
  56.  
  57. void
  58. FreeList(listArr)
  59.      XtPointer       listArr[];
  60. {
  61.   int             i;
  62.  
  63.   for (i = 0; listArr[i]; i++)
  64.     XtFree(listArr[i]);
  65.  
  66.   listArr[0] = NULL;
  67. }
  68.  
  69. int
  70. ConvertStringToIntArray(str, intArr)
  71.      char *str;
  72.      int  intArr[];
  73. {
  74.   char num[TIN_BUF], *numPtr;
  75.   int i;
  76.   
  77.   intArr[0] = 0;
  78.  
  79.   if (!str) return 0;
  80.  
  81.   for (i = 0;;) {
  82.     while (isspace(*str)) str++;
  83.     if (!*str) return 0;
  84.  
  85.     for (numPtr = num; *str && !isspace(*str); *numPtr++ = *str++);
  86.     *numPtr = '\0';
  87.     intArr[i] = atoi(num);
  88.     intArr[++i] = 0;
  89.   }
  90. }
  91.  
  92. jmp_buf         read_env;
  93.  
  94. void
  95. alrm(dummy)
  96.      int             dummy;
  97. {
  98.   longjmp(read_env, 1);
  99. }
  100.  
  101. /*
  102.  * trminp() is used as a single-character terminal input routine.
  103.  */
  104.  
  105. /* This routine needs a lot of work. In particular, the old siganl handler
  106.    for SIGALARM has to be saved and then restored, similary the time remaining
  107.    until the next alarm goes off if an alarm is already installed. */
  108.  
  109. int
  110. trminp(fd, seconds)
  111.      int             fd,
  112.                      seconds;
  113. {
  114.   /*BUFSIZ is defined in stdio.h*/
  115.   static char     rxbuf[BUFSIZ],
  116.                  *p;
  117.   static int      count = 0;
  118.  
  119.   if (count > 0) {
  120.     count--;
  121.     return (*p++ & 0xff);
  122.   }
  123.  
  124.   if (setjmp(read_env) != 0) {
  125.     alarm(0);
  126.     return -2;
  127.   }
  128.  
  129.   if (seconds > 0) {
  130.     signal(SIGALRM, alrm);
  131.     alarm((unsigned)seconds);
  132.   }
  133.  
  134.   if ((count = read(fd, p = rxbuf, BUFSIZ)) < 0) {
  135.     if (errno != EINTR) {
  136.       se_perror("character read");
  137.       return -3;
  138.     }
  139.     return -2;
  140.   }
  141.  
  142.   if (seconds > 0)
  143.     alarm(0);
  144.  
  145.   count--;
  146.   return (*p++ & 0xff);
  147. }
  148.  
  149. int
  150. termInpStr(fd, buf)
  151.      int             fd;
  152.      char *buf;
  153. {
  154.   int      count;
  155.  
  156.   /* BUFSIZ is defined in stdio.h */
  157.   if ((count = read(fd, buf, BUFSIZ)) < 0) {
  158.     se_perror("character read");
  159.     return -1;
  160.   }
  161.  
  162.   return count;
  163. }
  164.  
  165. /*
  166.  * usleep for systems that do not have it
  167.  */
  168.  
  169. #ifndef HAVE_USLEEP
  170. void
  171. usleep(usec)
  172.      unsigned long usec
  173. {
  174. #ifdef HAVE_SELECT
  175.   /*
  176.    * Orest Zborowski originally wrote this
  177.    */
  178.   struct timeval  timeout;
  179.  
  180.   timeout.tv_sec = usec / 1000000;
  181.   timeout.tv_usec = usec - 1000000 * timeout.tv_sec;
  182.   select(1, NULL, NULL, NULL, &timeout);
  183. #else
  184. /*
  185.  * This busy-waiting, normally a bad idea on a multi-tasking system, is used
  186.  * because sleep(1) is way too much of a delay.
  187.  */
  188.   int             i;
  189.  
  190.   for (i = 0; i < usec; i++);
  191. #endif
  192. }
  193.  
  194. #endif
  195.  
  196. #ifndef HAVE_DUP2        /* For those that do not have dup2()... */
  197. int
  198. dup2(oldfd, newfd)
  199.      int             oldfd,
  200.                      newfd;
  201. {
  202.   if (fcntl(oldfd, F_GETFL, 0) == -1)    /* Valid file descriptor? */
  203.     return (-1);           /* No, return an error. */
  204.   close(newfd);               /* Ensure newfd is closed */
  205.   return (fcntl(oldfd, F_DUPFD, newfd));    /* Dup oldfd into newfd */
  206. }
  207.  
  208. #endif /* HAVE_DUP2  Thanks to Bill Allie CIS: 76703,2061 */
  209.