annotate 3rdparty/packages/ed/makepat.c @ 3158:927ba5ebc06e

mc09 l2: move MMU bit-field defines to defs file.
author Neal Crook <foofoobedoo@gmail.com>
date Thu, 06 Apr 2017 21:43:58 +0100
parents bef1844de0dc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
994
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
1 /* makepat.c */
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
2 #include <stdio.h>
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
3 #include "tools.h"
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
4
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
5 /* Make a pattern template from the strinng pointed to by arg. Stop
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
6 * when delim or '\000' or '\n' is found in arg. Return a pointer to
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
7 * the pattern template.
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
8 *
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
9 * The pattern template used here are somewhat different than those
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
10 * used in the "Software Tools" book; each token is a structure of
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
11 * the form TOKEN (see tools.h). A token consists of an identifier,
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
12 * a pointer to a string, a literal character and a pointer to another
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
13 * token. This last is 0 if there is no subsequent token.
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
14 *
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
15 * The one strangeness here is caused (again) by CLOSURE which has
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
16 * to be put in front of the previous token. To make this insertion a
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
17 * little easier, the 'next' field of the last to point at the chain
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
18 * (the one pointed to by 'tail) is made to point at the previous node.
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
19 * When we are finished, tail->next is set to 0.
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
20 */
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
21 TOKEN *
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
22 makepat(arg, delim)
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
23 char *arg;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
24 int delim;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
25 {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
26 TOKEN *head, *tail, *ntok;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
27 int error;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
28
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
29 /* Check for characters that aren't legal at the beginning of a template. */
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
30
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
31 if (*arg == '\0' || *arg == delim || *arg == '\n' || *arg == CLOSURE)
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
32 return(0);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
33
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
34 error = 0;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
35 tail = head = NULL;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
36
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
37 while (*arg && *arg != delim && *arg != '\n' && !error) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
38 ntok = (TOKEN *) malloc(TOKSIZE);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
39 ntok->lchar = '\000';
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
40 ntok->next = 0;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
41
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
42 switch (*arg) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
43 case ANY: ntok->tok = ANY; break;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
44
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
45 case BOL:
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
46 if (head == 0) /* then this is the first symbol */
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
47 ntok->tok = BOL;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
48 else
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
49 ntok->tok = LITCHAR;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
50 ntok->lchar = BOL;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
51 break;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
52
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
53 case EOL:
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
54 if (*(arg + 1) == delim || *(arg + 1) == '\000' ||
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
55 *(arg + 1) == '\n') {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
56 ntok->tok = EOL;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
57 } else {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
58 ntok->tok = LITCHAR;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
59 ntok->lchar = EOL;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
60 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
61 break;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
62
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
63 case CLOSURE:
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
64 if (head != 0) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
65 switch (tail->tok) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
66 case BOL:
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
67 case EOL:
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
68 case CLOSURE:
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
69 return(0);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
70
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
71 default:
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
72 ntok->tok = CLOSURE;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
73 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
74 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
75 break;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
76
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
77 case CCL:
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
78
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
79 if (*(arg + 1) == NEGATE) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
80 ntok->tok = NCCL;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
81 arg += 2;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
82 } else {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
83 ntok->tok = CCL;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
84 arg++;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
85 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
86
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
87 if (ntok->bitmap = makebitmap(CLS_SIZE))
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
88 arg = dodash(CCLEND, arg, ntok->bitmap);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
89 else {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
90 fprintf(stderr, "Not enough memory for pat\n");
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
91 error = 1;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
92 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
93 break;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
94
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
95 default:
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
96 if (*arg == ESCAPE && *(arg + 1) == OPEN) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
97 ntok->tok = OPEN;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
98 arg++;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
99 } else if (*arg == ESCAPE && *(arg + 1) == CLOSE) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
100 ntok->tok = CLOSE;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
101 arg++;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
102 } else {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
103 ntok->tok = LITCHAR;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
104 ntok->lchar = esc(&arg);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
105 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
106 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
107
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
108 if (error || ntok == 0) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
109 unmakepat(head);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
110 return(0);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
111 } else if (head == 0) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
112 /* This is the first node in the chain. */
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
113
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
114 ntok->next = 0;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
115 head = tail = ntok;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
116 } else if (ntok->tok != CLOSURE) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
117 /* Insert at end of list (after tail) */
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
118
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
119 tail->next = ntok;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
120 ntok->next = tail;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
121 tail = ntok;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
122 } else if (head != tail) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
123 /* More than one node in the chain. Insert the
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
124 * CLOSURE node immediately in front of tail. */
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
125
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
126 (tail->next)->next = ntok;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
127 ntok->next = tail;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
128 } else {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
129 /* Only one node in the chain, Insert the CLOSURE
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
130 * node at the head of the linked list. */
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
131
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
132 ntok->next = head;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
133 tail->next = ntok;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
134 head = ntok;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
135 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
136 arg++;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
137 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
138
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
139 tail->next = 0;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
140 return(head);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
141 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
142