Home | History | Annotate | Line # | Download | only in qsubst
qsubst.c revision 1.1
      1  1.1  perry /*
      2  1.1  perry  * qsubst -- designed for renaming routines existing in a whole bunch
      3  1.1  perry  *  of files.  Needs -ltermcap.
      4  1.1  perry  *
      5  1.1  perry  * Usage:
      6  1.1  perry  *
      7  1.1  perry  * qsubst str1 str2 [ options ]
      8  1.1  perry  *
      9  1.1  perry  * qsubst reads its options (see below) to get a list of files.  For
     10  1.1  perry  *  each file on this list, it then replaces str1 with str2 wherever
     11  1.1  perry  *  possible in that file, depending on user input (see below).  The
     12  1.1  perry  *  result is written back onto the original file.
     13  1.1  perry  *
     14  1.1  perry  * For each possible substitution, the user is prompted with a few
     15  1.1  perry  *  lines before and after the line containing the string to be
     16  1.1  perry  *  substituted.  The string itself is displayed using the terminal's
     17  1.1  perry  *  standout mode, if any.  Then one character is read from the
     18  1.1  perry  *  terminal.  This is then interpreted as follows (this is designed to
     19  1.1  perry  *  be like Emacs' query-replace-string):
     20  1.1  perry  *
     21  1.1  perry  *	space	replace this occurrence and go on to the next one
     22  1.1  perry  *	.	replace this occurrence and don't change any more in
     23  1.1  perry  *		this file (ie, go on to the next file).
     24  1.1  perry  *	,	tentatively replace this occurrence.  The lines as they
     25  1.1  perry  *		would look if the substitution were made are printed
     26  1.1  perry  *		out.  Then another character is read and it is used to
     27  1.1  perry  *		decide the result (possibly undoing the tentative
     28  1.1  perry  *		replacement).
     29  1.1  perry  *	n	don't change this one, but go on to the next one
     30  1.1  perry  *	^G	don't change this one or any others in this file, but
     31  1.1  perry  *		instead go on to the next file.
     32  1.1  perry  *	!	change the rest in this file without asking, then go on
     33  1.1  perry  *		to the next file (at which point qsubst will start
     34  1.1  perry  *		asking again).
     35  1.1  perry  *	?	print out the current filename and ask again.
     36  1.1  perry  *
     37  1.1  perry  * The first two arguments to qsubst are always the string to replace
     38  1.1  perry  *  and the string to replace it with.  The options are as follows:
     39  1.1  perry  *
     40  1.1  perry  *	-w	The search string is considered as a C symbol; it must
     41  1.1  perry  *		be bounded by non-symbol characters.  This option
     42  1.1  perry  *		toggles.  (`w' for `word'.)
     43  1.1  perry  *	-!	Enter ! mode automatically at the beginning of each
     44  1.1  perry  *		file.
     45  1.1  perry  *	-go	Same as -!
     46  1.1  perry  *	-noask	Same as -!
     47  1.1  perry  *	-nogo	Negate -go
     48  1.1  perry  *	-ask	Negate -noask (same as -nogo)
     49  1.1  perry  *	-cN	(N is a number) Give N lines of context above and below
     50  1.1  perry  *		the line with the match when prompting the user.
     51  1.1  perry  *	-CAN	(N is a number) Give N lines of context above the line
     52  1.1  perry  *		with the match when prompting the user.
     53  1.1  perry  *	-CBN	(N is a number) Give N lines of context below the line
     54  1.1  perry  *		with the match when prompting the user.
     55  1.1  perry  *	-f filename
     56  1.1  perry  *		The filename following the -f argument is one of the
     57  1.1  perry  *		files qsubst should perform substitutions in.
     58  1.1  perry  *	-F filename
     59  1.1  perry  *		qsubst should read the named file to get the names of
     60  1.1  perry  *		files to perform substitutions in.  The names should
     61  1.1  perry  *		appear one to a line.
     62  1.1  perry  *
     63  1.1  perry  * The default amount of context is -c2, that is, two lines above and
     64  1.1  perry  *  two lines below the line with the match.
     65  1.1  perry  *
     66  1.1  perry  * Arguments not beginning with a - sign in the options field are
     67  1.1  perry  *  implicitly preceded by -f.  Thus, -f is really needed only when the
     68  1.1  perry  *  file name begins with a - sign.
     69  1.1  perry  *
     70  1.1  perry  * qsubst reads its options in order and processes files as it gets
     71  1.1  perry  *  them.  This means, for example, that a -go will affect only files
     72  1.1  perry  *  from -f or -F options appearing after the -go option.
     73  1.1  perry  *
     74  1.1  perry  * The most context you can get is ten lines each, above and below
     75  1.1  perry  *  (corresponding to -c10).
     76  1.1  perry  *
     77  1.1  perry  * Str1 is limited to 512 characters; there is no limit on the size of
     78  1.1  perry  *  str2.  Neither one may contain a NUL.
     79  1.1  perry  *
     80  1.1  perry  * NULs in the file may cause qsubst to make various mistakes.
     81  1.1  perry  *
     82  1.1  perry  * If any other program modifies the file while qsubst is running, all
     83  1.1  perry  *  bets are off.
     84  1.1  perry  *
     85  1.1  perry  * This program is in the public domain.  Anyone may use it in any way
     86  1.1  perry  *  for any purpose.  Of course, it's also up to you to determine
     87  1.1  perry  *  whether what it does is suitable for you; the above comments may
     88  1.1  perry  *  help, but I can't promise they're accurate.  It's free, and you get
     89  1.1  perry  *  what you pay for.
     90  1.1  perry  *
     91  1.1  perry  * If you find any bugs I would appreciate hearing about them,
     92  1.1  perry  *  especially if you also fix them.
     93  1.1  perry  *
     94  1.1  perry  *					der Mouse
     95  1.1  perry  *
     96  1.1  perry  *			       mouse (at) rodents.montreal.qc.ca
     97  1.1  perry  */
     98  1.1  perry 
     99  1.1  perry #include <stdio.h>
    100  1.1  perry #include <ctype.h>
    101  1.1  perry #include <errno.h>
    102  1.1  perry #include <unistd.h>
    103  1.1  perry #include <stdlib.h>
    104  1.1  perry #include <signal.h>
    105  1.1  perry #include <strings.h>
    106  1.1  perry #include <termios.h>
    107  1.1  perry #include <sys/file.h>
    108  1.1  perry #include <unused-arg.h>
    109  1.1  perry 
    110  1.1  perry /* These belong in an include file, but which one? */
    111  1.1  perry extern int tgetent(char *, const char *);
    112  1.1  perry extern int tgetflag(const char *);
    113  1.1  perry extern const char *tgetstr(const char *, char **);
    114  1.1  perry extern void tputs(const char *, int, int (*)(char));
    115  1.1  perry 
    116  1.1  perry extern const char *__progname;
    117  1.1  perry 
    118  1.1  perry #define MAX_C_A 10
    119  1.1  perry #define MAX_C_B 10
    120  1.1  perry #define BUF_SIZ 1024
    121  1.1  perry 
    122  1.1  perry static int debugging;
    123  1.1  perry static FILE *tempf;
    124  1.1  perry static long int tbeg;
    125  1.1  perry static FILE *workf;
    126  1.1  perry static char *str1;
    127  1.1  perry static char *str2;
    128  1.1  perry static int s1l;
    129  1.1  perry static int s2l;
    130  1.1  perry static long int nls[MAX_C_A+1];
    131  1.1  perry static char buf[(BUF_SIZ*2)+2];
    132  1.1  perry static char *bufp;
    133  1.1  perry static char *bufp0;
    134  1.1  perry static char *bufpmax;
    135  1.1  perry static int rahead;
    136  1.1  perry static int cabove;
    137  1.1  perry static int cbelow;
    138  1.1  perry static int wordmode;
    139  1.1  perry static int flying;
    140  1.1  perry static int flystate;
    141  1.1  perry static int allfly;
    142  1.1  perry static const char *nullstr = "";
    143  1.1  perry static int ul_;
    144  1.1  perry static char *current_file;
    145  1.1  perry static const char *beginul;
    146  1.1  perry static const char *endul;
    147  1.1  perry static char tcp_buf[1024];
    148  1.1  perry static char cap_buf[1024];
    149  1.1  perry static struct termios orig_tio;
    150  1.1  perry 
    151  1.1  perry static void tstp_self(void)
    152  1.1  perry {
    153  1.1  perry  void (*old_tstp)(int);
    154  1.1  perry  int mask;
    155  1.1  perry 
    156  1.1  perry  mask = sigblock(0);
    157  1.1  perry  kill(getpid(),SIGTSTP);
    158  1.1  perry  old_tstp = signal(SIGTSTP,SIG_DFL);
    159  1.1  perry  sigsetmask(mask&~sigmask(SIGTSTP));
    160  1.1  perry  signal(SIGTSTP,old_tstp);
    161  1.1  perry }
    162  1.1  perry 
    163  1.1  perry static void sigtstp(UNUSED_ARG(int sig))
    164  1.1  perry {
    165  1.1  perry  struct termios tio;
    166  1.1  perry 
    167  1.1  perry  if (tcgetattr(0,&tio) < 0)
    168  1.1  perry   { tstp_self();
    169  1.1  perry     return;
    170  1.1  perry   }
    171  1.1  perry  tcsetattr(0,TCSAFLUSH|TCSASOFT,&orig_tio);
    172  1.1  perry  tstp_self();
    173  1.1  perry  tcsetattr(0,TCSADRAIN|TCSASOFT,&tio);
    174  1.1  perry }
    175  1.1  perry 
    176  1.1  perry static void limit_above_below(void)
    177  1.1  perry {
    178  1.1  perry  if (cabove > MAX_C_A)
    179  1.1  perry   { cabove = MAX_C_A;
    180  1.1  perry   }
    181  1.1  perry  if (cbelow > MAX_C_B)
    182  1.1  perry   { cbelow = MAX_C_B;
    183  1.1  perry   }
    184  1.1  perry }
    185  1.1  perry 
    186  1.1  perry static int issymchar(char c)
    187  1.1  perry {
    188  1.1  perry  return( isascii(c) &&
    189  1.1  perry 	 ( isalnum(c) ||
    190  1.1  perry 	   (c == '_') ||
    191  1.1  perry 	   (c == '$') ) );
    192  1.1  perry }
    193  1.1  perry 
    194  1.1  perry static int foundit(void)
    195  1.1  perry {
    196  1.1  perry  if (wordmode)
    197  1.1  perry   { return( !issymchar(bufp[-1]) &&
    198  1.1  perry 	    !issymchar(bufp[-2-s1l]) &&
    199  1.1  perry 	    !bcmp(bufp-1-s1l,str1,s1l) );
    200  1.1  perry   }
    201  1.1  perry  else
    202  1.1  perry   { return(!bcmp(bufp-s1l,str1,s1l));
    203  1.1  perry   }
    204  1.1  perry }
    205  1.1  perry 
    206  1.1  perry static int putcharf(char c)
    207  1.1  perry {
    208  1.1  perry  putchar(c);
    209  1.1  perry  return(0); /* ??? */
    210  1.1  perry }
    211  1.1  perry 
    212  1.1  perry static void put_ul(char *s)
    213  1.1  perry {
    214  1.1  perry  if (ul_)
    215  1.1  perry   { for (;*s;s++)
    216  1.1  perry      { printf("_\b%c",*s);
    217  1.1  perry      }
    218  1.1  perry   }
    219  1.1  perry  else
    220  1.1  perry   { tputs(beginul,1,putcharf);
    221  1.1  perry     fputs(s,stdout);
    222  1.1  perry     tputs(endul,1,putcharf);
    223  1.1  perry   }
    224  1.1  perry }
    225  1.1  perry 
    226  1.1  perry static int getc_cbreak(void)
    227  1.1  perry {
    228  1.1  perry  struct termios tio;
    229  1.1  perry  struct termios otio;
    230  1.1  perry  char c;
    231  1.1  perry 
    232  1.1  perry  if (tcgetattr(0,&tio) < 0) return(getchar());
    233  1.1  perry  otio = tio;
    234  1.1  perry  tio.c_lflag &= ~(ICANON|ECHOKE|ECHOE|ECHO|ECHONL);
    235  1.1  perry  tio.c_cc[VMIN] = 1;
    236  1.1  perry  tio.c_cc[VTIME] = 0;
    237  1.1  perry  tcsetattr(0,TCSANOW|TCSASOFT,&tio);
    238  1.1  perry  switch (read(0,&c,1))
    239  1.1  perry   { case -1:
    240  1.1  perry        break;
    241  1.1  perry     case 0:
    242  1.1  perry        break;
    243  1.1  perry     case 1:
    244  1.1  perry        break;
    245  1.1  perry   }
    246  1.1  perry  tcsetattr(0,TCSANOW|TCSASOFT,&tio);
    247  1.1  perry  return(c);
    248  1.1  perry }
    249  1.1  perry 
    250  1.1  perry static int doit(void)
    251  1.1  perry {
    252  1.1  perry  long int save;
    253  1.1  perry  int i;
    254  1.1  perry  int lastnl;
    255  1.1  perry  int use_replacement;
    256  1.1  perry 
    257  1.1  perry  if (flying)
    258  1.1  perry   { return(flystate);
    259  1.1  perry   }
    260  1.1  perry  use_replacement = 0;
    261  1.1  perry  save = ftell(workf);
    262  1.1  perry  do
    263  1.1  perry   { for (i=MAX_C_A-cabove;nls[i]<0;i++) ;
    264  1.1  perry     fseek(workf,nls[i],0);
    265  1.1  perry     for (i=save-nls[i]-rahead;i;i--)
    266  1.1  perry      { putchar(getc(workf));
    267  1.1  perry      }
    268  1.1  perry     put_ul(use_replacement?str2:str1);
    269  1.1  perry     fseek(workf,save+s1l-rahead,0);
    270  1.1  perry     lastnl = 0;
    271  1.1  perry     i = cbelow + 1;
    272  1.1  perry     while (i > 0)
    273  1.1  perry      { int c;
    274  1.1  perry        c = getc(workf);
    275  1.1  perry        if (c == EOF)
    276  1.1  perry 	{ clearerr(workf);
    277  1.1  perry 	  break;
    278  1.1  perry 	}
    279  1.1  perry        putchar(c);
    280  1.1  perry        lastnl = 0;
    281  1.1  perry        if (c == '\n')
    282  1.1  perry 	{ i --;
    283  1.1  perry 	  lastnl = 1;
    284  1.1  perry 	}
    285  1.1  perry      }
    286  1.1  perry     if (! lastnl) printf("\n[no final newline] ");
    287  1.1  perry     fseek(workf,save,0);
    288  1.1  perry     i = -1;
    289  1.1  perry     while (i == -1)
    290  1.1  perry      { switch (getc_cbreak())
    291  1.1  perry 	{ case ' ':
    292  1.1  perry 	     i = 1;
    293  1.1  perry 	     break;
    294  1.1  perry 	  case '.':
    295  1.1  perry 	     i = 1;
    296  1.1  perry 	     flying = 1;
    297  1.1  perry 	     flystate = 0;
    298  1.1  perry 	     break;
    299  1.1  perry 	  case 'n':
    300  1.1  perry 	     i = 0;
    301  1.1  perry 	     break;
    302  1.1  perry 	  case '\7':
    303  1.1  perry 	     i = 0;
    304  1.1  perry 	     flying = 1;
    305  1.1  perry 	     flystate = 0;
    306  1.1  perry 	     break;
    307  1.1  perry 	  case '!':
    308  1.1  perry 	     i = 1;
    309  1.1  perry 	     flying = 1;
    310  1.1  perry 	     flystate = 1;
    311  1.1  perry 	     break;
    312  1.1  perry 	  case ',':
    313  1.1  perry 	     use_replacement = ! use_replacement;
    314  1.1  perry 	     i = -2;
    315  1.1  perry 	     printf("(using %s string gives)\n",use_replacement?"new":"old");
    316  1.1  perry 	     break;
    317  1.1  perry 	  case '?':
    318  1.1  perry 	     printf("File is `%s'\n",current_file);
    319  1.1  perry 	     break;
    320  1.1  perry 	  default:
    321  1.1  perry 	     putchar('\7');
    322  1.1  perry 	     break;
    323  1.1  perry 	}
    324  1.1  perry      }
    325  1.1  perry   } while (i < 0);
    326  1.1  perry  if (i)
    327  1.1  perry   { printf("(replacing");
    328  1.1  perry   }
    329  1.1  perry  else
    330  1.1  perry   { printf("(leaving");
    331  1.1  perry   }
    332  1.1  perry  if (flying)
    333  1.1  perry   { if (flystate == i)
    334  1.1  perry      { printf(" this and all the rest");
    335  1.1  perry      }
    336  1.1  perry     else if (flystate)
    337  1.1  perry      { printf(" this, replacing all the rest");
    338  1.1  perry      }
    339  1.1  perry     else
    340  1.1  perry      { printf(" this, leaving all the rest");
    341  1.1  perry      }
    342  1.1  perry   }
    343  1.1  perry  printf(")\n");
    344  1.1  perry  return(i);
    345  1.1  perry }
    346  1.1  perry 
    347  1.1  perry static void add_shift(long int *a, long int e, int n)
    348  1.1  perry {
    349  1.1  perry  int i;
    350  1.1  perry 
    351  1.1  perry  n --;
    352  1.1  perry  for (i=0;i<n;i++)
    353  1.1  perry   { a[i] = a[i+1];
    354  1.1  perry   }
    355  1.1  perry  a[n] = e;
    356  1.1  perry }
    357  1.1  perry 
    358  1.1  perry static void process_file(char *fn)
    359  1.1  perry {
    360  1.1  perry  int i;
    361  1.1  perry  long int n;
    362  1.1  perry  int c;
    363  1.1  perry 
    364  1.1  perry  workf = fopen(fn,"r+");
    365  1.1  perry  if (workf == NULL)
    366  1.1  perry   { fprintf(stderr,"%s: cannot read %s\n",__progname,fn);
    367  1.1  perry     return;
    368  1.1  perry   }
    369  1.1  perry  printf("(file: %s)\n",fn);
    370  1.1  perry  current_file = fn;
    371  1.1  perry  for (i=0;i<=MAX_C_A;i++)
    372  1.1  perry   { nls[i] = -1;
    373  1.1  perry   }
    374  1.1  perry  nls[MAX_C_A] = 0;
    375  1.1  perry  tbeg = -1;
    376  1.1  perry  if (wordmode)
    377  1.1  perry   { bufp0 = &buf[1];
    378  1.1  perry     rahead = s1l + 1;
    379  1.1  perry     buf[0] = '\0';
    380  1.1  perry   }
    381  1.1  perry  else
    382  1.1  perry   { bufp0 = &buf[0];
    383  1.1  perry     rahead = s1l;
    384  1.1  perry   }
    385  1.1  perry  if (debugging)
    386  1.1  perry   { printf("[rahead = %d, bufp0-buf = %d]\n",rahead,bufp0-&buf[0]);
    387  1.1  perry   }
    388  1.1  perry  n = 0;
    389  1.1  perry  bufp = bufp0;
    390  1.1  perry  bufpmax = &buf[sizeof(buf)-s1l-2];
    391  1.1  perry  flying = allfly;
    392  1.1  perry  flystate = 1;
    393  1.1  perry  while (1)
    394  1.1  perry   { c = getc(workf);
    395  1.1  perry     if (c == EOF)
    396  1.1  perry      { if (tbeg >= 0)
    397  1.1  perry 	{ if (bufp > bufp0) fwrite(bufp0,1,bufp-bufp0,tempf);
    398  1.1  perry 	  fseek(workf,tbeg,0);
    399  1.1  perry 	  n = ftell(tempf);
    400  1.1  perry 	  fseek(tempf,0L,0);
    401  1.1  perry 	  for (;n;n--)
    402  1.1  perry 	   { putc(getc(tempf),workf);
    403  1.1  perry 	   }
    404  1.1  perry 	  fflush(workf);
    405  1.1  perry 	  ftruncate(fileno(workf),ftell(workf));
    406  1.1  perry 	}
    407  1.1  perry        fclose(workf);
    408  1.1  perry        return;
    409  1.1  perry      }
    410  1.1  perry     *bufp++ = c;
    411  1.1  perry     n ++;
    412  1.1  perry     if (debugging)
    413  1.1  perry      { printf("[got %c, n now %ld, bufp-buf %d]\n",c,n,bufp-bufp0);
    414  1.1  perry      }
    415  1.1  perry     if ((n >= rahead) && foundit() && doit())
    416  1.1  perry      { int wbehind;
    417  1.1  perry        if (debugging)
    418  1.1  perry 	{ printf("[doing change]\n");
    419  1.1  perry 	}
    420  1.1  perry        wbehind = 1;
    421  1.1  perry        if (tbeg < 0)
    422  1.1  perry 	{ tbeg = ftell(workf) - rahead;
    423  1.1  perry 	  fseek(tempf,0L,0);
    424  1.1  perry 	  if (debugging)
    425  1.1  perry 	   { printf("[tbeg set to %d]\n",(int)tbeg);
    426  1.1  perry 	   }
    427  1.1  perry 	  wbehind = 0;
    428  1.1  perry 	}
    429  1.1  perry        if (bufp[-1] == '\n') add_shift(nls,ftell(workf),MAX_C_A+1);
    430  1.1  perry        if ((n > rahead) && wbehind)
    431  1.1  perry 	{ fwrite(bufp0,1,n-rahead,tempf);
    432  1.1  perry 	  if (debugging)
    433  1.1  perry 	   { printf("[writing %ld from bufp0]\n",n-rahead);
    434  1.1  perry 	   }
    435  1.1  perry 	}
    436  1.1  perry        fwrite(str2,1,s2l,tempf);
    437  1.1  perry        n = rahead - s1l;
    438  1.1  perry        if (debugging)
    439  1.1  perry 	{ printf("[n now %ld]\n",n);
    440  1.1  perry 	}
    441  1.1  perry        if (n > 0)
    442  1.1  perry 	{ bcopy(bufp-n,bufp0,n);
    443  1.1  perry 	  if (debugging)
    444  1.1  perry 	   { printf("[copying %ld back]\n",n);
    445  1.1  perry 	   }
    446  1.1  perry 	}
    447  1.1  perry        bufp = bufp0 + n;
    448  1.1  perry      }
    449  1.1  perry     else
    450  1.1  perry      { if (bufp[-1] == '\n') add_shift(nls,ftell(workf),MAX_C_A+1);
    451  1.1  perry        if (bufp >= bufpmax)
    452  1.1  perry 	{ if (tbeg >= 0)
    453  1.1  perry 	   { fwrite(bufp0,1,n-rahead,tempf);
    454  1.1  perry 	     if (debugging)
    455  1.1  perry 	      { printf("[flushing %ld]\n",n-rahead);
    456  1.1  perry 	      }
    457  1.1  perry 	   }
    458  1.1  perry 	  n = rahead;
    459  1.1  perry 	  bcopy(bufp-n,bufp0,n);
    460  1.1  perry 	  if (debugging)
    461  1.1  perry 	   { printf("[n now %ld]\n[copying %ld back]\n",n,n);
    462  1.1  perry 	   }
    463  1.1  perry 	  bufp = bufp0 + n;
    464  1.1  perry 	}
    465  1.1  perry      }
    466  1.1  perry   }
    467  1.1  perry }
    468  1.1  perry 
    469  1.1  perry static void process_indir_file(char *fn)
    470  1.1  perry {
    471  1.1  perry  char newfn[1024];
    472  1.1  perry  FILE *f;
    473  1.1  perry 
    474  1.1  perry  f = fopen(fn,"r");
    475  1.1  perry  if (f == NULL)
    476  1.1  perry   { fprintf(stderr,"%s: cannot read %s\n",__progname,fn);
    477  1.1  perry     return;
    478  1.1  perry   }
    479  1.1  perry  while (fgets(newfn,sizeof(newfn),f) == newfn)
    480  1.1  perry   { newfn[strlen(newfn)-1] = '\0';
    481  1.1  perry     process_file(newfn);
    482  1.1  perry   }
    483  1.1  perry  fclose(f);
    484  1.1  perry }
    485  1.1  perry 
    486  1.1  perry int main(int, char **);
    487  1.1  perry int main(int ac, char **av)
    488  1.1  perry {
    489  1.1  perry  int skip;
    490  1.1  perry  char *cp;
    491  1.1  perry 
    492  1.1  perry  if (ac < 3)
    493  1.1  perry   { fprintf(stderr,"Usage: %s str1 str2 [ -w -! -noask -go -f file -F file ]\n",
    494  1.1  perry 				__progname);
    495  1.1  perry     exit(1);
    496  1.1  perry   }
    497  1.1  perry  cp = getenv("TERM");
    498  1.1  perry  if (cp == 0)
    499  1.1  perry   { beginul = nullstr;
    500  1.1  perry     endul = nullstr;
    501  1.1  perry   }
    502  1.1  perry  else
    503  1.1  perry   { if (tgetent(tcp_buf,cp) != 1)
    504  1.1  perry      { beginul = nullstr;
    505  1.1  perry        endul = nullstr;
    506  1.1  perry      }
    507  1.1  perry     else
    508  1.1  perry      { cp = cap_buf;
    509  1.1  perry        if (tgetflag("os") || tgetflag("ul"))
    510  1.1  perry 	{ ul_ = 1;
    511  1.1  perry 	}
    512  1.1  perry        else
    513  1.1  perry 	{ ul_ = 0;
    514  1.1  perry 	  beginul = tgetstr("us",&cp);
    515  1.1  perry 	  if (beginul == 0)
    516  1.1  perry 	   { beginul = tgetstr("so",&cp);
    517  1.1  perry 	     if (beginul == 0)
    518  1.1  perry 	      { beginul = nullstr;
    519  1.1  perry 		endul = nullstr;
    520  1.1  perry 	      }
    521  1.1  perry 	     else
    522  1.1  perry 	      { endul = tgetstr("se",&cp);
    523  1.1  perry 	      }
    524  1.1  perry 	   }
    525  1.1  perry 	  else
    526  1.1  perry 	   { endul = tgetstr("ue",&cp);
    527  1.1  perry 	   }
    528  1.1  perry 	}
    529  1.1  perry      }
    530  1.1  perry   }
    531  1.1  perry   { static char tmp[] = "/tmp/qsubst.XXXXXX";
    532  1.1  perry     int fd;
    533  1.1  perry     fd = mkstemp(&tmp[0]);
    534  1.1  perry     if (fd < 0)
    535  1.1  perry      { fprintf(stderr,"%s: cannot create temp file: %s\n",__progname,strerror(errno));
    536  1.1  perry        exit(1);
    537  1.1  perry      }
    538  1.1  perry     tempf = fdopen(fd,"w+");
    539  1.1  perry   }
    540  1.1  perry  if ( (access(av[1],R_OK|W_OK) == 0) &&
    541  1.1  perry       (access(av[ac-1],R_OK|W_OK) < 0) &&
    542  1.1  perry       (access(av[ac-2],R_OK|W_OK) < 0) )
    543  1.1  perry   { fprintf(stderr,"%s: argument order has changed, it's now: str1 str2 files...\n",__progname);
    544  1.1  perry   }
    545  1.1  perry  str1 = av[1];
    546  1.1  perry  str2 = av[2];
    547  1.1  perry  av += 2;
    548  1.1  perry  ac -= 2;
    549  1.1  perry  s1l = strlen(str1);
    550  1.1  perry  s2l = strlen(str2);
    551  1.1  perry  if (s1l > BUF_SIZ)
    552  1.1  perry   { fprintf(stderr,"%s: search string too long (max %d chars)\n",__progname,BUF_SIZ);
    553  1.1  perry     exit(1);
    554  1.1  perry   }
    555  1.1  perry  tcgetattr(0,&orig_tio);
    556  1.1  perry  signal(SIGTSTP,sigtstp);
    557  1.1  perry  allfly = 0;
    558  1.1  perry  cabove = 2;
    559  1.1  perry  cbelow = 2;
    560  1.1  perry  skip = 0;
    561  1.1  perry  for (ac--,av++;ac;ac--,av++)
    562  1.1  perry   { if (skip > 0)
    563  1.1  perry      { skip --;
    564  1.1  perry        continue;
    565  1.1  perry      }
    566  1.1  perry     if (**av == '-')
    567  1.1  perry      { ++*av;
    568  1.1  perry        if (!strcmp(*av,"debug"))
    569  1.1  perry 	{ debugging ++;
    570  1.1  perry 	}
    571  1.1  perry        else if (!strcmp(*av,"w"))
    572  1.1  perry 	{ wordmode = ! wordmode;
    573  1.1  perry 	}
    574  1.1  perry        else if ( (strcmp(*av,"!") == 0) ||
    575  1.1  perry 		 (strcmp(*av,"go") == 0) ||
    576  1.1  perry 		 (strcmp(*av,"noask") == 0) )
    577  1.1  perry 	{ allfly = 1;
    578  1.1  perry 	}
    579  1.1  perry        else if ( (strcmp(*av,"nogo") == 0) ||
    580  1.1  perry 		 (strcmp(*av,"ask") == 0) )
    581  1.1  perry 	{ allfly = 0;
    582  1.1  perry 	}
    583  1.1  perry        else if (**av == 'c')
    584  1.1  perry 	{ cabove = atoi(++*av);
    585  1.1  perry 	  cbelow = cabove;
    586  1.1  perry 	  limit_above_below();
    587  1.1  perry 	}
    588  1.1  perry        else if (**av == 'C')
    589  1.1  perry 	{ ++*av;
    590  1.1  perry 	  if (**av == 'A')
    591  1.1  perry 	   { cabove = atoi(++*av);
    592  1.1  perry 	     limit_above_below();
    593  1.1  perry 	   }
    594  1.1  perry 	  else if (**av == 'B')
    595  1.1  perry 	   { cbelow = atoi(++*av);
    596  1.1  perry 	     limit_above_below();
    597  1.1  perry 	   }
    598  1.1  perry 	  else
    599  1.1  perry 	   { fprintf(stderr,"%s: -C must be -CA or -CB\n",__progname);
    600  1.1  perry 	   }
    601  1.1  perry 	}
    602  1.1  perry        else if ( (strcmp(*av,"f") == 0) ||
    603  1.1  perry 		 (strcmp(*av,"F") == 0) )
    604  1.1  perry 	{ if (++skip >= ac)
    605  1.1  perry 	   { fprintf(stderr,"%s: -%s what?\n",__progname,*av);
    606  1.1  perry 	   }
    607  1.1  perry 	  else
    608  1.1  perry 	   { if (**av == 'f')
    609  1.1  perry 	      { process_file(av[skip]);
    610  1.1  perry 	      }
    611  1.1  perry 	     else
    612  1.1  perry 	      { process_indir_file(av[skip]);
    613  1.1  perry 	      }
    614  1.1  perry 	   }
    615  1.1  perry 	}
    616  1.1  perry      }
    617  1.1  perry     else
    618  1.1  perry      { process_file(*av);
    619  1.1  perry      }
    620  1.1  perry   }
    621  1.1  perry  exit(0);
    622  1.1  perry }
    623