Commit 9a1c2704 9a1c2704537e75d949578d323d70bde761eb327f by Sergey Poznyakoff

Implemented "editheader" variable.

1 parent 20277076
Showing 1 changed file with 370 additions and 205 deletions
...@@ -21,12 +21,144 @@ ...@@ -21,12 +21,144 @@
21 #include <sys/stat.h> 21 #include <sys/stat.h>
22 22
23 static void 23 static void
24 dump_headers (FILE *fp, compose_env_t *env)
25 {
26 char buffer[512];
27 stream_t stream = NULL;
28 size_t off = 0, n;
29
30 header_get_stream (env->header, &stream);
31 while (stream_read (stream, buffer, sizeof buffer - 1, off, &n) == 0
32 && n != 0)
33 {
34 buffer[n] = 0;
35 fprintf (fp, "%s", buffer);
36 off += n;
37 }
38 fprintf (fp, "\n");
39 }
40
41 #define STATE_INIT 0
42 #define STATE_READ 1
43 #define STATE_BODY 2
44
45 static int
46 parse_headers (FILE *fp, compose_env_t *env)
47 {
48 int status;
49 header_t header;
50 char *name = NULL;
51 char *value = NULL;
52 char *buf = NULL;
53 int state = STATE_INIT;
54 size_t n = 0;
55 int errcnt = 0, line = 0;
56
57 if ((status = header_create (&header, NULL, 0, NULL)) != 0)
58 {
59 util_error ("can't create header: %s", mu_errstring (status));
60 return 1;
61 }
62
63 while (state != STATE_BODY
64 && errcnt == 0 && getline (&buf, &n, fp) > 0 && n > 0)
65 {
66 int len = strlen (buf);
67 if (len > 0 && buf[len-1] == '\n')
68 buf[len-1] = 0;
69
70 line++;
71 switch (state)
72 {
73 case STATE_INIT:
74 if (!buf[0] || isspace (buf[0]))
75 continue;
76 else
77 state = STATE_READ;
78 /*FALLTHRU*/
79
80 case STATE_READ:
81 if (buf[0] == 0)
82 state = STATE_BODY;
83 else if (isspace (buf[0]))
84 {
85 /* A continuation line */
86 if (name)
87 {
88 char *p = NULL;
89 asprintf (&p, "%s\n%s", value, buf);
90 free (value);
91 value = p;
92 }
93 else
94 {
95 util_error ("%d: not a header line", line);
96 errcnt++;
97 }
98 }
99 else
100 {
101 char *p;
102
103 if (name)
104 {
105 header_set_value (header, name, value, 0);
106 free (name);
107 free (value);
108 name = value = NULL;
109 }
110 p = strchr (buf, ':');
111 if (p)
112 {
113 *p++ = 0;
114 while (*p && isspace(*p))
115 p++;
116 value = strdup (p);
117 name = strdup (buf);
118 }
119 else
120 {
121 util_error ("%d: not a header line", line);
122 errcnt++;
123 }
124 }
125 break;
126 }
127 }
128
129 free (buf);
130 if (name)
131 {
132 header_set_value (header, name, value, 0);
133 free (name);
134 free (value);
135 }
136
137 if (errcnt)
138 {
139 char *p;
140
141 header_destroy (&header, NULL);
142 p = ml_readline ("Edit again?");
143 if (*p == 'y' || *p == 'Y')
144 return -1;
145 else
146 return 1;
147 }
148
149 header_destroy (&env->header, NULL);
150 env->header = header;
151 return 0;
152 }
153
154 static void
24 var_continue (void) 155 var_continue (void)
25 { 156 {
26 fprintf(stdout, "(continue)\n"); 157 fprintf (stdout, "(continue)\n");
27 } 158 }
28 159
29 static int var_check_args (int argc, char **argv) 160 static int
161 var_check_args (int argc, char **argv)
30 { 162 {
31 if (argc == 1) 163 if (argc == 1)
32 { 164 {
...@@ -40,11 +172,11 @@ static int var_check_args (int argc, char **argv) ...@@ -40,11 +172,11 @@ static int var_check_args (int argc, char **argv)
40 172
41 /* ~![shell-command] */ 173 /* ~![shell-command] */
42 int 174 int
43 var_shell(int argc, char **argv, struct send_environ *env) 175 var_shell (int argc, char **argv, compose_env_t *env)
44 { 176 {
45 int status; 177 int status;
46 ofile = env->ofile; 178 ofile = env->ofile;
47 status = mail_shell(argc, argv); 179 status = mail_shell (argc, argv);
48 ofile = env->file; 180 ofile = env->file;
49 return status; 181 return status;
50 } 182 }
...@@ -52,7 +184,7 @@ var_shell(int argc, char **argv, struct send_environ *env) ...@@ -52,7 +184,7 @@ var_shell(int argc, char **argv, struct send_environ *env)
52 /* ~:[mail-command] */ 184 /* ~:[mail-command] */
53 /* ~-[mail-command] */ 185 /* ~-[mail-command] */
54 int 186 int
55 var_command(int argc, char **argv, struct send_environ *env) 187 var_command (int argc, char **argv, compose_env_t *env)
56 { 188 {
57 struct mail_command_entry entry; 189 struct mail_command_entry entry;
58 int status; 190 int status;
...@@ -61,37 +193,37 @@ var_command(int argc, char **argv, struct send_environ *env) ...@@ -61,37 +193,37 @@ var_command(int argc, char **argv, struct send_environ *env)
61 return 1; 193 return 1;
62 if (argv[1][0] == '#') 194 if (argv[1][0] == '#')
63 return 0; 195 return 0;
64 entry = util_find_entry(mail_command_table, argv[1]); 196 entry = util_find_entry (mail_command_table, argv[1]);
65 if (!entry.func) 197 if (!entry.func)
66 { 198 {
67 util_error("Unknown command: %s", argv[1]); 199 util_error ("Unknown command: %s", argv[1]);
68 return 1; 200 return 1;
69 } 201 }
70 if (entry.flags & (EF_FLOW|EF_SEND)) 202 if (entry.flags & (EF_FLOW | EF_SEND))
71 { 203 {
72 util_error("Command not allowed in an escape sequence\n"); 204 util_error ("Command not allowed in an escape sequence\n");
73 return 1; 205 return 1;
74 } 206 }
75 207
76 ofile = env->ofile; 208 ofile = env->ofile;
77 status = (*entry.func)(argc-1, argv+1); 209 status = (*entry.func) (argc - 1, argv + 1);
78 ofile = env->file; 210 ofile = env->file;
79 return status; 211 return status;
80 } 212 }
81 213
82 /* ~? */ 214 /* ~? */
83 int 215 int
84 var_help(int argc, char **argv, struct send_environ *env) 216 var_help (int argc, char **argv, compose_env_t *env)
85 { 217 {
86 (void)env; 218 (void) env;
87 if (argc < 2) 219 if (argc < 2)
88 return util_help(mail_escape_table, NULL); 220 return util_help (mail_escape_table, NULL);
89 else 221 else
90 { 222 {
91 int status = 0; 223 int status = 0;
92 224
93 while (--argc) 225 while (--argc)
94 status |= util_help(mail_escape_table, *++argv); 226 status |= util_help (mail_escape_table, *++argv);
95 227
96 return status; 228 return status;
97 } 229 }
...@@ -101,166 +233,212 @@ var_help(int argc, char **argv, struct send_environ *env) ...@@ -101,166 +233,212 @@ var_help(int argc, char **argv, struct send_environ *env)
101 /* ~A */ 233 /* ~A */
102 /* ~a */ 234 /* ~a */
103 int 235 int
104 var_sign(int argc, char **argv, struct send_environ *env) 236 var_sign (int argc, char **argv, compose_env_t *env)
105 { 237 {
106 char *p; 238 char *p;
107 239
108 (void)argc; (void)env; 240 (void) argc;
241 (void) env;
109 242
110 if (util_getenv (&p, isupper (argv[0][0]) ? "Sign" : "sign", 243 if (util_getenv (&p, isupper (argv[0][0]) ? "Sign" : "sign",
111 Mail_env_string, 1) == 0) 244 Mail_env_string, 1) == 0)
112 { 245 {
113 if (isupper (argv[0][0])) 246 if (isupper (argv[0][0]))
114 { 247 {
115 char *name = util_fullpath (p); 248 char *name = util_fullpath (p);
116 FILE *fp = fopen(name, "r"); 249 FILE *fp = fopen (name, "r");
117 char *buf = NULL; 250 char *buf = NULL;
118 size_t n = 0; 251 size_t n = 0;
119 252
120 if (!fp) 253 if (!fp)
121 { 254 {
122 util_error("can't open %s: %s", name, strerror(errno)); 255 util_error ("can't open %s: %s", name, strerror (errno));
123 free(name); 256 free (name);
124 } 257 }
125 258
126 fprintf(stdout, "Reading %s\n", name); 259 fprintf (stdout, "Reading %s\n", name);
127 while (getline(&buf, &n, fp) > 0) 260 while (getline (&buf, &n, fp) > 0)
128 fprintf(ofile, "%s", buf); 261 fprintf (ofile, "%s", buf);
129 262
130 fclose(fp); 263 fclose (fp);
131 free(buf); 264 free (buf);
132 free(name); 265 free (name);
133 } 266 }
134 else 267 else
135 fprintf(ofile, "%s", p); 268 fprintf (ofile, "%s", p);
136 var_continue(); 269 var_continue ();
137 } 270 }
138 return 0; 271 return 0;
139 } 272 }
140 273
141 /* ~b[bcc-list] */ 274 /* ~b[bcc-list] */
142 int 275 int
143 var_bcc(int argc, char **argv, struct send_environ *env) 276 var_bcc (int argc, char **argv, compose_env_t *env)
144 { 277 {
145 while (--argc) 278 while (--argc)
146 { 279 compose_header_set (env, MU_HEADER_BCC, *++argv, COMPOSE_SINGLE_LINE);
147 util_strcat(&env->bcc, " ");
148 util_strcat(&env->bcc, *++argv);
149 }
150 return 0; 280 return 0;
151 } 281 }
152 282
153 /* ~c[cc-list] */ 283 /* ~c[cc-list] */
154 int 284 int
155 var_cc(int argc, char **argv, struct send_environ *env) 285 var_cc (int argc, char **argv, compose_env_t *env)
156 { 286 {
157 while (--argc) 287 while (--argc)
158 { 288 compose_header_set (env, MU_HEADER_CC, *++argv, COMPOSE_SINGLE_LINE);
159 util_strcat(&env->cc, " ");
160 util_strcat(&env->cc, *++argv);
161 }
162 return 0; 289 return 0;
163 } 290 }
164 291
165 /* ~d */ 292 /* ~d */
166 int 293 int
167 var_deadletter(int argc, char **argv, struct send_environ *env) 294 var_deadletter (int argc, char **argv, compose_env_t *env)
168 { 295 {
169 FILE *dead = fopen(getenv("DEAD"), "r"); 296 FILE *dead = fopen (getenv ("DEAD"), "r");
170 int c; 297 int c;
171 298
172 (void)argc; (void)argv; (void)env; 299 (void) argc;
300 (void) argv;
301 (void) env;
173 302
174 while ((c = fgetc(dead)) != EOF) 303 while ((c = fgetc (dead)) != EOF)
175 fputc(c, ofile); 304 fputc (c, ofile);
176 fclose(dead); 305 fclose (dead);
177 return 0; 306 return 0;
178 } 307 }
179 308
180 static int 309 static int
181 var_run_editor(char *ed, int argc, char **argv, struct send_environ *env) 310 var_run_editor (char *ed, int argc, char **argv, compose_env_t *env)
182 { 311 {
183 (void)argc; (void)argv; 312 if (!util_getenv (NULL, "editheaders", Mail_env_boolean, 0))
184 fclose(env->file); 313 {
185 ofile = env->ofile; 314 char *filename;
186 util_do_command("!%s %s", ed, env->filename); 315 int fd = mu_tempfile (NULL, &filename);
187 env->file = fopen(env->filename, "a+"); 316 FILE *fp = fdopen (fd, "w+");
188 ofile = env->file; 317 char buffer[512];
189 var_continue(); 318 int rc;
319
320 dump_headers (fp, env);
321
322 rewind (env->file);
323 while (fgets (buffer, sizeof (buffer), env->file))
324 fputs (buffer, fp);
325
326 fclose (env->file);
327
328 do
329 {
330 fclose (fp);
331 util_do_command ("!%s %s", ed, filename);
332 fp = fopen (filename, "r");
333 }
334 while ((rc = parse_headers (fp, env)) < 0);
335
336 if (rc == 0)
337 {
338 env->file = fopen (env->filename, "w");
339 while (fgets (buffer, sizeof (buffer), fp))
340 fputs (buffer, env->file);
341
342 fclose (env->file);
343 }
344 fclose (fp);
345 unlink (filename);
346 free (filename);
347 }
348 else
349 {
350 fclose (env->file);
351 ofile = env->ofile;
352 util_do_command ("!%s %s", ed, env->filename);
353 }
354
355 env->file = fopen (env->filename, "a+");
356 ofile = env->file;
357
358 var_continue ();
190 return 0; 359 return 0;
191 } 360 }
192 361
193 /* ~e */ 362 /* ~e */
194 int 363 int
195 var_editor(int argc, char **argv, struct send_environ *env) 364 var_editor (int argc, char **argv, compose_env_t *env)
196 { 365 {
197 return var_run_editor(getenv("EDITOR"), argc, argv, env); 366 return var_run_editor (getenv ("EDITOR"), argc, argv, env);
198 } 367 }
199 368
200 /* ~v */ 369 /* ~v */
201 int 370 int
202 var_visual(int argc, char **argv, struct send_environ *env) 371 var_visual (int argc, char **argv, compose_env_t *env)
203 { 372 {
204 return var_run_editor(getenv("VISUAL"), argc, argv, env); 373 return var_run_editor (getenv ("VISUAL"), argc, argv, env);
205 } 374 }
206 375
207 /* ~f[mesg-list] */ 376 /* ~f[mesg-list] */
208 /* ~F[mesg-list] */ 377 /* ~F[mesg-list] */
209 int 378 int
210 var_print(int argc, char **argv, struct send_environ *env) 379 var_print (int argc, char **argv, compose_env_t *env)
211 { 380 {
212 (void)env; 381 (void) env;
213 return mail_print(argc, argv); 382 return mail_print (argc, argv);
383 }
384
385 void
386 reread_header (compose_env_t *env, char *hdr, char *prompt)
387 {
388 char *p;
389 p = strdup (compose_header_get (env, hdr, ""));
390 ml_reread (prompt, &p);
391 compose_header_set (env, hdr, p, COMPOSE_REPLACE);
392 free (p);
214 } 393 }
215 394
216 /* ~h */ 395 /* ~h */
217 int 396 int
218 var_headers(int argc, char **argv, struct send_environ *env) 397 var_headers (int argc, char **argv, compose_env_t *env)
219 { 398 {
220 (void)argc; (void)argv; 399 reread_header (env, MU_HEADER_TO, "To: ");
221 ml_reread("To: ", &env->to); 400 reread_header (env, MU_HEADER_CC, "Cc: ");
222 ml_reread("Cc: ", &env->cc); 401 reread_header (env, MU_HEADER_BCC, "Bcc: ");
223 ml_reread("Bcc: ", &env->bcc); 402 reread_header (env, MU_HEADER_SUBJECT, "Subject: ");
224 ml_reread("Subject: ", &env->subj); 403 var_continue ();
225 var_continue();
226 return 0; 404 return 0;
227 } 405 }
228 406
229 /* ~i[var-name] */ 407 /* ~i[var-name] */
230 int 408 int
231 var_insert (int argc, char **argv, struct send_environ *send_env) 409 var_insert (int argc, char **argv, compose_env_t *send_env)
232 { 410 {
233 struct mail_env_entry *env; 411 struct mail_env_entry *env;
234 412
235 (void)send_env; 413 (void) send_env;
236 if (var_check_args (argc, argv)) 414 if (var_check_args (argc, argv))
237 return 1; 415 return 1;
238 env = util_find_env (argv[1], 0); 416 env = util_find_env (argv[1], 0);
239 if (env) 417 if (env)
240 switch (env->type) 418 switch (env->type)
241 { 419 {
242 case Mail_env_string: 420 case Mail_env_string:
243 fprintf (ofile, "%s", env->value.string); 421 fprintf (ofile, "%s", env->value.string);
244 break; 422 break;
245 423
246 case Mail_env_number: 424 case Mail_env_number:
247 fprintf (ofile, "%d", env->value.number); 425 fprintf (ofile, "%d", env->value.number);
248 break; 426 break;
249 427
250 case Mail_env_boolean: 428 case Mail_env_boolean:
251 fprintf (ofile, "%s", env->set ? "yes" : "no"); 429 fprintf (ofile, "%s", env->set ? "yes" : "no");
252 break; 430 break;
253 431
254 default: 432 default:
255 break; 433 break;
256 } 434 }
257 return 0; 435 return 0;
258 } 436 }
259 437
260 /* ~m[mesg-list] */ 438 /* ~m[mesg-list] */
261 /* ~M[mesg-list] */ 439 /* ~M[mesg-list] */
262 int 440 int
263 var_quote(int argc, char **argv, struct send_environ *env) 441 var_quote (int argc, char **argv, compose_env_t *env)
264 { 442 {
265 if (argc > 1) 443 if (argc > 1)
266 return util_msglist_esccmd (var_quote, argc, argv, env, 0); 444 return util_msglist_esccmd (var_quote, argc, argv, env, 0);
...@@ -275,139 +453,126 @@ var_quote(int argc, char **argv, struct send_environ *env) ...@@ -275,139 +453,126 @@ var_quote(int argc, char **argv, struct send_environ *env)
275 size_t n = 0; 453 size_t n = 0;
276 char *prefix = "\t"; 454 char *prefix = "\t";
277 455
278 if (mailbox_get_message(mbox, cursor, &mesg) != 0) 456 if (mailbox_get_message (mbox, cursor, &mesg) != 0)
279 return 1; 457 return 1;
280 458
281 fprintf(stdout, "Interpolating: %d\n", cursor); 459 fprintf (stdout, "Interpolating: %d\n", cursor);
282 460
283 util_getenv(&prefix, "indentprefix", Mail_env_string, 0); 461 util_getenv (&prefix, "indentprefix", Mail_env_string, 0);
284 462
285 if (islower(argv[0][0])) 463 if (islower (argv[0][0]))
286 { 464 {
287 size_t i, num = 0; 465 size_t i, num = 0;
288 char buf[512]; 466 char buf[512];
289 467
290 message_get_header(mesg, &hdr); 468 message_get_header (mesg, &hdr);
291 header_get_field_count(hdr, &num); 469 header_get_field_count (hdr, &num);
292 470
293 for (i = 1; i <= num; i++) 471 for (i = 1; i <= num; i++)
294 { 472 {
295 header_get_field_name(hdr, i, buf, sizeof buf, NULL); 473 header_get_field_name (hdr, i, buf, sizeof buf, NULL);
296 if (mail_header_is_visible(buf)) 474 if (mail_header_is_visible (buf))
297 { 475 {
298 fprintf(ofile, "%s%s: ", prefix, buf); 476 fprintf (ofile, "%s%s: ", prefix, buf);
299 header_get_field_value(hdr, i, buf, sizeof buf, NULL); 477 header_get_field_value (hdr, i, buf, sizeof buf, NULL);
300 fprintf(ofile, "%s\n", buf); 478 fprintf (ofile, "%s\n", buf);
301 } 479 }
302 } 480 }
303 fprintf(ofile, "\n"); 481 fprintf (ofile, "\n");
304 message_get_body(mesg, &body); 482 message_get_body (mesg, &body);
305 body_get_stream(body, &stream); 483 body_get_stream (body, &stream);
306 } 484 }
307 else 485 else
308 message_get_stream(mesg, &stream); 486 message_get_stream (mesg, &stream);
309 487
310 while (stream_readline(stream, buffer, sizeof buffer - 1, off, &n) == 0 488 while (stream_readline (stream, buffer, sizeof buffer - 1, off, &n) == 0
311 && n != 0) 489 && n != 0)
312 { 490 {
313 buffer[n] = '\0'; 491 buffer[n] = '\0';
314 fprintf(ofile, "%s%s", prefix, buffer); 492 fprintf (ofile, "%s%s", prefix, buffer);
315 off += n; 493 off += n;
316 } 494 }
317 var_continue(); 495 var_continue ();
318 } 496 }
319 return 0; 497 return 0;
320 } 498 }
321 499
322 /* ~p */ 500 /* ~p */
323 int 501 int
324 var_type_input(int argc, char **argv, struct send_environ *env) 502 var_type_input (int argc, char **argv, compose_env_t *env)
325 { 503 {
326 char buf[512]; 504 char buffer[512];
327
328 (void)argc; (void)argv;
329 505
330 fprintf(env->ofile, "Message contains:\n"); 506 fprintf (env->ofile, "Message contains:\n");
331 507
332 if (env->to) 508 dump_headers (env->ofile, env);
333 fprintf(env->ofile, "To: %s\n", env->to);
334 if (env->cc)
335 fprintf(env->ofile, "Cc: %s\n", env->cc);
336 if (env->bcc)
337 fprintf(env->ofile, "Bcc: %s\n", env->bcc);
338 if (env->subj)
339 fprintf(env->ofile, "Subject: %s\n\n", env->subj);
340 509
341 rewind(env->file); 510 rewind (env->file);
342 while (fgets(buf, sizeof(buf), env->file)) 511 while (fgets (buffer, sizeof (buffer), env->file))
343 fputs(buf, env->ofile); 512 fputs (buffer, env->ofile);
344 513
345 var_continue(); 514 var_continue ();
346 515
347 return 0; 516 return 0;
348 } 517 }
349 518
350 /* ~r[filename] */ 519 /* ~r[filename] */
351 int 520 int
352 var_read(int argc, char **argv, struct send_environ *env) 521 var_read (int argc, char **argv, compose_env_t *env)
353 { 522 {
354 char *filename; 523 char *filename;
355 FILE *inf; 524 FILE *inf;
356 size_t size, lines; 525 size_t size, lines;
357 char buf[512]; 526 char buf[512];
358 527
359 (void)env; 528 (void) env;
360 529
361 if (var_check_args (argc, argv)) 530 if (var_check_args (argc, argv))
362 return 1; 531 return 1;
363 filename = util_fullpath(argv[1]); 532 filename = util_fullpath (argv[1]);
364 inf = fopen(filename, "r"); 533 inf = fopen (filename, "r");
365 if (!inf) 534 if (!inf)
366 { 535 {
367 util_error("can't open %s: %s\n", filename, strerror(errno)); 536 util_error ("can't open %s: %s\n", filename, strerror (errno));
368 free(filename); 537 free (filename);
369 return 1; 538 return 1;
370 } 539 }
371 540
372 size = lines = 0; 541 size = lines = 0;
373 while (fgets(buf, sizeof(buf), inf)) 542 while (fgets (buf, sizeof (buf), inf))
374 { 543 {
375 lines++; 544 lines++;
376 size += strlen(buf); 545 size += strlen (buf);
377 fputs(buf, ofile); 546 fputs (buf, ofile);
378 } 547 }
379 fclose(inf); 548 fclose (inf);
380 fprintf(stdout, "\"%s\" %d/%d\n", filename, lines, size); 549 fprintf (stdout, "\"%s\" %d/%d\n", filename, lines, size);
381 free(filename); 550 free (filename);
382 return 0; 551 return 0;
383 } 552 }
384 553
385 /* ~s[string] */ 554 /* ~s[string] */
386 int 555 int
387 var_subj(int argc, char **argv, struct send_environ *env) 556 var_subj (int argc, char **argv, compose_env_t *env)
388 { 557 {
389 if (var_check_args (argc, argv)) 558 if (var_check_args (argc, argv))
390 return 1; 559 return 1;
391 free(env->subj); 560 compose_header_set (env, MU_HEADER_SUBJECT, argv[1], COMPOSE_REPLACE);
392 env->subj = strdup(argv[1]);
393 return 0; 561 return 0;
394 } 562 }
395 563
396 /* ~t[name-list] */ 564 /* ~t[name-list] */
397 int 565 int
398 var_to(int argc, char **argv, struct send_environ *env) 566 var_to (int argc, char **argv, compose_env_t *env)
399 { 567 {
400 while (--argc) 568 while (--argc)
401 { 569 compose_header_set (env, MU_HEADER_TO, *++argv, COMPOSE_SINGLE_LINE);
402 util_strcat(&env->to, " ");
403 util_strcat(&env->to, *++argv);
404 }
405 return 0; 570 return 0;
406 } 571 }
407 572
408 /* ~w[filename] */ 573 /* ~w[filename] */
409 int 574 int
410 var_write(int argc, char **argv, struct send_environ *env) 575 var_write (int argc, char **argv, compose_env_t *env)
411 { 576 {
412 char *filename; 577 char *filename;
413 FILE *fp; 578 FILE *fp;
...@@ -417,33 +582,33 @@ var_write(int argc, char **argv, struct send_environ *env) ...@@ -417,33 +582,33 @@ var_write(int argc, char **argv, struct send_environ *env)
417 if (var_check_args (argc, argv)) 582 if (var_check_args (argc, argv))
418 return 1; 583 return 1;
419 584
420 filename = util_fullpath(argv[1]); 585 filename = util_fullpath (argv[1]);
421 fp = fopen(filename, "w"); /*FIXME: check for the existence first */ 586 fp = fopen (filename, "w"); /*FIXME: check for the existence first */
422 587
423 if (!fp) 588 if (!fp)
424 { 589 {
425 util_error("can't open %s: %s\n", filename, strerror(errno)); 590 util_error ("can't open %s: %s\n", filename, strerror (errno));
426 free(filename); 591 free (filename);
427 return 1; 592 return 1;
428 } 593 }
429 594
430 rewind(env->file); 595 rewind (env->file);
431 size = lines = 0; 596 size = lines = 0;
432 while (fgets(buf, sizeof(buf), env->file)) 597 while (fgets (buf, sizeof (buf), env->file))
433 { 598 {
434 lines++; 599 lines++;
435 size += strlen(buf); 600 size += strlen (buf);
436 fputs(buf, fp); 601 fputs (buf, fp);
437 } 602 }
438 fclose(fp); 603 fclose (fp);
439 fprintf(stdout, "\"%s\" %d/%d\n", filename, lines, size); 604 fprintf (stdout, "\"%s\" %d/%d\n", filename, lines, size);
440 free(filename); 605 free (filename);
441 return 0; 606 return 0;
442 } 607 }
443 608
444 /* ~|[shell-command] */ 609 /* ~|[shell-command] */
445 int 610 int
446 var_pipe(int argc, char **argv, struct send_environ *env) 611 var_pipe (int argc, char **argv, compose_env_t *env)
447 { 612 {
448 int p[2]; 613 int p[2];
449 pid_t pid; 614 pid_t pid;
...@@ -451,13 +616,13 @@ var_pipe(int argc, char **argv, struct send_environ *env) ...@@ -451,13 +616,13 @@ var_pipe(int argc, char **argv, struct send_environ *env)
451 616
452 if (argc == 1) 617 if (argc == 1)
453 { 618 {
454 util_error("pipe: no command specified"); 619 util_error ("pipe: no command specified");
455 return 1; 620 return 1;
456 } 621 }
457 622
458 if (pipe(p)) 623 if (pipe (p))
459 { 624 {
460 util_error("pipe: %s", strerror(errno)); 625 util_error ("pipe: %s", strerror (errno));
461 return 1; 626 return 1;
462 } 627 }
463 628
...@@ -465,12 +630,12 @@ var_pipe(int argc, char **argv, struct send_environ *env) ...@@ -465,12 +630,12 @@ var_pipe(int argc, char **argv, struct send_environ *env)
465 if (fd == -1) 630 if (fd == -1)
466 return 1; 631 return 1;
467 632
468 if ((pid = fork()) < 0) 633 if ((pid = fork ()) < 0)
469 { 634 {
470 close(p[0]); 635 close (p[0]);
471 close(p[1]); 636 close (p[1]);
472 close(fd); 637 close (fd);
473 util_error("fork: %s", strerror(errno)); 638 util_error ("fork: %s", strerror (errno));
474 return 1; 639 return 1;
475 } 640 }
476 else if (pid == 0) 641 else if (pid == 0)
...@@ -480,23 +645,23 @@ var_pipe(int argc, char **argv, struct send_environ *env) ...@@ -480,23 +645,23 @@ var_pipe(int argc, char **argv, struct send_environ *env)
480 char **xargv; 645 char **xargv;
481 646
482 /* Attache the pipes */ 647 /* Attache the pipes */
483 close(0); 648 close (0);
484 dup(p[0]); 649 dup (p[0]);
485 close(p[0]); 650 close (p[0]);
486 close(p[1]); 651 close (p[1]);
487 652
488 close(1); 653 close (1);
489 dup(fd); 654 dup (fd);
490 close(fd); 655 close (fd);
491 656
492 /* Execute the process */ 657 /* Execute the process */
493 xargv = xcalloc(argc, sizeof(xargv[0])); 658 xargv = xcalloc (argc, sizeof (xargv[0]));
494 for (i = 0; i < argc-1; i++) 659 for (i = 0; i < argc - 1; i++)
495 xargv[i] = argv[i+1]; 660 xargv[i] = argv[i + 1];
496 xargv[i] = NULL; 661 xargv[i] = NULL;
497 execvp(xargv[0], xargv); 662 execvp (xargv[0], xargv);
498 util_error("cannot exec process `%s': %s", xargv[0], strerror(errno)); 663 util_error ("cannot exec process `%s': %s", xargv[0], strerror (errno));
499 exit(1); 664 exit (1);
500 } 665 }
501 else 666 else
502 { 667 {
...@@ -507,74 +672,74 @@ var_pipe(int argc, char **argv, struct send_environ *env) ...@@ -507,74 +672,74 @@ var_pipe(int argc, char **argv, struct send_environ *env)
507 int rc = 1; 672 int rc = 1;
508 int status; 673 int status;
509 674
510 close(p[0]); 675 close (p[0]);
511 676
512 /* Parent */ 677 /* Parent */
513 fp = fdopen(p[1], "w"); 678 fp = fdopen (p[1], "w");
514 679
515 fclose(env->file); 680 fclose (env->file);
516 env->file = fopen(env->filename, "r"); 681 env->file = fopen (env->filename, "r");
517 682
518 lines = size = 0; 683 lines = size = 0;
519 while (getline(&buf, &n, env->file) > 0) 684 while (getline (&buf, &n, env->file) > 0)
520 { 685 {
521 lines++; 686 lines++;
522 size += n; 687 size += n;
523 fputs(buf, fp); 688 fputs (buf, fp);
524 } 689 }
525 fclose(env->file); 690 fclose (env->file);
526 fclose(fp); /* Closes p[1] */ 691 fclose (fp); /* Closes p[1] */
527 692
528 waitpid(pid, &status, 0); 693 waitpid (pid, &status, 0);
529 if (!WIFEXITED(status)) 694 if (!WIFEXITED (status))
530 { 695 {
531 util_error("child terminated abnormally: %d", WEXITSTATUS(status)); 696 util_error ("child terminated abnormally: %d", WEXITSTATUS (status));
532 } 697 }
533 else 698 else
534 { 699 {
535 struct stat st; 700 struct stat st;
536 if (fstat(fd, &st)) 701 if (fstat (fd, &st))
537 { 702 {
538 util_error("can't stat output file: %s", strerror(errno)); 703 util_error ("can't stat output file: %s", strerror (errno));
539 } 704 }
540 else if (st.st_size > 0) 705 else if (st.st_size > 0)
541 rc = 0; 706 rc = 0;
542 } 707 }
543 708
544 fprintf(stdout, "\"|%s\" in: %d/%d ", argv[1], lines, size); 709 fprintf (stdout, "\"|%s\" in: %d/%d ", argv[1], lines, size);
545 if (rc) 710 if (rc)
546 { 711 {
547 fprintf(stdout, "no lines out\n"); 712 fprintf (stdout, "no lines out\n");
548 } 713 }
549 else 714 else
550 { 715 {
551 /* Ok, replace the old tempfile */ 716 /* Ok, replace the old tempfile */
552 fp = fdopen(fd, "r"); 717 fp = fdopen (fd, "r");
553 rewind(fp); 718 rewind (fp);
554 719
555 env->file = fopen(env->filename, "w+"); 720 env->file = fopen (env->filename, "w+");
556 721
557 lines = size = 0; 722 lines = size = 0;
558 while (getline(&buf, &n, fp) > 0) 723 while (getline (&buf, &n, fp) > 0)
559 { 724 {
560 lines++; 725 lines++;
561 size += n; 726 size += n;
562 fputs(buf, env->file); 727 fputs (buf, env->file);
563 } 728 }
564 fclose(env->file); 729 fclose (env->file);
565 730
566 fprintf(stdout, "out: %d/%d\n", lines, size); 731 fprintf (stdout, "out: %d/%d\n", lines, size);
567 } 732 }
568 733
569 /* Clean up the things */ 734 /* Clean up the things */
570 if (buf) 735 if (buf)
571 free(buf); 736 free (buf);
572 737
573 env->file = fopen(env->filename, "a+"); 738 env->file = fopen (env->filename, "a+");
574 ofile = env->file; 739 ofile = env->file;
575 } 740 }
576 741
577 close(fd); 742 close (fd);
578 743
579 return 0; 744 return 0;
580 } 745 }
......