📄 ledlib.c
字号:
case 'B': /* ignore punctuation */ { int origPs = *curPs; search (isupper ((int)ch), endOfWord, curLn, curPs, dir); if (*curPs == origPs) *number = 1; /* get out! */ fudge = (NEXT == EOS) ? 1 : 0; if (dir == FORWARD) { i = *curPs - tmp + fudge; strncpy (ledId->buffer, &curLn [tmp], i); strcpy (&curLn [tmp], &curLn [*curPs+fudge]); *curPs = tmp; } else { i = tmp - *curPs + fudge; strncpy (ledId->buffer, &CUR, i); strcpy (&CUR, &curLn [tmp+fudge]); tmp = *curPs - fudge; } break; } case ' ': case 'l': ledId->buffer [i++] = CUR; if (*curPs < strlen (curLn) - 1) { strcpy (&CUR, &NEXT); break; } else if (*curPs == strlen (curLn) - 1) *number = 1; /* get out! */ CUR = EOS; if (*curPs > 0) (*curPs)--; break; case 'd': strcpy (ledId->buffer, curLn); i = strlen (ledId->buffer); /* don't want 'i' to cause any grief */ *curPs = 0; CUR = EOS; *number = 1; /* get out! */ break; case '$': i = strlen (curLn) - *curPs + 1; strncpy (ledId->buffer, &CUR, i); CUR = EOS; *number = 1; /* get out! */ break; case '0': i = *curPs + 1; strncpy (ledId->buffer, &curLn [0], i); bcopy (&CUR, &curLn [0], strlen (&CUR) + 1); /* +EOS */ *curPs = 0; *number = 1; /* get out! */ break; case 'h': /* XXX - not implemented */ default: i = strlen (ledId->buffer); *number = 1; /* get out! */ status = ERROR; break; } } while (--*number > 0); *number = 0; ledId->buffer [i] = EOS; return (status); }/********************************************************************************* replace - replace current position thru count with specified character*/LOCAL STATUS replace ( char ch, char *curLn, int *curPs, int *number ) { if (*number < 1) *number = 1; /* don't do replacement if escape */ if (ch != ESC_CHAR) bfill (&CUR, min (*number, strlen (&CUR)), ch); *number = 0; return (OK); }/********************************************************************************* beep - make a noise*/LOCAL void beep ( int outFd ) { static char bell = BEL_CHAR; write (outFd, &bell, 1); }/********************************************************************************* writex - write bytes to a file but change control chars to '?'** RETURNS: number of characters actually written, pretty much like write.*/LOCAL int writex ( FAST int fd, char buffer [], FAST int nbytes ) { FAST int i; FAST char *pBuf = buffer; char non_ch = NON_CHAR; for (i = 0; i < nbytes; i++, pBuf++) { if (write (fd, (isprint ((int)(*pBuf)) ? pBuf : &non_ch), 1) != 1) return (ERROR); } return (nbytes); }/********************************************************************************* writen - write a character n times** NOTE: requires n <= (LINE_LEN + 1)** RETURNS: number of characters actually written, just like write.*/LOCAL int writen ( int fd, char ch, int nbytes ) { char buf [LINE_LEN + 1]; bfill (buf, nbytes, ch); return (write (fd, buf, nbytes)); }/********************************************************************************* histInit - initialize history list** On successive calls, resets history to new size.*/LOCAL void histInit ( LED_ID ledId, int histSize /* amount of history required */ ) { int histDiff = histSize - ledId->histSize; HIST *pH; if (ledId->histSize == 0) { lstFree (&ledId->histList); lstFree (&ledId->histFreeList); ledId->histNum = 0; lstInit (&ledId->histList); lstInit (&ledId->histFreeList); } if (histDiff >= 0) { /* add to history free list */ while (histDiff-- > 0) { /* allocate history space */ ledId->pHist = (HIST *) malloc (sizeof (HIST)); if (ledId->pHist == NULL) return; /* oh, oh! */ ledId->pHist->line[0] = EOS; lstAdd (&ledId->histFreeList, &ledId->pHist->node); } } else { /* free excess histories from history free list */ while (lstCount (&ledId->histFreeList) > 0 && histDiff++ < 0) { pH = (HIST *) lstGet (&ledId->histFreeList); free ((char *) pH); } /* free oldest histories from history list */ while (lstCount (&ledId->histList) > 0 && histDiff++ < 0) { pH = (HIST *) lstGet (&ledId->histList); free ((char *) pH); } } ledId->histSize = histSize; }/********************************************************************************* histAdd - add line to history*/LOCAL void histAdd ( FAST LED_ID ledId, FAST char *line ) { FAST HIST *pH = (HIST *) lstLast (&ledId->histList); FAST int i; ledId->pHist = NULL; /* ignore blank lines */ for (i = 0; isspace ((int)line[i]); i++) ; /* ignore blank lines & consecutive identical commands */ if (i >= strlen (line) || (pH != NULL && strcmp (line, pH->line) == 0)) return; /* try free list or get from top of history list */ if ((pH = (HIST *) lstGet (&ledId->histFreeList)) == NULL && (pH = (HIST *) lstGet (&ledId->histList)) == NULL) { /* history foul-up! */ return; } /* append to end of history list */ lstAdd (&ledId->histList, &pH->node); ledId->histNum++; strncpy (pH->line, line, LINE_LEN); pH->line [LINE_LEN - 1] = EOS; }/********************************************************************************* histNum - get historical line number** RETURNS: FALSE if no history by that number*/LOCAL BOOL histNum ( FAST LED_ID ledId, FAST char *line, /* where to return historical line */ FAST int n /* history number */ ) { int histFirst = ledId->histNum - lstCount (&ledId->histList) + 1; FAST HIST *pH; FAST int i; if (n < histFirst || n > ledId->histNum) return (FALSE); for (pH = (HIST *) lstFirst (&ledId->histList), i = histFirst; pH != NULL && i != n; pH = (HIST *) lstNext (&pH->node), i++) ; if (pH == NULL) return (FALSE); strcpy (line, pH->line); ledId->pHist = pH; return (TRUE); }/********************************************************************************* histNext - get next entry in history** If line == NULL then just forward history.** RETURNS: ERROR if no history*/LOCAL STATUS histNext ( FAST LED_ID ledId, FAST char *line /* where to return historical line */ ) { if (ledId->pHist == NULL) ledId->pHist = (HIST *) lstFirst (&ledId->histList); else if ((ledId->pHist = (HIST *) lstNext (&ledId->pHist->node)) == NULL) ledId->pHist = (HIST *) lstFirst (&ledId->histList); if (ledId->pHist == NULL) return (ERROR); if (line != NULL) strcpy (line, ledId->pHist->line); return (OK); }/********************************************************************************* histPrev - get previous entry in history** If line == NULL then just forward history.** RETURNS: ERROR if no history*/LOCAL STATUS histPrev ( FAST LED_ID ledId, FAST char *line /* where to return historical line */ ) { if (ledId->pHist == NULL) ledId->pHist = (HIST *) lstLast (&ledId->histList); else if ((ledId->pHist = (HIST *) lstPrevious (&ledId->pHist->node)) == NULL) { ledId->pHist = (HIST *) lstLast (&ledId->histList); } if (ledId->pHist == NULL) return (ERROR); if (line != NULL) strcpy (line, ledId->pHist->line); return (OK); }/********************************************************************************* histFind - find string in history list** Match should be of form: "/test", or "?test" for forward or backward searches.** RETURNS: TRUE if history found, line is replaced with historical line*/LOCAL BOOL histFind ( FAST LED_ID ledId, FAST char *match, /* what to match */ FAST char *line /* where to return historical line */ ) { char *pLine; FAST HIST *pH = ledId->pHist; FAST BOOL forward = match [0] == '?'; int i = lstCount (&ledId->histList) + 1; BOOL found = FALSE; if (pH == NULL && (pH = (HIST *) lstLast (&ledId->histList)) == NULL) return (FALSE); do { if (forward) { if ((pH = (HIST *) lstNext (&pH->node)) == NULL && (pH = (HIST *) lstFirst (&ledId->histList)) == NULL) return (FALSE); } pLine = pH->line; while ((pLine = index (pLine, match [1])) != 0) { if (strncmp (pLine, &match [1], strlen (match) - 1) == 0) { found = TRUE; break; /* found it! */ } else if (*(++pLine) == EOS) break; } if (!found && !forward) { if ((pH = (HIST *) lstPrevious (&pH->node)) == NULL && (pH = (HIST *) lstLast (&ledId->histList)) == NULL) return (FALSE); } } while (--i > 0 && !found); /* position 'pHist' correctly for next search */ if (!forward) { if ((ledId->pHist = (HIST *) lstPrevious (&pH->node)) == NULL) ledId->pHist = (HIST *) lstLast (&ledId->histList); } else ledId->pHist = pH; if (i < 1) return (FALSE); strcpy (line, pH->line); return (TRUE); }/********************************************************************************* histAll - print all historical lines*/LOCAL void histAll ( FAST LED_ID ledId ) { FAST int histFirst = ledId->histNum - lstCount (&ledId->histList) + 1; FAST HIST *pH; char buffer [LINE_LEN + 10]; /* Need to add padding for the * line number */ for (pH = (HIST *) lstFirst (&ledId->histList); pH != NULL; pH = (HIST *) lstNext (&pH->node)) { sprintf (buffer, "%3d %s", histFirst++, pH->line); (void)writex (ledId->outFd, buffer, strlen (buffer)); write (ledId->outFd, "\n", 1); } }#ifdef UNIX_DEBUG/********************************************************************************* main - UNIX debug module** NOMANUAL*/void main () { static char *junk[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "force", "fits", "fight", "several" }; char line [LINE_LEN+1]; int i; int ledId; setbuf (stdout, NULL); sysSymTbl = symTblCreate (6); for (i = 0; i < NELEMENTS (junk); i++) symSAdd (sysSymTbl, junk[i], 0, 0, symGroupDefault); ledId = ledOpen (0, 1, 20); system ("stty cbreak -echo"); printf ("ledLib: UNIX Debug\n"); printf ("-> "); while (ledRead (ledId, line, LINE_LEN) != EOF) { printf ("-> "); } system ("stty -cbreak echo"); }/********************************************************************************* errnoSet - bogus edition** NOMANUAL*/void errnoSet (status) int status; { printf ("errnoSet: new status 0x%x.\n", status); }#endif /* UNIX_DEBUG */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -