xaizek / vifm-pdcurses (License: mostly public domain) (since 2019-03-20)
PDCurses 3.4 with vifm-specific patches applied (a couple were upstreamed)
<root> / os2 / pdcclip.c (ddc61d902990f21f2c4529b76cc93e4e267baadd) (4,511B) (mode 100644) [raw]
/* Public Domain Curses */

#include "pdcos2.h"

RCSID("$Id: pdcclip.c,v 1.33 2008/07/14 04:24:51 wmcbrine Exp $")

/*man-start**************************************************************

  Name:                                                         clipboard

  Synopsis:
        int PDC_getclipboard(char **contents, long *length);
        int PDC_setclipboard(const char *contents, long length);
        int PDC_freeclipboard(char *contents);
        int PDC_clearclipboard(void);

  Description:
        PDC_getclipboard() gets the textual contents of the system's 
        clipboard. This function returns the contents of the clipboard 
        in the contents argument. It is the responsibilitiy of the 
        caller to free the memory returned, via PDC_freeclipboard().
        The length of the clipboard contents is returned in the length 
        argument.

        PDC_setclipboard copies the supplied text into the system's 
        clipboard, emptying the clipboard prior to the copy.

        PDC_clearclipboard() clears the internal clipboard.

  Return Values:
        indicator of success/failure of call.
        PDC_CLIP_SUCCESS        the call was successful
        PDC_CLIP_MEMORY_ERROR   unable to allocate sufficient memory for 
                                the clipboard contents
        PDC_CLIP_EMPTY          the clipboard contains no text
        PDC_CLIP_ACCESS_ERROR   no clipboard support

  Portability                                X/Open    BSD    SYS V
        PDC_getclipboard                        -       -       -
        PDC_setclipboard                        -       -       -
        PDC_freeclipboard                       -       -       -
        PDC_clearclipboard                      -       -       -

**man-end****************************************************************/

int PDC_getclipboard(char **contents, long *length)
{
#ifndef EMXVIDEO
    HMQ hmq;
    HAB hab;
    PTIB ptib;
    PPIB ppib;
    ULONG ulRet;
    long len;
    int rc;
#endif
    PDC_LOG(("PDC_getclipboard() - called\n"));

#ifndef EMXVIDEO
    DosGetInfoBlocks(&ptib, &ppib);
    ppib->pib_ultype = 3;
    hab = WinInitialize(0);
    hmq = WinCreateMsgQueue(hab, 0);

    if (!WinOpenClipbrd(hab))
    {
        WinDestroyMsgQueue(hmq);
        WinTerminate(hab);
        return PDC_CLIP_ACCESS_ERROR;
    }

    rc = PDC_CLIP_EMPTY;

    ulRet = WinQueryClipbrdData(hab, CF_TEXT);

    if (ulRet)
    {
        len = strlen((char *)ulRet);
        *contents = malloc(len + 1);

        if (!*contents)
            rc = PDC_CLIP_MEMORY_ERROR;
        else
        {
            strcpy((char *)*contents, (char *)ulRet);
            *length = len;
            rc = PDC_CLIP_SUCCESS;
        }
    }

    WinCloseClipbrd(hab);
    WinDestroyMsgQueue(hmq);
    WinTerminate(hab);

    return rc;
#else
    return PDC_CLIP_ACCESS_ERROR;
#endif
}

int PDC_setclipboard(const char *contents, long length)
{
#ifndef EMXVIDEO
    HAB hab;
    PTIB ptib;
    PPIB ppib;
    ULONG ulRC;
    PSZ szTextOut = NULL;
    int rc;
#endif
    PDC_LOG(("PDC_setclipboard() - called\n"));

#ifndef EMXVIDEO
    DosGetInfoBlocks(&ptib, &ppib);
    ppib->pib_ultype = 3;
    hab = WinInitialize(0);

    if (!WinOpenClipbrd(hab))
    {
        WinTerminate(hab);
        return PDC_CLIP_ACCESS_ERROR;
    }

    rc = PDC_CLIP_MEMORY_ERROR;

    ulRC = DosAllocSharedMem((PVOID)&szTextOut, NULL, length + 1,
                             PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE);

    if (ulRC == 0)
    {
        strcpy(szTextOut, contents);
        WinEmptyClipbrd(hab);

        if (WinSetClipbrdData(hab, (ULONG)szTextOut, CF_TEXT, CFI_POINTER))
            rc = PDC_CLIP_SUCCESS;
        else
        {
            DosFreeMem(szTextOut);
            rc = PDC_CLIP_ACCESS_ERROR;
        }
    }

    WinCloseClipbrd(hab);
    WinTerminate(hab);

    return rc;
#else
    return PDC_CLIP_ACCESS_ERROR;
#endif
}

int PDC_freeclipboard(char *contents)
{
    PDC_LOG(("PDC_freeclipboard() - called\n"));

    if (contents)
        free(contents);

    return PDC_CLIP_SUCCESS;
}

int PDC_clearclipboard(void)
{
#ifndef EMXVIDEO
    HAB hab;
    PTIB ptib;
    PPIB ppib;
#endif
    PDC_LOG(("PDC_clearclipboard() - called\n"));

#ifndef EMXVIDEO
    DosGetInfoBlocks(&ptib, &ppib);
    ppib->pib_ultype = 3;
    hab = WinInitialize(0);

    WinEmptyClipbrd(hab);

    WinCloseClipbrd(hab);
    WinTerminate(hab);

    return PDC_CLIP_SUCCESS;
#else
    return PDC_CLIP_ACCESS_ERROR;
#endif
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm-pdcurses

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm-pdcurses

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master