Make printer's DNS-SD Service Names freely configurable#1570
Conversation
Introduce a configuration variable DNSSDServiceName which, if set, provides a template for the DNS-SD Service Name used to register a printer. In the string, the following placeholders are substituted: %n: the printer's name %i: the printer's info field %l: the printer's location field %m: the printer's make & model field %c: the server's name %%: a literal percent sign If DNSSDServiceName is unset (the default), the existing behaviour is used.
michaelrsweet
left a comment
There was a problem hiding this comment.
OK, I really like this enhancement!
Some feedback (beyond the inline comments):
- Please see CONTRIBUTING.md - your code doesn't follow the style we use for the CUPS sources.
- I'd like to rename the directive to "DNSSDNameTemplate"
I will also add an issue tracking the ability to set the "printer-dns-sd-name" value for a printer separately (so this template would set the initial value and then you can tweak each printer's DNS-SD service name after the fact).
| { | ||
| if (p->info && strlen(p->info) > 0) | ||
| if (DNSSDServiceName && DNSSDServiceName[0] != '\0') { | ||
| char *s = DNSSDServiceName, *e = name + sizeof(name), *q = name, c; |
There was a problem hiding this comment.
The normal coding style here would be to set the end pointer to "name + sizeof(name) - 1" to leave room for the NUL at the end.
Also, you might use "src", "dst", and "dstend" as variable names to make things clearer... :)
| if (DNSSDServiceName && DNSSDServiceName[0] != '\0') { | ||
| char *s = DNSSDServiceName, *e = name + sizeof(name), *q = name, c; | ||
|
|
||
| while (1) { |
There was a problem hiding this comment.
"while (*src && dst < dstend)"?
| case 'i': r = p->info; break; | ||
| case 'm': r = p->make_model; break; | ||
| case 'c': r = DNSSDComputerName; break; | ||
| default: continue; |
There was a problem hiding this comment.
Maybe log a warning message for unknown substitution characters?
| default: continue; | ||
| } | ||
| if (r == NULL) continue; | ||
| q += strlcpy(q, r, e - q); |
There was a problem hiding this comment.
strlcpy isn't standard. cupsCopyString is the CUPS API that uses strlcpy or its equivalent. But the return value is the number of characters that would have been copied if there was infinite space - I usually do copy and then "q += strlen(q)".
|
Thanks for the quick reply!
- Please see CONTRIBUTING.md - your code doesn't follow the style we use for the CUPS sources.
I did of course read that but it doesn't say anything about coding style. So I tried to match what was there.
- I'd like to rename the directive to "DNSSDNameTemplate"
No objection.
The normal coding style here would be to set the end pointer to "name + sizeof(name) - 1" to leave room for the NUL at the end.
I thought of that but as is it made computing the space for strlcpy() easier.
Also, you might use "src", "dst", and "dstend" as variable names to make things clearer... :)
Probably. The code was more a PoC or a basis for discussion, especially which fields can be substituted and how the % escapes are named.
Plus we quickly needed someting that did %n.
"while (*src && dst < dstend)"?
I did think of that, but who puts in the trailing \0 then?
Maybe log a warning message for unknown substitution characters?
Yes.
strlcpy isn't standard. cupsCopyString is the CUPS API that uses strlcpy or its equivalent.
I originally developed against 2.4.16, which used strlcpy() a lot.
But the return value is the number of characters that would have been copied if there was infinite space
As does strlcpy(), no?
I usually do copy and then "q += strlen(q)".
The code breaks out if strlcpy() can't copy everything.
|
|
Sorry, I should have referenced "DEVELOPING.md" which has the coding style stuff: Coding GuidelinesContributed source code must follow the guidelines below. While the examples Source code comments provide the reference portion of the CUPS Programming Copyright noticeIf there are changes for a file, which contains copyright notice already Only the following files must have copyright notice updated every year: README.md, Source FilesAll source files names must consist of lowercase ASCII letters, numbers, dash
The top of each source file contains a header giving the purpose or nature of Header FilesAll public header files must include the "versioning.h" header file, or a header Private API header files must be named with the suffix "-private", for example CommentsAll source code utilizes block comments within functions to describe the IndentationAll code blocks enclosed by brackets begin with the opening brace on a new Single-line statements following "do", "else", "for", "if", and "while" are SpacingA space follows each reserved word such as Return ValuesParenthesis surround values returned from a function: FunctionsFunctions with a global scope have a lowercase prefix followed by capitalized Functions with a local scope are declared static with lowercase names and Each function begins with a comment header describing what the function does, Return/output values are indicated using an "O" prefix, input values are The codedoc documentation generator also VariablesVariables with a global scope are capitalized, e.g., Variables with a local scope are lowercase with underscores between words, Each variable is declared on a separate line and is immediately followed by a TypesAll type names are lowercase with underscores between words and Each type has a comment immediately after the typedef: StructuresAll structure names are lowercase with underscores between words and Each structure has a comment immediately after the struct and each member is ConstantsAll constant names are uppercase with underscores between words, e.g., Typed enumerations should be used whenever possible to allow for type checking Comments immediately follow each constant: |
Introduce a configuration variable
DNSSDServiceNamewhich, if set, provides a template for the DNS-SD Service Name used to register a printer. In the string, the following placeholders are substituted:%n: the printer's name%i: the printer's info field%l: the printer's location field%m: the printer's make & model field%c: the server's name%%: a literal percent signIf
DNSSDServiceNameis unset (the default), the existing behaviour is used.