]>
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 static struct swit switches
[] = {
58 static struct termios tio
;
59 #define ERASE tio.c_cc[VERASE]
60 #define KILL tio.c_cc[VKILL]
61 #define INTR tio.c_cc[VINTR]
63 static int wtuser
= 0;
64 static int sigint
= 0;
65 static jmp_buf sigenv
;
70 int getln (char *, int);
71 static int chrcnv (char *);
72 static void chrdsp (char *, char);
73 static void intrser (int);
77 main (int argc
, char **argv
)
79 int body
= 1, prepend
= 1, rapid
= 0;
80 int doteof
= 0, fdi
, fdo
, i
, state
;
81 char *cp
, *drft
= NULL
, *erasep
= NULL
;
82 char *killp
= NULL
, name
[NAMESZ
], field
[BUFSIZ
];
83 char buffer
[BUFSIZ
], tmpfil
[BUFSIZ
];
84 char **arguments
, **argp
;
89 setlocale(LC_ALL
, "");
91 invo_name
= r1bindex (argv
[0], '/');
93 /* read user profile/context */
96 arguments
= getarguments (invo_name
, argc
, argv
, 1);
99 while ((cp
= *argp
++))
101 switch (smatch (++cp
, switches
)) {
103 ambigsw (cp
, switches
);
106 adios (NULL
, "-%s unknown", cp
);
109 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
111 print_help (buffer
, switches
, 1);
114 print_version(invo_name
);
118 if (!(erasep
= *argp
++) || *erasep
== '-')
119 adios (NULL
, "missing argument to %s", argp
[-2]);
122 if (!(killp
= *argp
++) || *killp
== '-')
123 adios (NULL
, "missing argument to %s", argp
[-2]);
160 adios (NULL
, "usage: %s [switches] file", invo_name
);
161 if ((in
= fopen (drft
, "r")) == NULL
)
162 adios (drft
, "unable to open");
164 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
165 if (tfile
== NULL
) adios("prompter", "unable to create temporary file");
166 chmod (tmpfil
, 0600);
167 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
170 * Are we changing the kill or erase character?
172 if (killp
|| erasep
) {
173 cc_t save_erase
, save_kill
;
175 /* get the current terminal attributes */
178 /* save original kill, erase character for later */
182 /* set new kill, erase character in terminal structure */
183 KILL
= killp
? chrcnv (killp
) : save_kill
;
184 ERASE
= erasep
? chrcnv (erasep
) : save_erase
;
186 /* set the new terminal attributes */
187 tcsetattr(0, TCSADRAIN
, &tio
);
189 /* print out new kill erase characters */
190 chrdsp ("erase", ERASE
);
191 chrdsp (", kill", KILL
);
192 chrdsp (", intr", INTR
);
197 * We set the kill and erase character back to original
198 * setup in terminal structure so we can easily
199 * restore it upon exit.
206 SIGNAL2 (SIGINT
, intrser
);
209 * Loop through the lines of the draft skeleton.
211 for (state
= FLD
;;) {
212 int fieldsz
= sizeof field
;
213 switch (state
= m_getfld (state
, name
, field
, &fieldsz
, in
)) {
218 * Check if the value of field contains anything
219 * other than space or tab.
221 for (cp
= field
; *cp
; cp
++)
222 if (*cp
!= ' ' && *cp
!= '\t')
225 /* If so, just add header line to draft */
226 if (*cp
++ != '\n' || *cp
!= 0) {
227 printf ("%s:%s", name
, field
);
228 fprintf (out
, "%s:%s", name
, field
);
229 while (state
== FLDPLUS
) {
230 fieldsz
= sizeof field
;
231 state
= m_getfld (state
, name
, field
, &fieldsz
, in
);
232 printf ("%s", field
);
233 fprintf (out
, "%s", field
);
236 /* Else, get value of header field */
237 printf ("%s: ", name
);
239 i
= getln (field
, sizeof(field
));
242 if (killp
|| erasep
) {
243 tcsetattr(0, TCSADRAIN
, &tio
);
248 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
249 fprintf (out
, "%s:", name
);
251 if (field
[0] != ' ' && field
[0] != '\t')
253 fprintf (out
, "%s", field
);
255 && (i
= getln (field
, sizeof(field
))) >= 0);
261 if (state
== FLDEOF
) { /* moby hack */
262 fprintf (out
, "--------\n");
263 printf ("--------\n");
275 fprintf (out
, "--------\n");
276 if (field
[0] == 0 || !prepend
)
277 printf ("--------\n");
279 if (prepend
&& body
) {
280 printf ("\n--------Enter initial text\n\n");
283 getln (buffer
, sizeof(buffer
));
284 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
288 fprintf (out
, "%s", buffer
);
293 fprintf (out
, "%s", field
);
294 if (!rapid
&& !sigint
)
295 printf ("%s", field
);
296 } while (state
== BODY
&&
297 (fieldsz
= sizeof field
,
298 state
= m_getfld (state
, name
, field
, &fieldsz
, in
)));
299 if (prepend
|| !body
)
302 printf ("\n--------Enter additional text\n\n");
307 getln (field
, sizeof(field
));
308 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
312 fprintf (out
, "%s", field
);
317 adios (NULL
, "skeleton is poorly formatted");
323 printf ("--------\n");
328 SIGNAL (SIGINT
, SIG_IGN
);
330 if (killp
|| erasep
) {
331 tcsetattr(0, TCSADRAIN
, &tio
);
334 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
335 adios (tmpfil
, "unable to re-open");
336 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
337 adios (drft
, "unable to write");
338 cpydata (fdi
, fdo
, tmpfil
, drft
);
343 context_save (); /* save the context file */
350 getln (char *buffer
, int n
)
358 switch (setjmp (sigenv
)) {
373 switch (c
= getchar ()) {
376 longjmp (sigenv
, DONE
);
379 if (cp
[-1] == QUOTE
) {
404 longjmp (sigenv
, NOTOK
);
412 return (*cp
!= QUOTE
? *cp
: m_atoi (++cp
));
417 chrdsp (char *s
, char c
)
420 if (c
< ' ' || c
== 0177)
421 printf ("^%c", c
^ 0100);