Re: [vpp-dev] Silly programmer trick #47...
dbarach=cisco@lists.fd.io ("Dave Barach via lists.fd.io") writes: >Please don't be this person: >static clib_error_t * >my_cli_command_fn (vlib_main_t * vm, > unformat_input_t * input, > vlib_cli_command_t * cmd) >{ > while (unformat_check_input (input) !=3D UNFORMAT_END_OF_INPUT) >{ > if (unformat (input, "mystuff")) > else > return (clib_error_return (0, "unknown input '%U'", >format_unformat_error, input)); >} > return (NULL); >} >[...] >Commands coded like this work fine when typed one at a time, but they blow = >chunks when scripted... It seems possible that this incorrect pattern has propagated from vpp/src/examples/sample-plugin/sample/sample.c -- G. Paul Ziemba FreeBSD unix: 7:41AM up 11 days, 13:28, 19 users, load averages: 0.33, 0.21, 0.26 -=-=-=-=-=-=-=-=-=-=-=- Links: You receive all messages sent to this group. View/Reply Online (#17551): https://lists.fd.io/g/vpp-dev/message/17551 Mute This Topic: https://lists.fd.io/mt/77125668/21656 Group Owner: vpp-dev+ow...@lists.fd.io Unsubscribe: https://lists.fd.io/g/vpp-dev/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-
Re: [vpp-dev] Silly programmer trick #47...
Hi Dave, Thanks for the tip, I didn't realize this was causing this issue But I might not be alone on that one : $ grep -R "unformat_check_input (input) != UNFORMAT_END_OF_INPUT" ./src/ | wc -l >> 459 This might be a good use-case for trying out coccinelle. Cheers -Nathan Le ven. 25 sept. 2020 à 23:15, Dave Barach via lists.fd.io a écrit : > Please don’t be this person: > > > > static clib_error_t * > > my_cli_command_fn (vlib_main_t * vm, > >unformat_input_t * input, > >vlib_cli_command_t * cmd) > > { > > while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) > > { > > if (unformat (input, "mystuff")) > > else > > return (clib_error_return (0, "unknown input '%U'", > > format_unformat_error, input)); > > } > > return (NULL); > > } > > /* *INDENT-OFF* */ > > VLIB_CLI_COMMAND (my_command, static) = > > { > > .path = "my command", > > .function = my_cli_command_fn, > > }; > > > > Commands coded like this work fine when typed one at a time, but they blow > chunks when scripted... > > > > Script: > > my command mystuff > > comment { ouch my_cli_command_fn ate the word comment and threw up! } > > > > Instead, wrap the while(...) loop with the unformat_line_input guitar lick: > > > > elib_error_t *e = 0; > > /* Get a line of input. */ > > if (!unformat_user (input, unformat_line_input, line_input)) > > return 0; > > > > while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) > > { > > if (unformat (line_input, “mystuff”)) > > ; > > else { > > e = clib_error_return (0, "unknown input '%U'", > > format_unformat_error, input); > > goto done; > > } > > } > > > > > > done: > > unformat_free (line_input); > > return e; > > > > Thanks... Dave > > > > -=-=-=-=-=-=-=-=-=-=-=- Links: You receive all messages sent to this group. View/Reply Online (#17545): https://lists.fd.io/g/vpp-dev/message/17545 Mute This Topic: https://lists.fd.io/mt/77125668/21656 Group Owner: vpp-dev+ow...@lists.fd.io Unsubscribe: https://lists.fd.io/g/vpp-dev/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-
[vpp-dev] Silly programmer trick #47...
Please don't be this person: static clib_error_t * my_cli_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "mystuff")) else return (clib_error_return (0, "unknown input '%U'", format_unformat_error, input)); } return (NULL); } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (my_command, static) = { .path = "my command", .function = my_cli_command_fn, }; Commands coded like this work fine when typed one at a time, but they blow chunks when scripted... Script: my command mystuff comment { ouch my_cli_command_fn ate the word comment and threw up! } Instead, wrap the while(...) loop with the unformat_line_input guitar lick: elib_error_t *e = 0; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "mystuff")) ; else { e = clib_error_return (0, "unknown input '%U'", format_unformat_error, input); goto done; } } done: unformat_free (line_input); return e; Thanks... Dave -=-=-=-=-=-=-=-=-=-=-=- Links: You receive all messages sent to this group. View/Reply Online (#17532): https://lists.fd.io/g/vpp-dev/message/17532 Mute This Topic: https://lists.fd.io/mt/77125668/21656 Group Owner: vpp-dev+ow...@lists.fd.io Unsubscribe: https://lists.fd.io/g/vpp-dev/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-