1 /* $NetBSD: ifile.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */ 2 3 /* 4 * Copyright (C) 1984-2012 Mark Nudelman 5 * 6 * You may distribute under the terms of either the GNU General Public 7 * License or the Less License, as specified in the README file. 8 * 9 * For more information, see the README file. 10 */ 11 12 13 /* 14 * An IFILE represents an input file. 15 * 16 * It is actually a pointer to an ifile structure, 17 * but is opaque outside this module. 18 * Ifile structures are kept in a linked list in the order they 19 * appear on the command line. 20 * Any new file which does not already appear in the list is 21 * inserted after the current file. 22 */ 23 24 #include "less.h" 25 26 extern IFILE curr_ifile; 27 28 struct ifile { 29 struct ifile *h_next; /* Links for command line list */ 30 struct ifile *h_prev; 31 char *h_filename; /* Name of the file */ 32 void *h_filestate; /* File state (used in ch.c) */ 33 int h_index; /* Index within command line list */ 34 int h_hold; /* Hold count */ 35 char h_opened; /* Has this ifile been opened? */ 36 struct scrpos h_scrpos; /* Saved position within the file */ 37 }; 38 39 /* 40 * Convert an IFILE (external representation) 41 * to a struct file (internal representation), and vice versa. 42 */ 43 #define int_ifile(h) ((struct ifile *)(h)) 44 #define ext_ifile(h) ((IFILE)(h)) 45 46 /* 47 * Anchor for linked list. 48 */ 49 static struct ifile anchor = { &anchor, &anchor, NULL, NULL, 0, 0, '\0', 50 { NULL_POSITION, 0 } }; 51 static int ifiles = 0; 52 53 static void incr_index __P((struct ifile *, int)); 54 static void link_ifile __P((struct ifile *, struct ifile *)); 55 static void unlink_ifile __P((struct ifile *)); 56 static struct ifile *new_ifile __P((char *, struct ifile *)); 57 static struct ifile *find_ifile __P((char *)); 58 59 static void 60 incr_index(p, incr) 61 register struct ifile *p; 62 int incr; 63 { 64 for (; p != &anchor; p = p->h_next) 65 p->h_index += incr; 66 } 67 68 /* 69 * Link an ifile into the ifile list. 70 */ 71 static void 72 link_ifile(p, prev) 73 struct ifile *p; 74 struct ifile *prev; 75 { 76 /* 77 * Link into list. 78 */ 79 if (prev == NULL) 80 prev = &anchor; 81 p->h_next = prev->h_next; 82 p->h_prev = prev; 83 prev->h_next->h_prev = p; 84 prev->h_next = p; 85 /* 86 * Calculate index for the new one, 87 * and adjust the indexes for subsequent ifiles in the list. 88 */ 89 p->h_index = prev->h_index + 1; 90 incr_index(p->h_next, 1); 91 ifiles++; 92 } 93 94 /* 95 * Unlink an ifile from the ifile list. 96 */ 97 static void 98 unlink_ifile(p) 99 struct ifile *p; 100 { 101 p->h_next->h_prev = p->h_prev; 102 p->h_prev->h_next = p->h_next; 103 incr_index(p->h_next, -1); 104 ifiles--; 105 } 106 107 /* 108 * Allocate a new ifile structure and stick a filename in it. 109 * It should go after "prev" in the list 110 * (or at the beginning of the list if "prev" is NULL). 111 * Return a pointer to the new ifile structure. 112 */ 113 static struct ifile * 114 new_ifile(filename, prev) 115 char *filename; 116 struct ifile *prev; 117 { 118 register struct ifile *p; 119 120 /* 121 * Allocate and initialize structure. 122 */ 123 p = (struct ifile *) ecalloc(1, sizeof(struct ifile)); 124 p->h_filename = save(filename); 125 p->h_scrpos.pos = NULL_POSITION; 126 p->h_opened = 0; 127 p->h_hold = 0; 128 p->h_filestate = NULL; 129 link_ifile(p, prev); 130 return (p); 131 } 132 133 /* 134 * Delete an existing ifile structure. 135 */ 136 public void 137 del_ifile(h) 138 IFILE h; 139 { 140 register struct ifile *p; 141 142 if (h == NULL_IFILE) 143 return; 144 /* 145 * If the ifile we're deleting is the currently open ifile, 146 * move off it. 147 */ 148 unmark(h); 149 if (h == curr_ifile) 150 curr_ifile = getoff_ifile(curr_ifile); 151 p = int_ifile(h); 152 unlink_ifile(p); 153 free(p->h_filename); 154 free(p); 155 } 156 157 /* 158 * Get the ifile after a given one in the list. 159 */ 160 public IFILE 161 next_ifile(h) 162 IFILE h; 163 { 164 register struct ifile *p; 165 166 p = (h == NULL_IFILE) ? &anchor : int_ifile(h); 167 if (p->h_next == &anchor) 168 return (NULL_IFILE); 169 return (ext_ifile(p->h_next)); 170 } 171 172 /* 173 * Get the ifile before a given one in the list. 174 */ 175 public IFILE 176 prev_ifile(h) 177 IFILE h; 178 { 179 register struct ifile *p; 180 181 p = (h == NULL_IFILE) ? &anchor : int_ifile(h); 182 if (p->h_prev == &anchor) 183 return (NULL_IFILE); 184 return (ext_ifile(p->h_prev)); 185 } 186 187 /* 188 * Return a different ifile from the given one. 189 */ 190 public IFILE 191 getoff_ifile(ifile) 192 IFILE ifile; 193 { 194 IFILE newifile; 195 196 if ((newifile = prev_ifile(ifile)) != NULL_IFILE) 197 return (newifile); 198 if ((newifile = next_ifile(ifile)) != NULL_IFILE) 199 return (newifile); 200 return (NULL_IFILE); 201 } 202 203 /* 204 * Return the number of ifiles. 205 */ 206 public int 207 nifile() 208 { 209 return (ifiles); 210 } 211 212 /* 213 * Find an ifile structure, given a filename. 214 */ 215 static struct ifile * 216 find_ifile(filename) 217 char *filename; 218 { 219 register struct ifile *p; 220 221 for (p = anchor.h_next; p != &anchor; p = p->h_next) 222 if (strcmp(filename, p->h_filename) == 0) 223 return (p); 224 return (NULL); 225 } 226 227 /* 228 * Get the ifile associated with a filename. 229 * If the filename has not been seen before, 230 * insert the new ifile after "prev" in the list. 231 */ 232 public IFILE 233 get_ifile(filename, prev) 234 char *filename; 235 IFILE prev; 236 { 237 register struct ifile *p; 238 239 if ((p = find_ifile(filename)) == NULL) 240 p = new_ifile(filename, int_ifile(prev)); 241 return (ext_ifile(p)); 242 } 243 244 /* 245 * Get the filename associated with a ifile. 246 */ 247 public char * 248 get_filename(ifile) 249 IFILE ifile; 250 { 251 if (ifile == NULL) 252 return (NULL); 253 return (int_ifile(ifile)->h_filename); 254 } 255 256 /* 257 * Get the index of the file associated with a ifile. 258 */ 259 public int 260 get_index(ifile) 261 IFILE ifile; 262 { 263 return (int_ifile(ifile)->h_index); 264 } 265 266 /* 267 * Save the file position to be associated with a given file. 268 */ 269 public void 270 store_pos(ifile, scrpos) 271 IFILE ifile; 272 struct scrpos *scrpos; 273 { 274 int_ifile(ifile)->h_scrpos = *scrpos; 275 } 276 277 /* 278 * Recall the file position associated with a file. 279 * If no position has been associated with the file, return NULL_POSITION. 280 */ 281 public void 282 get_pos(ifile, scrpos) 283 IFILE ifile; 284 struct scrpos *scrpos; 285 { 286 *scrpos = int_ifile(ifile)->h_scrpos; 287 } 288 289 /* 290 * Mark the ifile as "opened". 291 */ 292 public void 293 set_open(ifile) 294 IFILE ifile; 295 { 296 int_ifile(ifile)->h_opened = 1; 297 } 298 299 /* 300 * Return whether the ifile has been opened previously. 301 */ 302 public int 303 opened(ifile) 304 IFILE ifile; 305 { 306 return (int_ifile(ifile)->h_opened); 307 } 308 309 public void 310 hold_ifile(ifile, incr) 311 IFILE ifile; 312 int incr; 313 { 314 int_ifile(ifile)->h_hold += incr; 315 } 316 317 public int 318 held_ifile(ifile) 319 IFILE ifile; 320 { 321 return (int_ifile(ifile)->h_hold); 322 } 323 324 public void * 325 get_filestate(ifile) 326 IFILE ifile; 327 { 328 return (int_ifile(ifile)->h_filestate); 329 } 330 331 public void 332 set_filestate(ifile, filestate) 333 IFILE ifile; 334 void *filestate; 335 { 336 int_ifile(ifile)->h_filestate = filestate; 337 } 338 339 #if 0 340 public void 341 if_dump() 342 { 343 register struct ifile *p; 344 345 for (p = anchor.h_next; p != &anchor; p = p->h_next) 346 { 347 printf("%x: %d. <%s> pos %d,%x\n", 348 p, p->h_index, p->h_filename, 349 p->h_scrpos.ln, p->h_scrpos.pos); 350 ch_dump(p->h_filestate); 351 } 352 } 353 #endif 354