Skip to content

Commit 8b00027

Browse files
committed
Better syntax highlight for xhost
1 parent 1b4746a commit 8b00027

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

tools/xhost.c

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,16 @@ static int XHost_Write(xstring_t *pString)
178178

179179
static void XHost_RemoveTailSpace(char *sEntry, size_t nLength)
180180
{
181-
if (nLength == 0) return;
182-
int nPosit = (int)nLength - 1;
181+
if (!nLength || !xstrused(sEntry)) return;
182+
size_t nPosit = nLength - 1;
183183

184-
while (nPosit >= 0 &&
184+
while (nPosit > 0 &&
185185
(isspace((unsigned char)sEntry[nPosit]) ||
186-
sEntry[nPosit] == '\n')) nPosit--;
186+
sEntry[nPosit] == '\n' ||
187+
sEntry[nPosit] == '\r' ||
188+
sEntry[nPosit] == '\t')) nPosit--;
187189

188-
if (nPosit >= 0) sEntry[nPosit + 1] = XSTR_NUL;
190+
sEntry[nPosit + 1] = XSTR_NUL;
189191
}
190192

191193
static xbool_t XHost_ParseEntry(xhost_entry_t *pEntry, const char *pLine)
@@ -407,16 +409,26 @@ static int XHost_RemoveEntry(xhost_ctx_t *pCtx, xbool_t bComment)
407409

408410
while (XFile_GetLine(&pCtx->file, pCtx->sLine, sizeof(pCtx->sLine)) > 0)
409411
{
410-
nLineNumber++;
411-
412-
if (pCtx->nLineNumber == nLineNumber || XHost_SearchEntry(pCtx))
412+
if (pCtx->nLineNumber == ++nLineNumber || XHost_SearchEntry(pCtx))
413413
{
414414
xlogd_wn("Found entry: %s", pCtx->sLine);
415415

416-
if (bComment && XString_Append(&pCtx->hosts, "#%s", pCtx->sLine) < 0)
416+
if (bComment)
417417
{
418-
xloge("Failed to add commented line to hosts buffer: %s", XSTRERR);
419-
return XSTDERR;
418+
int nPosit = 0;
419+
while (pCtx->sLine[nPosit] && isspace((unsigned char)pCtx->sLine[nPosit])) nPosit++;
420+
421+
if (pCtx->sLine[nPosit] && pCtx->sLine[nPosit] == '#')
422+
{
423+
XASSERT((XString_Append(&pCtx->hosts, "%s", pCtx->sLine) >= 0),
424+
xthrow("Failed to add line to hosts buffer (%s)", XSTRERR));
425+
426+
nCount++;
427+
continue;
428+
}
429+
430+
XASSERT((XString_Append(&pCtx->hosts, "#%s", pCtx->sLine) >= 0),
431+
xthrow("Failed to add line to hosts buffer (%s)", XSTRERR));
420432
}
421433

422434
nCount++;
@@ -476,7 +488,6 @@ static int XHost_UncommentEntry(xhost_ctx_t *pCtx)
476488
}
477489

478490
if (bComment) pCtx->sLine[nPosit] = '#';
479-
480491
XASSERT((XString_Append(&pCtx->hosts, "%s", pCtx->sLine) >= 0),
481492
xthrow("Failed to add line to hosts buffer (%s)", XSTRERR));
482493
}
@@ -527,7 +538,8 @@ static int XHost_DisplayHosts(xhost_ctx_t *pCtx, xbool_t bLines)
527538

528539
if (pCtx->sLine[nPosit] == '#')
529540
{
530-
XString_Append(&pCtx->hosts, "%s%s%s", XSTR_FMT_DIM, pCtx->sLine, XSTR_FMT_RESET);
541+
XHost_RemoveTailSpace(pCtx->sLine, strlen(pCtx->sLine));
542+
XString_Append(&pCtx->hosts, "%s%s%s\n", XSTR_FMT_DIM, pCtx->sLine, XSTR_FMT_RESET);
531543
continue;
532544
}
533545

@@ -545,9 +557,8 @@ static int XHost_DisplayHosts(xhost_ctx_t *pCtx, xbool_t bLines)
545557
if (pCtx->sLine[nCommentPosit] && pCtx->sLine[nCommentPosit] == '#')
546558
{
547559
XString_Add(&pCtx->hosts, &pCtx->sLine[nEnd], nCommentPosit - nEnd);
548-
XString_Append(&pCtx->hosts, "%s", XSTR_FMT_DIM);
549-
XString_Append(&pCtx->hosts, "%s", &pCtx->sLine[nCommentPosit]);
550-
XString_Append(&pCtx->hosts, "%s", XSTR_FMT_RESET);
560+
XHost_RemoveTailSpace(&pCtx->sLine[nCommentPosit], strlen(&pCtx->sLine[nCommentPosit]));
561+
XString_Append(&pCtx->hosts, "%s%s%s", XSTR_FMT_DIM, &pCtx->sLine[nCommentPosit], XSTR_FMT_RESET);
551562
}
552563
else
553564
{

0 commit comments

Comments
 (0)