⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nxprint.c

📁 linux下的终端管理工具源码
💻 C
字号:
/***************************************************************************//*                                                                         *//* Copyright (c) 2005, 2006 2X Software Ltd, http://www.2X.com., NoMachine *//*                                                                         *//* NXCLIENT, NX protocol compression and NX extensions to this software    *//* are copyright of Nomachine. Redistribution and use of the present       *//* software is allowed according to terms specified in the file LICENSE    *//* which comes in the source distribution.                                 *//*                                                                         *//* NX and NoMachine are trademarks of Medialogic S.p.A.                    *//*                                                                         *//* 2X is a trademark of 2X Software Ltd.                                   *//*                                                                         *//* All rights reserved.                                                    *//*                                                                         *//***************************************************************************/#include <cups/cups.h>#include <cups/language.h>#include "nxcups.h"void freePrinterDriversInfo(CupsPrinterDriverInfo_t *driver){  CupsPrinterDriverInfo_t *driver_info = NULL;  CupsPrinterDriverInfo_t *temp_driver_info = NULL;  if(driver == NULL) return;  for (driver_info = driver; driver_info != NULL; driver_info = temp_driver_info)  {    temp_driver_info = driver_info->next;    free(driver_info->vendor);    free(driver_info->name);    free(driver_info->driver);    free(driver_info);  }}CupsPrinterDriverInfo_t* getPrinterDrivers(){  http_t *http = NULL;  ipp_t  *req = NULL; /* ipp request */  ipp_t  *response = NULL; /* ipp answer */  ipp_attribute_t *attr = NULL; /* current attribute */  cups_lang_t *language = NULL;  char   *ppd_make     = NULL;  char   *ppd_name     = NULL;  char   *ppd_vendor   = NULL;  char   *ppd_language = NULL;    CupsPrinterDriverInfo_t *drivers = NULL;  CupsPrinterDriverInfo_t *first_driver = NULL;  //http = httpConnect(cupsServer(), 633);  http = httpConnect(cupsServer(), ippPort());  //first we create new ipp request;  req = ippNew();  req->request.op.operation_id = CUPS_GET_PPDS;  req->request.op.request_id   = 1;  language = cupsLangDefault();  ippAddString(req,IPP_TAG_OPERATION, IPP_TAG_CHARSET,     "attributes-charset",NULL, cupsLangEncoding(language));  ippAddString(req, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,    "attributes-natural-language", NULL, language->language);  ippAddString(req, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",    NULL,"ipp://localhost/printers/");  if ((response = cupsDoRequest(http,req,"/")) != NULL)  {    /*     * Loop through the device list and display them ...     */    if (response->request.status.status_code > IPP_OK_CONFLICT)    {      //fprintf(stderr,"lpinfo: cups-get-ppds failed: %s\n",      //      ippErrorString(response->request.status.status_code));      ippDelete(response);      return NULL;    }    for (attr = response->attrs; attr != NULL; attr = attr->next)    {      /*       * Skip leading attributes until we hit a PPD...       */      /* check what is there in the ... */      while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)        attr = attr->next;      if (attr == NULL)        break;      ppd_make     = NULL;      ppd_name     = NULL;      ppd_vendor   = NULL;      ppd_language = "en";      while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)      {        if (strcmp(attr->name, "ppd-make-and-model") == 0 &&          attr->value_tag == IPP_TAG_TEXT)        {          ppd_make = attr->values[0].string.text;        }        if(strcmp(attr->name, "ppd-name") == 0 &&          attr->value_tag == IPP_TAG_NAME)        {          ppd_name = attr->values[0].string.text;        }        if(strcmp(attr->name, "ppd-make") == 0 &&          attr->value_tag == IPP_TAG_TEXT)        {          ppd_vendor = attr->values[0].string.text;        }        if(strcmp(attr->name, "ppd-natural-language") == 0 &&           attr->value_tag == IPP_TAG_TEXT)        {          ppd_language = attr->values[0].string.text;        }        attr = attr->next;      }      /*       * see if we have everything needed       */      if (ppd_make == NULL || ppd_name == NULL)      {        if (attr == NULL)          break;        else          continue;      }      #ifdef DEBUG      printf("name = %s\t model= %s \n",ppd_name,ppd_make);      #endif      if(drivers == NULL){        drivers = (CupsPrinterDriverInfo_t *)malloc(sizeof(CupsPrinterDriverInfo_t));        first_driver = drivers;      }else{        drivers->next = (CupsPrinterDriverInfo_t *)malloc(sizeof(CupsPrinterDriverInfo_t));        drivers = drivers->next;      }      drivers->next = NULL;      drivers->driver = strdup(ppd_name);      drivers->name   = strdup(ppd_make);      drivers->vendor = strdup(ppd_vendor);      drivers->language = strdup(ppd_language);      if( attr == NULL)        break;    }    if(drivers) drivers->next = NULL; /* stop no more drivers */    ippDelete(response);  }  else  {    fprintf(stderr,"error|%s\n",ippErrorString(cupsLastError()));  }  return(first_driver);}void showCupsDrivers(){  CupsPrinterDriverInfo_t *drivers = NULL;  CupsPrinterDriverInfo_t *tmpDriver = NULL;  drivers = getPrinterDrivers();  for(tmpDriver = drivers ; tmpDriver != NULL ;tmpDriver = tmpDriver->next)  {    printf("driver|%s|%s|%s|%s\n",tmpDriver->vendor,tmpDriver->name,tmpDriver->driver,tmpDriver->language);  }  freePrinterDriversInfo(drivers);  drivers = NULL;}void showCupsPrinters(){  int         n = 0;  int         num_dests;  cups_dest_t *dests;  ipp_status_t *error;  num_dests = cupsGetDests(&dests);    if(num_dests == 0)  {    ipp_status_t error = cupsLastError();    if( error == IPP_OK)    {      printf("error|no printers found\n");    }    else    {      printf("error|%s\n",ippErrorString(cupsLastError()));    }  }    for ( n = 0 ; n < num_dests ; n++ )  {    printf("printer|%s\n",dests[n].name);  }      cupsFreeDests(num_dests, dests);}/**********  void *libHandle = NULL;  void *cupsHandle = NULL;    CupsPrinterDriverInfo_t * (*getDriversList)();  void (*cleanupDrivers)(CupsPrinterDriverInfo_t *);  //maybe we show here a little wait while loading driver ...  //maybe not as this will make a lot roundtrips  cupsHandle = dlopen("libcups.so",RTLD_LAZY|RTLD_GLOBAL);  if(!cupsHandle)  {    fprintf(stderr,"error: cannot load libcups.so: %s",dlerror());    exit(1);  }  libHandle = dlopen("libnxcups.so",RTLD_NOW|RTLD_GLOBAL);  if(!libHandle)  {    fprintf(stderr,"error: cannot load libnxcups.so: %s\n",dlerror());    exit(1);  }    dlerror();  *(void **)(&getDriversList) = dlsym(libHandle,"getPrinterDrivers");  *(void **)(&cleanupDrivers) = dlsym(libHandle,"freePrinterDriversInfo");    if(!getDriversList)  {    fprintf(stderr,"error2: %s\n",dlerror());    exit(1);  }  CupsPrinterDriverInfo_t *drivers = (*getDriversList)();*/int main(int argc, char *argv[]){  if(argc == 2 && strcmp(argv[1],"-l") == 0)  {    showCupsPrinters();  }   else if (argc == 2 && strcmp(argv[1],"-d") == 0)  {    showCupsDrivers();      }  else if (argc == 2 && strcmp(argv[1],"-v") == 0)  {    printf("NXPRINT Version 1.4.0\n");	     }  else   {    printf("Usage: nxprint [OPTION]\n\n");    printf("  -l\tprint the list of CUPS printers\n");    printf("  -d\tprint the list of CUPS drivers\n");    printf("  -v\tprint the version number\n");  }  fflush(stdin);  fflush(stdout);  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -