run.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. */
  6. #include "ack.h"
  7. #include "list.h"
  8. #include "trans.h"
  9. #include "grows.h"
  10. #include "data.h"
  11. #include <signal.h>
  12. #ifndef NORCSID
  13. static char rcs_id[] = "$Id$" ;
  14. #endif
  15. #define ARG_MORE 40 /* The size of args chunks to allocate */
  16. extern growstring scanvars();
  17. static char **arglist ; /* The first argument */
  18. static unsigned argcount ; /* The current number of arguments */
  19. static unsigned argmax; /* The maximum number of arguments so far */
  20. int runphase(phase) register trf *phase ; {
  21. register list_elem *elem ;
  22. char *prog ; int result ;
  23. growstring bline ;
  24. bline=scanvars(phase->t_prog) ;
  25. prog=gr_final(&bline) ;
  26. if ( v_flag || debug ) {
  27. if ( v_flag==1 && !debug ) {
  28. vprint("%s",phase->t_name) ;
  29. if ( !phase->t_combine ) {
  30. vprint(" %s%s\n",p_basename,
  31. strrindex(in.p_path,SUFCHAR) ) ;
  32. } else {
  33. scanlist(l_first(phase->t_inputs), elem) {
  34. vprint(" %s",p_cont(*elem)->p_path);
  35. }
  36. vprint("\n") ;
  37. }
  38. } else {
  39. /* list all args */
  40. vprint("%s",prog) ;
  41. scanlist(l_first(phase->t_flags), elem) {
  42. vprint(" %s",l_content(*elem)) ;
  43. }
  44. scanlist(l_first(phase->t_args), elem) {
  45. vprint(" %s",l_content(*elem)) ;
  46. }
  47. vprint("\n") ;
  48. }
  49. }
  50. argcount=0 ;
  51. x_arg(phase->t_name) ;
  52. scanlist(l_first(phase->t_flags), elem) {
  53. x_arg(l_content(*elem)) ;
  54. }
  55. scanlist(l_first(phase->t_args), elem) {
  56. x_arg(l_content(*elem)) ;
  57. }
  58. x_arg( (char *)0 ) ;
  59. result=run_exec(phase,prog) ;
  60. throws(prog) ;
  61. return result ;
  62. }
  63. int run_exec(phase,prog) trf *phase ; char *prog ; {
  64. int status, child, waitchild ;
  65. do_flush();
  66. while ( (child=fork())== -1 ) ;
  67. if ( child ) {
  68. /* The parent */
  69. do {
  70. waitchild= wait(&status) ;
  71. if ( waitchild== -1 ) {
  72. fatal("missing child") ;
  73. }
  74. } while ( waitchild!=child) ;
  75. if ( status ) {
  76. if ( status&0200 && (status&0177)!=SIGQUIT &&
  77. t_flag<=1 ) unlink("core") ;
  78. switch ( status&0177 ) {
  79. case 0 :
  80. break ;
  81. case SIGHUP:
  82. case SIGINT:
  83. case SIGQUIT:
  84. case SIGTERM:
  85. quit(-5) ;
  86. default:
  87. error("%s died with signal %d",
  88. prog,status&0177) ;
  89. }
  90. /* The assumption is that processes voluntarely
  91. dying with a non-zero status already produced
  92. some sort of error message to the outside world.
  93. */
  94. n_error++ ;
  95. return 0 ;
  96. }
  97. return 1 ; /* From the parent */
  98. }
  99. /* The child */
  100. if ( phase->t_stdin ) {
  101. if ( !in.p_path ) {
  102. fatal("no input file for %s",phase->t_name) ;
  103. }
  104. close(0) ;
  105. if ( open(in.p_path,0)!=0 ) {
  106. error("cannot open %s",in.p_path) ;
  107. exit(1) ;
  108. }
  109. }
  110. if ( phase->t_stdout ) {
  111. if ( !out.p_path ) {
  112. fatal("no output file for %s",phase->t_name) ;
  113. }
  114. close(1) ;
  115. if ( creat(out.p_path,0666)!=1 ) {
  116. close(1); dup(2);
  117. error("cannot create %s",out.p_path) ;
  118. exit(1) ;
  119. }
  120. }
  121. execv(prog,arglist) ;
  122. if ( phase->t_stdout ) { close(1) ; dup(2) ; }
  123. error("Cannot execute %s",prog) ;
  124. exit(1) ;
  125. /*NOTREACHED*/
  126. }
  127. x_arg(string) char *string ; {
  128. /* Add one execute argument to the argument vector */
  129. if ( argcount==argmax ) {
  130. if ( argmax==0 ) {
  131. argmax= 2*ARG_MORE ;
  132. arglist= (char **)getcore(argmax*sizeof (char *)) ;
  133. } else {
  134. argmax += ARG_MORE ;
  135. arglist= (char **)changecore((char *)arglist,
  136. argmax*sizeof (char *)) ;
  137. }
  138. }
  139. *(arglist+argcount++) = string ;
  140. }