1 /*
2  * cmd_show.c - User command to display output of background jobs
3  *
4  * Copyright (C) 1995, 1996 by Scott C. Gray
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, write to the Free Software
18  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * You may contact the author :
21  *   e-mail:  gray@voicenet.com
22  *            grays@xtend-tech.com
23  *            gray@xenotropic.com
24  */
25 #include <stdio.h>
26 #include "sqsh_config.h"
27 #include "sqsh_error.h"
28 #include "sqsh_global.h"
29 #include "sqsh_varbuf.h"
30 #include "sqsh_env.h"
31 #include "sqsh_cmd.h"
32 #include "sqsh_job.h"
33 #include "cmd.h"
34 
35 /*-- Current Version --*/
36 #if !defined(lint) && !defined(__LINT__)
37 static char RCS_Id[] = "$Id: cmd_show.c,v 1.2 2010/01/26 15:03:50 mwesdorp Exp $" ;
38 USE(RCS_Id)
39 #endif /* !defined(lint) */
40 
41 /*
42  * cmd_show:
43  */
44 int cmd_show( argc, argv )
45 	int    argc ;
46 	char  *argv[] ;
47 {
48 	char        str[1024] ;
49 	job_id_t    job_id ;
50 	char       *output ;
51 	FILE       *file ;
52 
53 	/*-- Check the argument count --*/
54 	if( argc != 2 ) {
55 		fprintf( stderr, "Use: \\show job_id\n" ) ;
56 		return CMD_FAIL ;
57 	}
58         if ((job_id = (job_id_t)atoi(argv[1])) <= 0) {
59 	        fprintf( stderr, "\\show: Invalid job_id %s\n", argv[1] ) ;
60 	        return CMD_FAIL ;
61 	}
62 
63 	switch( jobset_is_done( g_jobset, job_id ) ) {
64 		case  -1 :
65 			fprintf( stderr, "\\show: %s\n", sqsh_get_errstr() ) ;
66 			return CMD_FAIL ;
67 			break ;
68 
69 		case   0 :
70 			fprintf( stderr, "\\show: Job %d has not completed\n", job_id ) ;
71 			return CMD_FAIL ;
72 			break ;
73 
74 		default :
75 			break ;
76 	}
77 
78 
79 	/*
80 	 * Request the name of the file in which the jobs output
81 	 * has been deferred.
82 	 */
83 	if( (output = jobset_get_defer( g_jobset, job_id )) == NULL ) {
84 		fprintf( stderr, "\\show: Unable to retrieve output: %s\n",
85 		         sqsh_get_errstr() ) ;
86 		return CMD_FAIL ;
87 	}
88 
89 	/*
90 	 * Attempt to open the output file.
91 	 */
92 	if( (file = fopen( output, "r" )) == NULL ) {
93 		fprintf( stderr, "\\show: Unable to open %s: %s\n",
94 		         output, strerror(errno) ) ;
95 		return CMD_FAIL ;
96 	}
97 
98 	while( fgets( str, sizeof(str), file ) != NULL )
99 		fprintf( stdout, "%s", str ) ;
100 
101 	fclose( file ) ;
102 
103 	if( (jobset_end( g_jobset, job_id )) == False ) {
104 		fprintf( stderr, "\\show: Error killing %d, %s\n",
105 		         (int)job_id, sqsh_get_errstr() ) ;
106 		return CMD_FAIL ;
107 	}
108 
109 	return CMD_LEAVEBUF ;
110 }
111