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