]>
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>
28 #include <h/m_setjmp.h>
33 #define QUOTE '\\' /* sigh */
35 static struct swit switches
[] = {
37 { "idstart number", -7 }, /* interface from bbc */
39 { "idstop number", -6 }, /* .. */
41 { "idquit number", -6 }, /* .. */
43 { "idname BBoard", -6 }, /* .. */
45 { "prompt string", 0 },
53 { "vmhwrite fd", -8 },
57 { "popwrite fd", -8 },
69 static int mbx_style
= MMDF_FORMAT
;
74 char*fmsh
= NULL
; /* folder instead of file */
75 int modified
; /* command modified folder */
76 struct msgs
*mp
; /* used a lot */
78 struct Msg
*Msgs
= NULL
; /* Msgs[0] not used */
79 static FILE *fp
; /* input file */
80 static FILE *yp
= NULL
; /* temporary file */
81 static int mode
; /* mode of file */
82 static int numfds
= 0; /* number of files cached */
83 static int maxfds
= 0; /* number of files cached to be cached */
84 static time_t mtime
= (time_t) 0; /* mtime of file */
89 #define ALARM ((unsigned int) 10)
90 #define ttyN(c) ttyNaux ((c), NULL)
94 static int vmhpid
= OK
;
99 static int vmhtty
= NOTOK
;
106 static int topcur
= 0;
108 static int numwins
= 0;
109 static int windows
[NWIN
+ 1];
111 static jmp_buf peerenv
;
116 static int pfd
= NOTOK
; /* fd parent is reading from */
117 static int ppid
= 0; /* pid of parent */
122 int interactive
; /* running from a /dev/tty */
123 int redirected
; /* re-directing output */
124 FILE *sp
= NULL
; /* original stdout */
126 char *cmd_name
; /* command being run */
127 char myfilter
[BUFSIZ
]; /* path to mhl.forward */
129 static char *myprompt
= "(%s) ";/* prompting string */
134 static int gap
; /* gap in BBoard-ID:s */
135 static char *myname
= NULL
; /* BBoard name */
136 char *BBoard_ID
= "BBoard-ID"; /* BBoard-ID constant */
141 SIGNAL_HANDLER istat
; /* original SIGINT */
142 static SIGNAL_HANDLER pstat
; /* current SIGPIPE */
143 SIGNAL_HANDLER qstat
; /* original SIGQUIT */
146 SIGNAL_HANDLER tstat
; /* original SIGTSTP */
149 int interrupted
; /* SIGINT detected */
150 int broken_pipe
; /* SIGPIPE detected */
151 int told_to_quit
; /* SIGQUIT detected */
156 void fsetup (char *);
158 FILE *msh_ready (int, int);
161 void display_info (int);
164 void seq_setcur (struct msgs
*, int);
165 void padios (char *, char *, ...);
166 void padvise (char *, char *, ...);
172 static void msh (int);
173 static int read_map (char *, long);
174 static int read_file (long, int);
176 static void m_gMsgs (int);
177 FILE *msh_ready (int, int);
178 static int check_folder (int);
179 static void scanrange (int, int);
180 static void scanstring (char *);
181 static void write_ids (void);
182 static void quit (void);
183 static int getargs (char *, struct swit
*, struct Cmd
*);
184 static int getcmds (struct swit
*, struct Cmd
*, int);
185 static int parse (char *, struct Cmd
*);
186 static int init_io (struct Cmd
*, int);
187 static int initaux_io (struct Cmd
*);
188 static void fin_io (struct Cmd
*, int);
189 static void finaux_io (struct Cmd
*);
190 static void m_init (void);
191 static void intrser (int);
192 static void pipeser (int);
193 static void quitser (int);
194 static void alrmser (int);
195 static int pINI (void);
196 static int pQRY (char *, int);
197 static int pQRY1 (int);
198 static int pQRY2 (void);
199 static int pCMD (char *, struct swit
*, struct Cmd
*);
200 static int pFIN (void);
201 static int peerwait (void);
202 static int ttyNaux (struct Cmd
*, char *);
203 static int ttyR (struct Cmd
*);
204 static int winN (struct Cmd
*, int, int);
205 static int winR (struct Cmd
*);
206 static int winX (int);
210 main (int argc
, char **argv
)
212 int id
= 0, scansw
= 0, vmh1
= 0, vmh2
= 0;
213 char *cp
, *file
= NULL
, *folder
= NULL
;
214 char **argp
, **arguments
, buf
[BUFSIZ
];
217 setlocale(LC_ALL
, "");
219 invo_name
= r1bindex (argv
[0], '/');
221 /* read user profile/context */
224 mts_init (invo_name
);
225 arguments
= getarguments (invo_name
, argc
,argv
, 1);
228 while ((cp
= *argp
++)) {
230 switch (smatch (++cp
, switches
)) {
232 ambigsw (cp
, switches
);
235 adios (NULL
, "-%s unknown", cp
);
238 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
239 print_help (buf
, switches
, 1);
242 print_version(invo_name
);
246 if (!(cp
= *argp
++) || *cp
== '-')
247 adios (NULL
, "missing argument to %s", argp
[-2]);
248 if ((id
= atoi (cp
)) < 1)
249 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
252 if (!(cp
= *argp
++) || *cp
== '-')
253 adios (NULL
, "missing argument to %s", argp
[-2]);
254 if ((pfd
= atoi (cp
)) <= 1)
255 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
258 if (!(cp
= *argp
++) || *cp
== '-')
259 adios (NULL
, "missing argument to %s", argp
[-2]);
260 if ((ppid
= atoi (cp
)) <= 1)
261 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
264 if (!(myname
= *argp
++) || *myname
== '-')
265 adios (NULL
, "missing argument to %s", argp
[-2]);
276 if (!(myprompt
= *argp
++) || *myprompt
== '-')
277 adios (NULL
, "missing argument to %s", argp
[-2]);
281 if (!(cp
= *argp
++) || *cp
== '-')
282 adios (NULL
, "missing argument to %s", argp
[-2]);
283 if ((vmh1
= atoi (cp
)) < 1)
284 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
287 if (!(cp
= *argp
++) || *cp
== '-')
288 adios (NULL
, "missing argument to %s", argp
[-2]);
289 if ((vmh2
= atoi (cp
)) < 1)
290 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
294 if (!(cp
= *argp
++) || *cp
== '-')
295 adios (NULL
, "missing argument to %s", argp
[-2]);
298 if (!(cp
= *argp
++) || *cp
== '-')
299 adios (NULL
, "missing argument to %s", argp
[-2]);
309 if (*cp
== '+' || *cp
== '@') {
311 adios (NULL
, "only one folder at a time!");
313 folder
= pluspath (cp
);
317 adios (NULL
, "only one file at a time!");
322 if (!file
&& !folder
)
325 adios (NULL
, "use a file or a folder, not both");
326 strncpy (myfilter
, etcpath (mhlforward
), sizeof(myfilter
));
329 ioctl (pfd
, FIOCLEX
, NULL
);
332 istat
= SIGNAL2 (SIGINT
, intrser
);
333 qstat
= SIGNAL2 (SIGQUIT
, quitser
);
335 sc_width (); /* MAGIC... */
337 if ((vmh
= vmh1
&& vmh2
)) {
340 SIGNAL (SIGINT
, SIG_IGN
);
341 SIGNAL (SIGQUIT
, SIG_IGN
);
343 tstat
= SIGNAL (SIGTSTP
, SIG_IGN
);
352 display_info (id
> 0 ? scansw
: 0);
354 msh (id
> 0 ? scansw
: 0);
363 static struct swit mshcmds
[] = {
428 register char *cp
, **ap
;
429 char prompt
[BUFSIZ
], *vec
[MAXARGS
];
431 register struct Cmd
*cmdp
;
432 static int once_only
= ADVCMD
;
434 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
443 if ((i
= getcmds (mshcmds
, cmdp
, scansw
)) == EOF
) {
448 check_folder (scansw
);
449 if ((i
= getargs (prompt
, mshcmds
, cmdp
)) == EOF
) {
454 cmd_name
= mshcmds
[i
].sw
;
462 if (once_only
== ADVCMD
)
463 once_only
= i
= SHOWCMD
;
465 i
= mp
->curmsg
!= mp
->hghmsg
? NEXTCMD
: EXITCMD
;
466 cmd_name
= mshcmds
[i
].sw
;
472 case FORWCMD
: /* sigh */
482 if ((cp
= context_find (cmd_name
))) {
484 ap
= brkstring (cp
, " ", "\n");
485 ap
= copyip (ap
, vec
, MAXARGS
);
496 copyip (cmdp
->args
+ 1, ap
, MAXARGS
);
500 if (!vmh
&& init_io (cmdp
, vmh
) == NOTOK
) {
506 redirected
= vmh
|| cmdp
->direction
!= STDIO
;
517 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
518 forkcmd (vec
, cmd_name
);
522 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
527 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
533 || (filehak (vec
) == OK
? ttyN (cmdp
)
534 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
539 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
544 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
549 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
555 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
560 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
567 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
573 || (packhak (vec
) == OK
? ttyN (cmdp
)
574 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
579 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
584 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
589 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
594 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
599 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
604 padios (NULL
, "no dispatch for %s", cmd_name
);
626 fsetup (char *folder
)
632 maildir
= m_maildir (folder
);
633 if (chdir (maildir
) == NOTOK
)
634 padios (maildir
, "unable to change directory to");
636 /* read folder and create message structure */
637 if (!(mp
= folder_read (folder
)))
638 padios (NULL
, "unable to read folder %s", folder
);
640 /* check for empty folder */
642 padios (NULL
, "no messages in %s", folder
);
645 mtime
= stat (mp
->foldpath
, &st
) != NOTOK
? st
.st_mtime
: 0;
647 m_gMsgs (mp
->hghmsg
);
649 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
650 Msgs
[msgnum
].m_bboard_id
= 0;
651 Msgs
[msgnum
].m_top
= NOTOK
;
652 Msgs
[msgnum
].m_start
= Msgs
[msgnum
].m_stop
= 0L;
653 Msgs
[msgnum
].m_scanl
= NULL
;
658 fmsh
= getcpy (folder
);
660 maxfds
= OPEN_MAX
/ 2;
662 if ((maxfds
-= 2) < 1)
672 if ((fp
= fopen (file
, "r")) == NULL
)
673 padios (file
, "unable to read");
675 ioctl (fileno (fp
), FIOCLEX
, NULL
);
677 if (fstat (fileno (fp
), &st
) != NOTOK
) {
678 mode
= (int) (st
.st_mode
& 0777), mtime
= st
.st_mtime
;
679 msgp
= read_map (file
, (long) st
.st_size
);
682 mode
= m_gmprot (), mtime
= 0;
686 if ((msgp
= read_file (msgp
? Msgs
[msgp
].m_stop
: 0L, msgp
+ 1)) < 1)
687 padios (NULL
, "no messages in %s", myname
? myname
: file
);
689 if (!(mp
= (struct msgs
*) calloc ((size_t) 1, sizeof(*mp
))))
690 padios (NULL
, "unable to allocate folder storage");
692 if (!(mp
->msgstats
= calloc ((size_t) msgp
+ 3, sizeof(*(mp
->msgstats
)))))
693 padios (NULL
, "unable to allocate message status storage");
699 mp
->foldpath
= getcpy (myname
? myname
: file
);
700 clear_folder_flags (mp
);
703 if (st
.st_uid
!= getuid () || access (file
, W_OK
) == NOTOK
)
707 mp
->hghoff
= mp
->hghmsg
+ 1;
709 for (i
= mp
->lowmsg
; i
<= mp
->hghmsg
; i
++) {
710 clear_msg_flags (mp
, i
);
715 mp
->msgattrs
[0] = getcpy ("unseen");
716 mp
->msgattrs
[1] = NULL
;
718 m_unknown (fp
); /* the MAGIC invocation */
727 read_map (char *file
, long size
)
729 register int i
, msgp
;
730 register struct drop
*dp
, *mp
;
733 if ((i
= map_read (file
, size
, &rp
, 1)) == 0)
739 for (dp
= rp
+ 1; i
-- > 0; msgp
++, dp
++) {
740 mp
= &Msgs
[msgp
].m_drop
;
742 mp
->d_size
= dp
->d_size
;
743 mp
->d_start
= dp
->d_start
;
744 mp
->d_stop
= dp
->d_stop
;
745 Msgs
[msgp
].m_scanl
= NULL
;
754 read_file (long pos
, int msgp
)
757 register struct drop
*dp
, *mp
;
760 if ((i
= mbx_read (fp
, pos
, &rp
, 1)) <= 0)
763 m_gMsgs ((msgp
- 1) + i
);
765 for (dp
= rp
; i
-- > 0; msgp
++, dp
++) {
766 mp
= &Msgs
[msgp
].m_drop
;
768 mp
->d_size
= dp
->d_size
;
769 mp
->d_start
= dp
->d_start
;
770 mp
->d_stop
= dp
->d_stop
;
771 Msgs
[msgp
].m_scanl
= NULL
;
785 nMsgs
= n
+ MAXFOLDER
/ 2;
786 Msgs
= (struct Msg
*) calloc ((size_t) (nMsgs
+ 2), sizeof *Msgs
);
788 padios (NULL
, "unable to allocate Msgs structure");
795 nmsgs
= nMsgs
+ n
+ MAXFOLDER
/ 2;
796 Msgs
= (struct Msg
*) mh_xrealloc ((char *) Msgs
, (size_t) (nmsgs
+ 2) * sizeof *Msgs
);
797 memset((char *) (Msgs
+ nMsgs
+ 2), 0, (size_t) ((nmsgs
- nMsgs
) * sizeof *Msgs
));
804 msh_ready (int msgnum
, int full
)
817 if ((fd
= Msgs
[msgnum
].m_top
) == NOTOK
) {
818 if (numfds
>= maxfds
)
819 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++)
820 if (Msgs
[msgp
].m_top
!= NOTOK
) {
821 close (Msgs
[msgp
].m_top
);
822 Msgs
[msgp
].m_top
= NOTOK
;
827 if ((fd
= open (cp
= m_name (msgnum
), O_RDONLY
)) == NOTOK
)
828 padios (cp
, "unable to open message");
829 Msgs
[msgnum
].m_top
= fd
;
833 if ((fd
= dup (fd
)) == NOTOK
)
834 padios ("cached message", "unable to dup");
835 if ((yp
= fdopen (fd
, "r")) == NULL
)
836 padios (NULL
, "unable to fdopen cached message");
837 fseek (yp
, 0L, SEEK_SET
);
841 m_eomsbr ((int (*)()) 0); /* XXX */
842 fseek (fp
, Msgs
[msgnum
].m_start
, SEEK_SET
);
848 check_folder (int scansw
)
850 int seqnum
, i
, low
, hgh
, msgp
;
854 if (stat (mp
->foldpath
, &st
) == NOTOK
)
855 padios (mp
->foldpath
, "unable to stat");
856 if (mtime
== st
.st_mtime
)
860 low
= mp
->hghmsg
+ 1;
861 folder_free (mp
); /* free folder/message structure */
863 if (!(mp
= folder_read (fmsh
)))
864 padios (NULL
, "unable to re-read folder %s", fmsh
);
868 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++) {
869 if (Msgs
[msgp
].m_top
!= NOTOK
) {
870 close (Msgs
[msgp
].m_top
);
871 Msgs
[msgp
].m_top
= NOTOK
;
874 if (Msgs
[msgp
].m_scanl
) {
875 free (Msgs
[msgp
].m_scanl
);
876 Msgs
[msgp
].m_scanl
= NULL
;
882 if (modified
|| low
> hgh
)
886 if (fstat (fileno (fp
), &st
) == NOTOK
)
887 padios (mp
->foldpath
, "unable to fstat");
888 if (mtime
== st
.st_mtime
)
890 mode
= (int) (st
.st_mode
& 0777);
893 if ((msgp
= read_file (Msgs
[mp
->hghmsg
].m_stop
, mp
->hghmsg
+ 1)) < 1)
894 padios (NULL
, "no messages in %s", mp
->foldpath
); /* XXX */
895 if (msgp
>= MAXFOLDER
)
896 padios (NULL
, "more than %d messages in %s", MAXFOLDER
,
898 if (msgp
<= mp
->hghmsg
)
901 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgp
)))
902 padios (NULL
, "unable to allocate folder storage");
904 low
= mp
->hghmsg
+ 1, hgh
= msgp
;
905 seqnum
= scansw
? seq_getnum (mp
, "unseen") : -1;
906 for (i
= mp
->hghmsg
+ 1; i
<= msgp
; i
++) {
909 add_sequence(mp
, seqnum
, i
);
919 advise (NULL
, "new messages have arrived!\007");
921 scanrange (low
, hgh
);
928 scanrange (int low
, int hgh
)
932 snprintf (buffer
, sizeof(buffer
), "%d-%d", low
, hgh
);
938 scanstring (char *arg
)
940 char *cp
, **ap
, *vec
[MAXARGS
];
943 * This should be replace with a call to getarguments()
945 if ((cp
= context_find (cmd_name
= "scan"))) {
947 ap
= brkstring (cp
, " ", "\n");
948 ap
= copyip (ap
, vec
, MAXARGS
);
964 register int cur
, seqnum
, i
=0, msgnum
;
967 seq_setcur (mp
, mp
->lowmsg
);
968 if (id
<= 0 || (seqnum
= seq_getnum (mp
, "unseen")) == -1)
971 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
972 add_sequence(mp
, seqnum
, msgnum
);
977 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
978 if (does_exist(mp
, msgnum
)) /* FIX */
979 if ((i
= readid (msgnum
)) > 0 && i
< id
) {
981 clear_sequence(mp
, seqnum
, msgnum
);
984 for (i
= mp
->lowmsg
; i
< msgnum
; i
++)
985 clear_sequence(mp
, seqnum
, i
);
987 if (cur
> mp
->hghmsg
)
990 seq_setcur (mp
, cur
);
993 if ((gap
= 1 < id
&& id
< (i
= readid (mp
->lowmsg
)) ? id
: 0) && !vmh
)
994 advise (NULL
, "gap in ID:s, last seen %d, lowest present %d\n",
1003 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
1006 if (Msgs
[msgnum
].m_bboard_id
)
1007 return Msgs
[msgnum
].m_bboard_id
;
1009 zp
= msh_ready (msgnum
, 0);
1011 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
)) {
1015 if (!mh_strcasecmp (name
, BBoard_ID
)) {
1017 while (state
== FLDPLUS
) {
1018 state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
);
1024 return (Msgs
[msgnum
].m_bboard_id
= i
);
1028 while (state
== FLDPLUS
)
1029 state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
);
1030 if (state
!= FLDEOF
)
1040 display_info (int scansw
)
1044 interactive
= isatty (fileno (stdout
));
1046 if ((sd
= dup (fileno (stdout
))) == NOTOK
)
1047 padios ("standard output", "unable to dup");
1049 ioctl (sd
, FIOCLEX
, NULL
);
1050 #endif /* FIOCLEX */
1051 if ((sp
= fdopen (sd
, "w")) == NULL
)
1052 padios ("standard output", "unable to fdopen");
1055 m_putenv ("mhfolder", mp
->foldpath
);
1060 printf ("Reading ");
1061 if (SOprintf ("%s", myname
))
1062 printf ("%s", myname
);
1063 printf (", currently at message %d of %d\n",
1064 mp
->curmsg
, mp
->hghmsg
);
1067 printf ("Reading ");
1069 printf ("+%s", fmsh
);
1071 printf ("%s", mp
->foldpath
);
1072 printf (", currently at message %d of %d\n",
1073 mp
->curmsg
, mp
->hghmsg
);
1076 if (((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1078 && in_sequence(mp
, seqnum
, mp
->hghmsg
))
1079 scanstring ("unseen");
1086 int i
= 0, seqnum
, msgnum
;
1092 if ((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1093 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1094 if (!in_sequence(mp
, seqnum
, msgnum
)) {
1095 if (Msgs
[msgnum
].m_bboard_id
== 0)
1097 if ((i
= Msgs
[msgnum
].m_bboard_id
) > 0)
1101 snprintf (buffer
, sizeof(buffer
), "%d %d\n", i
, Msgs
[mp
->hghmsg
].m_bboard_id
);
1102 write (pfd
, buffer
, sizeof(buffer
));
1112 char *cp
, tmpfil
[BUFSIZ
];
1113 char map1
[BUFSIZ
], map2
[BUFSIZ
];
1117 if (!(mp
->msgflags
& MODIFIED
) || is_readonly(mp
) || fmsh
) {
1119 rc2peer (RC_FIN
, 0, NULL
);
1124 ttyNaux (NULLCMD
, "FAST");
1126 if ((dp
= lkfopen (mp
->foldpath
, "r")) == NULL
) {
1127 advise (mp
->foldpath
, "unable to lock");
1134 if (fstat (fileno (dp
), &st
) == NOTOK
) {
1135 advise (mp
->foldpath
, "unable to stat");
1138 if (mtime
!= st
.st_mtime
) {
1139 advise (NULL
, "new messages have arrived, no update");
1142 mode
= (int) (st
.st_mode
& 0777);
1144 if (mp
->nummsg
== 0) {
1145 cp
= concat ("Zero file \"", mp
->foldpath
, "\"? ", NULL
);
1146 if (getanswer (cp
)) {
1147 if ((i
= creat (mp
->foldpath
, mode
)) != NOTOK
)
1150 advise (mp
->foldpath
, "error zero'ing");
1151 unlink (map_name (mp
->foldpath
));/* XXX */
1156 cp
= concat ("Update file \"", mp
->foldpath
, "\"? ", NULL
);
1157 if (!getanswer (cp
))
1159 strncpy (tmpfil
, m_backup (mp
->foldpath
), sizeof(tmpfil
));
1160 if ((md
= mbx_open (tmpfil
, mbx_style
, st
.st_uid
, st
.st_gid
, mode
)) == NOTOK
) {
1161 advise (tmpfil
, "unable to open");
1165 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1166 if (does_exist(mp
, msgnum
) && pack (tmpfil
, md
, msgnum
) == NOTOK
) {
1167 mbx_close (tmpfil
, md
);
1169 unlink (map_name (tmpfil
));
1172 mbx_close (tmpfil
, md
);
1174 if (rename (tmpfil
, mp
->foldpath
) == NOTOK
)
1175 admonish (mp
->foldpath
, "unable to rename %s to", tmpfil
);
1177 strncpy (map1
, map_name (tmpfil
), sizeof(map1
));
1178 strncpy (map2
, map_name (mp
->foldpath
), sizeof(map2
));
1180 if (rename (map1
, map2
) == NOTOK
) {
1181 admonish (map2
, "unable to rename %s to", map1
);
1190 lkfclose (dp
, mp
->foldpath
);
1199 getargs (char *prompt
, struct swit
*sw
, struct Cmd
*cmdp
)
1203 static char buffer
[BUFSIZ
];
1209 printf ("%s", prompt
);
1212 for (cp
= buffer
; (i
= getchar ()) != '\n';) {
1213 if (interrupted
&& !told_to_quit
) {
1218 if (told_to_quit
|| i
== EOF
) {
1221 kill (ppid
, SIGEMT
);
1223 kill (ppid
, SIGTERM
);
1227 if (cp
< &buffer
[sizeof buffer
- 2])
1234 if (buffer
[0] == '?') {
1235 printf ("commands:\n");
1236 print_sw (ALL
, sw
, "", stdout
);
1237 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1242 if (parse (buffer
, cmdp
) == NOTOK
)
1245 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1247 ambigsw (cmdp
->args
[0], sw
);
1250 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1261 getcmds (struct swit
*sw
, struct Cmd
*cmdp
, int scansw
)
1264 struct record rcs
, *rc
;
1270 switch (peer2rc (rc
)) {
1272 pQRY (rc
->rc_data
, scansw
);
1276 if ((i
= pCMD (rc
->rc_data
, sw
, cmdp
)) != NOTOK
)
1283 kill (ppid
, SIGEMT
);
1285 kill (ppid
, SIGTERM
);
1290 padios (NULL
, "%s", rc
->rc_data
);
1293 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
1300 parse (char *buffer
, struct Cmd
*cmdp
)
1303 unsigned char c
, *cp
;
1307 pp
= cmdp
->args
[argp
++] = cmdp
->line
;
1308 cmdp
->redirect
= NULL
;
1309 cmdp
->direction
= STDIO
;
1310 cmdp
->stream
= NULL
;
1312 for (cp
= buffer
; (c
= *cp
); cp
++) {
1318 fmt2peer (RC_EOF
, "null command");
1322 while ((c
= *cp
++)) {
1329 cmdp
->args
[argp
++] = pp
;
1336 switch (c
= *cp
++) {
1338 padvise (NULL
, "unmatched \"");
1343 if ((c
= *cp
++) == 0)
1354 if ((c
= *cp
++) == 0) {
1356 padvise (NULL
, "the newline character can not be quoted");
1366 if (pp
== cmdp
->line
) {
1367 padvise (NULL
, "invalid null command");
1370 if (*cmdp
->args
[argp
- 1] == 0)
1372 cmdp
->direction
= c
== '>' ? CRTIO
: PIPIO
;
1373 if (cmdp
->direction
== CRTIO
&& (c
= *cp
) == '>') {
1374 cmdp
->direction
= APPIO
;
1377 cmdp
->redirect
= pp
+ 1;/* sigh */
1378 for (; (c
= *cp
); cp
++)
1382 padvise (NULL
, cmdp
->direction
!= PIPIO
1383 ? "missing name for redirect"
1384 : "invalid null command");
1387 strcpy (cmdp
->redirect
, cp
);
1388 if (cmdp
->direction
!= PIPIO
) {
1390 if (isspace (*cp
)) {
1391 padvise (NULL
, "bad name for redirect");
1394 if (expand (cmdp
->redirect
) == NOTOK
)
1403 cmdp
->args
[argp
] = NULL
;
1410 expand (char *redirect
)
1416 if (*redirect
!= '~')
1419 if ((cp
= strchr(pp
= redirect
+ 1, '/')))
1424 if ((pw
= getpwnam (pp
)))
1427 padvise (NULL
, "unknown user: %s", pp
);
1431 snprintf (path
, sizeof(path
), "%s/%s", pp
, cp
? cp
: "");
1432 strcpy (redirect
, path
);
1438 init_io (struct Cmd
*cmdp
, int vio
)
1445 result
= initaux_io (cmdp
);
1453 initaux_io (struct Cmd
*cmdp
)
1457 switch (cmdp
->direction
) {
1463 mode
= cmdp
->direction
== CRTIO
? "write" : "append";
1464 if ((cmdp
->stream
= fopen (cmdp
->redirect
, mode
)) == NULL
) {
1465 padvise (cmdp
->redirect
, "unable to %s ", mode
);
1466 cmdp
->direction
= STDIO
;
1472 if ((cmdp
->stream
= popen (cmdp
->redirect
, "w")) == NULL
) {
1473 padvise (cmdp
->redirect
, "unable to pipe");
1474 cmdp
->direction
= STDIO
;
1477 SIGNAL (SIGPIPE
, pipeser
);
1482 padios (NULL
, "unknown redirection for command");
1486 if (dup2 (fileno (cmdp
->stream
), fileno (stdout
)) == NOTOK
)
1487 padios ("standard output", "unable to dup2");
1495 fin_io (struct Cmd
*cmdp
, int vio
)
1507 finaux_io (struct Cmd
*cmdp
)
1509 switch (cmdp
->direction
) {
1516 close (fileno (stdout
));
1517 if (ferror (stdout
))
1518 padvise (NULL
, "problems writing %s", cmdp
->redirect
);
1519 fclose (cmdp
->stream
);
1524 close (fileno (stdout
));
1525 pclose (cmdp
->stream
);
1526 SIGNAL (SIGPIPE
, SIG_DFL
);
1530 padios (NULL
, "unknown redirection for command");
1533 if (dup2 (fileno (sp
), fileno (stdout
)) == NOTOK
)
1534 padios ("standard output", "unable to dup2");
1537 cmdp
->direction
= STDIO
;
1546 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1547 unset_selected (mp
, msgnum
);
1548 mp
->lowsel
= mp
->hghsel
= mp
->numsel
= 0;
1556 folder_free (mp
); /* free folder/message structure */
1562 seq_setcur (struct msgs
*mp
, int msgnum
)
1564 if (mp
->curmsg
== msgnum
)
1567 if (mp
->curmsg
&& Msgs
[mp
->curmsg
].m_scanl
) {
1568 free (Msgs
[mp
->curmsg
].m_scanl
);
1569 Msgs
[mp
->curmsg
].m_scanl
= NULL
;
1571 if (Msgs
[msgnum
].m_scanl
) {
1572 free (Msgs
[msgnum
].m_scanl
);
1573 Msgs
[msgnum
].m_scanl
= NULL
;
1576 mp
->curmsg
= msgnum
;
1594 if (broken_pipe
++ == 0)
1595 fprintf (stderr
, "broken pipe\n");
1614 longjmp (peerenv
, DONE
);
1623 struct record rcs
, *rc
;
1628 switch (peer2rc (rc
)) {
1631 while (isspace (*bp
))
1633 if (sscanf (bp
, "%d", &vrsn
) != 1) {
1635 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
1638 if (vrsn
!= RC_VRSN
) {
1639 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
1643 while (*bp
&& !isspace (*bp
))
1645 while (isspace (*bp
))
1647 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
1652 for (i
= 1; i
<= numwins
; i
++) {
1653 while (*bp
&& !isspace (*bp
))
1655 while (isspace (*bp
))
1657 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
1660 rc2peer (RC_ACK
, 0, NULL
);
1664 padios (NULL
, "%s", rc
->rc_data
);
1667 fmt2peer (RC_ERR
, "pINI protocol screw-up");
1668 done (1); /* NOTREACHED */
1671 return 1; /* dead code to satisfy the compiler */
1676 pQRY (char *str
, int scansw
)
1679 if (pQRY1 (scansw
) == NOTOK
|| pQRY2 () == NOTOK
)
1682 rc2peer (RC_EOF
, 0, NULL
);
1691 static int lastlow
= 0,
1696 oldhgh
= mp
->hghmsg
;
1697 if (check_folder (scansw
) && oldhgh
< mp
->hghmsg
) {
1698 switch (winX (STATUS
)) {
1703 printf ("new messages have arrived!");
1706 _exit (0); /* NOTREACHED */
1709 lastlow
= lastcur
= lasthgh
= lastnum
= 0;
1713 switch (winX (DISPLAY
)) {
1718 scanrange (oldhgh
+ 1, mp
->hghmsg
);
1721 _exit (0); /* NOTREACHED */
1730 switch (winX (STATUS
)) {
1735 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1736 myname
? myname
: fmsh
? fmsh
: mp
->foldpath
, gap
- 1,
1737 readid (mp
->lowmsg
));
1740 _exit (0); /* NOTREACHED */
1747 if (mp
->lowmsg
!= lastlow
1748 || mp
->curmsg
!= lastcur
1749 || mp
->hghmsg
!= lasthgh
1750 || mp
->nummsg
!= lastnum
)
1751 switch (winX (STATUS
)) {
1759 _exit (0); /* NOTREACHED */
1762 lastlow
= mp
->lowmsg
;
1763 lastcur
= mp
->curmsg
;
1764 lasthgh
= mp
->hghmsg
;
1765 lastnum
= mp
->nummsg
;
1776 int i
, j
, k
, msgnum
, n
;
1782 if (mp
->nummsg
== 0 && mp
->nummsg
!= num
)
1783 switch (winX (SCAN
)) {
1791 _exit (0); /* NOTREACHED */
1800 j
= (k
= windows
[SCAN
]) / 2;
1801 for (msgnum
= mp
->curmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1802 if (does_exist (mp
, msgnum
))
1806 k
= i
>= k
? 1 : k
- i
;
1813 for (msgnum
= mp
->curmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1814 if (does_exist (mp
, msgnum
)) {
1821 for (msgnum
= mp
->curmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1822 if (does_exist (mp
, msgnum
)) {
1826 if (n
++ >= windows
[SCAN
])
1832 && does_exist (mp
, lo
)
1833 && does_exist (mp
, hi
)
1835 || (lo
== mp
->curmsg
&& lo
== mp
->lowmsg
))
1837 || (hi
== mp
->curmsg
&& hi
== mp
->hghmsg
))
1838 && hi
- lo
== j
- i
)
1841 if (mp
->curmsg
!= cur
|| modified
)
1842 switch (winN (NULLCMD
, SCAN
, 0)) {
1850 scanrange (lo
= i
, hi
= j
);
1861 pCMD (char *str
, struct swit
*sw
, struct Cmd
*cmdp
)
1866 switch (winX (DISPLAY
)) {
1871 printf ("commands:\n");
1872 print_sw (ALL
, sw
, "", stdout
);
1873 printf ("type ``quit'' to leave %s\n", invo_name
);
1876 _exit (0); /* NOTREACHED */
1879 rc2peer (RC_EOF
, 0, NULL
);
1883 if (parse (str
, cmdp
) == NOTOK
)
1886 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1888 switch (winX (DISPLAY
)) {
1893 ambigsw (cmdp
->args
[0], sw
);
1896 _exit (0); /* NOTREACHED */
1899 rc2peer (RC_EOF
, 0, NULL
);
1905 "say what: ``%s'' -- type ? (or help) for help",
1920 switch (m_setjmp (peerenv
)) {
1922 SIGNAL (SIGALRM
, alrmser
);
1925 status
= peerwait ();
1939 struct record rcs
, *rc
;
1944 switch (peer2rc (rc
)) {
1947 rc2peer (RC_FIN
, 0, NULL
);
1951 advise (NULL
, "%s", rc
->rc_data
);
1955 fmt2peer (RC_FIN
, "pLOOP protocol screw-up");
1962 ttyNaux (struct Cmd
*cmdp
, char *s
)
1964 struct record rcs
, *rc
;
1969 if (cmdp
&& init_io (cmdp
, vmh
) == NOTOK
)
1972 /* XXX: fseek() too tricky for our own good */
1974 fseek (fp
, 0L, SEEK_SET
);
1977 switch (rc2rc (RC_TTY
, s
? strlen (s
) : 0, s
, rc
)) {
1979 vmhtty
= OK
; /* fall */
1984 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1987 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1988 done (1); /* NOTREACHED */
1992 SIGNAL (SIGTSTP
, tstat
);
1999 ttyR (struct Cmd
*cmdp
)
2001 struct record rcs
, *rc
;
2006 SIGNAL (SIGTSTP
, SIG_IGN
);
2018 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2020 rc2peer (RC_EOF
, 0, NULL
);
2024 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
2027 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
2028 done (1); /* NOTREACHED */
2031 return 1; /* dead code to satisfy compiler */
2036 winN (struct Cmd
*cmdp
, int n
, int eof
)
2039 char buffer
[BUFSIZ
];
2040 struct record rcs
, *rc
;
2043 if (vmhpid
== NOTOK
)
2048 /* XXX: fseek() too tricky for our own good */
2050 fseek (fp
, 0L, SEEK_SET
);
2054 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2055 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2063 padios (NULL
, "%s", rc
->rc_data
);
2066 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2070 if (pipe (pd
) == NOTOK
) {
2071 err2peer (RC_ERR
, "pipe", "unable to");
2075 switch (vmhpid
= fork()) {
2077 err2peer (RC_ERR
, "fork", "unable to");
2084 SIGNAL (SIGPIPE
, SIG_IGN
);
2085 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2086 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2094 advise (NULL
, "%s", rc
->rc_data
);
2098 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2102 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2105 rc2peer (RC_EOF
, 0, NULL
);
2110 advise (NULL
, "%s", rc
->rc_data
);
2115 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2120 err2peer (RC_ERR
, "pipe", "error reading from");
2122 _exit (i
!= NOTOK
? i
: 1);
2125 if ((vmhfd0
= dup (fileno (stdin
))) == NOTOK
)
2126 padios ("standard input", "unable to dup");
2127 if ((vmhfd1
= dup (fileno (stdout
))) == NOTOK
)
2128 padios ("standard output", "unable to dup");
2129 if ((vmhfd2
= dup (fileno (stderr
))) == NOTOK
)
2130 padios ("diagnostic output", "unable to dup");
2133 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2134 dup2 (i
, fileno (stdin
));
2139 if (dup2 (pd
[1], fileno (stdout
)) == NOTOK
)
2140 padios ("standard output", "unable to dup2");
2144 if (dup2 (pd
[1], fileno (stderr
)) == NOTOK
)
2145 padios ("diagnostic output", "unable to dup2");
2148 if (cmdp
&& init_io (cmdp
, 0) == NOTOK
)
2150 pstat
= SIGNAL (SIGPIPE
, pipeser
);
2162 winR (struct Cmd
*cmdp
)
2172 if (dup2 (vmhfd0
, fileno (stdin
)) == NOTOK
)
2173 padios ("standard input", "unable to dup2");
2178 if (dup2 (vmhfd1
, fileno (stdout
)) == NOTOK
)
2179 padios ("standard output", "unable to dup2");
2184 if (dup2 (vmhfd2
, fileno (stderr
)) == NOTOK
)
2185 padios ("diagnostic output", "unable to dup2");
2189 SIGNAL (SIGPIPE
, pstat
);
2191 if ((status
= pidwait (vmhpid
, OK
)) == 2)
2195 return (status
== 0 ? OK
: NOTOK
);
2203 char buffer
[BUFSIZ
];
2204 struct record rcs
, *rc
;
2209 /* XXX: fseek() too tricky for our own good */
2211 fseek (fp
, 0L, SEEK_SET
);
2213 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2214 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2222 padios (NULL
, "%s", rc
->rc_data
);
2225 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2229 if (pipe (pd
) == NOTOK
) {
2230 err2peer (RC_ERR
, "pipe", "unable to");
2234 switch (pid
= fork ()) {
2236 err2peer (RC_ERR
, "fork", "unable to");
2242 close (fileno (stdin
));
2243 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2244 dup2 (i
, fileno (stdin
));
2247 dup2 (pd
[1], fileno (stdout
));
2248 dup2 (pd
[1], fileno (stderr
));
2256 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2257 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2267 padios (NULL
, "%s", rc
->rc_data
);
2270 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2274 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2279 padios (NULL
, "%s", rc
->rc_data
);
2282 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2286 err2peer (RC_ERR
, "pipe", "error reading from");
2290 return (i
!= NOTOK
? pid
: NOTOK
);
2296 padios (char *what
, char *fmt
, ...)
2302 verr2peer (RC_FIN
, what
, fmt
, ap
);
2305 advertise (what
, NULL
, fmt
, ap
);
2314 padvise (char *what
, char *fmt
, ...)
2320 verr2peer (RC_ERR
, what
, fmt
, ap
);
2322 advertise (what
, NULL
, fmt
, ap
);