]>
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 */
154 int should_intr
; /* signal handler should interrupt call */
155 jmp_buf sigenv
; /* the environment pointer */
161 int SOprintf (char *, ...); /* from termsbr.c */
162 int sc_width (void); /* from termsbr.c */
163 void fsetup (char *);
165 FILE *msh_ready (int, int);
168 void display_info (int);
171 void seq_setcur (struct msgs
*, int);
172 void padios (char *, char *, ...);
173 void padvise (char *, char *, ...);
179 static void msh (int);
180 static int read_map (char *, long);
181 static int read_file (long, int);
183 static void m_gMsgs (int);
184 FILE *msh_ready (int, int);
185 static int check_folder (int);
186 static void scanrange (int, int);
187 static void scanstring (char *);
188 static void write_ids (void);
189 static void quit (void);
190 static int getargs (char *, struct swit
*, struct Cmd
*);
191 static int getcmds (struct swit
*, struct Cmd
*, int);
192 static int parse (char *, struct Cmd
*);
193 static int init_io (struct Cmd
*, int);
194 static int initaux_io (struct Cmd
*);
195 static void fin_io (struct Cmd
*, int);
196 static void finaux_io (struct Cmd
*);
197 static void m_init (void);
198 static RETSIGTYPE
intrser (int);
199 static RETSIGTYPE
pipeser (int);
200 static RETSIGTYPE
quitser (int);
201 static RETSIGTYPE
alrmser (int);
202 static int pINI (void);
203 static int pQRY (char *, int);
204 static int pQRY1 (int);
205 static int pQRY2 (void);
206 static int pCMD (char *, struct swit
*, struct Cmd
*);
207 static int pFIN (void);
208 static int peerwait (void);
209 static int ttyNaux (struct Cmd
*, char *);
210 static int ttyR (struct Cmd
*);
211 static int winN (struct Cmd
*, int, int);
212 static int winR (struct Cmd
*);
213 static int winX (int);
217 main (int argc
, char **argv
)
219 int id
= 0, scansw
= 0, vmh1
= 0, vmh2
= 0;
220 char *cp
, *file
= NULL
, *folder
= NULL
;
221 char **argp
, **arguments
, buf
[BUFSIZ
];
224 setlocale(LC_ALL
, "");
226 invo_name
= r1bindex (argv
[0], '/');
228 /* read user profile/context */
231 mts_init (invo_name
);
232 arguments
= getarguments (invo_name
, argc
,argv
, 1);
235 while ((cp
= *argp
++)) {
237 switch (smatch (++cp
, switches
)) {
239 ambigsw (cp
, switches
);
242 adios (NULL
, "-%s unknown", cp
);
245 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
246 print_help (buf
, switches
, 1);
249 print_version(invo_name
);
253 if (!(cp
= *argp
++) || *cp
== '-')
254 adios (NULL
, "missing argument to %s", argp
[-2]);
255 if ((id
= atoi (cp
)) < 1)
256 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
259 if (!(cp
= *argp
++) || *cp
== '-')
260 adios (NULL
, "missing argument to %s", argp
[-2]);
261 if ((pfd
= atoi (cp
)) <= 1)
262 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
265 if (!(cp
= *argp
++) || *cp
== '-')
266 adios (NULL
, "missing argument to %s", argp
[-2]);
267 if ((ppid
= atoi (cp
)) <= 1)
268 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
271 if (!(myname
= *argp
++) || *myname
== '-')
272 adios (NULL
, "missing argument to %s", argp
[-2]);
283 if (!(myprompt
= *argp
++) || *myprompt
== '-')
284 adios (NULL
, "missing argument to %s", argp
[-2]);
288 if (!(cp
= *argp
++) || *cp
== '-')
289 adios (NULL
, "missing argument to %s", argp
[-2]);
290 if ((vmh1
= atoi (cp
)) < 1)
291 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
294 if (!(cp
= *argp
++) || *cp
== '-')
295 adios (NULL
, "missing argument to %s", argp
[-2]);
296 if ((vmh2
= atoi (cp
)) < 1)
297 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
301 if (!(cp
= *argp
++) || *cp
== '-')
302 adios (NULL
, "missing argument to %s", argp
[-2]);
305 if (!(cp
= *argp
++) || *cp
== '-')
306 adios (NULL
, "missing argument to %s", argp
[-2]);
316 if (*cp
== '+' || *cp
== '@') {
318 adios (NULL
, "only one folder at a time!");
320 folder
= pluspath (cp
);
324 adios (NULL
, "only one file at a time!");
329 if (!file
&& !folder
)
332 adios (NULL
, "use a file or a folder, not both");
333 strncpy (myfilter
, etcpath (mhlforward
), sizeof(myfilter
));
336 ioctl (pfd
, FIOCLEX
, NULL
);
342 istat
= SIGNAL2 (SIGINT
, intrser
);
343 qstat
= SIGNAL2 (SIGQUIT
, quitser
);
345 sc_width (); /* MAGIC... */
347 if ((vmh
= vmh1
&& vmh2
)) {
350 SIGNAL (SIGINT
, SIG_IGN
);
351 SIGNAL (SIGQUIT
, SIG_IGN
);
353 tstat
= SIGNAL (SIGTSTP
, SIG_IGN
);
362 display_info (id
> 0 ? scansw
: 0);
364 msh (id
> 0 ? scansw
: 0);
373 static struct swit mshcmds
[] = {
438 register char *cp
, **ap
;
439 char prompt
[BUFSIZ
], *vec
[MAXARGS
];
441 register struct Cmd
*cmdp
;
442 static int once_only
= ADVCMD
;
444 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
453 if ((i
= getcmds (mshcmds
, cmdp
, scansw
)) == EOF
) {
458 check_folder (scansw
);
459 if ((i
= getargs (prompt
, mshcmds
, cmdp
)) == EOF
) {
464 cmd_name
= mshcmds
[i
].sw
;
472 if (once_only
== ADVCMD
)
473 once_only
= i
= SHOWCMD
;
475 i
= mp
->curmsg
!= mp
->hghmsg
? NEXTCMD
: EXITCMD
;
476 cmd_name
= mshcmds
[i
].sw
;
482 case FORWCMD
: /* sigh */
492 if ((cp
= context_find (cmd_name
))) {
494 ap
= brkstring (cp
, " ", "\n");
495 ap
= copyip (ap
, vec
, MAXARGS
);
506 copyip (cmdp
->args
+ 1, ap
, MAXARGS
);
510 if (!vmh
&& init_io (cmdp
, vmh
) == NOTOK
) {
516 redirected
= vmh
|| cmdp
->direction
!= STDIO
;
527 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
528 forkcmd (vec
, cmd_name
);
532 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
537 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
543 || (filehak (vec
) == OK
? ttyN (cmdp
)
544 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
549 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
554 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
559 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
565 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
570 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
577 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
583 || (packhak (vec
) == OK
? ttyN (cmdp
)
584 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
589 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
594 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
599 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
604 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
609 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
614 padios (NULL
, "no dispatch for %s", cmd_name
);
636 fsetup (char *folder
)
642 maildir
= m_maildir (folder
);
643 if (chdir (maildir
) == NOTOK
)
644 padios (maildir
, "unable to change directory to");
646 /* read folder and create message structure */
647 if (!(mp
= folder_read (folder
)))
648 padios (NULL
, "unable to read folder %s", folder
);
650 /* check for empty folder */
652 padios (NULL
, "no messages in %s", folder
);
655 mtime
= stat (mp
->foldpath
, &st
) != NOTOK
? st
.st_mtime
: 0;
657 m_gMsgs (mp
->hghmsg
);
659 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
660 Msgs
[msgnum
].m_bboard_id
= 0;
661 Msgs
[msgnum
].m_top
= NOTOK
;
662 Msgs
[msgnum
].m_start
= Msgs
[msgnum
].m_stop
= 0L;
663 Msgs
[msgnum
].m_scanl
= NULL
;
668 fmsh
= getcpy (folder
);
670 maxfds
= OPEN_MAX
/ 2;
672 if ((maxfds
-= 2) < 1)
682 if ((fp
= fopen (file
, "r")) == NULL
)
683 padios (file
, "unable to read");
685 ioctl (fileno (fp
), FIOCLEX
, NULL
);
687 if (fstat (fileno (fp
), &st
) != NOTOK
) {
688 mode
= (int) (st
.st_mode
& 0777), mtime
= st
.st_mtime
;
689 msgp
= read_map (file
, (long) st
.st_size
);
692 mode
= m_gmprot (), mtime
= 0;
696 if ((msgp
= read_file (msgp
? Msgs
[msgp
].m_stop
: 0L, msgp
+ 1)) < 1)
697 padios (NULL
, "no messages in %s", myname
? myname
: file
);
699 if (!(mp
= (struct msgs
*) calloc ((size_t) 1, sizeof(*mp
))))
700 padios (NULL
, "unable to allocate folder storage");
702 if (!(mp
->msgstats
= calloc ((size_t) msgp
+ 3, sizeof(*(mp
->msgstats
)))))
703 padios (NULL
, "unable to allocate message status storage");
709 mp
->foldpath
= getcpy (myname
? myname
: file
);
710 clear_folder_flags (mp
);
713 if (st
.st_uid
!= getuid () || access (file
, W_OK
) == NOTOK
)
717 mp
->hghoff
= mp
->hghmsg
+ 1;
719 for (i
= mp
->lowmsg
; i
<= mp
->hghmsg
; i
++) {
720 clear_msg_flags (mp
, i
);
725 mp
->msgattrs
[0] = getcpy ("unseen");
726 mp
->msgattrs
[1] = NULL
;
728 m_unknown (fp
); /* the MAGIC invocation */
737 read_map (char *file
, long size
)
739 register int i
, msgp
;
740 register struct drop
*dp
, *mp
;
743 if ((i
= map_read (file
, size
, &rp
, 1)) == 0)
749 for (dp
= rp
+ 1; i
-- > 0; msgp
++, dp
++) {
750 mp
= &Msgs
[msgp
].m_drop
;
752 mp
->d_size
= dp
->d_size
;
753 mp
->d_start
= dp
->d_start
;
754 mp
->d_stop
= dp
->d_stop
;
755 Msgs
[msgp
].m_scanl
= NULL
;
764 read_file (long pos
, int msgp
)
767 register struct drop
*dp
, *mp
;
770 if ((i
= mbx_read (fp
, pos
, &rp
, 1)) <= 0)
773 m_gMsgs ((msgp
- 1) + i
);
775 for (dp
= rp
; i
-- > 0; msgp
++, dp
++) {
776 mp
= &Msgs
[msgp
].m_drop
;
778 mp
->d_size
= dp
->d_size
;
779 mp
->d_start
= dp
->d_start
;
780 mp
->d_stop
= dp
->d_stop
;
781 Msgs
[msgp
].m_scanl
= NULL
;
795 nMsgs
= n
+ MAXFOLDER
/ 2;
796 Msgs
= (struct Msg
*) calloc ((size_t) (nMsgs
+ 2), sizeof *Msgs
);
798 padios (NULL
, "unable to allocate Msgs structure");
805 nmsgs
= nMsgs
+ n
+ MAXFOLDER
/ 2;
806 Msgs
= (struct Msg
*) mh_xrealloc ((char *) Msgs
, (size_t) (nmsgs
+ 2) * sizeof *Msgs
);
807 memset((char *) (Msgs
+ nMsgs
+ 2), 0, (size_t) ((nmsgs
- nMsgs
) * sizeof *Msgs
));
814 msh_ready (int msgnum
, int full
)
826 if ((fd
= Msgs
[msgnum
].m_top
) == NOTOK
) {
827 if (numfds
>= maxfds
)
828 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++)
829 if (Msgs
[msgp
].m_top
!= NOTOK
) {
830 close (Msgs
[msgp
].m_top
);
831 Msgs
[msgp
].m_top
= NOTOK
;
836 if ((fd
= open (cp
= m_name (msgnum
), O_RDONLY
)) == NOTOK
)
837 padios (cp
, "unable to open message");
838 Msgs
[msgnum
].m_top
= fd
;
842 if ((fd
= dup (fd
)) == NOTOK
)
843 padios ("cached message", "unable to dup");
844 if ((yp
= fdopen (fd
, "r")) == NULL
)
845 padios (NULL
, "unable to fdopen cached message");
846 fseek (yp
, 0L, SEEK_SET
);
850 m_eomsbr ((int (*)()) 0); /* XXX */
851 fseek (fp
, Msgs
[msgnum
].m_start
, SEEK_SET
);
857 check_folder (int scansw
)
859 int seqnum
, i
, low
, hgh
, msgp
;
863 if (stat (mp
->foldpath
, &st
) == NOTOK
)
864 padios (mp
->foldpath
, "unable to stat");
865 if (mtime
== st
.st_mtime
)
869 low
= mp
->hghmsg
+ 1;
870 folder_free (mp
); /* free folder/message structure */
872 if (!(mp
= folder_read (fmsh
)))
873 padios (NULL
, "unable to re-read folder %s", fmsh
);
877 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++) {
878 if (Msgs
[msgp
].m_top
!= NOTOK
) {
879 close (Msgs
[msgp
].m_top
);
880 Msgs
[msgp
].m_top
= NOTOK
;
883 if (Msgs
[msgp
].m_scanl
) {
884 free (Msgs
[msgp
].m_scanl
);
885 Msgs
[msgp
].m_scanl
= NULL
;
891 if (modified
|| low
> hgh
)
895 if (fstat (fileno (fp
), &st
) == NOTOK
)
896 padios (mp
->foldpath
, "unable to fstat");
897 if (mtime
== st
.st_mtime
)
899 mode
= (int) (st
.st_mode
& 0777);
902 if ((msgp
= read_file (Msgs
[mp
->hghmsg
].m_stop
, mp
->hghmsg
+ 1)) < 1)
903 padios (NULL
, "no messages in %s", mp
->foldpath
); /* XXX */
904 if (msgp
>= MAXFOLDER
)
905 padios (NULL
, "more than %d messages in %s", MAXFOLDER
,
907 if (msgp
<= mp
->hghmsg
)
910 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgp
)))
911 padios (NULL
, "unable to allocate folder storage");
913 low
= mp
->hghmsg
+ 1, hgh
= msgp
;
914 seqnum
= scansw
? seq_getnum (mp
, "unseen") : -1;
915 for (i
= mp
->hghmsg
+ 1; i
<= msgp
; i
++) {
918 add_sequence(mp
, seqnum
, i
);
928 advise (NULL
, "new messages have arrived!\007");
930 scanrange (low
, hgh
);
937 scanrange (int low
, int hgh
)
941 snprintf (buffer
, sizeof(buffer
), "%d-%d", low
, hgh
);
947 scanstring (char *arg
)
949 char *cp
, **ap
, *vec
[MAXARGS
];
952 * This should be replace with a call to getarguments()
954 if ((cp
= context_find (cmd_name
= "scan"))) {
956 ap
= brkstring (cp
, " ", "\n");
957 ap
= copyip (ap
, vec
, MAXARGS
);
973 register int cur
, seqnum
, i
=0, msgnum
;
976 seq_setcur (mp
, mp
->lowmsg
);
977 if (id
<= 0 || (seqnum
= seq_getnum (mp
, "unseen")) == -1)
980 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
981 add_sequence(mp
, seqnum
, msgnum
);
986 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
987 if (does_exist(mp
, msgnum
)) /* FIX */
988 if ((i
= readid (msgnum
)) > 0 && i
< id
) {
990 clear_sequence(mp
, seqnum
, msgnum
);
993 for (i
= mp
->lowmsg
; i
< msgnum
; i
++)
994 clear_sequence(mp
, seqnum
, i
);
996 if (cur
> mp
->hghmsg
)
999 seq_setcur (mp
, cur
);
1002 if ((gap
= 1 < id
&& id
< (i
= readid (mp
->lowmsg
)) ? id
: 0) && !vmh
)
1003 advise (NULL
, "gap in ID:s, last seen %d, lowest present %d\n",
1012 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
1015 if (Msgs
[msgnum
].m_bboard_id
)
1016 return Msgs
[msgnum
].m_bboard_id
;
1018 zp
= msh_ready (msgnum
, 0);
1020 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
)) {
1024 if (!mh_strcasecmp (name
, BBoard_ID
)) {
1026 while (state
== FLDPLUS
) {
1027 state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
);
1033 return (Msgs
[msgnum
].m_bboard_id
= i
);
1037 while (state
== FLDPLUS
)
1038 state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
);
1039 if (state
!= FLDEOF
)
1049 display_info (int scansw
)
1053 interactive
= isatty (fileno (stdout
));
1055 if ((sd
= dup (fileno (stdout
))) == NOTOK
)
1056 padios ("standard output", "unable to dup");
1057 #ifndef BSD42 /* XXX */
1059 ioctl (sd
, FIOCLEX
, NULL
);
1060 #endif /* FIOCLEX */
1061 #endif /* not BSD42 */
1062 if ((sp
= fdopen (sd
, "w")) == NULL
)
1063 padios ("standard output", "unable to fdopen");
1066 m_putenv ("mhfolder", mp
->foldpath
);
1071 printf ("Reading ");
1072 if (SOprintf ("%s", myname
))
1073 printf ("%s", myname
);
1074 printf (", currently at message %d of %d\n",
1075 mp
->curmsg
, mp
->hghmsg
);
1078 printf ("Reading ");
1080 printf ("+%s", fmsh
);
1082 printf ("%s", mp
->foldpath
);
1083 printf (", currently at message %d of %d\n",
1084 mp
->curmsg
, mp
->hghmsg
);
1087 if (((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1089 && in_sequence(mp
, seqnum
, mp
->hghmsg
))
1090 scanstring ("unseen");
1097 int i
= 0, seqnum
, msgnum
;
1103 if ((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1104 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1105 if (!in_sequence(mp
, seqnum
, msgnum
)) {
1106 if (Msgs
[msgnum
].m_bboard_id
== 0)
1108 if ((i
= Msgs
[msgnum
].m_bboard_id
) > 0)
1112 snprintf (buffer
, sizeof(buffer
), "%d %d\n", i
, Msgs
[mp
->hghmsg
].m_bboard_id
);
1113 write (pfd
, buffer
, sizeof(buffer
));
1123 char *cp
, tmpfil
[BUFSIZ
];
1124 char map1
[BUFSIZ
], map2
[BUFSIZ
];
1128 if (!(mp
->msgflags
& MODIFIED
) || is_readonly(mp
) || fmsh
) {
1130 rc2peer (RC_FIN
, 0, NULL
);
1135 ttyNaux (NULLCMD
, "FAST");
1137 if ((dp
= lkfopen (mp
->foldpath
, "r")) == NULL
) {
1138 advise (mp
->foldpath
, "unable to lock");
1145 if (fstat (fileno (dp
), &st
) == NOTOK
) {
1146 advise (mp
->foldpath
, "unable to stat");
1149 if (mtime
!= st
.st_mtime
) {
1150 advise (NULL
, "new messages have arrived, no update");
1153 mode
= (int) (st
.st_mode
& 0777);
1155 if (mp
->nummsg
== 0) {
1156 cp
= concat ("Zero file \"", mp
->foldpath
, "\"? ", NULL
);
1157 if (getanswer (cp
)) {
1158 if ((i
= creat (mp
->foldpath
, mode
)) != NOTOK
)
1161 advise (mp
->foldpath
, "error zero'ing");
1162 unlink (map_name (mp
->foldpath
));/* XXX */
1167 cp
= concat ("Update file \"", mp
->foldpath
, "\"? ", NULL
);
1168 if (!getanswer (cp
))
1170 strncpy (tmpfil
, m_backup (mp
->foldpath
), sizeof(tmpfil
));
1171 if ((md
= mbx_open (tmpfil
, mbx_style
, st
.st_uid
, st
.st_gid
, mode
)) == NOTOK
) {
1172 advise (tmpfil
, "unable to open");
1176 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1177 if (does_exist(mp
, msgnum
) && pack (tmpfil
, md
, msgnum
) == NOTOK
) {
1178 mbx_close (tmpfil
, md
);
1180 unlink (map_name (tmpfil
));
1183 mbx_close (tmpfil
, md
);
1185 if (rename (tmpfil
, mp
->foldpath
) == NOTOK
)
1186 admonish (mp
->foldpath
, "unable to rename %s to", tmpfil
);
1188 strncpy (map1
, map_name (tmpfil
), sizeof(map1
));
1189 strncpy (map2
, map_name (mp
->foldpath
), sizeof(map2
));
1191 if (rename (map1
, map2
) == NOTOK
) {
1192 admonish (map2
, "unable to rename %s to", map1
);
1201 lkfclose (dp
, mp
->foldpath
);
1210 getargs (char *prompt
, struct swit
*sw
, struct Cmd
*cmdp
)
1214 static char buffer
[BUFSIZ
];
1220 switch (setjmp (sigenv
)) {
1227 if (interrupted
&& !told_to_quit
) {
1233 kill (ppid
, SIGEMT
);
1235 kill (ppid
, SIGTERM
);
1241 printf ("%s", prompt
);
1244 for (cp
= buffer
; (i
= getchar ()) != '\n';) {
1246 if (interrupted
&& !told_to_quit
) {
1251 if (told_to_quit
|| i
== EOF
) {
1254 kill (ppid
, SIGEMT
);
1256 kill (ppid
, SIGTERM
);
1262 longjmp (sigenv
, DONE
);
1264 if (cp
< &buffer
[sizeof buffer
- 2])
1271 if (buffer
[0] == '?') {
1272 printf ("commands:\n");
1273 print_sw (ALL
, sw
, "", stdout
);
1274 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1279 if (parse (buffer
, cmdp
) == NOTOK
)
1282 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1284 ambigsw (cmdp
->args
[0], sw
);
1287 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1301 getcmds (struct swit
*sw
, struct Cmd
*cmdp
, int scansw
)
1304 struct record rcs
, *rc
;
1310 switch (peer2rc (rc
)) {
1312 pQRY (rc
->rc_data
, scansw
);
1316 if ((i
= pCMD (rc
->rc_data
, sw
, cmdp
)) != NOTOK
)
1323 kill (ppid
, SIGEMT
);
1325 kill (ppid
, SIGTERM
);
1330 padios (NULL
, "%s", rc
->rc_data
);
1333 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
1340 parse (char *buffer
, struct Cmd
*cmdp
)
1343 unsigned char c
, *cp
;
1347 pp
= cmdp
->args
[argp
++] = cmdp
->line
;
1348 cmdp
->redirect
= NULL
;
1349 cmdp
->direction
= STDIO
;
1350 cmdp
->stream
= NULL
;
1352 for (cp
= buffer
; (c
= *cp
); cp
++) {
1358 fmt2peer (RC_EOF
, "null command");
1362 while ((c
= *cp
++)) {
1369 cmdp
->args
[argp
++] = pp
;
1376 switch (c
= *cp
++) {
1378 padvise (NULL
, "unmatched \"");
1383 if ((c
= *cp
++) == 0)
1394 if ((c
= *cp
++) == 0) {
1396 padvise (NULL
, "the newline character can not be quoted");
1406 if (pp
== cmdp
->line
) {
1407 padvise (NULL
, "invalid null command");
1410 if (*cmdp
->args
[argp
- 1] == 0)
1412 cmdp
->direction
= c
== '>' ? CRTIO
: PIPIO
;
1413 if (cmdp
->direction
== CRTIO
&& (c
= *cp
) == '>') {
1414 cmdp
->direction
= APPIO
;
1417 cmdp
->redirect
= pp
+ 1;/* sigh */
1418 for (; (c
= *cp
); cp
++)
1422 padvise (NULL
, cmdp
->direction
!= PIPIO
1423 ? "missing name for redirect"
1424 : "invalid null command");
1427 strcpy (cmdp
->redirect
, cp
);
1428 if (cmdp
->direction
!= PIPIO
) {
1430 if (isspace (*cp
)) {
1431 padvise (NULL
, "bad name for redirect");
1434 if (expand (cmdp
->redirect
) == NOTOK
)
1443 cmdp
->args
[argp
] = NULL
;
1450 expand (char *redirect
)
1456 if (*redirect
!= '~')
1459 if ((cp
= strchr(pp
= redirect
+ 1, '/')))
1464 if ((pw
= getpwnam (pp
)))
1467 padvise (NULL
, "unknown user: %s", pp
);
1471 snprintf (path
, sizeof(path
), "%s/%s", pp
, cp
? cp
: "");
1472 strcpy (redirect
, path
);
1478 init_io (struct Cmd
*cmdp
, int vio
)
1485 result
= initaux_io (cmdp
);
1493 initaux_io (struct Cmd
*cmdp
)
1497 switch (cmdp
->direction
) {
1503 mode
= cmdp
->direction
== CRTIO
? "write" : "append";
1504 if ((cmdp
->stream
= fopen (cmdp
->redirect
, mode
)) == NULL
) {
1505 padvise (cmdp
->redirect
, "unable to %s ", mode
);
1506 cmdp
->direction
= STDIO
;
1512 if ((cmdp
->stream
= popen (cmdp
->redirect
, "w")) == NULL
) {
1513 padvise (cmdp
->redirect
, "unable to pipe");
1514 cmdp
->direction
= STDIO
;
1517 SIGNAL (SIGPIPE
, pipeser
);
1522 padios (NULL
, "unknown redirection for command");
1526 if (dup2 (fileno (cmdp
->stream
), fileno (stdout
)) == NOTOK
)
1527 padios ("standard output", "unable to dup2");
1535 fin_io (struct Cmd
*cmdp
, int vio
)
1547 finaux_io (struct Cmd
*cmdp
)
1549 switch (cmdp
->direction
) {
1556 close (fileno (stdout
));
1557 if (ferror (stdout
))
1558 padvise (NULL
, "problems writing %s", cmdp
->redirect
);
1559 fclose (cmdp
->stream
);
1564 close (fileno (stdout
));
1565 pclose (cmdp
->stream
);
1566 SIGNAL (SIGPIPE
, SIG_DFL
);
1570 padios (NULL
, "unknown redirection for command");
1573 if (dup2 (fileno (sp
), fileno (stdout
)) == NOTOK
)
1574 padios ("standard output", "unable to dup2");
1577 cmdp
->direction
= STDIO
;
1586 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1587 unset_selected (mp
, msgnum
);
1588 mp
->lowsel
= mp
->hghsel
= mp
->numsel
= 0;
1596 folder_free (mp
); /* free folder/message structure */
1602 seq_setcur (struct msgs
*mp
, int msgnum
)
1604 if (mp
->curmsg
== msgnum
)
1607 if (mp
->curmsg
&& Msgs
[mp
->curmsg
].m_scanl
) {
1608 free (Msgs
[mp
->curmsg
].m_scanl
);
1609 Msgs
[mp
->curmsg
].m_scanl
= NULL
;
1611 if (Msgs
[msgnum
].m_scanl
) {
1612 free (Msgs
[msgnum
].m_scanl
);
1613 Msgs
[msgnum
].m_scanl
= NULL
;
1616 mp
->curmsg
= msgnum
;
1624 #ifndef RELIABLE_SIGNALS
1625 SIGNAL (SIGINT
, intrser
);
1633 longjmp (sigenv
, NOTOK
);
1641 #ifndef RELIABLE_SIGNALS
1642 SIGNAL (SIGPIPE
, pipeser
);
1645 if (broken_pipe
++ == 0)
1646 fprintf (stderr
, "broken pipe\n");
1652 longjmp (sigenv
, NOTOK
);
1660 #ifndef RELIABLE_SIGNALS
1661 SIGNAL (SIGQUIT
, quitser
);
1669 longjmp (sigenv
, NOTOK
);
1677 longjmp (peerenv
, DONE
);
1686 struct record rcs
, *rc
;
1691 switch (peer2rc (rc
)) {
1694 while (isspace (*bp
))
1696 if (sscanf (bp
, "%d", &vrsn
) != 1) {
1698 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
1701 if (vrsn
!= RC_VRSN
) {
1702 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
1706 while (*bp
&& !isspace (*bp
))
1708 while (isspace (*bp
))
1710 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
1715 for (i
= 1; i
<= numwins
; i
++) {
1716 while (*bp
&& !isspace (*bp
))
1718 while (isspace (*bp
))
1720 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
1723 rc2peer (RC_ACK
, 0, NULL
);
1727 padios (NULL
, "%s", rc
->rc_data
);
1730 fmt2peer (RC_ERR
, "pINI protocol screw-up");
1731 done (1); /* NOTREACHED */
1734 return 1; /* dead code to satisfy the compiler */
1739 pQRY (char *str
, int scansw
)
1741 if (pQRY1 (scansw
) == NOTOK
|| pQRY2 () == NOTOK
)
1744 rc2peer (RC_EOF
, 0, NULL
);
1753 static int lastlow
= 0,
1758 oldhgh
= mp
->hghmsg
;
1759 if (check_folder (scansw
) && oldhgh
< mp
->hghmsg
) {
1760 switch (winX (STATUS
)) {
1765 printf ("new messages have arrived!");
1768 _exit (0); /* NOTREACHED */
1771 lastlow
= lastcur
= lasthgh
= lastnum
= 0;
1775 switch (winX (DISPLAY
)) {
1780 scanrange (oldhgh
+ 1, mp
->hghmsg
);
1783 _exit (0); /* NOTREACHED */
1792 switch (winX (STATUS
)) {
1797 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1798 myname
? myname
: fmsh
? fmsh
: mp
->foldpath
, gap
- 1,
1799 readid (mp
->lowmsg
));
1802 _exit (0); /* NOTREACHED */
1809 if (mp
->lowmsg
!= lastlow
1810 || mp
->curmsg
!= lastcur
1811 || mp
->hghmsg
!= lasthgh
1812 || mp
->nummsg
!= lastnum
)
1813 switch (winX (STATUS
)) {
1821 _exit (0); /* NOTREACHED */
1824 lastlow
= mp
->lowmsg
;
1825 lastcur
= mp
->curmsg
;
1826 lasthgh
= mp
->hghmsg
;
1827 lastnum
= mp
->nummsg
;
1838 int i
, j
, k
, msgnum
, n
;
1844 if (mp
->nummsg
== 0 && mp
->nummsg
!= num
)
1845 switch (winX (SCAN
)) {
1853 _exit (0); /* NOTREACHED */
1862 j
= (k
= windows
[SCAN
]) / 2;
1863 for (msgnum
= mp
->curmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1864 if (does_exist (mp
, msgnum
))
1868 k
= i
>= k
? 1 : k
- i
;
1875 for (msgnum
= mp
->curmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1876 if (does_exist (mp
, msgnum
)) {
1883 for (msgnum
= mp
->curmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1884 if (does_exist (mp
, msgnum
)) {
1888 if (n
++ >= windows
[SCAN
])
1894 && does_exist (mp
, lo
)
1895 && does_exist (mp
, hi
)
1897 || (lo
== mp
->curmsg
&& lo
== mp
->lowmsg
))
1899 || (hi
== mp
->curmsg
&& hi
== mp
->hghmsg
))
1900 && hi
- lo
== j
- i
)
1903 if (mp
->curmsg
!= cur
|| modified
)
1904 switch (winN (NULLCMD
, SCAN
, 0)) {
1912 scanrange (lo
= i
, hi
= j
);
1923 pCMD (char *str
, struct swit
*sw
, struct Cmd
*cmdp
)
1928 switch (winX (DISPLAY
)) {
1933 printf ("commands:\n");
1934 print_sw (ALL
, sw
, "", stdout
);
1935 printf ("type ``quit'' to leave %s\n", invo_name
);
1938 _exit (0); /* NOTREACHED */
1941 rc2peer (RC_EOF
, 0, NULL
);
1945 if (parse (str
, cmdp
) == NOTOK
)
1948 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1950 switch (winX (DISPLAY
)) {
1955 ambigsw (cmdp
->args
[0], sw
);
1958 _exit (0); /* NOTREACHED */
1961 rc2peer (RC_EOF
, 0, NULL
);
1967 "say what: ``%s'' -- type ? (or help) for help",
1982 switch (setjmp (peerenv
)) {
1984 SIGNAL (SIGALRM
, alrmser
);
1987 status
= peerwait ();
2001 struct record rcs
, *rc
;
2006 switch (peer2rc (rc
)) {
2009 rc2peer (RC_FIN
, 0, NULL
);
2013 advise (NULL
, "%s", rc
->rc_data
);
2017 fmt2peer (RC_FIN
, "pLOOP protocol screw-up");
2024 ttyNaux (struct Cmd
*cmdp
, char *s
)
2026 struct record rcs
, *rc
;
2031 if (cmdp
&& init_io (cmdp
, vmh
) == NOTOK
)
2034 /* XXX: fseek() too tricky for our own good */
2036 fseek (fp
, 0L, SEEK_SET
);
2039 switch (rc2rc (RC_TTY
, s
? strlen (s
) : 0, s
, rc
)) {
2041 vmhtty
= OK
; /* fall */
2046 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
2049 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
2050 done (1); /* NOTREACHED */
2054 SIGNAL (SIGTSTP
, tstat
);
2061 ttyR (struct Cmd
*cmdp
)
2063 struct record rcs
, *rc
;
2068 SIGNAL (SIGTSTP
, SIG_IGN
);
2080 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2082 rc2peer (RC_EOF
, 0, NULL
);
2086 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
2089 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
2090 done (1); /* NOTREACHED */
2093 return 1; /* dead code to satisfy compiler */
2098 winN (struct Cmd
*cmdp
, int n
, int eof
)
2101 char buffer
[BUFSIZ
];
2102 struct record rcs
, *rc
;
2105 if (vmhpid
== NOTOK
)
2110 /* XXX: fseek() too tricky for our own good */
2112 fseek (fp
, 0L, SEEK_SET
);
2116 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2117 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2125 padios (NULL
, "%s", rc
->rc_data
);
2128 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2132 if (pipe (pd
) == NOTOK
) {
2133 err2peer (RC_ERR
, "pipe", "unable to");
2137 switch (vmhpid
= fork()) {
2139 err2peer (RC_ERR
, "fork", "unable to");
2146 SIGNAL (SIGPIPE
, SIG_IGN
);
2147 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2148 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2156 advise (NULL
, "%s", rc
->rc_data
);
2160 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2164 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2167 rc2peer (RC_EOF
, 0, NULL
);
2172 advise (NULL
, "%s", rc
->rc_data
);
2177 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2182 err2peer (RC_ERR
, "pipe", "error reading from");
2184 _exit (i
!= NOTOK
? i
: 1);
2187 if ((vmhfd0
= dup (fileno (stdin
))) == NOTOK
)
2188 padios ("standard input", "unable to dup");
2189 if ((vmhfd1
= dup (fileno (stdout
))) == NOTOK
)
2190 padios ("standard output", "unable to dup");
2191 if ((vmhfd2
= dup (fileno (stderr
))) == NOTOK
)
2192 padios ("diagnostic output", "unable to dup");
2195 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2196 dup2 (i
, fileno (stdin
));
2201 if (dup2 (pd
[1], fileno (stdout
)) == NOTOK
)
2202 padios ("standard output", "unable to dup2");
2206 if (dup2 (pd
[1], fileno (stderr
)) == NOTOK
)
2207 padios ("diagnostic output", "unable to dup2");
2210 if (cmdp
&& init_io (cmdp
, 0) == NOTOK
)
2212 pstat
= SIGNAL (SIGPIPE
, pipeser
);
2224 winR (struct Cmd
*cmdp
)
2234 if (dup2 (vmhfd0
, fileno (stdin
)) == NOTOK
)
2235 padios ("standard input", "unable to dup2");
2240 if (dup2 (vmhfd1
, fileno (stdout
)) == NOTOK
)
2241 padios ("standard output", "unable to dup2");
2246 if (dup2 (vmhfd2
, fileno (stderr
)) == NOTOK
)
2247 padios ("diagnostic output", "unable to dup2");
2251 SIGNAL (SIGPIPE
, pstat
);
2253 if ((status
= pidwait (vmhpid
, OK
)) == 2)
2257 return (status
== 0 ? OK
: NOTOK
);
2265 char buffer
[BUFSIZ
];
2266 struct record rcs
, *rc
;
2271 /* XXX: fseek() too tricky for our own good */
2273 fseek (fp
, 0L, SEEK_SET
);
2275 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2276 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2284 padios (NULL
, "%s", rc
->rc_data
);
2287 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2291 if (pipe (pd
) == NOTOK
) {
2292 err2peer (RC_ERR
, "pipe", "unable to");
2296 switch (pid
= fork ()) {
2298 err2peer (RC_ERR
, "fork", "unable to");
2304 close (fileno (stdin
));
2305 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2306 dup2 (i
, fileno (stdin
));
2309 dup2 (pd
[1], fileno (stdout
));
2310 dup2 (pd
[1], fileno (stderr
));
2318 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2319 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2329 padios (NULL
, "%s", rc
->rc_data
);
2332 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2336 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2341 padios (NULL
, "%s", rc
->rc_data
);
2344 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2348 err2peer (RC_ERR
, "pipe", "error reading from");
2352 return (i
!= NOTOK
? pid
: NOTOK
);
2358 padios (char *what
, char *fmt
, ...)
2364 verr2peer (RC_FIN
, what
, fmt
, ap
);
2367 advertise (what
, NULL
, fmt
, ap
);
2376 padvise (char *what
, char *fmt
, ...)
2382 verr2peer (RC_ERR
, what
, fmt
, ap
);
2384 advertise (what
, NULL
, fmt
, ap
);