home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Networking / ncftp-2.4.2-MIHS / src / LGets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-07  |  2.9 KB  |  130 lines

  1. /* LGets.c */
  2.  
  3. #include "Sys.h"
  4. #include "Util.h"
  5. #include "LGets.h"
  6.  
  7. #ifdef HAVE_LIBREADLINE
  8. #    include <readline/readline.h>
  9. #    ifdef HAVE_READLINE_HISTORY_H
  10.         /* Required for version 2.0 of readline. */
  11. #        include <readline/history.h>
  12. #    endif
  13. #endif    /* HAVE_LIBREADLINE */
  14.  
  15. #ifdef HAVE_LIBGETLINE
  16. #    include <getline.h>
  17. #endif    /* HAVE_LIBGETLINE */
  18.  
  19. extern int gIsFromTTY, gIsToTTY;
  20.  
  21. char *StdioGets(char *promptStr, char *lineStr, size_t size)
  22. {
  23.     char *cp, *nl;
  24.  
  25.     if (gIsFromTTY) {
  26.         /* It's okay to print a prompt if we are redirecting stdout,
  27.          * as long as stdin is still a tty.  Otherwise, don't print
  28.          * a prompt at all if stdin is redirected.
  29.          */
  30.         (void) fputs(promptStr, stdout);
  31.     }
  32.     (void) fflush(stdout);    /* for svr4 */
  33.     cp = fgets(lineStr, (int)(size - 2), stdin);
  34.     if (cp != NULL) {
  35.         nl = cp + strlen(cp) - 1;
  36.         if (*nl == '\n')
  37.             *nl = '\0';
  38.     }
  39.     return cp;
  40. }    /* StdioGets */
  41.  
  42.  
  43.  
  44.  
  45. #ifdef HAVE_LIBREADLINE
  46. char *ReadlineGets(char *promptStr, char *lineStr, size_t size)
  47. {
  48.     char *dynamic;
  49.     
  50.     dynamic = readline(promptStr);
  51.     if (dynamic == NULL)
  52.         return NULL;
  53.     if (dynamic[0] != '\0') {
  54.         (void) Strncpy(lineStr, dynamic, size);
  55.         add_history(lineStr);
  56.      }
  57.        free(dynamic);
  58.        return (lineStr);
  59. }    /* ReadlineGets */
  60. #endif    /* HAVE_LIBREADLINE */
  61.  
  62.  
  63.  
  64.  
  65.  
  66. #ifdef HAVE_LIBGETLINE
  67. char *GetlineGets(char *promptStr, char *lineStr, size_t size)
  68. {
  69.     char *cp, *nl;
  70.  
  71.     if ((cp = getline(promptStr)) != NULL) {
  72.         if (*cp == '\0')    /* You hit ^D. */
  73.             return NULL;
  74.         cp = Strncpy(lineStr, cp, size);
  75.         nl = cp + strlen(cp) - 1;
  76.         if (*nl == '\n')
  77.             *nl = '\0';
  78.         if (*cp != '\0') {    /* Don't add blank lines to history buffer. */
  79.             gl_histadd(cp);
  80.         }
  81.     }
  82.     return (cp);
  83. }    /* GetlineGets */
  84. #endif    /* HAVE_LIBGETLINE */
  85.  
  86.  
  87.  
  88.  
  89. /* Given a prompt string, a destination string, and its size, return feedback
  90.  * from the user in the destination string, with any trailing newlines
  91.  * stripped.  Returns NULL if EOF encountered.
  92.  */
  93. char *LineModeGets(char *promptStr, char *lineStr, size_t size)
  94. {
  95.     char *cp;
  96.     longstring pLines;
  97.     string p2;
  98.  
  99.     lineStr[0] = 0;    /* Clear it, in case of an error later. */
  100.     if (gIsFromTTY && gIsToTTY) {
  101.         /* Don't worry about a cmdline/history editor or prompts
  102.          * if you redirected a file at me.
  103.          */
  104.  
  105.         /* The prompt string may actually be several lines if the user put a
  106.          * newline in it with the @N option.  In this case we only want to print
  107.          * the very last line, so the command-line editors won't screw up.  So
  108.          * now we print all the lines except the last line.
  109.          */
  110.         cp = strrchr(promptStr, '\n');
  111.         if (cp != NULL) {
  112.             STRNCPY(p2, cp + 1);
  113.             STRNCPY(pLines, promptStr);
  114.             cp = pLines + (int)(cp - promptStr);
  115.             *cp = '\0';
  116.             promptStr = p2;
  117.             (void) fputs(pLines, stdout);
  118.         }
  119.  
  120. #ifdef HAVE_LIBREADLINE
  121.         return (ReadlineGets(promptStr, lineStr, size));
  122. #endif    /* HAVE_LIBREADLINE */
  123. #ifdef HAVE_LIBGETLINE
  124.         return (GetlineGets(promptStr, lineStr, size));
  125. #endif    /* HAVE_LIBGETLINE */
  126.     }
  127.  
  128.     return (StdioGets(promptStr, lineStr, size));
  129. }    /* LineModeGets */
  130.