home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v92.tgz / v92.tar / v92 / src / runtime / rsys.r < prev    next >
Text File  |  1996-03-22  |  8KB  |  389 lines

  1. /*
  2.  * File: rsys.r
  3.  *  Contents: [flushrec], [getrec], getstrg, host, longread, [putrec], putstr
  4.  */
  5.  
  6. #ifdef RecordIO
  7. /*
  8.  * flushrec - force buffered output to be written with a record break.
  9.  *  Applies only to files with mode "s".
  10.  */
  11.  
  12. novalue flushrec(fd)
  13. FILE *fd;
  14. {
  15. #if SASC
  16.    afwrite("", 1, 0, fd);
  17. #endif                    /* SASC */
  18. }
  19.  
  20. /*
  21.  * getrec - read a record into buf from file fd. At most maxi characters
  22.  *  are read.  getrec returns the length of the record.
  23.  *  Returns -1 if EOF and -2 if length was limited by
  24.  *  maxi. [[ Needs ferror() check. ]]
  25.  *  This function is meaningful only for files opened with mode "s".
  26.  */
  27.  
  28. int getrec(buf, maxi, fd)
  29. register char *buf;
  30. int maxi;
  31. FILE *fd;
  32.    {
  33. #ifdef SASC
  34.    register int l;
  35.  
  36.    l = afreadh(buf, 1, maxi+1, fd);     /* read record or maxi+1 chars */
  37.    if (l == 0) return -1;
  38.    if (l <= maxi) return l;
  39.    ungetc(buf[maxi], fd);               /* if record not used up, push
  40.                                            back last char read */
  41.    return -2;
  42. #endif                    /* SASC */
  43.    }
  44. #endif                    /* RecordIO */
  45.  
  46. /*
  47.  * getstrg - read a line into buf from file fd.  At most maxi characters
  48.  *  are read.  getstrg returns the length of the line, not counting
  49.  *  the newline.  Returns -1 if EOF and -2 if length was limited by
  50.  *  maxi. [[ Needs ferror() check. ]]
  51.  */
  52.  
  53. int getstrg(buf, maxi, fd)
  54. register char *buf;
  55. int maxi;
  56. FILE *fd;
  57.    {
  58.    register int c, l;
  59.  
  60.  
  61. #if AMIGA
  62. #if LATTICE
  63.    /* This code is special for Lattice 4.0.  It was different for
  64.     *  Lattice 3.10 and probably won't work for other C compilers.
  65.     */
  66.    extern struct UFB _ufbs[];
  67.  
  68.    if (IsInteractive(_ufbs[fileno(fd)].ufbfh))
  69.       return read(fileno(fd),buf,maxi);
  70. #endif                    /* LATTICE */
  71. #endif                    /* AMIGA */
  72.  
  73. #ifdef XWindows
  74.    if (isatty(fileno(fd))) wflushall();
  75. #endif                    /* XWindows */
  76.  
  77.    l = 0;
  78.    while (1) {
  79. #ifdef Graphics
  80.       /* insert non-blocking read/code to service windows here */
  81. #endif                    /* Graphics */
  82.       if ((c = fgetc(fd)) == '\n') break;
  83.       if (c == EOF)
  84.      if (l > 0) return l;
  85.      else return -1;
  86.       if (++l > maxi) {
  87.      ungetc(c, fd);
  88.      return -2;
  89.      }
  90.       *buf++ = c;
  91.       }
  92.    return l;
  93.    }
  94.  
  95. /*
  96.  * iconhost - return some sort of host name into the buffer pointed at
  97.  *  by hostname.  This code accommodates several different host name
  98.  *  fetching schemes.
  99.  */
  100. novalue iconhost(hostname)
  101. char *hostname;
  102.    {
  103.  
  104. #ifdef WhoHost
  105.    /*
  106.     * The host name is in /usr/include/whoami.h. (V7, 4.[01]bsd)
  107.     */
  108.    whohost(hostname);
  109. #endif                    /* WhoHost */
  110.  
  111. #ifdef UtsName
  112.    {
  113.    /*
  114.     * Use the uname system call.  (System III & V)
  115.     */
  116.    struct utsname utsn;
  117.    uname(&utsn);
  118.    strcpy(hostname,utsn.nodename);
  119.    }
  120. #endif                    /* UtsName */
  121.  
  122. #ifdef GetHost
  123.    /*
  124.     * Use the gethostname system call.  (4.2bsd)
  125.     */
  126.    gethostname(hostname,MaxCvtLen);
  127. #endif                    /* GetHost */
  128.  
  129. #if VMS
  130.    /*
  131.     * VMS has its own special logic.
  132.     */
  133.    char *h;
  134.    if (!(h = getenv("ICON_HOST")) && !(h = getenv("SYS$NODE")))
  135.       h = "VAX/VMS";
  136.    strcpy(hostname,h);
  137. #endif                    /* VMS */
  138.  
  139. #ifdef HostStr
  140.    /*
  141.     * The string constant HostStr contains the host name.
  142.     */
  143.    strcpy(hostname,HostStr);
  144. #endif                    /* HostStr */
  145.  
  146.    }
  147.  
  148. #ifdef WhoHost
  149. #define HdrFile "/usr/include/whoami.h"
  150.  
  151. /*
  152.  * whohost - look for a line of the form
  153.  *  #define sysname "name"
  154.  * in HdrFile and return the name.
  155.  */
  156. novalue whohost(hostname)
  157. char *hostname;
  158.    {
  159.    char buf[BUFSIZ];
  160.    FILE *fd;
  161.  
  162.    fd = fopen(HdrFile, ReadText);
  163.    if (fd == NULL) {
  164.       sprintf(buf, "Cannot open %s, no value for &host\n", HdrFile);
  165.       syserr(buf);
  166.    }
  167.  
  168.    for (;;) {   /* each line in the file */
  169.       if (fgets(buf, sizeof buf, fd) == NULL) {
  170.          sprintf(buf, "No #define for sysname in %s, no value for &host\n",
  171.             HdrFile);
  172.          syserr(buf);
  173.       }
  174.       if (sscanf(buf,"#define sysname \"%[^\"]\"", hostname) == 1) {
  175.          fclose(fd);
  176.          return;
  177.       }
  178.    }
  179.    }
  180. #endif                    /* WhoHost */
  181.  
  182. /*
  183.  * Read a long string in shorter parts. (Standard read may not handle long
  184.  *  strings.)
  185.  */
  186. word longread(s,width,len,fd)
  187. FILE *fd;
  188. int width;
  189. char *s;
  190. long len;
  191. {
  192.    tended char *ts = s;
  193.    long tally = 0;
  194.    long n = 0;
  195.  
  196. #ifdef XWindows
  197.    if (isatty(fileno(fd))) wflushall();
  198. #endif                    /* XWindows */
  199.  
  200.    while (len > 0) {
  201.       n = fread(ts, width, (int)((len < MaxIn) ? len : MaxIn), fd);
  202.       if (n <= 0)
  203.          return tally;
  204.       tally += n;
  205.       ts += n;
  206.       len -= n;
  207.       }
  208.    return tally;
  209.    }
  210.  
  211. #ifdef RecordIO
  212. /*
  213.  * Write string referenced by descriptor d, avoiding a record break.
  214.  *  Applies only to files openend with mode "s".
  215.  */
  216.  
  217. int putrec(f, d)
  218. register FILE *f;
  219. dptr d;
  220.    {
  221. #if SASC
  222.    register char *s;
  223.    register word l;
  224.  
  225.    l = StrLen(*d);
  226.    if (l == 0)
  227.       return Succeeded;
  228.    s = StrLoc(*d);
  229.  
  230.    if (afwriteh(s,1,l,f) < l)
  231.       return Failed;
  232.    else
  233.       return Succeeded;
  234.    /*
  235.     * Note:  Because RecordIO depends on SASC, and because SASC
  236.     *  uses its own malloc rather than the Icon malloc, file usage
  237.     *  cannot cause a garbage collection.  This may require
  238.     *  reevaluation if RecordIO is supported for any other compiler.
  239.     */
  240. #endif                    /* SASC */
  241.    }
  242. #endif                    /* RecordIO */
  243.  
  244. /*
  245.  * Print string referenced by descriptor d. Note, d must not move during
  246.  *   a garbage collection.
  247.  */
  248.  
  249. int putstr(f, d)
  250. register FILE *f;
  251. dptr d;
  252.    {
  253.    register char *s;
  254.    register word l;
  255.    register int  i;
  256.  
  257.    l = StrLen(*d);
  258.    if (l == 0)
  259.       return  Succeeded;
  260.    s = StrLoc(*d);
  261.  
  262. #ifdef MSWindows
  263. #ifdef ConsoleWindow
  264.    if ((f == stdout && !(ConsoleFlags & StdOutRedirect)) ||
  265.     (f == stderr && !(ConsoleFlags & StdErrRedirect))) {
  266.       if (ConsoleBinding == NULL)
  267.          ConsoleBinding = OpenConsole();
  268. #if BORLAND_286
  269.       goto Lab1;
  270. #else
  271.       wputstr((wbp)ConsoleBinding, s, l);
  272. #endif
  273.       return Succeeded;
  274.       }
  275. Lab1:
  276. #endif                    /* ConsoleWindow */
  277. #endif                    /* MSWindows */
  278. #ifdef PresentationManager
  279.    if (ConsoleFlags & OutputToBuf) {
  280.       /* check for overflow */
  281.       if (MaxReadStr * 4 - ((int)ConsoleStringBufPtr - (int)ConsoleStringBuf) < l + 1)
  282.      return Failed;
  283.       /* big enough */
  284.       memcpy(ConsoleStringBufPtr, s, l);
  285.       ConsoleStringBufPtr += l;
  286.       *ConsoleStringBufPtr = '\0';
  287.       } /* End of if - push to buffer */
  288.    else if ((f == stdout && !(ConsoleFlags & StdOutRedirect)) ||
  289.         (f == stderr && !(ConsoleFlags & StdErrRedirect)))
  290.       wputstr((wbinding *)PMOpenConsole(), s, l);
  291.    return Succeeded;
  292. #endif                    /* PresentationManager */
  293. #if VMS
  294.    /*
  295.     * This is to get around a bug in VMS C's fwrite routine.
  296.     */
  297.    for (i = 0; i < l; i++)
  298.       if (putc(s[i], f) == EOF)
  299.          break;
  300.    if (i == l)
  301.       return Succeeded;
  302.    else
  303.       return Failed;
  304. #else                    /* VMS */
  305.    if (longwrite(s,l,f) < 0)
  306.       return Failed;
  307.    else
  308.       return Succeeded;
  309. #endif                    /* VMS */
  310.    }
  311.  
  312. /*
  313.  * idelay(n) - delay for n milliseconds
  314.  */
  315. idelay(n)
  316. int n;
  317.    {
  318.  
  319. /*
  320.  * The following code is operating-system dependent [@fsys.01].
  321.  */
  322. #if OS2
  323. #if OS2_32
  324.    DosSleep(n);
  325.    return Succeeded;
  326. #else                    /* OS2_32 */
  327.    return Failed;
  328. #endif                    /* OS2_32 */
  329. #endif                    /* OS2 */
  330.  
  331. #if VMS
  332.    delay_vms(n);
  333.    return Succeeded;
  334. #endif                    /* VMS */
  335.  
  336. #if SASC
  337.    sleepd(0.001*n);
  338.    return Succeeded;
  339. #endif                                   /* SASC */
  340.  
  341. #if UNIX
  342. #ifndef NoSelect
  343.    struct timeval t;
  344.    t.tv_sec = n / 1000;
  345.    t.tv_usec = (n % 1000) * 1000;
  346.    select(1, FD_NULL, FD_NULL, FD_NULL, &t);
  347.    return Succeeded;
  348. #else                    /* NoSelect */
  349.    return Failed;
  350. #endif                    /* NoSelect */
  351. #endif                    /* UNIX */
  352.  
  353. #if MSDOS
  354. #if SCCX_MX
  355.    msleep(n);
  356.    return Succeeded;
  357. #else                   /* SCCX_MX */
  358. #if NT
  359.    Sleep(n);
  360.    return Succeeded;
  361. #else                    /* NT */
  362. #if BORLAND_286
  363.    /* evil busy wait */
  364.     clock_t start = clock();
  365.     while ((double)((clock() - start) / CLK_TCK) / 1000 < n);
  366.     return Succeeded;
  367. #else                    /* BORLAND_286 */
  368.    return Failed;
  369. #endif                    /* BORLAND_286 */
  370. #endif                    /* NT */
  371. #endif                  /* SCCX_MX */
  372. #endif                    /* MSDOS */
  373.  
  374. #if MACINTOSH
  375.    void MacDelay(int n);
  376.    MacDelay(n);
  377.    return Succeeded;
  378. #endif                    /* MACINTOSH */
  379.  
  380.  
  381. #if PORT || AMIGA || ARM || ATARI_ST || MVS || VM
  382.    return Failed;
  383. #endif
  384.  
  385.    /*
  386.     * End of operating-system dependent code.
  387.     */
  388.    }
  389.