1 /*
2 ** Copyright (C) 2000 Breyten J. Ernsting <bje@dds.nl>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 **
18 */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "xtermset.h"
25 
26 good_term *first_term, *cur_term;
27 
term_new(int comp_type,char * name,int len)28 good_term *term_new(int comp_type, char *name, int len) {
29    good_term *r;
30 
31    r = (good_term *)malloc(sizeof(good_term));
32    if(r==NULL) return r;
33 
34    memset(r,0,sizeof(good_term));
35    r->comp_type = comp_type;
36    strncpy(r->name,name,len);
37 
38    if (first_term) {
39       term_last();
40       cur_term->next = r;
41    } else {
42       first_term = r;
43    }
44    cur_term = r;
45 
46    return r;
47 }
48 
term_freeall()49 void term_freeall() {
50    good_term *n = first_term;
51 
52    while(n) {
53       good_term *n2;
54       n2 = n->next;
55       free(n);
56       n = n2;
57    }
58 }
59 
term_first()60 good_term *term_first() {
61    cur_term = first_term;
62    return cur_term;
63 }
64 
term_last()65 good_term *term_last() {
66    good_term *n = cur_term;
67    while(n->next)
68      n = n->next;
69    cur_term = n;
70    return cur_term;
71 }
72 
term_next()73 good_term *term_next() {
74    if(cur_term->next) {
75       cur_term = cur_term->next;
76       return cur_term;
77    } else {
78       return NULL;
79    }
80 }
81 
term_prev()82 good_term *term_prev() {
83    good_term *n = first_term;
84 
85    while((n->next) && (n != cur_term))
86       n = n->next;
87 
88    if(n) {
89       cur_term = n;
90       return cur_term;
91    } else {
92       return NULL;
93    }
94 }
95 
term_find(int comp_type,char * name)96 good_term *term_find(int comp_type, char *name) {
97    good_term *n = first_term;
98    int fnd = 0;
99 
100    while(n && (!fnd)) {
101       fnd = ((n->comp_type == comp_type) && (strcmp(n->name,name)==0));
102       if(!fnd)
103          n = n->next;
104    }
105 
106    if(n) {
107       cur_term = n;
108       return cur_term;
109    } else {
110       return NULL;
111    }
112 }
113 
term_loadall()114 void term_loadall() {
115    FILE *fp;
116    good_term *n;
117    char line[MAXTERMCHARS+3];
118    int i,j=0,l,t=0;
119    char c;
120 
121    fp = fopen(TERMSDIR "/" TERMSFILE,"r");
122 
123    if(fp) {
124       while(!feof(fp)) {
125          line[0]='\0';
126          j=t=0;
127          for(i=0;!feof(fp);i++) {
128             c=getc(fp);
129             if ((c==EOF)||(c=='\n'))
130                break;
131             line[i]=c;
132          }
133          line[i]='\0';
134          if((line[0]=='#')||(strlen(line)==0))
135             continue;
136          l = strlen(line);
137          if(line[0]=='^') {
138             j = 1;
139             l--;
140             t |= toStart;
141          }
142          if(line[i-1] == '$') {
143             l--;
144             t |= toEnd;
145          }
146          n = term_new(t,&line[j],l);
147       }
148       fclose(fp);
149    }
150 }
151 
is_good_term()152 int is_good_term()
153 {
154    int igt = 0,fnd = 0;
155    char *trm;
156 
157    trm = getenv("TERM");
158 
159    if (trm == NULL) return 0;
160 
161    if (term_first()) {
162       do {
163          fnd = 0;
164          if (cur_term->comp_type == 0) { /* anywhere in string */
165             fnd = (strstr(trm,cur_term->name) != NULL);
166          } else if (cur_term->comp_type & toStart) {
167             fnd = (strncmp(trm,cur_term->name,strlen(cur_term->name)) == 0);
168          } else if (cur_term->comp_type & toEnd) {
169             fnd = (strncmp(trm+(strlen(trm)-strlen(cur_term->name)),cur_term->name,strlen(cur_term->name))==0);
170          }
171          igt = fnd?1:0;
172       }  while ((!fnd) && (term_next()));
173    } else {
174       if(strncmp(trm,"xterm",5)==0) {
175          igt = 1;
176       } else if (strncmp(trm,"dtterm",6)==0) {
177          igt = 1;
178       } else if (strncmp(trm,"rxvt",4)==0) {
179          igt = 1;
180       }
181    }
182 
183    return igt;
184 }
185