home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / W2KPRK.iso / apps / posix / source / PAX / TTYIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-17  |  7.5 KB  |  293 lines

  1. /* $Source: /u/mark/src/pax/RCS/ttyio.c,v $
  2.  *
  3.  * $Revision: 1.2 $
  4.  *
  5.  * ttyio.c - Terminal/Console I/O functions for all archive interfaces
  6.  *
  7.  * DESCRIPTION
  8.  *
  9.  *    These routines provide a consistent, general purpose interface to
  10.  *    the user via the users terminal, if it is available to the
  11.  *    process.
  12.  *
  13.  * AUTHOR
  14.  *
  15.  *     Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
  16.  *
  17.  * Sponsored by The USENIX Association for public distribution. 
  18.  *
  19.  * Copyright (c) 1989 Mark H. Colburn.
  20.  * All rights reserved.
  21.  *
  22.  * Redistribution and use in source and binary forms are permitted
  23.  * provided that the above copyright notice is duplicated in all such 
  24.  * forms and that any documentation, advertising materials, and other 
  25.  * materials related to such distribution and use acknowledge that the 
  26.  * software was developed * by Mark H. Colburn and sponsored by The 
  27.  * USENIX Association. 
  28.  *
  29.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  30.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  31.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  32.  *
  33.  * $Log:    ttyio.c,v $
  34.  * Revision 1.2  89/02/12  10:06:11  mark
  35.  * 1.2 release fixes
  36.  * 
  37.  * Revision 1.1  88/12/23  18:02:39  mark
  38.  * Initial revision
  39.  * 
  40.  */
  41.  
  42. #ifndef lint
  43. static char *ident = "$Id: ttyio.c,v 1.2 89/02/12 10:06:11 mark Exp $";
  44. static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  45. #endif /* ! lint */
  46.  
  47.  
  48. /* Headers */
  49.  
  50. #include "pax.h"
  51.  
  52.  
  53. /* open_tty - open the terminal for interactive queries
  54.  *
  55.  * DESCRIPTION
  56.  *
  57.  *     Assumes that background processes ignore interrupts and that the
  58.  *    open() or the isatty() will fail for processes which are not
  59.  *    attached to terminals. Returns a file descriptor or -1 if
  60.  *    unsuccessful. 
  61.  *
  62.  * RETURNS
  63.  *
  64.  *    Returns a file descriptor which can be used to read and write
  65.  *    directly to the user's terminal, or -1 on failure.  
  66.  *
  67.  * ERRORS
  68.  *
  69.  *    If SIGINT cannot be ignored, or the open fails, or the newly opened
  70.  *    terminal device is not a tty, then open_tty will return a -1 to the
  71.  *    caller.
  72.  */
  73.  
  74. #ifdef __STDC__
  75.  
  76. int open_tty(void)
  77.  
  78. #else
  79.  
  80. int open_tty()
  81.  
  82. #endif
  83. {
  84.     int             fd;        /* file descriptor for terminal */
  85. #ifdef __STDC__  /* Xn */
  86.     SIG_T         (*intr)(int);    /* used to restore interupts if signal fails */  /* Xn */
  87. #else  /* Xn */
  88.     SIG_T         (*intr)();    /* used to restore interupts if signal fails */
  89. #endif  /* Xn */
  90.  
  91. #ifdef DF_TRACE_DEBUG
  92. printf("DF_TRACE_DEBUG: int open_tty() in ttyio.c\n");
  93. #endif
  94.     if ((intr = signal(SIGINT, SIG_IGN)) == SIG_IGN) {
  95.     return (-1);
  96.     }
  97.     signal(SIGINT, intr);
  98.     if ((fd = open(TTY, O_RDWR)) < 0) {
  99.     return (-1);
  100.     }
  101.     if (isatty(fd)) {
  102.     return (fd);
  103.     }
  104.     close(fd);
  105.     return (-1);
  106. }
  107.  
  108.  
  109. /* nextask - ask a question and get a response
  110.  *
  111.  * DESCRIPTION
  112.  *
  113.  *    Give the user a prompt and wait for their response.  The prompt,
  114.  *    located in "msg" is printed, then the user is allowed to type
  115.  *    a response to the message.  The first "limit" characters of the
  116.  *    user response is stored in "answer".
  117.  *
  118.  *    Nextask ignores spaces and tabs. 
  119.  *
  120.  * PARAMETERS
  121.  *
  122.  *    char *msg    - Message to display for user 
  123.  *    char *answer    - Pointer to user's response to question 
  124.  *    int limit    - Limit of length for user's response
  125.  *
  126.  * RETURNS
  127.  *
  128.  *    Returns the number of characters in the user response to the 
  129.  *    calling function.  If an EOF was encountered, a -1 is returned to
  130.  *    the calling function.  If an error occured which causes the read
  131.  *    to return with a value of -1, then the function will return a
  132.  *    non-zero return status to the calling process, and abort
  133.  *    execution.
  134.  */
  135.  
  136. #ifdef __STDC__
  137.  
  138. int nextask(char *msg, char *answer, int limit)
  139.  
  140. #else
  141.  
  142. int nextask(msg, answer, limit)
  143. char           *msg;        /* message to display for user */
  144. char           *answer;        /* pointer to user's response to question */
  145. int             limit;        /* limit of length for user's response */
  146.  
  147. #endif
  148. {
  149.     int             idx;    /* index into answer for character input */
  150.     int             got;    /* number of characters read */
  151. #if 0
  152.     char            c;        /* character read */
  153. #endif /* 05/04/90-JPB */
  154.  
  155. #ifdef DF_TRACE_DEBUG
  156. printf("DF_TRACE_DEBUG: int nextask() in ttyio.c\n");
  157. #endif
  158.     if (ttyf < 0) {
  159.     fatal("/dev/tty Unavailable");
  160.     }
  161.     write(ttyf, msg, (uint) strlen(msg));
  162.     idx = 0;
  163. #if 0
  164.     while ((got = read(ttyf, &c, 1)) == 1) {
  165.     if (c == '\n') {
  166.         break;
  167.     } else if (c == ' ' || c == '\t') {
  168.         continue;
  169.     } else if (idx < limit - 1) {
  170.         answer[idx++] = c;
  171.     }
  172.     }
  173. #endif /* 05/04/90-JPB */
  174.     got = idx = read(ttyf, answer, limit);      /* 05/04/90-JPB */
  175.     if (got == 0) {        /* got an EOF */
  176.         return(-1);
  177.     }
  178.     if (got < 0) {
  179.     fatal(strerror(errno));  /* Xn */
  180.     }
  181.     answer[idx - 1] = '\0';             /* 05/04/90-JPB */
  182.     return(0);
  183. }
  184.  
  185.  
  186. /* lineget - get a line from a given stream
  187.  *
  188.  * DESCRIPTION
  189.  * 
  190.  *    Get a line of input for the stream named by "stream".  The data on
  191.  *    the stream is put into the buffer "buf".
  192.  *
  193.  * PARAMETERS
  194.  *
  195.  *    FILE *stream        - Stream to get input from 
  196.  *    char *buf        - Buffer to put input into
  197.  *
  198.  * RETURNS
  199.  *
  200.  *     Returns 0 if successful, -1 at EOF. 
  201.  */
  202.  
  203. #ifdef __STDC__
  204.  
  205. int lineget(FILE *stream, char *buf)
  206.  
  207. #else
  208.  
  209. int lineget(stream, buf)
  210. FILE           *stream;        /* stream to get input from */
  211. char           *buf;        /* buffer to put input into */
  212.  
  213. #endif
  214. {
  215.     int             c;
  216.  
  217. #ifdef DF_TRACE_DEBUG
  218. printf("DF_TRACE_DEBUG: int lineget() in ttyio.c\n");
  219. #endif
  220.     for (;;) {
  221.     if ((c = getc(stream)) == EOF) {
  222.         return (-1);
  223.     }
  224.     if (c == '\n') {
  225.         break;
  226.     }
  227.     *buf++ = (char)c;  /* Xn */
  228.     }
  229.     *buf = '\0';
  230.     return (0);
  231. }
  232.  
  233.  
  234. /* next - Advance to the next archive volume. 
  235.  *
  236.  * DESCRIPTION
  237.  *
  238.  *    Prompts the user to replace the backup medium with a new volume
  239.  *    when the old one is full.  There are some cases, such as when
  240.  *    archiving to a file on a hard disk, that the message can be a
  241.  *    little surprising.  Assumes that background processes ignore
  242.  *    interrupts and that the open() or the isatty() will fail for
  243.  *    processes which are not attached to terminals. Returns a file
  244.  *    descriptor or -1 if unsuccessful. 
  245.  *
  246.  * PARAMETERS
  247.  *
  248.  *    int mode    - mode of archive (READ, WRITE, PASS) 
  249.  */
  250.  
  251. #ifdef __STDC__
  252.  
  253. void next(int mode)
  254.  
  255. #else
  256.  
  257. void next(mode)
  258. int             mode;        /* mode of archive (READ, WRITE, PASS) */
  259.  
  260. #endif
  261. {
  262.     static char     go[] = "go";    /* keyword to proceed */  /* Xn */
  263.     static char     quit[] = "quit";    /* keyword to abort */  /* Xn */
  264.     char            msg[200];    /* buffer for message display */ 
  265.     char            answer[20];    /* buffer for user's answer */
  266.     int             ret;
  267.  
  268. #ifdef DF_TRACE_DEBUG
  269. printf("DF_TRACE_DEBUG: void next() in ttyio.c\n");
  270. #endif
  271.     close_archive();
  272.  
  273.     sprintf(msg, "%s: Ready for volume %u\n%s: Type \"%s\" when ready to proceed (or \"%s\" to abort): ",  /* Xn */
  274.            myname, arvolume + 1, myname, go, quit);  /* Xn */
  275. #ifdef __STDC__  /* Xn */
  276.     if (!f_quiet)  /* Xn */
  277.     (void) strcat(msg, "\a");  /* Xn */
  278. #else  /* Xn */
  279.     if (!f_quiet)  /* Xn */
  280.     (void) strcat(msg, "\07");  /* Xn */
  281. #endif  /* Xn */
  282.     for (;;) {
  283.     ret = nextask(msg, answer, sizeof(answer));
  284.     if (ret == -1 || strcmp(answer, quit) == 0) {  /* Xn */
  285.         fatal("Aborted");
  286.     }
  287.     if (strcmp(answer, go) == 0 && open_archive(mode) == 0) {  /* Xn */
  288.         break;
  289.     }
  290.     }
  291.     warnarch("Continuing", (OFFSET) 0);
  292. }
  293.