1 | /* |
---|
2 | * Copyright 1993, 1995 Christopher Seiwald. |
---|
3 | * |
---|
4 | * This file is part of Jam - see jam.c for Copyright information. |
---|
5 | */ |
---|
6 | |
---|
7 | # include "jam.h" |
---|
8 | # include "lists.h" |
---|
9 | # include "execcmd.h" |
---|
10 | |
---|
11 | # ifdef OS_VMS |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <string.h> |
---|
15 | #include <stdlib.h> |
---|
16 | #include <iodef.h> |
---|
17 | #include <ssdef.h> |
---|
18 | #include <descrip.h> |
---|
19 | #include <dvidef.h> |
---|
20 | #include <clidef.h> |
---|
21 | |
---|
22 | /* |
---|
23 | * execvms.c - execute a shell script, ala VMS |
---|
24 | * |
---|
25 | * The approach is this: |
---|
26 | * |
---|
27 | * If the command is a single line, and shorter than WRTLEN (what we |
---|
28 | * believe to be the maximum line length), we just system() it. |
---|
29 | * |
---|
30 | * If the command is multi-line, or longer than WRTLEN, we write the |
---|
31 | * command block to a temp file, splitting long lines (using "-" at |
---|
32 | * the end of the line to indicate contiuation), and then source that |
---|
33 | * temp file. We use special logic to make sure we don't continue in |
---|
34 | * the middle of a quoted string. |
---|
35 | * |
---|
36 | * 05/04/94 (seiwald) - async multiprocess interface; noop on VMS |
---|
37 | * 12/20/96 (seiwald) - rewritten to handle multi-line commands well |
---|
38 | * 01/14/96 (seiwald) - don't put -'s between "'s |
---|
39 | */ |
---|
40 | |
---|
41 | #define WRTLEN 240 |
---|
42 | |
---|
43 | #define MIN( a, b ) ((a) < (b) ? (a) : (b)) |
---|
44 | |
---|
45 | /* 1 for the @ and 4 for the .com */ |
---|
46 | |
---|
47 | char tempnambuf[ L_tmpnam + 1 + 4 ] = {0}; |
---|
48 | |
---|
49 | void |
---|
50 | execcmd( |
---|
51 | char *string, |
---|
52 | void (*func)( void *closure, int status ), |
---|
53 | void *closure, |
---|
54 | LIST *shell ) |
---|
55 | { |
---|
56 | char *s, *e, *p; |
---|
57 | int rstat = EXEC_CMD_OK; |
---|
58 | int status; |
---|
59 | |
---|
60 | /* See if string is more than one line */ |
---|
61 | /* discounting leading/trailing white space */ |
---|
62 | |
---|
63 | for( s = string; *s && isspace( *s ); s++ ) |
---|
64 | ; |
---|
65 | |
---|
66 | e = p = strchr( s, '\n' ); |
---|
67 | |
---|
68 | while( p && isspace( *p ) ) |
---|
69 | ++p; |
---|
70 | |
---|
71 | /* If multi line or long, write to com file. */ |
---|
72 | /* Otherwise, exec directly. */ |
---|
73 | |
---|
74 | if( p && *p || e - s > WRTLEN ) |
---|
75 | { |
---|
76 | FILE *f; |
---|
77 | |
---|
78 | /* Create temp file invocation "@sys$scratch:tempfile.com" */ |
---|
79 | |
---|
80 | if( !*tempnambuf ) |
---|
81 | { |
---|
82 | tempnambuf[0] = '@'; |
---|
83 | (void)tmpnam( tempnambuf + 1 ); |
---|
84 | strcat( tempnambuf, ".com" ); |
---|
85 | } |
---|
86 | |
---|
87 | /* Open tempfile */ |
---|
88 | |
---|
89 | if( !( f = fopen( tempnambuf + 1, "w" ) ) ) |
---|
90 | { |
---|
91 | printf( "can't open command file\n" ); |
---|
92 | (*func)( closure, EXEC_CMD_FAIL ); |
---|
93 | return; |
---|
94 | } |
---|
95 | |
---|
96 | /* For each line of the string */ |
---|
97 | |
---|
98 | while( *string ) |
---|
99 | { |
---|
100 | char *s = strchr( string, '\n' ); |
---|
101 | int len = s ? s + 1 - string : strlen( string ); |
---|
102 | |
---|
103 | fputc( '$', f ); |
---|
104 | |
---|
105 | /* For each chunk of a line that needs to be split */ |
---|
106 | |
---|
107 | while( len > 0 ) |
---|
108 | { |
---|
109 | char *q = string; |
---|
110 | char *qe = string + MIN( len, WRTLEN ); |
---|
111 | char *qq = q; |
---|
112 | int quote = 0; |
---|
113 | |
---|
114 | /* Look for matching "'s */ |
---|
115 | |
---|
116 | for( ; q < qe; q++ ) |
---|
117 | if( *q == '"' && ( quote = !quote ) ) |
---|
118 | qq = q; |
---|
119 | |
---|
120 | /* Back up to opening quote, if in one */ |
---|
121 | |
---|
122 | if( quote ) |
---|
123 | q = qq; |
---|
124 | |
---|
125 | fwrite( string, ( q - string ), 1, f ); |
---|
126 | |
---|
127 | len -= ( q - string ); |
---|
128 | string = q; |
---|
129 | |
---|
130 | if( len ) |
---|
131 | { |
---|
132 | fputc( '-', f ); |
---|
133 | fputc( '\n', f ); |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | fclose( f ); |
---|
139 | |
---|
140 | status = system( tempnambuf ) & 0x07; |
---|
141 | |
---|
142 | unlink( tempnambuf + 1 ); |
---|
143 | } |
---|
144 | else |
---|
145 | { |
---|
146 | /* Execute single line command */ |
---|
147 | /* Strip trailing newline before execing */ |
---|
148 | if( e ) *e = 0; |
---|
149 | status = system( s ) & 0x07; |
---|
150 | } |
---|
151 | |
---|
152 | /* Fail for error or fatal error */ |
---|
153 | /* OK on OK, warning, or info exit */ |
---|
154 | |
---|
155 | if( status == 2 || status == 4 ) |
---|
156 | rstat = EXEC_CMD_FAIL; |
---|
157 | |
---|
158 | (*func)( closure, rstat ); |
---|
159 | } |
---|
160 | |
---|
161 | int |
---|
162 | execwait() |
---|
163 | { |
---|
164 | return 0; |
---|
165 | } |
---|
166 | |
---|
167 | # endif /* VMS */ |
---|