]>
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.
9 #include "sbr/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
13 #include "h/signals.h"
16 #include "sbr/m_mktemp.h"
23 #define PROMPTER_SWITCHES \
24 X("erase chr", 0, ERASESW) \
25 X("kill chr", 0, KILLSW) \
26 X("prepend", 0, PREPSW) \
27 X("noprepend", 0, NPREPSW) \
28 X("rapid", 0, RAPDSW) \
29 X("norapid", 0, NRAPDSW) \
30 X("body", -4, BODYSW) \
31 X("nobody", -6, NBODYSW) \
32 X("doteof", 0, DOTSW) \
33 X("nodoteof", 0, NDOTSW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
37 #define X(sw, minchars, id) id,
38 DEFINE_SWITCH_ENUM(PROMPTER
);
41 #define X(sw, minchars, id) { sw, minchars, id },
42 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
45 static struct termios tio
;
49 static jmp_buf sigenv
;
54 static int getln (char *, int);
55 static int chrcnv (char *);
56 static void chrdsp (char *, char);
57 static void intrser (int);
61 main (int argc
, char **argv
)
67 int fdi
, fdo
, i
, state
;
68 char *cp
, *drft
= NULL
, *erasep
= NULL
;
69 char *killp
= NULL
, name
[NAMESZ
], field
[NMH_BUFSIZ
];
71 char **arguments
, **argp
;
74 m_getfld_state_t gstate
;
76 if (nmh_init(argv
[0], true, false)) { return 1; }
78 arguments
= getarguments (invo_name
, argc
, argv
, 1);
81 while ((cp
= *argp
++))
83 switch (smatch (++cp
, switches
)) {
85 ambigsw (cp
, switches
);
88 die("-%s unknown", cp
);
91 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
93 print_help (buffer
, switches
, 1);
96 print_version(invo_name
);
100 if (!(erasep
= *argp
++) || *erasep
== '-')
101 die("missing argument to %s", argp
[-2]);
104 if (!(killp
= *argp
++) || *killp
== '-')
105 die("missing argument to %s", argp
[-2]);
142 die("usage: %s [switches] file", invo_name
);
143 if ((in
= fopen (drft
, "r")) == NULL
)
144 adios (drft
, "unable to open");
146 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
147 die("unable to create temporary file in %s", get_temp_dir());
151 * Are we changing the kill or erase character?
153 if (killp
|| erasep
) {
154 cc_t save_erase
, save_kill
;
156 /* get the current terminal attributes */
159 /* save original kill, erase character for later */
160 save_kill
= tio
.c_cc
[VKILL
];
161 save_erase
= tio
.c_cc
[VERASE
];
163 /* set new kill, erase character in terminal structure */
164 tio
.c_cc
[VKILL
] = killp
? chrcnv (killp
) : save_kill
;
165 tio
.c_cc
[VERASE
] = erasep
? chrcnv (erasep
) : save_erase
;
167 /* set the new terminal attributes */
168 tcsetattr(0, TCSADRAIN
, &tio
);
170 /* print out new kill erase characters */
171 chrdsp ("erase", tio
.c_cc
[VERASE
]);
172 chrdsp (", kill", tio
.c_cc
[VKILL
]);
173 chrdsp (", intr", tio
.c_cc
[VINTR
]);
178 * We set the kill and erase character back to original
179 * setup in terminal structure so we can easily
180 * restore it upon exit.
182 tio
.c_cc
[VKILL
] = save_kill
;
183 tio
.c_cc
[VERASE
] = save_erase
;
187 SIGNAL2 (SIGINT
, intrser
);
190 * Loop through the lines of the draft skeleton.
192 gstate
= m_getfld_state_init(in
);
194 int fieldsz
= sizeof field
;
195 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
199 * Check if the value of field contains anything
200 * other than space or tab.
202 for (cp
= field
; *cp
; cp
++)
203 if (*cp
!= ' ' && *cp
!= '\t')
206 /* If so, just add header line to draft */
207 if (*cp
++ != '\n' || *cp
!= 0) {
208 printf ("%s:%s", name
, field
);
209 fprintf (out
, "%s:%s", name
, field
);
210 while (state
== FLDPLUS
) {
211 fieldsz
= sizeof field
;
212 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
213 fputs(field
, stdout
);
217 /* Else, get value of header field */
218 printf ("%s: ", name
);
220 i
= getln (field
, sizeof(field
));
223 if (killp
|| erasep
) {
224 tcsetattr(0, TCSADRAIN
, &tio
);
226 (void) m_unlink (tmpfil
);
229 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
230 fprintf (out
, "%s:", name
);
232 if (field
[0] != ' ' && field
[0] != '\t')
236 && (i
= getln (field
, sizeof(field
))) >= 0);
248 fprintf (out
, "--------\n");
249 if (field
[0] == 0 || !prepend
)
252 if (prepend
&& body
) {
253 puts("\n--------Enter initial text\n");
256 getln (buffer
, sizeof(buffer
));
257 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
267 if (!rapid
&& !sigint
)
268 fputs(field
, stdout
);
269 } while (state
== BODY
&&
270 (fieldsz
= sizeof field
,
271 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)));
272 if (prepend
|| !body
)
274 puts("\n--------Enter additional text\n");
279 getln (field
, sizeof(field
));
280 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
289 die("skeleton is poorly formatted");
293 m_getfld_state_destroy (&gstate
);
301 SIGNAL (SIGINT
, SIG_IGN
);
303 if (killp
|| erasep
) {
304 tcsetattr(0, TCSADRAIN
, &tio
);
307 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
308 adios (tmpfil
, "unable to re-open");
309 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
310 adios (drft
, "unable to write");
311 cpydata (fdi
, fdo
, tmpfil
, drft
);
314 (void) m_unlink (tmpfil
);
316 context_save (); /* save the context file */
323 getln (char *buffer
, int n
)
331 switch (setjmp (sigenv
)) {
349 switch (c
= getchar ()) {
353 longjmp (sigenv
, DONE
);
387 longjmp (sigenv
, NOTOK
);
395 return *cp
!= QUOTE
? *cp
: m_atoi(++cp
);
400 chrdsp (char *s
, char c
)
403 if (c
< ' ' || c
== 0177)
404 printf ("^%c", c
^ 0100);