On Sun, May 12, 2019 at 4:20 PM Waldek Kozaczuk <[email protected]>
wrote:

>
>
> Sent from my iPhone
>
> On May 12, 2019, at 04:58, Nadav Har'El <[email protected]> wrote:
> // This is a simple RAII class for retreiving the caller's copy of the
> global opt* variables
> // on initialization, and returning them back to the caller on destruction.
> class caller_optvars {
>     std::shared_ptr<osv::application_runtime> _runtime;
>     caller_optvars() : _runtime(sched::thread::current()->app_runtime()) {
>           if (_runtime) {
>               auto obj = _runtime->app.lib();
>               // Note that only optind and opterr are input variables.
>               optind =
> *reinterpret_cast<int*>(obj->cached_lookup("optind"));
>               opterr =
> *reinterpret_cast<int*>(obj->cached_lookup("opterr"));
>          }
>      }
>      ~caller_optvars() {
>           if (_runtime) {
>               auto obj = _runtime->app.lib();
>               // Note that only optind, optarg and optopt are output
> variables.
>               // Note: Only "optind" is both input and output. We could
> save the result of its lookup in this
>               // class. But not worth the effort.
>               *reinterpret_cast<int*>(obj->cached_lookup("optind")) =
> optind;
>               *reinterpret_cast<int*>(obj->cached_lookup("optopt")) =
> optopt;
>               *reinterpret_cast<char**>(obj->cached_lookup("optarg")) =
> optarg;
>          }
>      }
> }
>
> int getopt(int argc, char * const argv[], const char *optstring) {
>     caller_optvars guard;
>     return _getopt(argc, argv, optstring);
>
> }
>
> You could use the same RAII class for getopt_long() although I saw that
> you noticed that getopt_long()
> uses slightly different variables (doesn't use opterr and optind) so it
> can use a different class.
>
> Your approach is way more fancy than mine but I definitely like yours much
> better :-) it is always nice to learn from the examples like this.
>
> I think the assignments in both constructor and destructor need to be more
> defensive and check if symbol copy of optind or other actually exists.
>

Does obj->cached_lookup("optarg") look up only in the specific object, not
in all its dependencies (including the kernel)?
I was hoping it's the latter, but don't remember...

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/CANEVyjtV_cTQ1fmS5mLpW1%2B5mrbPVkM1Fc8cd7_LxRBuoB6L%2BQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to