run.c 3.5 KB

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