update from Rodney Ruddock (rodney@snowhite.cis.uoguelph.ca)
[unix-history] / usr / src / contrib / ed / filename.c
CommitLineData
837af067
KB
1/*-
2 * Copyright (c) 1992 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rodney Ruddock of the University of Guelph.
7 *
8 * %sccs.include.redist.c%
9 */
10
11#ifndef lint
05ee7127 12static char sccsid[] = "@(#)filename.c 5.7 (Berkeley) %G%";
837af067
KB
13#endif /* not lint */
14
ecbf4ad0
KB
15#include <sys/types.h>
16
e692f66f 17#include <limits.h>
ecbf4ad0
KB
18#include <regex.h>
19#include <setjmp.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
e692f66f
KB
24#ifdef DBI
25#include <db.h>
26#endif
27
837af067 28#include "ed.h"
ecbf4ad0 29#include "extern.h"
837af067 30
e692f66f 31
837af067
KB
32/*
33 * A central function for any command that has to deal with a filename
34 * (to be or not to be remembered).
35 */
ecbf4ad0
KB
36char *
37filename(inputt, errnum)
38 FILE *inputt;
39 int *errnum;
837af067 40{
ecbf4ad0
KB
41 register int l_cnt = 0;
42 char *l_fname;
72e6623b 43 int l_esc = 0, l_bang_flag = 0;
837af067 44
afdef93a 45 l_fname = calloc(FILENAME_LEN+2, sizeof(char));
ecbf4ad0
KB
46 if (l_fname == NULL) {
47 *errnum = -1;
48 strcpy(help_msg, "out of memory error");
49 return (NULL);
50 }
51 if ((ss = getc(inputt)) != ' ') {
52 if (ss == '\n') {
53 ungetc(ss, inputt);
54 /*
55 * It's not really an error, but to flag remembered
56 * filename is to be used.
57 */
58 *errnum = -2;
59 } else {
60 *errnum = -1;
61 strcpy(help_msg,
62 "space required before filename given");
63 }
64 return (NULL);
65 }
66 while (ss = getc(inputt))
67 if (ss != ' ') {
68 ungetc(ss, inputt);
69 break;
70 }
71 for (;;) {
72 ss = getc(inputt);
73 if ((ss == '\\') && (l_esc == 0)) {
74 ss = getchar();
75 l_esc = 1;
76 } else
77 l_esc = 0;
78 if ((ss == '\n') || (ss == EOF)) {
79 l_fname[l_cnt] = '\0';
80 break;
81 } else
05ee7127 82 if ((ss == '!') && (l_esc == 0) && (l_cnt < 2))
ecbf4ad0
KB
83 l_bang_flag = 1;
84 else
e692f66f 85 if ((ss != ' ') || (l_bang_flag))
ecbf4ad0 86 l_fname[l_cnt++] = ss;
e692f66f
KB
87 else {
88 *errnum = -1;
f3721604 89 return (NULL);
e692f66f 90 }
837af067 91
ecbf4ad0
KB
92 if (l_cnt >= FILENAME_LEN) {
93 strcpy(help_msg, "filename+path length too long");
94 *errnum = -1;
95 ungetc('\n', inputt);
96 return (NULL);
97 }
98 }
837af067 99
afdef93a
KB
100 if (l_fname[0] == '\0') {
101 sigspecial++;
102 strcpy(l_fname, filename_current);
103 sigspecial--;
104 if (sigint_flag && (!sigspecial))
105 SIGINT_ACTION;
106 }
e692f66f 107 *errnum = 1;
afdef93a 108 l_fname[FILENAME_LEN+1] = l_bang_flag;
ecbf4ad0
KB
109 return (l_fname);
110}