]>
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>
41 #define QUOTE '\\' /* sigh */
43 static struct swit switches
[] = {
45 { "idstart number", -7 }, /* interface from bbc */
47 { "idstop number", -6 }, /* .. */
49 { "idquit number", -6 }, /* .. */
51 { "idname BBoard", -6 }, /* .. */
53 { "prompt string", 0 },
61 { "vmhwrite fd", -8 },
65 { "popwrite fd", -8 },
77 static int mbx_style
= MMDF_FORMAT
;
82 char*fmsh
= NULL
; /* folder instead of file */
83 int modified
; /* command modified folder */
84 struct msgs
*mp
; /* used a lot */
86 struct Msg
*Msgs
= NULL
; /* Msgs[0] not used */
87 static FILE *fp
; /* input file */
88 static FILE *yp
= NULL
; /* temporary file */
89 static int mode
; /* mode of file */
90 static int numfds
= 0; /* number of files cached */
91 static int maxfds
= 0; /* number of files cached to be cached */
92 static time_t mtime
= (time_t) 0; /* mtime of file */
97 #define ALARM ((unsigned int) 10)
98 #define ttyN(c) ttyNaux ((c), NULL)
102 static int vmhpid
= OK
;
107 static int vmhtty
= NOTOK
;
114 static int topcur
= 0;
116 static int numwins
= 0;
117 static int windows
[NWIN
+ 1];
119 static jmp_buf peerenv
;
124 static int pfd
= NOTOK
; /* fd parent is reading from */
125 static int ppid
= 0; /* pid of parent */
130 int interactive
; /* running from a /dev/tty */
131 int redirected
; /* re-directing output */
132 FILE *sp
= NULL
; /* original stdout */
134 char *cmd_name
; /* command being run */
135 char myfilter
[BUFSIZ
]; /* path to mhl.forward */
137 static char *myprompt
= "(%s) ";/* prompting string */
142 static int gap
; /* gap in BBoard-ID:s */
143 static char *myname
= NULL
; /* BBoard name */
144 char *BBoard_ID
= "BBoard-ID"; /* BBoard-ID constant */
149 SIGNAL_HANDLER istat
; /* original SIGINT */
150 static SIGNAL_HANDLER pstat
; /* current SIGPIPE */
151 SIGNAL_HANDLER qstat
; /* original SIGQUIT */
154 SIGNAL_HANDLER tstat
; /* original SIGTSTP */
157 int interrupted
; /* SIGINT detected */
158 int broken_pipe
; /* SIGPIPE detected */
159 int told_to_quit
; /* SIGQUIT detected */
162 int should_intr
; /* signal handler should interrupt call */
163 jmp_buf sigenv
; /* the environment pointer */
169 int SOprintf (char *, ...); /* from termsbr.c */
170 int sc_width (void); /* from termsbr.c */
171 void fsetup (char *);
173 FILE *msh_ready (int, int);
176 void display_info (int);
179 void seq_setcur (struct msgs
*, int);
180 void padios (char *, char *, ...);
181 void padvise (char *, char *, ...);
187 static void msh (int);
188 static int read_map (char *, long);
189 static int read_file (long, int);
191 static void m_gMsgs (int);
192 FILE *msh_ready (int, int);
193 static int check_folder (int);
194 static void scanrange (int, int);
195 static void scanstring (char *);
196 static void write_ids (void);
197 static void quit (void);
198 static int getargs (char *, struct swit
*, struct Cmd
*);
199 static int getcmds (struct swit
*, struct Cmd
*, int);
200 static int parse (char *, struct Cmd
*);
201 static int init_io (struct Cmd
*, int);
202 static int initaux_io (struct Cmd
*);
203 static void fin_io (struct Cmd
*, int);
204 static void finaux_io (struct Cmd
*);
205 static void m_init (void);
206 static RETSIGTYPE
intrser (int);
207 static RETSIGTYPE
pipeser (int);
208 static RETSIGTYPE
quitser (int);
209 static RETSIGTYPE
alrmser (int);
210 static int pINI (void);
211 static int pQRY (char *, int);
212 static int pQRY1 (int);
213 static int pQRY2 (void);
214 static int pCMD (char *, struct swit
*, struct Cmd
*);
215 static int pFIN (void);
216 static int peerwait (void);
217 static int ttyNaux (struct Cmd
*, char *);
218 static int ttyR (struct Cmd
*);
219 static int winN (struct Cmd
*, int, int);
220 static int winR (struct Cmd
*);
221 static int winX (int);
225 main (int argc
, char **argv
)
227 int id
= 0, scansw
= 0, vmh1
= 0, vmh2
= 0;
228 char *cp
, *file
= NULL
, *folder
= NULL
;
229 char **argp
, **arguments
, buf
[BUFSIZ
];
232 setlocale(LC_ALL
, "");
234 invo_name
= r1bindex (argv
[0], '/');
236 /* read user profile/context */
239 mts_init (invo_name
);
240 arguments
= getarguments (invo_name
, argc
,argv
, 1);
243 while ((cp
= *argp
++)) {
245 switch (smatch (++cp
, switches
)) {
247 ambigsw (cp
, switches
);
250 adios (NULL
, "-%s unknown", cp
);
253 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
254 print_help (buf
, switches
, 1);
257 print_version(invo_name
);
261 if (!(cp
= *argp
++) || *cp
== '-')
262 adios (NULL
, "missing argument to %s", argp
[-2]);
263 if ((id
= atoi (cp
)) < 1)
264 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
267 if (!(cp
= *argp
++) || *cp
== '-')
268 adios (NULL
, "missing argument to %s", argp
[-2]);
269 if ((pfd
= atoi (cp
)) <= 1)
270 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
273 if (!(cp
= *argp
++) || *cp
== '-')
274 adios (NULL
, "missing argument to %s", argp
[-2]);
275 if ((ppid
= atoi (cp
)) <= 1)
276 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
279 if (!(myname
= *argp
++) || *myname
== '-')
280 adios (NULL
, "missing argument to %s", argp
[-2]);
291 if (!(myprompt
= *argp
++) || *myprompt
== '-')
292 adios (NULL
, "missing argument to %s", argp
[-2]);
296 if (!(cp
= *argp
++) || *cp
== '-')
297 adios (NULL
, "missing argument to %s", argp
[-2]);
298 if ((vmh1
= atoi (cp
)) < 1)
299 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
302 if (!(cp
= *argp
++) || *cp
== '-')
303 adios (NULL
, "missing argument to %s", argp
[-2]);
304 if ((vmh2
= atoi (cp
)) < 1)
305 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
309 if (!(cp
= *argp
++) || *cp
== '-')
310 adios (NULL
, "missing argument to %s", argp
[-2]);
313 if (!(cp
= *argp
++) || *cp
== '-')
314 adios (NULL
, "missing argument to %s", argp
[-2]);
324 if (*cp
== '+' || *cp
== '@') {
326 adios (NULL
, "only one folder at a time!");
328 folder
= pluspath (cp
);
332 adios (NULL
, "only one file at a time!");
337 if (!file
&& !folder
)
340 adios (NULL
, "use a file or a folder, not both");
341 strncpy (myfilter
, etcpath (mhlforward
), sizeof(myfilter
));
344 ioctl (pfd
, FIOCLEX
, NULL
);
350 istat
= SIGNAL2 (SIGINT
, intrser
);
351 qstat
= SIGNAL2 (SIGQUIT
, quitser
);
353 sc_width (); /* MAGIC... */
355 if ((vmh
= vmh1
&& vmh2
)) {
358 SIGNAL (SIGINT
, SIG_IGN
);
359 SIGNAL (SIGQUIT
, SIG_IGN
);
361 tstat
= SIGNAL (SIGTSTP
, SIG_IGN
);
370 display_info (id
> 0 ? scansw
: 0);
372 msh (id
> 0 ? scansw
: 0);
381 static struct swit mshcmds
[] = {
446 register char *cp
, **ap
;
447 char prompt
[BUFSIZ
], *vec
[MAXARGS
];
449 register struct Cmd
*cmdp
;
450 static int once_only
= ADVCMD
;
452 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
461 if ((i
= getcmds (mshcmds
, cmdp
, scansw
)) == EOF
) {
466 check_folder (scansw
);
467 if ((i
= getargs (prompt
, mshcmds
, cmdp
)) == EOF
) {
472 cmd_name
= mshcmds
[i
].sw
;
480 if (once_only
== ADVCMD
)
481 once_only
= i
= SHOWCMD
;
483 i
= mp
->curmsg
!= mp
->hghmsg
? NEXTCMD
: EXITCMD
;
484 cmd_name
= mshcmds
[i
].sw
;
490 case FORWCMD
: /* sigh */
500 if ((cp
= context_find (cmd_name
))) {
502 ap
= brkstring (cp
, " ", "\n");
503 ap
= copyip (ap
, vec
, MAXARGS
);
514 copyip (cmdp
->args
+ 1, ap
, MAXARGS
);
518 if (!vmh
&& init_io (cmdp
, vmh
) == NOTOK
) {
524 redirected
= vmh
|| cmdp
->direction
!= STDIO
;
535 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
536 forkcmd (vec
, cmd_name
);
540 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
545 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
551 || (filehak (vec
) == OK
? ttyN (cmdp
)
552 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
557 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
562 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
567 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
573 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
578 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
585 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
591 || (packhak (vec
) == OK
? ttyN (cmdp
)
592 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
597 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
602 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
607 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
612 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
617 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
622 padios (NULL
, "no dispatch for %s", cmd_name
);
644 fsetup (char *folder
)
650 maildir
= m_maildir (folder
);
651 if (chdir (maildir
) == NOTOK
)
652 padios (maildir
, "unable to change directory to");
654 /* read folder and create message structure */
655 if (!(mp
= folder_read (folder
)))
656 padios (NULL
, "unable to read folder %s", folder
);
658 /* check for empty folder */
660 padios (NULL
, "no messages in %s", folder
);
663 mtime
= stat (mp
->foldpath
, &st
) != NOTOK
? st
.st_mtime
: 0;
665 m_gMsgs (mp
->hghmsg
);
667 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
668 Msgs
[msgnum
].m_bboard_id
= 0;
669 Msgs
[msgnum
].m_top
= NOTOK
;
670 Msgs
[msgnum
].m_start
= Msgs
[msgnum
].m_stop
= 0L;
671 Msgs
[msgnum
].m_scanl
= NULL
;
676 fmsh
= getcpy (folder
);
678 maxfds
= OPEN_MAX
/ 2;
680 if ((maxfds
-= 2) < 1)
690 if ((fp
= fopen (file
, "r")) == NULL
)
691 padios (file
, "unable to read");
693 ioctl (fileno (fp
), FIOCLEX
, NULL
);
695 if (fstat (fileno (fp
), &st
) != NOTOK
) {
696 mode
= (int) (st
.st_mode
& 0777), mtime
= st
.st_mtime
;
697 msgp
= read_map (file
, (long) st
.st_size
);
700 mode
= m_gmprot (), mtime
= 0;
704 if ((msgp
= read_file (msgp
? Msgs
[msgp
].m_stop
: 0L, msgp
+ 1)) < 1)
705 padios (NULL
, "no messages in %s", myname
? myname
: file
);
707 if (!(mp
= (struct msgs
*) calloc ((size_t) 1, sizeof(*mp
))))
708 padios (NULL
, "unable to allocate folder storage");
710 if (!(mp
->msgstats
= calloc ((size_t) msgp
+ 3, sizeof(*(mp
->msgstats
)))))
711 padios (NULL
, "unable to allocate message status storage");
717 mp
->foldpath
= getcpy (myname
? myname
: file
);
718 clear_folder_flags (mp
);
721 if (st
.st_uid
!= getuid () || access (file
, W_OK
) == NOTOK
)
725 mp
->hghoff
= mp
->hghmsg
+ 1;
727 for (i
= mp
->lowmsg
; i
<= mp
->hghmsg
; i
++) {
728 clear_msg_flags (mp
, i
);
733 mp
->msgattrs
[0] = getcpy ("unseen");
734 mp
->msgattrs
[1] = NULL
;
736 m_unknown (fp
); /* the MAGIC invocation */
745 read_map (char *file
, long size
)
747 register int i
, msgp
;
748 register struct drop
*dp
, *mp
;
751 if ((i
= map_read (file
, size
, &rp
, 1)) == 0)
757 for (dp
= rp
+ 1; i
-- > 0; msgp
++, dp
++) {
758 mp
= &Msgs
[msgp
].m_drop
;
760 mp
->d_size
= dp
->d_size
;
761 mp
->d_start
= dp
->d_start
;
762 mp
->d_stop
= dp
->d_stop
;
763 Msgs
[msgp
].m_scanl
= NULL
;
772 read_file (long pos
, int msgp
)
775 register struct drop
*dp
, *mp
;
778 if ((i
= mbx_read (fp
, pos
, &rp
, 1)) <= 0)
781 m_gMsgs ((msgp
- 1) + i
);
783 for (dp
= rp
; i
-- > 0; msgp
++, dp
++) {
784 mp
= &Msgs
[msgp
].m_drop
;
786 mp
->d_size
= dp
->d_size
;
787 mp
->d_start
= dp
->d_start
;
788 mp
->d_stop
= dp
->d_stop
;
789 Msgs
[msgp
].m_scanl
= NULL
;
803 nMsgs
= n
+ MAXFOLDER
/ 2;
804 Msgs
= (struct Msg
*) calloc ((size_t) (nMsgs
+ 2), sizeof *Msgs
);
806 padios (NULL
, "unable to allocate Msgs structure");
813 nmsgs
= nMsgs
+ n
+ MAXFOLDER
/ 2;
814 Msgs
= (struct Msg
*) mh_xrealloc ((char *) Msgs
, (size_t) (nmsgs
+ 2) * sizeof *Msgs
);
815 memset((char *) (Msgs
+ nMsgs
+ 2), 0, (size_t) ((nmsgs
- nMsgs
) * sizeof *Msgs
));
822 msh_ready (int msgnum
, int full
)
834 if ((fd
= Msgs
[msgnum
].m_top
) == NOTOK
) {
835 if (numfds
>= maxfds
)
836 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++)
837 if (Msgs
[msgp
].m_top
!= NOTOK
) {
838 close (Msgs
[msgp
].m_top
);
839 Msgs
[msgp
].m_top
= NOTOK
;
844 if ((fd
= open (cp
= m_name (msgnum
), O_RDONLY
)) == NOTOK
)
845 padios (cp
, "unable to open message");
846 Msgs
[msgnum
].m_top
= fd
;
850 if ((fd
= dup (fd
)) == NOTOK
)
851 padios ("cached message", "unable to dup");
852 if ((yp
= fdopen (fd
, "r")) == NULL
)
853 padios (NULL
, "unable to fdopen cached message");
854 fseek (yp
, 0L, SEEK_SET
);
858 m_eomsbr ((int (*)()) 0); /* XXX */
859 fseek (fp
, Msgs
[msgnum
].m_start
, SEEK_SET
);
865 check_folder (int scansw
)
867 int seqnum
, i
, low
, hgh
, msgp
;
871 if (stat (mp
->foldpath
, &st
) == NOTOK
)
872 padios (mp
->foldpath
, "unable to stat");
873 if (mtime
== st
.st_mtime
)
877 low
= mp
->hghmsg
+ 1;
878 folder_free (mp
); /* free folder/message structure */
880 if (!(mp
= folder_read (fmsh
)))
881 padios (NULL
, "unable to re-read folder %s", fmsh
);
885 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++) {
886 if (Msgs
[msgp
].m_top
!= NOTOK
) {
887 close (Msgs
[msgp
].m_top
);
888 Msgs
[msgp
].m_top
= NOTOK
;
891 if (Msgs
[msgp
].m_scanl
) {
892 free (Msgs
[msgp
].m_scanl
);
893 Msgs
[msgp
].m_scanl
= NULL
;
899 if (modified
|| low
> hgh
)
903 if (fstat (fileno (fp
), &st
) == NOTOK
)
904 padios (mp
->foldpath
, "unable to fstat");
905 if (mtime
== st
.st_mtime
)
907 mode
= (int) (st
.st_mode
& 0777);
910 if ((msgp
= read_file (Msgs
[mp
->hghmsg
].m_stop
, mp
->hghmsg
+ 1)) < 1)
911 padios (NULL
, "no messages in %s", mp
->foldpath
); /* XXX */
912 if (msgp
>= MAXFOLDER
)
913 padios (NULL
, "more than %d messages in %s", MAXFOLDER
,
915 if (msgp
<= mp
->hghmsg
)
918 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgp
)))
919 padios (NULL
, "unable to allocate folder storage");
921 low
= mp
->hghmsg
+ 1, hgh
= msgp
;
922 seqnum
= scansw
? seq_getnum (mp
, "unseen") : -1;
923 for (i
= mp
->hghmsg
+ 1; i
<= msgp
; i
++) {
926 add_sequence(mp
, seqnum
, i
);
936 advise (NULL
, "new messages have arrived!\007");
938 scanrange (low
, hgh
);
945 scanrange (int low
, int hgh
)
949 snprintf (buffer
, sizeof(buffer
), "%d-%d", low
, hgh
);
955 scanstring (char *arg
)
957 char *cp
, **ap
, *vec
[MAXARGS
];
960 * This should be replace with a call to getarguments()
962 if ((cp
= context_find (cmd_name
= "scan"))) {
964 ap
= brkstring (cp
, " ", "\n");
965 ap
= copyip (ap
, vec
, MAXARGS
);
981 register int cur
, seqnum
, i
=0, msgnum
;
984 seq_setcur (mp
, mp
->lowmsg
);
985 if (id
<= 0 || (seqnum
= seq_getnum (mp
, "unseen")) == -1)
988 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
989 add_sequence(mp
, seqnum
, msgnum
);
994 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
995 if (does_exist(mp
, msgnum
)) /* FIX */
996 if ((i
= readid (msgnum
)) > 0 && i
< id
) {
998 clear_sequence(mp
, seqnum
, msgnum
);
1001 for (i
= mp
->lowmsg
; i
< msgnum
; i
++)
1002 clear_sequence(mp
, seqnum
, i
);
1004 if (cur
> mp
->hghmsg
)
1007 seq_setcur (mp
, cur
);
1010 if ((gap
= 1 < id
&& id
< (i
= readid (mp
->lowmsg
)) ? id
: 0) && !vmh
)
1011 advise (NULL
, "gap in ID:s, last seen %d, lowest present %d\n",
1020 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
1023 if (Msgs
[msgnum
].m_bboard_id
)
1024 return Msgs
[msgnum
].m_bboard_id
;
1026 zp
= msh_ready (msgnum
, 0);
1028 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
)) {
1032 if (!mh_strcasecmp (name
, BBoard_ID
)) {
1034 while (state
== FLDPLUS
) {
1035 state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
);
1041 return (Msgs
[msgnum
].m_bboard_id
= i
);
1045 while (state
== FLDPLUS
)
1046 state
= m_getfld (state
, name
, buf
, sizeof(buf
), zp
);
1047 if (state
!= FLDEOF
)
1057 display_info (int scansw
)
1061 interactive
= isatty (fileno (stdout
));
1063 if ((sd
= dup (fileno (stdout
))) == NOTOK
)
1064 padios ("standard output", "unable to dup");
1065 #ifndef BSD42 /* XXX */
1067 ioctl (sd
, FIOCLEX
, NULL
);
1068 #endif /* FIOCLEX */
1069 #endif /* not BSD42 */
1070 if ((sp
= fdopen (sd
, "w")) == NULL
)
1071 padios ("standard output", "unable to fdopen");
1074 m_putenv ("mhfolder", mp
->foldpath
);
1079 printf ("Reading ");
1080 if (SOprintf ("%s", myname
))
1081 printf ("%s", myname
);
1082 printf (", currently at message %d of %d\n",
1083 mp
->curmsg
, mp
->hghmsg
);
1086 printf ("Reading ");
1088 printf ("+%s", fmsh
);
1090 printf ("%s", mp
->foldpath
);
1091 printf (", currently at message %d of %d\n",
1092 mp
->curmsg
, mp
->hghmsg
);
1095 if (((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1097 && in_sequence(mp
, seqnum
, mp
->hghmsg
))
1098 scanstring ("unseen");
1105 int i
= 0, seqnum
, msgnum
;
1111 if ((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1112 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1113 if (!in_sequence(mp
, seqnum
, msgnum
)) {
1114 if (Msgs
[msgnum
].m_bboard_id
== 0)
1116 if ((i
= Msgs
[msgnum
].m_bboard_id
) > 0)
1120 snprintf (buffer
, sizeof(buffer
), "%d %d\n", i
, Msgs
[mp
->hghmsg
].m_bboard_id
);
1121 write (pfd
, buffer
, sizeof(buffer
));
1131 char *cp
, tmpfil
[BUFSIZ
];
1132 char map1
[BUFSIZ
], map2
[BUFSIZ
];
1136 if (!(mp
->msgflags
& MODIFIED
) || is_readonly(mp
) || fmsh
) {
1138 rc2peer (RC_FIN
, 0, NULL
);
1143 ttyNaux (NULLCMD
, "FAST");
1145 if ((dp
= lkfopen (mp
->foldpath
, "r")) == NULL
) {
1146 advise (mp
->foldpath
, "unable to lock");
1153 if (fstat (fileno (dp
), &st
) == NOTOK
) {
1154 advise (mp
->foldpath
, "unable to stat");
1157 if (mtime
!= st
.st_mtime
) {
1158 advise (NULL
, "new messages have arrived, no update");
1161 mode
= (int) (st
.st_mode
& 0777);
1163 if (mp
->nummsg
== 0) {
1164 cp
= concat ("Zero file \"", mp
->foldpath
, "\"? ", NULL
);
1165 if (getanswer (cp
)) {
1166 if ((i
= creat (mp
->foldpath
, mode
)) != NOTOK
)
1169 advise (mp
->foldpath
, "error zero'ing");
1170 unlink (map_name (mp
->foldpath
));/* XXX */
1175 cp
= concat ("Update file \"", mp
->foldpath
, "\"? ", NULL
);
1176 if (!getanswer (cp
))
1178 strncpy (tmpfil
, m_backup (mp
->foldpath
), sizeof(tmpfil
));
1179 if ((md
= mbx_open (tmpfil
, mbx_style
, st
.st_uid
, st
.st_gid
, mode
)) == NOTOK
) {
1180 advise (tmpfil
, "unable to open");
1184 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1185 if (does_exist(mp
, msgnum
) && pack (tmpfil
, md
, msgnum
) == NOTOK
) {
1186 mbx_close (tmpfil
, md
);
1188 unlink (map_name (tmpfil
));
1191 mbx_close (tmpfil
, md
);
1193 if (rename (tmpfil
, mp
->foldpath
) == NOTOK
)
1194 admonish (mp
->foldpath
, "unable to rename %s to", tmpfil
);
1196 strncpy (map1
, map_name (tmpfil
), sizeof(map1
));
1197 strncpy (map2
, map_name (mp
->foldpath
), sizeof(map2
));
1199 if (rename (map1
, map2
) == NOTOK
) {
1200 admonish (map2
, "unable to rename %s to", map1
);
1209 lkfclose (dp
, mp
->foldpath
);
1218 getargs (char *prompt
, struct swit
*sw
, struct Cmd
*cmdp
)
1222 static char buffer
[BUFSIZ
];
1228 switch (setjmp (sigenv
)) {
1235 if (interrupted
&& !told_to_quit
) {
1241 kill (ppid
, SIGEMT
);
1243 kill (ppid
, SIGTERM
);
1249 printf ("%s", prompt
);
1252 for (cp
= buffer
; (i
= getchar ()) != '\n';) {
1254 if (interrupted
&& !told_to_quit
) {
1259 if (told_to_quit
|| i
== EOF
) {
1262 kill (ppid
, SIGEMT
);
1264 kill (ppid
, SIGTERM
);
1270 longjmp (sigenv
, DONE
);
1272 if (cp
< &buffer
[sizeof buffer
- 2])
1279 if (buffer
[0] == '?') {
1280 printf ("commands:\n");
1281 print_sw (ALL
, sw
, "", stdout
);
1282 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1287 if (parse (buffer
, cmdp
) == NOTOK
)
1290 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1292 ambigsw (cmdp
->args
[0], sw
);
1295 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1309 getcmds (struct swit
*sw
, struct Cmd
*cmdp
, int scansw
)
1312 struct record rcs
, *rc
;
1318 switch (peer2rc (rc
)) {
1320 pQRY (rc
->rc_data
, scansw
);
1324 if ((i
= pCMD (rc
->rc_data
, sw
, cmdp
)) != NOTOK
)
1331 kill (ppid
, SIGEMT
);
1333 kill (ppid
, SIGTERM
);
1338 padios (NULL
, "%s", rc
->rc_data
);
1341 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
1348 parse (char *buffer
, struct Cmd
*cmdp
)
1351 unsigned char c
, *cp
;
1355 pp
= cmdp
->args
[argp
++] = cmdp
->line
;
1356 cmdp
->redirect
= NULL
;
1357 cmdp
->direction
= STDIO
;
1358 cmdp
->stream
= NULL
;
1360 for (cp
= buffer
; (c
= *cp
); cp
++) {
1366 fmt2peer (RC_EOF
, "null command");
1370 while ((c
= *cp
++)) {
1377 cmdp
->args
[argp
++] = pp
;
1384 switch (c
= *cp
++) {
1386 padvise (NULL
, "unmatched \"");
1391 if ((c
= *cp
++) == 0)
1402 if ((c
= *cp
++) == 0) {
1404 padvise (NULL
, "the newline character can not be quoted");
1414 if (pp
== cmdp
->line
) {
1415 padvise (NULL
, "invalid null command");
1418 if (*cmdp
->args
[argp
- 1] == 0)
1420 cmdp
->direction
= c
== '>' ? CRTIO
: PIPIO
;
1421 if (cmdp
->direction
== CRTIO
&& (c
= *cp
) == '>') {
1422 cmdp
->direction
= APPIO
;
1425 cmdp
->redirect
= pp
+ 1;/* sigh */
1426 for (; (c
= *cp
); cp
++)
1430 padvise (NULL
, cmdp
->direction
!= PIPIO
1431 ? "missing name for redirect"
1432 : "invalid null command");
1435 strcpy (cmdp
->redirect
, cp
);
1436 if (cmdp
->direction
!= PIPIO
) {
1438 if (isspace (*cp
)) {
1439 padvise (NULL
, "bad name for redirect");
1442 if (expand (cmdp
->redirect
) == NOTOK
)
1451 cmdp
->args
[argp
] = NULL
;
1458 expand (char *redirect
)
1464 if (*redirect
!= '~')
1467 if ((cp
= strchr(pp
= redirect
+ 1, '/')))
1472 if ((pw
= getpwnam (pp
)))
1475 padvise (NULL
, "unknown user: %s", pp
);
1479 snprintf (path
, sizeof(path
), "%s/%s", pp
, cp
? cp
: "");
1480 strcpy (redirect
, path
);
1486 init_io (struct Cmd
*cmdp
, int vio
)
1493 result
= initaux_io (cmdp
);
1501 initaux_io (struct Cmd
*cmdp
)
1505 switch (cmdp
->direction
) {
1511 mode
= cmdp
->direction
== CRTIO
? "write" : "append";
1512 if ((cmdp
->stream
= fopen (cmdp
->redirect
, mode
)) == NULL
) {
1513 padvise (cmdp
->redirect
, "unable to %s ", mode
);
1514 cmdp
->direction
= STDIO
;
1520 if ((cmdp
->stream
= popen (cmdp
->redirect
, "w")) == NULL
) {
1521 padvise (cmdp
->redirect
, "unable to pipe");
1522 cmdp
->direction
= STDIO
;
1525 SIGNAL (SIGPIPE
, pipeser
);
1530 padios (NULL
, "unknown redirection for command");
1534 if (dup2 (fileno (cmdp
->stream
), fileno (stdout
)) == NOTOK
)
1535 padios ("standard output", "unable to dup2");
1543 fin_io (struct Cmd
*cmdp
, int vio
)
1555 finaux_io (struct Cmd
*cmdp
)
1557 switch (cmdp
->direction
) {
1564 close (fileno (stdout
));
1565 if (ferror (stdout
))
1566 padvise (NULL
, "problems writing %s", cmdp
->redirect
);
1567 fclose (cmdp
->stream
);
1572 close (fileno (stdout
));
1573 pclose (cmdp
->stream
);
1574 SIGNAL (SIGPIPE
, SIG_DFL
);
1578 padios (NULL
, "unknown redirection for command");
1581 if (dup2 (fileno (sp
), fileno (stdout
)) == NOTOK
)
1582 padios ("standard output", "unable to dup2");
1585 cmdp
->direction
= STDIO
;
1594 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1595 unset_selected (mp
, msgnum
);
1596 mp
->lowsel
= mp
->hghsel
= mp
->numsel
= 0;
1604 folder_free (mp
); /* free folder/message structure */
1610 seq_setcur (struct msgs
*mp
, int msgnum
)
1612 if (mp
->curmsg
== msgnum
)
1615 if (mp
->curmsg
&& Msgs
[mp
->curmsg
].m_scanl
) {
1616 free (Msgs
[mp
->curmsg
].m_scanl
);
1617 Msgs
[mp
->curmsg
].m_scanl
= NULL
;
1619 if (Msgs
[msgnum
].m_scanl
) {
1620 free (Msgs
[msgnum
].m_scanl
);
1621 Msgs
[msgnum
].m_scanl
= NULL
;
1624 mp
->curmsg
= msgnum
;
1632 #ifndef RELIABLE_SIGNALS
1633 SIGNAL (SIGINT
, intrser
);
1641 longjmp (sigenv
, NOTOK
);
1649 #ifndef RELIABLE_SIGNALS
1650 SIGNAL (SIGPIPE
, pipeser
);
1653 if (broken_pipe
++ == 0)
1654 fprintf (stderr
, "broken pipe\n");
1660 longjmp (sigenv
, NOTOK
);
1668 #ifndef RELIABLE_SIGNALS
1669 SIGNAL (SIGQUIT
, quitser
);
1677 longjmp (sigenv
, NOTOK
);
1685 longjmp (peerenv
, DONE
);
1694 struct record rcs
, *rc
;
1699 switch (peer2rc (rc
)) {
1702 while (isspace (*bp
))
1704 if (sscanf (bp
, "%d", &vrsn
) != 1) {
1706 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
1709 if (vrsn
!= RC_VRSN
) {
1710 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
1714 while (*bp
&& !isspace (*bp
))
1716 while (isspace (*bp
))
1718 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
1723 for (i
= 1; i
<= numwins
; i
++) {
1724 while (*bp
&& !isspace (*bp
))
1726 while (isspace (*bp
))
1728 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
1731 rc2peer (RC_ACK
, 0, NULL
);
1735 padios (NULL
, "%s", rc
->rc_data
);
1738 fmt2peer (RC_ERR
, "pINI protocol screw-up");
1739 done (1); /* NOTREACHED */
1742 return 1; /* dead code to satisfy the compiler */
1747 pQRY (char *str
, int scansw
)
1749 if (pQRY1 (scansw
) == NOTOK
|| pQRY2 () == NOTOK
)
1752 rc2peer (RC_EOF
, 0, NULL
);
1761 static int lastlow
= 0,
1766 oldhgh
= mp
->hghmsg
;
1767 if (check_folder (scansw
) && oldhgh
< mp
->hghmsg
) {
1768 switch (winX (STATUS
)) {
1773 printf ("new messages have arrived!");
1776 _exit (0); /* NOTREACHED */
1779 lastlow
= lastcur
= lasthgh
= lastnum
= 0;
1783 switch (winX (DISPLAY
)) {
1788 scanrange (oldhgh
+ 1, mp
->hghmsg
);
1791 _exit (0); /* NOTREACHED */
1800 switch (winX (STATUS
)) {
1805 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1806 myname
? myname
: fmsh
? fmsh
: mp
->foldpath
, gap
- 1,
1807 readid (mp
->lowmsg
));
1810 _exit (0); /* NOTREACHED */
1817 if (mp
->lowmsg
!= lastlow
1818 || mp
->curmsg
!= lastcur
1819 || mp
->hghmsg
!= lasthgh
1820 || mp
->nummsg
!= lastnum
)
1821 switch (winX (STATUS
)) {
1829 _exit (0); /* NOTREACHED */
1832 lastlow
= mp
->lowmsg
;
1833 lastcur
= mp
->curmsg
;
1834 lasthgh
= mp
->hghmsg
;
1835 lastnum
= mp
->nummsg
;
1846 int i
, j
, k
, msgnum
, n
;
1852 if (mp
->nummsg
== 0 && mp
->nummsg
!= num
)
1853 switch (winX (SCAN
)) {
1861 _exit (0); /* NOTREACHED */
1870 j
= (k
= windows
[SCAN
]) / 2;
1871 for (msgnum
= mp
->curmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1872 if (does_exist (mp
, msgnum
))
1876 k
= i
>= k
? 1 : k
- i
;
1883 for (msgnum
= mp
->curmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1884 if (does_exist (mp
, msgnum
)) {
1891 for (msgnum
= mp
->curmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1892 if (does_exist (mp
, msgnum
)) {
1896 if (n
++ >= windows
[SCAN
])
1902 && does_exist (mp
, lo
)
1903 && does_exist (mp
, hi
)
1905 || (lo
== mp
->curmsg
&& lo
== mp
->lowmsg
))
1907 || (hi
== mp
->curmsg
&& hi
== mp
->hghmsg
))
1908 && hi
- lo
== j
- i
)
1911 if (mp
->curmsg
!= cur
|| modified
)
1912 switch (winN (NULLCMD
, SCAN
, 0)) {
1920 scanrange (lo
= i
, hi
= j
);
1931 pCMD (char *str
, struct swit
*sw
, struct Cmd
*cmdp
)
1936 switch (winX (DISPLAY
)) {
1941 printf ("commands:\n");
1942 print_sw (ALL
, sw
, "", stdout
);
1943 printf ("type ``quit'' to leave %s\n", invo_name
);
1946 _exit (0); /* NOTREACHED */
1949 rc2peer (RC_EOF
, 0, NULL
);
1953 if (parse (str
, cmdp
) == NOTOK
)
1956 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1958 switch (winX (DISPLAY
)) {
1963 ambigsw (cmdp
->args
[0], sw
);
1966 _exit (0); /* NOTREACHED */
1969 rc2peer (RC_EOF
, 0, NULL
);
1975 "say what: ``%s'' -- type ? (or help) for help",
1990 switch (setjmp (peerenv
)) {
1992 SIGNAL (SIGALRM
, alrmser
);
1995 status
= peerwait ();
2009 struct record rcs
, *rc
;
2014 switch (peer2rc (rc
)) {
2017 rc2peer (RC_FIN
, 0, NULL
);
2021 advise (NULL
, "%s", rc
->rc_data
);
2025 fmt2peer (RC_FIN
, "pLOOP protocol screw-up");
2032 ttyNaux (struct Cmd
*cmdp
, char *s
)
2034 struct record rcs
, *rc
;
2039 if (cmdp
&& init_io (cmdp
, vmh
) == NOTOK
)
2042 /* XXX: fseek() too tricky for our own good */
2044 fseek (fp
, 0L, SEEK_SET
);
2047 switch (rc2rc (RC_TTY
, s
? strlen (s
) : 0, s
, rc
)) {
2049 vmhtty
= OK
; /* fall */
2054 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
2057 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
2058 done (1); /* NOTREACHED */
2062 SIGNAL (SIGTSTP
, tstat
);
2069 ttyR (struct Cmd
*cmdp
)
2071 struct record rcs
, *rc
;
2076 SIGNAL (SIGTSTP
, SIG_IGN
);
2088 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2090 rc2peer (RC_EOF
, 0, NULL
);
2094 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
2097 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
2098 done (1); /* NOTREACHED */
2101 return 1; /* dead code to satisfy compiler */
2106 winN (struct Cmd
*cmdp
, int n
, int eof
)
2109 char buffer
[BUFSIZ
];
2110 struct record rcs
, *rc
;
2113 if (vmhpid
== NOTOK
)
2118 /* XXX: fseek() too tricky for our own good */
2120 fseek (fp
, 0L, SEEK_SET
);
2124 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2125 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2133 padios (NULL
, "%s", rc
->rc_data
);
2136 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2140 if (pipe (pd
) == NOTOK
) {
2141 err2peer (RC_ERR
, "pipe", "unable to");
2145 switch (vmhpid
= fork()) {
2147 err2peer (RC_ERR
, "fork", "unable to");
2154 SIGNAL (SIGPIPE
, SIG_IGN
);
2155 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2156 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2164 advise (NULL
, "%s", rc
->rc_data
);
2168 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2172 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2175 rc2peer (RC_EOF
, 0, NULL
);
2180 advise (NULL
, "%s", rc
->rc_data
);
2185 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2190 err2peer (RC_ERR
, "pipe", "error reading from");
2192 _exit (i
!= NOTOK
? i
: 1);
2195 if ((vmhfd0
= dup (fileno (stdin
))) == NOTOK
)
2196 padios ("standard input", "unable to dup");
2197 if ((vmhfd1
= dup (fileno (stdout
))) == NOTOK
)
2198 padios ("standard output", "unable to dup");
2199 if ((vmhfd2
= dup (fileno (stderr
))) == NOTOK
)
2200 padios ("diagnostic output", "unable to dup");
2203 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2204 dup2 (i
, fileno (stdin
));
2209 if (dup2 (pd
[1], fileno (stdout
)) == NOTOK
)
2210 padios ("standard output", "unable to dup2");
2214 if (dup2 (pd
[1], fileno (stderr
)) == NOTOK
)
2215 padios ("diagnostic output", "unable to dup2");
2218 if (cmdp
&& init_io (cmdp
, 0) == NOTOK
)
2220 pstat
= SIGNAL (SIGPIPE
, pipeser
);
2232 winR (struct Cmd
*cmdp
)
2242 if (dup2 (vmhfd0
, fileno (stdin
)) == NOTOK
)
2243 padios ("standard input", "unable to dup2");
2248 if (dup2 (vmhfd1
, fileno (stdout
)) == NOTOK
)
2249 padios ("standard output", "unable to dup2");
2254 if (dup2 (vmhfd2
, fileno (stderr
)) == NOTOK
)
2255 padios ("diagnostic output", "unable to dup2");
2259 SIGNAL (SIGPIPE
, pstat
);
2261 if ((status
= pidwait (vmhpid
, OK
)) == 2)
2265 return (status
== 0 ? OK
: NOTOK
);
2273 char buffer
[BUFSIZ
];
2274 struct record rcs
, *rc
;
2279 /* XXX: fseek() too tricky for our own good */
2281 fseek (fp
, 0L, SEEK_SET
);
2283 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2284 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2292 padios (NULL
, "%s", rc
->rc_data
);
2295 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2299 if (pipe (pd
) == NOTOK
) {
2300 err2peer (RC_ERR
, "pipe", "unable to");
2304 switch (pid
= fork ()) {
2306 err2peer (RC_ERR
, "fork", "unable to");
2312 close (fileno (stdin
));
2313 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2314 dup2 (i
, fileno (stdin
));
2317 dup2 (pd
[1], fileno (stdout
));
2318 dup2 (pd
[1], fileno (stderr
));
2326 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2327 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2337 padios (NULL
, "%s", rc
->rc_data
);
2340 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2344 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2349 padios (NULL
, "%s", rc
->rc_data
);
2352 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2356 err2peer (RC_ERR
, "pipe", "error reading from");
2360 return (i
!= NOTOK
? pid
: NOTOK
);
2366 padios (char *what
, char *fmt
, ...)
2372 verr2peer (RC_FIN
, what
, fmt
, ap
);
2375 advertise (what
, NULL
, fmt
, ap
);
2384 padvise (char *what
, char *fmt
, ...)
2390 verr2peer (RC_ERR
, what
, fmt
, ap
);
2392 advertise (what
, NULL
, fmt
, ap
);