]>
diplodocus.org Git - nmh/blob - uip/prompter.c
3 * prompter.c -- simple prompting editor front-end
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 #include <h/signals.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
);
50 static struct termios tio
;
51 #define ERASE tio.c_cc[VERASE]
52 #define KILL tio.c_cc[VKILL]
53 #define INTR tio.c_cc[VINTR]
55 static int wtuser
= 0;
56 static int sigint
= 0;
57 static jmp_buf sigenv
;
62 int getln (char *, int);
63 static int chrcnv (char *);
64 static void chrdsp (char *, char);
65 static void intrser (int);
69 main (int argc
, char **argv
)
71 int body
= 1, prepend
= 1, rapid
= 0;
72 int doteof
= 0, fdi
, fdo
, i
, state
;
73 char *cp
, *drft
= NULL
, *erasep
= NULL
;
74 char *killp
= NULL
, name
[NAMESZ
], field
[BUFSIZ
];
76 char **arguments
, **argp
;
79 m_getfld_state_t gstate
= 0;
81 if (nmh_init(argv
[0], 2)) { return 1; }
83 arguments
= getarguments (invo_name
, argc
, argv
, 1);
86 while ((cp
= *argp
++))
88 switch (smatch (++cp
, switches
)) {
90 ambigsw (cp
, switches
);
93 adios (NULL
, "-%s unknown", cp
);
96 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
98 print_help (buffer
, switches
, 1);
101 print_version(invo_name
);
105 if (!(erasep
= *argp
++) || *erasep
== '-')
106 adios (NULL
, "missing argument to %s", argp
[-2]);
109 if (!(killp
= *argp
++) || *killp
== '-')
110 adios (NULL
, "missing argument to %s", argp
[-2]);
147 adios (NULL
, "usage: %s [switches] file", invo_name
);
148 if ((in
= fopen (drft
, "r")) == NULL
)
149 adios (drft
, "unable to open");
151 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
152 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
156 * Are we changing the kill or erase character?
158 if (killp
|| erasep
) {
159 cc_t save_erase
, save_kill
;
161 /* get the current terminal attributes */
164 /* save original kill, erase character for later */
168 /* set new kill, erase character in terminal structure */
169 KILL
= killp
? chrcnv (killp
) : save_kill
;
170 ERASE
= erasep
? chrcnv (erasep
) : save_erase
;
172 /* set the new terminal attributes */
173 tcsetattr(0, TCSADRAIN
, &tio
);
175 /* print out new kill erase characters */
176 chrdsp ("erase", ERASE
);
177 chrdsp (", kill", KILL
);
178 chrdsp (", intr", INTR
);
183 * We set the kill and erase character back to original
184 * setup in terminal structure so we can easily
185 * restore it upon exit.
192 SIGNAL2 (SIGINT
, intrser
);
195 * Loop through the lines of the draft skeleton.
198 int fieldsz
= sizeof field
;
199 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)) {
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_getfld (&gstate
, name
, field
, &fieldsz
, in
);
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 printf ("\n--------Enter initial text\n\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_getfld (&gstate
, name
, field
, &fieldsz
, in
)));
276 if (prepend
|| !body
)
279 printf ("\n--------Enter additional text\n\n");
284 getln (field
, sizeof(field
));
285 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
294 adios (NULL
, "skeleton is poorly formatted");
298 m_getfld_state_destroy (&gstate
);
306 SIGNAL (SIGINT
, SIG_IGN
);
308 if (killp
|| erasep
) {
309 tcsetattr(0, TCSADRAIN
, &tio
);
312 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
313 adios (tmpfil
, "unable to re-open");
314 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
315 adios (drft
, "unable to write");
316 cpydata (fdi
, fdo
, tmpfil
, drft
);
319 (void) m_unlink (tmpfil
);
321 context_save (); /* save the context file */
328 getln (char *buffer
, int n
)
332 static int quoting
= 0;
336 switch (setjmp (sigenv
)) {
354 switch (c
= getchar ()) {
358 longjmp (sigenv
, DONE
);
392 longjmp (sigenv
, NOTOK
);
400 return (*cp
!= QUOTE
? *cp
: m_atoi (++cp
));
405 chrdsp (char *s
, char c
)
408 if (c
< ' ' || c
== 0177)
409 printf ("^%c", c
^ 0100);