]>
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/cpydata.h"
10 #include "sbr/m_atoi.h"
11 #include "sbr/context_save.h"
12 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
17 #include "h/signals.h"
20 #include "sbr/m_mktemp.h"
27 #define PROMPTER_SWITCHES \
28 X("erase chr", 0, ERASESW) \
29 X("kill chr", 0, KILLSW) \
30 X("prepend", 0, PREPSW) \
31 X("noprepend", 0, NPREPSW) \
32 X("rapid", 0, RAPDSW) \
33 X("norapid", 0, NRAPDSW) \
34 X("body", -4, BODYSW) \
35 X("nobody", -6, NBODYSW) \
36 X("doteof", 0, DOTSW) \
37 X("nodoteof", 0, NDOTSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(PROMPTER
);
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
49 static struct termios tio
;
53 static jmp_buf sigenv
;
58 static int getln (char *, int);
59 static int chrcnv (char *);
60 static void chrdsp (char *, char);
61 static void intrser (int);
65 main (int argc
, char **argv
)
71 int fdi
, fdo
, i
, state
;
72 char *cp
, *drft
= NULL
, *erasep
= NULL
;
73 char *killp
= NULL
, name
[NAMESZ
], field
[NMH_BUFSIZ
];
75 char **arguments
, **argp
;
78 m_getfld_state_t gstate
;
80 if (nmh_init(argv
[0], true, false)) { return 1; }
82 arguments
= getarguments (invo_name
, argc
, argv
, 1);
85 while ((cp
= *argp
++))
87 switch (smatch (++cp
, switches
)) {
89 ambigsw (cp
, switches
);
92 die("-%s unknown", cp
);
95 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
97 print_help (buffer
, switches
, 1);
100 print_version(invo_name
);
104 if (!(erasep
= *argp
++) || *erasep
== '-')
105 die("missing argument to %s", argp
[-2]);
108 if (!(killp
= *argp
++) || *killp
== '-')
109 die("missing argument to %s", argp
[-2]);
146 die("usage: %s [switches] file", invo_name
);
147 if ((in
= fopen (drft
, "r")) == NULL
)
148 adios (drft
, "unable to open");
150 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
151 die("unable to create temporary file in %s", get_temp_dir());
155 * Are we changing the kill or erase character?
157 if (killp
|| erasep
) {
158 cc_t save_erase
, save_kill
;
160 /* get the current terminal attributes */
163 /* save original kill, erase character for later */
164 save_kill
= tio
.c_cc
[VKILL
];
165 save_erase
= tio
.c_cc
[VERASE
];
167 /* set new kill, erase character in terminal structure */
168 tio
.c_cc
[VKILL
] = killp
? chrcnv (killp
) : save_kill
;
169 tio
.c_cc
[VERASE
] = erasep
? chrcnv (erasep
) : save_erase
;
171 /* set the new terminal attributes */
172 tcsetattr(0, TCSADRAIN
, &tio
);
174 /* print out new kill erase characters */
175 chrdsp ("erase", tio
.c_cc
[VERASE
]);
176 chrdsp (", kill", tio
.c_cc
[VKILL
]);
177 chrdsp (", intr", tio
.c_cc
[VINTR
]);
182 * We set the kill and erase character back to original
183 * setup in terminal structure so we can easily
184 * restore it upon exit.
186 tio
.c_cc
[VKILL
] = save_kill
;
187 tio
.c_cc
[VERASE
] = save_erase
;
191 SIGNAL2 (SIGINT
, intrser
);
194 * Loop through the lines of the draft skeleton.
196 gstate
= m_getfld_state_init(in
);
198 int fieldsz
= sizeof field
;
199 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
203 * Check if the value of field contains anything
204 * other than space or tab.
206 for (cp
= field
; *cp
; cp
++)
207 if (*cp
!= ' ' && *cp
!= '\t')
210 /* If so, just add header line to draft */
211 if (*cp
++ != '\n' || *cp
!= 0) {
212 printf ("%s:%s", name
, field
);
213 fprintf (out
, "%s:%s", name
, field
);
214 while (state
== FLDPLUS
) {
215 fieldsz
= sizeof field
;
216 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
217 fputs(field
, stdout
);
221 /* Else, get value of header field */
222 printf ("%s: ", name
);
224 i
= getln (field
, sizeof(field
));
227 if (killp
|| erasep
) {
228 tcsetattr(0, TCSADRAIN
, &tio
);
230 (void) m_unlink (tmpfil
);
233 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
234 fprintf (out
, "%s:", name
);
236 if (field
[0] != ' ' && field
[0] != '\t')
240 && (i
= getln (field
, sizeof(field
))) >= 0);
252 fprintf (out
, "--------\n");
253 if (field
[0] == 0 || !prepend
)
256 if (prepend
&& body
) {
257 puts("\n--------Enter initial text\n");
260 getln (buffer
, sizeof(buffer
));
261 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
271 if (!rapid
&& !sigint
)
272 fputs(field
, stdout
);
273 } while (state
== BODY
&&
274 (fieldsz
= sizeof field
,
275 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)));
276 if (prepend
|| !body
)
278 puts("\n--------Enter additional text\n");
283 getln (field
, sizeof(field
));
284 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
293 die("skeleton is poorly formatted");
297 m_getfld_state_destroy (&gstate
);
305 SIGNAL (SIGINT
, SIG_IGN
);
307 if (killp
|| erasep
) {
308 tcsetattr(0, TCSADRAIN
, &tio
);
311 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
312 adios (tmpfil
, "unable to re-open");
313 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
314 adios (drft
, "unable to write");
315 cpydata (fdi
, fdo
, tmpfil
, drft
);
318 (void) m_unlink (tmpfil
);
320 context_save (); /* save the context file */
327 getln (char *buffer
, int n
)
335 switch (setjmp (sigenv
)) {
353 switch (c
= getchar ()) {
357 longjmp (sigenv
, DONE
);
391 longjmp (sigenv
, NOTOK
);
399 return *cp
!= QUOTE
? *cp
: m_atoi(++cp
);
404 chrdsp (char *s
, char c
)
407 if (c
< ' ' || c
== 0177)
408 printf ("^%c", c
^ 0100);