]>
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;
82 setlocale(LC_ALL
, "");
84 invo_name
= r1bindex (argv
[0], '/');
86 /* read user profile/context */
89 arguments
= getarguments (invo_name
, argc
, argv
, 1);
92 while ((cp
= *argp
++))
94 switch (smatch (++cp
, switches
)) {
96 ambigsw (cp
, switches
);
99 adios (NULL
, "-%s unknown", cp
);
102 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
104 print_help (buffer
, switches
, 1);
107 print_version(invo_name
);
111 if (!(erasep
= *argp
++) || *erasep
== '-')
112 adios (NULL
, "missing argument to %s", argp
[-2]);
115 if (!(killp
= *argp
++) || *killp
== '-')
116 adios (NULL
, "missing argument to %s", argp
[-2]);
153 adios (NULL
, "usage: %s [switches] file", invo_name
);
154 if ((in
= fopen (drft
, "r")) == NULL
)
155 adios (drft
, "unable to open");
157 tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
158 if (tmpfil
== NULL
) adios("prompter", "unable to create temporary file");
159 chmod (tmpfil
, 0600);
162 * Are we changing the kill or erase character?
164 if (killp
|| erasep
) {
165 cc_t save_erase
, save_kill
;
167 /* get the current terminal attributes */
170 /* save original kill, erase character for later */
174 /* set new kill, erase character in terminal structure */
175 KILL
= killp
? chrcnv (killp
) : save_kill
;
176 ERASE
= erasep
? chrcnv (erasep
) : save_erase
;
178 /* set the new terminal attributes */
179 tcsetattr(0, TCSADRAIN
, &tio
);
181 /* print out new kill erase characters */
182 chrdsp ("erase", ERASE
);
183 chrdsp (", kill", KILL
);
184 chrdsp (", intr", INTR
);
189 * We set the kill and erase character back to original
190 * setup in terminal structure so we can easily
191 * restore it upon exit.
198 SIGNAL2 (SIGINT
, intrser
);
201 * Loop through the lines of the draft skeleton.
204 int fieldsz
= sizeof field
;
205 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)) {
209 * Check if the value of field contains anything
210 * other than space or tab.
212 for (cp
= field
; *cp
; cp
++)
213 if (*cp
!= ' ' && *cp
!= '\t')
216 /* If so, just add header line to draft */
217 if (*cp
++ != '\n' || *cp
!= 0) {
218 printf ("%s:%s", name
, field
);
219 fprintf (out
, "%s:%s", name
, field
);
220 while (state
== FLDPLUS
) {
221 fieldsz
= sizeof field
;
222 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
);
223 printf ("%s", field
);
224 fprintf (out
, "%s", field
);
227 /* Else, get value of header field */
228 printf ("%s: ", name
);
230 i
= getln (field
, sizeof(field
));
233 if (killp
|| erasep
) {
234 tcsetattr(0, TCSADRAIN
, &tio
);
239 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
240 fprintf (out
, "%s:", name
);
242 if (field
[0] != ' ' && field
[0] != '\t')
244 fprintf (out
, "%s", field
);
246 && (i
= getln (field
, sizeof(field
))) >= 0);
258 fprintf (out
, "--------\n");
259 if (field
[0] == 0 || !prepend
)
260 printf ("--------\n");
262 if (prepend
&& body
) {
263 printf ("\n--------Enter initial text\n\n");
266 getln (buffer
, sizeof(buffer
));
267 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
271 fprintf (out
, "%s", buffer
);
276 fprintf (out
, "%s", field
);
277 if (!rapid
&& !sigint
)
278 printf ("%s", field
);
279 } while (state
== BODY
&&
280 (fieldsz
= sizeof field
,
281 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)));
282 if (prepend
|| !body
)
285 printf ("\n--------Enter additional text\n\n");
290 getln (field
, sizeof(field
));
291 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
295 fprintf (out
, "%s", field
);
300 adios (NULL
, "skeleton is poorly formatted");
304 m_getfld_state_destroy (&gstate
);
307 printf ("--------\n");
312 SIGNAL (SIGINT
, SIG_IGN
);
314 if (killp
|| erasep
) {
315 tcsetattr(0, TCSADRAIN
, &tio
);
318 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
319 adios (tmpfil
, "unable to re-open");
320 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
321 adios (drft
, "unable to write");
322 cpydata (fdi
, fdo
, tmpfil
, drft
);
327 context_save (); /* save the context file */
334 getln (char *buffer
, int n
)
342 switch (setjmp (sigenv
)) {
357 switch (c
= getchar ()) {
360 longjmp (sigenv
, DONE
);
363 if (cp
[-1] == QUOTE
) {
388 longjmp (sigenv
, NOTOK
);
396 return (*cp
!= QUOTE
? *cp
: m_atoi (++cp
));
401 chrdsp (char *s
, char c
)
404 if (c
< ' ' || c
== 0177)
405 printf ("^%c", c
^ 0100);