]>
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>
29 #define PROMPTER_SWITCHES \
30 X("erase chr", 0, ERASESW) \
31 X("kill chr", 0, KILLSW) \
32 X("prepend", 0, PREPSW) \
33 X("noprepend", 0, NPREPSW) \
34 X("rapid", 0, RAPDSW) \
35 X("norapid", 0, NRAPDSW) \
36 X("body", -4, BODYSW) \
37 X("nobody", -6, NBODYSW) \
38 X("doteof", 0, DOTSW) \
39 X("nodoteof", 0, NDOTSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(PROMPTER
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
52 static struct termios tio
;
53 #define ERASE tio.c_cc[VERASE]
54 #define KILL tio.c_cc[VKILL]
55 #define INTR tio.c_cc[VINTR]
57 static int wtuser
= 0;
58 static int sigint
= 0;
59 static jmp_buf sigenv
;
64 int getln (char *, int);
65 static int chrcnv (char *);
66 static void chrdsp (char *, char);
67 static void intrser (int);
71 main (int argc
, char **argv
)
73 int body
= 1, prepend
= 1, rapid
= 0;
74 int doteof
= 0, fdi
, fdo
, i
, state
;
75 char *cp
, *drft
= NULL
, *erasep
= NULL
;
76 char *killp
= NULL
, name
[NAMESZ
], field
[BUFSIZ
];
77 char buffer
[BUFSIZ
], tmpfil
[BUFSIZ
];
78 char **arguments
, **argp
;
81 m_getfld_state_t gstate
= 0;
84 setlocale(LC_ALL
, "");
86 invo_name
= r1bindex (argv
[0], '/');
88 /* read user profile/context */
91 arguments
= getarguments (invo_name
, argc
, argv
, 1);
94 while ((cp
= *argp
++))
96 switch (smatch (++cp
, switches
)) {
98 ambigsw (cp
, switches
);
101 adios (NULL
, "-%s unknown", cp
);
104 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
106 print_help (buffer
, switches
, 1);
109 print_version(invo_name
);
113 if (!(erasep
= *argp
++) || *erasep
== '-')
114 adios (NULL
, "missing argument to %s", argp
[-2]);
117 if (!(killp
= *argp
++) || *killp
== '-')
118 adios (NULL
, "missing argument to %s", argp
[-2]);
155 adios (NULL
, "usage: %s [switches] file", invo_name
);
156 if ((in
= fopen (drft
, "r")) == NULL
)
157 adios (drft
, "unable to open");
159 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
160 if (tfile
== NULL
) adios("prompter", "unable to create temporary file");
161 chmod (tmpfil
, 0600);
162 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
165 * Are we changing the kill or erase character?
167 if (killp
|| erasep
) {
168 cc_t save_erase
, save_kill
;
170 /* get the current terminal attributes */
173 /* save original kill, erase character for later */
177 /* set new kill, erase character in terminal structure */
178 KILL
= killp
? chrcnv (killp
) : save_kill
;
179 ERASE
= erasep
? chrcnv (erasep
) : save_erase
;
181 /* set the new terminal attributes */
182 tcsetattr(0, TCSADRAIN
, &tio
);
184 /* print out new kill erase characters */
185 chrdsp ("erase", ERASE
);
186 chrdsp (", kill", KILL
);
187 chrdsp (", intr", INTR
);
192 * We set the kill and erase character back to original
193 * setup in terminal structure so we can easily
194 * restore it upon exit.
201 SIGNAL2 (SIGINT
, intrser
);
204 * Loop through the lines of the draft skeleton.
207 int fieldsz
= sizeof field
;
208 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)) {
212 * Check if the value of field contains anything
213 * other than space or tab.
215 for (cp
= field
; *cp
; cp
++)
216 if (*cp
!= ' ' && *cp
!= '\t')
219 /* If so, just add header line to draft */
220 if (*cp
++ != '\n' || *cp
!= 0) {
221 printf ("%s:%s", name
, field
);
222 fprintf (out
, "%s:%s", name
, field
);
223 while (state
== FLDPLUS
) {
224 fieldsz
= sizeof field
;
225 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
);
226 printf ("%s", field
);
227 fprintf (out
, "%s", field
);
230 /* Else, get value of header field */
231 printf ("%s: ", name
);
233 i
= getln (field
, sizeof(field
));
236 if (killp
|| erasep
) {
237 tcsetattr(0, TCSADRAIN
, &tio
);
242 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
243 fprintf (out
, "%s:", name
);
245 if (field
[0] != ' ' && field
[0] != '\t')
247 fprintf (out
, "%s", field
);
249 && (i
= getln (field
, sizeof(field
))) >= 0);
261 fprintf (out
, "--------\n");
262 if (field
[0] == 0 || !prepend
)
263 printf ("--------\n");
265 if (prepend
&& body
) {
266 printf ("\n--------Enter initial text\n\n");
269 getln (buffer
, sizeof(buffer
));
270 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
274 fprintf (out
, "%s", buffer
);
279 fprintf (out
, "%s", field
);
280 if (!rapid
&& !sigint
)
281 printf ("%s", field
);
282 } while (state
== BODY
&&
283 (fieldsz
= sizeof field
,
284 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)));
285 if (prepend
|| !body
)
288 printf ("\n--------Enter additional text\n\n");
293 getln (field
, sizeof(field
));
294 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
298 fprintf (out
, "%s", field
);
303 adios (NULL
, "skeleton is poorly formatted");
307 m_getfld_state_destroy (&gstate
);
310 printf ("--------\n");
315 SIGNAL (SIGINT
, SIG_IGN
);
317 if (killp
|| erasep
) {
318 tcsetattr(0, TCSADRAIN
, &tio
);
321 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
322 adios (tmpfil
, "unable to re-open");
323 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
324 adios (drft
, "unable to write");
325 cpydata (fdi
, fdo
, tmpfil
, drft
);
330 context_save (); /* save the context file */
337 getln (char *buffer
, int n
)
345 switch (setjmp (sigenv
)) {
360 switch (c
= getchar ()) {
363 longjmp (sigenv
, DONE
);
366 if (cp
[-1] == QUOTE
) {
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);