home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1456 / getline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  3.5 KB  |  178 lines

  1.  
  2. /*
  3.  * Copyright (C) 1990 Jay Konigsberg - see Makefile for details
  4.  * This is Free Software, distrubited under the GNU Software Aggrement.
  5.  */
  6.  
  7. /*
  8.  * getline - gets a line of text from the fd: in_file. If the line of
  9.  * text is longer than the allowed length (LINELEN) it strips off the
  10.  * last word if there is whitespace, or the last char if not. The
  11.  * overflow is saved, unless the dumpoverflow flag is set.
  12.  *
  13.  * The overflow is returned to the caller and maintained in static
  14.  * space.
  15.  */
  16.  
  17. #include "simped.h"
  18.  
  19. char *getline(buffer, text_entered, in_file, delimiter, dumpoverflow)
  20. char buffer[];        /* input buffer to be passed out */
  21. int  *text_entered;    /* flag that something was entered */
  22. FILE *in_file;        /* stdin, or the fd of a file */
  23. char delimiter;        /* if set, will return like a return will */
  24. int  dumpoverflow;    /* boolean */
  25. {
  26. int    fflush(),
  27.     fputs(),
  28.     puts();
  29.  
  30. static    char *overflow;
  31. static    char extrachar = '\0';    /* autowrap char when there is no whitespace */
  32.  
  33.  
  34. int    blankchar,        /* location of blanking during autowrap */
  35.     inpchar,        /* char read in for procesing into buffer */
  36.     curchar=0,        /* input buffer pointer */
  37.     whitespace,        /* location of autowrap cut */
  38.     have_nl=FALSE;        /* newline flag */
  39.  
  40. *text_entered = FALSE;
  41. have_nl = FALSE;
  42. fflush(stdout);
  43.  
  44. for (curchar=0; (curchar<=LINELEN && (! have_nl)); ++curchar)
  45.     {
  46.     if (overflow)
  47.     {
  48.     curchar = strlen(overflow);
  49.     if (in_file == stdin)
  50.         {
  51.         fputs(overflow, stdout);
  52.         fflush(stdout);
  53.         }
  54.     buffer[0]='\0';
  55.     strcat(buffer, overflow);
  56.     overflow = NULL;
  57.     }
  58.     else
  59.     {
  60.     if (extrachar)
  61.         {
  62.         curchar = 1;
  63.         if (in_file == stdin)
  64.         {
  65.         putchar(extrachar);
  66.         }
  67.         buffer[0]=extrachar;
  68.         buffer[1]=extrachar='\0';
  69.         }
  70.     }
  71.     inpchar = getc(in_file);
  72.     if (inpchar == EOF)
  73.     {
  74.     *text_entered = EOF;
  75.     break;
  76.     }
  77.     if( inpchar == '\b' )
  78.     {
  79.     if ( curchar > 0)
  80.         {
  81.         if (in_file == stdin)
  82.         {
  83.         fputs("\b \b", stdout);
  84.         }
  85.         curchar -= 2; /* one will be added back in the for loop */
  86.         }
  87.     else
  88.         {
  89.         putchar(BELL);
  90.         curchar--;
  91.         }
  92.     buffer[curchar+1]='\0';
  93.     }
  94.     else 
  95.     {
  96.     if ( inpchar == '\t' )
  97.         {
  98.         inpchar = ' ';
  99.         }
  100.     else
  101.         {
  102.         if ( inpchar == '\n' )
  103.         {
  104.         have_nl = TRUE;
  105.         }
  106.         }
  107.     if ( delimiter == inpchar )
  108.         {
  109.         putchar(delimiter);
  110.         buffer[curchar]='\n';
  111.         buffer[curchar+1]='\0';
  112.         break;
  113.         }
  114.     if (inpchar != EOF)
  115.         {
  116.         buffer[curchar] = inpchar; 
  117.         buffer[curchar+1] = '\0'; 
  118.         if (in_file == stdin)
  119.         {
  120.         putchar(inpchar);
  121.         }
  122.         }
  123.     }
  124.     fflush(stdout);
  125.     }
  126. /* At least 1 char & a CR */
  127. if ( curchar >= 2 || ( curchar > 1 && delimiter != '0') )
  128.     {
  129.     *text_entered = TRUE;
  130.     }
  131. /* If the line overflowed - do an autowrap */
  132. if (curchar == LINELEN+1 && ! have_nl)
  133.     {
  134.     /* look for the last whitespace */
  135.     for (whitespace=curchar-1; whitespace >= 0; --whitespace)
  136.     {
  137.         if ( buffer[whitespace] == ' ' )
  138.         {
  139.         break;
  140.         }
  141.     }
  142.     if (whitespace > 0)
  143.     {
  144.     for(blankchar=LINELEN; blankchar>whitespace; --blankchar)
  145.         {
  146.         if (in_file == stdin)
  147.             {
  148.             fputs("\b \b", stdout);
  149.             }
  150.         }
  151.     if (in_file == stdin)
  152.         {
  153.         puts("");
  154.         }
  155.     overflow=(char *)&buffer[whitespace+1];
  156.     buffer[whitespace] = '\0';
  157.     }
  158.     else /* No whitespace, put the last char in overflow */
  159.     {
  160.     if (in_file == stdin)
  161.         {
  162.         fputs("\b \b", stdout);
  163.         puts("");
  164.         }
  165.     extrachar = buffer[LINELEN];
  166.     buffer[LINELEN] = '\n';
  167.     buffer[LINELEN+1] = '\0';
  168.     }
  169.     }
  170. if (dumpoverflow)
  171.     {
  172.     overflow=NULL;
  173.     extrachar='\0';
  174.     return('\0');
  175.     }
  176. return(overflow);
  177. }
  178.