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