]>
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 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 int SOprintf (char *, ...); /* from termsbr.c */
157 int sc_width (void); /* from termsbr.c */
158 void fsetup (char *);
160 FILE *msh_ready (int, int);
163 void display_info (int);
166 void seq_setcur (struct msgs
*, int);
167 void padios (char *, char *, ...);
168 void padvise (char *, char *, ...);
174 static void msh (int);
175 static int read_map (char *, long);
176 static int read_file (long, int);
178 static void m_gMsgs (int);
179 FILE *msh_ready (int, int);
180 static int check_folder (int);
181 static void scanrange (int, int);
182 static void scanstring (char *);
183 static void write_ids (void);
184 static void quit (void);
185 static int getargs (char *, struct swit
*, struct Cmd
*);
186 static int getcmds (struct swit
*, struct Cmd
*, int);
187 static int parse (char *, struct Cmd
*);
188 static int init_io (struct Cmd
*, int);
189 static int initaux_io (struct Cmd
*);
190 static void fin_io (struct Cmd
*, int);
191 static void finaux_io (struct Cmd
*);
192 static void m_init (void);
193 static void intrser (int);
194 static void pipeser (int);
195 static void quitser (int);
196 static void alrmser (int);
197 static int pINI (void);
198 static int pQRY (char *, int);
199 static int pQRY1 (int);
200 static int pQRY2 (void);
201 static int pCMD (char *, struct swit
*, struct Cmd
*);
202 static int pFIN (void);
203 static int peerwait (void);
204 static int ttyNaux (struct Cmd
*, char *);
205 static int ttyR (struct Cmd
*);
206 static int winN (struct Cmd
*, int, int);
207 static int winR (struct Cmd
*);
208 static int winX (int);
212 main (int argc
, char **argv
)
214 int id
= 0, scansw
= 0, vmh1
= 0, vmh2
= 0;
215 char *cp
, *file
= NULL
, *folder
= NULL
;
216 char **argp
, **arguments
, buf
[BUFSIZ
];
219 setlocale(LC_ALL
, "");
221 invo_name
= r1bindex (argv
[0], '/');
223 /* read user profile/context */
226 mts_init (invo_name
);
227 arguments
= getarguments (invo_name
, argc
,argv
, 1);
230 while ((cp
= *argp
++)) {
232 switch (smatch (++cp
, switches
)) {
234 ambigsw (cp
, switches
);
237 adios (NULL
, "-%s unknown", cp
);
240 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
241 print_help (buf
, switches
, 1);
244 print_version(invo_name
);
248 if (!(cp
= *argp
++) || *cp
== '-')
249 adios (NULL
, "missing argument to %s", argp
[-2]);
250 if ((id
= atoi (cp
)) < 1)
251 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
254 if (!(cp
= *argp
++) || *cp
== '-')
255 adios (NULL
, "missing argument to %s", argp
[-2]);
256 if ((pfd
= atoi (cp
)) <= 1)
257 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
260 if (!(cp
= *argp
++) || *cp
== '-')
261 adios (NULL
, "missing argument to %s", argp
[-2]);
262 if ((ppid
= atoi (cp
)) <= 1)
263 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
266 if (!(myname
= *argp
++) || *myname
== '-')
267 adios (NULL
, "missing argument to %s", argp
[-2]);
278 if (!(myprompt
= *argp
++) || *myprompt
== '-')
279 adios (NULL
, "missing argument to %s", argp
[-2]);
283 if (!(cp
= *argp
++) || *cp
== '-')
284 adios (NULL
, "missing argument to %s", argp
[-2]);
285 if ((vmh1
= atoi (cp
)) < 1)
286 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
289 if (!(cp
= *argp
++) || *cp
== '-')
290 adios (NULL
, "missing argument to %s", argp
[-2]);
291 if ((vmh2
= atoi (cp
)) < 1)
292 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
296 if (!(cp
= *argp
++) || *cp
== '-')
297 adios (NULL
, "missing argument to %s", argp
[-2]);
300 if (!(cp
= *argp
++) || *cp
== '-')
301 adios (NULL
, "missing argument to %s", argp
[-2]);
311 if (*cp
== '+' || *cp
== '@') {
313 adios (NULL
, "only one folder at a time!");
315 folder
= pluspath (cp
);
319 adios (NULL
, "only one file at a time!");
324 if (!file
&& !folder
)
327 adios (NULL
, "use a file or a folder, not both");
328 strncpy (myfilter
, etcpath (mhlforward
), sizeof(myfilter
));
331 ioctl (pfd
, FIOCLEX
, NULL
);
334 istat
= SIGNAL2 (SIGINT
, intrser
);
335 qstat
= SIGNAL2 (SIGQUIT
, quitser
);
337 sc_width (); /* MAGIC... */
339 if ((vmh
= vmh1
&& vmh2
)) {
342 SIGNAL (SIGINT
, SIG_IGN
);
343 SIGNAL (SIGQUIT
, SIG_IGN
);
345 tstat
= SIGNAL (SIGTSTP
, SIG_IGN
);
354 display_info (id
> 0 ? scansw
: 0);
356 msh (id
> 0 ? scansw
: 0);
365 static struct swit mshcmds
[] = {
430 register char *cp
, **ap
;
431 char prompt
[BUFSIZ
], *vec
[MAXARGS
];
433 register struct Cmd
*cmdp
;
434 static int once_only
= ADVCMD
;
436 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
445 if ((i
= getcmds (mshcmds
, cmdp
, scansw
)) == EOF
) {
450 check_folder (scansw
);
451 if ((i
= getargs (prompt
, mshcmds
, cmdp
)) == EOF
) {
456 cmd_name
= mshcmds
[i
].sw
;
464 if (once_only
== ADVCMD
)
465 once_only
= i
= SHOWCMD
;
467 i
= mp
->curmsg
!= mp
->hghmsg
? NEXTCMD
: EXITCMD
;
468 cmd_name
= mshcmds
[i
].sw
;
474 case FORWCMD
: /* sigh */
484 if ((cp
= context_find (cmd_name
))) {
486 ap
= brkstring (cp
, " ", "\n");
487 ap
= copyip (ap
, vec
, MAXARGS
);
498 copyip (cmdp
->args
+ 1, ap
, MAXARGS
);
502 if (!vmh
&& init_io (cmdp
, vmh
) == NOTOK
) {
508 redirected
= vmh
|| cmdp
->direction
!= STDIO
;
519 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
520 forkcmd (vec
, cmd_name
);
524 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
529 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
535 || (filehak (vec
) == OK
? ttyN (cmdp
)
536 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
541 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
546 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
551 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
557 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
562 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
569 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
575 || (packhak (vec
) == OK
? ttyN (cmdp
)
576 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
581 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
586 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
591 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
596 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
601 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
606 padios (NULL
, "no dispatch for %s", cmd_name
);
628 fsetup (char *folder
)
634 maildir
= m_maildir (folder
);
635 if (chdir (maildir
) == NOTOK
)
636 padios (maildir
, "unable to change directory to");
638 /* read folder and create message structure */
639 if (!(mp
= folder_read (folder
)))
640 padios (NULL
, "unable to read folder %s", folder
);
642 /* check for empty folder */
644 padios (NULL
, "no messages in %s", folder
);
647 mtime
= stat (mp
->foldpath
, &st
) != NOTOK
? st
.st_mtime
: 0;
649 m_gMsgs (mp
->hghmsg
);
651 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
652 Msgs
[msgnum
].m_bboard_id
= 0;
653 Msgs
[msgnum
].m_top
= NOTOK
;
654 Msgs
[msgnum
].m_start
= Msgs
[msgnum
].m_stop
= 0L;
655 Msgs
[msgnum
].m_scanl
= NULL
;
660 fmsh
= getcpy (folder
);
662 maxfds
= OPEN_MAX
/ 2;
664 if ((maxfds
-= 2) < 1)
674 if ((fp
= fopen (file
, "r")) == NULL
)
675 padios (file
, "unable to read");
677 ioctl (fileno (fp
), FIOCLEX
, NULL
);
679 if (fstat (fileno (fp
), &st
) != NOTOK
) {
680 mode
= (int) (st
.st_mode
& 0777), mtime
= st
.st_mtime
;
681 msgp
= read_map (file
, (long) st
.st_size
);
684 mode
= m_gmprot (), mtime
= 0;
688 if ((msgp
= read_file (msgp
? Msgs
[msgp
].m_stop
: 0L, msgp
+ 1)) < 1)
689 padios (NULL
, "no messages in %s", myname
? myname
: file
);
691 if (!(mp
= (struct msgs
*) calloc ((size_t) 1, sizeof(*mp
))))
692 padios (NULL
, "unable to allocate folder storage");
694 if (!(mp
->msgstats
= calloc ((size_t) msgp
+ 3, sizeof(*(mp
->msgstats
)))))
695 padios (NULL
, "unable to allocate message status storage");
701 mp
->foldpath
= getcpy (myname
? myname
: file
);
702 clear_folder_flags (mp
);
705 if (st
.st_uid
!= getuid () || access (file
, W_OK
) == NOTOK
)
709 mp
->hghoff
= mp
->hghmsg
+ 1;
711 for (i
= mp
->lowmsg
; i
<= mp
->hghmsg
; i
++) {
712 clear_msg_flags (mp
, i
);
717 mp
->msgattrs
[0] = getcpy ("unseen");
718 mp
->msgattrs
[1] = NULL
;
720 m_unknown (fp
); /* the MAGIC invocation */
729 read_map (char *file
, long size
)
731 register int i
, msgp
;
732 register struct drop
*dp
, *mp
;
735 if ((i
= map_read (file
, size
, &rp
, 1)) == 0)
741 for (dp
= rp
+ 1; i
-- > 0; msgp
++, dp
++) {
742 mp
= &Msgs
[msgp
].m_drop
;
744 mp
->d_size
= dp
->d_size
;
745 mp
->d_start
= dp
->d_start
;
746 mp
->d_stop
= dp
->d_stop
;
747 Msgs
[msgp
].m_scanl
= NULL
;
756 read_file (long pos
, int msgp
)
759 register struct drop
*dp
, *mp
;
762 if ((i
= mbx_read (fp
, pos
, &rp
, 1)) <= 0)
765 m_gMsgs ((msgp
- 1) + i
);
767 for (dp
= rp
; i
-- > 0; msgp
++, dp
++) {
768 mp
= &Msgs
[msgp
].m_drop
;
770 mp
->d_size
= dp
->d_size
;
771 mp
->d_start
= dp
->d_start
;
772 mp
->d_stop
= dp
->d_stop
;
773 Msgs
[msgp
].m_scanl
= NULL
;
787 nMsgs
= n
+ MAXFOLDER
/ 2;
788 Msgs
= (struct Msg
*) calloc ((size_t) (nMsgs
+ 2), sizeof *Msgs
);
790 padios (NULL
, "unable to allocate Msgs structure");
797 nmsgs
= nMsgs
+ n
+ MAXFOLDER
/ 2;
798 Msgs
= (struct Msg
*) mh_xrealloc ((char *) Msgs
, (size_t) (nmsgs
+ 2) * sizeof *Msgs
);
799 memset((char *) (Msgs
+ nMsgs
+ 2), 0, (size_t) ((nmsgs
- nMsgs
) * sizeof *Msgs
));
806 msh_ready (int msgnum
, int full
)
818 if ((fd
= Msgs
[msgnum
].m_top
) == NOTOK
) {
819 if (numfds
>= maxfds
)
820 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++)
821 if (Msgs
[msgp
].m_top
!= NOTOK
) {
822 close (Msgs
[msgp
].m_top
);
823 Msgs
[msgp
].m_top
= NOTOK
;
828 if ((fd
= open (cp
= m_name (msgnum
), O_RDONLY
)) == NOTOK
)
829 padios (cp
, "unable to open message");
830 Msgs
[msgnum
].m_top
= fd
;
834 if ((fd
= dup (fd
)) == NOTOK
)
835 padios ("cached message", "unable to dup");
836 if ((yp
= fdopen (fd
, "r")) == NULL
)
837 padios (NULL
, "unable to fdopen cached message");
838 fseek (yp
, 0L, SEEK_SET
);
842 m_eomsbr ((int (*)()) 0); /* XXX */
843 fseek (fp
, Msgs
[msgnum
].m_start
, SEEK_SET
);
849 check_folder (int scansw
)
851 int seqnum
, i
, low
, hgh
, msgp
;
855 if (stat (mp
->foldpath
, &st
) == NOTOK
)
856 padios (mp
->foldpath
, "unable to stat");
857 if (mtime
== st
.st_mtime
)
861 low
= mp
->hghmsg
+ 1;
862 folder_free (mp
); /* free folder/message structure */
864 if (!(mp
= folder_read (fmsh
)))
865 padios (NULL
, "unable to re-read folder %s", fmsh
);
869 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++) {
870 if (Msgs
[msgp
].m_top
!= NOTOK
) {
871 close (Msgs
[msgp
].m_top
);
872 Msgs
[msgp
].m_top
= NOTOK
;
875 if (Msgs
[msgp
].m_scanl
) {
876 free (Msgs
[msgp
].m_scanl
);
877 Msgs
[msgp
].m_scanl
= NULL
;
883 if (modified
|| low
> hgh
)
887 if (fstat (fileno (fp
), &st
) == NOTOK
)
888 padios (mp
->foldpath
, "unable to fstat");
889 if (mtime
== st
.st_mtime
)
891 mode
= (int) (st
.st_mode
& 0777);
894 if ((msgp
= read_file (Msgs
[mp
->hghmsg
].m_stop
, mp
->hghmsg
+ 1)) < 1)
895 padios (NULL
, "no messages in %s", mp
->foldpath
); /* XXX */
896 if (msgp
>= MAXFOLDER
)
897 padios (NULL
, "more than %d messages in %s", MAXFOLDER
,
899 if (msgp
<= mp
->hghmsg
)
902 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgp
)))
903 padios (NULL
, "unable to allocate folder storage");
905 low
= mp
->hghmsg
+ 1, hgh
= msgp
;
906 seqnum
= scansw
? seq_getnum (mp
, "unseen") : -1;
907 for (i
= mp
->hghmsg
+ 1; i
<= msgp
; i
++) {
910 add_sequence(mp
, seqnum
, i
);
920 advise (NULL
, "new messages have arrived!\007");
922 scanrange (low
, hgh
);
929 scanrange (int low
, int hgh
)
933 snprintf (buffer
, sizeof(buffer
), "%d-%d", low
, hgh
);
939 scanstring (char *arg
)
941 char *cp
, **ap
, *vec
[MAXARGS
];
944 * This should be replace with a call to getarguments()
946 if ((cp
= context_find (cmd_name
= "scan"))) {
948 ap
= brkstring (cp
, " ", "\n");
949 ap
= copyip (ap
, vec
, MAXARGS
);
965 register int cur
, seqnum
, i
=0, msgnum
;
968 seq_setcur (mp
, mp
->lowmsg
);
969 if (id
<= 0 || (seqnum
= seq_getnum (mp
, "unseen")) == -1)
972 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
973 add_sequence(mp
, seqnum
, msgnum
);
978 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
979 if (does_exist(mp
, msgnum
)) /* FIX */
980 if ((i
= readid (msgnum
)) > 0 && i
< id
) {
982 clear_sequence(mp
, seqnum
, msgnum
);
985 for (i
= mp
->lowmsg
; i
< msgnum
; i
++)
986 clear_sequence(mp
, seqnum
, i
);
988 if (cur
> mp
->hghmsg
)
991 seq_setcur (mp
, cur
);
994 if ((gap
= 1 < id
&& id
< (i
= readid (mp
->lowmsg
)) ? id
: 0) && !vmh
)
995 advise (NULL
, "gap in ID:s, last seen %d, lowest present %d\n",
1004 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
1007 if (Msgs
[msgnum
].m_bboard_id
)
1008 return Msgs
[msgnum
].m_bboard_id
;
1010 zp
= msh_ready (msgnum
, 0);
1012 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
)) {
1016 if (!mh_strcasecmp (name
, BBoard_ID
)) {
1018 while (state
== FLDPLUS
) {
1019 state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
);
1025 return (Msgs
[msgnum
].m_bboard_id
= i
);
1029 while (state
== FLDPLUS
)
1030 state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
);
1031 if (state
!= FLDEOF
)
1041 display_info (int scansw
)
1045 interactive
= isatty (fileno (stdout
));
1047 if ((sd
= dup (fileno (stdout
))) == NOTOK
)
1048 padios ("standard output", "unable to dup");
1050 ioctl (sd
, FIOCLEX
, NULL
);
1051 #endif /* FIOCLEX */
1052 if ((sp
= fdopen (sd
, "w")) == NULL
)
1053 padios ("standard output", "unable to fdopen");
1056 m_putenv ("mhfolder", mp
->foldpath
);
1061 printf ("Reading ");
1062 if (SOprintf ("%s", myname
))
1063 printf ("%s", myname
);
1064 printf (", currently at message %d of %d\n",
1065 mp
->curmsg
, mp
->hghmsg
);
1068 printf ("Reading ");
1070 printf ("+%s", fmsh
);
1072 printf ("%s", mp
->foldpath
);
1073 printf (", currently at message %d of %d\n",
1074 mp
->curmsg
, mp
->hghmsg
);
1077 if (((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1079 && in_sequence(mp
, seqnum
, mp
->hghmsg
))
1080 scanstring ("unseen");
1087 int i
= 0, seqnum
, msgnum
;
1093 if ((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1094 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1095 if (!in_sequence(mp
, seqnum
, msgnum
)) {
1096 if (Msgs
[msgnum
].m_bboard_id
== 0)
1098 if ((i
= Msgs
[msgnum
].m_bboard_id
) > 0)
1102 snprintf (buffer
, sizeof(buffer
), "%d %d\n", i
, Msgs
[mp
->hghmsg
].m_bboard_id
);
1103 write (pfd
, buffer
, sizeof(buffer
));
1113 char *cp
, tmpfil
[BUFSIZ
];
1114 char map1
[BUFSIZ
], map2
[BUFSIZ
];
1118 if (!(mp
->msgflags
& MODIFIED
) || is_readonly(mp
) || fmsh
) {
1120 rc2peer (RC_FIN
, 0, NULL
);
1125 ttyNaux (NULLCMD
, "FAST");
1127 if ((dp
= lkfopen (mp
->foldpath
, "r")) == NULL
) {
1128 advise (mp
->foldpath
, "unable to lock");
1135 if (fstat (fileno (dp
), &st
) == NOTOK
) {
1136 advise (mp
->foldpath
, "unable to stat");
1139 if (mtime
!= st
.st_mtime
) {
1140 advise (NULL
, "new messages have arrived, no update");
1143 mode
= (int) (st
.st_mode
& 0777);
1145 if (mp
->nummsg
== 0) {
1146 cp
= concat ("Zero file \"", mp
->foldpath
, "\"? ", NULL
);
1147 if (getanswer (cp
)) {
1148 if ((i
= creat (mp
->foldpath
, mode
)) != NOTOK
)
1151 advise (mp
->foldpath
, "error zero'ing");
1152 unlink (map_name (mp
->foldpath
));/* XXX */
1157 cp
= concat ("Update file \"", mp
->foldpath
, "\"? ", NULL
);
1158 if (!getanswer (cp
))
1160 strncpy (tmpfil
, m_backup (mp
->foldpath
), sizeof(tmpfil
));
1161 if ((md
= mbx_open (tmpfil
, mbx_style
, st
.st_uid
, st
.st_gid
, mode
)) == NOTOK
) {
1162 advise (tmpfil
, "unable to open");
1166 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1167 if (does_exist(mp
, msgnum
) && pack (tmpfil
, md
, msgnum
) == NOTOK
) {
1168 mbx_close (tmpfil
, md
);
1170 unlink (map_name (tmpfil
));
1173 mbx_close (tmpfil
, md
);
1175 if (rename (tmpfil
, mp
->foldpath
) == NOTOK
)
1176 admonish (mp
->foldpath
, "unable to rename %s to", tmpfil
);
1178 strncpy (map1
, map_name (tmpfil
), sizeof(map1
));
1179 strncpy (map2
, map_name (mp
->foldpath
), sizeof(map2
));
1181 if (rename (map1
, map2
) == NOTOK
) {
1182 admonish (map2
, "unable to rename %s to", map1
);
1191 lkfclose (dp
, mp
->foldpath
);
1200 getargs (char *prompt
, struct swit
*sw
, struct Cmd
*cmdp
)
1204 static char buffer
[BUFSIZ
];
1210 printf ("%s", prompt
);
1213 for (cp
= buffer
; (i
= getchar ()) != '\n';) {
1214 if (interrupted
&& !told_to_quit
) {
1219 if (told_to_quit
|| i
== EOF
) {
1222 kill (ppid
, SIGEMT
);
1224 kill (ppid
, SIGTERM
);
1228 if (cp
< &buffer
[sizeof buffer
- 2])
1235 if (buffer
[0] == '?') {
1236 printf ("commands:\n");
1237 print_sw (ALL
, sw
, "", stdout
);
1238 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1243 if (parse (buffer
, cmdp
) == NOTOK
)
1246 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1248 ambigsw (cmdp
->args
[0], sw
);
1251 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1262 getcmds (struct swit
*sw
, struct Cmd
*cmdp
, int scansw
)
1265 struct record rcs
, *rc
;
1271 switch (peer2rc (rc
)) {
1273 pQRY (rc
->rc_data
, scansw
);
1277 if ((i
= pCMD (rc
->rc_data
, sw
, cmdp
)) != NOTOK
)
1284 kill (ppid
, SIGEMT
);
1286 kill (ppid
, SIGTERM
);
1291 padios (NULL
, "%s", rc
->rc_data
);
1294 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
1301 parse (char *buffer
, struct Cmd
*cmdp
)
1304 unsigned char c
, *cp
;
1308 pp
= cmdp
->args
[argp
++] = cmdp
->line
;
1309 cmdp
->redirect
= NULL
;
1310 cmdp
->direction
= STDIO
;
1311 cmdp
->stream
= NULL
;
1313 for (cp
= buffer
; (c
= *cp
); cp
++) {
1319 fmt2peer (RC_EOF
, "null command");
1323 while ((c
= *cp
++)) {
1330 cmdp
->args
[argp
++] = pp
;
1337 switch (c
= *cp
++) {
1339 padvise (NULL
, "unmatched \"");
1344 if ((c
= *cp
++) == 0)
1355 if ((c
= *cp
++) == 0) {
1357 padvise (NULL
, "the newline character can not be quoted");
1367 if (pp
== cmdp
->line
) {
1368 padvise (NULL
, "invalid null command");
1371 if (*cmdp
->args
[argp
- 1] == 0)
1373 cmdp
->direction
= c
== '>' ? CRTIO
: PIPIO
;
1374 if (cmdp
->direction
== CRTIO
&& (c
= *cp
) == '>') {
1375 cmdp
->direction
= APPIO
;
1378 cmdp
->redirect
= pp
+ 1;/* sigh */
1379 for (; (c
= *cp
); cp
++)
1383 padvise (NULL
, cmdp
->direction
!= PIPIO
1384 ? "missing name for redirect"
1385 : "invalid null command");
1388 strcpy (cmdp
->redirect
, cp
);
1389 if (cmdp
->direction
!= PIPIO
) {
1391 if (isspace (*cp
)) {
1392 padvise (NULL
, "bad name for redirect");
1395 if (expand (cmdp
->redirect
) == NOTOK
)
1404 cmdp
->args
[argp
] = NULL
;
1411 expand (char *redirect
)
1417 if (*redirect
!= '~')
1420 if ((cp
= strchr(pp
= redirect
+ 1, '/')))
1425 if ((pw
= getpwnam (pp
)))
1428 padvise (NULL
, "unknown user: %s", pp
);
1432 snprintf (path
, sizeof(path
), "%s/%s", pp
, cp
? cp
: "");
1433 strcpy (redirect
, path
);
1439 init_io (struct Cmd
*cmdp
, int vio
)
1446 result
= initaux_io (cmdp
);
1454 initaux_io (struct Cmd
*cmdp
)
1458 switch (cmdp
->direction
) {
1464 mode
= cmdp
->direction
== CRTIO
? "write" : "append";
1465 if ((cmdp
->stream
= fopen (cmdp
->redirect
, mode
)) == NULL
) {
1466 padvise (cmdp
->redirect
, "unable to %s ", mode
);
1467 cmdp
->direction
= STDIO
;
1473 if ((cmdp
->stream
= popen (cmdp
->redirect
, "w")) == NULL
) {
1474 padvise (cmdp
->redirect
, "unable to pipe");
1475 cmdp
->direction
= STDIO
;
1478 SIGNAL (SIGPIPE
, pipeser
);
1483 padios (NULL
, "unknown redirection for command");
1487 if (dup2 (fileno (cmdp
->stream
), fileno (stdout
)) == NOTOK
)
1488 padios ("standard output", "unable to dup2");
1496 fin_io (struct Cmd
*cmdp
, int vio
)
1508 finaux_io (struct Cmd
*cmdp
)
1510 switch (cmdp
->direction
) {
1517 close (fileno (stdout
));
1518 if (ferror (stdout
))
1519 padvise (NULL
, "problems writing %s", cmdp
->redirect
);
1520 fclose (cmdp
->stream
);
1525 close (fileno (stdout
));
1526 pclose (cmdp
->stream
);
1527 SIGNAL (SIGPIPE
, SIG_DFL
);
1531 padios (NULL
, "unknown redirection for command");
1534 if (dup2 (fileno (sp
), fileno (stdout
)) == NOTOK
)
1535 padios ("standard output", "unable to dup2");
1538 cmdp
->direction
= STDIO
;
1547 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1548 unset_selected (mp
, msgnum
);
1549 mp
->lowsel
= mp
->hghsel
= mp
->numsel
= 0;
1557 folder_free (mp
); /* free folder/message structure */
1563 seq_setcur (struct msgs
*mp
, int msgnum
)
1565 if (mp
->curmsg
== msgnum
)
1568 if (mp
->curmsg
&& Msgs
[mp
->curmsg
].m_scanl
) {
1569 free (Msgs
[mp
->curmsg
].m_scanl
);
1570 Msgs
[mp
->curmsg
].m_scanl
= NULL
;
1572 if (Msgs
[msgnum
].m_scanl
) {
1573 free (Msgs
[msgnum
].m_scanl
);
1574 Msgs
[msgnum
].m_scanl
= NULL
;
1577 mp
->curmsg
= msgnum
;
1593 if (broken_pipe
++ == 0)
1594 fprintf (stderr
, "broken pipe\n");
1611 longjmp (peerenv
, DONE
);
1620 struct record rcs
, *rc
;
1625 switch (peer2rc (rc
)) {
1628 while (isspace (*bp
))
1630 if (sscanf (bp
, "%d", &vrsn
) != 1) {
1632 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
1635 if (vrsn
!= RC_VRSN
) {
1636 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
1640 while (*bp
&& !isspace (*bp
))
1642 while (isspace (*bp
))
1644 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
1649 for (i
= 1; i
<= numwins
; i
++) {
1650 while (*bp
&& !isspace (*bp
))
1652 while (isspace (*bp
))
1654 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
1657 rc2peer (RC_ACK
, 0, NULL
);
1661 padios (NULL
, "%s", rc
->rc_data
);
1664 fmt2peer (RC_ERR
, "pINI protocol screw-up");
1665 done (1); /* NOTREACHED */
1668 return 1; /* dead code to satisfy the compiler */
1673 pQRY (char *str
, int scansw
)
1675 if (pQRY1 (scansw
) == NOTOK
|| pQRY2 () == NOTOK
)
1678 rc2peer (RC_EOF
, 0, NULL
);
1687 static int lastlow
= 0,
1692 oldhgh
= mp
->hghmsg
;
1693 if (check_folder (scansw
) && oldhgh
< mp
->hghmsg
) {
1694 switch (winX (STATUS
)) {
1699 printf ("new messages have arrived!");
1702 _exit (0); /* NOTREACHED */
1705 lastlow
= lastcur
= lasthgh
= lastnum
= 0;
1709 switch (winX (DISPLAY
)) {
1714 scanrange (oldhgh
+ 1, mp
->hghmsg
);
1717 _exit (0); /* NOTREACHED */
1726 switch (winX (STATUS
)) {
1731 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1732 myname
? myname
: fmsh
? fmsh
: mp
->foldpath
, gap
- 1,
1733 readid (mp
->lowmsg
));
1736 _exit (0); /* NOTREACHED */
1743 if (mp
->lowmsg
!= lastlow
1744 || mp
->curmsg
!= lastcur
1745 || mp
->hghmsg
!= lasthgh
1746 || mp
->nummsg
!= lastnum
)
1747 switch (winX (STATUS
)) {
1755 _exit (0); /* NOTREACHED */
1758 lastlow
= mp
->lowmsg
;
1759 lastcur
= mp
->curmsg
;
1760 lasthgh
= mp
->hghmsg
;
1761 lastnum
= mp
->nummsg
;
1772 int i
, j
, k
, msgnum
, n
;
1778 if (mp
->nummsg
== 0 && mp
->nummsg
!= num
)
1779 switch (winX (SCAN
)) {
1787 _exit (0); /* NOTREACHED */
1796 j
= (k
= windows
[SCAN
]) / 2;
1797 for (msgnum
= mp
->curmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1798 if (does_exist (mp
, msgnum
))
1802 k
= i
>= k
? 1 : k
- i
;
1809 for (msgnum
= mp
->curmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1810 if (does_exist (mp
, msgnum
)) {
1817 for (msgnum
= mp
->curmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1818 if (does_exist (mp
, msgnum
)) {
1822 if (n
++ >= windows
[SCAN
])
1828 && does_exist (mp
, lo
)
1829 && does_exist (mp
, hi
)
1831 || (lo
== mp
->curmsg
&& lo
== mp
->lowmsg
))
1833 || (hi
== mp
->curmsg
&& hi
== mp
->hghmsg
))
1834 && hi
- lo
== j
- i
)
1837 if (mp
->curmsg
!= cur
|| modified
)
1838 switch (winN (NULLCMD
, SCAN
, 0)) {
1846 scanrange (lo
= i
, hi
= j
);
1857 pCMD (char *str
, struct swit
*sw
, struct Cmd
*cmdp
)
1862 switch (winX (DISPLAY
)) {
1867 printf ("commands:\n");
1868 print_sw (ALL
, sw
, "", stdout
);
1869 printf ("type ``quit'' to leave %s\n", invo_name
);
1872 _exit (0); /* NOTREACHED */
1875 rc2peer (RC_EOF
, 0, NULL
);
1879 if (parse (str
, cmdp
) == NOTOK
)
1882 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1884 switch (winX (DISPLAY
)) {
1889 ambigsw (cmdp
->args
[0], sw
);
1892 _exit (0); /* NOTREACHED */
1895 rc2peer (RC_EOF
, 0, NULL
);
1901 "say what: ``%s'' -- type ? (or help) for help",
1916 switch (setjmp (peerenv
)) {
1918 SIGNAL (SIGALRM
, alrmser
);
1921 status
= peerwait ();
1935 struct record rcs
, *rc
;
1940 switch (peer2rc (rc
)) {
1943 rc2peer (RC_FIN
, 0, NULL
);
1947 advise (NULL
, "%s", rc
->rc_data
);
1951 fmt2peer (RC_FIN
, "pLOOP protocol screw-up");
1958 ttyNaux (struct Cmd
*cmdp
, char *s
)
1960 struct record rcs
, *rc
;
1965 if (cmdp
&& init_io (cmdp
, vmh
) == NOTOK
)
1968 /* XXX: fseek() too tricky for our own good */
1970 fseek (fp
, 0L, SEEK_SET
);
1973 switch (rc2rc (RC_TTY
, s
? strlen (s
) : 0, s
, rc
)) {
1975 vmhtty
= OK
; /* fall */
1980 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1983 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1984 done (1); /* NOTREACHED */
1988 SIGNAL (SIGTSTP
, tstat
);
1995 ttyR (struct Cmd
*cmdp
)
1997 struct record rcs
, *rc
;
2002 SIGNAL (SIGTSTP
, SIG_IGN
);
2014 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2016 rc2peer (RC_EOF
, 0, NULL
);
2020 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
2023 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
2024 done (1); /* NOTREACHED */
2027 return 1; /* dead code to satisfy compiler */
2032 winN (struct Cmd
*cmdp
, int n
, int eof
)
2035 char buffer
[BUFSIZ
];
2036 struct record rcs
, *rc
;
2039 if (vmhpid
== NOTOK
)
2044 /* XXX: fseek() too tricky for our own good */
2046 fseek (fp
, 0L, SEEK_SET
);
2050 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2051 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2059 padios (NULL
, "%s", rc
->rc_data
);
2062 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2066 if (pipe (pd
) == NOTOK
) {
2067 err2peer (RC_ERR
, "pipe", "unable to");
2071 switch (vmhpid
= fork()) {
2073 err2peer (RC_ERR
, "fork", "unable to");
2080 SIGNAL (SIGPIPE
, SIG_IGN
);
2081 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2082 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2090 advise (NULL
, "%s", rc
->rc_data
);
2094 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2098 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2101 rc2peer (RC_EOF
, 0, NULL
);
2106 advise (NULL
, "%s", rc
->rc_data
);
2111 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2116 err2peer (RC_ERR
, "pipe", "error reading from");
2118 _exit (i
!= NOTOK
? i
: 1);
2121 if ((vmhfd0
= dup (fileno (stdin
))) == NOTOK
)
2122 padios ("standard input", "unable to dup");
2123 if ((vmhfd1
= dup (fileno (stdout
))) == NOTOK
)
2124 padios ("standard output", "unable to dup");
2125 if ((vmhfd2
= dup (fileno (stderr
))) == NOTOK
)
2126 padios ("diagnostic output", "unable to dup");
2129 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2130 dup2 (i
, fileno (stdin
));
2135 if (dup2 (pd
[1], fileno (stdout
)) == NOTOK
)
2136 padios ("standard output", "unable to dup2");
2140 if (dup2 (pd
[1], fileno (stderr
)) == NOTOK
)
2141 padios ("diagnostic output", "unable to dup2");
2144 if (cmdp
&& init_io (cmdp
, 0) == NOTOK
)
2146 pstat
= SIGNAL (SIGPIPE
, pipeser
);
2158 winR (struct Cmd
*cmdp
)
2168 if (dup2 (vmhfd0
, fileno (stdin
)) == NOTOK
)
2169 padios ("standard input", "unable to dup2");
2174 if (dup2 (vmhfd1
, fileno (stdout
)) == NOTOK
)
2175 padios ("standard output", "unable to dup2");
2180 if (dup2 (vmhfd2
, fileno (stderr
)) == NOTOK
)
2181 padios ("diagnostic output", "unable to dup2");
2185 SIGNAL (SIGPIPE
, pstat
);
2187 if ((status
= pidwait (vmhpid
, OK
)) == 2)
2191 return (status
== 0 ? OK
: NOTOK
);
2199 char buffer
[BUFSIZ
];
2200 struct record rcs
, *rc
;
2205 /* XXX: fseek() too tricky for our own good */
2207 fseek (fp
, 0L, SEEK_SET
);
2209 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2210 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2218 padios (NULL
, "%s", rc
->rc_data
);
2221 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2225 if (pipe (pd
) == NOTOK
) {
2226 err2peer (RC_ERR
, "pipe", "unable to");
2230 switch (pid
= fork ()) {
2232 err2peer (RC_ERR
, "fork", "unable to");
2238 close (fileno (stdin
));
2239 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2240 dup2 (i
, fileno (stdin
));
2243 dup2 (pd
[1], fileno (stdout
));
2244 dup2 (pd
[1], fileno (stderr
));
2252 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2253 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2263 padios (NULL
, "%s", rc
->rc_data
);
2266 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2270 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2275 padios (NULL
, "%s", rc
->rc_data
);
2278 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2282 err2peer (RC_ERR
, "pipe", "error reading from");
2286 return (i
!= NOTOK
? pid
: NOTOK
);
2292 padios (char *what
, char *fmt
, ...)
2298 verr2peer (RC_FIN
, what
, fmt
, ap
);
2301 advertise (what
, NULL
, fmt
, ap
);
2310 padvise (char *what
, char *fmt
, ...)
2316 verr2peer (RC_ERR
, what
, fmt
, ap
);
2318 advertise (what
, NULL
, fmt
, ap
);