Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
6.4 KiB

  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. #ifndef lint
  42. static char *ident = "$Id: ttyio.c,v 1.2 89/02/12 10:06:11 mark Exp $";
  43. static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  44. #endif /* ! lint */
  45. /* Headers */
  46. #include "pax.h"
  47. /* open_tty - open the terminal for interactive queries
  48. *
  49. * DESCRIPTION
  50. *
  51. * Assumes that background processes ignore interrupts and that the
  52. * open() or the isatty() will fail for processes which are not
  53. * attached to terminals. Returns a file descriptor or -1 if
  54. * unsuccessful.
  55. *
  56. * RETURNS
  57. *
  58. * Returns a file descriptor which can be used to read and write
  59. * directly to the user's terminal, or -1 on failure.
  60. *
  61. * ERRORS
  62. *
  63. * If SIGINT cannot be ignored, or the open fails, or the newly opened
  64. * terminal device is not a tty, then open_tty will return a -1 to the
  65. * caller.
  66. */
  67. int open_tty(void)
  68. {
  69. int fd; /* file descriptor for terminal */
  70. SIG_T (*intr)(int); /* used to restore interupts if signal fails */ /* Xn */
  71. #ifdef DF_TRACE_DEBUG
  72. printf("DF_TRACE_DEBUG: int open_tty() in ttyio.c\n");
  73. #endif
  74. if ((intr = signal(SIGINT, SIG_IGN)) == SIG_IGN) {
  75. return (-1);
  76. }
  77. signal(SIGINT, intr);
  78. if ((fd = open(TTY, O_RDWR)) < 0) {
  79. return (-1);
  80. }
  81. if (isatty(fd)) {
  82. return (fd);
  83. }
  84. close(fd);
  85. return (-1);
  86. }
  87. /* nextask - ask a question and get a response
  88. *
  89. * DESCRIPTION
  90. *
  91. * Give the user a prompt and wait for their response. The prompt,
  92. * located in "msg" is printed, then the user is allowed to type
  93. * a response to the message. The first "limit" characters of the
  94. * user response is stored in "answer".
  95. *
  96. * Nextask ignores spaces and tabs.
  97. *
  98. * PARAMETERS
  99. *
  100. * char *msg - Message to display for user
  101. * char *answer - Pointer to user's response to question
  102. * int limit - Limit of length for user's response
  103. *
  104. * RETURNS
  105. *
  106. * Returns the number of characters in the user response to the
  107. * calling function. If an EOF was encountered, a -1 is returned to
  108. * the calling function. If an error occured which causes the read
  109. * to return with a value of -1, then the function will return a
  110. * non-zero return status to the calling process, and abort
  111. * execution.
  112. */
  113. int nextask(char *msg, char *answer, int limit)
  114. {
  115. int idx; /* index into answer for character input */
  116. int got; /* number of characters read */
  117. char c; /* character read */
  118. #ifdef DF_TRACE_DEBUG
  119. printf("DF_TRACE_DEBUG: int nextask() in ttyio.c\n");
  120. #endif
  121. if (ttyf < 0) {
  122. fatal("/dev/tty Unavailable");
  123. }
  124. write(ttyf, msg, (uint) strlen(msg));
  125. idx = 0;
  126. /* while ((got = read(ttyf, &c, 1)) == 1) {
  127. if (c == '\n') {
  128. break;
  129. } else if (c == ' ' || c == '\t') {
  130. continue;
  131. } else if (idx < limit - 1) {
  132. answer[idx++] = c;
  133. }
  134. }
  135. 05/04/90-JPB */
  136. got = idx = read(ttyf, answer, limit); /* 05/04/90-JPB */
  137. if (got == 0) { /* got an EOF */
  138. return(-1);
  139. }
  140. if (got < 0) {
  141. fatal(strerror(errno)); /* Xn */
  142. }
  143. answer[idx - 1] = '\0'; /* 05/04/90-JPB */
  144. return(0);
  145. }
  146. /* lineget - get a line from a given stream
  147. *
  148. * DESCRIPTION
  149. *
  150. * Get a line of input for the stream named by "stream". The data on
  151. * the stream is put into the buffer "buf".
  152. *
  153. * PARAMETERS
  154. *
  155. * FILE *stream - Stream to get input from
  156. * char *buf - Buffer to put input into
  157. *
  158. * RETURNS
  159. *
  160. * Returns 0 if successful, -1 at EOF.
  161. */
  162. int lineget(FILE *stream, char *buf)
  163. {
  164. int c;
  165. #ifdef DF_TRACE_DEBUG
  166. printf("DF_TRACE_DEBUG: int lineget() in ttyio.c\n");
  167. #endif
  168. for (;;) {
  169. if ((c = getc(stream)) == EOF) {
  170. return (-1);
  171. }
  172. if (c == '\n') {
  173. break;
  174. }
  175. *buf++ = (char)c;
  176. }
  177. *buf = '\0';
  178. return (0);
  179. }
  180. /* next - Advance to the next archive volume.
  181. *
  182. * DESCRIPTION
  183. *
  184. * Prompts the user to replace the backup medium with a new volume
  185. * when the old one is full. There are some cases, such as when
  186. * archiving to a file on a hard disk, that the message can be a
  187. * little surprising. Assumes that background processes ignore
  188. * interrupts and that the open() or the isatty() will fail for
  189. * processes which are not attached to terminals. Returns a file
  190. * descriptor or -1 if unsuccessful.
  191. *
  192. * PARAMETERS
  193. *
  194. * int mode - mode of archive (READ, WRITE, PASS)
  195. */
  196. void next(int mode)
  197. {
  198. static char go[] = "go"; /* keyword to proceed */ /* Xn */
  199. static char quit[] = "quit"; /* keyword to abort */ /* Xn */
  200. char msg[200]; /* buffer for message display */
  201. char answer[20]; /* buffer for user's answer */
  202. int ret;
  203. #ifdef DF_TRACE_DEBUG
  204. printf("DF_TRACE_DEBUG: void next() in ttyio.c\n");
  205. #endif
  206. close_archive();
  207. sprintf(msg, "%s: Ready for volume %u\n%s: Type \"%s\" when ready to proceed (or \"%s\" to abort): ", /* Xn */
  208. myname, arvolume + 1, myname, go, quit); /* Xn */
  209. if (!f_quiet) /* Xn */
  210. (void) strcat(msg, "\a"); /* Xn */
  211. for (;;) {
  212. ret = nextask(msg, answer, sizeof(answer));
  213. if (ret == -1 || strcmp(answer, quit) == 0) { /* Xn */
  214. fatal("Aborted");
  215. }
  216. if (strcmp(answer, go) == 0 && open_archive(mode) == 0) { /* Xn */
  217. break;
  218. }
  219. }
  220. warnarch("Continuing", (OFFSET) 0);
  221. }