📄 respond.c
字号:
/* $Id: respond.c,v 3.0 1992/02/01 03:09:32 davison Trn $ *//* This software is Copyright 1991 by Stan Barber. * * Permission is hereby granted to copy, reproduce, redistribute or otherwise * use this software as long as: there is no monetary profit gained * specifically from the use or reproduction or this software, it is not * sold, rented, traded or otherwise marketed, and this copyright notice is * included prominently in any copy made. * * The author make no claims as to the fitness or correctness of this software * for any use whatsoever, and it is provided as is. Any use of this software * is at the user's own risk. */#include "EXTERN.h"#include "common.h"#include "intrp.h"#include "cache.h"#include "head.h"#include "term.h"#include "ngdata.h"#include "ng.h"#include "util.h"#include "trn.h"#include "artio.h"#include "final.h"#include "decode.h"#include "INTERN.h"#include "respond.h"static char nullart[] = "\nNull article\n";bool cut_line();voidrespond_init(){ ;}intsave_article(){ bool_int use_pref; register char *s, *c; char altbuf[CBUFLEN]; int iter; bool interactive = (buf[1] == FINISHCMD); char cmd = *buf; if (!finish_command(interactive)) /* get rest of command */ return SAVE_ABORT; if ((use_pref = isupper(cmd)) != 0) cmd = tolower(cmd); parseheader(art);#ifdef METAMAIL savefrom = (!mime_article && (cmd == 'w' || cmd == 'e'))#else savefrom = (cmd == 'w' || cmd == 'e')#endif ? htype[PAST_HEADER].ht_minpos : 0; if (artopen(art) == Nullfp) {#ifdef VERBOSE IF(verbose) fputs("\n\Saving null articles is not very productive! :-)\n\",stdout) FLUSH; ELSE#endif#ifdef TERSE fputs(nullart,stdout) FLUSH;#endif return SAVE_DONE; } if (chdir(cwd)) { printf(nocd,cwd) FLUSH; sig_catcher(0); } if (cmd == 'e') { /* is this an extract command? */ static bool custom_extract = FALSE; int cnt = 0; bool found_cut = FALSE; char art_buf[LBUFLEN], *cmdstr; s = buf+1; /* skip e */ while (*s == ' ') s++; /* skip leading spaces */ safecpy(altbuf,filexp(s),sizeof altbuf); s = altbuf; if (*s) { cmdstr = cpytill(buf,s,'|'); /* check for | */ s = buf + strlen(buf)-1; while (*s == ' ') s--; /* trim trailing spaces */ *++s = '\0'; if (*cmdstr) { s = cmdstr+1; /* skip | */ while (*s == ' ') s++; if (*s) { /* if new command, use it */ if (extractprog) free(extractprog); extractprog = savestr(s); /* put extracter in %e */ } else cmdstr = extractprog; } else cmdstr = Nullch; s = buf; } else { if (extractdest) strcpy(s, extractdest); if (custom_extract) cmdstr = extractprog; else cmdstr = Nullch; } if (cmdstr) { if (strEQ(extractprog,"-")) cmdstr = Nullch; else if (decode_fp != Nullfp) decode_end(); } custom_extract = (cmdstr != 0); fseek(artfp,savefrom,0); if (*s != '/') { /* relative path? */ c = (s==buf ? altbuf : buf); interp(c, (sizeof buf), getval("SAVEDIR",SAVEDIR)); if (makedir(c,MD_DIR)) /* ensure directory exists */ strcpy(c,cwd); if (*s) { while (*c) c++; *c++ = '/'; strcpy(c,s); /* add filename */ } s = (s==buf ? altbuf : buf); } if (*s != '/') { /* path still relative? */ c = (s==buf ? altbuf : buf); sprintf(c, "%s/%s", cwd, s); s = c; /* absolutize it */ } if (decode_fp != Nullfp) { printf("Continuing %s:%s\n", decode_fname, cmd != '\0' && strNE(extractdest,s) ? " (Ignoring conflicting directory)" : nullstr ) FLUSH; if (decode_type == UUDECODE) uudecode(artfp); else unship(artfp); } else { if (extractdest) free(extractdest); s = extractdest = savestr(s); /* make it handy for %E */ if (makedir(s, MD_DIR)) { /* ensure directory exists */ int_count++; return SAVE_DONE; } if (chdir(s)) { printf(nocd,s) FLUSH; sig_catcher(0); } s = getwd(buf); /* simplify path for output */ while(fgets(art_buf,LBUFLEN,artfp) != Nullch) { if (*art_buf <= ' ') continue; /* Ignore empty or initially-whitespace lines */#ifdef METAMAIL if (mime_article) { if (!custom_extract) { printf("Extracting MIME article into %s:\n", s) FLUSH; extractprog = savestr(filexp(getval("MIMESTORE",MIMESTORE))); } else printf("Extracting MIME article into %s using %s\n", s, extractprog) FLUSH; cnt = 0; interp(cmd_buf, sizeof cmd_buf, getval("EXMIMESAVER",EXMIMESAVER)); termlib_reset(); resetty(); /* restore tty state */ doshell(SH,cmd_buf); noecho(); /* revert to cbreaking */ crmode(); termlib_init(); break; }#endif if (found_cut && custom_extract) { printf("Extracting data into %s using %s:\n", s, extractprog) FLUSH; goto extract_it; } if (((*art_buf == '#' || *art_buf == ':') && (strnEQ(art_buf+1, "! /bin/sh", 9) || strnEQ(art_buf+1, "!/bin/sh", 8) || strnEQ(art_buf+2, "This is ", 8))) || strnEQ(art_buf, "sed ", 4) || strnEQ(art_buf, "cat ", 4) || strnEQ(art_buf, "echo ", 5)) { fseek(artfp,-(long)strlen(art_buf)-NL_SIZE+1,1); savefrom = ftell(artfp); if (custom_extract) { printf("Extracting shar into %s using %s:\n", s, extractprog) FLUSH; goto extract_it; } /* Check for special-case of shar'ed-uuencoded file */ while(fgets(art_buf,LBUFLEN,artfp) != Nullch) { if (*art_buf == '#' || *art_buf == ':' || strnEQ(art_buf, "echo ", 5) || strnEQ(art_buf, "sed ", 4)) continue; if (strnEQ(art_buf, "Xbegin ", 7)) { decode_type = UUDECODE; goto decode_it; } break; } printf("Extracting shar into %s:\n", s) FLUSH; if (extractprog) free(extractprog); extractprog = savestr(filexp(getval("UNSHAR",UNSHAR))); extract_it: cnt = 0; interp(cmd_buf,(sizeof cmd_buf),getval("EXSAVER",EXSAVER)); termlib_reset(); resetty(); /* restore tty state */ doshell(SH,cmd_buf); noecho(); /* revert to cbreaking */ crmode(); termlib_init(); break; } else if (!custom_extract && (strEQ(art_buf,"$\n") || strEQ(art_buf,"$ f\n"))) { savefrom = ftell(artfp)-strlen(art_buf)-NL_SIZE+1; if (found_cut || (fgets(art_buf,LBUFLEN,artfp) != Nullch && (strnEQ(art_buf, "ship ", 5) || strnEQ(art_buf, "cont ", 5)))) { decode_type = UNSHIP; goto decode_it; } } else if (!custom_extract && (strEQ(art_buf,"table\n") || strnEQ(art_buf,"begin ", 6))) { decode_type = UUDECODE; savefrom = ftell(artfp)-strlen(art_buf)-NL_SIZE+1; decode_it: printf("Extracting %s file into %s:\n", decode_type == UNSHIP? "shipped":"uuencoded", s) FLUSH; if (extractprog) free(extractprog); extractprog = savestr("-"); fseek(artfp, savefrom, 0); cnt = 0; if (decode_type == UUDECODE) { uud_start(); uudecode(artfp); } else unship(artfp); break; } else { if (cut_line(art_buf)) { savefrom = ftell(artfp); found_cut = TRUE; } else if (found_cut || ++cnt == 300) { break; } } }/* while */ if (cnt) { if (custom_extract) printf("Didn't find cut line for extraction to '%s'.\n", extractprog) FLUSH; else printf("Unable to determine type of file.\n") FLUSH; } }/* if */ } else if ((s = index(buf,'|')) != Nullch) { /* is it a pipe command? */ s++; /* skip the | */ while (*s == ' ') s++; safecpy(altbuf,filexp(s),sizeof altbuf); if (savedest) free(savedest); savedest = savestr(altbuf); interp(cmd_buf, (sizeof cmd_buf), getval("PIPESAVER",PIPESAVER)); /* then set up for command */ termlib_reset(); resetty(); /* restore tty state */ if (use_pref) /* use preferred shell? */ doshell(Nullch,cmd_buf); /* do command with it */ else doshell(sh,cmd_buf); /* do command with sh */ noecho(); /* and stop echoing */ crmode(); /* and start cbreaking */ termlib_init(); } else { /* normal save */ bool there, mailbox; char *savename = getval("SAVENAME",SAVENAME); s = buf+1; /* skip s or S */ if (*s == '-') { /* if they are confused, skip - also */#ifdef VERBOSE IF(verbose) fputs("Warning: '-' ignored. This isn't readnews.\n",stdout) FLUSH; ELSE#endif#ifdef TERSE fputs("'-' ignored.\n",stdout) FLUSH;#endif s++; } for (; *s == ' '; s++); /* skip spaces */ safecpy(altbuf,filexp(s),sizeof altbuf); s = altbuf; if (*s != '/') { interp(buf, (sizeof buf), getval("SAVEDIR",SAVEDIR)); if (makedir(buf,MD_DIR)) /* ensure directory exists */ strcpy(buf,cwd); if (*s) { for (c = buf; *c; c++) ; *c++ = '/'; strcpy(c,s); /* add filename */ } s = buf; } for (iter = 0; (there = stat(s,&filestat) >= 0) && (filestat.st_mode & S_IFDIR); iter++) { /* is it a directory? */ c = (s+strlen(s)); *c++ = '/'; /* put a slash before filename */ interp(c, s==buf?(sizeof buf):(sizeof altbuf), iter ? "News" : savename ); /* generate a default name somehow or other */ } makedir(s,MD_FILE); if (*s != '/') { /* relative path? */ c = (s==buf ? altbuf : buf); sprintf(c, "%s/%s", cwd, s); s = c; /* absolutize it */ } if (savedest) free(savedest); s = savedest = savestr(s); /* doesn't move any more */ /* make it handy for %b */ if (!there) { if (mbox_always) mailbox = TRUE; else if (norm_always) mailbox = FALSE; else { char *dflt = (instr(savename,"%a", TRUE) ? "nyq" : "ynq"); sprintf(cmd_buf, "\nFile %s doesn't exist--\n use mailbox format? [%s] ", s,dflt); reask_save: in_char(cmd_buf, 'M'); putchar('\n') FLUSH; setdef(buf,dflt);#ifdef VERIFY printcmd();#endif if (*buf == 'h') {#ifdef VERBOSE IF(verbose) printf("\n\Type y to create %s as a mailbox.\n\Type n to create it as a normal file.\n\Type q to abort the save.\n\",s) FLUSH; ELSE#endif#ifdef TERSE fputs("\n\y to create mailbox.\n\n to create normal file.\n\q to abort.\n\",stdout) FLUSH;#endif goto reask_save; } else if (*buf == 'n') { mailbox = FALSE; } else if (*buf == 'y') { mailbox = TRUE; } else if (*buf == 'q') { goto s_bomb; } else { fputs(hforhelp,stdout) FLUSH; settle_down(); goto reask_save; } } } else if (filestat.st_mode & S_IFCHR) mailbox = FALSE; else { int tmpfd; tmpfd = open(s,0); if (tmpfd == -1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -