]>
diplodocus.org Git - nmh/blob - uip/msh.c
3 * msh.c -- The nmh shell
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 * Keep more status information in maildrop map
17 #include <h/signals.h>
18 #include <h/dropsbr.h>
19 #include <h/fmt_scan.h>
20 #include <h/scansbr.h>
33 #define QUOTE '\\' /* sigh */
35 #define MSH_SWITCHES \
36 X("idstart number", -7, IDSW) /* interface from bbc */ \
37 X("idstop number", -6, FDSW) /* .. */ \
38 X("idquit number", -6, QDSW) /* .. */ \
39 X("idname BBoard", -6, NMSW) /* .. */ \
40 X("prompt string", 0, PRMPTSW) \
41 X("scan", 0, SCANSW) \
42 X("noscan", 0, NSCANSW) \
43 X("vmhread fd", -7, READSW) \
44 X("vmhwrite fd", -8, WRITESW) \
45 X("popread fd", -7, PREADSW) \
46 X("popwrite fd", -8, PWRITSW) \
47 X("topcur", 0, TCURSW) \
48 X("notopcur", 0, NTCURSW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(MSH
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(MSH
, switches
);
60 static int mbx_style
= MMDF_FORMAT
;
65 char*fmsh
= NULL
; /* folder instead of file */
66 int modified
; /* command modified folder */
67 struct msgs
*mp
; /* used a lot */
69 struct Msg
*Msgs
= NULL
; /* Msgs[0] not used */
70 static FILE *fp
; /* input file */
71 static FILE *yp
= NULL
; /* temporary file */
72 static int mode
; /* mode of file */
73 static int numfds
= 0; /* number of files cached */
74 static int maxfds
= 0; /* number of files cached to be cached */
75 static time_t mtime
= (time_t) 0; /* mtime of file */
80 #define ALARM ((unsigned int) 10)
81 #define ttyN(c) ttyNaux ((c), NULL)
85 static int vmhpid
= OK
;
90 static int vmhtty
= NOTOK
;
97 static int topcur
= 0;
99 static int numwins
= 0;
100 static int windows
[NWIN
+ 1];
102 static jmp_buf peerenv
;
107 static int pfd
= NOTOK
; /* fd parent is reading from */
108 static int ppid
= 0; /* pid of parent */
113 int interactive
; /* running from a /dev/tty */
114 int redirected
; /* re-directing output */
115 FILE *sp
= NULL
; /* original stdout */
117 char *cmd_name
; /* command being run */
118 char myfilter
[BUFSIZ
]; /* path to mhl.forward */
120 static char *myprompt
= "(%s) ";/* prompting string */
125 static int gap
; /* gap in BBoard-ID:s */
126 static char *myname
= NULL
; /* BBoard name */
127 char *BBoard_ID
= "BBoard-ID"; /* BBoard-ID constant */
132 SIGNAL_HANDLER istat
; /* original SIGINT */
133 static SIGNAL_HANDLER pstat
; /* current SIGPIPE */
134 SIGNAL_HANDLER qstat
; /* original SIGQUIT */
137 SIGNAL_HANDLER tstat
; /* original SIGTSTP */
140 int interrupted
; /* SIGINT detected */
141 int broken_pipe
; /* SIGPIPE detected */
142 int told_to_quit
; /* SIGQUIT detected */
147 void fsetup (char *);
151 void display_info (int);
154 void seq_setcur (struct msgs
*, int);
155 void padios (char *, char *, ...);
156 void padvise (char *, char *, ...);
162 static void msh (int);
163 static int read_map (char *, long);
164 static int read_file (long, int);
166 static void m_gMsgs (int);
167 static int check_folder (int);
168 static void scanrange (int, int);
169 static void scanstring (char *);
170 static void write_ids (void);
171 static void quit (void);
172 static int getargs (char *, struct swit
*, struct Cmd
*);
173 static int getcmds (struct swit
*, struct Cmd
*, int);
174 static int parse (char *, struct Cmd
*);
175 static int init_io (struct Cmd
*, int);
176 static int initaux_io (struct Cmd
*);
177 static void fin_io (struct Cmd
*, int);
178 static void finaux_io (struct Cmd
*);
179 static void m_init (void);
180 static void intrser (int);
181 static void pipeser (int);
182 static void quitser (int);
183 static void alrmser (int);
184 static int pINI (void);
185 static int pQRY (char *, int);
186 static int pQRY1 (int);
187 static int pQRY2 (void);
188 static int pCMD (char *, struct swit
*, struct Cmd
*);
189 static int pFIN (void);
190 static int peerwait (void);
191 static int ttyNaux (struct Cmd
*, char *);
192 static int ttyR (struct Cmd
*);
193 static int winN (struct Cmd
*, int, int);
194 static int winR (struct Cmd
*);
195 static int winX (int);
199 main (int argc
, char **argv
)
201 int id
= 0, scansw
= 0, vmh1
= 0, vmh2
= 0;
202 char *cp
, *file
= NULL
, *folder
= NULL
;
203 char **argp
, **arguments
, buf
[BUFSIZ
];
206 setlocale(LC_ALL
, "");
208 invo_name
= r1bindex (argv
[0], '/');
210 /* read user profile/context */
213 mts_init (invo_name
);
214 arguments
= getarguments (invo_name
, argc
,argv
, 1);
217 while ((cp
= *argp
++)) {
219 switch (smatch (++cp
, switches
)) {
221 ambigsw (cp
, switches
);
224 adios (NULL
, "-%s unknown", cp
);
227 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
228 print_help (buf
, switches
, 1);
231 print_version(invo_name
);
235 if (!(cp
= *argp
++) || *cp
== '-')
236 adios (NULL
, "missing argument to %s", argp
[-2]);
237 if ((id
= atoi (cp
)) < 1)
238 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
241 if (!(cp
= *argp
++) || *cp
== '-')
242 adios (NULL
, "missing argument to %s", argp
[-2]);
243 if ((pfd
= atoi (cp
)) <= 1)
244 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
247 if (!(cp
= *argp
++) || *cp
== '-')
248 adios (NULL
, "missing argument to %s", argp
[-2]);
249 if ((ppid
= atoi (cp
)) <= 1)
250 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
253 if (!(myname
= *argp
++) || *myname
== '-')
254 adios (NULL
, "missing argument to %s", argp
[-2]);
265 if (!(myprompt
= *argp
++) || *myprompt
== '-')
266 adios (NULL
, "missing argument to %s", argp
[-2]);
270 if (!(cp
= *argp
++) || *cp
== '-')
271 adios (NULL
, "missing argument to %s", argp
[-2]);
272 if ((vmh1
= atoi (cp
)) < 1)
273 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
276 if (!(cp
= *argp
++) || *cp
== '-')
277 adios (NULL
, "missing argument to %s", argp
[-2]);
278 if ((vmh2
= atoi (cp
)) < 1)
279 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
283 if (!(cp
= *argp
++) || *cp
== '-')
284 adios (NULL
, "missing argument to %s", argp
[-2]);
287 if (!(cp
= *argp
++) || *cp
== '-')
288 adios (NULL
, "missing argument to %s", argp
[-2]);
298 if (*cp
== '+' || *cp
== '@') {
300 adios (NULL
, "only one folder at a time!");
302 folder
= pluspath (cp
);
306 adios (NULL
, "only one file at a time!");
311 if (!file
&& !folder
)
314 adios (NULL
, "use a file or a folder, not both");
315 strncpy (myfilter
, etcpath (mhlforward
), sizeof(myfilter
));
318 ioctl (pfd
, FIOCLEX
, NULL
);
321 istat
= SIGNAL2 (SIGINT
, intrser
);
322 qstat
= SIGNAL2 (SIGQUIT
, quitser
);
324 sc_width (); /* MAGIC... */
326 if ((vmh
= vmh1
&& vmh2
)) {
329 SIGNAL (SIGINT
, SIG_IGN
);
330 SIGNAL (SIGQUIT
, SIG_IGN
);
332 tstat
= SIGNAL (SIGTSTP
, SIG_IGN
);
341 display_info (id
> 0 ? scansw
: 0);
343 msh (id
> 0 ? scansw
: 0);
352 #define MSHCMDS_SWITCHES \
353 X("advance", -7, ADVCMD) \
354 X("ali", 0, ALICMD) \
355 X("burst", 0, EXPLCMD) \
356 X("comp", 0, COMPCMD) \
357 X("dist", 0, DISTCMD) \
358 X("exit", 0, EXITCMD) \
359 X("folder", 0, FOLDCMD) \
360 X("forw", 0, FORWCMD) \
361 X("help", 0, HELPCMD) \
363 X("mark", 0, MARKCMD) \
364 X("mhmail", 0, MAILCMD) \
365 X("mhn", 0, MHNCMD) \
366 X("msgchk", 0, MSGKCMD) \
367 X("next", 0, NEXTCMD) \
368 X("packf", 0, PACKCMD) \
369 X("pick", 0, PICKCMD) \
370 X("prev", 0, PREVCMD) \
371 X("quit", 0, QUITCMD) \
372 X("refile", 0, FILECMD) \
373 X("repl", 0, REPLCMD) \
374 X("rmm", 0, RMMCMD) \
375 X("scan", 0, SCANCMD) \
376 X("send", 0, SENDCMD) \
377 X("show", 0, SHOWCMD) \
378 X("sortm", 0, SORTCMD) \
379 X("whatnow", 0, WHATCMD) \
380 X("whom", 0, WHOMCMD) \
382 #define X(sw, minchars, id) id,
383 DEFINE_SWITCH_ENUM(MSHCMDS
);
386 #define X(sw, minchars, id) { sw, minchars, id },
387 DEFINE_SWITCH_ARRAY(MSHCMDS
, mshcmds
);
395 register char *cp
, **ap
;
396 char prompt
[BUFSIZ
], *vec
[MAXARGS
];
398 register struct Cmd
*cmdp
;
399 static int once_only
= ADVCMD
;
401 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
410 if ((i
= getcmds (mshcmds
, cmdp
, scansw
)) == EOF
) {
415 check_folder (scansw
);
416 if ((i
= getargs (prompt
, mshcmds
, cmdp
)) == EOF
) {
421 cmd_name
= mshcmds
[i
].sw
;
429 if (once_only
== ADVCMD
)
430 once_only
= i
= SHOWCMD
;
432 i
= mp
->curmsg
!= mp
->hghmsg
? NEXTCMD
: EXITCMD
;
433 cmd_name
= mshcmds
[i
].sw
;
439 case FORWCMD
: /* sigh */
449 if ((cp
= context_find (cmd_name
))) {
451 ap
= brkstring (cp
, " ", "\n");
452 ap
= copyip (ap
, vec
, MAXARGS
);
463 copyip (cmdp
->args
+ 1, ap
, MAXARGS
);
467 if (!vmh
&& init_io (cmdp
, vmh
) == NOTOK
) {
473 redirected
= vmh
|| cmdp
->direction
!= STDIO
;
484 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
485 forkcmd (vec
, cmd_name
);
489 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
494 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
500 || (filehak (vec
) == OK
? ttyN (cmdp
)
501 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
506 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
511 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
516 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
522 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
527 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
534 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
540 || (packhak (vec
) == OK
? ttyN (cmdp
)
541 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
546 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
551 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
556 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
561 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
566 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
571 padios (NULL
, "no dispatch for %s", cmd_name
);
593 fsetup (char *folder
)
599 maildir
= m_maildir (folder
);
600 if (chdir (maildir
) == NOTOK
)
601 padios (maildir
, "unable to change directory to");
603 /* read folder and create message structure */
604 if (!(mp
= folder_read (folder
)))
605 padios (NULL
, "unable to read folder %s", folder
);
607 /* check for empty folder */
609 padios (NULL
, "no messages in %s", folder
);
612 mtime
= stat (mp
->foldpath
, &st
) != NOTOK
? st
.st_mtime
: 0;
614 m_gMsgs (mp
->hghmsg
);
616 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
617 Msgs
[msgnum
].m_bboard_id
= 0;
618 Msgs
[msgnum
].m_top
= NOTOK
;
619 Msgs
[msgnum
].m_start
= Msgs
[msgnum
].m_stop
= 0L;
620 Msgs
[msgnum
].m_scanl
= NULL
;
625 fmsh
= getcpy (folder
);
627 maxfds
= OPEN_MAX
/ 2;
629 if ((maxfds
-= 2) < 1)
639 if ((fp
= fopen (file
, "r")) == NULL
)
640 padios (file
, "unable to read");
642 ioctl (fileno (fp
), FIOCLEX
, NULL
);
644 if (fstat (fileno (fp
), &st
) != NOTOK
) {
645 mode
= (int) (st
.st_mode
& 0777), mtime
= st
.st_mtime
;
646 msgp
= read_map (file
, (long) st
.st_size
);
649 mode
= m_gmprot (), mtime
= 0;
653 if ((msgp
= read_file (msgp
? Msgs
[msgp
].m_stop
: 0L, msgp
+ 1)) < 1)
654 padios (NULL
, "no messages in %s", myname
? myname
: file
);
656 if (!(mp
= (struct msgs
*) calloc ((size_t) 1, sizeof(*mp
))))
657 padios (NULL
, "unable to allocate folder storage");
659 if (!(mp
->msgstats
= calloc ((size_t) msgp
+ 3, sizeof(*(mp
->msgstats
)))))
660 padios (NULL
, "unable to allocate message status storage");
666 mp
->foldpath
= getcpy (myname
? myname
: file
);
667 clear_folder_flags (mp
);
670 if (st
.st_uid
!= getuid () || access (file
, W_OK
) == NOTOK
)
674 mp
->hghoff
= mp
->hghmsg
+ 1;
676 for (i
= mp
->lowmsg
; i
<= mp
->hghmsg
; i
++) {
677 clear_msg_flags (mp
, i
);
682 mp
->msgattrs
[0] = getcpy ("unseen");
683 mp
->msgattrs
[1] = NULL
;
685 m_unknown (fp
); /* the MAGIC invocation */
694 read_map (char *file
, long size
)
696 register int i
, msgp
;
697 register struct drop
*dp
, *mp
;
700 if ((i
= map_read (file
, size
, &rp
, 1)) == 0)
706 for (dp
= rp
+ 1; i
-- > 0; msgp
++, dp
++) {
707 mp
= &Msgs
[msgp
].m_drop
;
709 mp
->d_size
= dp
->d_size
;
710 mp
->d_start
= dp
->d_start
;
711 mp
->d_stop
= dp
->d_stop
;
712 Msgs
[msgp
].m_scanl
= NULL
;
721 read_file (long pos
, int msgp
)
724 register struct drop
*dp
, *mp
;
727 if ((i
= mbx_read (fp
, pos
, &rp
, 1)) <= 0)
730 m_gMsgs ((msgp
- 1) + i
);
732 for (dp
= rp
; i
-- > 0; msgp
++, dp
++) {
733 mp
= &Msgs
[msgp
].m_drop
;
735 mp
->d_size
= dp
->d_size
;
736 mp
->d_start
= dp
->d_start
;
737 mp
->d_stop
= dp
->d_stop
;
738 Msgs
[msgp
].m_scanl
= NULL
;
752 nMsgs
= n
+ MAXFOLDER
/ 2;
753 Msgs
= (struct Msg
*) calloc ((size_t) (nMsgs
+ 2), sizeof *Msgs
);
755 padios (NULL
, "unable to allocate Msgs structure");
762 nmsgs
= nMsgs
+ n
+ MAXFOLDER
/ 2;
763 Msgs
= (struct Msg
*) mh_xrealloc ((char *) Msgs
, (size_t) (nmsgs
+ 2) * sizeof *Msgs
);
764 memset((char *) (Msgs
+ nMsgs
+ 2), 0, (size_t) ((nmsgs
- nMsgs
) * sizeof *Msgs
));
771 msh_ready (int msgnum
, int full
)
784 if ((fd
= Msgs
[msgnum
].m_top
) == NOTOK
) {
785 if (numfds
>= maxfds
)
786 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++)
787 if (Msgs
[msgp
].m_top
!= NOTOK
) {
788 close (Msgs
[msgp
].m_top
);
789 Msgs
[msgp
].m_top
= NOTOK
;
794 if ((fd
= open (cp
= m_name (msgnum
), O_RDONLY
)) == NOTOK
)
795 padios (cp
, "unable to open message");
796 Msgs
[msgnum
].m_top
= fd
;
800 if ((fd
= dup (fd
)) == NOTOK
)
801 padios ("cached message", "unable to dup");
802 if ((yp
= fdopen (fd
, "r")) == NULL
)
803 padios (NULL
, "unable to fdopen cached message");
804 fseek (yp
, 0L, SEEK_SET
);
808 m_eomsbr ((int (*)()) 0); /* XXX */
809 fseek (fp
, Msgs
[msgnum
].m_start
, SEEK_SET
);
815 check_folder (int scansw
)
817 int seqnum
, i
, low
, hgh
, msgp
;
821 if (stat (mp
->foldpath
, &st
) == NOTOK
)
822 padios (mp
->foldpath
, "unable to stat");
823 if (mtime
== st
.st_mtime
)
827 low
= mp
->hghmsg
+ 1;
828 folder_free (mp
); /* free folder/message structure */
830 if (!(mp
= folder_read (fmsh
)))
831 padios (NULL
, "unable to re-read folder %s", fmsh
);
835 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++) {
836 if (Msgs
[msgp
].m_top
!= NOTOK
) {
837 close (Msgs
[msgp
].m_top
);
838 Msgs
[msgp
].m_top
= NOTOK
;
841 if (Msgs
[msgp
].m_scanl
) {
842 free (Msgs
[msgp
].m_scanl
);
843 Msgs
[msgp
].m_scanl
= NULL
;
849 if (modified
|| low
> hgh
)
853 if (fstat (fileno (fp
), &st
) == NOTOK
)
854 padios (mp
->foldpath
, "unable to fstat");
855 if (mtime
== st
.st_mtime
)
857 mode
= (int) (st
.st_mode
& 0777);
860 if ((msgp
= read_file (Msgs
[mp
->hghmsg
].m_stop
, mp
->hghmsg
+ 1)) < 1)
861 padios (NULL
, "no messages in %s", mp
->foldpath
); /* XXX */
862 if (msgp
>= MAXFOLDER
)
863 padios (NULL
, "more than %d messages in %s", MAXFOLDER
,
865 if (msgp
<= mp
->hghmsg
)
868 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgp
)))
869 padios (NULL
, "unable to allocate folder storage");
871 low
= mp
->hghmsg
+ 1, hgh
= msgp
;
872 seqnum
= scansw
? seq_getnum (mp
, "unseen") : -1;
873 for (i
= mp
->hghmsg
+ 1; i
<= msgp
; i
++) {
876 add_sequence(mp
, seqnum
, i
);
886 advise (NULL
, "new messages have arrived!\007");
888 scanrange (low
, hgh
);
895 scanrange (int low
, int hgh
)
899 snprintf (buffer
, sizeof(buffer
), "%d-%d", low
, hgh
);
905 scanstring (char *arg
)
907 char *cp
, **ap
, *vec
[MAXARGS
];
910 * This should be replace with a call to getarguments()
912 if ((cp
= context_find (cmd_name
= "scan"))) {
914 ap
= brkstring (cp
, " ", "\n");
915 ap
= copyip (ap
, vec
, MAXARGS
);
931 register int cur
, seqnum
, i
=0, msgnum
;
934 seq_setcur (mp
, mp
->lowmsg
);
935 if (id
<= 0 || (seqnum
= seq_getnum (mp
, "unseen")) == -1)
938 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
939 add_sequence(mp
, seqnum
, msgnum
);
944 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
945 if (does_exist(mp
, msgnum
)) /* FIX */
946 if ((i
= readid (msgnum
)) > 0 && i
< id
) {
948 clear_sequence(mp
, seqnum
, msgnum
);
951 for (i
= mp
->lowmsg
; i
< msgnum
; i
++)
952 clear_sequence(mp
, seqnum
, i
);
954 if (cur
> mp
->hghmsg
)
957 seq_setcur (mp
, cur
);
960 if ((gap
= 1 < id
&& id
< (i
= readid (mp
->lowmsg
)) ? id
: 0) && !vmh
)
961 advise (NULL
, "gap in ID:s, last seen %d, lowest present %d\n",
970 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
973 if (Msgs
[msgnum
].m_bboard_id
)
974 return Msgs
[msgnum
].m_bboard_id
;
976 zp
= msh_ready (msgnum
, 0);
978 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
)) {
982 if (!mh_strcasecmp (name
, BBoard_ID
)) {
984 while (state
== FLDPLUS
) {
985 state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
);
991 return (Msgs
[msgnum
].m_bboard_id
= i
);
995 while (state
== FLDPLUS
)
996 state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
);
1007 display_info (int scansw
)
1011 interactive
= isatty (fileno (stdout
));
1013 if ((sd
= dup (fileno (stdout
))) == NOTOK
)
1014 padios ("standard output", "unable to dup");
1016 ioctl (sd
, FIOCLEX
, NULL
);
1017 #endif /* FIOCLEX */
1018 if ((sp
= fdopen (sd
, "w")) == NULL
)
1019 padios ("standard output", "unable to fdopen");
1022 m_putenv ("mhfolder", mp
->foldpath
);
1027 printf ("Reading ");
1028 if (SOprintf ("%s", myname
))
1029 printf ("%s", myname
);
1030 printf (", currently at message %d of %d\n",
1031 mp
->curmsg
, mp
->hghmsg
);
1034 printf ("Reading ");
1036 printf ("+%s", fmsh
);
1038 printf ("%s", mp
->foldpath
);
1039 printf (", currently at message %d of %d\n",
1040 mp
->curmsg
, mp
->hghmsg
);
1043 if (((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1045 && in_sequence(mp
, seqnum
, mp
->hghmsg
))
1046 scanstring ("unseen");
1053 int i
= 0, seqnum
, msgnum
;
1059 if ((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1060 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1061 if (!in_sequence(mp
, seqnum
, msgnum
)) {
1062 if (Msgs
[msgnum
].m_bboard_id
== 0)
1064 if ((i
= Msgs
[msgnum
].m_bboard_id
) > 0)
1068 snprintf (buffer
, sizeof(buffer
), "%d %d\n", i
, Msgs
[mp
->hghmsg
].m_bboard_id
);
1069 write (pfd
, buffer
, sizeof(buffer
));
1079 char *cp
, tmpfil
[BUFSIZ
];
1080 char map1
[BUFSIZ
], map2
[BUFSIZ
];
1084 if (!(mp
->msgflags
& MODIFIED
) || is_readonly(mp
) || fmsh
) {
1086 rc2peer (RC_FIN
, 0, NULL
);
1091 ttyNaux (NULLCMD
, "FAST");
1093 if ((dp
= lkfopen (mp
->foldpath
, "r")) == NULL
) {
1094 advise (mp
->foldpath
, "unable to lock");
1101 if (fstat (fileno (dp
), &st
) == NOTOK
) {
1102 advise (mp
->foldpath
, "unable to stat");
1105 if (mtime
!= st
.st_mtime
) {
1106 advise (NULL
, "new messages have arrived, no update");
1109 mode
= (int) (st
.st_mode
& 0777);
1111 if (mp
->nummsg
== 0) {
1112 cp
= concat ("Zero file \"", mp
->foldpath
, "\"? ", NULL
);
1113 if (getanswer (cp
)) {
1114 if ((i
= creat (mp
->foldpath
, mode
)) != NOTOK
)
1117 advise (mp
->foldpath
, "error zero'ing");
1118 unlink (map_name (mp
->foldpath
));/* XXX */
1123 cp
= concat ("Update file \"", mp
->foldpath
, "\"? ", NULL
);
1124 if (!getanswer (cp
))
1126 strncpy (tmpfil
, m_backup (mp
->foldpath
), sizeof(tmpfil
));
1127 if ((md
= mbx_open (tmpfil
, mbx_style
, st
.st_uid
, st
.st_gid
, mode
)) == NOTOK
) {
1128 advise (tmpfil
, "unable to open");
1132 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1133 if (does_exist(mp
, msgnum
) && pack (tmpfil
, md
, msgnum
) == NOTOK
) {
1134 mbx_close (tmpfil
, md
);
1136 unlink (map_name (tmpfil
));
1139 mbx_close (tmpfil
, md
);
1141 if (rename (tmpfil
, mp
->foldpath
) == NOTOK
)
1142 admonish (mp
->foldpath
, "unable to rename %s to", tmpfil
);
1144 strncpy (map1
, map_name (tmpfil
), sizeof(map1
));
1145 strncpy (map2
, map_name (mp
->foldpath
), sizeof(map2
));
1147 if (rename (map1
, map2
) == NOTOK
) {
1148 admonish (map2
, "unable to rename %s to", map1
);
1157 lkfclose (dp
, mp
->foldpath
);
1166 getargs (char *prompt
, struct swit
*sw
, struct Cmd
*cmdp
)
1170 static char buffer
[BUFSIZ
];
1176 printf ("%s", prompt
);
1179 for (cp
= buffer
; (i
= getchar ()) != '\n';) {
1180 if (interrupted
&& !told_to_quit
) {
1185 if (told_to_quit
|| i
== EOF
) {
1188 kill (ppid
, SIGEMT
);
1190 kill (ppid
, SIGTERM
);
1194 if (cp
< &buffer
[sizeof buffer
- 2])
1201 if (buffer
[0] == '?') {
1202 printf ("commands:\n");
1203 print_sw (ALL
, sw
, "", stdout
);
1204 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1209 if (parse (buffer
, cmdp
) == NOTOK
)
1212 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1214 ambigsw (cmdp
->args
[0], sw
);
1217 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1228 getcmds (struct swit
*sw
, struct Cmd
*cmdp
, int scansw
)
1231 struct record rcs
, *rc
;
1237 switch (peer2rc (rc
)) {
1239 pQRY (rc
->rc_data
, scansw
);
1243 if ((i
= pCMD (rc
->rc_data
, sw
, cmdp
)) != NOTOK
)
1250 kill (ppid
, SIGEMT
);
1252 kill (ppid
, SIGTERM
);
1257 padios (NULL
, "%s", rc
->rc_data
);
1260 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
1267 parse (char *buffer
, struct Cmd
*cmdp
)
1270 unsigned char c
, *cp
;
1274 pp
= cmdp
->args
[argp
++] = cmdp
->line
;
1275 cmdp
->redirect
= NULL
;
1276 cmdp
->direction
= STDIO
;
1277 cmdp
->stream
= NULL
;
1279 for (cp
= buffer
; (c
= *cp
); cp
++) {
1285 fmt2peer (RC_EOF
, "null command");
1289 while ((c
= *cp
++)) {
1296 cmdp
->args
[argp
++] = pp
;
1303 switch (c
= *cp
++) {
1305 padvise (NULL
, "unmatched \"");
1310 if ((c
= *cp
++) == 0)
1321 if ((c
= *cp
++) == 0) {
1323 padvise (NULL
, "the newline character can not be quoted");
1333 if (pp
== cmdp
->line
) {
1334 padvise (NULL
, "invalid null command");
1337 if (*cmdp
->args
[argp
- 1] == 0)
1339 cmdp
->direction
= c
== '>' ? CRTIO
: PIPIO
;
1340 if (cmdp
->direction
== CRTIO
&& (c
= *cp
) == '>') {
1341 cmdp
->direction
= APPIO
;
1344 cmdp
->redirect
= pp
+ 1;/* sigh */
1345 for (; (c
= *cp
); cp
++)
1349 padvise (NULL
, cmdp
->direction
!= PIPIO
1350 ? "missing name for redirect"
1351 : "invalid null command");
1354 strcpy (cmdp
->redirect
, cp
);
1355 if (cmdp
->direction
!= PIPIO
) {
1357 if (isspace (*cp
)) {
1358 padvise (NULL
, "bad name for redirect");
1361 if (expand (cmdp
->redirect
) == NOTOK
)
1370 cmdp
->args
[argp
] = NULL
;
1377 expand (char *redirect
)
1383 if (*redirect
!= '~')
1386 if ((cp
= strchr(pp
= redirect
+ 1, '/')))
1391 if ((pw
= getpwnam (pp
)))
1394 padvise (NULL
, "unknown user: %s", pp
);
1398 snprintf (path
, sizeof(path
), "%s/%s", pp
, cp
? cp
: "");
1399 strcpy (redirect
, path
);
1405 init_io (struct Cmd
*cmdp
, int vio
)
1412 result
= initaux_io (cmdp
);
1420 initaux_io (struct Cmd
*cmdp
)
1424 switch (cmdp
->direction
) {
1430 mode
= cmdp
->direction
== CRTIO
? "write" : "append";
1431 if ((cmdp
->stream
= fopen (cmdp
->redirect
, mode
)) == NULL
) {
1432 padvise (cmdp
->redirect
, "unable to %s ", mode
);
1433 cmdp
->direction
= STDIO
;
1439 if ((cmdp
->stream
= popen (cmdp
->redirect
, "w")) == NULL
) {
1440 padvise (cmdp
->redirect
, "unable to pipe");
1441 cmdp
->direction
= STDIO
;
1444 SIGNAL (SIGPIPE
, pipeser
);
1449 padios (NULL
, "unknown redirection for command");
1453 if (dup2 (fileno (cmdp
->stream
), fileno (stdout
)) == NOTOK
)
1454 padios ("standard output", "unable to dup2");
1462 fin_io (struct Cmd
*cmdp
, int vio
)
1474 finaux_io (struct Cmd
*cmdp
)
1476 switch (cmdp
->direction
) {
1483 close (fileno (stdout
));
1484 if (ferror (stdout
))
1485 padvise (NULL
, "problems writing %s", cmdp
->redirect
);
1486 fclose (cmdp
->stream
);
1491 close (fileno (stdout
));
1492 pclose (cmdp
->stream
);
1493 SIGNAL (SIGPIPE
, SIG_DFL
);
1497 padios (NULL
, "unknown redirection for command");
1500 if (dup2 (fileno (sp
), fileno (stdout
)) == NOTOK
)
1501 padios ("standard output", "unable to dup2");
1504 cmdp
->direction
= STDIO
;
1513 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1514 unset_selected (mp
, msgnum
);
1515 mp
->lowsel
= mp
->hghsel
= mp
->numsel
= 0;
1523 folder_free (mp
); /* free folder/message structure */
1529 seq_setcur (struct msgs
*mp
, int msgnum
)
1531 if (mp
->curmsg
== msgnum
)
1534 if (mp
->curmsg
&& Msgs
[mp
->curmsg
].m_scanl
) {
1535 free (Msgs
[mp
->curmsg
].m_scanl
);
1536 Msgs
[mp
->curmsg
].m_scanl
= NULL
;
1538 if (Msgs
[msgnum
].m_scanl
) {
1539 free (Msgs
[msgnum
].m_scanl
);
1540 Msgs
[msgnum
].m_scanl
= NULL
;
1543 mp
->curmsg
= msgnum
;
1561 if (broken_pipe
++ == 0)
1562 fprintf (stderr
, "broken pipe\n");
1581 longjmp (peerenv
, DONE
);
1590 struct record rcs
, *rc
;
1595 switch (peer2rc (rc
)) {
1598 while (isspace (*bp
))
1600 if (sscanf (bp
, "%d", &vrsn
) != 1) {
1602 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
1605 if (vrsn
!= RC_VRSN
) {
1606 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
1610 while (*bp
&& !isspace (*bp
))
1612 while (isspace (*bp
))
1614 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
1619 for (i
= 1; i
<= numwins
; i
++) {
1620 while (*bp
&& !isspace (*bp
))
1622 while (isspace (*bp
))
1624 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
1627 rc2peer (RC_ACK
, 0, NULL
);
1631 padios (NULL
, "%s", rc
->rc_data
);
1634 fmt2peer (RC_ERR
, "pINI protocol screw-up");
1635 done (1); /* NOTREACHED */
1638 return 1; /* dead code to satisfy the compiler */
1643 pQRY (char *str
, int scansw
)
1646 if (pQRY1 (scansw
) == NOTOK
|| pQRY2 () == NOTOK
)
1649 rc2peer (RC_EOF
, 0, NULL
);
1658 static int lastlow
= 0,
1663 oldhgh
= mp
->hghmsg
;
1664 if (check_folder (scansw
) && oldhgh
< mp
->hghmsg
) {
1665 switch (winX (STATUS
)) {
1670 printf ("new messages have arrived!");
1673 _exit (0); /* NOTREACHED */
1676 lastlow
= lastcur
= lasthgh
= lastnum
= 0;
1680 switch (winX (DISPLAY
)) {
1685 scanrange (oldhgh
+ 1, mp
->hghmsg
);
1688 _exit (0); /* NOTREACHED */
1697 switch (winX (STATUS
)) {
1702 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1703 myname
? myname
: fmsh
? fmsh
: mp
->foldpath
, gap
- 1,
1704 readid (mp
->lowmsg
));
1707 _exit (0); /* NOTREACHED */
1714 if (mp
->lowmsg
!= lastlow
1715 || mp
->curmsg
!= lastcur
1716 || mp
->hghmsg
!= lasthgh
1717 || mp
->nummsg
!= lastnum
)
1718 switch (winX (STATUS
)) {
1726 _exit (0); /* NOTREACHED */
1729 lastlow
= mp
->lowmsg
;
1730 lastcur
= mp
->curmsg
;
1731 lasthgh
= mp
->hghmsg
;
1732 lastnum
= mp
->nummsg
;
1743 int i
, j
, k
, msgnum
, n
;
1749 if (mp
->nummsg
== 0 && mp
->nummsg
!= num
)
1750 switch (winX (SCAN
)) {
1758 _exit (0); /* NOTREACHED */
1767 j
= (k
= windows
[SCAN
]) / 2;
1768 for (msgnum
= mp
->curmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1769 if (does_exist (mp
, msgnum
))
1773 k
= i
>= k
? 1 : k
- i
;
1780 for (msgnum
= mp
->curmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1781 if (does_exist (mp
, msgnum
)) {
1788 for (msgnum
= mp
->curmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1789 if (does_exist (mp
, msgnum
)) {
1793 if (n
++ >= windows
[SCAN
])
1799 && does_exist (mp
, lo
)
1800 && does_exist (mp
, hi
)
1802 || (lo
== mp
->curmsg
&& lo
== mp
->lowmsg
))
1804 || (hi
== mp
->curmsg
&& hi
== mp
->hghmsg
))
1805 && hi
- lo
== j
- i
)
1808 if (mp
->curmsg
!= cur
|| modified
)
1809 switch (winN (NULLCMD
, SCAN
, 0)) {
1817 scanrange (lo
= i
, hi
= j
);
1828 pCMD (char *str
, struct swit
*sw
, struct Cmd
*cmdp
)
1833 switch (winX (DISPLAY
)) {
1838 printf ("commands:\n");
1839 print_sw (ALL
, sw
, "", stdout
);
1840 printf ("type ``quit'' to leave %s\n", invo_name
);
1843 _exit (0); /* NOTREACHED */
1846 rc2peer (RC_EOF
, 0, NULL
);
1850 if (parse (str
, cmdp
) == NOTOK
)
1853 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1855 switch (winX (DISPLAY
)) {
1860 ambigsw (cmdp
->args
[0], sw
);
1863 _exit (0); /* NOTREACHED */
1866 rc2peer (RC_EOF
, 0, NULL
);
1872 "say what: ``%s'' -- type ? (or help) for help",
1887 switch (setjmp (peerenv
)) {
1889 SIGNAL (SIGALRM
, alrmser
);
1892 status
= peerwait ();
1906 struct record rcs
, *rc
;
1911 switch (peer2rc (rc
)) {
1914 rc2peer (RC_FIN
, 0, NULL
);
1918 advise (NULL
, "%s", rc
->rc_data
);
1922 fmt2peer (RC_FIN
, "pLOOP protocol screw-up");
1929 ttyNaux (struct Cmd
*cmdp
, char *s
)
1931 struct record rcs
, *rc
;
1936 if (cmdp
&& init_io (cmdp
, vmh
) == NOTOK
)
1939 /* XXX: fseek() too tricky for our own good */
1941 fseek (fp
, 0L, SEEK_SET
);
1944 switch (rc2rc (RC_TTY
, s
? strlen (s
) : 0, s
, rc
)) {
1946 vmhtty
= OK
; /* fall */
1951 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1954 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1955 done (1); /* NOTREACHED */
1959 SIGNAL (SIGTSTP
, tstat
);
1966 ttyR (struct Cmd
*cmdp
)
1968 struct record rcs
, *rc
;
1973 SIGNAL (SIGTSTP
, SIG_IGN
);
1985 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
1987 rc2peer (RC_EOF
, 0, NULL
);
1991 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1994 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1995 done (1); /* NOTREACHED */
1998 return 1; /* dead code to satisfy compiler */
2003 winN (struct Cmd
*cmdp
, int n
, int eof
)
2006 char buffer
[BUFSIZ
];
2007 struct record rcs
, *rc
;
2010 if (vmhpid
== NOTOK
)
2015 /* XXX: fseek() too tricky for our own good */
2017 fseek (fp
, 0L, SEEK_SET
);
2021 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2022 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2030 padios (NULL
, "%s", rc
->rc_data
);
2033 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2037 if (pipe (pd
) == NOTOK
) {
2038 err2peer (RC_ERR
, "pipe", "unable to");
2042 switch (vmhpid
= fork()) {
2044 err2peer (RC_ERR
, "fork", "unable to");
2051 SIGNAL (SIGPIPE
, SIG_IGN
);
2052 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2053 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2061 advise (NULL
, "%s", rc
->rc_data
);
2065 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2069 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2072 rc2peer (RC_EOF
, 0, NULL
);
2077 advise (NULL
, "%s", rc
->rc_data
);
2082 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2087 err2peer (RC_ERR
, "pipe", "error reading from");
2089 _exit (i
!= NOTOK
? i
: 1);
2092 if ((vmhfd0
= dup (fileno (stdin
))) == NOTOK
)
2093 padios ("standard input", "unable to dup");
2094 if ((vmhfd1
= dup (fileno (stdout
))) == NOTOK
)
2095 padios ("standard output", "unable to dup");
2096 if ((vmhfd2
= dup (fileno (stderr
))) == NOTOK
)
2097 padios ("diagnostic output", "unable to dup");
2100 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2101 dup2 (i
, fileno (stdin
));
2106 if (dup2 (pd
[1], fileno (stdout
)) == NOTOK
)
2107 padios ("standard output", "unable to dup2");
2111 if (dup2 (pd
[1], fileno (stderr
)) == NOTOK
)
2112 padios ("diagnostic output", "unable to dup2");
2115 if (cmdp
&& init_io (cmdp
, 0) == NOTOK
)
2117 pstat
= SIGNAL (SIGPIPE
, pipeser
);
2129 winR (struct Cmd
*cmdp
)
2139 if (dup2 (vmhfd0
, fileno (stdin
)) == NOTOK
)
2140 padios ("standard input", "unable to dup2");
2145 if (dup2 (vmhfd1
, fileno (stdout
)) == NOTOK
)
2146 padios ("standard output", "unable to dup2");
2151 if (dup2 (vmhfd2
, fileno (stderr
)) == NOTOK
)
2152 padios ("diagnostic output", "unable to dup2");
2156 SIGNAL (SIGPIPE
, pstat
);
2158 if ((status
= pidwait (vmhpid
, OK
)) == 2)
2162 return (status
== 0 ? OK
: NOTOK
);
2170 char buffer
[BUFSIZ
];
2171 struct record rcs
, *rc
;
2176 /* XXX: fseek() too tricky for our own good */
2178 fseek (fp
, 0L, SEEK_SET
);
2180 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2181 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2189 padios (NULL
, "%s", rc
->rc_data
);
2192 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2196 if (pipe (pd
) == NOTOK
) {
2197 err2peer (RC_ERR
, "pipe", "unable to");
2201 switch (pid
= fork ()) {
2203 err2peer (RC_ERR
, "fork", "unable to");
2209 close (fileno (stdin
));
2210 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2211 dup2 (i
, fileno (stdin
));
2214 dup2 (pd
[1], fileno (stdout
));
2215 dup2 (pd
[1], fileno (stderr
));
2223 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2224 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2234 padios (NULL
, "%s", rc
->rc_data
);
2237 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2241 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2246 padios (NULL
, "%s", rc
->rc_data
);
2249 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2253 err2peer (RC_ERR
, "pipe", "error reading from");
2257 return (i
!= NOTOK
? pid
: NOTOK
);
2263 padios (char *what
, char *fmt
, ...)
2269 verr2peer (RC_FIN
, what
, fmt
, ap
);
2272 advertise (what
, NULL
, fmt
, ap
);
2281 padvise (char *what
, char *fmt
, ...)
2287 verr2peer (RC_ERR
, what
, fmt
, ap
);
2289 advertise (what
, NULL
, fmt
, ap
);