wrong cursor position in c_show
[unix-history] / usr / src / usr.bin / window / lcmd.c
CommitLineData
6a571c25 1#ifndef lint
8aee01cc 2static char *sccsid = "@(#)lcmd.c 3.3 83/08/12";
6a571c25
EW
3#endif
4
5#include "defs.h"
6
7int l_window();
8int l_select();
9int l_escape();
10int l_label();
11int l_terse();
12int l_source();
13int l_write();
8aee01cc 14int l_close();
6a571c25
EW
15
16struct lcmd {
17 char *l_name; /* name of command */
18 int l_lmin; /* minimum length to check */
19 int l_lmax; /* maximum length to check */
20 int l_amin; /* minimum argument */
21 int l_amax; /* maximum argument */
22 int (*l_func)(); /* the function */
23};
24static struct lcmd lcmd[] = {
8aee01cc
EW
25 "%", 1, 1, 0, 0, l_select,
26 "close", 1, 0, 1, -1, l_close,
27 "escape", 1, 0, 1, 1, l_escape,
28 "label", 1, 0, 2, 2, l_label,
29 "source", 1, 0, 1, 1, l_source,
30 "terse", 1, 0, 0, 1, l_terse,
31 "window", 1, 0, 4, 4, l_window,
32 "write", 2, 0, 2, 2, l_write,
6a571c25
EW
33 0
34};
35
36dosource(filename)
37char *filename;
38{
39 register FILE *f;
40 char buf[BUFSIZ];
41
42 if ((f = fopen(filename, "r")) == 0)
43 return -1;
44 insource++;
45 beginerror(filename);
46 for (lineno = 1; fgets(buf, sizeof buf, f) != 0; lineno++)
47 dolongcmd(buf);
48 enderror();
49 insource = 0;
50 return 0;
51}
52
53dolongcmd(line)
54char *line;
55{
56 register struct lcmd *lp;
57 register len;
58
59 makeargv(line);
60 if (argc == 0)
61 return;
62 for (lp = lcmd; lp->l_name; lp++) {
63 len = strlen(*argv);
64 if (len < lp->l_lmin)
65 continue;
66 if (!strncmp(*argv, lp->l_name, lp->l_lmax ? lp->l_lmax : len))
67 break;
68 }
69 if (lp->l_name) {
70 if (lp->l_amin > argc - 1)
71 error("Too few arguments.");
8aee01cc 72 else if (lp->l_amax >= 0 && lp->l_amax < argc - 1)
6a571c25
EW
73 error("Too many arguments.");
74 else
75 (*lp->l_func)();
76 } else
77 error("%s: Unknown command.", *argv);
78}
79
80makeargv(p)
81register char *p;
82{
83 static char buf[BUFSIZ];
84 register char *q = buf, **pp = argv;
85 char quote = 0, escape = 0;
86 int i;
87
88 for (; *p == ' ' || *p == '\t'; p++)
89 ;
90 while (*p && *p != '\n' && *p != '#'
91 && pp < &argv[sizeof argv/sizeof *argv - 1]) {
92 *pp++ = q;
93 while (*p && *p != '\n') {
94 if (escape) {
95 switch (*p) {
96 case 'n':
97 *q++ = '\n';
98 p++;
99 break;
100 case 'r':
101 *q++ = '\r';
102 p++;
103 break;
104 case '0': case '1': case '2': case '3':
105 case '4': case '5': case '6': case '7':
106 *q = 0;
107 for (i = 3; --i >= 0
108 && *p >= '0' && *p <= '9';)
109 *q = *q << 3 | *p++ - '0';
110 q++;
111 break;
112 default:
113 *q++ = *p++;
114 break;
115 }
116 escape = 0;
117 } else if (*p == '\\') {
118 escape = 1;
119 p++;
120 } else if (quote) {
121 if (*p == quote) {
122 quote = 0;
123 p++;
124 } else
125 *q++ = *p++;
126 } else {
127 if (*p == '"' || *p == '\'')
128 quote = *p++;
129 else if (*p == ' ' || *p == '\t')
130 break;
131 else
132 *q++ = *p++;
133 }
134 }
135 *q++ = 0;
136 for (; *p == ' ' || *p == '\t'; p++)
137 ;
138 }
139 *pp = 0;
140 argc = pp - argv;
141}