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

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