]>
diplodocus.org Git - nmh/blob - uip/prompter.c
c13a3b8f4b6785d334489007d730a8db687ffcc1
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.
11 #include "h/signals.h"
14 #include "sbr/m_mktemp.h"
21 #define PROMPTER_SWITCHES \
22 X("erase chr", 0, ERASESW) \
23 X("kill chr", 0, KILLSW) \
24 X("prepend", 0, PREPSW) \
25 X("noprepend", 0, NPREPSW) \
26 X("rapid", 0, RAPDSW) \
27 X("norapid", 0, NRAPDSW) \
28 X("body", -4, BODYSW) \
29 X("nobody", -6, NBODYSW) \
30 X("doteof", 0, DOTSW) \
31 X("nodoteof", 0, NDOTSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(PROMPTER
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
43 static struct termios tio
;
47 static jmp_buf sigenv
;
52 static int getln (char *, int);
53 static int chrcnv (char *);
54 static void chrdsp (char *, char);
55 static void intrser (int);
59 main (int argc
, char **argv
)
65 int fdi
, fdo
, i
, state
;
66 char *cp
, *drft
= NULL
, *erasep
= NULL
;
67 char *killp
= NULL
, name
[NAMESZ
], field
[NMH_BUFSIZ
];
69 char **arguments
, **argp
;
72 m_getfld_state_t gstate
;
74 if (nmh_init(argv
[0], true, false)) { return 1; }
76 arguments
= getarguments (invo_name
, argc
, argv
, 1);
79 while ((cp
= *argp
++))
81 switch (smatch (++cp
, switches
)) {
83 ambigsw (cp
, switches
);
86 die("-%s unknown", cp
);
89 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
91 print_help (buffer
, switches
, 1);
94 print_version(invo_name
);
98 if (!(erasep
= *argp
++) || *erasep
== '-')
99 die("missing argument to %s", argp
[-2]);
102 if (!(killp
= *argp
++) || *killp
== '-')
103 die("missing argument to %s", argp
[-2]);
140 die("usage: %s [switches] file", invo_name
);
141 if ((in
= fopen (drft
, "r")) == NULL
)
142 adios (drft
, "unable to open");
144 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
145 die("unable to create temporary file in %s", get_temp_dir());
149 * Are we changing the kill or erase character?
151 if (killp
|| erasep
) {
152 cc_t save_erase
, save_kill
;
154 /* get the current terminal attributes */
157 /* save original kill, erase character for later */
158 save_kill
= tio
.c_cc
[VKILL
];
159 save_erase
= tio
.c_cc
[VERASE
];
161 /* set new kill, erase character in terminal structure */
162 tio
.c_cc
[VKILL
] = killp
? chrcnv (killp
) : save_kill
;
163 tio
.c_cc
[VERASE
] = erasep
? chrcnv (erasep
) : save_erase
;
165 /* set the new terminal attributes */
166 tcsetattr(0, TCSADRAIN
, &tio
);
168 /* print out new kill erase characters */
169 chrdsp ("erase", tio
.c_cc
[VERASE
]);
170 chrdsp (", kill", tio
.c_cc
[VKILL
]);
171 chrdsp (", intr", tio
.c_cc
[VINTR
]);
176 * We set the kill and erase character back to original
177 * setup in terminal structure so we can easily
178 * restore it upon exit.
180 tio
.c_cc
[VKILL
] = save_kill
;
181 tio
.c_cc
[VERASE
] = save_erase
;
185 SIGNAL2 (SIGINT
, intrser
);
188 * Loop through the lines of the draft skeleton.
190 gstate
= m_getfld_state_init(in
);
192 int fieldsz
= sizeof field
;
193 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
197 * Check if the value of field contains anything
198 * other than space or tab.
200 for (cp
= field
; *cp
; cp
++)
201 if (*cp
!= ' ' && *cp
!= '\t')
204 /* If so, just add header line to draft */
205 if (*cp
++ != '\n' || *cp
!= 0) {
206 printf ("%s:%s", name
, field
);
207 fprintf (out
, "%s:%s", name
, field
);
208 while (state
== FLDPLUS
) {
209 fieldsz
= sizeof field
;
210 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
211 fputs(field
, stdout
);
215 /* Else, get value of header field */
216 printf ("%s: ", name
);
218 i
= getln (field
, sizeof(field
));
221 if (killp
|| erasep
) {
222 tcsetattr(0, TCSADRAIN
, &tio
);
224 (void) m_unlink (tmpfil
);
227 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
228 fprintf (out
, "%s:", name
);
230 if (field
[0] != ' ' && field
[0] != '\t')
234 && (i
= getln (field
, sizeof(field
))) >= 0);
246 fprintf (out
, "--------\n");
247 if (field
[0] == 0 || !prepend
)
250 if (prepend
&& body
) {
251 puts("\n--------Enter initial text\n");
254 getln (buffer
, sizeof(buffer
));
255 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
265 if (!rapid
&& !sigint
)
266 fputs(field
, stdout
);
267 } while (state
== BODY
&&
268 (fieldsz
= sizeof field
,
269 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)));
270 if (prepend
|| !body
)
272 puts("\n--------Enter additional text\n");
277 getln (field
, sizeof(field
));
278 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
287 die("skeleton is poorly formatted");
291 m_getfld_state_destroy (&gstate
);
299 SIGNAL (SIGINT
, SIG_IGN
);
301 if (killp
|| erasep
) {
302 tcsetattr(0, TCSADRAIN
, &tio
);
305 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
306 adios (tmpfil
, "unable to re-open");
307 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
308 adios (drft
, "unable to write");
309 cpydata (fdi
, fdo
, tmpfil
, drft
);
312 (void) m_unlink (tmpfil
);
314 context_save (); /* save the context file */
321 getln (char *buffer
, int n
)
329 switch (setjmp (sigenv
)) {
347 switch (c
= getchar ()) {
351 longjmp (sigenv
, DONE
);
385 longjmp (sigenv
, NOTOK
);
393 return *cp
!= QUOTE
? *cp
: m_atoi(++cp
);
398 chrdsp (char *s
, char c
)
401 if (c
< ' ' || c
== 0177)
402 printf ("^%c", c
^ 0100);