home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1456 / getlinenum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.4 KB  |  131 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. #include "simped.h"
  8. #define    PLACES    3    /* 3 mantissa places [0], [1], [2], [3]=NULL*/
  9.  
  10. /*
  11.  * arg 2 is a flag for special return. 
  12.  * "cr=1"  will return 1 if nothing entered. Mainly for list.
  13.  * "/?=cr" will return on / or ? instead of cr. Mainly for editline.
  14.  *       This will return ? or / in *delimiter. (arg 3), otherwise,
  15.  *       agr 3 will be null.
  16.  * ""      (null) is the default and will ask for a "Line number:" if
  17.  *         one isn't entered.
  18.  */
  19. int getlinenum(count, flag, delimiter)
  20. int  count;
  21. char *flag;
  22. char *delimiter;
  23. {
  24. int    printf(),
  25.     fflush(),
  26.     puts(),
  27.     fputs(),
  28.     atoi();
  29.  
  30. char    linenum[PLACES],/* the line number before atoi */
  31.     inpdigit;    /* each digit as it is read in */
  32.  
  33. int    theline,    /* returns the line number, 0 if the screen
  34.              * hasnt changed or -1 on error */
  35.     linenuminx=0,    /* an index to line */
  36.     creturn=FALSE;    /* flag that "theline" may not equal zero */
  37.  
  38.  
  39. do
  40.     {
  41.     if ((inpdigit=getchar()) == '\n' && linenuminx == 0)
  42.     {
  43.     if (creturn)
  44.         {
  45.         return(theline = -1);
  46.         }
  47.     else
  48.         {
  49.         if ( ! strcmp(flag, "cr=1") )
  50.         {
  51.         return(theline = 1);
  52.         }
  53.         else
  54.         {
  55.         fputs("\nLine number: ", stdout);
  56.         fflush(stdout);
  57.         creturn = TRUE;
  58.         inpdigit=' ';
  59.         }
  60.         }
  61.     }
  62.     else
  63.     {
  64.     if(inpdigit == '\b')
  65.         {
  66.         fputs("\b \b", stdout);
  67.         fflush(stdout);
  68.         if ( --linenuminx >= 0 )
  69.         linenum[linenuminx] = '\0';
  70.         if (linenuminx == -1)
  71.         {
  72.         if (creturn)
  73.             {
  74.             return(theline = -1);
  75.             }
  76.         else /* returning from the function via bs, no screen change */
  77.             {
  78.             return(theline = 0);
  79.             }
  80.         }
  81.         }
  82.     else
  83.         {
  84.         if (linenuminx < PLACES && inpdigit >= '0' &&
  85.         inpdigit <= '9' && inpdigit != '\n')
  86.         {
  87.         putchar(inpdigit);
  88.         fflush(stdout);
  89.         linenum[linenuminx++]=inpdigit;
  90.         linenum[linenuminx]='\0';
  91.         }
  92.         else
  93.         {
  94.         if (inpdigit != '\n')
  95.             {
  96.             if ( ! strcmp(flag, "/?=cr") )
  97.             {
  98.             putchar(inpdigit);
  99.             fflush(stdout);
  100.             *delimiter=(char)inpdigit;
  101.             inpdigit=(int)"";
  102.             break;
  103.             }
  104.             else
  105.             {
  106.             putchar(BELL);
  107.             fflush(stdout);
  108.             }
  109.             }
  110.         }
  111.         }
  112.     }
  113.     }
  114. while (inpdigit != '\n');
  115.  
  116. theline=atoi(linenum);
  117. if(theline > count)
  118.     {
  119.     printf("\nThere are only %d lines.\n", count);
  120.     theline = -1;
  121.     }
  122. else
  123.     if (theline == 0)
  124.     {
  125.     puts("\nThere is no line 0.");
  126.     theline = -1;
  127.     }
  128. fflush(stdout);
  129. return(theline);
  130. }
  131.