📄 tty_subr.c
字号:
}/* * Add a character to the end of a clist. Return -1 is no * more clists, or 0 for success. */intputc(chr, clistp) int chr; struct clist *clistp;{ struct cblock *cblockp; int s; s = spltty(); if (clistp->c_cl == NULL) { if (clistp->c_cbreserved < 1) { splx(s); printf("putc to a clist with no reserved cblocks\n"); return (-1); /* nothing done */ } cblockp = cblock_alloc(); clistp->c_cbcount = 1; clistp->c_cf = clistp->c_cl = cblockp->c_info; clistp->c_cc = 0; } else { cblockp = (struct cblock *)((long)clistp->c_cl & ~CROUND); if (((long)clistp->c_cl & CROUND) == 0) { struct cblock *prev = (cblockp - 1); if (clistp->c_cbcount >= clistp->c_cbreserved) { if (clistp->c_cbcount >= clistp->c_cbmax || cslushcount <= 0) { splx(s); return (-1); } --cslushcount; } cblockp = cblock_alloc(); clistp->c_cbcount++; prev->c_next = cblockp; clistp->c_cl = cblockp->c_info; } } /* * If this character is quoted, set the quote bit, if not, clear it. */ if (chr & TTY_QUOTE) { setbit(cblockp->c_quote, clistp->c_cl - (char *)cblockp->c_info); /* * Use one of the spare quote bits to record that something * may be quoted. */ setbit(cblockp->c_quote, CBQSIZE * NBBY - 1); } else clrbit(cblockp->c_quote, clistp->c_cl - (char *)cblockp->c_info); *clistp->c_cl++ = chr; clistp->c_cc++; splx(s); return (0);}/* * Copy data from linear buffer to clist chain. Return the * number of characters not copied. */intb_to_q(src, amount, clistp) char *src; int amount; struct clist *clistp;{ struct cblock *cblockp; char *firstbyte, *lastbyte; u_char startmask, endmask; int startbit, endbit, num_between, numc; int s; /* * Avoid allocating an initial cblock and then not using it. * c_cc == 0 must imply c_cbount == 0. */ if (amount <= 0) return (amount); s = spltty(); /* * If there are no cblocks assigned to this clist yet, * then get one. */ if (clistp->c_cl == NULL) { if (clistp->c_cbreserved < 1) { splx(s); printf("b_to_q to a clist with no reserved cblocks.\n"); return (amount); /* nothing done */ } cblockp = cblock_alloc(); clistp->c_cbcount = 1; clistp->c_cf = clistp->c_cl = cblockp->c_info; clistp->c_cc = 0; } else { cblockp = (struct cblock *)((long)clistp->c_cl & ~CROUND); } while (amount) { /* * Get another cblock if needed. */ if (((long)clistp->c_cl & CROUND) == 0) { struct cblock *prev = cblockp - 1; if (clistp->c_cbcount >= clistp->c_cbreserved) { if (clistp->c_cbcount >= clistp->c_cbmax || cslushcount <= 0) { splx(s); return (amount); } --cslushcount; } cblockp = cblock_alloc(); clistp->c_cbcount++; prev->c_next = cblockp; clistp->c_cl = cblockp->c_info; } /* * Copy a chunk of the linear buffer up to the end * of this cblock. */ numc = min(amount, (char *)(cblockp + 1) - clistp->c_cl); bcopy(src, clistp->c_cl, numc); /* * Clear quote bits if they aren't known to be clear. * The following could probably be made into a seperate * "bitzero()" routine, but why bother? */ if (isset(cblockp->c_quote, CBQSIZE * NBBY - 1)) { startbit = clistp->c_cl - (char *)cblockp->c_info; endbit = startbit + numc - 1; firstbyte = (u_char *)cblockp->c_quote + (startbit / NBBY); lastbyte = (u_char *)cblockp->c_quote + (endbit / NBBY); /* * Calculate mask of bits to preserve in first and * last bytes. */ startmask = NBBY - (startbit % NBBY); startmask = 0xff >> startmask; endmask = (endbit % NBBY); endmask = 0xff << (endmask + 1); if (firstbyte != lastbyte) { *firstbyte &= startmask; *lastbyte &= endmask; num_between = lastbyte - firstbyte - 1; if (num_between) bzero(firstbyte + 1, num_between); } else { *firstbyte &= (startmask | endmask); } } /* * ...and update pointer for the next chunk. */ src += numc; clistp->c_cl += numc; clistp->c_cc += numc; amount -= numc; /* * If we go through the loop again, it's always * for data in the next cblock, so by adding one (cblock), * (which makes the pointer 1 beyond the end of this * cblock) we prepare for the assignment of 'prev' * above. */ cblockp += 1; } splx(s); return (amount);}/* * Get the next character in the clist. Store it at dst. Don't * advance any clist pointers, but return a pointer to the next * character position. */char *nextc(clistp, cp, dst) struct clist *clistp; char *cp; int *dst;{ struct cblock *cblockp; ++cp; /* * See if the next character is beyond the end of * the clist. */ if (clistp->c_cc && (cp != clistp->c_cl)) { /* * If the next character is beyond the end of this * cblock, advance to the next cblock. */ if (((long)cp & CROUND) == 0) cp = ((struct cblock *)cp - 1)->c_next->c_info; cblockp = (struct cblock *)((long)cp & ~CROUND); /* * Get the character. Set the quote flag if this character * is quoted. */ *dst = (u_char)*cp | (isset(cblockp->c_quote, cp - (char *)cblockp->c_info) ? TTY_QUOTE : 0); return (cp); } return (NULL);}/* * "Unput" a character from a clist. */intunputc(clistp) struct clist *clistp;{ struct cblock *cblockp = 0, *cbp = 0; int s; int chr = -1; s = spltty(); if (clistp->c_cc) { --clistp->c_cc; --clistp->c_cl; chr = (u_char)*clistp->c_cl; cblockp = (struct cblock *)((long)clistp->c_cl & ~CROUND); /* * Set quote flag if this character was quoted. */ if (isset(cblockp->c_quote, (u_char *)clistp->c_cl - cblockp->c_info)) chr |= TTY_QUOTE; /* * If all of the characters have been unput in this * cblock, then find the previous one and free this * one. */ if (clistp->c_cc && (clistp->c_cl <= (char *)cblockp->c_info)) { cbp = (struct cblock *)((long)clistp->c_cf & ~CROUND); while (cbp->c_next != cblockp) cbp = cbp->c_next; /* * When the previous cblock is at the end, the 'last' * pointer always points (invalidly) one past. */ clistp->c_cl = (char *)(cbp+1); cblock_free(cblockp); if (--clistp->c_cbcount >= clistp->c_cbreserved) ++cslushcount; cbp->c_next = NULL; } } /* * If there are no more characters on the list, then * free the last cblock. */ if ((clistp->c_cc == 0) && clistp->c_cl) { cblockp = (struct cblock *)((long)clistp->c_cl & ~CROUND); cblock_free(cblockp); if (--clistp->c_cbcount >= clistp->c_cbreserved) ++cslushcount; clistp->c_cf = clistp->c_cl = NULL; } splx(s); return (chr);}/* * Move characters in source clist to destination clist, * preserving quote bits. */voidcatq(src_clistp, dest_clistp) struct clist *src_clistp, *dest_clistp;{ int chr, s; s = spltty(); /* * If the destination clist is empty (has no cblocks atttached), * and there are no possible complications with the resource counters, * then we simply assign the current clist to the destination. */ if (!dest_clistp->c_cf && src_clistp->c_cbcount <= src_clistp->c_cbmax && src_clistp->c_cbcount <= dest_clistp->c_cbmax) { dest_clistp->c_cf = src_clistp->c_cf; dest_clistp->c_cl = src_clistp->c_cl; src_clistp->c_cf = src_clistp->c_cl = NULL; dest_clistp->c_cc = src_clistp->c_cc; src_clistp->c_cc = 0; dest_clistp->c_cbcount = src_clistp->c_cbcount; src_clistp->c_cbcount = 0; splx(s); return; } splx(s); /* * XXX This should probably be optimized to more than one * character at a time. */ while ((chr = getc(src_clistp)) != -1) putc(chr, dest_clistp);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -