]>
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
;
83 setlocale(LC_ALL
, "");
85 invo_name
= r1bindex (argv
[0], '/');
87 /* read user profile/context */
90 arguments
= getarguments (invo_name
, argc
, argv
, 1);
93 while ((cp
= *argp
++))
95 switch (smatch (++cp
, switches
)) {
97 ambigsw (cp
, switches
);
100 adios (NULL
, "-%s unknown", cp
);
103 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
105 print_help (buffer
, switches
, 1);
108 print_version(invo_name
);
112 if (!(erasep
= *argp
++) || *erasep
== '-')
113 adios (NULL
, "missing argument to %s", argp
[-2]);
116 if (!(killp
= *argp
++) || *killp
== '-')
117 adios (NULL
, "missing argument to %s", argp
[-2]);
154 adios (NULL
, "usage: %s [switches] file", invo_name
);
155 if ((in
= fopen (drft
, "r")) == NULL
)
156 adios (drft
, "unable to open");
158 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
159 if (tfile
== NULL
) adios("prompter", "unable to create temporary file");
160 chmod (tmpfil
, 0600);
161 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
164 * Are we changing the kill or erase character?
166 if (killp
|| erasep
) {
167 cc_t save_erase
, save_kill
;
169 /* get the current terminal attributes */
172 /* save original kill, erase character for later */
176 /* set new kill, erase character in terminal structure */
177 KILL
= killp
? chrcnv (killp
) : save_kill
;
178 ERASE
= erasep
? chrcnv (erasep
) : save_erase
;
180 /* set the new terminal attributes */
181 tcsetattr(0, TCSADRAIN
, &tio
);
183 /* print out new kill erase characters */
184 chrdsp ("erase", ERASE
);
185 chrdsp (", kill", KILL
);
186 chrdsp (", intr", INTR
);
191 * We set the kill and erase character back to original
192 * setup in terminal structure so we can easily
193 * restore it upon exit.
200 SIGNAL2 (SIGINT
, intrser
);
203 * Loop through the lines of the draft skeleton.
205 for (state
= FLD
;;) {
206 switch (state
= m_getfld (state
, name
, field
, sizeof(field
), in
)) {
211 * Check if the value of field contains anything
212 * other than space or tab.
214 for (cp
= field
; *cp
; cp
++)
215 if (*cp
!= ' ' && *cp
!= '\t')
218 /* If so, just add header line to draft */
219 if (*cp
++ != '\n' || *cp
!= 0) {
220 printf ("%s:%s", name
, field
);
221 fprintf (out
, "%s:%s", name
, field
);
222 while (state
== FLDPLUS
) {
224 m_getfld (state
, name
, field
, sizeof(field
), in
);
225 printf ("%s", field
);
226 fprintf (out
, "%s", field
);
229 /* Else, get value of header field */
230 printf ("%s: ", name
);
232 i
= getln (field
, sizeof(field
));
235 if (killp
|| erasep
) {
236 tcsetattr(0, TCSADRAIN
, &tio
);
241 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
242 fprintf (out
, "%s:", name
);
244 if (field
[0] != ' ' && field
[0] != '\t')
246 fprintf (out
, "%s", field
);
248 && (i
= getln (field
, sizeof(field
))) >= 0);
254 if (state
== FLDEOF
) { /* moby hack */
255 fprintf (out
, "--------\n");
256 printf ("--------\n");
268 fprintf (out
, "--------\n");
269 if (field
[0] == 0 || !prepend
)
270 printf ("--------\n");
272 if (prepend
&& body
) {
273 printf ("\n--------Enter initial text\n\n");
276 getln (buffer
, sizeof(buffer
));
277 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
281 fprintf (out
, "%s", buffer
);
286 fprintf (out
, "%s", field
);
287 if (!rapid
&& !sigint
)
288 printf ("%s", field
);
289 } while (state
== BODY
&&
290 (state
= m_getfld (state
, name
, field
, sizeof(field
), in
)));
291 if (prepend
|| !body
)
294 printf ("\n--------Enter additional text\n\n");
299 getln (field
, sizeof(field
));
300 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
304 fprintf (out
, "%s", field
);
309 adios (NULL
, "skeleton is poorly formatted");
315 printf ("--------\n");
320 SIGNAL (SIGINT
, SIG_IGN
);
322 if (killp
|| erasep
) {
323 tcsetattr(0, TCSADRAIN
, &tio
);
326 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
327 adios (tmpfil
, "unable to re-open");
328 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
329 adios (drft
, "unable to write");
330 cpydata (fdi
, fdo
, tmpfil
, drft
);
335 context_save (); /* save the context file */
342 getln (char *buffer
, int n
)
350 switch (setjmp (sigenv
)) {
365 switch (c
= getchar ()) {
368 longjmp (sigenv
, DONE
);
371 if (cp
[-1] == QUOTE
) {
396 longjmp (sigenv
, NOTOK
);
404 return (*cp
!= QUOTE
? *cp
: m_atoi (++cp
));
409 chrdsp (char *s
, char c
)
412 if (c
< ' ' || c
== 0177)
413 printf ("^%c", c
^ 0100);