Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the functionality of Preserved Comments in cupsd.conf when cups… #640

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cups/adminutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ cupsAdminSetServerSettings(
cups_option_t *cupsd_settings, /* New settings */
*setting; /* Current setting */
_cups_globals_t *cg = _cupsGlobals(); /* Global data */
char comment_check[1024]; /* Comment already? */
char comment[1024] = ""; /* Comment */


/*
Expand Down Expand Up @@ -698,8 +700,11 @@ cupsAdminSetServerSettings(
if (server_port <= 0)
server_port = IPP_PORT;

while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
while (_cupsFileGetConfAndComments(cupsd, line, sizeof(line), &value, &linenum, comment, sizeof(comment)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about returning the comment via line parameter and remove the new additional parameters? Then the comment printout can happen via last cupsFilePrintf(). WDYT?

{
if(_cups_strcasecmp(comment, comment_check))
cupsFilePrintf(temp, "\n%s\n", comment);

if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) &&
(remote_admin >= 0 || remote_any >= 0 || share_printers >= 0))
{
Expand Down Expand Up @@ -774,8 +779,6 @@ cupsAdminSetServerSettings(

if (debug_logging)
{
cupsFilePuts(temp,
"# Show troubleshooting information in error_log.\n");
cupsFilePuts(temp, "LogLevel debug\n");
}
else
Expand Down Expand Up @@ -1059,6 +1062,8 @@ cupsAdminSetServerSettings(
}
else
cupsFilePrintf(temp, "%*s%s\n", indent, "", line);

_cups_strcpy(comment_check, comment);
}

/*
Expand Down
136 changes: 136 additions & 0 deletions cups/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,142 @@ cupsFileGetConf(cups_file_t *fp, /* I - CUPS file */
return (NULL);
}

/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the function definition to an alphabetically correct place in the file (under _cupsFileCheckFilter())

* 'cupsFileGetConfAndComments()' - Get a line and Comments from a configuration file.
*/

char * /* O - Line read or @code NULL@ on end of file or error */
_cupsFileGetConfAndComments(
cups_file_t *fp, /* I - CUPS file */
char *buf, /* O - String buffer */
size_t buflen, /* I - Size of string buffer */
char **value, /* O - Pointer to value */
int *linenum, /* IO - Current line number */
char *comment, /* I - Comment */
size_t commentlen) /* I - Size of comment buffer */
{
char *ptr; /* Pointer into line */

/*
* Range check input...
*/

DEBUG_printf(("2cupsFileGetConfAndComments(fp=%p, buf=%p, buflen=" CUPS_LLFMT
", value=%p, linenum=%p, comment=%p, commentlen=" CUPS_LLFMT ", comment_linenum=%p)", (void *)fp, (void *)buf, CUPS_LLCAST buflen, (void *)value, (void *)linenum, (void *)comment, CUPS_LLCAST commentlen));

if (!fp || (fp->mode != 'r' && fp->mode != 's') ||
!buf || buflen < 2 || !value || !comment || commentlen<2)
{
if (value)
*value = NULL;

return (NULL);
}

/*
* Read the next non-comment line...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy&paste remain...

*/

*value = NULL;

while (cupsFileGets(fp, buf, buflen))
{
(*linenum) ++;

/*
* Strip any comments...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy and paste leftover.

*/

if ((ptr = strchr(buf, '#')) != NULL)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with passing comments via line, we can detect the comment, then remove the leading and trailing whitespaces and return buf.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zdohnal I made the changes as you requested. Now I 'm using line variable to get comments. Please review them.

if (ptr > buf && ptr[-1] == '\\')
{
// Unquote the #...
_cups_strcpy(ptr - 1, ptr);
}
else
{
// Strip the comment and any trailing whitespace...
while (ptr > buf)
{
if (!_cups_isspace(ptr[-1]))
break;

ptr --;
}

_cups_strcpy(comment, buf);
*ptr = '\0';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will become a dead code if we return before that.

Additionally I've realized we should deal with inline comments in case they appear (even though they are prohibited by cupsd.conf man page) in a way.

So I propose:

  • check whether there is text before '#':
    • if there is, strip the comment, since it is invalid and process further in the function and return the text before the '#' char
    • if there is only whitespace before '#', strip the leading whitespace and return the comment.

So the block if ((ptr = strchr(buf, '#')) != NULL) can become a handler for inline comments, which will strip the comment in case there is text before it, and the execution will process further.

}
}

/*
* Strip leading whitespace...
*/

for (ptr = buf; _cups_isspace(*ptr); ptr ++);

if (ptr > buf)
_cups_strcpy(buf, ptr);

/*
* See if there is anything left...
*/

if (buf[0])
{
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we could check whether the first char is # and if so, return buf. With appropriate comment.

* Yes, grab any value and return...
*/

for (ptr = buf; *ptr; ptr ++)
if (_cups_isspace(*ptr))
break;

if (*ptr)
{
/*
* Have a value, skip any other spaces...
*/

while (_cups_isspace(*ptr))
*ptr++ = '\0';

if (*ptr)
*value = ptr;

/*
* Strip trailing whitespace and > for lines that begin with <...
*/

ptr += strlen(ptr) - 1;

if (buf[0] == '<' && *ptr == '>')
*ptr-- = '\0';
else if (buf[0] == '<' && *ptr != '>')
{
/*
* Syntax error...
*/

*value = NULL;
return (buf);
}

while (ptr > *value && _cups_isspace(*ptr))
*ptr-- = '\0';
}

/*
* Return the line...
*/

return (buf);
}
}

return (NULL);
}

/*
* 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
Expand Down
1 change: 1 addition & 0 deletions cups/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ typedef struct _cups_file_s cups_file_t;/**** CUPS file type ****/
* Prototypes...
*/

extern char *_cupsFileGetConfAndComments(cups_file_t *fp, char *buf, size_t buflen, char **value, int *linenum, char *comment, size_t commentlen) _CUPS_PRIVATE;
extern int cupsFileClose(cups_file_t *fp) _CUPS_API_1_2;
extern int cupsFileCompression(cups_file_t *fp) _CUPS_API_1_2;
extern int cupsFileEOF(cups_file_t *fp) _CUPS_API_1_2;
Expand Down