]>
diplodocus.org Git - nmh/blob - uip/prompter.c
1 /* prompter.c -- simple prompting editor front-end
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include <h/signals.h>
25 #define PROMPTER_SWITCHES \
26 X("erase chr", 0, ERASESW) \
27 X("kill chr", 0, KILLSW) \
28 X("prepend", 0, PREPSW) \
29 X("noprepend", 0, NPREPSW) \
30 X("rapid", 0, RAPDSW) \
31 X("norapid", 0, NRAPDSW) \
32 X("body", -4, BODYSW) \
33 X("nobody", -6, NBODYSW) \
34 X("doteof", 0, DOTSW) \
35 X("nodoteof", 0, NDOTSW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(PROMPTER
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
48 static struct termios tio
;
49 #define ERASE tio.c_cc[VERASE]
50 #define KILL tio.c_cc[VKILL]
51 #define INTR tio.c_cc[VINTR]
53 static int wtuser
= 0;
54 static int sigint
= 0;
55 static jmp_buf sigenv
;
60 int getln (char *, int);
61 static int chrcnv (char *);
62 static void chrdsp (char *, char);
63 static void intrser (int);
67 main (int argc
, char **argv
)
69 int body
= 1, prepend
= 1, rapid
= 0;
70 int doteof
= 0, fdi
, fdo
, i
, state
;
71 char *cp
, *drft
= NULL
, *erasep
= NULL
;
72 char *killp
= NULL
, name
[NAMESZ
], field
[NMH_BUFSIZ
];
74 char **arguments
, **argp
;
77 m_getfld_state_t gstate
= 0;
79 if (nmh_init(argv
[0], 2)) { return 1; }
81 arguments
= getarguments (invo_name
, argc
, argv
, 1);
84 while ((cp
= *argp
++))
86 switch (smatch (++cp
, switches
)) {
88 ambigsw (cp
, switches
);
91 adios (NULL
, "-%s unknown", cp
);
94 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
96 print_help (buffer
, switches
, 1);
99 print_version(invo_name
);
103 if (!(erasep
= *argp
++) || *erasep
== '-')
104 adios (NULL
, "missing argument to %s", argp
[-2]);
107 if (!(killp
= *argp
++) || *killp
== '-')
108 adios (NULL
, "missing argument to %s", argp
[-2]);
145 adios (NULL
, "usage: %s [switches] file", invo_name
);
146 if ((in
= fopen (drft
, "r")) == NULL
)
147 adios (drft
, "unable to open");
149 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
150 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
154 * Are we changing the kill or erase character?
156 if (killp
|| erasep
) {
157 cc_t save_erase
, save_kill
;
159 /* get the current terminal attributes */
162 /* save original kill, erase character for later */
166 /* set new kill, erase character in terminal structure */
167 KILL
= killp
? chrcnv (killp
) : save_kill
;
168 ERASE
= erasep
? chrcnv (erasep
) : save_erase
;
170 /* set the new terminal attributes */
171 tcsetattr(0, TCSADRAIN
, &tio
);
173 /* print out new kill erase characters */
174 chrdsp ("erase", ERASE
);
175 chrdsp (", kill", KILL
);
176 chrdsp (", intr", INTR
);
181 * We set the kill and erase character back to original
182 * setup in terminal structure so we can easily
183 * restore it upon exit.
190 SIGNAL2 (SIGINT
, intrser
);
193 * Loop through the lines of the draft skeleton.
196 int fieldsz
= sizeof field
;
197 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)) {
201 * Check if the value of field contains anything
202 * other than space or tab.
204 for (cp
= field
; *cp
; cp
++)
205 if (*cp
!= ' ' && *cp
!= '\t')
208 /* If so, just add header line to draft */
209 if (*cp
++ != '\n' || *cp
!= 0) {
210 printf ("%s:%s", name
, field
);
211 fprintf (out
, "%s:%s", name
, field
);
212 while (state
== FLDPLUS
) {
213 fieldsz
= sizeof field
;
214 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
);
215 fputs(field
, stdout
);
219 /* Else, get value of header field */
220 printf ("%s: ", name
);
222 i
= getln (field
, sizeof(field
));
225 if (killp
|| erasep
) {
226 tcsetattr(0, TCSADRAIN
, &tio
);
228 (void) m_unlink (tmpfil
);
231 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
232 fprintf (out
, "%s:", name
);
234 if (field
[0] != ' ' && field
[0] != '\t')
238 && (i
= getln (field
, sizeof(field
))) >= 0);
250 fprintf (out
, "--------\n");
251 if (field
[0] == 0 || !prepend
)
254 if (prepend
&& body
) {
255 printf ("\n--------Enter initial text\n\n");
258 getln (buffer
, sizeof(buffer
));
259 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
269 if (!rapid
&& !sigint
)
270 fputs(field
, stdout
);
271 } while (state
== BODY
&&
272 (fieldsz
= sizeof field
,
273 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)));
274 if (prepend
|| !body
)
277 printf ("\n--------Enter additional text\n\n");
282 getln (field
, sizeof(field
));
283 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
292 adios (NULL
, "skeleton is poorly formatted");
296 m_getfld_state_destroy (&gstate
);
304 SIGNAL (SIGINT
, SIG_IGN
);
306 if (killp
|| erasep
) {
307 tcsetattr(0, TCSADRAIN
, &tio
);
310 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
311 adios (tmpfil
, "unable to re-open");
312 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
313 adios (drft
, "unable to write");
314 cpydata (fdi
, fdo
, tmpfil
, drft
);
317 (void) m_unlink (tmpfil
);
319 context_save (); /* save the context file */
326 getln (char *buffer
, int n
)
330 static int quoting
= 0;
334 switch (setjmp (sigenv
)) {
352 switch (c
= getchar ()) {
356 longjmp (sigenv
, DONE
);
390 longjmp (sigenv
, NOTOK
);
398 return (*cp
!= QUOTE
? *cp
: m_atoi (++cp
));
403 chrdsp (char *s
, char c
)
406 if (c
< ' ' || c
== 0177)
407 printf ("^%c", c
^ 0100);