What does %s mean inside a string literal? - printf

I'm looking at the following code I found in libgksu and I'm wondering what the %s inside the string does. I'm unable to use Google for this since it strips away characters such as the percentile during the search, leaving me only with 's' as a search term.
if (!strcmp(context->user, "root"))
msg = g_strdup_printf (_("<b><big>Enter your password to perform"
" administrative tasks</big></b>\n\n"
"The application '%s' lets you "
"modify essential parts of your "
"system."),
command);
The purpose of this piece of code is to provide the text for the dialogue box that the user sees when an application requests superuser privileges on Linux, as can be seen in this screenshot
The %s in this case is the variable that contains the name of the application requesting privileges, but it isn't as simple as that because I've seen the %s used throughout the code in completely different contexts. For example, the else component of the above if statement is
else
msg = g_strdup_printf (_("<b><big>Enter your password to run "
"the application '%s' as user %s"
"</big></b>"),
command, context->user);
and %s is being used to mark the name of both an application and a user. Can someone please tell me what purpose of %s is and where I can find out more information on it's use? I'm assuming this is a regular expression, but as I said earlier, I can't Google to find out.

%s is a C format specifier for a string.
msg = g_strdup_printf (_("<b><big>Enter your password to run "
"the application '%s' as user %s"
"</big></b>"),
command, context->user);
means "where you see the first %s, replace it with the contents of command as a string, and where you see the second %s, replace it with the contents of context->user as a string.

printf() has a long C-based history. the %s is a 'format character', indicating "insert a string here". The extra parameters after the string in your two function calls are the values to fill into the format character placeholders:
In the first example, %s will be replaced with the contents of the command variable. In the second example, the first %s will get command, and the second %s will get context->user.

It's a format flag. You can look at the 'printf' man page for more informations.
Basicaly, every %s will be replaced by the function argument corresponding.
printf("%s %s", "hello", "world") will print a simple "hello world"

%s will simply be replaced by string like
char a[15]="some string";
printf("this is %s.",a);
so output will be
this is some string.

Related

How to show an unknown list of variables and their values, if possible

As mentioned before in some questions with "Progress-4GL" and "OpenEdge" tags, I'm working with AppBuilder and Procedure editor. As a result, the debugging possibilities are extremely limited: for knowing the value of a variable, I need to do show them on screen, something like this:
MESSAGE "temp1=[" temp1 "], temp2=[" temp2 "]" VIEW-AS ALERT-BOX.
I can also put that information in a logfile, but that's not the main point here.
I would like to write a procedure, which can handle this, something like:
PROCEDURE SHOW_VARIABLES_AND_VALUES (INPUT I1, INPUT I2, ...):
1. <put parameter names and values together inside one string> => """I1="" I1"
2. <do this for all input parameters (the number is unknown)> => """I1="" I1, ""I2="" I2, ..."
3. <how to use this (MESSAGE VIEW-AS ALERT-BOX, LOG, ...) there I'll know what to do>
Does anybody know how to handle the fist two points (put variable name and value together and handle an unknown number of input parameters)?
Thanks in advance
You can use SUBSTITUTE function.
MESSAGE SUBSTITUTE ("temp1=&1 ~ntemp2=&2 ~n temp3=&3",
temp1,
temp2,
temp3) VIEW-AS ALERT-BOX.
Unfortunately there is no dynamic access to variables or parameters. So there's no way to automatically add all input parameters to a message string. Also there is no anytype parameter type in the ABL - for user defined functions or procedures. So you'd have to use the STRING() function a lot to convert your input parameters to string as the best fit parameter for everything.
The built in SUBSTITUTE function on the other hand can handle anytype of arguments. So temp1, temp2 and temp3 can actually be variables or parameters of any datatype.
As mentioned in one of my comments on one of your earlier questions: Give the OpenEdge debugger a chance. The debugger outside of Progress Developer studio looks historic. But it does it's job.
Meanwhile I've decided to use following system (as my request seems to be impossible):
MESSAGE "temp1=[" temp1 "]~n" ~
"temp2=[" temp2 "]~n" ~
"temp3=[" temp3 "]~n" ~
"temp4=[" temp4 "]" ~
VIEW-AS ALERT-BOX.
In order to make it easy to work with, I've found out the following keyboard "shortcut" for the tilde character: ALT+0126.
As indicated by Stefan, this is far better (no tilde and no shortcut needed):
MESSAGE "temp1=[" temp1 "]" SKIP
"temp2=[" temp2 "]" SKIP
"temp3=[" temp3 "]" SKIP
"temp4=[" temp4 "]" SKIP
VIEW-AS ALERT-BOX.

ZSH function not working with a "Missing end of string" error

I'm teaching myself to write zsh functions and I'm stumped right away with a string error I don't understand. I have this function:
function copyToDrafts() {
print($1)
}
in my command line editor (Terminal) I type:
copyToDrafts "test"
and receive this error:
copyToDrafts:1: missing end of string
I couldn't find any explanation on the error message and can't see anything wrong with what I am passing, though obviously something is wrong. Any help would be appreciated.
The parentheses are not part of the syntax; they are interpreted as introducing a glob qualifier on the pattern print. After parameter expansion, the pattern to be evaluated is
print(test)
with the following glob qualifiers:
t - match files named print that have their sticky bits set
e execute a shell command. s acts as the delimiter, but there is no "closing" s, which produces the observed error.
You simply need to drop the parentheses.
copyToDrafts () {
print $1
}

DCL symbol syntax in OpenVMS

I am really confused by some syntax in the DCL of OpenVMS. For example, these are some of the lines which confused me:
$ wo = "write sys$output"
Does it create a symbol wo for write sys$output?
$ billing_run_number == p1
Is p1 a parameter passed to the .com file when it was executed? How many parameters can it be supplied with?
$ wo "BILLING_RUN_NUMBER = ''billing_run_number'"
Is ''abc' substituted by the content of the symbol abc? Why is it ''abc' but not 'abc'? Can we use ""?
$ if ((status .nes. "P") .and. (status .nes. "M")) .or. (ftp_status .nes. "Y")
What does .nes. mean? equal? I've also seen .ne. , .eqs. too. What is the different of them?
Why are "and" and "or" surrounded by two dots? A DCL specific syntax?
from memory: $ wo = "write sys$output" is as you say, assigning wo as an alias for "write sys$output", VMS's equivalent to Unix stdout.
.nes. is "not equal to string", compared to .ne. which is a numeric "not equals".
p1 is a (the first) parameter as you guessed. I can't remember if it goes p1 through p9, or more, or if there is no arbitrary limit. p0 might be the program name, like Python's sys.argv[0].
A command procedure accepts up to 8 parameters, called P1 .. P8.
a single quote (') interpolates the following variable name, so wo "BILLING_RUN_NUMBER = ''billing_run_number'" would output, for example, BILLING_RUN_NUMBER = '42', assuming p1 was equal to 42. I can't remember exactly how DCL knows what to do when it sees two single quotes in a row like that...
The official incantation is ''symbol' to have the actual DCL text replaced by the value of symbol
that'll get you started at least... most shops that use VMS have a few hundred pounds of documentation in 3-ring binders. ask around.
In addition to the documentation referenced above, there is also extensive information via the HELP HINTS, HELP :=, HELP =, and HELP # commands. P9-P16 became available with OpenVMS V8.4, I believe.
Also, pay careful attention to the difference between global symbols (defined with a doubled equal sign {== or :==}) and local symbols (defined with a single equal sign {= or :=}). Like in case-sensitive languages a symbol defined A = 1 is a different symbol than one defined A == 1 and the local symbol can mask references to the global symbol - also some commands like READ and INQUIRE can create symbols, but I think they are always a local symbol - verify that since I'm working from memory.\
The command SET SYMBOL /SCOPE[={LOCAL|NOLOCAL},{GLOBAL|NOGLOBAL}) can also affect whether you can see certain types of symbols.
In general, stay with local symbols whenever you can - you usually only need a global symbol if a higher level (calling) command procedure needs access, or if you need the symbol still defined when you return to interactive DCL - the exception is any program that you run that specifically reads or write or creates a global symbol - rare, but I've run into a few.
Is p1 a parameter passed to the .com
file when it was executed? How many
parameter can it be supplied with?
You can pass up to 8 parameters. Each one are defined as P1, P2... P8
If you need more than 8 parameters, you can use trick like
#my_dcl "my_p1" "my_p2" "my_p3" "my_p4" "my_p5" "my_p6" "my_p7" "my_p8 my_p9 my_p10"
In my_dcl, P8 will contain value of "my_p8 my_p9 my_p10" in one single string.
$ wo "BILLING_RUN_NUMBER =
''billing_run_number'"
Is ''abc' substituded by the content
of the symbol abc? Why is it ''abc'
but not 'abc'? Can we use ""?
$ if ((status .nes. "P") .and. (status
.nes. "M")) .or. (ftp_status .nes.
"Y")
The single quote means translate the content of the string.
So,if you define wo = "write sys$output"
you can use
wo "Hello World!"
or
'wo "Hello World!"
But what if you want to show write sys$output Hello World
If you try,
wo "'wo Hello World!"
you'll get wo 'wo Hello World!
So, you have to surround it with single quote.
The first two are a escaped single quote, the last one means to stop translation.
wo "''wo' Hello World!"
Like other script language, you can have variable variable...
var_hidden = "Hello world!"
my_var = "var_hidden"
wo 'my_var'
will print Hello world!

Comma, ')',or valid expression continuation expected

I need my VB.net to write a file containing the following line
objWriter.WriteLine ("TEXTA " (FILEA) " TEXTB")
Unfortunatly the variable (FILEA) is causing problems i now get the error
Comma, ')', or valid expression continuation expected.
Could someone explain this please?
You're not concatenating (joining) the strings proerly...
objWriter.WriteLine ("TEXTA " & FILEA & " TEXTB")
A better style to get into the habit of using is:
objWriter.WriteLine (string.format("TEXTA {0} TEXTB", FILEA))
The FILEA variable replaces the {0} placeholder in the format string. Depending on what the writer you're using is, you may have a formatted overload so you could just do:
objWriter.WriteLine ("TEXTA {0} TEXTB", FILEA)
And since you asked for an explanation;
The compiler is asking you what exactly you want it to do - you've given it 3 variables (String, variable, String) and haven't told it that you want to join them together - It's saying that after the first string "TEXTA", there should either be the closing bracket (to end the method call), a comma (to pass another parameter to the method) OR a "valid continuation expression" - ie something that tells it what to do with the next bit. in this case, you want a continuation expression, specifically an ampersand to signify "concatenate with the next 'thing'".
Presumably you're looking for string concatenation? Try this:
objWriter.WriteLine("TEXTA" & FILEA & "TEXTB");
Note that FILEA isn't exactly a conventional variable name... which leads me to suspect there may be something else you're trying to achieve. Could you give more details?

why does using "\" shows error in jython

I am trying to use a copy command for Windows and we have directories such as c:\oracle.
While trying to execute one such, we get the following error:
source_file=folder+"\"
^
SyntaxError: Lexical error at line 17, column 23. Encountered: "\r" (13), after : ""
Here folder is my path of c:\oracle and while trying to add file to it like:
source=folder+"\"+src_file
I am not able to do so. Any suggestion on how to solve this issue?
I tried with / but my copy windows calling source in os.command is getting "the syntax is incorrect" and the only way to solve it is to use \ but I am getting the above error in doing so.
Please suggest. Thanks for your help
Thanks.
Short answer:
You need:
source_file = folder + "\\" + src_file
Long answer:
The problem with
source_file = folder + "\" + src_file
is that \ is the escape character. What it's doing in this particular case is escaping the " so that it's treated as a character of the string rather than the string terminator, similar to:
source_file = folder + "X + src_file
which would have the same problem.
In other words, you're trying to construct a string consisting of ", some other text and the end of line (\r, the carriage return character). That's where your error is coming from:
Encountered: "\r" (13)
Paxdiablo is absolutely correct about why \ isn't working for you. However, you could also solve your problem by using os.path.normpath instead of trying to construct the proper platform-specific path characters yourself.
In all programming languages I know of, you can't put a quote inside a string like this: "this is a quote: "." The reason for this is that the first quote opens the string, the second then closes it (!), and then the third one opens another string - with the following two problems:
whatever is between the quotes #2 and #3 is probably not valid code;
the quote #3 is probably not being closed.
There are two common mechanisms of solving this: doubling and escaping. Escaping is far more common, and what it means is you put a special character (usually \) in front of characters that you don't want to be interpreted in their usual value. Thus, "no, *this* is a quote: \"." is a proper string, where the quote #2 is not closing the string - and the character \ does not appear.
However, now you have another problem - how do you actually make the escape character appear in a string? Simple: escape it! "This is an escape: \\!" is how you do it: the backslash #1 is the escape character, and the backslash #2 is the escapee: it will not be interpreted with its usual escape semantics, but as a simple backslash character.
Thus, your line should say this:
source=folder+"\\"+src_file
BTW: upvote for both #paxdiablo (who got in before my diatribe) and #Nick (who has a proper Pythonic way to do what you want to do)