run.c 3.5 KB

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